Kruskal's

Kruskal's Algorithm for minimum spanning trees.

O(E log E)

Uses union-find to build a spanning tree in O(E) time, but O(E log E) is needed for sorting.

Let N be the number of vertices.

Let Edge be an object with three integers, (u,v,w), representing the vertices it connects (u and v) and its weight (w).

[See Union-Find for definitions of the functions union(x,y) and find(x)]

Java
Edge[] e; //Fill this with edges Arrays.sort(e); //Sort by increasing weight int[] p = new int[n]; //Set up union-find array (p[i] is i's parent in the union-find) for(int i=0;i &lt; n;i++) p[i] = i; ArrayList&lt;Integer&gt; al = new ArrayList&lt;Integer&gt;; //This will hold the edges used in the MST for(int i=0;i &lt; e.length;i++) { 	if(find(e[i].u) != find(e[i].v)) { 		al.add(i); union(e[i].u, e[i].v); } }