Code Examples
  Home arrow Code Examples arrow Page 4 - 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 - Prim's Algorithm in C


    (Page 4 of 4 )

    Here it is the code snippet for what I tried to explain on the previous page:

    int Prim(int at)

    {

    int i = 0;

     

    memset(tree, 0, (n+1)*sizeof(int));

     

    tree[at] = 1;

    ancient[at] = 0 ;

     

     

    // sort the edge list

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

     

     

    // build up the tree

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

    {

    distanceMatrix[edges[i].from][edges[i].to ] = i;

    distanceMatrix[edges[i].to ][edges[i].from] = i;

     

    if (edges[i].from == at || edges[i].to == at)

    {

    edges[i].inTheCut = true;

    }

    else

    {

    edges[i].inTheCut = false;

    }

    }

     

    int overallWeight = 0;

    int curent = 0;

    int addedVertex = 1;

    int neighborVertex = 0;

    int neighborEdge = 0;

    pListIt p = NULL;

    int j = 0;

     

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

    {

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

    {

    if (edges[j].inTheCut)

    {

    curent = j;

    break;

    }

    }

     

     

    printf( " %d) %d ~ %d -> %d n",

    i, edges[curent].from, edges[curent].to, edges[curent].weight);

     

    overallWeight += edges[curent].weight;

     

    if (tree[edges[curent].from] == 1)

    {

    addedVertex = edges[curent].to;

    ancient[addedVertex] = edges[curent].from;

    }

     

    tree[addedVertex] = 1;

     

    for (p = adjacencyList[addedVertex]; p != NULL;

    p = p -> p_next)

    {

    neighborVertex = p->value;

    neighborEdge = distanceMatrix[addedVertex][neighborVertex];

     

    if (tree[neighborVertex] == 1)

    // reset if both ends are there

    edges[neighborEdge].inTheCut = false; else

    // set if only one is there

    edges[neighborEdge].inTheCut = true;

    }

     

    }

     

    printf( "nn The overall Weight: %d " , overallWeight);

    return 0;

    }

    From the following link you can see this encapsulated into a working C file that, after you compile it you can execute it, test it and see if you can improve it. It is painlessly commented, so if you feel like doing some homework in coding, you'll be able to understand it with no effort, even without this article.

     

    -->Minimum Spanning Tree.zip<--

     

    I invite you to rate my article or express your ideas, questions and other thoughts you may have here on the blog for the article. If you want, you may also join our friendly and ever-growing forum at DevHardware. Here our experts will eagerly answer your questions, whatever they may be. During our next meeting with graphs, I am going to present how to calculate the minimal distances between points inside a graph. Until that time arrives, Live With Passion!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

    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 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek