Minimum Spanning Tree Kruskal's Algorithm in DSA Java - Time & Space Complexity
We want to understand how the time needed to find the minimum spanning tree using Kruskal's algorithm changes as the graph grows.
Specifically, how does the number of edges and vertices affect the work done?
Analyze the time complexity of the following code snippet.
// Kruskal's algorithm to find MST
class Edge implements Comparable<Edge> {
int src, dest, weight;
public int compareTo(Edge other) {
return this.weight - other.weight;
}
}
void kruskalMST(List<Edge> edges, int V) {
Collections.sort(edges); // sort edges by weight
DisjointSet ds = new DisjointSet(V);
for (Edge e : edges) {
if (ds.find(e.src) != ds.find(e.dest)) {
ds.union(e.src, e.dest);
// add edge to MST
}
}
}
This code sorts all edges by weight, then adds edges to the MST if they connect different sets, using a disjoint set data structure.
- Primary operation: Sorting all edges and iterating through each edge once.
- How many times: Sorting takes time based on number of edges E; the loop runs once per edge (E times).
- Disjoint set operations: Each union/find operation is almost constant time due to path compression.
As the number of edges (E) and vertices (V) grow, the sorting step dominates the time.
| Input Size (V, E) | Approx. Operations |
|---|---|
| V=10, E=15 | Sorting ~ 15 log 15 ≈ 60 operations; loop 15 times |
| V=100, E=500 | Sorting ~ 500 log 500 ≈ 4500 operations; loop 500 times |
| V=1000, E=5000 | Sorting ~ 5000 log 5000 ≈ 65000 operations; loop 5000 times |
Pattern observation: The sorting step grows faster than the loop, so total time grows roughly proportional to E log E.
Time Complexity: O(E log E)
This means the time grows mostly with sorting all edges, which depends on the number of edges and grows a bit faster than just the number of edges.
[X] Wrong: "The algorithm runs in O(V^2) because it checks all pairs of vertices."
[OK] Correct: Kruskal's algorithm works mainly with edges, not all vertex pairs. It sorts edges and uses efficient union-find, so time depends on edges, not all vertex pairs.
Understanding how Kruskal's algorithm scales helps you explain your approach clearly and shows you know how to handle graphs efficiently.
"What if we used a different data structure for union-find without path compression? How would the time complexity change?"
Practice
Solution
Step 1: Understand Kruskal's algorithm purpose
Kruskal's algorithm is designed to find a subset of edges that connects all vertices with the minimum total edge weight and no cycles.Step 2: Compare with other graph problems
Shortest path and cycle detection are different problems solved by other algorithms like Dijkstra or DFS.Final Answer:
To find the minimum spanning tree of a graph -> Option CQuick Check:
Minimum Spanning Tree = Find minimum edges connecting all nodes [OK]
- Confusing MST with shortest path
- Thinking Kruskal detects cycles only
- Assuming it sorts vertices
Solution
Step 1: Identify the need for set operations
Kruskal's algorithm requires checking if two vertices belong to the same set and merging sets without cycles.Step 2: Match data structure to operations
Disjoint Set (Union-Find) supports fast union and find operations, ideal for this purpose.Final Answer:
Disjoint Set (Union-Find) -> Option AQuick Check:
Union-Find = fast set checks and merges [OK]
- Using ArrayList for set operations
- Confusing PriorityQueue with Union-Find
- Using HashMap which is not efficient here
Vertices: 1, 2, 3, 4
Edges with weights:
1-2: 1
1-3: 3
2-3: 2
3-4: 4
2-4: 5
Solution
Step 1: Sort edges by weight
Edges sorted: 1-2(1), 2-3(2), 1-3(3), 3-4(4), 2-4(5)Step 2: Pick edges without cycles
Pick 1-2, then 2-3 (no cycle), then 1-3 creates cycle so skip, pick 3-4 (no cycle), skip 2-4 (cycle)Final Answer:
[1-2, 2-3, 3-4] -> Option AQuick Check:
Edges chosen by increasing weight without cycles [OK]
- Picking edges that form cycles
- Not sorting edges by weight
- Choosing heavier edges first
for (Edge e : edges) {
if (findSet(e.u) != findSet(e.v)) {
union(e.u, e.v);
mst.add(e);
}
}
Collections.sort(edges);Solution
Step 1: Check order of sorting and iteration
Kruskal's requires edges sorted by weight before processing to pick smallest edges first.Step 2: Identify mistake in code order
Sorting is done after the loop, so edges are not processed in correct order.Final Answer:
Edges should be sorted before the loop -> Option BQuick Check:
Sort edges first, then pick edges [OK]
- Sorting edges after processing
- Calling union before checking sets
- Adding edges outside condition
Edges: (A-B, 4), (A-C, 3), (B-C, 2), (B-D, 5), (C-D, 7), (C-E, 8), (D-E, 6)
Using Kruskal's algorithm, what is the total weight of the minimum spanning tree?
Solution
Step 1: Sort edges by weight
Sorted edges: B-C(2), A-C(3), A-B(4), B-D(5), D-E(6), C-D(7), C-E(8)Step 2: Pick edges without cycles until all vertices connected
Pick B-C(2), A-C(3), A-B(4) forms cycle so skip, pick B-D(5), pick D-E(6). Total edges chosen: B-C, A-C, B-D, D-E.Step 3: Calculate total weight
2 + 3 + 5 + 6 = 16. But A-B(4) skipped due to cycle, so MST edges are B-C, A-C, B-D, D-E.Step 4: Re-check cycle with A-B(4)
Actually, A-B(4) creates cycle with A-C and B-C, so skip it. MST weight is 2+3+5+6=16.Final Answer:
16 -> Option DQuick Check:
Sum of chosen edges weights = 16 [OK]
- Including edges that form cycles
- Misordering edges by weight
- Adding all edges without checking cycles
Ready to prove it?
Pass the test and this pattern moves unknown
