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 Java code that runs Kruskal's algorithm on a graph with 4 vertices and edges with weights?
DSA Java
import java.util.*; class Edge implements Comparable<Edge> { int src, dest, weight; public Edge(int s, int d, int w) { src = s; dest = d; weight = w; } public int compareTo(Edge e) { return this.weight - e.weight; } } class Graph { int V; List<Edge> edges; public Graph(int v) { V = v; edges = new ArrayList<>(); } void addEdge(int s, int d, int w) { edges.add(new Edge(s, d, w)); } int find(int[] parent, int i) { if (parent[i] == i) return i; return find(parent, parent[i]); } void union(int[] parent, int[] rank, int x, int y) { int xroot = find(parent, x); int yroot = find(parent, y); if (rank[xroot] < rank[yroot]) parent[xroot] = yroot; else if (rank[yroot] < rank[xroot]) parent[yroot] = xroot; else { parent[yroot] = xroot; rank[xroot]++; } } List<Edge> kruskalMST() { Collections.sort(edges); int[] parent = new int[V]; int[] rank = new int[V]; for (int i = 0; i < V; i++) { parent[i] = i; rank[i] = 0; } List<Edge> result = new ArrayList<>(); for (Edge e : edges) { int x = find(parent, e.src); int y = find(parent, e.dest); if (x != y) { result.add(e); union(parent, rank, x, y); } } return result; } } public class Main { public static void main(String[] args) { Graph g = new Graph(4); g.addEdge(0, 1, 10); g.addEdge(0, 2, 6); g.addEdge(0, 3, 5); g.addEdge(1, 3, 15); g.addEdge(2, 3, 4); List<Edge> mst = g.kruskalMST(); for (Edge e : mst) { System.out.print("(" + e.src + "," + e.dest + ") "); } } }
Attempts:
2 left
💡 Hint
Remember Kruskal's algorithm picks edges in increasing order of weight without forming 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 picks (2,3), (0,3), and (0,1) to connect all vertices without cycles.
🧠 Conceptual
intermediateUnderstanding cycle detection in Kruskal's algorithm
In Kruskal's algorithm, what is the main purpose of the 'union-find' data structure?
Attempts:
2 left
💡 Hint
Think about how Kruskal avoids adding edges that form loops.
✗ Incorrect
Union-find helps quickly check if two vertices belong to the same connected component, preventing cycles when adding edges.
🔧 Debug
advancedIdentify the error in this Kruskal's implementation snippet
What error will the following Java code snippet cause when running Kruskal's algorithm?
DSA Java
int find(int[] parent, int i) { while (parent[i] != i) { i = parent[i]; } return i; } void union(int[] parent, int[] rank, int x, int y) { int xroot = find(parent, x); int yroot = find(parent, y); if (rank[xroot] < rank[yroot]) parent[xroot] = yroot; else if (rank[yroot] < rank[xroot]) parent[yroot] = xroot; else { parent[yroot] = xroot; rank[xroot]++; } } // parent array initialized as parent[i] = -1 for all i
Attempts:
2 left
💡 Hint
Check how the parent array is initialized and how find uses it.
✗ Incorrect
If parent[i] is -1 initially, the while loop condition parent[i] != i never becomes false, causing infinite loop and stack overflow.
❓ Predict Output
advancedOutput of MST total weight calculation
What is the output of the following Java code that calculates the total weight of the MST using Kruskal's algorithm?
DSA Java
import java.util.*; class Edge implements Comparable<Edge> { int src, dest, weight; public Edge(int s, int d, int w) { src = s; dest = d; weight = w; } public int compareTo(Edge e) { return this.weight - e.weight; } } class Graph { int V; List<Edge> edges; public Graph(int v) { V = v; edges = new ArrayList<>(); } void addEdge(int s, int d, int w) { edges.add(new Edge(s, d, w)); } int find(int[] parent, int i) { if (parent[i] == i) return i; return find(parent, parent[i]); } void union(int[] parent, int[] rank, int x, int y) { int xroot = find(parent, x); int yroot = find(parent, y); if (rank[xroot] < rank[yroot]) parent[xroot] = yroot; else if (rank[yroot] < rank[xroot]) parent[yroot] = xroot; else { parent[yroot] = xroot; rank[xroot]++; } } int kruskalMSTWeight() { Collections.sort(edges); int[] parent = new int[V]; int[] rank = new int[V]; for (int i = 0; i < V; i++) { parent[i] = i; rank[i] = 0; } int totalWeight = 0; for (Edge e : edges) { int x = find(parent, e.src); int y = find(parent, e.dest); if (x != y) { totalWeight += e.weight; union(parent, rank, x, y); } } return totalWeight; } } public class Main { public static void main(String[] args) { Graph g = new Graph(5); g.addEdge(0, 1, 2); g.addEdge(0, 3, 6); g.addEdge(1, 2, 3); g.addEdge(1, 3, 8); g.addEdge(1, 4, 5); g.addEdge(2, 4, 7); g.addEdge(3, 4, 9); System.out.println(g.kruskalMSTWeight()); } }
Attempts:
2 left
💡 Hint
Sum the weights of edges chosen by Kruskal's MST.
✗ Incorrect
Edges chosen: (0,1)=2, (1,2)=3, (1,4)=5, (0,3)=6. Total = 2+3+5+6=16. The MST edges are (0,1), (1,2), (1,4), (0,3) total 16.
🚀 Application
expertMinimum Spanning Tree edge count for disconnected graph
Given a graph with 6 vertices and edges only connecting vertices {0,1,2} and separately {3,4,5}, what is the number of edges in the MST returned by Kruskal's algorithm?
Attempts:
2 left
💡 Hint
MST edges = number of vertices in connected component - 1, summed over all components.
✗ Incorrect
The graph has two disconnected parts each with 3 vertices. Each part's MST has 2 edges. Total MST edges = 2 + 2 = 4.
