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_inline70252. 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_inline71864, each of which is a subgraph of G. The algorithm begins with a tree that contains one selected vertex, say tex2html_wrap_inline71114. That is, tex2html_wrap_inline71870.

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

displaymath71858

The set tex2html_wrap_inline71874 contains all the edges tex2html_wrap_inline71876 such that exactly one of v or w is in tex2html_wrap_inline71636 (but not both). Select the edge tex2html_wrap_inline71884 with the smallest edge weight,

displaymath71859

Then tex2html_wrap_inline71886, where tex2html_wrap_inline71888 and tex2html_wrap_inline71890. After tex2html_wrap_inline71762 such steps we get tex2html_wrap_inline71894 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_inline71850 shown in Figure gif. The circled vertices are the elements of tex2html_wrap_inline71636, the solid edges represent the elements of tex2html_wrap_inline71902 and the dashed edges represent the elements of tex2html_wrap_inline71874.

   figure52524
Figure: Operation of Prim's algorithm.




next up previous contents index

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