Challenge - 5 Problems
Kruskal MST Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Kruskal's MST edges
What is the output of the edges included in the MST after running Kruskal's algorithm on the given graph?
DSA Rust
use std::collections::HashMap; #[derive(Debug, Clone)] struct Edge { u: usize, v: usize, weight: i32, } struct DisjointSet { parent: Vec<usize>, rank: Vec<usize>, } impl DisjointSet { fn new(n: usize) -> Self { DisjointSet { parent: (0..n).collect(), rank: vec![0; n], } } fn find(&mut self, x: usize) -> usize { if self.parent[x] != x { self.parent[x] = self.find(self.parent[x]); } self.parent[x] } fn union(&mut self, x: usize, y: usize) -> bool { let xroot = self.find(x); let yroot = self.find(y); if xroot == yroot { return false; } if self.rank[xroot] < self.rank[yroot] { self.parent[xroot] = yroot; } else if self.rank[xroot] > self.rank[yroot] { self.parent[yroot] = xroot; } else { self.parent[yroot] = xroot; self.rank[xroot] += 1; } true } } fn kruskal(n: usize, edges: &mut Vec<Edge>) -> Vec<Edge> { edges.sort_by_key(|e| e.weight); let mut ds = DisjointSet::new(n); let mut mst = Vec::new(); for edge in edges.iter() { if ds.union(edge.u, edge.v) { mst.push(edge.clone()); } } mst } fn main() { let mut edges = vec![ Edge { u: 0, v: 1, weight: 10 }, Edge { u: 0, v: 2, weight: 6 }, Edge { u: 0, v: 3, weight: 5 }, Edge { u: 1, v: 3, weight: 15 }, Edge { u: 2, v: 3, weight: 4 }, ]; let mst = kruskal(4, &mut edges); for e in mst { println!("{} - {} : {}", e.u, e.v, e.weight); } }
Attempts:
2 left
💡 Hint
Sort edges by weight and pick edges that don't form a cycle.
✗ Incorrect
Kruskal's algorithm sorts edges by weight and adds edges to MST if they connect different sets.
Edges chosen are (2-3:4), (0-3:5), and (0-1:10).
🧠 Conceptual
intermediateNumber of edges in MST for a graph with 7 vertices
For a connected undirected graph with 7 vertices, how many edges will the Minimum Spanning Tree (MST) contain?
Attempts:
2 left
💡 Hint
MST connects all vertices with minimum edges to avoid cycles.
✗ Incorrect
An MST for a connected graph with n vertices always has n-1 edges.
🔧 Debug
advancedIdentify the bug in this Kruskal's union function
What error will this union function cause when used in Kruskal's algorithm?
DSA Rust
fn union(&mut self, x: usize, y: usize) -> bool { let xroot = self.find(x); let yroot = self.find(y); if xroot == yroot { return false; } if self.rank[xroot] > self.rank[yroot] { self.parent[yroot] = xroot; } else { self.parent[xroot] = yroot; if self.rank[xroot] == self.rank[yroot] { self.rank[yroot] += 1; } } true }
Attempts:
2 left
💡 Hint
Check which root's rank is incremented after union.
✗ Incorrect
The rank should be incremented on the root that becomes the parent.
Here, rank is incremented on yroot even if xroot becomes parent, causing wrong rank info.
❓ Predict Output
advancedOutput of MST total weight calculation
What is the total weight of the MST computed by this code snippet?
DSA Rust
let mut edges = vec![
Edge { u: 0, v: 1, weight: 7 },
Edge { u: 0, v: 3, weight: 5 },
Edge { u: 1, v: 2, weight: 8 },
Edge { u: 1, v: 3, weight: 9 },
Edge { u: 1, v: 4, weight: 7 },
Edge { u: 2, v: 4, weight: 5 },
Edge { u: 3, v: 4, weight: 15 },
Edge { u: 3, v: 5, weight: 6 },
Edge { u: 4, v: 5, weight: 8 },
Edge { u: 4, v: 6, weight: 9 },
Edge { u: 5, v: 6, weight: 11 },
];
let mst = kruskal(7, &mut edges);
let total_weight: i32 = mst.iter().map(|e| e.weight).sum();
println!("{}", total_weight);Attempts:
2 left
💡 Hint
Sum weights of edges chosen by Kruskal's algorithm.
✗ Incorrect
The MST edges chosen have weights: 5, 5, 6, 7, 7, 7 (total 37).
🧠 Conceptual
expertWhy does Kruskal's algorithm use a Disjoint Set data structure?
What is the main reason Kruskal's algorithm uses a Disjoint Set (Union-Find) data structure?
Attempts:
2 left
💡 Hint
Think about cycle detection in MST building.
✗ Incorrect
Disjoint Set helps quickly determine if two vertices are already connected, preventing cycles.
