Code Examples
  Home arrow Code Examples arrow 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: 5 stars5 stars5 stars5 stars5 stars / 6
    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


    (Page 1 of 4 )

    At some portions in our life we arrive at a situation where it is no longer enough to be average, or to perform at an average rate. In order to succeed we need to bring forth the best solution possible from the circumstances. If this means finding the shortest spanning tree inside something that may be represented as a graph, then you've come to the right place. Today, we are going to examine two techniques to achieve this: Kruskall’s and Prim’s.

    Before we venture more deeply into the waters of minimal spanning trees, I would like to remind you that this article is part of a larger series I am writing about graphs, and I invite you to read the rest of them if you are interested. Each article presents a set of new techniques to resolve a particular problem. It is also illustrated with code snippets written in C. This article is the seventh part of a 13-part series.

    Strictly speaking, if you want to understand this article, only the first article in this series, An Insight into Graphs, is needed. It will explain what graphs are, why we use them, and how we incorporate them into a coding language. For now, let us discuss minimal spanning trees, shall we?

    Until now we've treated the trees only from the point of view of whether or not there exists an edge between two vertexes. We used the adjacent matrix or the edge list, and sometimes the adjacent list, to represent this. Nevertheless, the edges do not have the same traits all the time.

    We may like a few of them more, and even build a list of how favorable each one of them is for us. This idea stems from day-to-day problems. For instance, we may want to build the road system of a country. Now we do not have the money to construct a road between all of the cities, so the idea is to construct a system that allows us to spend the least amount of money (it is a world crisis, remember?).

    If we are on a plane (i.e. the country is completely flat), then all we need to take into account is the distance. Then again, if the country has some mountains, we also need to consider how much each road would cost to build between the cities. This way we can assign weights to the edges. Storing them is usually done in two ways. The first solution is simply to add another member to the edge list, that we can call intuitively the weight of the edge.

    The second one is to build the weight/distance/niceness matrix of the graph. Here we will store at position "i" "j" the weight/distance/niceness of the edge, and otherwise infinity. Since we are limited to a maximal number in programming, we are going to use INT_MAX/2, assuring for us that adding infinity with infinity will be still a positive number. The closer to the best an edge is in the rank of weight/ distance/niceness, the closer it will be to the number 0 (or the more negative it is).

    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-2010 by Developer Shed. All rights reserved. DS Cluster 8 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek