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 edges list after running Kruskal's algorithm on the given graph?
DSA C#
using System; using System.Collections.Generic; class Edge : IComparable<Edge> { public int Src, Dest, Weight; public Edge(int s, int d, int w) { Src = s; Dest = d; Weight = w; } public int CompareTo(Edge other) => Weight.CompareTo(other.Weight); } class DisjointSet { private int[] parent, rank; public DisjointSet(int n) { parent = new int[n]; rank = new int[n]; for (int i = 0; i < n; i++) parent[i] = i; } public int Find(int x) { if (parent[x] != x) parent[x] = Find(parent[x]); return parent[x]; } public bool Union(int x, int y) { int rx = Find(x), ry = Find(y); if (rx == ry) return false; if (rank[rx] < rank[ry]) parent[rx] = ry; else if (rank[ry] < rank[rx]) parent[ry] = rx; else { parent[ry] = rx; rank[rx]++; } return true; } } class Program { static void Main() { int V = 4; List<Edge> edges = new List<Edge> { new Edge(0, 1, 10), new Edge(0, 2, 6), new Edge(0, 3, 5), new Edge(1, 3, 15), new Edge(2, 3, 4) }; edges.Sort(); DisjointSet ds = new DisjointSet(V); List<Edge> mst = new List<Edge>(); foreach (var e in edges) { if (ds.Union(e.Src, e.Dest)) mst.Add(e); } foreach (var e in mst) { Console.WriteLine($"{e.Src} - {e.Dest}: {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 if they connect different sets.
The edges chosen are (2-3:4), (0-3:5), and (0-1:10) forming the MST.
🧠 Conceptual
intermediateWhy does Kruskal's algorithm use a disjoint set?
What is the main purpose of using a disjoint set (union-find) data structure in Kruskal's algorithm?
Attempts:
2 left
💡 Hint
Think about how to avoid cycles when adding edges.
✗ Incorrect
The disjoint set helps quickly determine if two vertices are already connected, preventing cycles.
🔧 Debug
advancedIdentify the error in this Kruskal's implementation snippet
What error will this code snippet cause when running Kruskal's algorithm?
DSA C#
DisjointSet ds = new DisjointSet(V); foreach (var e in edges) { if (ds.Find(e.Src) != ds.Find(e.Dest)) { mst.Add(e); } }
Attempts:
2 left
💡 Hint
Check if sets are merged after adding edges.
✗ Incorrect
The code checks if vertices are in different sets but never merges them, so cycles can form.
❓ Predict Output
advancedOutput of MST total weight calculation
What is the total weight of the MST after running Kruskal's algorithm on this graph?
DSA C#
int V = 5; List<Edge> edges = new List<Edge> { new Edge(0, 1, 2), new Edge(0, 3, 6), new Edge(1, 2, 3), new Edge(1, 3, 8), new Edge(1, 4, 5), new Edge(2, 4, 7), new Edge(3, 4, 9) }; edges.Sort(); DisjointSet ds = new DisjointSet(V); int totalWeight = 0; foreach (var e in edges) { if (ds.Union(e.Src, e.Dest)) totalWeight += e.Weight; } Console.WriteLine(totalWeight);
Attempts:
2 left
💡 Hint
Add weights of edges chosen by Kruskal's algorithm.
✗ Incorrect
The MST edges chosen have weights 2, 3, 5, and 6, totaling 16.
🧠 Conceptual
expertWhy does Kruskal's algorithm always produce a minimum spanning tree?
Which property ensures that Kruskal's algorithm always produces a minimum spanning tree?
Attempts:
2 left
💡 Hint
Think about why picking the smallest edges first works.
✗ Incorrect
Kruskal's algorithm relies on the greedy choice and cut properties to ensure the MST is minimal and contains no cycles.
