Tarjan's algorithm is a depth-first search (DFS) based algorithm used to find bridges in an undirected graph. It efficiently identifies bridges by maintaining information about the depth of each vertex and the earliest reachable ancestor in the DFS tree. Here's a detailed explanation of Tarjan's algorithm:
-
Initialization:
- Initialize an empty set to keep track of visited vertices.
- Initialize an empty list to store the bridges found.
-
Perform Depth-First Search (DFS):
- Start DFS traversal from any arbitrary vertex in the graph.
- During the traversal, maintain the following information for each vertex:
- Discovery time: The order in which vertices are visited during the DFS traversal.
- Low time: The earliest discovery time reachable from the current vertex via the DFS traversal, including back edges.
- Initialize a variable time to keep track of the discovery time. Set time to 0 initially.
-
DFS Recursive Function:
- Implement a recursive DFS function that explores the graph from a given vertex u:
- Mark u as visited.
- Set both the discovery time and low time of u to the current value of time.
- Increment time.
- Iterate through all neighbors v of u:
- If v has not been visited yet, recursively call the DFS function for v.
- If v is already visited and is not the parent of u, update the low time of u to be the minimum of its current low time and the discovery time of v.
- During the backtracking phase of DFS, update the low time of u if necessary.
-
Identify Bridges:
- When returning from the recursive DFS call from vertex v to its parent u, check if the low time of v is greater than the discovery time of u.
- If the condition holds, then the edge (u, v) is a bridge.
- Add (u, v) to the list of bridges.
-
Repeat for Unvisited Vertices:
- Continue the DFS traversal for all unvisited vertices in the graph until all vertices have been visited.
-
Return Bridges:
- Once the DFS traversal is complete, return the list of bridges found during the traversal.
Tarjan's algorithm efficiently identifies all bridges in an undirected graph. It has a time complexity of O(V + E), where V is the number of vertices and E is the number of edges in the graph. This makes it suitable for practical use even with large graphs.