Minimum 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)
Greedy Algorithms Pasi Fränti Greedy algorithm 1.Coin problem 2.Minimum spanning tree 3.Generalized knapsack problem 4.Traveling salesman problem.
Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 13 Minumum spanning trees Motivation Properties of minimum spanning trees Kruskal’s.
CSE 780 Algorithms Advanced Algorithms Minimum spanning tree Generic algorithm Kruskal’s algorithm Prim’s algorithm.
3 -1 Chapter 3 The Greedy Method 3 -2 The greedy method Suppose that a problem can be solved by a sequence of decisions. The greedy method has that each.
Lecture 18: Minimum Spanning Trees Shang-Hua Teng.
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)
Analysis of Algorithms CS 477/677
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.
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.
Analysis of Algorithms
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
ADA: 10. MSTs1 Objective o look at two algorithms for finding mimimum spanning trees (MSTs) over graphs Prim's algorithm, Kruskal's algorithm Algorithm.
COSC 3101NJ. Elder Assignment 2 Remarking Assignment 2 Marks y = 0.995x R 2 = Old Mark New Mark.
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 Minimum Spanning Trees (some material adapted from slides by Peter Lee, Ananda Guna, Bettina Speckmann)
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)
1 Week 3: Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm.
WEEK 12 Graphs IV Minimum Spanning Tree Algorithms.
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
Greedy Algorithms General principle of greedy algorithm
Lecture ? The Algorithms of Kruskal and Prim
Chapter 9 : Graphs Part II (Minimum Spanning Trees)
Introduction to Algorithms
Lecture 22 Minimum Spanning Tree
Minimum Spanning Trees
Lecture 12 Algorithm Analysis
Minimum Spanning Trees
Short paths and spanning trees
Minimum Spanning Tree.
CSE373: Data Structures & Algorithms Lecture 20: Minimum Spanning Trees Linda Shapiro Spring 2016.
CSE 373 Data Structures and Algorithms
Algorithms and Data Structures Lecture XII
Data Structures – LECTURE 13 Minumum spanning trees
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
CSE332: Data Abstractions Lecture 18: Minimum Spanning Trees
Minimum Spanning Tree.
Minimum Spanning Trees
CSE 373: Data Structures and Algorithms
Greedy Algorithms Comp 122, Spring 2004.
CSE 373: Data Structures and Algorithms
Lecture 12 Algorithm Analysis
Advanced Algorithms Analysis and Design
CSE 373: Data Structures and Algorithms
Chapter 23: Minimum Spanning Trees: A graph optimization problem
Chapter 9: Graphs Spanning Trees
Minimum Spanning Trees
Presentation transcript:

Minimum Spanning Trees "One day Chao-Chou fell down in the snow, and called out: "Help me! Help Me!" A monk came and lay down beside him. Chao-Chou got up and went away." - Zen Koan CLRS, Sections 23.1 – 23.2

Weighted Graphs A weighted graph G is G=<V,E>: V is the set of nodes. E is the set of edges (directed or undirected). with the weight function w that assigns a weight to each edge (u,v) in E. For example: w(Leeds, Sheffield) = 59 and w(Manchester, Liverpool) = 61 CS 321 - Data Structures

Minimum Spanning Tree (MST) Let G = <V,E> be a connected, undirected weighted graph. The minimum-weight spanning tree T is an acyclic subset of E that connects all vertices of V and whose weight is: 𝑤(𝑇) = 𝑢,𝑣 ∈ 𝑇 𝑤(𝑢,𝑣) is minimized. Since T is acyclic and connects all the vertices, T is a tree. CS 321 - Data Structures

Example: MST In this graph, the shaded edges represent a MST T with weight w(T)=43. CS 321 - Data Structures

Example: MST The MST is not unique. If we replace the edge (e,f) with the edge (e,c), we obtain another MST T’ with the same weight w(T’)=43. CS 321 - Data Structures

MST Algorithms Kruskal’s Algorithm Prim’s Algorithm CS 321 - Data Structures

Kruskal’s Algorithm Partition vertices into clusters. Initially, single-vertex clusters. Sort edges according to weight. Merge “closest” clusters. When merge clusters, add edge to, A, a set of edges that will be included in an MST. At the end of the algorithm: All vertices merged into a single cluster. The edges in the set A represent an MST. CS 321 - Data Structures

Kruskal’s Algorithm Input: G=<V,E>, an undirected graph. w, edge weight function. Find-Set(u) returns cluster of vertices containing u. UNION(u,v) combines clusters containing u and v. CS 321 - Data Structures

Example: Kruskal’s Algorithm B C A D 1 3 5 10 8 7 E 11 9 Esorted={(A,B),(D,E),(B,C),(A,C),(B,E),(C,E),(A,D),(C,D)} A={} Clusters ={{A},{B},{C},{D},{E}} CS 321 - Data Structures

Example: Kruskal’s Algorithm B C A D 1 3 5 10 8 7 E 11 9 remove (A,B) Find-Set(A) ≠ Find-Set(B) add (A,B) to A Union(Find-Set(A), Find-Set(B)) Esorted={(D,E),(B,C),(A,C),(B,E),(C,E),(A,D),(C,D)} A={(A,B)} Clusters ={{A,B},{C},{D},{E}} CS 321 - Data Structures

Example: Kruskal’s Algorithm B C A D 1 3 5 10 8 7 E 11 9 remove (D,E) Find-Set(D) ≠ Find-Set(E) add (D,E) to A Union(Find-Set(D), Find-Set(E)) Esorted={(B,C),(A,C),(B,E),(C,E),(A,D),(C,D)} A={(A,B),(D,E)} Clusters ={{A,B},{C},{D,E}} CS 321 - Data Structures

Example: Kruskal’s Algorithm B C A D 1 3 5 10 8 7 E 11 9 remove (B,C) Find-Set(B) ≠ Find-Set(C) add (B,C) to A Union(Find-Set(B), Find-Set(C)) Esorted={(A,C),(B,E),(C,E),(A,D),(C,D)} A={(A,B),(D,E),(B,C)} Clusters ={{A,B,C},{D,E}} CS 321 - Data Structures

Example: Kruskal’s Algorithm B C A D 1 3 5 10 8 7 E 11 9 remove (A,C) Find-Set(A) = Find-Set(C) don’t add (A,C) to A Esorted={(B,E),(C,E),(A,D),(C,D)} A={(A,B),(D,E),(B,C)} Clusters ={{A,B,C},{D,E}} CS 321 - Data Structures

Example: Kruskal’s Algorithm 10 B C A D 1 3 5 8 7 E 11 9 remove (B,E) Find-Set(B) ≠ Find-Set(E) add (B,E) to A Union(Find-Set(B), Find-Set(E)) Esorted={(C,E),(A,D),(C,D)} A={(A,B),(D,E),(B,C),(B,E)} Clusters ={{A,B,C,D,E}} CS 321 - Data Structures

Example: Kruskal’s Algorithm 10 B C A D 1 3 5 8 7 E 11 9 remove (C,E) Find-Set(C) = Find-Set(E) don’t add (C,E) to A Esorted={(A,D),(C,D)} A={(A,B),(D,E),(B,C),(B,E)} Clusters ={{A,B,C,D,E}} CS 321 - Data Structures

Example: Kruskal’s Algorithm 10 B C A D 1 3 5 8 7 E 11 9 remove (A,D) Find-Set(A) = Find-Set(D) don’t add (A,D) to A Esorted={(C,D)} A={(A,B),(D,E),(B,C),(B,E)} Clusters ={{A,B,C,D,E}} CS 321 - Data Structures

Example: Kruskal’s Algorithm 10 B C A D 1 3 5 8 7 E 11 9 remove (C,D) Find-Set(C) = Find-Set(D) don’t add (C,D) to A Esorted={} A={(A,B),(D,E),(B,C),(B,E)} Clusters ={{A,B,C,D,E}} CS 321 - Data Structures

Example: Kruskal’s Algorithm 10 B C A D 1 3 5 8 7 E 11 9 MST A={(A,B),(D,E),(B,C),(B,E)} Clusters ={{A,B,C,D,E}} CS 321 - Data Structures

Kruskal’s Algorithm The cost of Kruskal’s algorithm is If implemented using the disjoint-set data structure Make-Set costs O(1) Find-Set costs O(log|V|) Union costs O(1) O(|V|) O(|E|log(|E|)) O(|E|log(|V|)) The cost of Kruskal’s algorithm is O(|E|log(|E|)) + O(|E|log(|V|)) = O(|E|log(|V|)) CS 321 - Data Structures

Prim’s Algorithm Given a start vertex r, that represents the root of the MST, grow the MST one vertex at a time. Store with each vertex v a key value representing the smallest weight of an edge connecting v to a vertex in the partial tree representing an MST. At each step: Add vertex u not in tree with the smallest key value. Update keys of the vertices adjacent to u. CS 321 - Data Structures

Q is a min-priority queue Prim’s Algorithm Input: G =<V,E>,an undirected graph. w, an edge weight function. r, the root vertex. Q is a min-priority queue CS 321 - Data Structures

Example: Prim’s Algorithm B D C A F E 7 4 2 8 5 3 9  Vertex A B C D E F Key  π - Q: A B C D E F CS 321 - Data Structures

Example: Prim’s Algorithm B D C A F E 7 4 2 8 5 3 9  u ← A Vertex A B C D E F Key 2 8  7 π - Q: B E C D F CS 321 - Data Structures

Example: Prim’s Algorithm B D C A F E 7 4 2 8 5 3 9  u ← B Vertex A B C D E F Key 2 5 7  π - Q: D E C F CS 321 - Data Structures

Example: Prim’s Algorithm B D C A F E 7 4 2 8 5 3 9 u ← D Vertex A B C D E F Key 2 5 7 4 π - Q: F E C CS 321 - Data Structures

Example: Prim’s Algorithm B D C A F E 7 4 2 8 5 3 9 u ← F Vertex A B C D E F Key 2 5 7 3 4 π - Q: E C CS 321 - Data Structures

Example: Prim’s Algorithm B D C A F E 7 4 2 8 5 3 9 u ← E Vertex A B C D E F Key 2 5 7 3 4 π - Q: C CS 321 - Data Structures

Example: Prim’s Algorithm B D C A F E 7 4 2 8 5 3 9 u ← C Vertex A B C D E F Key 2 8 7 3 4 π - Q: CS 321 - Data Structures

Example: Prim’s Algorithm B D C A F E 7 4 2 8 5 3 9 MST Vertex A B C D E F Key 2 5 7 3 4 π - CS 321 - Data Structures

Prim’s Algorithm Complexity Within the while loop, execute |V| EXTRACT-MIN for cost of O(log(|V|)) and update v.key for cost of O(log(|V|))at most |E| times O(|V|) O(1) O(|V|) O(|V|)log(|V|) O(|E|)log(|V|) The cost of Prim’s algorithm is The total cost of the while is O(|V|log(|V|)) + O(|E|log(|V|)) = = O(|E|log(|V|)) CS 321 - Data Structures

Greedy Algorithms The Kruskal’s and Prim’s algorithms are greedy algorithms. A greedy algorithm always makes the choice that looks best at the moment. It makes a locally optimal choice in the hope that this choice will lead to a globally optimal solution. Greedy algorithms do not always yield optimal solutions, but for many problems they do. For the MST problem, Kruskal’s and Prim’s algorithm produce optimal results. CS 321 - Data Structures

CS 321 - Data Structures

Generic Algorithm The generic algorithm manages a set of edges A that is always a subset of some minimum spanning tree. Initially A = {}. At each step we add a safe edge (u,v) to A. An edge (u,v) is safe to A if A U {(u,v)} is still a subset of a minimum spanning tree. CS 321 - Data Structures

Generic Algorithm INPUT G = (V,E) is an undirected graph w is the edge weighting function CS 321 - Data Structures

What kind of edges are safe to A? Generic Algorithm What kind of edges are safe to A? Let us introduce some definitions. A cut (S, V−S) is a partition of vertices into disjoint sets V and V−S. An edge (u,v) in E crosses the cut (S, V−S) if one endpoint is in S and the other is in V−S. A cut respects A if and only if no edge in A crosses the cut. An edge is a light edge crossing a cut if and only if its weight is the minimum over all edges crossing the cut. For a given cut there may be multiple light edges crossing it. CS 321 - Data Structures

Generic Algorithm Example Let S be the set of black nodes. Then (S,V−S) is a cut. A subset A of edges is shaded. A is a subset of some MST. An edge crosses the cut if it is between a black node and a white one. E.g. the edge (b,h) crosses the cut. The cut (S,V−S) respects the set A as A does not contain any edge crossing the cut. The edge (c,d) is a light edge crossing the cut. CS 321 - Data Structures

What kind of edges are safe to A? Generic Algorithm What kind of edges are safe to A? Theorem: Let A be a subset of some MST, (S, V−S) be a cut that respects A, and (u,v) be a light edge crossing (S, V−S). Then (u,v) is safe for A. In our example, the edge (c,d) is a safe edge to add to A. CS 321 - Data Structures

Generic Algorithm: Example Humans can easily recognize a cut that respects A If we choose the cut (S,V−S) where S = {e}, then a safe edge to add to A is either (e,b) or (e,d) Suppose that we have this graph. A initially is empty. CS 321 - Data Structures

Generic Algorithm: Example Humans can easily recognize a cut that respects A A = {(e,b)} A cut (S,V−S) that respects A is, for instance, S = {a} and V-S = {b,c,d,e}, then a safe edge to add to A is (a,b). CS 321 - Data Structures

Generic Algorithm: Example Humans can easily recognize a cut that respects A A = {(e,b), (a,b)} A cut (S,V−S) that respects A is, for instance, S = {d} and V-S = {a,b,c,e}, then a safe edge to add to A is (b,d). CS 321 - Data Structures

Generic Algorithm: Example Humans can easily recognize a cut that respects A A = {(e,b), (a,b), (b,d)} A cut (S,V−S) that respects A is now S = {c} and V-S = {a,b,d,e}, then a safe edge to add to A is (b,c). CS 321 - Data Structures

Generic Algorithm: Example Minimum Spanning Tree A = {(e,b), (a,b), (b,d), (b,c)} MST weight is 7 CS 321 - Data Structures

Generic Algorithm The algorithms of Kruskal and Prim each use a specific rule to determine a safe edge to add to A in the GENERIC-MST algorithm. CS 321 - Data Structures