Minimum spanning tree (Kruskal's) in Data Structures Theory - Time & Space Complexity
Analyzing the time complexity of Kruskal's algorithm helps us understand how its running time grows as the graph size increases.
We want to know how the number of edges and vertices affects the work needed to find the minimum spanning tree.
Analyze the time complexity of the following Kruskal's algorithm snippet.
function kruskal(graph):
mst = empty set
sort edges by weight ascending
for each edge in sorted edges:
if adding edge does not form cycle:
add edge to mst
return mst
This code finds a minimum spanning tree by sorting edges and adding them if they don't create cycles.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Sorting all edges by weight.
- How many times: Sorting runs once on all edges; then each edge is checked once for cycle formation using union-find operations.
As the number of edges and vertices grow, sorting edges takes more time, and checking cycles grows with edges.
| Input Size (n = edges) | Approx. Operations |
|---|---|
| 10 edges | About 10 log 10 + 10 union-find checks |
| 100 edges | About 100 log 100 + 100 union-find checks |
| 1000 edges | About 1000 log 1000 + 1000 union-find checks |
Pattern observation: The sorting step dominates and grows a bit faster than linearly as edges increase.
Time Complexity: O(E log E)
This means the time grows roughly proportional to the number of edges times the logarithm of that number.
[X] Wrong: "Since we only add edges one by one, the algorithm runs in linear time O(E)."
[OK] Correct: Sorting edges takes more than linear time, and cycle checks add extra work, so the total time is more than just going through edges once.
Understanding Kruskal's time complexity shows you can analyze how sorting and data structure choices affect algorithm speed, a key skill in many coding challenges.
"What if we used a different data structure for cycle detection, like a simple visited array instead of union-find? How would the time complexity change?"
Practice
Solution
Step 1: Understand the purpose of Kruskal's algorithm
Kruskal's algorithm is designed to connect all points in a graph with the smallest total edge weight without forming any loops.Step 2: Compare with other graph problems
Unlike shortest path or cycle detection alone, Kruskal's focuses on building a minimum spanning tree, which is a tree connecting all vertices with minimal cost.Final Answer:
To find the minimum spanning tree connecting all vertices without cycles -> Option CQuick Check:
Kruskal's goal = minimum spanning tree [OK]
- Confusing minimum spanning tree with shortest path
- Thinking it detects cycles only
- Assuming it sorts vertices instead of edges
Solution
Step 1: Identify the initial step of Kruskal's algorithm
Kruskal's algorithm begins by sorting all edges by their weight from smallest to largest to ensure the cheapest edges are considered first.Step 2: Eliminate incorrect options
Depth-first search and vertex removal are not part of Kruskal's initial steps, and sorting vertices alphabetically is irrelevant.Final Answer:
Sort all edges in increasing order of their weights -> Option BQuick Check:
First step = sort edges by weight [OK]
- Sorting vertices instead of edges
- Starting with a search instead of sorting
- Removing vertices prematurely
(A-B: 1), (B-C: 4), (A-C: 3), (C-D: 2), what edges will Kruskal's algorithm select for the minimum spanning tree?Solution
Step 1: Sort edges by weight
Edges sorted: (A-B:1), (C-D:2), (A-C:3), (B-C:4).Step 2: Add edges without creating cycles
Add (A-B), then (C-D). Adding (A-C) connects A and C without cycle. Adding (B-C) would create a cycle, so skip it.Final Answer:
(A-B), (C-D), (A-C) -> Option DQuick Check:
Edges chosen = smallest weights without cycles [OK]
- Including all edges regardless of cycles
- Choosing heavier edges before lighter ones
- Not checking for cycles before adding edges
Solution
Step 1: Understand cycle prevention in Kruskal's
Kruskal's algorithm uses a disjoint set (union-find) data structure to track connected components and avoid cycles.Step 2: Identify the cause of cycle creation
If an edge creates a cycle, it means the disjoint set was not used or updated properly to detect that the two vertices are already connected.Final Answer:
The disjoint set structure is not used or updated correctly -> Option AQuick Check:
Cycle error = faulty disjoint set use [OK]
- Ignoring disjoint set updates
- Assuming sorting fixes cycles
- Confusing graph direction with cycle detection
(X-Y: 2), (Y-Z: 2), (X-Z: 3), (Z-W: 1), (W-X: 4). Using Kruskal's algorithm, which set of edges forms the minimum spanning tree?Solution
Step 1: Sort edges by weight
Edges sorted: (Z-W:1), (X-Y:2), (Y-Z:2), (X-Z:3), (W-X:4).Step 2: Add edges without cycles until all vertices connected
Add (Z-W), then (X-Y), then (Y-Z). Now all vertices X, Y, Z, W are connected. Adding (W-X) or (X-Z) would create cycles, so skip them.Final Answer:
(Z-W), (X-Y), (Y-Z) -> Option AQuick Check:
Minimum spanning tree = edges with smallest weights connecting all nodes [OK]
- Adding extra edges causing cycles
- Skipping necessary edges to connect all vertices
- Choosing heavier edges before lighter ones
Ready to prove it?
Pass the test and this pattern moves unknown
