Discrete Mathsfor Engineers

Structures, proofs, and intuition

EN | PT

Depth-First Search (DFS)

Part 7 — Applications II (SCC & Advanced Structures)

Series: Depth-First Search (DFS) Part 7 of 8

7. Applications II


Roadmap (what you will learn in this part):
  1. What strongly connected components (SCCs) are
  2. Why DFS finish times matter again
  3. The transpose graph intuition
  4. Kosaraju’s algorithm
  5. Why Kosaraju works
  6. Advanced DFS-based structures and patterns

Depth-First Search becomes especially powerful when we move from traversal to global graph decomposition.

In this part, we study strongly connected components (SCCs) and show how DFS reveals large-scale structure in directed graphs.

7.1 Strongly Connected Components

In a directed graph, two vertices u and v are strongly connected if:

\( u \leadsto v \quad \text{e} \quad v \leadsto u \)

That is, each vertex can reach the other.

A strongly connected component is a maximal set of vertices that are pairwise strongly connected.

Interpretation: inside one SCC, every vertex is reachable from every other vertex.

7.2 SCCs as a Higher-Level Structure

If we collapse each SCC into a single super-vertex, we obtain the condensation graph.

A fundamental fact is:

Theorem. The condensation graph of a directed graph is a DAG.

So SCC decomposition turns an arbitrary directed graph into an acyclic structure of components.

7.3 Why Finish Times Matter Again

In Part 6, finish times gave a topological-style order for DAGs. Here they guide us across SCCs.

If one SCC can reach another SCC, then DFS tends to finish the source component later.

High-level idea: decreasing finish times process SCCs in the right structural order.

7.4 The Transpose Graph

The transpose of a directed graph G = (V, E) is the graph G^T = (V, E^T) obtained by reversing every edge:

\( (u,v) \in E \iff (v,u) \in E^{T} \)

SCCs are preserved under transposition: the components remain the same, but the direction between components reverses.

7.5 Kosaraju’s Algorithm

Kosaraju’s algorithm computes SCCs using two DFS passes:

  1. Run DFS on G and record finish times.
  2. Build the transpose graph G^T.
  3. Run DFS on G^T in decreasing order of finish times.
  4. Each DFS tree in the second pass is one SCC.
Core strategy: first pass computes the right order; second pass extracts the components.

7.6 Kosaraju — Pseudocode Sketch

KOSARAJU(G):

    run DFS(G)
    let vertices be ordered by decreasing finish time

    compute G^T

    mark all vertices WHITE

    for each vertex u in decreasing finish-time order:
        if color[u] == WHITE:
            DFS-VISIT(G^T, u)
            output the vertices reached in this DFS as one SCC

The second DFS pass groups vertices into strongly connected components.

7.7 Why Kosaraju Works

The first DFS pass identifies an order that respects the component-level structure.

After transposition, edges between SCCs are reversed. So when the second pass starts from the largest remaining finish time, DFS stays inside exactly one SCC before crossing to others.

Key idea: finish times isolate the “right” SCC roots for the transpose graph.

7.8 Complexity

Kosaraju’s algorithm runs in linear time:

O(|V| + |E|)

because:

  • the first DFS is linear,
  • building the transpose is linear,
  • the second DFS is linear.

7.9 Advanced DFS-Based Structures

DFS is also the foundation for deeper graph algorithms and structures:

  • Tarjan’s SCC algorithm — SCCs using one DFS pass
  • Bridges and articulation points — in undirected graphs
  • Biconnected components
  • Low-link values — a key DFS structural invariant
Takeaway: DFS is not just a traversal algorithm; it is a framework for extracting graph structure.

7.10 Engineering Use Cases

SCC decomposition appears in many real systems:

  • dependency cycle analysis
  • package/module graph condensation
  • compiler optimization passes
  • workflow and service dependency analysis

In practice, SCCs help turn cyclic systems into a DAG of strongly connected blocks.

7.11 Mini Summary

  • SCCs are maximal mutually reachable vertex sets.
  • Collapsing SCCs produces a DAG.
  • Kosaraju uses two DFS passes and graph transposition.
  • DFS remains linear while extracting deep global structure.

Next: Part 8 — DFS vs BFS

We now have the full DFS toolbox: recursive exploration, timestamps, edge classification, cycle detection, topological sorting, and SCC decomposition.

In Part 8, we close the series by comparing DFS with BFS: two traversal strategies with very different structural roles.