KruskalMST
T = Ø
while |T| < n-1 do
choose e in E of minimum cost
E := E - {e}
if e does not create a cycle in T then
T := T union {e}
Assume that T is not minimal. Let Tmin be a MST containing E1, E2, ..., Ei-1 for MAXIMUM possible i.
Tmin union {Ei} will produce a cycle [else Ei could have been included, which violates the assumed maximality of i] and this cycle must include some edge x that is not in T [if all edges of the cycle are in T then T has a cycle].
The algorithm added Ei to T but did not add x to T. Therefore, cost(x) ≥ cost(Ei) [because if cost(x) < cost(Ei) then x would have been added since x would not create a cycle in T at that point].
Consider Tnew = (Tmin - {x}) union {Ei}.
If cost(x) > cost(Ei) then Tnew is a spanning tree with cost < Tmin, contradicting the minimality of Tmin.
If cost(x) = cost(Ei) then Tnew is a MST, contradicting the assumption of maximality of i.
Therefore we have a contradiction to the statement that cost(x) ≥ cost(Ei) and so our assumption must be fallacious.
Therefore T is minimal.
KruskalMST
T = Ø
VS := { {v} | v in V } /** collection of disjoint sets of nodes **/
/** each representing a connected set of nodes **/
/** that form a tree in the spanning forest **/
construct a priority queue Q containing all edges in E
/** can use a heap or a 2-3 tree **/
while |VS| > 1 do
choose (v,w) = minimum cost edge in Q
delete (v,w) from Q
T1 := FIND(v) /** find tree which has node v **/
T2 := FIND(w)
if T1 ≠ T2 then
/** v,w are in different trees **/
UNION(T1, T2, T2)
T := T union { (v,w) }
Time analysis
Initialization takes O(e) for Q, O(n) for VS.
The loop is executed at most e times, each time choosing a minimum edge in time O(log e), for total time O(e log e).
Total time for all UNIONs is O(e G(e) ).
Therefore, total time is O(e log e).