Challenge - 5 Problems
Tarjan Bridge Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Tarjan's Bridge Finding on a Simple Graph
What is the output (list of bridges) of the following Go code implementing Tarjan's algorithm on the given graph?
DSA Go
package main import "fmt" var ( time int graph map[int][]int visited map[int]bool disc map[int]int low map[int]int bridges [][2]int ) func dfs(u, parent int) { visited[u] = true time++ disc[u] = time low[u] = time for _, v := range graph[u] { if v == parent { continue } if !visited[v] { dfs(v, u) if low[v] > disc[u] { bridges = append(bridges, [2]int{u, v}) } if low[v] < low[u] { low[u] = low[v] } } else { if disc[v] < low[u] { low[u] = disc[v] } } } } func main() { graph = map[int][]int{ 0: {1, 2}, 1: {0, 2}, 2: {0, 1, 3}, 3: {2, 4}, 4: {3}, } visited = make(map[int]bool) disc = make(map[int]int) low = make(map[int]int) bridges = make([][2]int, 0) time = 0 for u := range graph { if !visited[u] { dfs(u, -1) } } fmt.Println(bridges) }
Attempts:
2 left
💡 Hint
Focus on edges where the low value of the adjacent node is greater than the discovery time of the current node.
✗ Incorrect
The edges (2,3) and (3,4) are bridges because removing them increases the number of connected components. The other edges are part of cycles.
🧠 Conceptual
intermediateUnderstanding Low and Discovery Times in Tarjan's Algorithm
In Tarjan's algorithm for finding bridges, what does the 'low' value of a node represent?
Attempts:
2 left
💡 Hint
Think about the earliest visited node reachable from the current node or its subtree.
✗ Incorrect
The 'low' value is the smallest discovery time reachable from the node by following zero or more tree edges and possibly one back edge.
🔧 Debug
advancedIdentify the Bug in Tarjan's Bridge Detection Code
What error will occur when running this Go code snippet for Tarjan's algorithm?
DSA Go
func dfs(u, parent int) { visited[u] = true time++ disc[u] = time low[u] = time for _, v := range graph[u] { if v == parent { continue } if !visited[v] { dfs(v, u) if low[v] >= disc[u] { bridges = append(bridges, [2]int{u, v}) } if low[v] < low[u] { low[u] = low[v] } } else { if disc[v] < low[u] { low[u] = disc[v] } } } }
Attempts:
2 left
💡 Hint
Check the condition that decides if an edge is a bridge.
✗ Incorrect
The condition 'low[v] >= disc[u]' is incorrect for bridges; it should be 'low[v] > disc[u]'. Using '>=' causes edges that are part of cycles to be wrongly marked as bridges.
🚀 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), (5-6), how many bridges does Tarjan's algorithm find?
Attempts:
2 left
💡 Hint
Look for edges whose removal increases connected components.
✗ Incorrect
Edges (1-3) and (5-6) are bridges. The rest form cycles.
❓ Predict Output
expertOutput of Tarjan's Algorithm on a Disconnected Graph
What is the output of the following Go code that runs Tarjan's bridge-finding algorithm on a disconnected graph?
DSA Go
package main import "fmt" var ( time int graph map[int][]int visited map[int]bool disc map[int]int low map[int]int bridges [][2]int ) func dfs(u, parent int) { visited[u] = true time++ disc[u] = time low[u] = time for _, v := range graph[u] { if v == parent { continue } if !visited[v] { dfs(v, u) if low[v] > disc[u] { bridges = append(bridges, [2]int{u, v}) } if low[v] < low[u] { low[u] = low[v] } } else { if disc[v] < low[u] { low[u] = disc[v] } } } } func main() { graph = map[int][]int{ 0: {1}, 1: {0}, 2: {3}, 3: {2, 4}, 4: {3}, } visited = make(map[int]bool) disc = make(map[int]int) low = make(map[int]int) bridges = make([][2]int, 0) time = 0 for u := range graph { if !visited[u] { dfs(u, -1) } } fmt.Println(bridges) }
Attempts:
2 left
💡 Hint
Remember to run DFS on all disconnected components.
✗ Incorrect
The graph has three edges that are bridges: (0,1), (2,3), and (3,4). Each removal increases components.
