The Algorithms logo
The Algorithms

Tarjans Algorithm

package com.thealgorithms.datastructures.graphs;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

 * Java program that implements Tarjan's Algorithm.
 * @author <a href="">Shivanagouda S A</a>
 * <p>
 * Tarjan's algorithm is a linear time algorithm to find the strongly connected components of a
directed graph, which, from here onwards will be referred as SCC.

 * A graph is said to be strongly connected if every vertex is reachable from every other vertex.
The SCCs of a directed graph form a partition into subgraphs that are themselves strongly
connected. Single node is always a SCC.

 * Example:
0 --------> 1 -------> 3 --------> 4
^          /
|         /
|        /
|       /
|      /
|     /
|    /
|   /
|  /
| /

For the above graph, the SCC list goes as follows:
1, 2, 0

We can also see that order of the nodes in an SCC doesn't matter since they are in cycle.

Tarjan's Algorithm:
 * DFS search produces a DFS tree
 * Strongly Connected Components form subtrees of the DFS tree.
 * If we can find the head of these subtrees, we can get 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 (here can be cross edges, but they will not be

 * Kosaraju Algorithm aims at doing the same but uses two DFS traversalse whereas Tarjan’s
algorithm does the same in a single DFS, which leads to much lower constant factors in the latter.

public class TarjansAlgorithm {

    // Timer for tracking lowtime and insertion time
    private int time;

    private final List<List<Integer>> sccList = new ArrayList<List<Integer>>();

    public List<List<Integer>> stronglyConnectedComponents(int v, List<List<Integer>> graph) {

        // Initially all vertices as unvisited, insertion and low time are undefined

        // insertionTime:Time when a node is visited 1st time while DFS traversal

        // lowTime: indicates the earliest visited vertex (the vertex with minimum insertion time)
        // that can be reached from a subtree rooted with a particular node.
        int[] lowTime = new int[v];
        int[] insertionTime = new int[v];
        for (int i = 0; i < v; i++) {
            insertionTime[i] = -1;
            lowTime[i] = -1;

        // To check if element is present in stack
        boolean[] isInStack = new boolean[v];

        // Store nodes during DFS
        Stack<Integer> st = new Stack<Integer>();

        for (int i = 0; i < v; i++) {
            if (insertionTime[i] == -1) {
                stronglyConnCompsUtil(i, lowTime, insertionTime, isInStack, st, graph);

        return sccList;

    private void stronglyConnCompsUtil(int u, int[] lowTime, int[] insertionTime, boolean[] isInStack, Stack<Integer> st, List<List<Integer>> graph) {

        // Initialize insertion time and lowTime value of current node
        insertionTime[u] = time;
        lowTime[u] = time;
        time += 1;

        // Push current node into stack
        isInStack[u] = true;

        // Go through all vertices adjacent to this
        for (Integer vertex : graph.get(u)) {
            // If the adjacent node is unvisited, do DFS
            if (insertionTime[vertex] == -1) {
                stronglyConnCompsUtil(vertex, lowTime, insertionTime, isInStack, st, graph);
                // update lowTime for the current node comparing lowtime of adj node
                lowTime[u] = Math.min(lowTime[u], lowTime[vertex]);
            } else if (isInStack[vertex]) {
                // If adj node is in stack, update low
                lowTime[u] = Math.min(lowTime[u], insertionTime[vertex]);
        // If lowtime and insertion time are same, current node is the head of an SCC
        //  head node found, get all the nodes in this SCC
        if (lowTime[u] == insertionTime[u]) {
            int w = -1;
            var scc = new ArrayList<Integer>();

            // Stack has all the nodes of the current SCC
            while (w != u) {
                w = st.pop();
                isInStack[w] = false;