Data Structures – LECTURE 13 Minumum spanning trees

Slides:



Advertisements
Similar presentations
Comp 122, Spring 2004 Greedy Algorithms. greedy - 2 Lin / Devi Comp 122, Fall 2003 Overview  Like dynamic programming, used to solve optimization problems.
Advertisements

Greedy Algorithms Greed is good. (Some of the time)
Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
Minimum Spanning Tree CSE 331 Section 2 James Daly.
Chapter 23 Minimum Spanning Trees
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 13 Minumum spanning trees Motivation Properties of minimum spanning trees Kruskal’s.
Minimum Spanning Tree Algorithms
CSE 780 Algorithms Advanced Algorithms Minimum spanning tree Generic algorithm Kruskal’s algorithm Prim’s algorithm.
Lecture 18: Minimum Spanning Trees Shang-Hua Teng.
1 Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
Analysis of Algorithms CS 477/677
Lecture 12 Minimum Spanning Tree. Motivating Example: Point to Multipoint Communication Single source, Multiple Destinations Broadcast – All nodes in.
CSE Algorithms Minimum Spanning Trees Union-Find Algorithm
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
1.1 Data Structure and Algorithm Lecture 13 Minimum Spanning Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Minimum Spanning Trees.
Design and Analysis of Computer Algorithm September 10, Design and Analysis of Computer Algorithm Lecture 5-2 Pradondet Nilagupta Department of Computer.
Graph, Spring 2004 © L. Joskowicz 1 Graphs and basic search algorithms Motivation Definitions and properties Representation Breadth-First Search Depth-First.
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
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.
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)
© 2007 Seth James Nielson Minimum Spanning Trees … or how to bring the world together on a budget.
 2004 SDU Lecture 6- Minimum Spanning Tree 1.The Minimum Spanning Tree Problem 2.Greedy algorithms 3.A Generic Algorithm 4.Kruskal’s Algorithm.
1 22c:31 Algorithms Minimum-cost Spanning Tree (MST)
© 2007 Seth James Nielson Minimum Spanning Trees … or how to bring the world together on a budget.
1 Week 3: Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
MST Lemma Let G = (V, E) be a connected, undirected graph with real-value weights on the edges. Let A be a viable subset of E (i.e. a subset of some 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.
Minimum Spanning Tree. p2. Minimum Spanning Tree G=(V,E): connected and undirected w: E  R, weight function a b g h i c f d e
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
November 22, Algorithms and Data Structures Lecture XII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Tirgul 12 Solving T4 Q. 3,4 Rehearsal about MST and Union-Find
Greedy Algorithms General principle of greedy algorithm
Lecture ? The Algorithms of Kruskal and Prim
Minimum Spanning Trees
Algorithm Analysis Fall 2017 CS 4306/03
Introduction to Algorithms
Minimum Spanning Trees
Spanning Trees Kruskel’s Algorithm Prim’s Algorithm
CS 3343: Analysis of Algorithms
Minimum Spanning Trees
Lecture 12 Algorithm Analysis
CISC 235: Topic 10 Graph Algorithms.
Greedy Algorithms / Minimum Spanning Tree Yin Tat Lee
Minimum Spanning Tree.
Algorithms and Data Structures Lecture XII
Chapter 23 Minimum Spanning Tree
CS 583 Analysis of Algorithms
Minimum Spanning Trees
Kruskal’s Minimum Spanning Tree Algorithm
Analysis of Algorithms CS 477/677
Lecture 12 Algorithm Analysis
Minimum Spanning Tree Algorithms
Minimum Spanning Trees
Minimum Spanning Trees
Minimum Spanning Tree.
Minimum Spanning Trees
Greedy Algorithms Comp 122, Spring 2004.
Introduction to Algorithms: Greedy Algorithms (and Graphs)
Lecture 12 Algorithm Analysis
Total running time is O(E lg E).
Advanced Algorithms Analysis and Design
Binhai Zhu Computer Science Department, Montana State University
Chapter 23: Minimum Spanning Trees: A graph optimization problem
Minimum Spanning Trees
Presentation transcript:

Data Structures – LECTURE 13 Minumum spanning trees Motivation Properties of minimum spanning trees Kruskal’s algorithm Prim’s algorithm Chapter 23 in the textbook (pp 561—579). Data Structures, Spring 2004 © L. Joskowicz

Motivation Given a set of nodes and possible connections with weights between them, find the subset of connections that connects all the nodes and whose sum of weights is the smallest. Examples: telephone switching network electronic board wiring The nodes and subset of connections form a tree! This tree is called the Minimum Spanning Tree (MST – (עץ פורש מינימום Data Structures, Spring 2004 © L. Joskowicz

Example: spanning tree 8 7 b c d h g f i a e Cost: 51 4 9 11 2 14 4 7 6 8 10 1 2 Data Structures, Spring 2004 © L. Joskowicz

Example: minimum spanning tree b c d h g f i a e 8 7 1 2 11 14 4 6 9 10 Cost: 37 Data Structures, Spring 2004 © L. Joskowicz

Spanning trees Definition: Let G=(V,E) be a weighted connected undirected graph. A spanning tree of G is a subset T  E of edges, such that the sub-graph G’=(V,T) is connected and acyclic. The minimum spanning tree (MST) is a spanning tree that minimizes the sum: Data Structures, Spring 2004 © L. Joskowicz

Generic MST algorithm Greedy strategy: grow the minimum spanning tree one edge at a time, making sure that the added edge preserves the tree structure and the minimality condition add “safe” edges incrementally. Generic-MST(G=(V,E)) T = ; while (T is not a spanning tree of G) do choose a safe edge e=(u,v)  E T = T  {e} return T Invariant: the set of edges T is a subset of a spanning tree. The invariant is trivially satisfied when T is initialized to become the empty set. If the invariant is preserved in the while loop, it follows that the returned set of edges is indeed a spanning tree. Data Structures, Spring 2004 © L. Joskowicz

Properties of MST (1) Question: how to find safe edges efficiently? Theorem 1: Let and e=(u,v) be a minimum weight edge with one endpoint in U and the other in V–U. Then there exists a minimum spanning tree T such that e is in T. V U V–U T Data Structures, Spring 2004 © L. Joskowicz

Properties of MST (2) U cut V–U 8 7 b c d h g f i a e 4 9 11 2 14 4 7 6 8 10 1 2 Data Structures, Spring 2004 © L. Joskowicz

Properties of MST (2) Proof: Let T be an MST. If e is not in T, add e to T. Because T is a tree, the addition of e creates a cycle which contains e and at least one more edge e’=(u’,v’), where u’U and v’ V–U. Clearly, w(e) ≤ w(e’) since e is of minimum weight among the edges connecting U and V–U. We can thus delete e’ from T. The resulting T’ = T – {e’} {e} is a tree whose weight is less or equal than that of T: w(T’) ≤ w(T). Data Structures, Spring 2004 © L. Joskowicz

Properties of MST (3) Theorem 2: Let G=(V,E) be a connected undirected graph and A a subset of E included in a minimum spanning tree T for G. Let (U, V–U) be a cut that respects A (no edge of A crosses the cut), and let e=(u,v) be a minimum weight edge crossing (U, V–U). Then e is safe for A. V U V–U T A = T  E’ cut Data Structures, Spring 2004 © L. Joskowicz

Properties of MST (4) Proof: Define an edge e to be a light edge crossing a cut if its weight is the minimum crossing the cut. Let T be an MST that includes A, and assume T does not contain the light edge e = (u,v) (if it does, e is safe). Construct another MST T’ that includes A  {e}. The edge forms a cycle with edges on the path p from u to v in T. Since u and v are on opposite sides of the cut, there is at least one edge e’ = (x,y) in T on the path p that also crosses the cut. The edge e’ is not in A because the cut respects A. Since e’ is on the unique path from u to v in T, removing it breaks T into two components. Data Structures, Spring 2004 © L. Joskowicz

Properties of MST (5) Adding e = (u,v) reconnects the two components to form a new spanning tree: T’ = T –{e’}  {e} We now show that T’ is an MST. Since e = (u,v) is a light edge crossing (U, V–U) and e’ = (x,y) also crosses this cut, w(u,v) ≤ w(x,y). Thus: w(T’) = w(T) – w(u,v) + w(x,y) ≤ w(T) Since T is an MST and w(T’) ≤ w(T), then w(T’) = w(T) and T’ is also an MST. Data Structures, Spring 2004 © L. Joskowicz

Properties of MST (6) Corollary: Let G=(V,E) be a connected undirected graph and A a subset of E included in a minimum spanning tree T for G, and let C = (VC, EC) be a tree in the forest GA = (V,A). If e is a light edge connecting C to some other component in GA, then e is safe for A. Proof: The cut (VC, V–VC) respects A, and e is a light edge for this cut. Therefore, e is safe. Data Structures, Spring 2004 © L. Joskowicz

Two algorithms to find an MST There are two ways of adding a safe edge: Kruskal’s algorithm: the set A is a forest and the safe edge added is always the least-weight edge in the graph connecting two distinct components (Theorem 2). Prim’s algorithm: the set A is a tree and the safe edge added is always the least-weight edge connecting A to a vertex not in A (Theorem 1). Data Structures, Spring 2004 © L. Joskowicz

Kruskal’s algorithm MST-Kruskal(G) A   for each vertex vV do Make-Set(v) sort the edges in E in non-decreasing weight order for each edge e = (u,v)E do if Find-Set(u) ≠ Find-Set(v) /* the trees are distinct */ then A  A  {e} Union(u,v) /* combine two trees */ return A Time analysis: O(V) for initialization, O(E lg E) for sorting the edges. There are O(V+E) disjoint-set operations, which take O((E+V)(E+V)) time. Thus, the running time is dominated by O(E lg E). Data Structures, Spring 2004 © L. Joskowicz

Example: Kruskal’s algorithm 8 7 b c d h g f i a e 4 9 11 2 14 4 7 X X 6 X 8 10 1 2 Cost: 37 Data Structures, Spring 2004 © L. Joskowicz

Analysis of Kruskal’s algorithm Correctness: follows directly from Theorem 2. Complexity: Depends on the implementation of the set operations! A naïve implementation takes O(|V| |E|). Sorting the edges takes O(|E| lg |E|). the for loop goes over every edge and performs two Find-Set and one Union operation. These can be implemented to take O(1) amortized time. The total running time is O(|E| lg |E|) = O(|E| lg |V|). Data Structures, Spring 2004 © L. Joskowicz

Prim’s algorithm MST-Prim(G, root) for each vertex vV do key(v)  ∞; π[v]  null key(root)  0; Q  V while Q is not empty do u  Extract-Min(Q) for each v that is a neighbor of u do if vQ and w(u,v) < key(v) then π[v]  u key(v)  w(u,v) /*decrease value of key */ Time analysis: O(V) for initialization, O(E lg E) for sorting the edges. There are O(V+E) disjoint-set operations, which take O((E+V)(E+V)) time. Thus, the running time is dominated by O(E lg E). Data Structures, Spring 2004 © L. Joskowicz

Example: Prim’s algorithm ∞ 8 7 b c d h g f i a e 4 9 11 2 14 4 7 6 8 10 1 2 Cost: 37 Data Structures, Spring 2004 © L. Joskowicz

Analysis of Prim’s algorithm Correctness: follows directly from the Theorem 1. Complexity: Depends on the implementation of the minimum priority queue. With a binary mean-heap, we have: Building the initial heap takes O(|V|). Extract-Min takes O(lg |V|) per vertex  total O(|V| lg |V|) The for loop is executed O(|E|). Membership test is O(1). Decreasing a key is O(lg |V|). Overall, the running time is O(|V| lg |V| + |E| lg |V|) = O(|E| lg |V|). Data Structures, Spring 2004 © L. Joskowicz

Summary: MST MST is a tree of all nodes with minimum total cost Two greedy algorithms for finding MST: Kruskal’s algorithm: edge-based. Runs in O(|V| |E|). Prim’s algorithm: vertex-based. Runs in O(|E| lg |V|). Complexity of Kruskal’s algorithm can be improved with Union-Find ADT to O(|E| lg |V|), Complexity of Prim’s algorithm can be improved with Fibonacci heaps to O(|V| lg |V| + |E|). Randomized algorithm takes O(|V| + |E|) expected time. Data Structures, Spring 2004 © L. Joskowicz