Lecture Notes for Chapter 23: Minimum Spanning Trees

Similar documents
Minimum Spanning Trees

Minimum Spanning Trees My T. UF

Minimum Spanning Trees

COP 4531 Complexity & Analysis of Data Structures & Algorithms

Minimum spanning trees

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

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

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

Minimum Spanning Trees

Minimum Spanning Trees

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

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

Chapter 23. Minimum Spanning Trees

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

CHAPTER 23. Minimum Spanning Trees

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

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

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

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

Introduction to Algorithms

Lecture 10. Elementary Graph Algorithm Minimum Spanning Trees

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

Introduction to Algorithms

Minimum Spanning Trees

Announcements Problem Set 5 is out (today)!

Minimum Spanning Trees Ch 23 Traversing graphs

Introduction to Algorithms

Partha Sarathi Manal

2 A Template for Minimum Spanning Tree Algorithms

23.2 Minimum Spanning Trees

Complexity of Prim s Algorithm

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

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

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

Minimum Spanning Trees Outline: MST

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

Minimum Spanning Trees and Prim s Algorithm

Lecture 10: Analysis of Algorithms (CS ) 1

6.1 Minimum Spanning Trees

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

Minimum Spanning Tree (undirected graph)

Greedy Algorithms Part Three

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

Week 11: Minimum Spanning trees

Minimum Spanning Tree

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

Theory of Computing. Lecture 10 MAS 714 Hartmut Klauck

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

Taking Stock. Last Time Flows. This Time Review! 1 Characterize the structure of an optimal solution

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

Algorithm Design and Analysis

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

Minimum Spanning Trees

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

Theory of Computing. Lecture 10 MAS 714 Hartmut Klauck

CIS 121 Data Structures and Algorithms Minimum Spanning Trees

Kruskal s MST Algorithm

Introduction: (Edge-)Weighted Graph

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

Unit 2: Algorithmic Graph Theory

Depth-first Search (DFS)

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

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

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

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

Shortest Path Algorithms

CSE331 Introduction to Algorithms Lecture 15 Minimum Spanning Trees

CSE 100 Minimum Spanning Trees Prim s and Kruskal

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

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.

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

COMP251: Single source shortest paths

Minimum Spanning Tree (5A) Young Won Lim 5/11/18

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

CS161 - Minimum Spanning Trees and Single Source Shortest Paths

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

Shortest path problems

Minimum Spanning Trees

Minimum Spanning Trees. Minimum Spanning Trees. Minimum Spanning Trees. Minimum Spanning Trees

Greedy Algorithms. Textbook reading. Chapter 4 Chapter 5. CSci 3110 Greedy Algorithms 1/63

LECTURE 26 PRIM S ALGORITHM

Design and Analysis of Algorithms

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

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

CSE 332: Data Structures & Parallelism Lecture 22: Minimum Spanning Trees. Ruth Anderson Winter 2018

looking ahead to see the optimum

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

1 Minimum Spanning Trees: History

CS 4349 Lecture October 18th, 2017

Week 12: Minimum Spanning trees and Shortest Paths

CSE 100: GRAPH ALGORITHMS

Lecture Summary CSC 263H. August 5, 2016

Minimum-Cost Spanning Tree. Example

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

Example. Minimum-Cost Spanning Tree. Edge Selection Greedy Strategies. Edge Selection Greedy Strategies

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

COP 4531 Complexity & Analysis of Data Structures & Algorithms

Solutions to relevant spring 2000 exam problems

Algorithms. Algorithms. Algorithms 4.3 MINIMUM SPANNING TREES

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

Transcription:

Lecture Notes for Chapter 23: Minimum Spanning Trees Chapter 23 overview Problem A town has a set of houses and a set of roads. A road connects 2 and only 2 houses. A road connecting houses u and v has a repair cost w(u,v). Goal: Repair enough (and no more) roads such that 1. everyone stays connected: can reach every house from all other houses, and 2. total repair cost is minimum. Model as a graph: Undirected graph G = (V, E). Weight w(u,v)on each edge (u,v) E. Find T E such that 1. T connects all vertices (T is a spanning tree), and 2. w(t ) = w(u,v)is minimized. (u,v) T A spanning tree whose weight is minimum over all spanning trees is called a minimum spanning tree, ormst. Example of such a graph [edges in MST are shaded] : a 10 12 9 b c 8 8 d 7 e 3 3 1 f 5 6 g h 9 2 11 i In this example, there is more than one MST. Replace edge (e, f ) by (c, e). Get a different spanning tree with the same weight.

23-2 Lecture Notes for Chapter 23: Minimum Spanning Trees Growing a minimum spanning tree Some properties of an MST: It has V 1 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. Generic MST algorithm GENERIC-MST(G,w) A while A is not a spanning tree do Þnd 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 satisþes 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 an MST. Finding a safe edge How do we Þnd safe edges? Let s look at the example. Edge (c, f ) has the lowest weight of any edge in the graph. Is it safe for A =? Intuitively: Let S V be any set of vertices that includes c but not f (so that f is in V S). In any MST, there has to be one edge (at least) that connects S with V S. Why not choose the edge with minimum weight? (Which would be (c, f ) in this case.) Some deþnitions: Let S V and A E.

Lecture Notes for Chapter 23: Minimum Spanning Trees 23-3 A cut (S, V S) is a partition of vertices into disjoint sets V and S V. 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 > 1 light edge crossing it. 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 an 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)}. Recall: a tree has unique path between each pair of vertices. Since T is an 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). S u x v y V S [Except for the dashed edge (u,v), all edges shown are in T. A is some subset of the edges of T, but A cannot contain any edges that cross the cut (S, V S), since this cut respects A. Shaded edges are the path p.] 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.

23-4 Lecture Notes for Chapter 23: Minimum Spanning Trees 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 an MST, then T must be an 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 an MST, (u,v)is safe for A. (theorem) 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 an MST has exactly V 1 edges, the for loop iterates V 1 times. Equivalently, after adding V 1 safe edges, we re down to just one component. 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 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.

Lecture Notes for Chapter 23: Minimum Spanning Trees 23-5 KRUSKAL(V, E,w) A for each vertex v V do MAKE-SET(v) sort E into nondecreasing order by weight w for each (u,v)taken from the sorted list do if FIND-SET(u) = FIND-SET(v) then A A {(u,v)} UNION(u,v) return A Run through the above example to see how Kruskal s algorithm works on it: (c, f ) : safe (g, i) : safe (e, f ) : safe (c, e) : reject (d, h) : safe ( f, h) : safe (e, d) : reject (b, d) : safe (d, g) : safe (b, c) : reject (g, h) : reject (a, b) : safe At this point, we have only one component, so all other edges will be rejected. [We could add a test to the main loop of KRUSKAL to stop once V 1 edges have been added to A.] Get the shaded edges shown in the Þgure. Suppose we had examined (c, e) before (e, f ). Then would have found (c, e) safe and would have rejected (e, f ). Analysis Initialize A: O(1) First for loop: V MAKE-SETs Sort E: O(E lg E) Second for loop: O(E) FIND-SETs and UNIONs Assuming the implementation of disjoint-set data structure, already seen in Chapter 21, that uses union by rank and path compression: O((V + E)α(V )) + O(E lg E). Since G is connected, E V 1 O(E α(v )) + O(E lg E). α( V ) = O(lg V ) = O(lg E). Therefore, total time is O(E lg E). E V 2 lg E = O(2lgV ) = O(lg V ).

23-6 Lecture Notes for Chapter 23: Minimum Spanning Trees Therefore, O(E lg V ) time. (If edges are already sorted, O(E α(v )), which is almost linear.) Prim s algorithm Builds one tree, so A is always a tree. Starts from an arbitrary root r. At each step, Þnd a light edge crossing cut (V A, V V A ), where V A = vertices that A is incident on. Add this edge to A. V A [Edges of A are shaded.] light edge How to Þnd the light edge quickly? Use a priority queue Q: Each object 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. 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}}.

Lecture Notes for Chapter 23: Minimum Spanning Trees 23-7 PRIM(V, E,w,r) Q for each u V do key[u] π[u] NIL INSERT(Q, u) DECREASE-KEY(Q, r, 0) key[r] 0 while Q = do u EXTRACT-MIN(Q) for each v Adj[u] do if v Q and w(u,v)<key[v] then π[v] u DECREASE-KEY(Q,v,w(u,v)) Do example from previous graph. [Let a student pick the root.] Analysis Depends on how the priority queue is implemented: Suppose Q is a binary heap. Initialize Q and Þrst 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 lg V ) Total: O(E lg V ) Suppose we could do DECREASE-KEY in O(1) amortized time. Then E DECREASE-KEY calls take O(E) time altogether total time becomes O(V lg V + E). In fact, there is a way to do DECREASE-KEY in O(1) amortized time: Fibonacci heaps, in Chapter 20.