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

Prim's Algorithm

Prim's algorithm  finds a minimum-cost spanning tree of an edge-weighted, connected, undirected graph tex2html_wrap_inline71355. The algorithm constructs the minimum-cost spanning tree of a graph by selecting edges from the graph one-by-one and adding those edges to the spanning tree.

Prim's algorithm is essentially a minor variation of Dijkstra's algorithm (see Section gif). To construct the spanning tree, the algorithm constructs a sequence of spanning trees tex2html_wrap_inline72957, each of which is a subgraph of G. The algorithm begins with a tree that contains one selected vertex, say tex2html_wrap_inline72207. I.e., tex2html_wrap_inline72963.

Given tex2html_wrap_inline72965, we obtain the next tree in the sequence as follows. Consider the set of edges given by

displaymath72951

The set tex2html_wrap_inline72967 contains all the edges tex2html_wrap_inline72969 such that exactly one of v or w is in tex2html_wrap_inline72729 (but not both). Select the edge tex2html_wrap_inline72977 with the smallest edge weight,

displaymath72952

Then tex2html_wrap_inline72979, where tex2html_wrap_inline72981 and tex2html_wrap_inline72983. After tex2html_wrap_inline72855 such steps we get tex2html_wrap_inline72987 which is the minimum-cost spanning tree of G.

Figure gif illustrates how Prim's algorithm determines the minimum-cost spanning tree of the graph tex2html_wrap_inline72943 shown in Figure gif. The circled vertices are the elements of tex2html_wrap_inline72729, the solid edges represent the elements of tex2html_wrap_inline72995 and the dashed edges represent the elements of tex2html_wrap_inline72967.

   figure52993
Figure: Operation of Prim's Algorithm




next up previous contents index

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