Bird
Raised Fist0
DSA C++programming~10 mins

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

Choose your learning style10 modes available
Concept Flow - Bridges in Graph Tarjan's Algorithm
Start DFS at a node
↓
Mark node visited, set discovery & low time
↓
For each adjacent node
↓
If not visited
↓
DFS on adjacent node
↓
Update low time of current node
↓
Check if low[adj
↓
If yes, edge is a bridge
↓
If visited and not parent
↓
Update low time of current node
↓
Repeat until all nodes visited
↓
Output all bridges found
The algorithm runs DFS, assigns discovery and low times, updates low times using back edges, and identifies bridges where low time of adjacent node is greater than discovery time of current node.
Execution Sample
DSA C++
void dfs(int u, int parent) {
  visited[u] = true;
  disc[u] = low[u] = time++;
  for (int v : adj[u]) {
    if (!visited[v]) {
      dfs(v, u);
      low[u] = min(low[u], low[v]);
      if (low[v] > disc[u]) bridges.push_back({u, v});
    } else if (v != parent) {
      low[u] = min(low[u], disc[v]);
    }
  }
}
This DFS function marks discovery and low times, explores neighbors, updates low values, and detects bridges.
Execution Table
StepOperationNode VisitedParentDiscovery Time (disc)Low Time (low)Bridges FoundVisual State
1Start DFS0-1disc[0]=0low[0]=0[]0(d=0,l=0)
2Visit neighbor10disc[1]=1low[1]=1[]0(d=0,l=0) -> 1(d=1,l=1)
3Visit neighbor21disc[2]=2low[2]=2[]0 -> 1 -> 2(d=2,l=2)
4Back edge found02--[]Back edge 2->0 updates low[2]=min(2,0)=0
5Update low of 110-low[1]=min(1,0)=0[]0(d=0,l=0) -> 1(d=1,l=0) -> 2(d=2,l=0)
6Check bridge 1-221--[]low[2]=0 <= disc[1]=1 no bridge
7Visit neighbor31disc[3]=3low[3]=3[]Add 3(d=3,l=3) connected to 1
8Visit neighbor43disc[4]=4low[4]=4[]Add 4(d=4,l=4) connected to 3
9No back edge43--[]No back edge from 4
10Update low of 331-low[3]=min(3,4)=3[]3(d=3,l=3) -> 4(d=4,l=4)
11Check bridge 3-443--[{3,4}]low[4]=4 > disc[3]=3 bridge found
12Update low of 110-low[1]=min(0,3)=0[{3,4}]1(d=1,l=0) with child 3(d=3,l=3)
13Check bridge 0-110--[{3,4}]low[1]=0 <= disc[0]=0 no bridge
14DFS complete----[{3,4}]Final bridges: (3,4)
💡 All nodes visited, DFS complete, bridges identified
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 5After Step 7After Step 11Final
disc[-1,-1,-1,-1,-1][0,-1,-1,-1,-1][0,1,-1,-1,-1][0,1,2,-1,-1][0,1,2,-1,-1][0,1,2,3,-1][0,1,2,3,4][0,1,2,3,4]
low[-1,-1,-1,-1,-1][0,-1,-1,-1,-1][0,1,-1,-1,-1][0,1,0,-1,-1][0,0,0,-1,-1][0,0,0,3,-1][0,0,0,3,4][0,0,0,3,4]
visited[false,false,false,false,false][true,false,false,false,false][true,true,false,false,false][true,true,true,false,false][true,true,true,false,false][true,true,true,true,false][true,true,true,true,true][true,true,true,true,true]
bridges[][][][][][][{3,4}][{3,4}]
Key Moments - 3 Insights
Why do we update low[u] with disc[v] when v is visited and not parent?
Because this means there is a back edge from u to v, which helps update the earliest reachable ancestor (low[u]). See execution_table step 4 where low[2] is updated using disc[0].
Why is an edge a bridge only if low[v] > disc[u]?
If low[v] > disc[u], it means v and its descendants cannot reach u or any ancestor of u via back edges, so removing edge u-v disconnects the graph. See execution_table step 11 where edge (3,4) is identified as a bridge.
What happens if the graph is disconnected?
The DFS is started from each unvisited node to cover all components. The example assumes starting at node 0, but in practice, the algorithm runs DFS on all nodes to find bridges in all components.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the updated low time of node 2?
A0
B2
C1
D3
💡 Hint
Check the 'Low Time (low)' column at step 4 where back edge updates low[2]
At which step is the first bridge found according to the execution_table?
AStep 6
BStep 11
CStep 13
DStep 14
💡 Hint
Look at the 'Bridges Found' column to see when the first bridge appears
If node 4 had a back edge to node 1, how would low[4] change after step 9?
AIt would remain 4
BIt would become 1
CIt would become 0
DIt would become 3
💡 Hint
Back edges update low time to minimum discovery time reachable; check variable_tracker for low values
Concept Snapshot
Tarjan's Algorithm finds bridges by DFS traversal.
Assign discovery and low times to each node.
Update low times using back edges.
If low[v] > disc[u], edge u-v is a bridge.
Run DFS on all nodes to cover disconnected graphs.
Bridges are edges whose removal increases graph components.
Full Transcript
Tarjan's algorithm for bridges uses DFS to assign discovery and low times to nodes. It explores neighbors, updates low times with back edges, and identifies bridges when the low time of a neighbor is greater than the discovery time of the current node. The execution trace shows starting DFS at node 0, visiting neighbors, updating low times, and detecting the bridge between nodes 3 and 4. Key moments clarify why low times update with back edges and the condition for bridges. The quiz tests understanding of low time updates and bridge detection steps.

Practice

(1/5)
1. What is the main purpose of Tarjan's algorithm when finding bridges in a graph?
easy
A. To detect cycles in a graph
B. To find the shortest path between two nodes
C. To find edges that, if removed, increase the number of connected components
D. To count the total number of edges in the graph

Solution

  1. Step 1: Understand the definition of a bridge

    A bridge is an edge that, when removed, increases the number of connected components in the graph.
  2. Step 2: Relate Tarjan's algorithm to bridges

    Tarjan's algorithm efficiently finds such edges by using DFS and tracking discovery and low times.
  3. Final Answer:

    To find edges that, if removed, increase the number of connected components -> Option C
  4. Quick Check:

    Bridges = edges increasing components when removed [OK]
Hint: Bridges disconnect graph if removed [OK]
Common Mistakes:
  • Confusing bridges with cycles
  • Thinking it finds shortest paths
  • Assuming it counts edges only
2. Which of the following is the correct way to declare the discovery time array in C++ for Tarjan's bridge-finding algorithm on a graph with n vertices?
easy
A. int disc[n];
B. vector<int> disc(n, -1);
C. int disc = new int[n];
D. int disc[] = new int();

Solution

  1. Step 1: Understand C++ array and vector declarations

    Static arrays like int disc[n]; are not standard in C++ (unless n is a constant). Using vector<int> is safer and dynamic.
  2. Step 2: Identify correct initialization for discovery times

    Initializing with -1 indicates unvisited nodes, which is common practice.
  3. Final Answer:

    vector<int> disc(n, -1); -> Option B
  4. Quick Check:

    Use vector with size and initial value [OK]
Hint: Use vector<int> with size and init value [OK]
Common Mistakes:
  • Using variable-length arrays (not standard C++)
  • Incorrect pointer syntax
  • Not initializing discovery times
3. Consider the following C++ code snippet using Tarjan's algorithm to find bridges. What will be the output bridges for the given graph edges?
int n = 4;
vector<int> graph[4];
graph[0].push_back(1);
graph[1].push_back(0);
graph[1].push_back(2);
graph[2].push_back(1);
graph[2].push_back(3);
graph[3].push_back(2);
// After running Tarjan's bridge finder, which edges are bridges?
// Edges are represented as pairs (u, v) with u < v
medium
A. [(0, 1), (1, 2), (2, 3)]
B. [(0, 1), (1, 2)]
C. [(0, 1), (2, 3)]
D. []

Solution

  1. Step 1: Visualize the graph structure

    The graph is a chain: 0 - 1 - 2 - 3 with bidirectional edges, forming a path with no cycles.
  2. Step 2: Apply Tarjan's bridge finding logic

    In a path graph (a tree), every edge is a bridge because removing any disconnects the graph. Tarjan's DFS will detect all three edges where low[v] > disc[u] for each tree edge.
  3. Final Answer:

    [(0, 1), (1, 2), (2, 3)] -> Option A
  4. Quick Check:

    All edges in path are bridges [OK]
Hint: All edges in chain are bridges [OK]
Common Mistakes:
  • Ignoring edge (0,1) as a bridge
  • Assuming no bridges in chain
  • Confusing bridge with cycle edges
4. Identify the bug in the following Tarjan's algorithm snippet for finding bridges:
void dfs(int u, int parent) {
  visited[u] = true;
  disc[u] = low[u] = time++;
  for (int v : graph[u]) {
    if (v == parent) continue;
    if (!visited[v]) {
      dfs(v, u);
      low[u] = min(low[u], low[v]);
      if (low[v] > disc[u])
        bridges.push_back({u, v});
    } else {
      low[u] = min(low[u], disc[v]);
    }
  }
}
medium
A. Missing initialization of time variable before DFS
B. Incorrect comparison in if condition for bridge detection
C. Not marking nodes as visited
D. Not checking for parent node in adjacency

Solution

  1. Step 1: Review the DFS function variables

    The variable 'time' is used to assign discovery times but is not shown initialized before DFS calls.
  2. Step 2: Understand impact of uninitialized time

    If 'time' is not initialized to 0 before DFS, discovery and low times will be incorrect, causing wrong bridge detection.
  3. Final Answer:

    Missing initialization of time variable before DFS -> Option A
  4. Quick Check:

    Initialize time = 0 before DFS [OK]
Hint: Initialize time counter before DFS call [OK]
Common Mistakes:
  • Forgetting to initialize time
  • Confusing low and disc arrays
  • Not skipping parent node in DFS
5. Given a connected undirected graph with n vertices and m edges, how can Tarjan's algorithm be adapted to find all bridges efficiently, and what is its time complexity?
hard
A. Use Dijkstra's algorithm; time complexity O(m log n)
B. Use BFS with distance arrays; time complexity O(n^2)
C. Use DFS without low arrays; time complexity O(m^2)
D. Use DFS with discovery and low arrays; time complexity O(n + m)

Solution

  1. Step 1: Describe Tarjan's algorithm adaptation

    Tarjan's algorithm uses DFS traversal, tracking discovery times and low values to detect bridges efficiently.
  2. Step 2: Analyze time complexity

    Each vertex and edge is visited once during DFS, so the time complexity is O(n + m), where n is vertices and m is edges.
  3. Final Answer:

    Use DFS with discovery and low arrays; time complexity O(n + m) -> Option D
  4. Quick Check:

    DFS + low arrays = O(n + m) [OK]
Hint: DFS with low arrays runs in linear time [OK]
Common Mistakes:
  • Using BFS which is not suitable here
  • Ignoring low arrays in DFS
  • Confusing with shortest path algorithms
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard