Minimum Spanning Trees Shortest Paths

Similar documents
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.

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

CIS 121 Data Structures and Algorithms Minimum Spanning Trees

COMP 355 Advanced Algorithms

Algorithms for Minimum Spanning Trees

Design and Analysis of Algorithms

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

tree follows. Game Trees

Design and Analysis of Algorithms

Minimum Spanning Trees

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

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

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

CS200: Graphs. Rosen Ch , 9.6, Walls and Mirrors Ch. 14

6.1 Minimum Spanning Trees

CHAPTER 13 GRAPH ALGORITHMS

COP 4531 Complexity & Analysis of Data Structures & Algorithms

Week 12: Minimum Spanning trees and Shortest Paths

Minimum Spanning Trees

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

Shortest path problems

Algorithm Design and Analysis

4.5 Minimum Spanning Tree. Minimo albero di copertura o ricoprimento

Minimum Spanning Trees Ch 23 Traversing graphs

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

Lecture 10: Analysis of Algorithms (CS ) 1

Definition: A graph G = (V, E) is called a tree if G is connected and acyclic. The following theorem captures many important facts about trees.

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

Theory of Computing. Lecture 10 MAS 714 Hartmut Klauck

5.4 Shortest Paths. Jacobs University Visualization and Computer Graphics Lab. CH : Algorithms and Data Structures 456

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

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

Minimum-Cost Spanning Tree. Example

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

Analysis of Algorithms, I

Graphs. Part II: SP and MST. Laura Toma Algorithms (csci2200), Bowdoin College

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

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

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

Minimum Spanning Trees

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

Announcements Problem Set 5 is out (today)!

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

Lecture 6 Basic Graph Algorithms

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

CSE 521: Design and Analysis of Algorithms I

Tutorial. Question There are no forward edges. 4. For each back edge u, v, we have 0 d[v] d[u].

DO NOT RE-DISTRIBUTE THIS SOLUTION FILE

looking ahead to see the optimum

Problem Set 2 Solutions

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

Week 11: Minimum Spanning trees

CSC 1700 Analysis of Algorithms: Minimum Spanning Tree

Data Structures and Algorithms. Werner Nutt

Dist(Vertex u, Vertex v, Graph preprocessed) return u.dist v.dist

Minimum Spanning Trees My T. UF

CS 6783 (Applied Algorithms) Lecture 5

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

CS 310 Advanced Data Structures and Algorithms

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

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

CSE 421 Greedy Alg: Union Find/Dijkstra s Alg

Algorithms after Dijkstra and Kruskal for Big Data. User Manual

SPANNING TREES. Lecture 21 CS2110 Spring 2016

Introduction to Algorithms. Lecture 11

CSE331 Introduction to Algorithms Lecture 15 Minimum Spanning Trees

Minimum Spanning Trees

Minimum Spanning Trees

1 Variations of the Traveling Salesman Problem

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

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

CSE332: Data Abstractions Lecture 25: Minimum Spanning Trees. Ruth Anderson via Conrad Nied Winter 2015

Priority queue ADT part 1

CS61B, Fall 2002 Discussion #15 Amir Kamil UC Berkeley 12/5/02

23.2 Minimum Spanning Trees

1 The Shortest Path Problem

Single Source Shortest Path

COT 6405 Introduction to Theory of Algorithms

Algorithm Analysis Graph algorithm. Chung-Ang University, Jaesung Lee

Theory of Computing. Lecture 7 MAS 714 Hartmut Klauck

CSC 373: Algorithm Design and Analysis Lecture 4

Introduction to Optimization

Lecture 20: Other Algorithms on Graphs. Undirected Trees. Spanning Trees

Three Graph Algorithms

Three Graph Algorithms

CS521 \ Notes for the Final Exam

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

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

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

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

Exam 3 Practice Problems

In this chapter, we consider some of the interesting problems for which the greedy technique works, but we start with few simple examples.

Design and Analysis of Algorithms

Minimum Spanning Trees

Parallel Graph Algorithms

Design and Analysis of Algorithms

CS 161: Design and Analysis of Algorithms

Graphs Weighted Graphs. Reading: 12.5, 12.6, 12.7

Transcription:

Minimum Spanning Trees Shortest Paths

Minimum Spanning Tree Given a set of locations, with positive distances to each other, we want to create a network that connects all nodes to each other with minimal sum of distances. 4 4 4 6 3 8 9 6 9 6 8 0 5 4 8 5 G = (V, E) S eît c e = 50 Then that graph is a tree. WHY?

Applications MST is fundamental problem with diverse applications. Network design. telephone, electrical, hydraulic, TV cable, computer, road Approximation algorithms for NP-complete problems. traveling salesperson problem Cluster analysis. Minimal vs minimum. Minimum (noun) is always the smallest possible or allowable amount. Minimal (adjective) implies that the amount is (relatively) small. But English allows the use of nouns as adjectives, and also nouns as verbs. (I Googled it J ). "There was minimal residue in the wine bottle." (compared with other wines, this one didn't throw much sediment). "You must leave a minimum sum of $50 in the account." (whether you think $50 is a small amount of money or not). 3

Greedy Algorithms Kruskal's algorithm. Start with T = f. Consider edges in ascending order of cost. Add edge e to 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 node s and greedily grow a tree T from s. At each step, add the cheapest edge e to T that has exactly one endpoint in T, ie without creating a cycle. 4

The cut property Simplifying assumption. All edge costs are distinct. Cut property. Let S be a subset of nodes, S neither empty nor equal V, and let e be the minimum cost edge with exactly one endpoint in S. Then the MST contains e. The cut property establishes the correctness of Prim s algorithm. S e e is in the MST 5

The cut property Cut property. Let S be a 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) If e =(v,w) is the only edge connecting S and V-S it must be in T, else e is on a cycle in the graph (not the MST). Now suppose e does not belong to T. Let e = (v',w') be the first edge between S and V-S on the path from v'. T' = T È { e } - { e } is also a spanning tree. Since c e < c e, cost(t') < cost(t). This is a contradiction. v w e S v e w 6

Prim's Algorithm Prim's algorithm. [Jarník 930, Prim 95, Dijkstra 959] Initialize S = any node. Apply cut property to S: add min cost edge (v, w) where v is in S and w is in V-S, and add w to S. Repeat until S = V. S

Prim s algorithm: Implementation Maintain set of explored nodes S. For each unexplored node v, maintain attachment cost a[v] = cost of cheapest edge v to a node in S. Prim(G,s) foreach (v Î V) a[v] a[s]= 0 priority queue Q = {} foreach (v Î V) insert v onto Q (key: a value) set S {} while (Q is not empty) { u delete min element from Q S S È { u } foreach (edge e = (u, v) incident to u) if ((v Ï S) and (c e < a[v])) decrease priority a[v] to c e 8

c a s b b s c a s b c a s b c a 0 s b c a PQ: s:0 a: b: c: PQ: b: c: a: PQ: c: a: PQ: a: PQ:

Kruskal produces an MST Kruskal's algorithm. [Kruskal, 956] v Consider edges in ascending order of weight. Add edge unless doing so would create a cycle. v Kruskal keeps adding edges until all nodes are connected, and does not create cycles, so produces a spanning tree.

Kruskal produces an MST v Consider e=(v, w) added by Kruskal. S is the set of nodes connected to v just before e is added; v is in S and w is not (otherwise we created a cycle). Therefore e is the cheapest edge connecting S to a node in V-S, and hence, e is in any MST (cut property). w e S v

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. The result is a spanning tree. Is it optimal? When is it safe to remove an edge?

Safely removing edges Cycle property. Let C be any cycle in G, and let e be the max cost edge belonging to C. Then e doesn t belong to any MST of G. e w v v e' w S Solid edges: T T'=T - {e} + {e } 3

Safely removing edges Cycle property. Let C be any cycle in G, and let e be the max cost edge belonging to C. Then e doesn t belong to any MST of G. Let T be a spanning tree that contains e=(v,w). Remove e; this will disconnect T, creating S containing v, and V-S containing w. C {e} is a path P. Following P from v will at some stage cross S into V-S by edge e'. T - {e} + {e } is again a spanning tree and its cost is lower than T, so T is not an MST. v e v w e' w S 4

Shortest Paths Problems Given a weighted directed graph G=(V,E) find the shortest path path length is the sum of its edge weights. The shortest path from u to v is if there is no path from u to v. Variations: ) SSSP (Single source SP): find the SP from some node s to all nodes in the graph. ) SPSP (single pair SP): find the SP from some u to some v. We can use ) to solve ), also there is no asymptotically faster algorithm for ) than that for ). 3) SDSP (single destination SP) can use ) by reversing its edges. 4) APSP (all pair SPs) could be solved by V applications of ), but can be solved faster (cs40).

Dijkstra SSSP Dijkstra's (Greedy) SSSP algorithm only works for graphs with only positive edge weights. S is the set of explored nodes For each u in S, d[u] is a distance Init: S = {s} the source, and d[s]=0 while S V: select a node v in V-S with at least one edge from S, for which d'[v]=min e=(u,v),u in S d[u]+w e add v to S (S=S+v) d[v]=d'[v] the minimum path extending out of S To compute the actual minimum paths, maintain an array p[v] of predecessors. Notice: Dijkstra is very similar to Primm's MST algorithm

0 s b c a 5 b 0 s c a 5 0 s b 3 c a 5 0 s b 3 c a 5 4

Dijkstra works For each u in S, the path P s,u is the shortest (s,u) path Proof by induction on the size of S Base: S = d[s]=0 OK Step: Suppose it holds for S =k>=, then grow S by adding node v using edge (u,v) (u already in S) to create the next S. Then path P s,u,v is path P s,u +(u,v), and is the shortest path to v WHY? What are the "ingredients" of an exchange argument? What are the inequalities?

Greedy exchange argument Assume there is another path P from s to v. P leaves S somewhere with edge (x,y). Then the path P goes from s to x to y to v. What can you say about P: s à* x à y compared to P s,u,v? How does the algorithm pick P s,u,v? Why does it not work for negative edges? y P from s to y is at least as long as P s,u,v because the algorithm picks the shortest extension out of S. s x Hence the path P: s à* x à y à* v is at least as long as P s,u,v : s à* u à v Would not work if w(y,v) <0 set S u next S v