### Prim's Algorithm for finding Minimum cost Spanning Tree

• Prim's Algorithm

• Overview:

1. Start at any node in the graph

 Mark the starting node as reached Mark all the other nodes in the graph as unreached

Right now, the Minimum cost Spanning Tree (MST) consists of the starting node

We expand the MST with the procedure given below....

2. Find an edge e with minimum cost in the graph that connects:

 A reached node x to an unreached node y

3. Add the edge e found in the previous step to the Minimum cost Spanning Tree

Mark the unreached node y as reached

4. Repeat the steps 2 and 3 until all nodes in the graph have become reached

• Example:

• Find a Minimum cost Spanning Tree for this graph:

• We pick the starting node: node 0 and mark it as reached:

All other nodes are marked a unreached

• Find an edge with minimum cost that connects a reached node to an unreached node:

This edge is: (0,3).

Add the edge (0,3) to the MST and mark the node 3 as reached:

• Find an edge with minimum cost that connects a reached node to an unreached node:

This edge is: (3,4).

Add the edge (3,4) to the MST and mark the node 4 as reached:

• Find an edge with minimum cost that connects a reached node to an unreached node:

This edge is: (0,1).

Add the edge (0,1) to the MST and mark the node 1 as reached:

• I think you get the "drift" by now.... So I will speed things up a little.

• Next edge added is: (7,2):

• Next edge added is: (2,5):

• Next edge added is: (0,8):

• Next edge added is: (5,6):

• Now all nodes of the graph are reached

Done !!!

The Minimum cost Spanning Tree is:

• Prim's Algorithm in Pseudo code

• Pseudo Code:

 ``` ReachSet = {0}; // You can use any node... UnReachSet = {1, 2, ..., N-1}; SpanningTree = {}; while ( UnReachSet ≠ empty ) { Find edge e = (x, y) such that: 1. x ∈ ReachSet 2. y ∈ UnReachSet 3. e has smallest cost SpanningTree = SpanningTree ∪ {e}; ReachSet = ReachSet ∪ {y}; UnReachSet = UnReachSet - {y}; } ```

• Representing information in Prim's Algorithm

• Dictactical Note:

 The code of the Prim's Algorithm depends heavily on the representation of the graph that you have chosen... I took a clear and simple approach in this topic instead of an efficient approach. The representation I chose will ressult in a very slow algorithm You can get a faster algorithm using adjacency list representation.

• Representing the graph

• I will use an adjacency matrix

The graph:

Representation:

 ``` 0 1 2 3 4 5 6 7 8 +- -+ | * 3 * 2 * * * * 4 | // 0 | 3 * * * * * * 4 * | // 1 | * * * 6 * 1 * 2 * | // 2 | 2 * 6 * 1 * * * * | // 3 M = | * * * 1 * * * * 8 | // 4 | * * 1 * * * 8 * * | // 5 | * * * * * 8 * * * | // 6 | * 4 2 * * * * * * | // 7 | 4 * * * 8 * * * * | // 8 +- -+ * = a very large value (infinite) ```

• Representing the Reached and UnReached nodes:

• I used an array of boolean:

 ``` boolean[] Reached = new boolean[NNodes]; // Reach/unreach nodes ```

• If Reached[k] == true, then node k is in the Reached set

If Reached[k] == false, then node k is in the UnReached set

• Representing the edges of the Minimum cost Spanning Tree:

• Notice that the Prim's Algorithm adds the edge (x,y) where y is an unreached node.

 So node y is unreached and in the same iteration, y will become reached

• The edge (x,y) is part of the minimum cost spanning tree

• I represent the edge (x,y) using the array predNode[ ]:

 ``` The edge (x,y) is stored as: predNode[y] = x; ```

(predNode[y] = x means: the predecessor node of y is x)

• Coding Prim's Algorithm in Java (using the chosen representation)

• Recall the pseudo code:

 ``` ReachSet = {0}; // You can use any node... UnReachSet = {1, 2, ..., N-1}; SpanningTree = {}; while ( UnReachSet ≠ empty ) { Find edge e = (x, y) such that: 1. x ∈ ReachSet 2. y ∈ UnReachSet 3. e has smallest cost SpanningTree = SpanningTree ∪ {e}; ReachSet = ReachSet ∪ {y}; UnReachSet = UnReachSet - {y}; } ```

• The Java code is:

 ``` public void Prim( ) { int i, j, k, x, y; boolean[] Reached = new boolean[NNodes]; // Reach/unreach nodes int[] predNode = new int[NNodes]; // Remember min cost edge /* ----------------------------------- ReachSet = {0}; UnReachSet = {1, 2, ..., N-1}; ----------------------------------- */ Reached[0] = true; for ( i = 1; i < NNodes; i++ ) { Reached[i] = false; } predNode[0] = 0; // This is not necessary.... // Just make sure we don't have a bogus edge /* ===================================================== UnReachSet will decreas by 1 node in each iteration There are NNodes-1 unreached nodes; so we can loop NNodes-1 times and UnReachSet will become empty ! ===================================================== */ for ( k = 1; k < NNodes; k++) // Loop NNodes-1 times (UnReachSet = empty) { /* ================================================================ Find min. cost link between: reached node ---> unreached node ================================================================ */ x = y = 0; for ( i = 0; i < NNodes; i++ ) for ( j = 0; j < NNodes; j++ ) { if ( Reached[i] && ! Reached[j] && LinkCost[i][j] < LinkCostes[x][y] ) { x = i; // Link (i,j) has lower cost !!! y = j; } } /* =============================== Add e (x,y) to Spanning tree =============================== */ predNode[y] = x; /* ========================================== ReachSet = ReachSet ∪ {y} UnReachSet = UnReachSet - {y} ========================================== */ Reached[y] = true; } printMinCostEdges( predNode ); // Print the min, cost spanning tree } ```

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   javac testProg.java To run:          java testProg

Output:

 ```Input graph: Representation: LinkCost[][]: * 3 * 2 * * * * 4 3 * * * * * * 4 * * * * 6 * 1 * 2 * 2 * 6 * 1 * * * * * * * 1 * * * * 8 * * 1 * * * 8 * * * * * * * 8 * * * * 4 2 * * * * * * 4 * * * 8 * * * * ============================================= ReachSet = 0 Min cost edge: (0,3)cost = 2 ReachSet = 0 3 Min cost edge: (3,4)cost = 1 ReachSet = 0 3 4 Min cost edge: (0,1)cost = 3 ReachSet = 0 1 3 4 Min cost edge: (0,8)cost = 4 ReachSet = 0 1 3 4 8 Min cost edge: (1,7)cost = 4 ReachSet = 0 1 3 4 7 8 Min cost edge: (7,2)cost = 2 ReachSet = 0 1 2 3 4 7 8 Min cost edge: (2,5)cost = 1 ReachSet = 0 1 2 3 4 5 7 8 Min cost edge: (5,6)cost = 8 ReachSet = 0 1 2 3 4 5 6 7 8 ======================================== Edges of the Min. cost spanning tree: 0 --> 0 0 --> 1 7 --> 2 0 --> 3 3 --> 4 2 --> 5 5 --> 6 1 --> 7 0 --> 8 ```

Graphically: