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 size 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 main steps
sort(edges, edges + E, compare);
for (int i = 0; i < E; i++) {
if (findSet(edges[i].u) != findSet(edges[i].v)) {
unionSet(edges[i].u, edges[i].v);
mst_weight += edges[i].weight;
}
}
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 them.
- How many times: Sorting takes about E log E steps; the loop runs E times.
- Union-Find operations: Each union or find operation is almost constant time due to path compression.
As the number of edges (E) and vertices (V) grow, sorting edges takes more time, and we check each edge once.
| Input Size (E) | Approx. Operations |
|---|---|
| 10 | About 10 * log 10 ≈ 33 steps |
| 100 | About 100 * log 100 ≈ 664 steps |
| 1000 | About 1000 * log 1000 ≈ 9966 steps |
Pattern observation: The work grows a bit faster than the number of edges because of sorting, but union-find operations stay very fast.
Time Complexity: O(E log E)
This means the time grows mostly with sorting edges, and the rest of the steps add only small extra time.
[X] Wrong: "The algorithm takes O(V^2) time because it checks all pairs of vertices."
[OK] Correct: Kruskal's algorithm works mainly with edges, not all vertex pairs, and sorting edges dominates the time, not checking all pairs.
Understanding Kruskal's time complexity shows you can analyze sorting and efficient data structures like union-find, skills valuable in many algorithm problems.
"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 algorithms
Unlike shortest path or cycle detection, Kruskal's focuses on building a minimum spanning tree.Final Answer:
To find the minimum spanning tree of a graph -> Option DQuick Check:
Kruskal's algorithm = Minimum Spanning Tree [OK]
- Confusing MST with shortest path
- Thinking it detects cycles only
- Assuming it sorts vertices
Solution
Step 1: Identify cycle detection method in Kruskal's
Kruskal's algorithm uses a Disjoint Set (Union-Find) data structure to check if adding an edge creates a cycle by checking if two vertices belong to the same set.Step 2: Compare other data structures
Queue and stack do not help detect cycles efficiently here; priority queue is used for sorting edges but not cycle detection.Final Answer:
Disjoint Set (Union-Find) -> Option AQuick Check:
Cycle detection in Kruskal's = Union-Find [OK]
- Confusing priority queue with cycle detection
- Using stack or queue for cycle detection
- Not knowing Union-Find purpose
Edges = [(A-B, 1), (B-C, 4), (A-C, 3), (C-D, 2)]What is the total weight of the minimum spanning tree found by Kruskal's algorithm?
Solution
Step 1: Sort edges by weight
Sorted edges: (A-B,1), (C-D,2), (A-C,3), (B-C,4)Step 2: Pick edges without cycles
Pick (A-B,1), then (C-D,2), then (A-C,3). Adding (B-C,4) creates a cycle, so skip it.Step 3: Calculate total weight
Total weight = 1 + 2 + 3 = 6Final Answer:
6 -> Option BQuick Check:
Sum of MST edges = 6 [OK]
- Adding all edges without checking cycles
- Choosing edges in wrong order
- Miscounting total weight
int findParent(int parent[], int i) {
if (parent[i] == i)
return i;
return findParent(parent, parent[i]);
}
void unionSet(int parent[], int x, int y) {
int xset = findParent(parent, x);
int yset = findParent(parent, y);
parent[xset] = yset;
}Solution
Step 1: Analyze findParent function
findParent recursively finds the root parent but does not update parent[i] to root, missing path compression optimization.Step 2: Understand unionSet function
unionSet correctly merges sets by updating parent of one root to another; no swap needed.Final Answer:
Missing path compression in findParent -> Option CQuick Check:
Path compression missing = findParent optimization [OK]
- Thinking unionSet must swap parents
- Believing findParent should return void
- Ignoring path compression importance
(1-2, 5), (1-3, 3), (2-3, 4), (2-4, 2), (3-4, 7)Using Kruskal's algorithm, which edges will be included in the minimum spanning tree?
Solution
Step 1: Sort edges by weight
Sorted edges: (2-4,2), (1-3,3), (2-3,4), (1-2,5), (3-4,7)Step 2: Pick edges without cycles
Pick (2-4,2), then (1-3,3). Next (2-3,4) connects 2 and 3 which are in different sets, so pick it. After these three edges, all vertices are connected and MST is complete.Step 3: Verify options
(2-4), (1-3), (2-3) matches edges (2-4), (1-3), (2-3). (2-4), (1-3), (1-2) includes (1-2) which is heavier than (2-3), so incorrect.Final Answer:
(2-4), (1-3), (2-3) -> Option AQuick Check:
Pick smallest edges connecting all vertices without cycle [OK]
- Choosing heavier edges over lighter ones
- Adding edges that create cycles
- Not stopping after n-1 edges
Ready to prove it?
Pass the test and this pattern moves unknown
