Code Examples
  Home arrow Code Examples arrow Page 2 - Minimum Spanning Tree
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
CODE EXAMPLES

Minimum Spanning Tree
By: Gabor Bernat
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 5
    2009-05-26

    Table of Contents:
  • Minimum Spanning Tree
  • Kruskall's Algorithm
  • Prim's Technique
  • Prim's Algorithm in C

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Minimum Spanning Tree - Kruskall's Algorithm


    (Page 2 of 4 )

    The best solution for our road system will be the minimal spanning tree. This is defined for graphs with no directed edges inside them. Therefore, if we find the correct n-1 edges (recall the rule that a tree with n edges will have exactly n-1 edges), we will have a road from all vertexes to all others, with the minimal cost between them.

    The algorithm relies on a simple idea. We compile a list in ascending order of the edges based on their weight/distance/niceness (in the future we will refer to them only as weight). Now we kick off with a clean graph, from which we remove all the edges.

    The idea is to now take all the edges in this increasing order and add n-1 number of edges in such a manner that we do not form a circle. In a tree, there will be no circle, so if we want to avoid having this happen, the problem is solved. The tree has no directed edges inside it; therefore, we can form a circle only if we add an edge to a couple of nodes, both of which are already corrected to an edge.

    For optimal performance, we are going to resolve this problem by first assigning to each of the vertexes a different identification number (made up of their vertex number). We will perceive each node first as a member of a different tree. Adding new edges between these trees will result in the integration of one tree into another.

    This way we can add an edge to the minimal spanning tree only if the two ends are members of a different tree. Systematically, we are going to melt one tree into another, and in the end, we are going to have a single tree. This will be the minimal spanning tree. Add up the weight of the edges that are members of the tree, and you will have the overall cost of the minimal spanning tree as well.

    It is quite easy to understand and to program. The only detail left is how we are going to sort the tree. I decided to call the qsort function residing inside the libraries of C. For this, we also need to write a function comparing the two edges. I've included that as well. Study both of them:

    int compareEdges(const void* edge1, const void * edge2)

    {

    pEdge first = (pEdge)edge1;

    pEdge second = (pEdge)edge2;

     

    return first->weight - second->weight;

    }

    void Kruskal ()

    {

    int i = 0, overallWeight = 0, j =0;

    int fromTree = 0, toTree = 0, changeNr = 0;

     

     

    // sort the edge list

    qsort ((void*) edges, m, sizeof (Edge), compareEdges);

     

     

    printf ("n");

    print ();

    printf ("n");

     

    printf ("nThe list of the edges with the algorithm of Kruskall:n");

     

     

    // build up the individual list

    for ( i =1; i <= n; ++i)

    tree[i] = i;

     

     

    for ( i= 0; i < m && changeNr < n; ++i)

    {

    fromTree = tree[edges[i].from];

    toTree = tree[edges[i].to];

     

    if( fromTree != toTree)

    {

    ++changeNr;

    overallWeight += edges[i].weight;

    printf(" %d) %d ~ %d -> %d n", changeNr, edges[i].from, edges[i].to, edges[i].weight);

     

    //Union of the trees

    for ( j = 0; j < m; ++j)

    if(tree[j] == toTree)

    tree[j] = fromTree;

    }

    }

    printf( " n The minimal weight %d ", overallWeight);

    }

     

    More Code Examples Articles
    More By Gabor Bernat


     

    CODE EXAMPLES ARTICLES

    - Bipartite Graphs
    - Connectivity in Graphs
    - The Ford-Fulkerson Algorithm
    - Critical Paths
    - The Bellman-Ford and Roy-Floyd Algorithms
    - Shortest Path Algorithms in Graphs
    - Minimum Spanning Tree
    - Articulation Edges and Vertexes
    - Circles and Connectivity in Graphs
    - Depth-First Search in Graphs
    - Breadth-First Search in Graphs
    - The Prufer Code and the Floyd-Warshall Algor...
    - An Insight into Graphs
    - Coding a Custom Object with WSC
    - Creating a Custom Object with WSC





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek