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 C# code that uses Tarjan's algorithm to find bridges in an undirected graph?
DSA C#
using System; using System.Collections.Generic; class Graph { int V; List<int>[] adj; int time = 0; public Graph(int v) { V = v; adj = new List<int>[v]; for (int i = 0; i < v; i++) adj[i] = new List<int>(); } public void AddEdge(int u, int v) { adj[u].Add(v); adj[v].Add(u); } void BridgeUtil(int u, bool[] visited, int[] disc, int[] low, int[] parent, List<(int,int)> bridges) { visited[u] = true; disc[u] = low[u] = ++time; foreach (int v in adj[u]) { if (!visited[v]) { parent[v] = u; BridgeUtil(v, visited, disc, low, parent, bridges); low[u] = Math.Min(low[u], low[v]); if (low[v] > disc[u]) { bridges.Add((u, v)); } } else if (v != parent[u]) { low[u] = Math.Min(low[u], disc[v]); } } } public List<(int,int)> FindBridges() { bool[] visited = new bool[V]; int[] disc = new int[V]; int[] low = new int[V]; int[] parent = new int[V]; for (int i = 0; i < V; i++) parent[i] = -1; List<(int,int)> bridges = new List<(int,int)>(); for (int i = 0; i < V; i++) { if (!visited[i]) { BridgeUtil(i, visited, disc, low, parent, bridges); } } return bridges; } } class Program { static void Main() { Graph g = new Graph(5); g.AddEdge(1, 0); g.AddEdge(0, 2); g.AddEdge(2, 1); g.AddEdge(0, 3); g.AddEdge(3, 4); var bridges = g.FindBridges(); foreach (var b in bridges) { Console.WriteLine($"{b.Item1} - {b.Item2}"); } } }
Attempts:
2 left
💡 Hint
Remember that a bridge is an edge which, when removed, increases the number of connected components.
✗ Incorrect
The edges (3,4) and (0,3) are bridges because removing either disconnects the graph. The triangle formed by nodes 0,1,2 has no bridges.
🧠 Conceptual
intermediateUnderstanding Low and Discovery Times in Tarjan's Algorithm
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 and cycles.
✗ Incorrect
The 'low' value of a node is the smallest discovery time reachable from that node by following zero or more tree edges and then possibly one back edge.
🔧 Debug
advancedIdentify the Bug in Tarjan's Bridge Detection Code
What error does the following code snippet cause when run to find bridges in a graph?
DSA C#
void BridgeUtil(int u, bool[] visited, int[] disc, int[] low, int[] parent, List<(int,int)> bridges) { visited[u] = true; disc[u] = low[u] = ++time; foreach (int v in adj[u]) { if (!visited[v]) { parent[v] = u; BridgeUtil(v, visited, disc, low, parent, bridges); low[u] = Math.Min(low[u], low[v]); if (low[v] >= disc[u]) { bridges.Add((u, v)); } } else if (v != parent[u]) { low[u] = Math.Min(low[u], disc[v]); } } }
Attempts:
2 left
💡 Hint
Check the condition that decides if an edge is a bridge.
✗ Incorrect
The condition 'if (low[v] >= disc[u])' incorrectly marks edges that are part of cycles as bridges. The correct condition is 'if (low[v] > disc[u])' to identify bridges properly.
🚀 Application
advancedNumber of Bridges in a Complex Graph
Given a graph with edges: (0-1), (1-2), (2-0), (1-3), (3-4), (4-5), (5-3), (4-6), how many bridges does Tarjan's algorithm find?
Attempts:
2 left
💡 Hint
Identify cycles and edges whose removal disconnects the graph.
✗ Incorrect
The cycles are (0,1,2) and (3,4,5). The edges (1-3) and (4-6) are bridges. So total 2 bridges.
❓ Predict Output
expertOutput of Tarjan's Algorithm on a Disconnected Graph
What is the output of the following code that finds bridges in a disconnected graph?
DSA C#
using System; using System.Collections.Generic; class Graph { int V; List<int>[] adj; int time = 0; public Graph(int v) { V = v; adj = new List<int>[v]; for (int i = 0; i < v; i++) adj[i] = new List<int>(); } public void AddEdge(int u, int v) { adj[u].Add(v); adj[v].Add(u); } void BridgeUtil(int u, bool[] visited, int[] disc, int[] low, int[] parent, List<(int,int)> bridges) { visited[u] = true; disc[u] = low[u] = ++time; foreach (int v in adj[u]) { if (!visited[v]) { parent[v] = u; BridgeUtil(v, visited, disc, low, parent, bridges); low[u] = Math.Min(low[u], low[v]); if (low[v] > disc[u]) { bridges.Add((u, v)); } } else if (v != parent[u]) { low[u] = Math.Min(low[u], disc[v]); } } } public List<(int,int)> FindBridges() { bool[] visited = new bool[V]; int[] disc = new int[V]; int[] low = new int[V]; int[] parent = new int[V]; for (int i = 0; i < V; i++) parent[i] = -1; List<(int,int)> bridges = new List<(int,int)>(); for (int i = 0; i < V; i++) { if (!visited[i]) { BridgeUtil(i, visited, disc, low, parent, bridges); } } return bridges; } } class Program { static void Main() { Graph g = new Graph(7); g.AddEdge(0, 1); g.AddEdge(1, 2); g.AddEdge(2, 0); g.AddEdge(3, 4); g.AddEdge(4, 5); g.AddEdge(5, 3); g.AddEdge(4, 6); var bridges = g.FindBridges(); foreach (var b in bridges) { Console.WriteLine($"{b.Item1} - {b.Item2}"); } } }
Attempts:
2 left
💡 Hint
Check which edges, when removed, increase the number of connected components.
✗ Incorrect
The cycle (3,4,5) has no bridges. The cycle (0,1,2) has no bridges. The only bridge is edge (4,6).
