Minimum Spanning Tree Kruskal's Algorithm in DSA C++ - Time & Space Complexity
We want to understand how the time needed to find a 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 Kruskal's algorithm code snippet.
// Assume edges is a list of (weight, u, v)
// n is number of vertices
sort(edges.begin(), edges.end());
DisjointSet ds(n);
for (auto &[w, u, v] : edges) {
if (ds.find(u) != ds.find(v)) {
ds.union_set(u, v);
// add edge to MST
}
}
This code sorts all edges by weight and then adds edges to the MST if they connect different sets.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Sorting all edges and iterating through each edge once.
- How many times: Sorting takes time based on number of edges (m), and the loop runs once per edge (m times).
- Disjoint set operations: Each union/find operation is almost constant time due to path compression.
As the number of edges grows, sorting takes more time, roughly increasing faster than the number of edges.
| Input Size (Edges m) | Approx. Operations |
|---|---|
| 10 | About 10 log 10 = 33 operations |
| 100 | About 100 log 100 = 664 operations |
| 1000 | About 1000 log 1000 = 9965 operations |
Pattern observation: Operations grow a bit faster than the number of edges, roughly edges times log edges.
Time Complexity: O(m log m)
This means the time grows roughly proportional to the number of edges times the logarithm of edges, mainly due to sorting.
[X] Wrong: "The algorithm runs in linear time because it just loops over edges once."
[OK] Correct: Sorting the edges before looping takes extra time that grows faster than just looping, so the sorting step dominates.
Understanding Kruskal's time complexity helps you explain how sorting and efficient union-find operations work together to build MSTs quickly.
"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 the purpose of Kruskal's Algorithm
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 algorithms
Unlike shortest path or cycle detection, Kruskal's specifically builds a minimum spanning tree.Final Answer:
To find the minimum spanning tree of a connected weighted graph -> Option AQuick Check:
Minimum Spanning Tree = Find minimum edges connecting all nodes [OK]
- Confusing MST with shortest path
- Thinking it detects cycles only
- Assuming it sorts vertices
Solution
Step 1: Identify correct sorting syntax for vector of edges
Using std::sort with iterators edges.begin() and edges.end() is correct for vectors.Step 2: Check lambda comparator correctness
The comparator should return true if first edge weight is less than second for ascending order.Final Answer:
std::sort(edges.begin(), edges.end(), [](auto &a, auto &b){ return a.weight < b.weight; }); -> Option AQuick Check:
Sort edges ascending by weight using std::sort with correct lambda [OK]
- Using wrong iterator syntax for vector
- Reversing comparator logic
- Calling sort on vector object directly
edges = { {1, 2, 3}, {2, 3, 1}, {1, 3, 2} } where each tuple is (u, v, weight).What is the total weight of the MST found by Kruskal's Algorithm?
Solution
Step 1: Sort edges by weight
Sorted edges by weight: (2,3,1), (1,3,2), (1,2,3)Step 2: Pick edges without forming cycles
Pick (2,3,1), then (1,3,2). Adding (1,2,3) forms a cycle, so skip.Final Answer:
1 + 2 = 3 -> Option CQuick Check:
Sum of selected edges = 1 + 2 = 3 [OK]
- Adding all edges without checking cycles
- Choosing edges in wrong order
- Misreading edge weights
if (find(u) != find(v)) {
unionSet(u, v);
mst_weight += w;
}What is the likely error if the MST weight is incorrect?
Solution
Step 1: Analyze union-find usage
The code correctly checks if u and v belong to different sets before union and adding weight.Step 2: Check prerequisites for Kruskal's correctness
Kruskal requires edges sorted by weight ascending before processing; missing this causes incorrect MST weight.Final Answer:
Edges are not sorted by weight before processing -> Option DQuick Check:
Sorting edges first is essential for correct MST [OK]
- Ignoring edge sorting
- Assuming union-find alone fixes MST
- Confusing path compression with correctness
{(1,2,2), (1,3,3), (2,3,1), (2,4,4), (3,5,5), (4,5,7)}Using Kruskal's Algorithm, which edges will be included in the MST?
Solution
Step 1: Sort edges by weight
Sorted edges: (2,3,1), (1,2,2), (1,3,3), (2,4,4), (3,5,5), (4,5,7)Step 2: Pick edges without cycles
Pick (2,3,1), (1,2,2), (2,4,4), (3,5,5). Adding (1,3,3) or (4,5,7) creates cycles.Final Answer:
{(2,3,1), (1,2,2), (2,4,4), (3,5,5)} -> Option BQuick Check:
Edges chosen connect all nodes with minimum total weight [OK]
- Including edges that form cycles
- Not sorting edges before selection
- Missing edges needed to connect all nodes
Ready to prove it?
Pass the test and this pattern moves unknown
