I am trying self-study Graph Theory, and now trying to understand how to find SCC in a graph. I have read several different questions/answers on SO (e.g., 1,2,3,4,5,6,7,8), but I cant find one with a complete step-by-step example I could follow.
According to CORMEN (Introduction to Algorithms), one method is:
- Call DFS(G) to compute finishing times f[u] for each vertex u
- Compute Transpose(G)
- Call DFS(Transpose(G)), but in the main loop of DFS, consider the vertices in order of decreasing f[u] (as computed in step 1)
- Output the vertices of each tree in the depth-first forest of step 3 as a separate strong connected component
Observe the following graph (question is 3.4 from here. I have found several solutions here and here, but I am trying to break this down and understand it myself.)
Step 1: Call DFS(G) to compute finishing times f[u] for each vertex u
Running DFS starting on vertex A:
Please notice RED text formatted as [Pre-Vist, Post-Visit]
Step 2: Compute Transpose(G)
Step 3. Call DFS(Transpose(G)), but in the main loop of DFS, consider the vertices in order of decreasing f[u] (as computed in step 1)
Okay, so vertices in order of decreasing post-visit(finishing times) values:
{E, B, A, H, G, I , C, D, F ,J}
So at this step, we run DFS on G^T but start with each vertex from above list:
- DFS(E): {E}
- DFS(B): {B}
- DFS(A): {A}
- DFS(H): {H, I, G}
- DFS(G): remove from list since it is already visited
- DFS(I): remove from list since it is already visited
- DFS(C): {C, J, F, D}
- DFS(J): remove from list since it is already visited
- DFS(F): remove from list since it is already visited
- DFS(D): remove from list since it is already visited
Step 4: Output the vertices of each tree in the depth-first forest of step 3 as a separate strong connected component.
So we have five strongly connected components: {E}, {B}, {A}, {H, I, G}, {C, J, F, D}
B
andE
: both have an indegree of zero and form a (degenerate) SCC all by themselves. I think the linked answers to be wrong. (beat me by 40 secs.) – Sestos