Challenge - 5 Problems
Prim's MST Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Prim's Algorithm on a Small Graph
What is the printed state of the minimum spanning tree edges after running Prim's algorithm on the given graph?
DSA Rust
use std::collections::BinaryHeap;
use std::cmp::Reverse;
fn prim_mst(graph: &Vec<Vec<(usize, usize)>>) -> Vec<(usize, usize, usize)> {
let n = graph.len();
let mut visited = vec![false; n];
let mut heap = BinaryHeap::new();
let mut mst_edges = Vec::new();
visited[0] = true;
for &(to, weight) in &graph[0] {
heap.push(Reverse((weight, 0, to)));
}
while let Some(Reverse((weight, from, to))) = heap.pop() {
if visited[to] {
continue;
}
visited[to] = true;
mst_edges.push((from, to, weight));
for &(next, w) in &graph[to] {
if !visited[next] {
heap.push(Reverse((w, to, next)));
}
}
}
mst_edges
}
fn main() {
let graph = vec![
vec![(1, 2), (3, 6)],
vec![(0, 2), (2, 3), (3, 8), (4, 5)],
vec![(1, 3), (4, 7)],
vec![(0, 6), (1, 8), (4, 9)],
vec![(1, 5), (2, 7), (3, 9)],
];
let mst = prim_mst(&graph);
for (from, to, weight) in mst {
println!("{} -> {} with weight {}", from, to, weight);
}
}Attempts:
2 left
💡 Hint
Start from node 0 and always pick the smallest edge connecting to an unvisited node.
✗ Incorrect
Prim's algorithm starts at node 0, picks edge 0->1 (weight 2), then from nodes 0 and 1 picks 1->2 (3), then 1->4 (5), and finally 0->3 (6). This forms the minimum spanning tree.
🧠 Conceptual
intermediateUnderstanding Prim's Algorithm Data Structures
Which data structure is essential for efficiently selecting the next edge with the smallest weight in Prim's algorithm?
Attempts:
2 left
💡 Hint
Think about how to always get the smallest edge quickly.
✗ Incorrect
Prim's algorithm uses a priority queue (often implemented as a binary heap) to efficiently select the edge with the smallest weight connecting to the growing MST.
🔧 Debug
advancedIdentify the Bug in Prim's Algorithm Implementation
What error will this Rust code produce when running Prim's algorithm on a graph with disconnected components?
DSA Rust
fn prim_mst(graph: &Vec<Vec<(usize, usize)>>) -> Vec<(usize, usize, usize)> {
let n = graph.len();
let mut visited = vec![false; n];
let mut heap = std::collections::BinaryHeap::new();
let mut mst_edges = Vec::new();
visited[0] = true;
for &(to, weight) in &graph[0] {
heap.push(std::cmp::Reverse((weight, 0, to)));
}
while let Some(std::cmp::Reverse((weight, from, to))) = heap.pop() {
if visited[to] {
continue;
}
visited[to] = true;
mst_edges.push((from, to, weight));
for &(next, w) in &graph[to] {
if !visited[next] {
heap.push(std::cmp::Reverse((w, to, next)));
}
}
}
mst_edges
}
fn main() {
let graph = vec![
vec![(1, 1)],
vec![(0, 1)],
vec![(3, 2)],
vec![(2, 2)],
];
let mst = prim_mst(&graph);
for (from, to, weight) in mst {
println!("{} -> {} with weight {}", from, to, weight);
}
}Attempts:
2 left
💡 Hint
Consider what happens if the graph is not fully connected.
✗ Incorrect
Prim's algorithm as implemented starts from node 0 and only visits nodes reachable from it. Disconnected components are ignored, so MST edges are only for the connected component containing node 0.
❓ Predict Output
advancedOutput of Prim's Algorithm on a Weighted Graph with Multiple Equal Edges
What is the output of the following Prim's algorithm run on this graph?
DSA Rust
use std::collections::BinaryHeap;
use std::cmp::Reverse;
fn prim_mst(graph: &Vec<Vec<(usize, usize)>>) -> Vec<(usize, usize, usize)> {
let n = graph.len();
let mut visited = vec![false; n];
let mut heap = BinaryHeap::new();
let mut mst_edges = Vec::new();
visited[0] = true;
for &(to, weight) in &graph[0] {
heap.push(Reverse((weight, 0, to)));
}
while let Some(Reverse((weight, from, to))) = heap.pop() {
if visited[to] {
continue;
}
visited[to] = true;
mst_edges.push((from, to, weight));
for &(next, w) in &graph[to] {
if !visited[next] {
heap.push(Reverse((w, to, next)));
}
}
}
mst_edges
}
fn main() {
let graph = vec![
vec![(1, 1), (2, 1)],
vec![(0, 1), (2, 2), (3, 4)],
vec![(0, 1), (1, 2), (3, 3)],
vec![(1, 4), (2, 3)],
];
let mst = prim_mst(&graph);
for (from, to, weight) in mst {
println!("{} -> {} with weight {}", from, to, weight);
}
}Attempts:
2 left
💡 Hint
Edges with equal weights are chosen based on the order they appear in the heap.
✗ Incorrect
Prim's algorithm picks edges with smallest weight first. From node 0, edges to 1 and 2 both have weight 1. Both are added. Then from node 2, edge to 3 with weight 3 is chosen before edge 1->3 with weight 4.
🚀 Application
expertNumber of Edges in MST for a Complete Graph
Given a complete graph with 7 nodes, how many edges will the minimum spanning tree produced by Prim's algorithm contain?
Attempts:
2 left
💡 Hint
Recall the property of MSTs regarding number of edges and nodes.
✗ Incorrect
A minimum spanning tree for any connected graph with n nodes always has exactly n-1 edges.
