A directed graph is strongly connected if there is a path between all pairs of vertices. A strongly connected component (SCC) of a directed graph is a maximal strongly connected subgraph. For example, there are 3 SCCs in the following graph.

We have discussed Kosaraju’s algorithm for strongly connected components. The previously discussed algorithm requires two DFS traversals of a Graph. In this post, Tarjan’s algorithm is discussed that requires only one DFS traversal.

Tarjan Algorithm is based on following facts:
1. DFS search produces a DFS tree/forest
2. Strongly Connected Components form subtrees of the DFS tree.
3. If we can find head of such subtrees, we can print/store all the nodes in that subtree (including head) and that will be one SCC.
4. There is no back edge from one SCC to another (There can be cross edges, but cross edges will not be used while processing the graph).

[ad type=”banner”]

To find head of a SCC, we calculate desc and low array (as done for articulation point, bridge, biconnected component). As discussed in the previous posts, low[u] indicates earliest visited vertex (the vertex with minimum discovery time) that can be reached from subtree rooted with u. A node u is head if disc[u] = low[u].

Disc and Low Values
(click on image to see it properly)

Strongly Connected Component relates to directed graph only, but Disc and Low values relate to both directed and undirected graph, so in above pic we have taken an undirected graph.

In above Figure, we have shown a graph and it’s one of DFS tree (There could be different DFS trees on same graph depending on order in which edges are traversed).
In DFS tree, continuous arrows are tree edges and dashed arrows are back edges (DFS Tree Edges
Disc and Low values are showin in Figure for every node as (Disc/Low).
Disc: This is the time when a node is visited 1st time while DFS traversal. For nodes A, B, C, .., J in DFS tree, Disc values are 1, 2, 3, .., 10.
Low: In DFS tree, Tree edges take us forward, from ancestor node to one of it’s descendants. For example, from node C, tree edges can take us to node node G, node I etc. Back edges take us backward, from a descendant node to one of it’s ancestors. For example, from node G, Back edges take us to E or C. If we look at both Tree and Back edge together, then we can see that if we start traversal from one node, we may go down the tree via Tree edges and then go up via back edges. For example, from node E, we can go down to G and then go up to C. Similarly from E, we can go down to I or J and then go up to F. “Low” value of a node tells the topmost reachable ancestor (with minimum possible Disc value) via the subtree of that node. So for any node, Low value equal to it’s Disc value anyway (A node is ancestor of itself). Then we look into it’s subtree and see if there is any node which can take us to any of it’s ancestor. If there are multiple back edges in subtree which take us to different ancestors, then we take the one with minimum Disc value (i.e. the topmost one). If we look at node F, it has two subtrees. Subtree with node G, takes us to E and C. The other subtree takes us back to F only. Here topmost ancestor is C where F can reach and so Low value of F is 3 (The Disc value of C).
Based on above discussion, it should be clear that Low values of B, C, and D are 1 (As A is the topmost node where B, C and D can reach). In same way, Low values of E, F, G are 3 and Low values of H, I, J are 6.

[ad type=”banner”]

For any node u, when DFS starts, Low will be set to it’s Disc 1st.
Then later on DFS will be performed on each of it’s children v one by one, Low value of u can change it two case:
Case1 (Tree Edge): If node v is not visited already, then after DFS of v is complete, then minimum of low[u] and low[v] will be updated to low[u].
low[u] = min(low[u], low[v]);
Case 2 (Back Edge): When child v is already visited, then minimum of low[u] and Disc[v] will be updated to low[u].
low[u] = min(low[u], disc[v]);
In case two, can we take low[v] instead of disc[v] ?? . Answer is NO. If you can think why answer is NO, you probably understood the Low and Disc concept.

Image Source: http://www.cs.yale.edu/homes/aspnes/pinewiki/DepthFirstSearch.html
Same Low and Disc values help to solve other graph problems like articulation point, bridge and biconnected component.

To track the subtree rooted at head, we can use a stack (keep pushing node while visiting). When a head node found, pop all nodes from stack till you get head out of stack.

To make sure, we don’t consider cross edges, when we reach a node which is already visited, we should process the visited node only if it is present in stack, else ignore the node.

Following is implementation of Tarjan’s algorithm to print all SCCs.

[pastacode lang=”cpp” manual=”%2F%2F%20A%20C%2B%2B%20program%20to%20find%20strongly%20connected%20components%20in%20a%20given%0A%2F%2F%20directed%20graph%20using%20Tarjan’s%20algorithm%20(single%20DFS)%0A%23include%3Ciostream%3E%0A%23include%20%3Clist%3E%0A%23include%20%3Cstack%3E%0A%23define%20NIL%20-1%0Ausing%20namespace%20std%3B%0A%20%0A%2F%2F%20A%20class%20that%20represents%20an%20directed%20graph%0Aclass%20Graph%0A%7B%0A%20%20%20%20int%20V%3B%20%20%20%20%2F%2F%20No.%20of%20vertices%0A%20%20%20%20list%3Cint%3E%20*adj%3B%20%20%20%20%2F%2F%20A%20dynamic%20array%20of%20adjacency%20lists%0A%20%0A%20%20%20%20%2F%2F%20A%20Recursive%20DFS%20based%20function%20used%20by%20SCC()%0A%20%20%20%20void%20SCCUtil(int%20u%2C%20int%20disc%5B%5D%2C%20int%20low%5B%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20stack%3Cint%3E%20*st%2C%20bool%20stackMember%5B%5D)%3B%0Apublic%3A%0A%20%20%20%20Graph(int%20V)%3B%20%20%20%2F%2F%20Constructor%0A%20%20%20%20void%20addEdge(int%20v%2C%20int%20w)%3B%20%20%20%2F%2F%20function%20to%20add%20an%20edge%20to%20graph%0A%20%20%20%20void%20SCC()%3B%20%20%20%20%2F%2F%20prints%20strongly%20connected%20components%0A%7D%3B%0A%20%0AGraph%3A%3AGraph(int%20V)%0A%7B%0A%20%20%20%20this-%3EV%20%3D%20V%3B%0A%20%20%20%20adj%20%3D%20new%20list%3Cint%3E%5BV%5D%3B%0A%7D%0A%20%0Avoid%20Graph%3A%3AaddEdge(int%20v%2C%20int%20w)%0A%7B%0A%20%20%20%20adj%5Bv%5D.push_back(w)%3B%0A%7D%0A%20%0A%2F%2F%20A%20recursive%20function%20that%20finds%20and%20prints%20strongly%20connected%0A%2F%2F%20components%20using%20DFS%20traversal%0A%2F%2F%20u%20–%3E%20The%20vertex%20to%20be%20visited%20next%0A%2F%2F%20disc%5B%5D%20–%3E%20Stores%20discovery%20times%20of%20visited%20vertices%0A%2F%2F%20low%5B%5D%20–%20%3E%3E%20earliest%20visited%20vertex%20(the%20vertex%20with%20minimum%0A%2F%2F%20%20%20%20%20%20%20%20%20%20%20%20%20discovery%20time)%20that%20can%20be%20reached%20from%20subtree%0A%2F%2F%20%20%20%20%20%20%20%20%20%20%20%20%20rooted%20with%20current%20vertex%0A%2F%2F%20*st%20–%20%3E%3E%20To%20store%20all%20the%20connected%20ancestors%20(could%20be%20part%0A%2F%2F%20%20%20%20%20%20%20%20%20%20%20of%20SCC)%0A%2F%2F%20stackMember%5B%5D%20–%3E%20bit%2Findex%20array%20for%20faster%20check%20whether%0A%2F%2F%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20a%20node%20is%20in%20stack%0Avoid%20Graph%3A%3ASCCUtil(int%20u%2C%20int%20disc%5B%5D%2C%20int%20low%5B%5D%2C%20stack%3Cint%3E%20*st%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20bool%20stackMember%5B%5D)%0A%7B%0A%20%20%20%20%2F%2F%20A%20static%20variable%20is%20used%20for%20simplicity%2C%20we%20can%20avoid%20use%0A%20%20%20%20%2F%2F%20of%20static%20variable%20by%20passing%20a%20pointer.%0A%20%20%20%20static%20int%20time%20%3D%200%3B%0A%20%0A%20%20%20%20%2F%2F%20Initialize%20discovery%20time%20and%20low%20value%0A%20%20%20%20disc%5Bu%5D%20%3D%20low%5Bu%5D%20%3D%20%2B%2Btime%3B%0A%20%20%20%20st-%3Epush(u)%3B%0A%20%20%20%20stackMember%5Bu%5D%20%3D%20true%3B%0A%20%0A%20%20%20%20%2F%2F%20Go%20through%20all%20vertices%20adjacent%20to%20this%0A%20%20%20%20list%3Cint%3E%3A%3Aiterator%20i%3B%0A%20%20%20%20for%20(i%20%3D%20adj%5Bu%5D.begin()%3B%20i%20!%3D%20adj%5Bu%5D.end()%3B%20%2B%2Bi)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20v%20%3D%20*i%3B%20%20%2F%2F%20v%20is%20current%20adjacent%20of%20’u’%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20If%20v%20is%20not%20visited%20yet%2C%20then%20recur%20for%20it%0A%20%20%20%20%20%20%20%20if%20(disc%5Bv%5D%20%3D%3D%20-1)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20SCCUtil(v%2C%20disc%2C%20low%2C%20st%2C%20stackMember)%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Check%20if%20the%20subtree%20rooted%20with%20’v’%20has%20a%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20connection%20to%20one%20of%20the%20ancestors%20of%20’u’%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Case%201%20(per%20above%20discussion%20on%20Disc%20and%20Low%20value)%0A%20%20%20%20%20%20%20%20%20%20%20%20low%5Bu%5D%20%20%3D%20min(low%5Bu%5D%2C%20low%5Bv%5D)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Update%20low%20value%20of%20’u’%20only%20of%20’v’%20is%20still%20in%20stack%0A%20%20%20%20%20%20%20%20%2F%2F%20(i.e.%20it’s%20a%20back%20edge%2C%20not%20cross%20edge).%0A%20%20%20%20%20%20%20%20%2F%2F%20Case%202%20(per%20above%20discussion%20on%20Disc%20and%20Low%20value)%0A%20%20%20%20%20%20%20%20else%20if%20(stackMember%5Bv%5D%20%3D%3D%20true)%0A%20%20%20%20%20%20%20%20%20%20%20%20low%5Bu%5D%20%20%3D%20min(low%5Bu%5D%2C%20disc%5Bv%5D)%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20head%20node%20found%2C%20pop%20the%20stack%20and%20print%20an%20SCC%0A%20%20%20%20int%20w%20%3D%200%3B%20%20%2F%2F%20To%20store%20stack%20extracted%20vertices%0A%20%20%20%20if%20(low%5Bu%5D%20%3D%3D%20disc%5Bu%5D)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20while%20(st-%3Etop()%20!%3D%20u)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20w%20%3D%20(int)%20st-%3Etop()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20cout%20%3C%3C%20w%20%3C%3C%20%22%20%22%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20stackMember%5Bw%5D%20%3D%20false%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20st-%3Epop()%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20w%20%3D%20(int)%20st-%3Etop()%3B%0A%20%20%20%20%20%20%20%20cout%20%3C%3C%20w%20%3C%3C%20%22%5Cn%22%3B%0A%20%20%20%20%20%20%20%20stackMember%5Bw%5D%20%3D%20false%3B%0A%20%20%20%20%20%20%20%20st-%3Epop()%3B%0A%20%20%20%20%7D%0A%7D%0A%20%0A%2F%2F%20The%20function%20to%20do%20DFS%20traversal.%20It%20uses%20SCCUtil()%0Avoid%20Graph%3A%3ASCC()%0A%7B%0A%20%20%20%20int%20*disc%20%3D%20new%20int%5BV%5D%3B%0A%20%20%20%20int%20*low%20%3D%20new%20int%5BV%5D%3B%0A%20%20%20%20bool%20*stackMember%20%3D%20new%20bool%5BV%5D%3B%0A%20%20%20%20stack%3Cint%3E%20*st%20%3D%20new%20stack%3Cint%3E()%3B%0A%20%0A%20%20%20%20%2F%2F%20Initialize%20disc%20and%20low%2C%20and%20stackMember%20arrays%0A%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20V%3B%20i%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20disc%5Bi%5D%20%3D%20NIL%3B%0A%20%20%20%20%20%20%20%20low%5Bi%5D%20%3D%20NIL%3B%0A%20%20%20%20%20%20%20%20stackMember%5Bi%5D%20%3D%20false%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Call%20the%20recursive%20helper%20function%20to%20find%20strongly%0A%20%20%20%20%2F%2F%20connected%20components%20in%20DFS%20tree%20with%20vertex%20’i’%0A%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20V%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20if%20(disc%5Bi%5D%20%3D%3D%20NIL)%0A%20%20%20%20%20%20%20%20%20%20%20%20SCCUtil(i%2C%20disc%2C%20low%2C%20st%2C%20stackMember)%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20function%0Aint%20main()%0A%7B%0A%20%20%20%20cout%20%3C%3C%20%22%5CnSCCs%20in%20first%20graph%20%5Cn%22%3B%0A%20%20%20%20Graph%20g1(5)%3B%0A%20%20%20%20g1.addEdge(1%2C%200)%3B%0A%20%20%20%20g1.addEdge(0%2C%202)%3B%0A%20%20%20%20g1.addEdge(2%2C%201)%3B%0A%20%20%20%20g1.addEdge(0%2C%203)%3B%0A%20%20%20%20g1.addEdge(3%2C%204)%3B%0A%20%20%20%20g1.SCC()%3B%0A%20%0A%20%20%20%20cout%20%3C%3C%20%22%5CnSCCs%20in%20second%20graph%20%5Cn%22%3B%0A%20%20%20%20Graph%20g2(4)%3B%0A%20%20%20%20g2.addEdge(0%2C%201)%3B%0A%20%20%20%20g2.addEdge(1%2C%202)%3B%0A%20%20%20%20g2.addEdge(2%2C%203)%3B%0A%20%20%20%20g2.SCC()%3B%0A%20%0A%20%20%20%20cout%20%3C%3C%20%22%5CnSCCs%20in%20third%20graph%20%5Cn%22%3B%0A%20%20%20%20Graph%20g3(7)%3B%0A%20%20%20%20g3.addEdge(0%2C%201)%3B%0A%20%20%20%20g3.addEdge(1%2C%202)%3B%0A%20%20%20%20g3.addEdge(2%2C%200)%3B%0A%20%20%20%20g3.addEdge(1%2C%203)%3B%0A%20%20%20%20g3.addEdge(1%2C%204)%3B%0A%20%20%20%20g3.addEdge(1%2C%206)%3B%0A%20%20%20%20g3.addEdge(3%2C%205)%3B%0A%20%20%20%20g3.addEdge(4%2C%205)%3B%0A%20%20%20%20g3.SCC()%3B%0A%20%0A%20%20%20%20cout%20%3C%3C%20%22%5CnSCCs%20in%20fourth%20graph%20%5Cn%22%3B%0A%20%20%20%20Graph%20g4(11)%3B%0A%20%20%20%20g4.addEdge(0%2C1)%3Bg4.addEdge(0%2C3)%3B%0A%20%20%20%20g4.addEdge(1%2C2)%3Bg4.addEdge(1%2C4)%3B%0A%20%20%20%20g4.addEdge(2%2C0)%3Bg4.addEdge(2%2C6)%3B%0A%20%20%20%20g4.addEdge(3%2C2)%3B%0A%20%20%20%20g4.addEdge(4%2C5)%3Bg4.addEdge(4%2C6)%3B%0A%20%20%20%20g4.addEdge(5%2C6)%3Bg4.addEdge(5%2C7)%3Bg4.addEdge(5%2C8)%3Bg4.addEdge(5%2C9)%3B%0A%20%20%20%20g4.addEdge(6%2C4)%3B%0A%20%20%20%20g4.addEdge(7%2C9)%3B%0A%20%20%20%20g4.addEdge(8%2C9)%3B%0A%20%20%20%20g4.addEdge(9%2C8)%3B%0A%20%20%20%20g4.SCC()%3B%0A%20%0A%20%20%20%20cout%20%3C%3C%20%22%5CnSCCs%20in%20fifth%20graph%20%5Cn%22%3B%0A%20%20%20%20Graph%20g5(5)%3B%0A%20%20%20%20g5.addEdge(0%2C1)%3B%0A%20%20%20%20g5.addEdge(1%2C2)%3B%0A%20%20%20%20g5.addEdge(2%2C3)%3B%0A%20%20%20%20g5.addEdge(2%2C4)%3B%0A%20%20%20%20g5.addEdge(3%2C0)%3B%0A%20%20%20%20g5.addEdge(4%2C2)%3B%0A%20%20%20%20g5.SCC()%3B%0A%20%0A%20%20%20%20return%200%3B%0A%7D” message=”” highlight=”” provider=”manual”/] [ad type=”banner”] Output:

SCCs in first graph
4
3
1 2 0

SCCs in second graph
3
2
1
0

SCCs in third graph
5
3
4
6
2 1 0

SCCs in fourth graph
8 9
7
5 4 6
3 2 1 0
10

SCCs in fifth graph
4 3 2 1 0