Minimum Spanning Tree Kruskal's Algorithm in DSA Typescript - 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 the number of edges and vertices affects the work done.
Analyze the time complexity of the following Kruskal's algorithm snippet.
function kruskal(edges: [number, number, number][], verticesCount: number) {
edges.sort((a, b) => a[2] - b[2]);
const parent = Array(verticesCount).fill(0).map((_, i) => i);
const rank = Array(verticesCount).fill(0);
function find(x: number): number {
if (parent[x] !== x) parent[x] = find(parent[x]);
return parent[x];
}
function union(a: number, b: number): boolean {
const rootA = find(a);
const rootB = find(b);
if (rootA === rootB) return false;
if (rank[rootA] < rank[rootB]) parent[rootA] = rootB;
else if (rank[rootB] < rank[rootA]) parent[rootB] = rootA;
else {
parent[rootB] = rootA;
rank[rootA]++;
}
return true;
}
const mst = [];
for (const [u, v, w] of edges) {
if (union(u, v)) mst.push([u, v, w]);
}
return mst;
}
This code sorts edges by weight, then adds edges to the MST if they connect different sets, using union-find to avoid cycles.
Look for loops and repeated calls that take most time.
- Primary operation: Sorting all edges by weight.
- How many times: Sorting runs once on all edges, which is E edges.
- Secondary operation: The loop over all edges to try union operations.
- Union-Find operations: Each union or find runs almost constant time, but called up to E times.
As the number of edges (E) and vertices (V) grow, the sorting step dominates.
| Input Size (E, V) | Approx. Operations |
|---|---|
| 10 edges, 5 vertices | About 10 log 10 = 33 operations for sorting + 10 union-find calls |
| 100 edges, 50 vertices | About 100 log 100 = 664 operations for sorting + 100 union-find calls |
| 1000 edges, 500 vertices | About 1000 log 1000 = 9965 operations for sorting + 1000 union-find calls |
Pattern observation: The sorting grows faster than the union-find calls, roughly proportional to E times log E.
Time Complexity: O(E log E)
This means the time grows mostly with sorting the edges, which depends on the number of edges times the logarithm of that number.
[X] Wrong: "The union-find operations take as much time as sorting."
[OK] Correct: Union-find operations are almost constant time due to path compression and ranking, so sorting dominates the total time.
Understanding Kruskal's time complexity shows you can analyze algorithms that combine sorting and efficient data structures, a key skill in many coding challenges.
"What if the edges were already sorted? How would that affect the time complexity?"
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 algorithms, Kruskal's focuses on building 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: Check the correct TypeScript tuple array syntax
Edges should be an array of tuples, each tuple having three numbers: [node1, node2, weight]. const edges: [number, number, number][] = [[0, 1, 10], [1, 2, 5], [0, 2, 15]]; uses this syntax correctly.Step 2: Identify incorrect syntax in other options
const edges = {0:1:10, 1:2:5, 0:2:15}; uses invalid object syntax. const edges: number[][] = [0, 1, 10, 1, 2, 5, 0, 2, 15]; is a flat array, not an array of tuples. const edges: Array<number> = [[0, 1, 10], [1, 2, 5], [0, 2, 15]]; uses wrong type annotation (Array<number> instead of tuple array).Final Answer:
const edges: [number, number, number][] = [[0, 1, 10], [1, 2, 5], [0, 2, 15]]; -> Option CQuick Check:
Tuple array syntax = const edges: [number, number, number][] = [[0, 1, 10], [1, 2, 5], [0, 2, 15]]; [OK]
- Using flat arrays instead of tuples
- Wrong object syntax for edges
- Incorrect type annotations
const edges: [number, number, number][] = [[0, 1, 4], [0, 2, 3], [1, 2, 1], [1, 3, 2], [2, 3, 4], [3, 4, 2], [4, 5, 6]];
Solution
Step 1: Sort edges by weight
Sorted edges: [1,2,1], [1,3,2], [3,4,2], [0,2,3], [0,1,4], [2,3,4], [4,5,6]Step 2: Pick edges without forming cycles until all nodes connected
Pick [1,2,1], [1,3,2], [3,4,2], [0,2,3], and [4,5,6]. These connect all nodes with minimum total weight without cycles.Final Answer:
[[1, 2, 1], [1, 3, 2], [3, 4, 2], [0, 2, 3], [4, 5, 6]] -> Option BQuick Check:
Edges sorted + no cycles = [[1, 2, 1], [1, 3, 2], [3, 4, 2], [0, 2, 3], [4, 5, 6]] [OK]
- Choosing heavier edges first
- Including edges that form cycles
- Missing edges to connect all nodes
const edges: [number, number, number][] = [[0,1,1],[1,2,2],[0,2,3]];
const parent = [0,1,2];
function find(x: number): number {
if(parent[x] === x) return x;
return find(parent[x]);
}
function union(a: number, b: number) {
const rootA = find(a);
const rootB = find(b);
if(rootA !== rootB) parent[rootB] = rootA;
}
// Missing sorting of edges before processingSolution
Step 1: Check Kruskal's Algorithm requirements
Kruskal's requires edges to be sorted by weight ascending before union-find operations.Step 2: Identify missing sorting step
The code comment notes missing sorting; without sorting, MST may not be minimal.Final Answer:
The edges array is not sorted by weight before processing -> Option DQuick Check:
Sorting edges first is essential [OK]
- Skipping edge sorting
- Ignoring path compression (optional)
- Incorrect parent initialization
[[0,1,10],[0,2,6],[0,3,5],[1,3,15],[2,3,4]]
Use Kruskal's Algorithm to find the total weight of the minimum spanning tree.
Solution
Step 1: Sort edges by weight
Sorted edges: [2,3,4], [0,3,5], [0,2,6], [0,1,10], [1,3,15]Step 2: Pick edges without cycles until all vertices connected
Pick [2,3,4], [0,3,5], [0,2,6] connect nodes 0,2,3. Then pick [0,1,10] to connect node 1. Total weight = 4+5+6+10 = 25. But this forms a cycle because 0,2,3 are connected. Instead, pick [2,3,4], [0,3,5], [0,1,10]. Then pick [0,2,6] creates cycle. So skip [0,2,6]. Next pick [1,3,15] is heavier and unnecessary. So MST edges: [2,3,4], [0,3,5], [0,1,10]. Total weight = 4+5+10=19.Final Answer:
19 -> Option AQuick Check:
Sum of MST edges weights = 19 [OK]
- Including edges that form cycles
- Adding all edges without checking
- Misordering edges by weight
Ready to prove it?
Pass the test and this pattern moves unknown
