Minimum Spanning Tree using Kruskal's Algorithm
In this method, first, we examine all the edges one by one starting from the smallest edge. To decide whether the selected edge should be included in the spanning tree or not, we will examine the nodes connecting the edge. If the two belong to the same tree then we will not include the edge in the spanning tree, since the two nodes are in the same tree, they are already connected, and adding these edges would result in a cycle. So we will insert an edge in the spanning tree only if its nodes are in different trees.
How can we decide whether two nodes are in the same tree or not?
This can be done through UNION-FIND structure which is based on the partition of a set. A partition of a set may be thought of as a set of equivalence classes. Each subset of the partition contains a set of equivalent elements. For each subset, we denote one element as the representative of that subset. Each element in the subset is, somehow, equivalent and represented by the representative. When we add elements to the subset, we arrange that all the elements point to their representative. Initially, each node is its own representative. As the initial pair of nodes are joined to form a tree, the representative pointer of one of the nodes is made to point to the other, which becomes the representative of the tree. Let x denote an object, we wish to support the following operations.
1.MAKE-SET(x)creates a new set whose only member is pointed to by x. 2.FIND-SET(x)returns a pointer to the representative of the set containing x. 3.UNION(x,y)unites the dynamic sets that contain x and y, say Sx and Sy into a new set that is the union of these two sets. The representative of the resulting set is some member of Sx⋃Sy. Since we require the sets in the collection to be disjoint, we destroy the sets Sx and Sy removing them from the collection.
Let's now see the procedure of the Kruskal algorithm.
Kruskal's Algorithm
MST-KRUSKAL(G,w) A<-φ for each vertex v &Belongs;V[G] do MAKE-SET(v) sort the edges of E into increasing order by weight w. for each edge (u,v)&belongsto;E taken in increasing order do if FIND-SET(u)!= FIND-SET(v) then A<-A ⋃ {u,v} UNION (u,v) return A
Let's take an example.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.