A spanning tree of a connected graph is just a sub-graph that contains all the vertices and has the edges which connects all the vertices. So the number of edges will be 1 less than the number of nodes. Let's take a connected graph which is shown in the Fig 1.Here all these trees are spanning tree and the number of edges is one less than the number of nodes. If graph is not connected. i.e., a graph with n vertices has edges less than n-1 then no spanning tree is possible.
Minimum Spanning Tree
Given a connected weighted graph G, it is often desired to create a spanning tree T for G such that the sum of weights of the tree edges in T is as small as possible. Such a tree is called a minimum spanning tree. There are number of techniques for creating a minimum spanning tree for a weighted graph but the most famous methods are Prim's and Kruskal algorithm.
Prim's Minimum Spanning Tree
In this method, we start with any node and add the other node in spanning tree on the basis of weight of edge containing to that node. Suppose we start with the node u then we have to see all the connecting edges and which edge has minimum weight. Then we will add that edge and node to the spanning tree. Suppose if two nodes u1 and u2 are in spanning tree and both have edge connecting to v3 then edge which has minimum weight will be added in spanning tree. Let's see the method.
Prim's Algorithm
MST-PRIM(G,w,r) for each u ∈ V[G] do key[u]<-&infinity; π[u]<-NIL key[r]<-0 Q<-V[G] while Q!=φ do u<-EXTRACT-MIN(Q) for each v ∈ Adj[u] do if v ∈ Q and weight(u,v)<key[v] then π[u]<-u key[v]<-weight(u,v)
Consider an example to clarify the method.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.