next up previous
Next: Up: Previous:

Kruskal's Algorithm

Kruskal's Algorithm
$\;\;\;\;\;$repeat
$\;\;\;\;\;$ $\;\;\;\;\;$find a light edge (u,v) between two unconnected components
$\;\;\;\;\;$ $\;\;\;\;\;$A = A $\cup$ {(u,v)}
$\;\;\;\;\;$until all edges have been considered



MST-Kruskal(G, w) ; G = (V, E)
1 A = {}  
2 foreach v in V ; O(V)
3 $\;\;\;\;\;$MakeSet(v)  
4 sort edges E by nondecreasing weight w ; O(E lg E)
5 foreach edge (u,v) in E, in order ; m = |E| operations
6 $\;\;\;\;\;$if FindSet(u) $\neq$ FindSet(v) ; n = |V| keys
7 $\;\;\;\;\;$then A = A $\cup$ {(u,v)} ; O(m $\alpha$(m,n))
8 $\;\;\;\;\;$ $\;\;\;\;\;$Union(u,v) ; O(E $\alpha$(E,V))
9 return A ; $\alpha$(E,V) = O(lg E)



T(V,E) = O(V) + O(E lg E) + O(E lg E), V = O(E)
= O(E lg E)


next up previous
Next: Up: Previous: