Kruskal's Algorithm
- Like Prim's Algorithm, it is also a greedy algorithm.
- While Prim's Algorithm starts from single node and keep growing to reach a final tree, this algorithm starts with $|V|$ trees and keep on combining to finally reach the MST.
Main Features
- It starts with a forest of size $n = |V|$ trees.
- At each step, it combines two trees to obtain a bigger tree and reduces the number of tress in the forest by 1.
- When the forest has a single tree, it is a Minimum Spanning Tree (MST).
Use Union-Find data structure to make it faster.
Formal Sketch of Kruskal's MST Algorithm
- $A = \phi$ // A is an empty forest
- Sort the edges in the graph in increasing order of the weight.
- for each edge $(u,v) \in G.E$ in increasing order of weight {
- if $u$ and $v$ are not already connected (find by using Union-Find) {
- $A = A \cup \{(u,v)\}$
- UNION(u,v) // Union-Find operation
- if $u$ and $v$ are not already connected (find by using Union-Find) {
- return A
Implementation
Following is the implementation of above explanation.
Complexity
- Sorting takes $O(|E|log|E|)$ times
- Union-Find operations can be done in $O(log|V|)$ time if properly implemented
for dense graph, $|E| \approx |V|^2 \Rightarrow$ SLOW
for sparse graph, $|E| \approx |V| \Rightarrow$ FAST

