0
0
DSA Cprogramming~10 mins

Bridges in Graph Tarjan's Algorithm in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Bridges in Graph Tarjan's Algorithm
Start DFS at node u
Set discovery[u
For each neighbor v of u
If v not visited
DFS(v)
Update low[u
If low[v
Edge u-v is a bridge
Record bridge
Continue
Else if v != parent[u
Update low[u
DFS complete for u
Repeat for all nodes
Output all bridges found
The algorithm runs DFS from each node, sets discovery and low times, updates low values based on neighbors, and identifies edges where low[v] > discovery[u] as bridges.
Execution Sample
DSA C
void dfs(int u, int parent) {
  discovery[u] = low[u] = time++;
  for (int v : adj[u]) {
    if (discovery[v] == -1) {
      dfs(v, u);
      low[u] = min(low[u], low[v]);
      if (low[v] > discovery[u]) bridges.push_back({u, v});
    } else if (v != parent) {
      low[u] = min(low[u], discovery[v]);
    }
  }
}
This code performs DFS, sets discovery and low times, updates low values, and detects bridges when low[v] > discovery[u].
Execution Table
StepOperationCurrent Node (u)Neighbor (v)discovery[]low[]Bridge FoundVisual State
1Start DFS at node 00-[0:0,1:-1,2:-1,3:-1,4:-1][0:0,1:-1,2:-1,3:-1,4:-1]NoNodes: 0(discovery=0, low=0)
2Visit neighbor 1 of 001[0:0,1:-1,2:-1,3:-1,4:-1][0:0,1:-1,2:-1,3:-1,4:-1]NoNodes: 0(d=0,l=0)
3DFS call on node 11-[0:0,1:1,2:-1,3:-1,4:-1][0:0,1:1,2:-1,3:-1,4:-1]NoNodes: 0(d=0,l=0),1(d=1,l=1)
4Visit neighbor 0 of 1 (parent)10[0:0,1:1,2:-1,3:-1,4:-1][0:0,1:1,2:-1,3:-1,4:-1]NoNo update, 0 is parent
5Visit neighbor 2 of 112[0:0,1:1,2:-1,3:-1,4:-1][0:0,1:1,2:-1,3:-1,4:-1]NoNodes: 0(d=0,l=0),1(d=1,l=1)
6DFS call on node 22-[0:0,1:1,2:2,3:-1,4:-1][0:0,1:1,2:2,3:-1,4:-1]NoNodes: 0(d=0,l=0),1(d=1,l=1),2(d=2,l=2)
7Visit neighbor 1 of 2 (parent)21[0:0,1:1,2:2,3:-1,4:-1][0:0,1:1,2:2,3:-1,4:-1]NoNo update, 1 is parent
8Visit neighbor 3 of 223[0:0,1:1,2:2,3:-1,4:-1][0:0,1:1,2:2,3:-1,4:-1]NoNodes: 0(d=0,l=0),1(d=1,l=1),2(d=2,l=2)
9DFS call on node 33-[0:0,1:1,2:2,3:3,4:-1][0:0,1:1,2:2,3:3,4:-1]NoNodes: 0(d=0,l=0),1(d=1,l=1),2(d=2,l=2),3(d=3,l=3)
10Visit neighbor 2 of 3 (parent)32[0:0,1:1,2:2,3:3,4:-1][0:0,1:1,2:2,3:3,4:-1]NoNo update, 2 is parent
11Visit neighbor 4 of 334[0:0,1:1,2:2,3:3,4:-1][0:0,1:1,2:2,3:3,4:-1]NoNodes: 0(d=0,l=0),1(d=1,l=1),2(d=2,l=2),3(d=3,l=3)
12DFS call on node 44-[0:0,1:1,2:2,3:3,4:4][0:0,1:1,2:2,3:3,4:4]NoNodes: 0(d=0,l=0),1(d=1,l=1),2(d=2,l=2),3(d=3,l=3),4(d=4,l=4)
13Visit neighbor 3 of 4 (parent)43[0:0,1:1,2:2,3:3,4:4][0:0,1:1,2:2,3:3,4:4]NoNo update, 3 is parent
14Backtrack to node 33-[0:0,1:1,2:2,3:3,4:4][0:0,1:1,2:2,3:3,4:4]NoUpdate low[3] = min(3,4) = 3
15Check bridge condition for edge 3-434[0:0,1:1,2:2,3:3,4:4][0:0,1:1,2:2,3:3,4:4]YesBridge found: (3,4)
16Backtrack to node 22-[0:0,1:1,2:2,3:3,4:4][0:0,1:1,2:2,3:3,4:4]NoUpdate low[2] = min(2,3) = 2
17Check bridge condition for edge 2-323[0:0,1:1,2:2,3:3,4:4][0:0,1:1,2:2,3:3,4:4]YesBridge found: (2,3)
18Backtrack to node 11-[0:0,1:1,2:2,3:3,4:4][0:0,1:1,2:2,3:3,4:4]NoUpdate low[1] = min(1,2) = 1
19Check bridge condition for edge 1-212[0:0,1:1,2:2,3:3,4:4][0:0,1:1,2:2,3:3,4:4]YesBridge found: (1,2)
20Backtrack to node 00-[0:0,1:1,2:2,3:3,4:4][0:0,1:1,2:2,3:3,4:4]NoUpdate low[0] = min(0,1) = 0
21Check bridge condition for edge 0-101[0:0,1:1,2:2,3:3,4:4][0:0,1:1,2:2,3:3,4:4]YesBridge found: (0,1)
22DFS complete for all nodes--[0:0,1:1,2:2,3:3,4:4][0:0,1:1,2:2,3:3,4:4]NoAll bridges found: (0,1), (1,2), (2,3), (3,4)
💡 All nodes visited, DFS complete, all bridges identified where low[v] > discovery[u]
Variable Tracker
VariableStartAfter Step 1After Step 6After Step 12After Step 15After Step 17After Step 19After Step 21Final
discovery[-1,-1,-1,-1,-1][0,-1,-1,-1,-1][0,1,2,-1,-1][0,1,2,3,4][0,1,2,3,4][0,1,2,3,4][0,1,2,3,4][0,1,2,3,4][0,1,2,3,4]
low[-1,-1,-1,-1,-1][0,-1,-1,-1,-1][0,1,2,-1,-1][0,1,2,3,4][0,1,2,3,4][0,1,2,3,4][0,1,2,3,4][0,1,2,3,4][0,1,2,3,4]
time013555555
bridges[][][][][(3,4)][(3,4),(2,3)][(3,4),(2,3),(1,2)][(3,4),(2,3),(1,2),(0,1)][(3,4),(2,3),(1,2),(0,1)]
Key Moments - 3 Insights
Why do we check if low[v] > discovery[u] to find a bridge?
Because if low[v] is greater than discovery[u], it means v and its descendants cannot reach u or any ancestor of u without using edge u-v, so removing u-v disconnects the graph. See steps 15, 17, 19, and 21 in execution_table.
Why do we ignore the parent node when updating low[u] with discovery[v]?
Because the parent edge is the one we came from, and including it would incorrectly lower low[u]. We only update low[u] with discovery[v] for back edges to ancestors other than the parent. See step 4 and 7 where neighbors equal parent are ignored.
What happens if the graph has cycles? How does low[] help?
Low[] tracks the earliest visited node reachable from a subtree. In cycles, low[v] will be less or equal to discovery[u], so no bridge is found on that edge. This prevents false bridge detection. See no bridges found on edges in cycles in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 15, which edge is identified as a bridge?
A(3,4)
B(2,3)
C(1,2)
D(0,1)
💡 Hint
Check the 'Bridge Found' and 'Visual State' columns at step 15 in execution_table.
At which step does the discovery time of node 2 get assigned?
AStep 3
BStep 6
CStep 9
DStep 12
💡 Hint
Look at the discovery[] column in execution_table rows and find when discovery[2] changes from -1.
If node 3 had an additional back edge to node 0, how would low[3] change after visiting that neighbor?
Alow[3] would remain the same
Blow[3] would increase
Clow[3] would decrease to discovery[0]
Dlow[3] would become discovery[3]
💡 Hint
Refer to the update rule low[u] = min(low[u], discovery[v]) for back edges in the code and execution_table.
Concept Snapshot
Tarjan's Algorithm finds bridges by DFS traversal.
Assign discovery and low times to each node.
For each edge u-v, if low[v] > discovery[u], edge u-v is a bridge.
Ignore parent edge when updating low values.
Low[u] tracks earliest reachable ancestor from u's subtree.
Bridges disconnect the graph if removed.
Full Transcript
Tarjan's Algorithm for finding bridges in a graph uses a depth-first search (DFS) approach. Each node is assigned a discovery time when first visited and a low value representing the earliest visited node reachable from its subtree. During DFS, for each neighbor v of node u, if v is unvisited, DFS is called recursively on v. After returning, low[u] is updated to the minimum of low[u] and low[v]. If low[v] is greater than discovery[u], the edge u-v is a bridge because v cannot reach u or any ancestor without this edge. If v is already visited and not the parent of u, low[u] is updated to the minimum of low[u] and discovery[v], accounting for back edges. The algorithm continues until all nodes are visited, and all bridges are recorded. This method efficiently identifies edges whose removal increases the number of connected components in the graph.