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.Â
C++
#include<iostream>#include<vector>#include<stack>#include<algorithm>usingnamespacestd;// A recursive DFS based function used by getSCCs()// u -> The vertex to be visited next// disc[] -> Stores discovery times of visited vertices// low[] -> Earliest visited vertex that can be reached// from subtree rooted with current vertex// st -> Stack to store all active DFS vertices// inSt[] -> Boolean array to check whether a node is in stack// timer -> Global time counter for discovery times// allSCCs -> Stores all strongly connected componentsvoidfindSCC(intu,vector<vector<int>>&adj,vector<int>&disc,vector<int>&low,vector<bool>&inSt,stack<int>&st,int&timer,vector<vector<int>>&allSCCs){// Initialize discovery time and low valuedisc[u]=low[u]=++timer;// Push current vertex to stack and mark it as in stackst.push(u);inSt[u]=true;// Go through all vertices adjacent to thisfor(intv:adj[u]){// If v is not visited yet, then recur for it// Case 1: Tree edgeif(disc[v]==-1){findSCC(v,adj,disc,low,inSt,st,timer,allSCCs);// Check if the subtree rooted with v has a// connection to one of the ancestors of ulow[u]=min(low[u],low[v]);}// Update low value of u only if v is still in stack// Case 2: Back edge (not cross edge)elseif(inSt[v]){low[u]=min(low[u],disc[v]);}}// If u is head node of SCC, pop the stack and store the SCCif(low[u]==disc[u]){vector<int>scc;// Pop all vertices from stack till u is foundwhile(true){intx=st.top();st.pop();inSt[x]=false;scc.push_back(x);if(x==u)break;}// Store one strongly connected componentallSCCs.push_back(scc);}}// The function to do DFS traversal.// It uses findSCC() to find all strongly connected componentsvector<vector<int>>getSCCs(vector<vector<int>>&adj){intn=adj.size();vector<int>disc(n,-1);vector<int>low(n,-1);vector<bool>inSt(n,false);stack<int>st;inttimer=0;vector<vector<int>>allSCCs;// Call the recursive helper function to find SCCs// in DFS tree with vertex ifor(inti=0;i<n;i++){if(disc[i]==-1){findSCC(i,adj,disc,low,inSt,st,timer,allSCCs);}}returnallSCCs;}intmain(){intn=6;vector<vector<int>>adj(n);// Graph constructionadj[0].push_back(1);adj[1].push_back(2);adj[2].push_back(0);adj[2].push_back(3);adj[3].push_back(4);adj[4].push_back(3);adj[4].push_back(5);vector<vector<int>>sccs=getSCCs(adj);cout<<"Strongly Connected Components:\n";for(auto&scc:sccs){for(intnode:scc){cout<<node<<" ";}cout<<"\n";}return0;}
Java
importjava.util.ArrayList;importjava.util.Stack;importjava.util.Arrays;classGFG{// A recursive DFS based function used by getSCCs()// u -> The vertex to be visited next// disc[] -> Stores discovery times of visited vertices// low[] -> Earliest visited vertex that can be reached// from subtree rooted with current vertex// st -> Stack to store all active DFS vertices// inSt[] -> Boolean array to check whether a node is in stack// timer -> Global time counter for discovery times// allSCCs -> Stores all strongly connected componentsstaticvoidfindSCC(intu,int[][]adj,int[]disc,int[]low,boolean[]inSt,Stack<Integer>st,int[]timer,ArrayList<ArrayList<Integer>>allSCCs){// Initialize discovery time and low valuedisc[u]=low[u]=++timer[0];// Push current vertex to stack and mark it as in stackst.push(u);inSt[u]=true;// Go through all vertices adjacent to thisfor(intv:adj[u]){// If v is not visited yet, then recur for it// Case 1: Tree edgeif(disc[v]==-1){findSCC(v,adj,disc,low,inSt,st,timer,allSCCs);// Check if the subtree rooted with v has a// connection to one of the ancestors of ulow[u]=Math.min(low[u],low[v]);}// Update low value of u only if v is still in stack// Case 2: Back edge (not cross edge)elseif(inSt[v]){low[u]=Math.min(low[u],disc[v]);}}// If u is head node of SCC, pop the stack and store the SCCif(low[u]==disc[u]){ArrayList<Integer>scc=newArrayList<>();// Pop all vertices from stack till u is foundwhile(true){intx=st.pop();inSt[x]=false;scc.add(x);if(x==u)break;}// Store one strongly connected componentallSCCs.add(scc);}}// The function to do DFS traversal.// It uses findSCC() to find all strongly connected componentsstaticArrayList<ArrayList<Integer>>getSCCs(int[][]adj){intn=adj.length;int[]disc=newint[n];int[]low=newint[n];boolean[]inSt=newboolean[n];Arrays.fill(disc,-1);Stack<Integer>st=newStack<>();int[]timer=newint[1];ArrayList<ArrayList<Integer>>allSCCs=newArrayList<>();// Call the recursive helper function to find SCCs// in DFS tree with vertex ifor(inti=0;i<n;i++){if(disc[i]==-1){findSCC(i,adj,disc,low,inSt,st,timer,allSCCs);}}returnallSCCs;}publicstaticvoidmain(String[]args){int[][]adj=newint[6][];adj[0]=newint[]{1};adj[1]=newint[]{2};adj[2]=newint[]{0,3};adj[3]=newint[]{4};adj[4]=newint[]{3,5};adj[5]=newint[]{};ArrayList<ArrayList<Integer>>sccs=getSCCs(adj);System.out.println("Strongly Connected Components:");for(ArrayList<Integer>scc:sccs){for(intx:scc){System.out.print(x+" ");}System.out.println();}}}
Python
# A recursive DFS based function used by getSCCs()# u -> The vertex to be visited next# disc[] -> Stores discovery times of visited vertices# low[] -> Earliest visited vertex that can be reached# from subtree rooted with current vertex# st -> Stack to store all active DFS vertices# inSt[] -> Boolean array to check whether a node is in stack# timer -> Global time counter for discovery times# allSCCs -> Stores all strongly connected componentsdeffindSCC(u,adj,disc,low,inSt,st,timer,allSCCs):# Initialize discovery time and low valuetimer[0]+=1disc[u]=low[u]=timer[0]# Push current vertex to stack and mark it as in stackst.append(u)inSt[u]=True# Go through all vertices adjacent to thisforvinadj[u]:# If v is not visited yet, then recur for it# Case 1: Tree edgeifdisc[v]==-1:findSCC(v,adj,disc,low,inSt,st,timer,allSCCs)# Check if the subtree rooted with v has a# connection to one of the ancestors of ulow[u]=min(low[u],low[v])# Update low value of u only if v is still in stack# Case 2: Back edge (not cross edge)elifinSt[v]:low[u]=min(low[u],disc[v])# If u is head node of SCC, pop the stack and store the SCCiflow[u]==disc[u]:scc=[]# Pop all vertices from stack till u is foundwhileTrue:x=st.pop()inSt[x]=Falsescc.append(x)ifx==u:break# Store one strongly connected componentallSCCs.append(scc)# The function to do DFS traversal.# It uses findSCC() to find all strongly connected componentsdefgetSCCs(adj):n=len(adj)disc=[-1]*nlow=[-1]*ninSt=[False]*nst=[]timer=[0]allSCCs=[]# Call the recursive helper function to find SCCs# in DFS tree with vertex iforiinrange(n):ifdisc[i]==-1:findSCC(i,adj,disc,low,inSt,st,timer,allSCCs)returnallSCCsif__name__=="__main__":adj=[[]for_inrange(6)]# Graph constructionadj[0].append(1)adj[1].append(2)adj[2].append(0)adj[2].append(3)adj[3].append(4)adj[4].append(3)adj[4].append(5)sccs=getSCCs(adj)print("Strongly Connected Components:")forsccinsccs:print(*scc)
C#
usingSystem;usingSystem.Collections.Generic;classGFG{// A recursive DFS based function used by getSCCs()// u -> The vertex to be visited next// disc[] -> Stores discovery times of visited vertices// low[] -> Earliest visited vertex that can be reached// from subtree rooted with current vertex// st -> Stack to store all active DFS vertices// inSt[] -> Boolean array to check whether a node is in stack// timer -> Global time counter for discovery times// allSCCs -> Stores all strongly connected componentsstaticvoidfindSCC(intu,List<List<int>>adj,int[]disc,int[]low,bool[]inSt,Stack<int>st,refinttimer,List<List<int>>allSCCs){// Initialize discovery time and low valuedisc[u]=low[u]=++timer;// Push current vertex to stack and mark it as in stackst.Push(u);inSt[u]=true;// Go through all vertices adjacent to thisforeach(intvinadj[u]){// If v is not visited yet, then recur for it// Case 1: Tree edgeif(disc[v]==-1){findSCC(v,adj,disc,low,inSt,st,reftimer,allSCCs);// Check if the subtree rooted with v has a// connection to one of the ancestors of ulow[u]=Math.Min(low[u],low[v]);}// Update low value of u only if v is still in stack// Case 2: Back edge (not cross edge)elseif(inSt[v]){low[u]=Math.Min(low[u],disc[v]);}}// If u is head node of SCC, pop the stack and store the SCCif(low[u]==disc[u]){List<int>scc=newList<int>();// Pop all vertices from stack till u is foundwhile(true){intx=st.Pop();inSt[x]=false;scc.Add(x);if(x==u)break;}// Store one strongly connected componentallSCCs.Add(scc);}}// The function to do DFS traversal.// It uses findSCC() to find all strongly connected componentsstaticList<List<int>>getSCCs(List<List<int>>adj){intn=adj.Count;int[]disc=newint[n];int[]low=newint[n];bool[]inSt=newbool[n];for(inti=0;i<n;i++)disc[i]=-1;Stack<int>st=newStack<int>();inttimer=0;List<List<int>>allSCCs=newList<List<int>>();// Call the recursive helper function to find SCCs// in DFS tree with vertex ifor(inti=0;i<n;i++){if(disc[i]==-1){findSCC(i,adj,disc,low,inSt,st,reftimer,allSCCs);}}returnallSCCs;}staticvoidMain(){intn=6;// Adjacency list using arraysint[][]adjArr=newint[n][];adjArr[0]=newint[]{1};adjArr[1]=newint[]{2};adjArr[2]=newint[]{0,3};adjArr[3]=newint[]{4};adjArr[4]=newint[]{3,5};adjArr[5]=newint[]{};// Convert int[][] to List<List<int>>varadj=newList<List<int>>();for(inti=0;i<n;i++){adj.Add(newList<int>(adjArr[i]));}varsccs=getSCCs(adj);Console.WriteLine("Strongly Connected Components:");foreach(varsccinsccs){foreach(intxinscc){Console.Write(x+" ");}Console.WriteLine();}}}
JavaScript
// A recursive DFS based function used by getSCCs()// u -> The vertex to be visited next// disc[] -> Stores discovery times of visited vertices// low[] -> Earliest visited vertex that can be reached// from subtree rooted with current vertex// st -> Stack to store all active DFS vertices// inSt[] -> Boolean array to check whether a node is in stack// timer -> Global time counter for discovery times// allSCCs -> Stores all strongly connected componentsfunctionfindSCC(u,adj,disc,low,inSt,st,timer,allSCCs){// Initialize discovery time and low valuetimer.val++;disc[u]=low[u]=timer.val;// Push current vertex to stack and mark it as in stackst.push(u);inSt[u]=true;// Go through all vertices adjacent to thisfor(letvofadj[u]){// If v is not visited yet, then recur for it// Case 1: Tree edgeif(disc[v]===-1){findSCC(v,adj,disc,low,inSt,st,timer,allSCCs);// Check if the subtree rooted with v has a// connection to one of the ancestors of ulow[u]=Math.min(low[u],low[v]);}// Update low value of u only if v is still in stack// Case 2: Back edge (not cross edge)elseif(inSt[v]){low[u]=Math.min(low[u],disc[v]);}}// If u is head node of SCC, pop the stack and store the SCCif(low[u]===disc[u]){letscc=[];// Pop all vertices from stack till u is foundwhile(true){letx=st.pop();inSt[x]=false;scc.push(x);if(x===u)break;}// Store one strongly connected componentallSCCs.push(scc);}}functiongetSCCs(adj){letn=adj.length;letdisc=Array(n).fill(-1);letlow=Array(n).fill(-1);letinSt=Array(n).fill(false);letst=[];lettimer={val:0};letallSCCs=[];// Call the recursive helper function to find SCCs// in DFS tree with vertex ifor(leti=0;i<n;i++){if(disc[i]===-1){findSCC(i,adj,disc,low,inSt,st,timer,allSCCs);}}returnallSCCs;}// Driver Codeletadj=Array.from({length:6},()=>[]);adj[0].push(1);adj[1].push(2);adj[2].push(0);adj[2].push(3);adj[3].push(4);adj[4].push(3);adj[4].push(5);letsccs=getSCCs(adj);console.log("Strongly Connected Components:");for(letsccofsccs){console.log(scc.join(" "));}
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)