Graph Traversals, Topological Sort & Strongly Connected Components
“BFS/DFS edge classification, discovery/finish timestamps, Kahn's algorithm, and Kosaraju/Tarjan SCCs.”
1.DFS Timestamps & Edge Classification Theory
§ 4.1.During Depth-First Search (DFS), each vertex u is assigned two timestamps:
- d[u]: Discovery time (when vertex turns from WHITE to GRAY).
- f[u]: Finish time (when all outgoing edges are explored and vertex turns BLACK).
The Parenthesis Structure:
For any pair u, v, exactly one holds:
1. [d[u], f[u]] and [d[v], f[v]] are entirely disjoint: neither is an ancestor of the other.
2. [d[u], f[u]] is completely contained within [d[v], f[v]]: u is a descendant of v in the DFS forest.
Edge Classification on Directed Graphs:
- Tree Edge: Leads to an unvisited (white) vertex.
- Back Edge: Leads to an ancestor currently on the recursion stack (gray vertex: d[v] < d[u] < f[u] < f[v]). A back edge proves existence of a directed cycle.
- Forward Edge: Leads to a descendant already explored (black vertex: d[u] < d[v] < f[v] < f[u]).
- Cross Edge: Leads to a vertex in another branch/tree (black vertex: d[v] < f[v] < d[u] < f[u]).
2.Topological Sorting & Strongly Connected Components
§ 4.2.A Topological Sort of a directed acyclic graph (DAG) G = (V, E) is a linear ordering of all vertices such that for every directed edge (u, v), vertex u comes before v.
Two Classic Algorithms:
1. DFS Finish Time Ordering: Run DFS on G. As each vertex finishes (f[u]), prepend it to a linked list. The resulting list is a valid topological sort. Time O(V + E).
2. Kahn's In-Degree Algorithm: Compute in-degrees of all vertices. Enqueue all vertices with in-degree 0. Repeatedly dequeue vertex u, add to sorted order, and decrement in-degrees of its neighbors. If a neighbor reaches in-degree 0, enqueue it. Time O(V + E).
Strongly Connected Components (Kosaraju's Algorithm):
A directed graph is strongly connected if every vertex is reachable from every other vertex.
Kosaraju's 2-pass algorithm:
1. Run DFS on G and record vertices in a stack ordered by finish time.
2. Construct the transpose graph G^T (reverse all edge directions).
3. Pop vertices from stack and run DFS on G^T. Each DFS tree corresponds to an SCC.
3. Worked Problem: DFS Edge Classification & Timestamp Tracing
Alphabetical neighbor tie-breaking affects discovery times, but the topological property and existence of back edges remains invariant.
Problem: A directed graph G has vertices {1, 2, 3, 4} and edges (1, 2), (1, 3), (2, 4), (3, 2), (4, 3). Run DFS starting at vertex 1 (exploring lower numerical vertex first). Classify all edges and determine whether G contains a cycle.
Step 1: Trace DFS execution with timestamp clock starting at t = 1:
- Visit 1: d[1] = 1.
- From 1, explore (1, 2): 2 is unvisited => (1, 2) is a TREE EDGE. d[2] = 2.
- From 2, explore (2, 4): 4 is unvisited => (2, 4) is a TREE EDGE. d[4] = 3.
- From 4, explore (4, 3): 3 is unvisited => (4, 3) is a TREE EDGE. d[3] = 4.
- From 3, explore (3, 2): vertex 2 is currently GRAY (on stack: d[2]=2, f[2] not set).
Since 2 is an ancestor on stack, (3, 2) is a BACK EDGE!
- No more edges from 3: f[3] = 5.
- Backtrack to 4: no more edges => f[4] = 6.
- Backtrack to 2: no more edges => f[2] = 7.
- Backtrack to 1, explore (1, 3): vertex 3 is BLACK (d[3]=4, f[3]=5 < d[1]=1... wait d[1]=1 < d[3]=4 < f[3]=5 < f[1]).
Since 3 is a descendant already explored, (1, 3) is a FORWARD EDGE.
- Finish 1: f[1] = 8.
Step 2: Summary of edge classifications:
- Tree Edges: (1, 2), (2, 4), (4, 3)
- Back Edge: (3, 2)
- Forward Edge: (1, 3)
- Cross Edges: None
Step 3: Cycle Conclusion:
Because a BACK EDGE (3, 2) was discovered, the graph contains a directed cycle: 2 -> 4 -> 3 -> 2.
Primary Academic References & Exam Sources
Every concept in Notes From a B.Tech Brain is synthesized from authoritative academic texts, seminal peer-reviewed research, verified exam archives, and renowned university lectures.
Curated for clarity, rigorous numerical solving, and semester excellence:
Best visual explanations of algorithm design and complexity analysis.
Exam-focused with GATE PYQ numericals on every major algorithm.