1.09k likes | 3.04k Views
Minimum Spanning Tree. What is a Minimum Spanning Tree. Constructing a Minimum Spanning Tree. Prim’s Algorithm. Animated Example. Implementation. Kruskal’s algorithm. Animated Example. Implementation. Review Questions. What is a Minimum Spanning Tree.
E N D
Minimum Spanning Tree • What is a Minimum Spanning Tree. • Constructing a Minimum Spanning Tree. • Prim’s Algorithm. • Animated Example. • Implementation. • Kruskal’s algorithm. • Animated Example. • Implementation. • Review Questions.
What is a Minimum Spanning Tree. • Minimum Spanning Tree is concerned with connected undirected graphs. • The idea is to find another graph with the same number of vertices, but with minimum number of edges. • Let G = (V,E) be connected undirected graph. A minimum spanning tree for G is a graph, T = (V’, E’) with the following properties: • V’ = V • T is connected • T is acyclic.
a a a a b b b b d d d d c c c c e e e e f f f f Example of Minimum Spanning Tree • The following figure shows a graph G1 together with its three possible minimum spanning trees. (a) (b) (c) (d) • The following figure shows a graph G1 together with its three possible minimum spanning trees.
What is a Minimum-Cost Spanning Tree • Minimum-Cost spanning tree is concerned with edge-weighted connected undirected graphs. • For an edge-weighted , connected, undirected graph, G, the total cost of G is the sum of the weights on all its edges. • A minimum-cost spanning tree for G is a minimum spanning tree of G that has the least total cost.
Constructing Minimum Spanning Tree • A spanning tree can be constructed using any of the traversal algorithms discussed, and taking note of the edges that are actually followed during the traversals. • The spanning tree obtained using breadth-first traversal is called breadth-first spanning tree, while that obtained with depth-first traversal is called depth-first spanning tree. • For example, for the graph in figure 1, figure (c) is a depth-first spanning tree, while figure (d) is a breadth-first spanning tree. • In the rest of the lecture, we discuss two algorithms for constructing minimum-cost spanning tree.
Prim’s Algorithm • Prim’s algorithm finds a minimum cost spanning tree by selecting edges from the graph one-by-one as follows: • It starts with a tree, T, consisting of the starting vertex, x. • Then, it adds the shortest edge emanating from x that connects T to the rest of the graph. • It then moves to the added vertex and repeat the process. Let T be a tree consisting of only the starting vertex x While (T has fewer than n vertices) { find the smallest edge connecting T to G-T add it to T }
Implementation of Prim’s Algorithm. • Prims algorithn can be implememted similar to the Dijskra’s algorithm as shown below: 1 public static Graph primsAlgorithm(Graph g, int start) { 2 int n = g.getNumberOfVertices(); 3 Entry table[] = new Entry[n]; 4 for(int v = 0; v < n; v++) 5 table[v] = new Entry(); 6 table[start].distance = 0; 7 PriorityQueue queue=new BinaryHeap(g.getNumberOfEdges()); 8 queue.enqueue(new Association(new Int(0), 9 g.getVertex(start))); 10 while(!queue.isEmpty()) { 11 Association association=(Association)queue.dequeueMin(); 12 Vertex v0 = (Vertex)association.getValue(); 13 int n0 = v0.getNumber(); 14 if(!table[n0].known) { 15 table[n0].known = true; 16 Enumeration p = v0.getEmanatingEdges();
Implementation of Prim’s Algorithm Cont'd 17 while (p.hasMoreElements()) { 18 Edge edge = (Edge) p.nextElement(); 19 Vertex v1 = edge.getMate(v0); 20 int n1 = v1.getNumber(); 21 Int weight = (Int)edge.getWeight(); 22 int d = weight.intValue(); 23 if(!table[n1].known && table[n1].distance>d) { 24 table[n1].distance = d; 25 table[n1].predecessor = n0; 26 queue.enqueue(new Association(new Int(d), v1)); 27 } 28 } 29 } 30 } 31 Graph result = new GraphAsLists(n); 32 for(int v = 0; v < n; v++) 33 result.addVertex(v); 34 for(int v = 0; v < n; v++) 35 if( v != start) 36 result.addEdge(v, table[v].predecessor, 37 new Int(table[v].distance)); 38 return result;}
Kruskal's Algorithm. • Kruskal’ÿ algorithm also finds the minimum const spanning tree of a graph by adding edges one-by-one. • However, Kruskal’ÿ algorithm forms a forest of trees, which are joined together incrementally to form the minimum cost spanning tree. sort the edges of G in increasing order of weights form a forest by taking each vertex in G to be a tree for each edge e in sorted order if the endpoints of e are in separate trees join the two tree to form a bigger tree return the resulting single tree
Priority Queue Current Edge ad ef ce de cd df ac ab bc 1 2 3 4 5 5 8 13 15 Animated Example for Kruskal’s Algorithm. Demonstrating Kruskal's algorithm.
S1 S3 S4 S2 4 2 10 7 1 5 0 8 6 9 3 11 Implementation of Kruskal’s Algorithm. • To implement Kruskal's algorithm, we need a data structure that maintains a partition of sets. • find(item) : returns the set containing the item. • join(s1, s2) : replace the sets, s1 and s2 with their union. • We shall implement the partition data structure as a forest. That is, each set in the partition is represented as a tree.
Implementation of Kruskal’s Algorithm Cont'd. • The trees we shall use to represent a set in a partition are unlike any of the trees we have seen so far. • The nodes have arbitrary degrees. • There are no restrictions on the position of the keys. • Instead of pointing to children, each node of the tree points to its parent.
Implementation of Kruskal’s Algorithm Cont'd. • The ith array element holds the node that contains item i. • Thus, to implement the find method, we start at index i of the array and follow the parent pointers until we reach a node whose parent is null and return it. • To implemented the join method, we attach the smaller tree to the root of the larger one
Implementation of Kruskal’s Algorithm Cont'd. • The following code shows the implementation of the Partition class: 1 public class Partition { 2 protected PartitionTree[] forest; 3 protected int count; 4 public class PartitionTree { 5 protected int item, count = 1; 6 protected PartitionTree parent; 7 public PartitionTree(int item) { 8 this.item = item; 9 parent = null; 10 } 11 } 12 public Partition(int size) 13 forest = new PartitionTree[size]; 14 for (int item = 0; item <size; item++) 15 forest[item] = new PartitionTree(item); 16 count = size; 17 } 18 //...
Implementation of Kruskal’s Algorithm Cont'd 18 public PartitionTree find(int item) { 19 PartitionTree ptr = forest[item]; 20 while (ptr.parent != null) { 21 ptr = ptr.parent; 22 23 return ptr; 24 25 public void join (PartitionTree t1, PartitionTree t2) { 26 if (!isPartitionTree(t1) || !isPartitionTree(t2)) 27 throw new IllegalArgumentException("bad argument"); 28 else if (t1.count > t2.count) { 29 t2.parent = t1; 30 t1.count+=t2.count; 31 32 else { 33 t1.parent = t2; 34 t2.count += t1.count; 35 36 37 //... 38
Implementation of Kruskal’s Algorithm Cont'd. • We can use the Partition data structure to implement the Kruskal’s algorithm as follows: 1 public static Graph kruskalsAlgorithm(Graph g) { 2 int n = g.getNumberOfVertices(); 3 Graph result = new GraphAsLists(n); 4 for(int v = 0; v < n; v++) 5 result.addVertex(v); 6 PriorityQueue queue=new BinaryHeap(g.getNumberOfEdges()); 7 Enumeration p = g.getEdges(); 8 while(p.hasMoreElements()) { 9 Edge edge = (Edge) p.nextElement(); 10 Int weight = (Int)edge.getWeight(); 11 queue.enqueue(new Association(weight, edge)); 12 } 13 //...
Implementation of Kruskal’s Algorithm Cont'd 13 Partition partition = new Partition(n); 14 while (!queue.isEmpty() && partition.getCount() > 1) { 15 Association association = 16 (Association) queue.dequeueMin(); 17 Edge edge = (Edge)association.getValue(); 18 Int weight = (Int) edge.getWeight(); 19 int n0 = edge.getV0().getNumber(); 20 int n1 = edge.getV1().getNumber(); 21 Partition.PartitionTree t1 = partition.find(n0); 22 Partition.PartitionTree t2 = partition.find(n1); 23 if(t1 != t2) { 24 partition.join(t1, t2); 25 result.addEdge(n0, n1, weight); 26 } 27 } 28 return result; 29
Review Questions GB • Find the breadth-first spanning tree and depth-first spanning tree of the graph GA shown above. • For the graph GB shown above, trace the execution of Prim's algorithm as it finds the minimum-cost spanning tree of the graph starting from vertex a. • Repeat question 2 above using Kruskal's algorithm. • Complete the implementation of the Partition class by writing a method isPartitionTree(PartitionTree tree) that returns true if tree is a valid partition tree in the partition forest.