Challenge - 5 Problems
Articulation Points Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of articulation points detection on a simple graph
What is the output of the following Rust code that finds articulation points in a graph?
DSA Rust
use std::collections::HashMap;
fn articulation_points(
graph: &HashMap<usize, Vec<usize>>,
u: usize,
visited: &mut Vec<bool>,
disc: &mut Vec<usize>,
low: &mut Vec<usize>,
parent: &mut Vec<Option<usize>>,
ap: &mut Vec<bool>,
time: &mut usize,
) {
visited[u] = true;
*time += 1;
disc[u] = *time;
low[u] = *time;
let mut children = 0;
for &v in &graph[&u] {
if !visited[v] {
children += 1;
parent[v] = Some(u);
articulation_points(graph, v, visited, disc, low, parent, ap, time);
low[u] = low[u].min(low[v]);
if parent[u].is_none() && children > 1 {
ap[u] = true;
}
if parent[u].is_some() && low[v] >= disc[u] {
ap[u] = true;
}
} else if Some(v) != parent[u] {
low[u] = low[u].min(disc[v]);
}
}
}
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, 5]);
graph.insert(3, vec![2, 4]);
graph.insert(4, vec![3]);
graph.insert(5, vec![2, 6, 7]);
graph.insert(6, vec![5, 7]);
graph.insert(7, vec![5, 6]);
let n = 8;
let mut visited = vec![false; n];
let mut disc = vec![0; n];
let mut low = vec![0; n];
let mut parent = vec![None; n];
let mut ap = vec![false; n];
let mut time = 0;
for i in 0..n {
if !visited[i] {
articulation_points(&graph, i, &mut visited, &mut disc, &mut low, &mut parent, &mut ap, &mut time);
}
}
let result: Vec<usize> = ap.iter().enumerate().filter_map(|(i, &x)| if x { Some(i) } else { None }).collect();
println!("{:?}", result);
}Attempts:
2 left
💡 Hint
Focus on nodes whose removal increases the number of connected components.
✗ Incorrect
Nodes 2, 3, and 5 are articulation points because removing any of them disconnects parts of the graph.
🧠 Conceptual
intermediateUnderstanding articulation points in a graph
Which of the following best describes an articulation point in a connected graph?
Attempts:
2 left
💡 Hint
Think about the effect of removing a vertex on graph connectivity.
✗ Incorrect
An articulation point is a vertex whose removal disconnects the graph into more components.
🔧 Debug
advancedIdentify the error in articulation points detection code
What error will the following Rust code produce when run?
DSA Rust
use std::collections::HashMap;
fn articulation_points(
graph: &HashMap<usize, Vec<usize>>,
u: usize,
visited: &mut Vec<bool>,
disc: &mut Vec<usize>,
low: &mut Vec<usize>,
parent: &mut Vec<Option<usize>>,
ap: &mut Vec<bool>,
time: &mut usize,
) {
visited[u] = true;
*time += 1;
disc[u] = *time;
low[u] = *time;
let mut children = 0;
for &v in &graph[&u] {
if !visited[v] {
children += 1;
parent[v] = Some(u);
articulation_points(graph, v, visited, disc, low, parent, ap, time);
low[u] = low[u].min(low[v]);
if parent[u].is_none() && children > 1 {
ap[u] = true;
}
if parent[u].is_some() && low[v] >= disc[u] {
ap[u] = true;
}
} else if Some(v) != parent[u] {
low[u] = low[u].min(disc[v]);
}
}
}
fn main() {
let mut graph = HashMap::new();
graph.insert(0, vec![1]);
graph.insert(1, vec![0, 2]);
let n = 3;
let mut visited = vec![false; n];
let mut disc = vec![0; n];
let mut low = vec![0; n];
let mut parent = vec![None; n];
let mut ap = vec![false; n];
let mut time = 0;
for i in 0..n {
if !visited[i] {
articulation_points(&graph, i, &mut visited, &mut disc, &mut low, &mut parent, &mut ap, &mut time);
}
}
let result: Vec<usize> = ap.iter().enumerate().filter_map(|(i, &x)| if x { Some(i) } else { None }).collect();
println!("{:?}", result);
}Attempts:
2 left
💡 Hint
Check if all nodes have entries in the graph map.
✗ Incorrect
Node 2's adjacency list is missing some neighbors causing a panic when accessed.
📝 Syntax
advancedIdentify the syntax error in articulation points function
Which option contains a syntax error in the Rust articulation points function?
DSA Rust
fn articulation_points(
graph: &HashMap<usize, Vec<usize>>,
u: usize,
visited: &mut Vec<bool>,
disc: &mut Vec<usize>,
low: &mut Vec<usize>,
parent: &mut Vec<Option<usize>>,
ap: &mut Vec<bool>,
time: &mut usize,
) {
visited[u] = true
*time += 1;
disc[u] = *time;
low[u] = *time;
// rest of code omitted
}Attempts:
2 left
💡 Hint
Look carefully at line endings.
✗ Incorrect
Rust requires semicolons after statements; missing one causes a syntax error.
🚀 Application
expertNumber of articulation points in a complex graph
Given a graph with 10 nodes and edges:
0-1, 1-2, 2-3, 3-4, 2-5, 5-6, 6-7, 7-5, 6-8, 8-9
How many articulation points does this graph have?
Attempts:
2 left
💡 Hint
Identify nodes whose removal disconnects parts of the graph.
✗ Incorrect
Nodes 1, 2, 3, and 6 are articulation points in this graph.
