Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1. Convert to the DAG by merging together nodes in the same strongly connected component in a supernode.
- 2. If there are two or more leaf supernodes, output any one node from two different leaf supernodes.
- 3. Otherwise, remove the leaf supernode from the graph and repeat (2)
- 4. Otherwise, there is no such pair.
- Suppose the algorithm finds (A,B). If they became leaves at some point, there is no path from A to B, as algorithm removes nodes in a topological order (so no path can go into the removed part of the graph and back).
- If at some step there is only one leaf supernode X, no node N from X can be part of an answer, as there is a path from each other node to N. Removing edges from the graph never invalidates a correct answer: if (A,B) is an answer for G, then it is also a valid answer for G minus edge E. So we can't lose any answer by removing a leaf supernode X.
- As graph is a DAG, there are always leaf nodes.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement