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 Tarjan's algorithm code when run on the given graph?
DSA Python
def find_bridges(n, edges): graph = [[] for _ in range(n)] for u, v in edges: graph[u].append(v) graph[v].append(u) time = 0 disc = [-1] * n low = [-1] * n bridges = [] def dfs(u, parent): nonlocal time disc[u] = time low[u] = time time += 1 for v in graph[u]: if disc[v] == -1: dfs(v, u) low[u] = min(low[u], low[v]) if low[v] > disc[u]: bridges.append((u, v)) elif v != parent: low[u] = min(low[u], disc[v]) for i in range(n): if disc[i] == -1: dfs(i, -1) return sorted(bridges) n = 5 edges = [(0, 1), (1, 2), (2, 0), (1, 3), (3, 4)] print(find_bridges(n, edges))
Attempts:
2 left
💡 Hint
Look for edges where the low time of the connected node is greater than the discovery time of the current node.
✗ Incorrect
Edges (1, 3) and (3, 4) are bridges because removing them increases the number of connected components. The triangle formed by nodes 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?
Attempts:
2 left
💡 Hint
Think about how 'low' helps detect cycles and back edges.
✗ Incorrect
The 'low' value of a node is the smallest discovery time reachable from that node by following DFS tree edges and back edges. It helps identify if a node can reach an ancestor, which is key to finding bridges.
🔧 Debug
advancedIdentify the Bug in Tarjan's Bridge Code
What error will occur when running this modified Tarjan's algorithm code?
DSA Python
def find_bridges(n, edges): graph = [[] for _ in range(n)] for u, v in edges: graph[u].append(v) graph[v].append(u) time = 0 disc = [-1] * n low = [-1] * n bridges = [] def dfs(u, parent): disc[u] = time low[u] = time time += 1 for v in graph[u]: if disc[v] == -1: dfs(v, u) low[u] = min(low[u], low[v]) if low[v] > disc[u]: bridges.append((u, v)) elif v != parent: low[u] = min(low[u], disc[v]) for i in range(n): if disc[i] == -1: dfs(i, -1) return sorted(bridges)
Attempts:
2 left
💡 Hint
Check how the variable 'time' is used inside the dfs function.
✗ Incorrect
The variable 'time' is assigned inside dfs without declaring it as nonlocal, causing Python to treat it as local and raising UnboundLocalError when incremented.
🚀 Application
advancedNumber of Bridges in a Complete Graph
If you run Tarjan's algorithm to find bridges on a complete graph with 4 nodes (every node connected to every other), how many bridges will it find?
Attempts:
2 left
💡 Hint
Think about whether removing any edge disconnects the graph.
✗ Incorrect
In a complete graph, every node is connected to every other node directly. Removing any single edge does not disconnect the graph, so there are no bridges.
❓ Predict Output
expertOutput of Tarjan's Algorithm on a Complex Graph
What is the output (list of bridges) of the following Tarjan's algorithm code when run on the given graph?
DSA Python
def find_bridges(n, edges): graph = [[] for _ in range(n)] for u, v in edges: graph[u].append(v) graph[v].append(u) time = 0 disc = [-1] * n low = [-1] * n bridges = [] def dfs(u, parent): nonlocal time disc[u] = time low[u] = time time += 1 for v in graph[u]: if disc[v] == -1: dfs(v, u) low[u] = min(low[u], low[v]) if low[v] > disc[u]: bridges.append((u, v)) elif v != parent: low[u] = min(low[u], disc[v]) for i in range(n): if disc[i] == -1: dfs(i, -1) return sorted(bridges) n = 7 edges = [(0, 1), (1, 2), (2, 0), (1, 3), (3, 4), (4, 5), (5, 3), (4, 6)] print(find_bridges(n, edges))
Attempts:
2 left
💡 Hint
Check which edges connect components that would disconnect if removed.
✗ Incorrect
Edges (1, 3) and (4, 6) are bridges. The cycle between nodes 3, 4, 5 has no bridges.
