Korasaju’s Algorithm
Is a linear time algorithm to find the strongly connected components of a directed graph.
Definition: for a Directed Graph , the reverse (or transpose) graph is the graph with same vertices, and reversed edges.
Basically run DFS twice.
You can always turn directed graphs into SCCs
Consider the case where we just have vertices and zero edges. Every vertex itself is a SCC. Then if we add edges, it is possible we create a larger SCC, thus decreasing the number of them. Note that the minimum number of SCCs (assuming at least one vertex exists) is 1. That case is when every vertex is connected with each other.
Complexity: (don’t forget the time to reverse )
Exercise: check that the strongly connected components of and are the same.
Correctness Want to prove: for any vertices , the following are equivalent:
- and are in the same strongly connected components of
- and are in the same tree in the DFS forest of (with vertices ordered in decreasing finish time)
Proof of 1 2 (order of the vertices does not matter here).
Let be the strongly connected component of that contains and .
Another question:
Apparently is always a DAG. Start with a graph , next compute the SCCs of . We construct a graph , whose vertex set is the SCCs of , and we add a directed edge from an SCC to an SCC if and only if there exist vertices such that is an edge in . The claim is that is a DAG.