Education
 

Prim's algorithm

From Mathematics

Prim's algorithm is a common method for finding a minimal spanning tree of a connected, weighted, undirected graph. It is a greedy algorithm and, surprisingly, is always correct despite its simplicity. It is not hard at all to execute this algorithm with pencil and paper. However, its proof is a little more involved.

Contents

[edit] Algorithm

Let math be a connected undirected graph, let math be a bijection from its edges to their weights and let math be a graph that will form a minimal spanning tree of math, where math and math are initially empty.

[edit] Initial Stage

  1. Select an edge math such that math minimizes math. If there is more than one choice, pick out any one arbitrarily. Add math to math and add its endpoints to math.

[edit] Iterative Stage

  1. Select an edge math adjacent to math such that math minimizes math. As before, if there is more than one edge that minimizes weight, the choice is arbitrary. Add math to math and add the endpoint of math that is not currently in math to math.
  2. Repeat previous step until math, i.e. until the minimal spanning tree has one less edge than the number of vertices. This step indicates the algorithm is complete, since a tree with math vertices necessarily has math edges.

[edit] See Also