Logo Data Structures and Algorithms with Object-Oriented Design Patterns in Java
next up previous contents index

Kruskal's Algorithm

Like Prim's algorithm, Kruskal's algorithm  also constructs the minimum spanning tree of a graph by adding edges to the spanning tree one-by-one. At all points during its execution the set of edges selected by Prim's algorithm forms exactly one tree. On the other hand, the set of edges selected by Kruskal's algorithm forms a forest of trees.

Kruskal's algorithm is conceptually quite simple. The edges are selected and added to the spanning tree in increasing order of their weights. An edge is added to the tree only if it does not create a cycle.

The beauty of Kruskal's algorithm is the way that potential cycles are detected. Consider an undirected graph tex2html_wrap_inline70093. We can view the set of vertices, tex2html_wrap_inline70095, as a universal set  and the set of edges, tex2html_wrap_inline70101, as the definition of an equivalence relation  over the universe tex2html_wrap_inline70095. (See Definition gif). In general, an equivalence relation partitions a universal set into a set of equivalence classes. If the graph is connected, there is only one equivalence class--all the elements of the universal set are equivalent. Therefore, a spanning tree is a minimal set of equivalences that result in a single equivalence class.

Kruskal's algorithm computes, tex2html_wrap_inline71777, a sequence of partitions  of the set of vertices tex2html_wrap_inline70095. (Partitions are discussed in Section gif). The initial partition consists of tex2html_wrap_inline70519 sets of size one:

displaymath71765

Each subsequent element of the sequence is obtained from its predecessor by joining two of the elements of the partition. Therefore, tex2html_wrap_inline71783 has the form

displaymath71766

for tex2html_wrap_inline71785.

To construct the sequence the edges in tex2html_wrap_inline70101 are considered one-by-one in increasing order of their weights. Suppose we have computed the sequence up to tex2html_wrap_inline71783 and the next edge to be considered is tex2html_wrap_inline71717. If v and w are both members of the same element of partition tex2html_wrap_inline71783, then the edge forms a cycle, and is not part of the minimum-cost spanning tree.

On the other hand, suppose v and w are members of two different elements of partition tex2html_wrap_inline71783, say tex2html_wrap_inline71805 and tex2html_wrap_inline71807 (respectively). Then tex2html_wrap_inline71717 must be an edge in the minimum-cost spanning tree. In this case, we compute tex2html_wrap_inline71811 by joining tex2html_wrap_inline71805 and tex2html_wrap_inline71807. That is, we replace tex2html_wrap_inline71805 and tex2html_wrap_inline71807 in tex2html_wrap_inline71783 by the union tex2html_wrap_inline71823.

Figure gif illustrates how Kruskal's algorithm determines the minimum-cost spanning tree of the graph tex2html_wrap_inline71691 shown in Figure gif. The algorithm computes the following sequence of partitions:

eqnarray53116

   figure53118
Figure: Operation of Kruskal's algorithm.