Data Structures and Algorithms with Object-Oriented Design Patterns in C#
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_inline70252. We can view the set of vertices, tex2html_wrap_inline70254, as a universal set  and the set of edges, tex2html_wrap_inline70260, as the definition of an equivalence relation  over the universe tex2html_wrap_inline70254. (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_inline71936, a sequence of partitions  of the set of vertices tex2html_wrap_inline70254. (Partitions are discussed in Section gif). The initial partition consists of tex2html_wrap_inline70678 sets of size one:

displaymath71924

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

displaymath71925

for tex2html_wrap_inline71944.

To construct the sequence the edges in tex2html_wrap_inline70260 are considered one-by-one in increasing order of their weights. Suppose we have computed the sequence up to tex2html_wrap_inline71942 and the next edge to be considered is tex2html_wrap_inline71876. If v and w are both members of the same element of partition tex2html_wrap_inline71942, 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_inline71942, say tex2html_wrap_inline71964 and tex2html_wrap_inline71966 (respectively). Then tex2html_wrap_inline71876 must be an edge in the minimum-cost spanning tree. In this case, we compute tex2html_wrap_inline71970 by joining tex2html_wrap_inline71964 and tex2html_wrap_inline71966. That is, we replace tex2html_wrap_inline71964 and tex2html_wrap_inline71966 in tex2html_wrap_inline71942 by the union tex2html_wrap_inline71982.

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

eqnarray53252

   figure53254
Figure: Operation of Kruskal's algorithm.




next up previous contents index

Bruno Copyright © 2001 by Bruno R. Preiss, P.Eng. All rights reserved.