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 following C++ code that uses Kruskal's algorithm to find the MST edges?
DSA C++
#include <iostream> #include <vector> #include <algorithm> using namespace std; struct Edge { int u, v, w; bool operator<(const Edge& other) const { return w < other.w; } }; int parent[5]; int find(int x) { if (parent[x] != x) parent[x] = find(parent[x]); return parent[x]; } void unite(int a, int b) { a = find(a); b = find(b); if (a != b) parent[b] = a; } int main() { int n = 4; vector<Edge> edges = { {0, 1, 10}, {0, 2, 6}, {0, 3, 5}, {1, 3, 15}, {2, 3, 4} }; for (int i = 0; i < n; i++) parent[i] = i; sort(edges.begin(), edges.end()); vector<Edge> mst; for (auto& e : edges) { if (find(e.u) != find(e.v)) { unite(e.u, e.v); mst.push_back(e); } } for (auto& e : mst) { cout << e.u << "-" << e.v << " " << e.w << "\n"; } return 0; }
Attempts:
2 left
💡 Hint
Remember Kruskal's algorithm picks edges in increasing order of weight and avoids cycles.
✗ Incorrect
The edges sorted by weight are (2-3:4), (0-3:5), (0-2:6), (0-1:10), (1-3:15). The MST includes edges 2-3, 0-3, and 0-1 without forming cycles.
🧠 Conceptual
intermediateNumber of edges in MST for a connected graph
For a connected graph with 7 vertices, how many edges will the Minimum Spanning Tree (MST) contain?
Attempts:
2 left
💡 Hint
Think about how many edges connect all vertices without cycles.
✗ Incorrect
An MST for a connected graph with n vertices always has exactly n-1 edges.
🔧 Debug
advancedIdentify the error in this Kruskal's implementation snippet
What error will the following code snippet cause when running Kruskal's algorithm?
DSA C++
int find(int x) { while (parent[x] != x) { x = parent[x]; } return x; } void unite(int a, int b) { a = find(a); b = find(b); if (a != b) parent[a] = b; }
Attempts:
2 left
💡 Hint
Check how union assigns parents and how it affects the tree structure.
✗ Incorrect
In union, assigning parent[a] = b is incorrect if b is not root; it should assign parent[b] = a or use union by rank. This can cause incorrect MST.
❓ Predict Output
advancedOutput of MST total weight calculation
What is the output of the following code that calculates the total weight of MST using Kruskal's algorithm?
DSA C++
#include <iostream> #include <vector> #include <algorithm> using namespace std; struct Edge { int u, v, w; bool operator<(const Edge& other) const { return w < other.w; } }; int parent[6]; int find(int x) { if (parent[x] != x) parent[x] = find(parent[x]); return parent[x]; } void unite(int a, int b) { a = find(a); b = find(b); if (a != b) parent[b] = a; } int main() { int n = 5; vector<Edge> edges = { {0, 1, 2}, {0, 3, 6}, {1, 2, 3}, {1, 3, 8}, {1, 4, 5}, {2, 4, 7} }; for (int i = 0; i < n; i++) parent[i] = i; sort(edges.begin(), edges.end()); int total_weight = 0; for (auto& e : edges) { if (find(e.u) != find(e.v)) { unite(e.u, e.v); total_weight += e.w; } } cout << total_weight << "\n"; return 0; }
Attempts:
2 left
💡 Hint
Sum weights of edges chosen by Kruskal's algorithm in ascending order without cycles.
✗ Incorrect
Edges chosen: (0-1:2), (1-2:3), (1-4:5), (0-3:6). Total = 2+3+5+6=16 is correct because edges sorted: 2,3,5,6,7,8. MST picks 2,3,5,6 total 16.
🧠 Conceptual
expertWhy does Kruskal's algorithm use Union-Find?
Why is the Union-Find data structure essential in Kruskal's algorithm?
Attempts:
2 left
💡 Hint
Think about how Kruskal avoids cycles while adding edges.
✗ Incorrect
Union-Find helps quickly determine if two vertices are already connected (in the same set), preventing cycles when adding edges.
