Kruskal's Algorithm
for Finding a Minimum-Cost Spanning Tree
of a
Connected Undirected Graph
/* The following algorithm is written in C-like pseudo code */
setOfEdges Kruskal ( graph G )
{
/* initialization */
sort the edge set, E, of G by increasing length ;
n = the number of nodes in the node set, N, of G
T = empty set ; /* will contain the edges of the spanning tree */
initialize n sets, each containing a different element of N;
/* greedy loop */
do
{
e = the shortest edge, {u,v}, not yet considered ;
ucomp = Find(u) ; /* locate the set containing u */
vcomp = Find(v) ; /* locate the set containing v */
if (ucomp != vcomp)
{
Union(ucomp, vcomp) ; /* merge into one set. */
T = T union {e} ;
}
} while (T contains fewer than n-1 edges) ;
return T ;
}