Thread: minimum spanning tree

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    2

    minimum spanning tree

    implementation of minimum spanning tree using binary heap in c or c++???????????please write code in c or c++ ...

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    63
    I won't write the actual code, but I'll give you tips. These come directly from the CLRS Algorithms textbook.

    This is what essentially all MST algorithms must do:
    Code:
    /* G is your graph, W could be a function pointer to find weights */
    /* A is a subset of a minimum spanning tree */
    
    Generic MST Algorithm(G, W) 
    
    A = nil 
    
    While A does not form a spanning tree
         Find a safe edge and add it to A
    
    Return A
    Since you said you needed to use a heap, you can implement a priority queue as a heap and use Prim's Algorithm.
    Code:
    /* G is your graph, W could be a function pointer for finding weights */
    /* r is the vertex we start at, AKA the root of the MST */
    /* Q is the priority queue implemented via binary heap */
    
    Prim's Algorithm(G, W, r)
    
    For each vertex u in G.V /* G.V is the vertex set of the graph */
         u.key = infinity /* distance from r */
         u.parent = nil
    
    r.key = 0
    
    Insert into Q all vertices from G.V /* sort by key value */
    
    While Q is not empty
         vertex u = dequeue(Q) // u is vertex with next smallest key value
         
         For each vertex v that is adjacent to u
              
              If v is in Q and W(u,v) < v.key
                   v.parent = u
                   v.key = W(u,v)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with Minimum Spanning Tree
    By Johnathank in forum C++ Programming
    Replies: 1
    Last Post: 09-27-2010, 03:25 AM
  2. Minimum Spanning Tree Shortest path
    By iamnew in forum C Programming
    Replies: 2
    Last Post: 06-18-2010, 12:37 AM
  3. Minimum Spanning Tree
    By iamnew in forum C++ Programming
    Replies: 1
    Last Post: 06-13-2010, 07:45 AM
  4. Minimum Spanning Tree
    By iamnew in forum C Programming
    Replies: 3
    Last Post: 06-06-2010, 04:54 PM
  5. Updated a Minimum Spanning Tree in O(V) time?
    By scp89 in forum C Programming
    Replies: 3
    Last Post: 05-07-2009, 02:02 PM