Minimum Spanning Trees

Similar documents
Lecture Notes for Chapter 23: Minimum Spanning Trees

Minimum Spanning Trees My T. UF

COP 4531 Complexity & Analysis of Data Structures & Algorithms

Minimum spanning trees

Decreasing a key FIB-HEAP-DECREASE-KEY(,, ) 3.. NIL. 2. error new key is greater than current key 6. CASCADING-CUT(, )

Minimum Spanning Trees

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Minimum-Spanning-Tree problem. Minimum Spanning Trees (Forests) Minimum-Spanning-Tree problem

22 Elementary Graph Algorithms. There are two standard ways to represent a

Greedy Algorithms. At each step in the algorithm, one of several choices can be made.

Context: Weighted, connected, undirected graph, G = (V, E), with w : E R.

22 Elementary Graph Algorithms. There are two standard ways to represent a

Minimum Spanning Trees

Minimum Spanning Trees

2pt 0em. Computer Science & Engineering 423/823 Design and Analysis of Algorithms. Lecture 04 Minimum-Weight Spanning Trees (Chapter 23)

Announcements Problem Set 5 is out (today)!

Introduction to Algorithms

Introduction to Algorithms

CS 561, Lecture 9. Jared Saia University of New Mexico

Chapter 23. Minimum Spanning Trees

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

Partha Sarathi Manal

Introduction to Algorithms

CSE 431/531: Analysis of Algorithms. Greedy Algorithms. Lecturer: Shi Li. Department of Computer Science and Engineering University at Buffalo

Introduction: (Edge-)Weighted Graph

1 Start with each vertex being its own component. 2 Merge two components into one by choosing the light edge

CHAPTER 23. Minimum Spanning Trees

2 A Template for Minimum Spanning Tree Algorithms

G205 Fundamentals of Computer Engineering. CLASS 21, Mon. Nov Stefano Basagni Fall 2004 M-W, 1:30pm-3:10pm

UC Berkeley CS 170: Efficient Algorithms and Intractable Problems Handout 8 Lecturer: David Wagner February 20, Notes 8 for CS 170

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

Minimum Spanning Trees Outline: MST

Minimum Spanning Trees and Prim s Algorithm

23.2 Minimum Spanning Trees

CIS 121 Data Structures and Algorithms Minimum Spanning Trees

CSE 431/531: Algorithm Analysis and Design (Spring 2018) Greedy Algorithms. Lecturer: Shi Li

Algorithm Design and Analysis

Week 11: Minimum Spanning trees

CSE 100 Minimum Spanning Trees Prim s and Kruskal

Graphs and Network Flows ISE 411. Lecture 7. Dr. Ted Ralphs

CSE373: Data Structures & Algorithms Lecture 17: Minimum Spanning Trees. Dan Grossman Fall 2013

CS 5321: Advanced Algorithms Minimum Spanning Trees. Acknowledgement. Minimum Spanning Trees

6.1 Minimum Spanning Trees

We ve done. Introduction to the greedy method Activity selection problem How to prove that a greedy algorithm works Fractional Knapsack Huffman coding

Week 12: Minimum Spanning trees and Shortest Paths

Greedy Algorithms Part Three

Representations of Weighted Graphs (as Matrices) Algorithms and Data Structures: Minimum Spanning Trees. Weighted Graphs

Depth-first Search (DFS)

Algorithms and Data Structures: Minimum Spanning Trees (Kruskal) ADS: lecture 16 slide 1

Minimum Spanning Trees

Lecture 10: Analysis of Algorithms (CS ) 1

LECTURE 26 PRIM S ALGORITHM

Minimum Spanning Trees

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Part VI Graph algorithms. Chapter 22 Elementary Graph Algorithms Chapter 23 Minimum Spanning Trees Chapter 24 Single-source Shortest Paths

CSE331 Introduction to Algorithms Lecture 15 Minimum Spanning Trees

Theory of Computing. Lecture 10 MAS 714 Hartmut Klauck

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Dijkstra s algorithm for shortest paths when no edges have negative weight.

2.1 Greedy Algorithms. 2.2 Minimum Spanning Trees. CS125 Lecture 2 Fall 2016

Minimum Spanning Trees Ch 23 Traversing graphs

Minimum Spanning Tree

CSC 505, Fall 2000: Week 7. In which our discussion of Minimum Spanning Tree Algorithms and their data Structures is brought to a happy Conclusion.

Spanning Trees. CSE373: Data Structures & Algorithms Lecture 17: Minimum Spanning Trees. Motivation. Observations. Spanning tree via DFS

COMP 355 Advanced Algorithms

Algorithms and Theory of Computation. Lecture 5: Minimum Spanning Tree

Elementary Graph Algorithms: Summary. Algorithms. CmSc250 Intro to Algorithms

CS 4349 Lecture October 18th, 2017

1 Minimum Spanning Trees (MST) b 2 3 a. 10 e h. j m

Minimum Spanning Trees. Lecture II: Minimium Spanning Tree Algorithms. An Idea. Some Terminology. Dr Kieran T. Herley

Algorithms and Theory of Computation. Lecture 5: Minimum Spanning Tree

Minimum Spanning Tree (undirected graph)

Chapter 9. Greedy Technique. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

CHAPTER 13 GRAPH ALGORITHMS

Shortest Path Algorithms

Lecture 13. Reading: Weiss, Ch. 9, Ch 8 CSE 100, UCSD: LEC 13. Page 1 of 29

Undirected Graphs. DSA - lecture 6 - T.U.Cluj-Napoca - M. Joldos 1

Design and Analysis of Algorithms

managing an evolving set of connected components implementing a Union-Find data structure implementing Kruskal s algorithm

COS 226 Lecture 19: Minimal Spanning Trees (MSTs) PFS BFS and DFS examples. Classic algorithms for solving a natural problem

Algorithms and Data Structures: Minimum Spanning Trees I and II - Prim s Algorithm. ADS: lects 14 & 15 slide 1

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. February 26, 2019

CSE 373 MAY 10 TH SPANNING TREES AND UNION FIND

looking ahead to see the optimum

Introduction to Algorithms. Minimum Spanning Tree. Chapter 23: Minimum Spanning Trees

MST worksheet By Jim Xu

Minimum Spanning Trees

CSE 100: GRAPH ALGORITHMS

CSC 8301 Design & Analysis of Algorithms: Kruskal s and Dijkstra s Algorithms

Kruskal s MST Algorithm

The minimum spanning tree problem

Notes on Minimum Spanning Trees. Red Rule: Given a cycle containing no red edges, select a maximum uncolored edge on the cycle, and color it red.

CSE 421 Greedy Alg: Union Find/Dijkstra s Alg

Minimum Spanning Trees

Complexity of Prim s Algorithm

Algorithms. Algorithms. Algorithms 4.3 MINIMUM SPANNING TREES

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 20. Example. Shortest Paths Definitions

11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions

Chapter 9 Graph Algorithms

Lecture 25 Spanning Trees

Transcription:

Minimum Spanning Trees

Overview Problem A town has a set of houses and a set of roads. A road connects and only houses. A road connecting houses u and v has a repair cost w(u, v). Goal: Repair enough (and no more) roads such that. everyone stays connected: can reach every house from all other houses, and. total repair cost is minimum.

Overview Model as a graph A spanning tree whose weight is minimum over all spanning trees is called a minimum spanning tree, or MST.

Overview Example of such a graph [edges in MST are shaded] In this example, there is more than one MST. Replace edge (e, f ) by (c, e). Get a different spanning tree with the same 4 weight.

Growing a minimum spanning tree Some properties of an MST: It has V edges. It has no cycles. It might not be unique. Building up the solution We will build a set A of edges. Initially, A has no edges. As we add edges to A, maintain a loop invariant: Loop invariant: A is a subset of some MST. Add only edges that maintain the invariant. If A is a subset of some MST, an edge (u, v) is safe for A if and only if A {(u, v)} is also a subset of some MST. So we will add only safe edges. 5

Growing a minimum spanning tree Generic MST algorithm GENERIC-MST(G,w) A while A is not a spanning tree (i.e., while nodes are not completely connected) do find an edge (u, v) that is safe for A A A {(u, v)} return A Use the loop invariant to show that this generic algorithm works. Initialization: The empty set trivially satisfies the loop invariant. Maintenance: Since we add only safe edges, A remains a subset of some MST. Termination: All edges added to A are in an MST, so when we stop, A is a spanning tree that is also a MST.

Growing a minimum spanning tree Finding a safe edge Some definitions: Let S V and A E. A cut (S, V S) is a partition of vertices into disjoint sets S and V S. Edge (u, v) E crosses 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 minimum over all edges crossing the cut. For a given cut, there can be > light edge crossing it. 7

Growing a minimum spanning tree 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. Proof Let T be a MST that includes A. If T contains (u, v), done. So now assume that T does not contain (u, v). We ll construct a different MST T that includes A {(u, v)}.

Growing a minimum spanning tree Recall: a tree has unique path between each pair of vertices. Since T is a MST, it contains a unique path p between u and v. Path p must cross the cut (S, V S) at least once. Let (x, y) be an edge of p that crosses the cut. From how we chose (u, v), must have w(u, v) w(x, y). (Because of given fact that (u, v) is a light edge)

Growing a minimum spanning tree 0

Growing a minimum spanning tree Since the cut respects A, edge (x, y) is not in A. To form T from T: Remove (x, y). Breaks T into two components. Add (u, v). Reconnects. So T = T {(x, y)} {(u, v)}. T is a spanning tree. w(t ) = w(t ) w(x, y) + w(u, v) w(t), since w(u, v) w(x, y). Since T is a spanning tree, w(t ) w(t ), and T is a MST, then T must be a MST. Need to show that (u, v) is safe for A: A T and (x, y) A A T. A {(u, v)} T. Since T is a MST, (u, v) is safe for A. (theorem)

Growing a minimum spanning tree So, in GENERIC-MST: A is a forest containing connected components. Initially, each component is a single vertex. Any safe edge merges two of these components into one. Each component is a tree. Since a MST has exactly V edges, the for loop iterates V times. Equivalently, after adding V safe edges, we re down to just one component (which is MST).

Growing a minimum spanning tree Corollary If C = (V C, E C ) is a connected component in the forest G A = (V, A) and (u, v) is a light edge connecting C to some other component in G A (i.e., (u, v) is a light edge crossing the cut (V C, V V C )), then (u, v) is safe for A. Proof Set S = V C in the theorem. (corollary) This naturally leads to the algorithm called Kruskal s algorithm to solve the minimum-spanning-tree problem.

Kruskal s algorithm Kruskal s algorithm G = (V, E) is a connected, undirected, weighted graph. w : E R. Starts with each vertex being its own component. Repeatedly merges two components into one by choosing the light edge that connects them (i.e., the light edge crossing the cut between them). Scans the set of edges in monotonically increasing order by weight. Uses a disjoint-set data structure to determine whether an edge connects vertices in different components. There can be more than one connected components during merge process. 4

Kruskal s algorithm 5

Example of Kruskal s algorithm a 0 b e d 7 5 g i c f h A = {}

Example of Kruskal s algorithm a 0 b e d 7 5 g i c f h A = {(c, f)} 7

Example of Kruskal s algorithm a 0 b e d 7 5 g i c f h A = {(c, f), (g, i)}

Example of Kruskal s algorithm a 0 b e d 7 5 g i c f h A = {(c, f), (g, i), (e, f)}

Example of Kruskal s algorithm a 0 b e d 7 5 g i c f h A = {(c, f), (g, i), (e, f), (d, h)} 0

Example of Kruskal s algorithm a 0 b e d 7 5 g i c f h A = {(c, f), (g, i), (e, f), (d, h), (f, h)}

Example of Kruskal s algorithm a 0 b e d 7 5 g i c f h A = {(c, f), (g, i), (e, f), (d, h), (f, h), (b, d)}

Example of Kruskal s algorithm a 0 b e d 7 5 g i c f h A = {(c, f), (g, i), (e, f), (d, h), (f, h), (b, d), (d, g)}

Example of Kruskal s algorithm a 0 b e d 7 5 g i c f h A = {(c, f), (g, i), (e, f), (d, h), (f, h), (b, d), (d, g), (a, b)} 4

Kruskal s algorithm Analysis Initialize A: O() First for loop: V MAKE-SETs Sort E: O(E lg E) (sort edges using nlgn alg.) Second for loop: O(E) FIND-SETs and UNIONs Therefore, total time is O(E lg E). 5

Prim s algorithm Builds one tree, so A is always a tree. Starts from an arbitrary root r. At each step, find a light edge crossing cut (V A, V V A ), where V A = vertices that A is incident on. Add this edge to A.

Prim s algorithm How to find the light edge quickly? Use a priority queue Q: Each object, in Q, is a vertex in V V A. Key of v is minimum weight of any edge (u, v), where u V A. Then the vertex returned by EXTRACT-MIN is v such that there exists u V A and (u, v) is light edge crossing (V A, V V A ). Key of v is if v is not adjacent to any vertices in V A. 7

Prim s algorithm The edges of A will form a rooted tree with root r: r is given as an input to the algorithm, but it can be any vertex. Each vertex knows its parent in the tree by the attribute π[v] = parent of v. π[v] = NIL if v = r or v has no parent. As algorithm progresses, A = {(v, π[v]) : v V {r} Q}. At termination, V A = V Q =, so MST is A = {(v, π[v]) : v V {r}}.

Prim s algorithm v Q implies v is not in V A (i.e., v is not connected).

Example of Prim s algorithm a 0 b e d 7 5 g i c f 0 h Q = {a, b, c, d, e, f, g, h, i} V A = {} A = {} 0

Example of Prim s algorithm a 0 b e d 7 5 g i c f 0 h Q = {a, b, d, e, f, g, h, i} V A = {c} A = {}

Example of Prim s algorithm a 0 b e d 7 5 g i c 0 f h Q = {a, b, d, e, g, h, i} V A = {c, f} A = {(f, c)}

Example of Prim s algorithm a 0 b e 7 d 7 5 g i c 0 f h Q = {a, b, d, g, h, i} V A = {c, f, e} A = {(f, c), (e, f)}

Example of Prim s algorithm a 0 b e 5 d 7 5 g i c 0 f h Q = {a, b, d, g, i} V A = {c, f, e, h} A = {(f, c), (e, f), (h, f)} 4

Example of Prim s algorithm a 0 b e 5 d 7 5 g i c 0 f h Q = {a, b, g, i} V A = {c, f, e, h, d} A = {(f, c), (e, f), (h, f), (d, h)} 5

Example of Prim s algorithm 0 a 0 b e 5 d 7 5 g i c 0 f h Q = {a, g, i} V A = {c, f, e, h, d, b} A = {(f, c), (e, f), (h, f), (d, h), (b, d)}

Example of Prim s algorithm 0 a 0 b e 5 d 7 5 g i c 0 f h Q = {a, i} V A = {c, f, e, h, d, b, g} A = {(f, c), (e, f), (h, f), (d, h), (b, d), (g, d)} 7

Example of Prim s algorithm 0 a 0 b e 5 d 7 5 g i c 0 f h Q = {a} V A = {c, f, e, h, d, b, g, i} A = {(f, c), (e, f), (h, f), (d, h), (b, d), (g, d), (i, g)}

Example of Prim s algorithm 0 a 0 b e 5 d 7 5 g i c 0 f h Q = {} V A = {c, f, e, h, d, b, g, i, a} A = {(f, c), (e, f), (h, f), (d, h), (b, d), (g, d), (i, g), (a, b)}

Prim s algorithm Analysis Depends on how the priority queue is implemented: Suppose Q is a binary heap. Initialize Q and first for loop: O(V lg V) Decrease key of r: O(lg V) while loop: V EXTRACT-MIN calls O(V lg V) E DECREASE-KEY calls O(E lgv) ( E, since all the edge-weights can be used in DECREASE-KEY calls or some of them can be already smaller than w(u, v) being considered. ) Total: O(E lg V) 40