Minimum Spanning Tree
Written by Mike James   
Monday, 04 January 2010
Article Index
Minimum Spanning Tree
Prim's algorithm
Implementing Prim's algorithm


Implementing Prim's algorithm

Now we have to move on to implement Prim's algorithm proper. As well as just implementing the method, an animated display will be used to show the paths as they are being considered for inclusion. Place a second button on the form and change its caption to "Find MST". It's click routine is simply

 private void button2_Click(
object sender, RoutedEventArgs e)

The work is all done by the prims function.

The first thing we need are two arrays to store the list of vertices that have been included and are still to be processed:

 void prims()
int[] included = new int[size];
int[] excluded = new int[size];

We also need another network array to hold the distances that form the minimum spanning tree:

 Single[,] finished = 
new Single[size, size];

Two integer variables are used to hold the start and finish node numbers of each path added to the tree:

 int start = 0;
 int finish = 0;

The two arrays need to be initialised:

 for (int i = 0; i < size; i++)
excluded[i] = i;
included[i] = -1;

At the start of the algorithm all of the vertices are excluded, i.e. vertices 0 to size-1, and none of the vertices are included, i.e. all entries -1. Notice that -1 is used to flag the fact that there is no entry in the array.

The first point in the MST is selected at random:

 included[0] = excluded[R.Next(size)];
excluded[included[0]] = -1;

Notice that the vertex that has been added to "included" has to be removed from "excluded" by setting that element to -1.

At last we can start the loop that finds the closest vertex to the currently included set and adds it to "included".

 for (int n = 1; n < size; n++)
closest(n, ref start, ref finish,
included, excluded);

where start and finish are the indices of the vertices of the path that is to be added to the tree. That is finish is the closest of the currently excluded vertices and start is the vertex in the included set it is closest to - adding the arc from start to finish to the MST.

To be clear, the function closest finds the closest vertex listed in the excluded array to any of the vertices listed in the included array.

The end point of the path is the new vertex that has to be added to the included set, i.e the vertex indexed by start is already in the included array:

 included[n] = excluded[finish];

and hence removed from the excluded set:

 excluded[finish] = -1;

The distance associated with the path also has to be added to the "finished" array to indicate that it has to be drawn as part of the MST:

 finished[included[n], included[start]] = 
Network[included[n], included[start]];

and the finished array has to be made symmetric just in case someone wants to make use of the upper triangle rather than the lower:

 finished[included[start], included[n]] = 
Network[included[start], included[n]];

and finally we can show the finished MST:


All that is needed to complete the program is the "closest" function:

 private void closest(int n,  
ref int start, ref int finish,
int[] included, int[] excluded)
Single smallest = -1;
for (int i = 0; i < n; i++)
for (int j = 0; j < size; j++)
if (excluded[j] == -1) continue;
if (smallest == -1) smallest =
Network[included[i], excluded[j]];
if (Network[included[i], excluded[j]]
> smallest) continue;
smallest =
Network[included[i], excluded[j]];
start = i;
finish = j;

This is essentially a search loop for the smallest value but it has to take into account whether the vertex is valid or not. A vertex that isn't in the excluded list isn't valid and one with a zero distance isn't connected to the node in question and once again isn't valid. Apart from this complication each node in the excluded list is used to find the smallest distance.

If you put all of this together and run the program you will discover that not only can you generate random graphs but also their MSTs.

You can learn a great deal by watching it. If you press the "Find MST" button again the whole process is repeated from a different starting point. Notice that the MST isn't unique so if you discover that starting from a different vertex gives you a different MST this isn't an error.

A minimum spanning tree.

You can add additional facilities to this program quite easily. For example, a display of the total length of the MST would be interesting and a slider control to modify the number of arcs would also be worth the effort. You could also animate the display to show the arcs being selected one at a time.

If you would like the code for this project then register and click on CodeBin.


Running the LAME MP3 encoder under the control of .NET isn't difficult and it provides a really good example of controlling an external .EXE.

Disk Drive Dangers - SMART and WMI

Getting access to the hardware is never easy from an application. If you want to know how well your disk drive is performing then there is a way of accessing the SMART data - including the temperature [ ... ]

Other Projects


Last Updated ( Wednesday, 31 March 2010 )

RSS feed of all content
I Programmer - full contents
Copyright © 2014 All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.