ProCodebaseProCodebase
  • ModusA Growth OS for your business, on WhatsAppKiosqSell more. Chase less.AI InterviewerAutomated screening & AI-led interviewsXperto AIAI prep companion for candidatesAI Tools HubResume builder, learning paths & more
  • Pre-Vetted DevelopersScreened, scored and ready to interviewAI-Native DevelopersSenior engineers on an hourly basis
  • Services
  • Features
  • Jobs
  • FAQs
Sign inBook a demo
  • Services
  • Features
  • Jobs
  • FAQs
Sign inBook a demo
ProCodebaseProCodebase

ProCodebase Technologies builds AI products for hiring and growth, and ships software for clients as a technical consultancy. We source, screen and deliver pre-vetted developers — so you only interview high-signal candidates.

Products

  • Modus
  • Kiosq
  • AI Interviewer
  • Xperto AI
  • AI Tools Hub

Hire & build

  • Pre-Vetted Developers
  • AI-Native Developers
  • Technical Consultancy
  • MVP Development
  • Features

Resources

  • Articles
  • Topics
  • Certifications
  • Collections
  • Jobs

Company

  • About Us
  • Contact Us
  • Book a Demo
  • FAQs

© 2026 ProCodebase Technologies. All rights reserved.

  • Privacy Policy
  • Terms & Conditions
  • Refund & Cancellation

Level Up Your Skills with Xperto-AI

A multi-AI agent platform that helps you level up your development skills and ace your interview preparation to secure your dream job.

Launch Xperto-AI

Understanding Tarjan's Algorithm for Finding Strongly Connected Components in Graphs

author
Generated by
ProCodebase AI

16/11/2024

Tarjan's Algorithm

Sign in to read full article

Introduction to Strongly Connected Components

Before we dive into Tarjan's Algorithm, let's clarify the concept of strongly connected components. In a directed graph, a strongly connected component is a subgraph where every vertex is reachable from every other vertex within that component. For example, in a directed cycle of three nodes A -> B -> C -> A, all nodes form a strongly connected component.

Detecting SCCs is crucial in various applications, from social network analysis to optimizing compilers. For instance, identifying cycles in a dependency graph can help resolve dependencies efficiently.

What is Tarjan's Algorithm?

Tarjan's Algorithm, introduced by Robert Tarjan in 1972, is a depth-first search (DFS)-based algorithm that efficiently counts and identifies all the strongly connected components in a directed graph in linear time, i.e., O(V + E), where V is the number of vertices and E is the number of edges. The key feature of Tarjan's algorithm is the use of a single depth-first traversal while maintaining a stack to track the nodes.

Key Components of the Algorithm

  1. DFS Traversal: The algorithm relies heavily on a depth-first search to explore the graph.
  2. Indexing & Low-link Values: Each node is assigned an index representing the order in which it was discovered. The low-link value indicates the smallest index reachable from that node.
  3. Stack Management: Tarjan’s algorithm maintains a stack to track the nodes currently being explored. Nodes are only popped from the stack when an SCC is completely identified.

Steps of Tarjan's Algorithm

  1. Initialize indexes and low-link values for all vertices to -1 and create an empty stack.
  2. Perform DFS for each unvisited vertex:
    • Assign an index and a low-link value.
    • Push the vertex onto the stack.
    • Recursively visit adjacent vertices. Update the low-link value based on adjacent vertices.
    • If a vertex is part of an SCC, pop nodes from the stack until the current vertex is encountered.
  3. Return the identified SCCs.

Example Implementation in Java

Let’s implement Tarjan’s Algorithm in Java. Here’s a straightforward implementation to detect and print strongly connected components.

import java.util.ArrayList; import java.util.List; import java.util.Stack; public class TarjanSCC { private int index = 0; private int[] indexes; private int[] lowlinks; private Stack<Integer> stack = new Stack<>(); private boolean[] onStack; private List<List<Integer>> stronglyConnectedComponents = new ArrayList<>(); public List<List<Integer>> findSCCs(List<List<Integer>> graph) { int n = graph.size(); indexes = new int[n]; lowlinks = new int[n]; onStack = new boolean[n]; for (int v = 0; v < n; v++) { if (indexes[v] == 0) { strongConnect(v, graph); } } return stronglyConnectedComponents; } private void strongConnect(int v, List<List<Integer>> graph) { indexes[v] = lowlinks[v] = ++index; stack.push(v); onStack[v] = true; for (int w : graph.get(v)) { if (indexes[w] == 0) { strongConnect(w, graph); lowlinks[v] = Math.min(lowlinks[v], lowlinks[w]); } else if (onStack[w]) { lowlinks[v] = Math.min(lowlinks[v], indexes[w]); } } if (lowlinks[v] == indexes[v]) { List<Integer> scc = new ArrayList<>(); int w; do { w = stack.pop(); onStack[w] = false; scc.add(w); } while (w != v); stronglyConnectedComponents.add(scc); } } public static void main(String[] args) { List<List<Integer>> graph = new ArrayList<>(); // Initialize graph, for instance: // graph.add(Arrays.asList(1)); // Vertex 0 points to 1 // graph.add(Arrays.asList(2)); // Vertex 1 points to 2 // graph.add(Arrays.asList(0)); // Vertex 2 points to 0 (forming a cycle) TarjanSCC tarjan = new TarjanSCC(); List<List<Integer>> result = tarjan.findSCCs(graph); System.out.println("Strongly Connected Components: " + result); } }

Breaking Down the Code

  • Graph Representation: The graph is represented as an adjacency list using a List<List<Integer>>, where each list contains the neighbors of a vertex.
  • Initialization: Arrays for indexes and low-link values are initialized to track visited nodes.
  • DFS Implementation: The strongConnect method performs a DFS on the graph, updating the indexes and low-link values accordingly.
  • SCC Extraction: When a vertex’s low-link value equals its index, it signals the end of an SCC, and nodes are popped from the stack until the SCC is fully identified.

Conclusion

Tarjan's Algorithm is a powerful method for finding strongly connected components in directed graphs, harnessing the techniques of depth-first search and stack management. Whether you're tackling a competitive programming problem or preparing for technical interviews, understanding this efficient algorithm will bolster your graph traversal skills and problem-solving toolkit.

Popular tags

Tarjan's AlgorithmStrongly Connected ComponentsGraph Theory

Share now!

Like & bookmark

Related collections

  • Advanced Graph Interview Questions in Java

    16/11/2024 · DSA

  • Mastering Arrays : The Basic Data Structure

    06/12/2024 · DSA

  • Trees Interview Questions Using Java

    13/10/2024 · DSA

  • Mastering Bit Manipulation: Unlocking Binary Power

    08/12/2024 · DSA

  • Advanced String-based Interview Techniques

    15/11/2024 · DSA

Related articles

  • Understanding Heaps

    06/12/2024 · DSA

  • The Two-Pointer Technique

    06/12/2024 · DSA

  • Understanding Shortest Path Algorithms

    16/11/2024 · DSA

  • Minimum Cost to Connect Ropes Using Heap

    16/11/2024 · DSA

  • Finding the Median of a Stream Using Heaps

    16/11/2024 · DSA

  • Understanding Quick Sort

    06/12/2024 · DSA

  • Sort a Nearly Sorted Array Using Heap

    16/11/2024 · DSA

Popular category

  • Python
  • Generative AI
  • Machine Learning
  • ReactJS
  • System Design