Tarjan's Algorithm to find Strongly Connected Components
Last Updated : 14 Mar, 2026
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 the following facts:
DFS search produces a DFS tree/forest
Strongly Connected Components form subtrees of the DFS tree.
If we can find the head of such subtrees, we can print/store all the nodes in that subtree (including the head) and that will be one SCC.
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).
To find the head of an SCC, we calculate the disc and low array (as done for articulation point, bridge, and biconnected component). As discussed in the previous posts, low[u] indicates the earliest visited vertex (the vertex with minimum discovery time) that can be reached from a subtree rooted with u. A node u is head if disc[u] = low[u]
Below is an illustration of the above approach:
To solve the problem follow the below idea:
Strongly Connected Component relates to directed graph only, but Disc and Low values relate to both directed and undirected graph, so in the above pic we have taken an undirected graph.
In the above Figure, we have shown a graph and one of the DFS trees (There could be different DFS trees on the same graph depending on the order in which edges are traversed). In a DFS tree, continuous arrows are tree edges, and dashed arrows are back edges (DFS Tree Edges ). Disc and Low values are shown in the 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, .., and J in the DFS tree, Disc values are 1, 2, 3, .., 10.
Low: In the DFS tree, Tree edges take us forward, from the ancestor node to one of its descendants. For example, from node C, tree edges can take us to node G, node I, etc. Back edges take us backward, from a descendant node to one of its ancestors.
For example: From node G, the Back edges take us to E or C. If we look at both the Tree and Back edges 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, a Low value is equal to its Disc value anyway (A node is the ancestor of itself). Then we look into its subtree and see if there is any node that can take us to any of its ancestors.
If there are multiple back edges in the subtree that take us to different ancestors, then we take the one with the 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 the Low value of F is 3 (The Disc value of C).
Based on the above discussion, it should be clear that the Low values of B, C, and D are 1 (As A is the topmost node where B, C, and D can reach). In the same way, the Low values of E, F, and G are 3, and the Low values of H, I, and J are 6. For any node u, when DFS starts, Low will be set to its Disc 1st
Then later on DFS will be performed on each of its children v one by one, Low value of u can change in two cases:
Case1 (Tree Edge): If node v is not visited already, then after the DFS of v is complete, a 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 a 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 the disc[v] ?? The answer is NO. If you can think why the answer is NO, you probably understood the Low and Disc concept.
Same Low and Disc values help to solve other graph problems like articulation point, bridge, and biconnected component. To track the subtree rooted at the head, we can use a stack (keep pushing the node while visiting). When a head node is found, pop all nodes from the stack till you get the head out of the stack. To make sure, we don't consider cross edges, when we reach a node that is already visited, we should process the visited node only if it is present in the stack, or else ignore the node.
Below is the implementation of Tarjan's algorithm to print all SCCs.
Output
Strongly Connected Components:
5
4 3
2 1 0
Time Complexity: The above algorithm mainly calls DFS, DFS takes O(V+E) for a graph represented using an adjacency list. Auxiliary Space: O(V)