Minimum Spanning Tree What is a Minimum Spanning Tree. Constructing a Minimum Spanning Tree. What is a Minimum-Cost Spanning Tree. Prim’s Algorithm. –Animated.

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

Chapter 23 Minimum Spanning Tree
Weighted graphs Example Consider the following graph, where nodes represent cities, and edges show if there is a direct flight between each pair of cities.
Minimum Spanning Tree Sarah Brubaker Tuesday 4/22/8.
Greedy Algorithms Greed is good. (Some of the time)
Topological Sort Topological sort is the list of vertices in the reverse order of their finishing times (post-order) of the depth-first search. Topological.
Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
1 Minimum Spanning Tree Prim-Jarnik algorithm Kruskal algorithm.
Graph Algorithms. Jaruloj Chongstitvatana Chapter 3: Greedy Algorithms 2 Outline Graph Representation Shortest path Minimum spanning trees.
Shortest Path Algorithm What is the Shortest Path Problem? Is the shortest path problem well defined? The Dijkstra's Algorithm for Shortest Path Problem.
3.3 Spanning Trees Tucker, Applied Combinatorics, Section 3.3, by Patti Bodkin and Tamsen Hunter.
Minimum Spanning Tree What is a Minimum Spanning Tree. Constructing a Minimum Spanning Tree. Prim’s Algorithm. –Animated Example. –Implementation. Kruskal’s.
Discussion #36 Spanning Trees
Graph Algorithms: Minimum Spanning Tree We are given a weighted, undirected graph G = (V, E), with weight function w:
1 Minimum Spanning Trees (MSTs) and Minimum Cost Spanning Trees (MCSTs) What is a Minimum Spanning Tree? Constructing Minimum Spanning Trees. What is a.
1 Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Graph Traversals Depth-First Traversal. The Algorithm.
Shortest Path Algorithm What is the Shortest Path Problem? Is the shortest path problem well defined? The Dijkstra's Algorithm for Shortest Path Problem.
Dynamic Sets and Data Structures Over the course of an algorithm’s execution, an algorithm may maintain a dynamic set of objects The algorithm will perform.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 11 Instructor: Paul Beame.
1 Minimum Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting.
1 Minimum Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.
Data Structures and Algorithms Graphs Minimum Spanning Tree PLSD210.
Minimal Spanning Trees What is a minimal spanning tree (MST) and how to find one.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
Design and Analysis of Computer Algorithm September 10, Design and Analysis of Computer Algorithm Lecture 5-2 Pradondet Nilagupta Department of Computer.
ADA: 10. MSTs1 Objective o look at two algorithms for finding mimimum spanning trees (MSTs) over graphs Prim's algorithm, Kruskal's algorithm Algorithm.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Graph Dr. Bernard Chen Ph.D. University of Central Arkansas.
Chapter 2 Graph Algorithms.
Minimum Spanning Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Prim's Algorithm This algorithm starts with one node. It then, one by one, adds a node that is unconnected to the new graph to the new graph, each time.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
Union-find Algorithm Presented by Michael Cassarino.
1 Minimum Spanning Trees (some material adapted from slides by Peter Lee, Ananda Guna, Bettina Speckmann)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
Trees Dr. Yasir Ali. A graph is called a tree if, and only if, it is circuit-free and connected. A graph is called a forest if, and only if, it is circuit-free.
Trees Thm 2.1. (Cayley 1889) There are nn-2 different labeled trees
Minimum Spanning Tree What is a Minimum Spanning Tree.
1 Week 3: Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
Lecture 12 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Algorithm Design and Analysis June 11, Algorithm Design and Analysis Pradondet Nilagupta Department of Computer Engineering This lecture note.
Contest Algorithms January 2016 Describe a MST and the Prim and Kruskal algorithms for generating one. 8. Minimal Spanning Trees (MSTs) 1Contest Algorithms:
CSE 589 Applied Algorithms Spring 1999 Prim’s Algorithm for MST Load Balance Spanning Tree Hamiltonian Path.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
Unit 11 Graphs (2) King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Minimum Spanning Trees
Minimum Spanning Tree What is a Minimum Spanning Tree.
Shortest Path Algorithm
Minimum Spanning Trees
Minimum Spanning Tree Chapter 13.6.
Minimum Spanning Trees and Shortest Paths
Lecture 12 Algorithm Analysis
Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee
Short paths and spanning trees
Spanning Trees.
Minimum Spanning Tree.
Minimum Spanning Trees
Minimum Spanning Tree.
Minimum Spanning Trees
Minimum Spanning Trees
Minimum Spanning Tree.
Lecture 12 Algorithm Analysis
Shortest Path Algorithm
CSE 373: Data Structures and Algorithms
Shortest Path Algorithm
Presentation transcript:

Minimum Spanning Tree What is a Minimum Spanning Tree. Constructing a Minimum Spanning Tree. What is a Minimum-Cost 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 applicable only to 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.

Example of Minimum Spanning Tree The following figure shows a graph G1 together with its three possible minimum spanning trees. abd cef abd cef The example shows that the minimal spanning tree for a graph is generally not unique. (a) (b)(c)(d)

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.

What is a Minimum-Cost Spanning Tree Minimum-Cost spanning tree is applicable only to 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.

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 finds and adds the shortest edge connecting T to the rest of the graph. The process is repeated until all vertices are added to T. 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 }

Animated Example for Prim’s Algorithm.

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’s algorithm also finds the minimum cost spanning tree of a graph by adding edges one-by-one. However, Kruskal’s 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 of G to be a tree for each edge e in sorted list if the endpoints of e are in separate trees join the two tree to form a bigger tree by adding e return the resulting single tree

Animated Example for Kruskal’s Algorithm. Demonstrating Kruskal's algorithm. Priority Queue Current Edge ad ef ce de cd df ac ab bc

Implementation of Kruskal’s Algorithm. To implement Kruskal's algorithm, we need a data structure that maintains a partition of sets. It should have the following methods: –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 S1 S2 S3S4

Implementation of Kruskal’s Algorithm Cont'd. The tree we shall use to represent a set in a partition is unlike any of the trees we have seen so far. The nodes have arbitrary degrees. There is no restriction in the ordering of the keys. Instead of pointing to children, each node of the tree points to its parent. This tree is represented as an array of objects, where Each object # i is stored at index I Each object points to its parent or (null if root) S1 S2 S3S4

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 (the one with fewer keys) to the root of the larger one S1 S2 S3S4

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 } 36count } 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 1.Find the breadth-first spanning tree and depth-first spanning tree of the graph GA shown above. 2.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. 3.Repeat question 2 above using Kruskal's algorithm. 4.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. GBGB