An SCC is the maximum set of vertices such that every vertex can reach every vertex. A graph and it's reverse have the same SCCs.
Algorithm
- Generate the reverse graph (
) - Reverse all the edges (u->v turn into v->u)
- Execute DFS of
and save the post-order traversal - Every time a vertex reaches the end of its recursive call, add it to the list
- Execute DFS of
and visit the nodes in reversed-post-order traversal (the reverse of the previous result). - Each resulting tree is a new SCC.
Code
The code is way to messy to put here