Challenge - 5 Problems
Tarjan Bridges Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Tarjan's Algorithm for Bridges
What is the output (list of bridges) of the following Java code implementing Tarjan's algorithm on the given graph?
DSA Java
import java.util.*; public class BridgesTarjan { private int time = 0; private List<List<Integer>> graph; private int[] disc, low; private boolean[] visited; private List<String> bridges; public BridgesTarjan(int n) { graph = new ArrayList<>(); for (int i = 0; i < n; i++) graph.add(new ArrayList<>()); disc = new int[n]; low = new int[n]; visited = new boolean[n]; bridges = new ArrayList<>(); } public void addEdge(int u, int v) { graph.get(u).add(v); graph.get(v).add(u); } public void dfs(int u, int parent) { visited[u] = true; disc[u] = low[u] = ++time; for (int v : graph.get(u)) { if (v == parent) continue; if (!visited[v]) { dfs(v, u); low[u] = Math.min(low[u], low[v]); if (low[v] > disc[u]) { bridges.add(u + "-" + v); } } else { low[u] = Math.min(low[u], disc[v]); } } } public List<String> findBridges() { for (int i = 0; i < graph.size(); i++) { if (!visited[i]) dfs(i, -1); } Collections.sort(bridges); return bridges; } public static void main(String[] args) { BridgesTarjan bt = new BridgesTarjan(5); bt.addEdge(0, 1); bt.addEdge(1, 2); bt.addEdge(2, 0); bt.addEdge(1, 3); bt.addEdge(3, 4); List<String> result = bt.findBridges(); System.out.println(result); } }
Attempts:
2 left
💡 Hint
Focus on edges whose removal increases the number of connected components.
✗ Incorrect
Edges 1-3 and 3-4 are bridges because removing them disconnects the graph. The triangle 0-1-2 has no bridges.
🧠 Conceptual
intermediateUnderstanding Low and Discovery Times
In Tarjan's algorithm for finding bridges, what does the 'low' array represent for each node?
Attempts:
2 left
💡 Hint
Think about how 'low' helps detect back edges.
✗ Incorrect
'Low' stores the smallest discovery time reachable from the node by traversing zero or more tree edges and then possibly one back edge.
🔧 Debug
advancedIdentify the Bug in Bridge Detection Code
What error will the following code cause when running Tarjan's algorithm for bridges?
DSA Java
if (low[v] >= disc[u]) { bridges.add(u + "-" + v); }
Attempts:
2 left
💡 Hint
Check the condition for bridges vs articulation points.
✗ Incorrect
The condition 'low[v] >= disc[u]' is used for articulation points, not bridges. Bridges require 'low[v] > disc[u]'.
❓ Predict Output
advancedOutput of Bridges in a Disconnected Graph
What is the output of the following code that finds bridges in a disconnected graph?
DSA Java
import java.util.*; public class BridgesTarjan { private int time = 0; private List<List<Integer>> graph; private int[] disc, low; private boolean[] visited; private List<String> bridges; public BridgesTarjan(int n) { graph = new ArrayList<>(); for (int i = 0; i < n; i++) graph.add(new ArrayList<>()); disc = new int[n]; low = new int[n]; visited = new boolean[n]; bridges = new ArrayList<>(); } public void addEdge(int u, int v) { graph.get(u).add(v); graph.get(v).add(u); } public void dfs(int u, int parent) { visited[u] = true; disc[u] = low[u] = ++time; for (int v : graph.get(u)) { if (v == parent) continue; if (!visited[v]) { dfs(v, u); low[u] = Math.min(low[u], low[v]); if (low[v] > disc[u]) { bridges.add(u + "-" + v); } } else { low[u] = Math.min(low[u], disc[v]); } } } public List<String> findBridges() { for (int i = 0; i < graph.size(); i++) { if (!visited[i]) dfs(i, -1); } Collections.sort(bridges); return bridges; } public static void main(String[] args) { BridgesTarjan bt = new BridgesTarjan(6); bt.addEdge(0, 1); bt.addEdge(1, 2); bt.addEdge(3, 4); bt.addEdge(4, 5); List<String> result = bt.findBridges(); System.out.println(result); } }
Attempts:
2 left
💡 Hint
Consider each disconnected component separately.
✗ Incorrect
Each edge in the two disconnected chains is a bridge because removing it increases components.
🧠 Conceptual
expertWhy Tarjan's Algorithm Uses DFS Time Stamps
Why does Tarjan's algorithm assign discovery times to nodes during DFS when finding bridges?
Attempts:
2 left
💡 Hint
Think about how the algorithm detects cycles and connectivity.
✗ Incorrect
Discovery times help identify back edges to ancestors, which is essential to find if an edge is a bridge.
