242-535 ADA: 10. MSTs1 Objective o look at two algorithms for finding mimimum spanning trees (MSTs) over graphs Prim's algorithm, Kruskal's algorithm Algorithm.

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

Comp 122, Spring 2004 Greedy Algorithms. greedy - 2 Lin / Devi Comp 122, Fall 2003 Overview  Like dynamic programming, used to solve optimization problems.
CSCE 411H Design and Analysis of Algorithms Set 8: Greedy Algorithms Prof. Evdokia Nikolova* Spring 2013 CSCE 411H, Spring 2013: Set 8 1 * Slides adapted.
Minimum Spanning Tree Sarah Brubaker Tuesday 4/22/8.
Greedy Algorithms Greed is good. (Some of the time)
Greed is good. (Some of the time)
Minimum Spanning Trees Definition Two properties of MST’s Prim and Kruskal’s Algorithm –Proofs of correctness Boruvka’s algorithm Verifying an MST Randomized.
1 Minimum Spanning Tree Prim-Jarnik algorithm Kruskal algorithm.
CSE 780 Algorithms Advanced Algorithms Minimum spanning tree Generic algorithm Kruskal’s algorithm Prim’s algorithm.
Minimum Spanning Trees Definition Algorithms –Prim –Kruskal Proofs of correctness.
CPSC 411, Fall 2008: Set 4 1 CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Fall 2008.
Minimum-Cost Spanning Tree weighted connected undirected graph spanning tree cost of spanning tree is sum of edge costs find spanning tree that has minimum.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
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.
CPSC 411, Fall 2008: Set 4 1 CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Fall 2008.
More Chapter 7: Greedy Algorithms Kruskal’s Minimum Spanning Tree Algorithm.
Minimum Spanning Trees. Subgraph A graph G is a subgraph of graph H if –The vertices of G are a subset of the vertices of H, and –The edges of G are a.
Minimum Spanning Trees What is a MST (Minimum Spanning Tree) and how to find it with Prim’s algorithm and Kruskal’s algorithm.
Design and Analysis of Computer Algorithm September 10, Design and Analysis of Computer Algorithm Lecture 5-2 Pradondet Nilagupta Department of Computer.
Analysis of Algorithms
MST Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
2IL05 Data Structures Fall 2007 Lecture 13: Minimum Spanning Trees.
Spring 2015 Lecture 11: Minimum Spanning Trees
UNC Chapel Hill Lin/Foskey/Manocha Minimum Spanning Trees Problem: Connect a set of nodes by a network of minimal total length Some applications: –Communication.
Minimum Spanning Trees and Kruskal’s Algorithm CLRS 23.
1 Minimum Spanning Trees. Minimum- Spanning Trees 1. Concrete example: computer connection 2. Definition of a Minimum- Spanning Tree.
1 Greedy Algorithms and MST Dr. Ying Lu RAIK 283 Data Structures & Algorithms.
Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem.
November 13, Algorithms and Data Structures Lecture XII Simonas Šaltenis Aalborg University
Chapter 23: Minimum Spanning Trees: A graph optimization problem Given undirected graph G(V,E) and a weight function w(u,v) defined on all edges (u,v)
Union-Find  Application in Kruskal’s Algorithm  Optimizing Union and Find Methods.
Minimum- Spanning Trees
MA/CSSE 473 Day 34 MST details: Kruskal's Algorithm Prim's Algorithm.
1 22c:31 Algorithms Minimum-cost Spanning Tree (MST)
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:
November 22, Algorithms and Data Structures Lecture XII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Prim’s MST Djikstra SP. PRIM’s - Minimum Spanning Tree -A spanning tree of a graph is a tree that has all the vertices of the graph connected by some.
Greedy Algorithms General principle of greedy algorithm
Chapter 9 : Graphs Part II (Minimum Spanning Trees)
Introduction to Algorithms
May 12th – Minimum Spanning Trees
Eager Prim Dijkstra.
Minimum Spanning Trees and Shortest Paths
Minimum Spanning Trees
Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee
Discrete Maths 10. Trees 242/ , Semester 2, Objective
Short paths and spanning trees
Minimum-Cost Spanning Tree
CSCE350 Algorithms and Data Structure
Minimum Spanning Tree.
Eager Prim Dijkstra.
Minimum Spanning Tree.
CSE373: Data Structures & Algorithms Lecture 12: Minimum Spanning Trees Catie Baker Spring 2015.
CSE373: Data Structures & Algorithms Lecture 20: Minimum Spanning Trees Linda Shapiro Spring 2016.
Algorithms and Data Structures Lecture XII
Kruskal’s Minimum Spanning Tree Algorithm
CSE332: Data Abstractions Lecture 18: Minimum Spanning Trees
Minimum Spanning Trees
Minimum Spanning Tree.
Minimum Spanning Trees
Algorithms: Design and Analysis
Greedy Algorithms Comp 122, Spring 2004.
CSE 373: Data Structures and Algorithms
Chapter 23: Minimum Spanning Trees: A graph optimization problem
Topological Sorting Minimum Spanning Trees Shortest Path
Minimum-Cost Spanning Tree
Minimum Spanning Trees
Presentation transcript:

ADA: 10. MSTs1 Objective o look at two algorithms for finding mimimum spanning trees (MSTs) over graphs Prim's algorithm, Kruskal's algorithm Algorithm Design and Analysis (ADA) , Semester Minimum Spanning Trees (MSTs) 10. Minimum Spanning Trees (MSTs)

ADA: 10. MSTs2 1.Minimum Spanning Tree 2.Prim's Algorithm 3.Kruskal's Algorithm 4.Difference between Prim and KruskalOverview

ADA: 10. MSTs3 A minimum spanning tree T is a subgraph of a weighted graph G which contains all the verticies of G and whose edges have the minimum summed weight. Example weighted graph G: 1. Minimum Spanning Tree A B C D E F

ADA: 10. MSTs4 A minimum spanning tree (weight = 12): A non-minimum spanning tree (weight = 20): A B C D E F A B C D E F

ADA: 10. MSTs5 Typical MST Applications

ADA: 10. MSTs6 MSTs have the optimal substructure property: an optimal tree is composed of optimal subtrees. This can be seen by considering how to break a MST into parts: o Let T be an MST of G with an edge (u,v) o Removing (u,v) splits T into two trees T 1 and T 2 o Claim: T 1 is an MST of G 1 = (V 1,E 1 ), and T 2 is an MST of G 2 = (V 2,E 2 ) ( V 1 and V 2 do not share any vertices o Proof: w(T) = w(u,v) + w(T 1 ) + w(T 2 ) There can’t be a better tree than T 1 or T 2, or T would be suboptimal MST Optimality

ADA: 10. MSTs7 Both dynamic programming (DP) and greedy algorithms can be used on problems that exhibit optimality. What's the difference between the two approaches? DP: use when the problem is optimal and there are repeating sub-problems Greedy: use when the problem is optimal, and each sub-problem can be solved without combining/examining smaller sub-sub-problems o this is called the greedy-choice property Optimality and Coding

ADA: 10. MSTs8 DP solves sub-problems bottom-up since the current sub-problem may depend on sub-sub-problems. Greedy algorithms usually execute top-down, since a sub-problem can be solved without using sub-sub- problem solutions. In a greedy algorithm, we make whatever choice seems best at the moment and then solve any other sub-problems arising after the choice is made.

ADA: 10. MSTs9 Since a MST is an optimal data structure, it is possible to use dynamic programming (DP) techniques o e.g. memoization, bottom-up execution In fact, both the MST algorithms in this part are greedy o at each iteration the choice of how to grow the MST only depends on the current state of the MST, not earlier sub- states or simpler versions DP, Greed and MST

ADA: 10. MSTs10 Prim's algorithm finds a minimum spanning tree T by iteratively adding edges to T. At each iteration, a minimum-weight edge is added that does not create a cycle in the current T. The new edge must be connected to a vertex which is already in T. The algorithm can stop after |V|-1 edges have been added to T. 2. Prim's Algorithm

ADA: 10. MSTs11 Tree prim(Graph G, int numVerts) { Tree T = anyVert(G); for i = 1 to numVerts-1 { Edge e = select an edge of minimum weight that is connected to a vertex already in T and does not form a cycle in T; T = T + e ; } return T } Simple Pseudocode

ADA: 10. MSTs12 For the graph G. 1) Add any vertex to T o e.g A,T = {A} 2) Examine all the edges leaving {A} and add the vertex with the smallest weight. o edgeweight (A,B) 4 (A,C) 2 (A,E) 3 o add edge (A,C), T becomes {A,C} Informal Algorithm continued A B C D E F

ADA: 10. MSTs13 3) Examine all the edges leaving {A,C} and add the vertex with the smallest weight. o edgeweightedgeweight (A,B) 4(C,D) 1 (A,E) 3(C,E) 6 (C,F) 3 o add edge (C,D), T becomes {A,C,D} continued

ADA: 10. MSTs14 4) Examine all the edges leaving {A,C,D} and add the vertex with the smallest weight. o edgeweightedgeweight (A,B) 4(D,B) 5 (A,E) 3(C,E) 6 (C,F) 3(D,F) 6 o add edge (A,E) or (C,F), it does not matter o add edge (A,E), T becomes {A,C,D,E} continued

ADA: 10. MSTs15 5) Examine all the edges leaving {A,C,D,E} and add the vertex with the smallest weight. o edgeweightedgeweight (A,B) 4(D,B) 5 (C,F) 3(D,F) 6 (E,F) 2 o add edge (E,F), T becomes {A,C,D,E,F} continued

ADA: 10. MSTs16 6) Examine all the edges leaving {A,C,D,E,F} and add the vertex with the smallest weight. o edgeweightedgeweight (A,B) 4(D,B) 5 o add edge (A,B), T becomes {A,B,C,D,E,F} All the verticies of G are now in T, so we stop. continued

ADA: 10. MSTs17 Resulting minimum spanning tree (weight = 12): A B C D E F

ADA: 10. MSTs18 Build up the MST (black tree) by repeatedly adding the minimum crossing edge (thick red line) to it o a crossing edge connects a node in the MST to the rest of the graph. Ignore edges that form cycles (grey lines). Prim's Algorithm Graphically

ADA: 10. MSTs19 The following weighted graph: Example

ADA: 10. MSTs20 Building the MST

ADA: 10. MSTs

ADA: 10. MSTs Finished

ADA: 10. MSTs23 boolean marked[]; // for storing the vertices in the MST Queue mst; // for storing the edges in the MST MinPriQueue pq; // for storing the crossing (and ineligible) edges Prim's in More Detail

ADA: 10. MSTs24 void prims(Graph graph, int start) { visit(graph, start); while (!pq.isEmpty()) { Edge e = pq.remove(); // get lowest weight edge int v = e.either(); // (v,w) are the ends of the edge e int w = e.other(v); if (!marked[v] || !marked[w]) { mst.add(e); // add edge to MST queue if (!marked[v]) // visit vertex v or w visit(graph, v); if (!marked[w]) visit(graph, w); } } // end of prims()

ADA: 10. MSTs25 void visit(Graph graph, int v) /* Mark v and add to priority queue all the edges from v to unmarked vertices */ { marked[v] = true; for (Edge e : graph.adj(v)) if (!marked[e.other(v)]) pq.add(e); }

ADA: 10. MSTs26 The running time depends on the implementation of the minimum priority queue, which uses a minimum heap. o An add() takes time O(log n), and remove is O(1) o At most E edges are added to the priority queue and at most E are removed. The algorithm has a worst case running time of O(E log E). Running Time

ADA: 10. MSTs27 Krukal's algorithm finds a minimum spanning tree T by iteratively adding edges to T. At each iteration, a minimum-weight edge is added that does not create a cycle in the current T. The new edge can come from anywhere in G. The algorithm can stop after |V|-1 edges have been added to T. 3. Kruskal's Algorithm

ADA: 10. MSTs28 Tree kruskal(Graph G, int numVerts) { Tree T = allVerts(G); for i = 1 to numVerts-1 { Edge e = select an edge of minimum weight from G which does not form a cycle in T; T = T + e ; } return T } Simple Pseudocode

ADA: 10. MSTs29 Graph G: Example 1 a bc d e f g h i j k l iteredge 1(c,d) 2(k,l) 3(b,f) 4(c,g) 5(a,b) 6(f, j) 7(b,c) 8(j,k) 9(g,h) 10(i, j) 11(a,e) continued

ADA: 10. MSTs30 Minimum spanning tree (weight = 24): a bc d e f g h i j k l

ADA: 10. MSTs31 Example 2 next MST edge edges sorted by weight black edges are used in MST

ADA: 10. MSTs32 Queue mst; // for storing the edges in the MST MinPriQueue pq; // for storing the all the edges from G UnionFind uf; /* used to maintain disjoint sets of vertices: one for vertices in the MST, and other sets for nodes outside */ Kruskal's in More Detail

ADA: 10. MSTs33 void kruskal(Graph graph) { pq.addAll(graph.edges()); // add all edges to pri queue uf.makeSets(graph.vertices()); // create disjoint sets of V's while ((!pq.isEmpty()) && (mst.size() < graph.vertices().size()-1)) { Edge e = pq.remove(); // get lowest weight edge int v = e.either(); // (v,w) are the ends of the edge e int w = e.other(v); if (!uf.isConnected(v, w)) { // are v and w not in same set? // only one of v or w must be in the MST set uf.union(v, w); // combine v and w's sets mst.add(e); // add edge to mst } } // end of kruskal()

ADA: 10. MSTs34 As with Prim's algorithm, the running time depends on the implementation of the minimum priority queue, which uses a minimum heap. The algorithm also uses a Union-find data structure o E find() and isConnected() calls, which are both O(log n) The algorithm has a worst case running time of O(E log E) – same as Prim's o in practice, Prim's algorithm is usually faster than Kruskal's Running Time

ADA: 10. MSTs35 A disjoint-set data structure keeps track of a set of elements split into disjoint (non-overlapping) subsets. Union-find consists of two main operations: o find(): report which subset a particular element is in o union(): join two subsets into a single subset o others: makeSets(), isConnected(), etc.Union-find

ADA: 10. MSTs36

ADA: 10. MSTs37 Prim's algorithm chooses an edge that must be connected to a vertex in the minimum spanning tree T. Kruskal's algorithm chooses an edge from G that may or may not be connected to a vertex in T. 4. Difference between Prim and Kruskal