Challenge - 5 Problems
Tarjan's 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 Rust code implementing Tarjan's algorithm on the given graph?
DSA Rust
use std::collections::HashMap;
fn tarjan_bridges(
u: usize,
parent: Option<usize>,
time: &mut usize,
graph: &HashMap<usize, Vec<usize>>,
disc: &mut Vec<Option<usize>>,
low: &mut Vec<usize>,
bridges: &mut Vec<(usize, usize)>,
) {
*time += 1;
disc[u] = Some(*time);
low[u] = *time;
if let Some(neighbors) = graph.get(&u) {
for &v in neighbors {
if disc[v].is_none() {
tarjan_bridges(v, Some(u), time, graph, disc, low, bridges);
low[u] = low[u].min(low[v]);
if low[v] > disc[u].unwrap() {
bridges.push((u, v));
}
} else if Some(v) != parent {
low[u] = low[u].min(disc[v].unwrap());
}
}
}
}
fn main() {
let mut graph = HashMap::new();
graph.insert(0, vec![1, 2]);
graph.insert(1, vec![0, 2]);
graph.insert(2, vec![0, 1, 3]);
graph.insert(3, vec![2, 4]);
graph.insert(4, vec![3]);
let n = 5;
let mut disc = vec![None; n];
let mut low = vec![0; n];
let mut bridges = Vec::new();
let mut time = 0;
for i in 0..n {
if disc[i].is_none() {
tarjan_bridges(i, None, &mut time, &graph, &mut disc, &mut low, &mut bridges);
}
}
bridges.sort();
println!("{:?}", bridges);
}Attempts:
2 left
💡 Hint
Look for edges where the low value of the neighbor is greater than the discovery time of the current node.
✗ Incorrect
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 for bridges, what does the 'low' value of a node represent?
Attempts:
2 left
💡 Hint
Think about what 'low' helps to detect in the graph.
✗ Incorrect
'Low' is the smallest discovery time reachable from the node by following tree edges and back edges, helping detect cycles and bridges.
🔧 Debug
advancedIdentify the Bug in Tarjan's Bridge Detection
What error will the following Rust code produce when running Tarjan's algorithm for bridges?
DSA Rust
fn tarjan(
u: usize,
parent: usize,
time: &mut usize,
graph: &Vec<Vec<usize>>,
disc: &mut Vec<Option<usize>>,
low: &mut Vec<usize>,
bridges: &mut Vec<(usize, usize)>,
) {
*time += 1;
disc[u] = Some(*time);
low[u] = *time;
for &v in &graph[u] {
if disc[v].is_none() {
tarjan(v, u, time, graph, disc, low, bridges);
low[u] = low[u].min(low[v]);
if low[v] > disc[u].unwrap() {
bridges.push((u, v));
}
} else if v != parent {
low[u] = low[u].min(disc[v].unwrap());
}
}
}
fn main() {
let graph = vec![
vec![1, 2],
vec![0, 2],
vec![0, 1, 3],
vec![2, 4],
vec![3],
];
let n = graph.len();
let mut disc = vec![None; n];
let mut low = vec![0; n];
let mut bridges = Vec::new();
let mut time = 0;
for i in 0..n {
if disc[i].is_none() {
tarjan(i, usize::MAX, &mut time, &graph, &mut disc, &mut low, &mut bridges);
}
}
bridges.sort();
println!("{:?}", bridges);
}Attempts:
2 left
💡 Hint
Check how parent is handled and if usize::MAX causes issues.
✗ Incorrect
Using usize::MAX as parent is valid since it never matches any node index. The code runs correctly and outputs the bridges.
❓ Predict Output
advancedOutput of Bridge Detection on a Complex Graph
What is the output list of bridges for the following graph using Tarjan's algorithm?
DSA Rust
use std::collections::HashMap;
fn tarjan(
u: usize,
parent: Option<usize>,
time: &mut usize,
graph: &HashMap<usize, Vec<usize>>,
disc: &mut Vec<Option<usize>>,
low: &mut Vec<usize>,
bridges: &mut Vec<(usize, usize)>,
) {
*time += 1;
disc[u] = Some(*time);
low[u] = *time;
if let Some(neighbors) = graph.get(&u) {
for &v in neighbors {
if disc[v].is_none() {
tarjan(v, Some(u), time, graph, disc, low, bridges);
low[u] = low[u].min(low[v]);
if low[v] > disc[u].unwrap() {
bridges.push((u, v));
}
} else if Some(v) != parent {
low[u] = low[u].min(disc[v].unwrap());
}
}
}
}
fn main() {
let mut graph = HashMap::new();
graph.insert(0, vec![1, 2]);
graph.insert(1, vec![0, 2, 3]);
graph.insert(2, vec![0, 1]);
graph.insert(3, vec![1, 4, 5]);
graph.insert(4, vec![3, 5]);
graph.insert(5, vec![3, 4, 6]);
graph.insert(6, vec![5]);
let n = 7;
let mut disc = vec![None; n];
let mut low = vec![0; n];
let mut bridges = Vec::new();
let mut time = 0;
for i in 0..n {
if disc[i].is_none() {
tarjan(i, None, &mut time, &graph, &mut disc, &mut low, &mut bridges);
}
}
println!("{:?}", bridges);
}Attempts:
2 left
💡 Hint
Check edges where removing them disconnects the graph.
✗ Incorrect
Edges (1,3) and (5,6) are bridges because their removal increases connected components. Other edges are part of cycles.
🧠 Conceptual
expertWhy Tarjan's Algorithm Uses DFS for Bridges
Why does Tarjan's algorithm use Depth-First Search (DFS) to find bridges in a graph?
Attempts:
2 left
💡 Hint
Think about how discovery and low times relate to DFS traversal.
✗ Incorrect
DFS allows tracking discovery times and back edges, which are essential to identify bridges by comparing low and discovery times.
