Design and Analysis of Algorithms

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

Design and Analysis of Algorithms

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

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

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

Lecture 10: Analysis of Algorithms (CS ) 1

Algorithm Design and Analysis

Kruskal s MST Algorithm

Trees, Trees and More Trees

COMP 355 Advanced Algorithms

Theory of Computing. Lecture 10 MAS 714 Hartmut Klauck

Minimum Spanning Trees Shortest Paths

CIS 121 Data Structures and Algorithms Minimum Spanning Trees

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

Minimum spanning trees

CMSC 451: Lecture 22 Approximation Algorithms: Vertex Cover and TSP Tuesday, Dec 5, 2017

Minimum Spanning Trees My T. UF

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

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

Minimum Spanning Trees

Chapter 23. Minimum Spanning Trees

COP 4531 Complexity & Analysis of Data Structures & Algorithms

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

Introduction to Algorithms

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

Algorithms for Minimum Spanning Trees

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

Theorem 2.9: nearest addition algorithm

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

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

Design and Analysis of Algorithms

CS 310 Advanced Data Structures and Algorithms

Lecture 8: The Traveling Salesman Problem

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

Minimum Spanning Trees COSC 594: Graph Algorithms Spring By Kevin Chiang and Parker Tooley

Autumn Minimum Spanning Tree Disjoint Union / Find Traveling Salesman Problem

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

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

Announcements Problem Set 5 is out (today)!

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

Minimum Spanning Trees

Minimum Spanning Trees

CSC 1700 Analysis of Algorithms: Minimum Spanning Tree

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

1 The Traveling Salesman Problem

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

Unit 8: Coping with NP-Completeness. Complexity classes Reducibility and NP-completeness proofs Coping with NP-complete problems. Y.-W.

The minimum spanning tree problem

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

Algorithms. Algorithms. Algorithms 4.3 MINIMUM SPANNING TREES

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

Example of why greedy step is correct

looking ahead to see the optimum

NP Completeness. Andreas Klappenecker [partially based on slides by Jennifer Welch]

Minimum Spanning Trees

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

CS261: A Second Course in Algorithms Lecture #16: The Traveling Salesman Problem

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

Greedy Algorithms Part Three

23.2 Minimum Spanning Trees

Minimum-Cost Spanning Tree. Example

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

Lecture Notes for Chapter 23: Minimum Spanning Trees

Week 11: Minimum Spanning trees

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

Approximation Algorithms

Module 6 P, NP, NP-Complete Problems and Approximation Algorithms

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

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

Notes for Recitation 9

V1.0: Seth Gilbert, V1.1: Steven Halim August 30, Abstract. d(e), and we assume that the distance function is non-negative (i.e., d(x, y) 0).

val(y, I) α (9.0.2) α (9.0.3)

1 The Traveling Salesman Problem

CS 6783 (Applied Algorithms) Lecture 5

2 Approximation Algorithms for Metric TSP

CSE 100 Minimum Spanning Trees Prim s and Kruskal

Minimum Spanning Trees Ch 23 Traversing graphs

CSE331 Introduction to Algorithms Lecture 15 Minimum Spanning Trees

Chapter 9 Graph Algorithms

Notes for Lecture 24

Minimum Spanning Trees

Traveling Salesman Problem. Algorithms and Networks 2014/2015 Hans L. Bodlaender Johan M. M. van Rooij

Matching 4/21/2016. Bipartite Matching. 3330: Algorithms. First Try. Maximum Matching. Key Questions. Existence of Perfect Matching

Module 6 NP-Complete Problems and Heuristics

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, Approximation Algorithms

tree follows. Game Trees

CS161 - Minimum Spanning Trees and Single Source Shortest Paths

Module 6 NP-Complete Problems and Heuristics

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

Introduction to Algorithms

Minimum Spanning Trees

Lecture 6 Basic Graph Algorithms

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

6.1 Minimum Spanning Trees

CSE202 Greedy algorithms. Fan Chung Graham

CSC 373: Algorithm Design and Analysis Lecture 4

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

Travelling Salesman Problem. Algorithms and Networks 2015/2016 Hans L. Bodlaender Johan M. M. van Rooij

Mathematical Tools for Engineering and Management

Transcription:

CSE 101, Winter 2018 Design and Analysis of Algorithms Lecture 9: Minimum Spanning Trees Class URL: http://vlsicad.ucsd.edu/courses/cse101-w18/ Goal: MST cut and cycle properties Prim, Kruskal greedy algorithms

Trees From Lecture 8 (Page 141 of PDF; Page 129 of hardcopy: Sidebar) A tree is connected and acyclic A tree on n nodes has n - 1 edges Any connected, undirected graph with V - 1 edges is a tree An undirected graph is a tree if and only if there is a unique path between any pair of nodes Fact about trees: Any two of the following properties imply the third: Connected Acyclic V - 1 edges

Spanning Tree vs. Steiner Tree

Spanning Trees vs. Steiner Trees Min-Cost Spanning Tree Problem: Easy, solvable with a greedy algorithm Min-Cost Steiner Tree Problem: Hard, no known polynomial-time algorithm 2 3 The red dot is called a Steiner point (= an intermediate junction ) The maximum cost savings from adding Steiner points = min ratio of (min Steiner tree cost) / (min spanning tree cost) depends on the metric, or distance function Cf. Manhattan, Euclidean, Chebyshev, Adapted from Prof. G. Robins, UVA

Minimum Steiner Tree is a Hard Problem (Minimum Spanning Tree was Easy) How would you approach finding a low-cost Steiner tree? Adapted from Prof. G. Robins, UVA

Iterated 1-Steiner Algorithm (1990) Given a pointset S, what point p minimizes MST(S {p}) Algorithmic idea: Iterate (greedily) In practice: solution cost is within 0.5% of OPT on average Adapted from Prof. G. Robins, UVA

Greedy Algorithms Kruskal s algorithm. Start with T =. Consider edges in ascending order of cost. Insert edge e in T unless doing so would create a cycle. Reverse-Delete algorithm. Start with T = E. Consider edges in descending order of cost. Delete edge e from T unless doing so would disconnect T. Prim s algorithm. Start with some root node s and greedily grow a tree T from s outward. At each step, add the cheapest edge e to T that has exactly one endpoint in T. Source: slides by Kevin Wayne for Kleinberg-Tardos, Algorithm Design, Pearson Addison-Wesley, 2005.

Greedy Algorithms Simplifying assumption. All edge costs c e are distinct. Cut property. Let S be any subset of nodes, and let e be the min cost edge with exactly one endpoint in S. Then the MST contains e. Cycle property. Let C be any cycle, and let f be the max cost edge belonging to C. Then the MST does not contain f. f C S e e is in the MST f is not in the MST Source: slides by Kevin Wayne for Kleinberg-Tardos, Algorithm Design, Pearson Addison-Wesley, 2005.

Cycles and Cuts Cycle. Set of edges of the form a-b, b-c, c-d,, y-z, z-a. 1 2 3 6 5 4 Cycle C = 1-2, 2-3, 3-4, 4-5, 5-6, 6-1 7 8 Cutset. A cut is a subset of nodes S. The corresponding cutset D is the subset of edges with exactly one endpoint in S. 1 2 6 5 4 3 Cut S = { 4, 5, 8 } Cutset D = 5-6, 5-7, 3-4, 3-5, 7-8 7 8 Source: slides by Kevin Wayne for Kleinberg-Tardos, Algorithm Design, Pearson Addison-Wesley, 2005.

Illustration

Intersection of a Cycle and a Cutset Fact. Any cycle and any cutset must intersect in an even number of edges. Why? 1 2 7 6 5 4 8 3 Cycle C = 1-2, 2-3, 3-4, 4-5, 5-6, 6-1 Cutset D = 3-4, 3-5, 5-6, 5-7, 7-8 Intersection = 3-4, 5-6 Picture: C S V - S Source: slides by Kevin Wayne for Kleinberg-Tardos, Algorithm Design, Pearson Addison-Wesley, 2005.

Toward Greedy MST Algs: Cut Property Simplifying assumption: All edge costs c e distinct Cut property. Let S be any subset of nodes, and let e be the min cost edge with exactly one endpoint in S. Then the MST T* contains e. S f e T* Source: slides by Kevin Wayne for Kleinberg-Tardos, Algorithm Design, Pearson Addison-Wesley, 2005.

Toward Greedy MST Algs: Cut Property Cut property. Let S be any subset of nodes, and let e be the min cost edge with exactly one endpoint in S. Then the MST T* contains e. Proof. (EXCHANGE ARGUMENT) Assume toward a contradiction that e does not belong to T* Adding e to T* creates a cycle C in T* Edge e is both in the cycle C and in the cutset D corresponding to S there exists another edge, say f, that is in both C and D T' = T* { e } - { f } is also a spanning tree Since c e < c f, cost(t') < cost(t*) This contradicts the assumption e must be in T* S f e T* Source: slides by Kevin Wayne for Kleinberg-Tardos, Algorithm Design, Pearson Addison-Wesley, 2005.

Toward Greedy MST Algs: Cycle Property Simplifying assumption: all edge costs c e distinct Cycle property. Let C be any cycle in G, and let f be the max cost edge belonging to C. Then the MST T* does not contain f. S f e T* Source: slides by Kevin Wayne for Kleinberg-Tardos, Algorithm Design, Pearson Addison-Wesley, 2005.

Toward Greedy MST Algs: Cycle Property Simplifying assumption: all edge costs c e distinct Cycle property. Let C be any cycle in G, and let f be the max cost edge belonging to C. Then the MST T* does not contain f. S f e T* Source: slides by Kevin Wayne for Kleinberg-Tardos, Algorithm Design, Pearson Addison-Wesley, 2005.

Toward Greedy MST Algs: Cycle Property Cycle property. Let C be any cycle in G, and let f be the max cost edge belonging to C. Then the MST T* does not contain f. Proof. (EXCHANGE ARGUMENT) Assume toward a contradiction that f belongs to T*. Deleting f from T* creates a cut S in T* Edge f is both in the cycle C and in the cutset D corresponding to S there exists another edge, say e, that is in both C and D T' = T* { e } - { f } is also a spanning tree Since c e < c f, cost(t') < cost(t*) This contradicts the assumption f must not be in T* S f e T* Source: slides by Kevin Wayne for Kleinberg-Tardos, Algorithm Design, Pearson Addison-Wesley, 2005.

Prim s Algorithm and Correctness Prim's algorithm [Jarník 1930, Dijkstra 1957, Prim 1959] Initialize S = any node Apply cut property to S Add min cost edge in cutset corresponding to S to T, and add one new explored node u to S S Source: slides by Kevin Wayne for Kleinberg-Tardos, Algorithm Design, Pearson Addison-Wesley, 2005.

Implementation of Prim s Algorithm Very similar to Dijkstra; O(E log V) with binary heap PQ Figure 5.9 in DPV O(V) O(V) O(E) O(log V)

Kruskal s Algorithm and Correctness Kruskal s algorithm. [Boruvka, 1926; Kruskal, 1956] Consider edges in ascending (i.e., sorted) order of weight Case 1: If adding e to T creates a cycle, discard e according to cycle property Case 2: Otherwise, insert e = (u, v) into T according to cut property where S = set of nodes in u s connected component v e S e u Case 1 Case 2 Source: slides by Kevin Wayne for Kleinberg-Tardos, Algorithm Design, Pearson Addison-Wesley, 2005.

Example: Kruskal s Algorithm

The Union-Find Problem Need a fast way to test if adding e i to T creates a cycle At i th iteration, T is a set of trees Initially, each tree contains one node Adding e i is okay unless u and v are in the same tree Iteration is over edges, shortest first Disjoint set UNION-FIND data structure to support: Make-Set(u): creates a set containing u (for initialization) V times Find-Set(u): Return representative element of set that contains u Union(u, v): Merge the sets containing u and v (and choose new representative) V - 1 times 2 E times Vertices of the graph = elements to be stored in the sets Source: slides from Prof. L. Carter, UCSD 2002

Algorithms for Union-Find Approach #1: Define array A with A[u] = representative of u Find-Set(u): return A[u] O(1) time Union(u,v): Let x = A[u], y = A[v]; change all x s to y s in A ( V ) time Source: slides from Prof. L. Carter, UCSD 2002

Algorithms for Union-Find Approach #1: Define array A with A[u] = representative of u Find-Set(u): return A[u] O(1) time Union(u,v): Let x = A[u], y = A[v]; change all x s to y s in A ( V ) time Approach #2: Also keep a list of members for each set Find-Set(u): return A[u] O(1) time Union(u,v): For each member z of u s list, add it to v s list, set A[z] = y Worst case:??? Key idea: Move elements of smaller list to larger No element moves more than lg V times // remember, lg = log base 2 Even if Union can take O( V ) time, doing V unions takes O( V lg V ) time amortized analysis (see DPV, Figures 5.6, 5.7) Kruskal: O( E lg E ) time to sort edges; O( E ) Find-Set tests; O( V ) T {e} (Union) operations O( E lg V ) overall // Why is (E lg E) the same as (E lg V)? Source: slides from Prof. L. Carter, UCSD 2002

Implementation of Kruskal's Algorithm Use union-find data structure Build set X of edges in the MST Maintain set for each connected component O(V) total O(E log E) = O(E log V) total O(E) total O(V log V) total

EXTRA MATERIAL: DFS in MST == A Heuristic for the Geometric Traveling Salesperson Problem Brings together (1) TSP, (2) DFS, (3) MST, (4) heuristic (for a hard problem) with provable approximation ratio

Euclidean TSP Euclidean Traveling Salesman Problem: Let C 1,C 2,,C n be a set of points in the plane. Find a tour (= cycle) over all n points with minimum total edge cost. Fact: cost(mst) < cost(tour opt ) (1) MST is the minimum-cost graph that connects all vertices, and has only n-1 edges (2) Any TSP tour must also connect all vertices, and will have n edges Note: a tour = a spanning tree (not necessarily a minimum spanning tree) plus another edge

Approximation Algorithm : Euclidean TSP Idea: Consider the tour that consists of a DFS traversal of an MST (starting from any city). If the tour is about to revisit a city, then skip that city (== shortcut to the next city ) Key observation: If the triangle inequality holds, the shortcuts can only reduce the tour cost!

Approximation Algorithm: Euclidean TSP d a b c DSF traversal of MST Taking shortcut from DSF tour. (e.g. replacing a-b-c-b-a-d, by a-b-c-d) Tour Heur 2 * MST 2 * Tour opt

Concept of Approximation Algorithm Approximation algorithm: An algorithm that returns near-optimal solutions (i.e. is "provably good ) is called an approximation algorithm. Performance Ratio: An approximation algorithm for a problem has a performance ratio of (n) if for any instance I of size n, the cost C of the solution produced by the approximation algorithm is within a factor of (n) of the cost C * of an optimal solution: max I = n (C/C * ) (n)