Tarjan's Algorithm (for SCC and Bridge Detection)
Tarjan’s Algorithm for Strongly Connected Components (SCC)
Section titled “Tarjan’s Algorithm for Strongly Connected Components (SCC)”Tarjan’s algorithm is a graph traversal algorithm used to find the strongly connected components (SCCs) of a directed graph. An SCC is a subgraph where for any two vertices u and v within the subgraph, there is a path from u to v and a path from v to u.
Key Concepts
Section titled “Key Concepts”- Discovery Time (
disc[u]): The time (or order) at which a nodeuis first visited during DFS. - Low-link Value (
low[u]): The lowestdiscvalue reachable fromu(includinguitself) through the DFS tree edges and at most one back-edge. - Stack: Used to keep track of the nodes currently in the DFS traversal path. Nodes are pushed onto the stack when visited and popped when an SCC is found.
Algorithm Steps
Section titled “Algorithm Steps”- Initialize
discandlowarrays with -1, and a boolean arrayonStackto false. - Maintain a global
timevariable, initialized to 0. - For each unvisited vertex
uin the graph, calldfs(u): a. Setdisc[u] = low[u] = time++. b. Pushuonto the stack and setonStack[u] = true. c. For each neighborvofu: i. Ifvis not visited (disc[v] == -1): 1. Recursively calldfs(v). 2. Updatelow[u] = min(low[u], low[v]). ii. Ifvis visited andonStack[v]is true: 1. Updatelow[u] = min(low[u], disc[v])(this is a back-edge). d. Iflow[u] == disc[u](meaninguis the root of an SCC): i. Pop nodes from the stack untiluis popped, forming an SCC. ii. SetonStackto false for popped nodes.
Time and Space Complexity
Section titled “Time and Space Complexity”- Time Complexity: O(V + E), where V is the number of vertices and E is the number of edges, as each vertex and edge is visited once.
- Space Complexity: O(V + E) for adjacency list,
disc,low,onStackarrays, and the stack.
Tarjan’s Algorithm for Bridge Detection
Section titled “Tarjan’s Algorithm for Bridge Detection”Tarjan’s algorithm can also be adapted to find bridges in an undirected graph. A bridge is an edge whose removal increases the number of connected components of the graph.
Key Concepts (similar to SCC, but for undirected graphs)
Section titled “Key Concepts (similar to SCC, but for undirected graphs)”- Discovery Time (
disc[u]): The time at which a nodeuis first visited during DFS. - Low-link Value (
low[u]): The lowestdiscvalue reachable fromu(includinguitself) through the DFS tree edges and at most one back-edge (excluding the edge to its immediate parent in the DFS tree).
Algorithm Steps
Section titled “Algorithm Steps”- Initialize
discandlowarrays with -1, and aparentarray with -1. - Maintain a global
timevariable, initialized to 0. - For each unvisited vertex
uin the graph, calldfs(u, -1)(initial parent is -1): a. Setdisc[u] = low[u] = time++. b. For each neighborvofu: i. Ifvis theparentofu, continue. ii. Ifvis not visited (disc[v] == -1): 1. Setparent[v] = u. 2. Recursively calldfs(v, u). 3. Updatelow[u] = min(low[u], low[v]). 4. Iflow[v] > disc[u], then the edge(u, v)is a bridge. iii. Ifvis visited and not theparentofu: 1. Updatelow[u] = min(low[u], disc[v])(this is a back-edge).
Time and Space Complexity
Section titled “Time and Space Complexity”- Time Complexity: O(V + E).
- Space Complexity: O(V + E).