Challenge - 5 Problems
DAG Shortest Path Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of shortest path distances in DAG
What is the output of the shortest path distances array after running the given Rust code on the DAG?
DSA Rust
use std::collections::VecDeque;
fn shortest_path_dag(n: usize, edges: &[(usize, usize, i32)], start: usize) -> Vec<Option<i32>> {
let mut graph = vec![vec![]; n];
let mut indegree = vec![0; n];
for &(u, v, w) in edges {
graph[u].push((v, w));
indegree[v] += 1;
}
let mut dist = vec![None; n];
dist[start] = Some(0);
let mut queue = VecDeque::new();
for i in 0..n {
if indegree[i] == 0 {
queue.push_back(i);
}
}
while let Some(u) = queue.pop_front() {
if let Some(d) = dist[u] {
for &(v, w) in &graph[u] {
if dist[v].is_none() || dist[v].unwrap() > d + w {
dist[v] = Some(d + w);
}
indegree[v] -= 1;
if indegree[v] == 0 {
queue.push_back(v);
}
}
} else {
for &(v, _) in &graph[u] {
indegree[v] -= 1;
if indegree[v] == 0 {
queue.push_back(v);
}
}
}
}
dist
}
fn main() {
let n = 5;
let edges = vec![
(0, 1, 3),
(0, 2, 6),
(1, 2, 4),
(1, 3, 4),
(2, 3, 8),
(1, 4, 11),
(3, 4, -4),
];
let start = 0;
let dist = shortest_path_dag(n, &edges, start);
println!("{:?}", dist);
}Attempts:
2 left
💡 Hint
Trace the graph edges and update distances using topological order.
✗ Incorrect
The shortest path distances from node 0 are calculated by relaxing edges in topological order. The distances are: 0 to 0 is 0, 0 to 1 is 3, 0 to 2 is 7 (via 1), 0 to 3 is 7 (via 1), and 0 to 4 is 3 (via 3 with negative edge).
🧠 Conceptual
intermediateWhy use topological order for shortest path in DAG?
Why is topological order important when finding shortest paths in a Directed Acyclic Graph (DAG)?
Attempts:
2 left
💡 Hint
Think about how processing order affects distance updates in graphs without cycles.
✗ Incorrect
Topological order processes nodes only after all nodes leading into them are processed, ensuring that when we update distances, all shorter paths to predecessors are already known. This avoids revisiting nodes multiple times and works because DAGs have no cycles.
🔧 Debug
advancedIdentify the bug causing incorrect shortest path output
Given the Rust code below for shortest path in DAG, which option correctly identifies the bug causing wrong distances?
DSA Rust
fn shortest_path_dag_bug(n: usize, edges: &[(usize, usize, i32)], start: usize) -> Vec<Option<i32>> {
let mut graph = vec![vec![]; n];
let mut indegree = vec![0; n];
for &(u, v, w) in edges {
graph[u].push((v, w));
indegree[v] += 1;
}
let mut dist = vec![None; n];
dist[start] = Some(0);
let mut queue = std::collections::VecDeque::new();
for i in 0..n {
if indegree[i] == 0 {
queue.push_back(i);
}
}
while let Some(u) = queue.pop_front() {
for &(v, w) in &graph[u] {
if dist[u].is_some() {
let d = dist[u].unwrap();
if dist[v].is_none() || dist[v].unwrap() > d + w {
dist[v] = Some(d + w);
}
}
indegree[v] -= 1;
if indegree[v] == 0 {
queue.push_back(v);
}
}
}
dist
}Attempts:
2 left
💡 Hint
Look at how nodes with no known distance are processed.
✗ Incorrect
The code processes all nodes regardless of whether their distance is known. Nodes with dist[u] == None should not relax edges because their distance is unknown, leading to incorrect updates.
📝 Syntax
advancedIdentify the syntax error in Rust code for shortest path in DAG
Which option contains a syntax error in the Rust code snippet for shortest path in DAG?
DSA Rust
let mut dist = vec![None; n]; dist[start] = Some(0); for &(u, v, w) in edges { graph[u].push((v, w)) indegree[v] += 1; }
Attempts:
2 left
💡 Hint
Check line endings carefully in Rust code.
✗ Incorrect
Rust requires semicolons at the end of statements. The line graph[u].push((v, w)) is missing a semicolon, causing a syntax error.
🚀 Application
expertCalculate shortest path distances for a complex DAG
Given the DAG with edges and weights below, what is the shortest path distance from node 0 to node 5 after running the shortest path algorithm using topological order?
DSA Rust
Edges: (0, 1, 2) (0, 2, 4) (1, 2, 1) (1, 3, 7) (2, 4, 3) (3, 5, 1) (4, 3, 2) (4, 5, 5) Start node: 0 Number of nodes: 6
Attempts:
2 left
💡 Hint
Trace shortest paths step-by-step using topological order and edge relaxation.
✗ Incorrect
The shortest path from 0 to 5 is 0->1->2->4->3->5 with distances: 0->1=2, 1->2=1 (total 3), 2->4=3 (total 6), 4->3=2 (total 8), 3->5=1 (total 9). But 0->1->3->5 is 2+7+1=10, and 0->2->4->5 is 4+3+5=12. The shortest is 9 via 0->1->2->4->3->5.
