1 Chapter 5-2 Greedy Algorithms Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.

Slides:



Advertisements
Similar presentations
COMP 482: Design and Analysis of Algorithms
Advertisements

Single Source Shortest Paths
1 Chapter 4 Greedy Algorithms Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
Greed "Greed is good. Greed is right. Greed works. Greed clarifies, cuts through, and captures the essence of the evolutionary spirit." Gordon Gecko (Michael.
The Greedy Approach Chapter 8. The Greedy Approach It’s a design technique for solving optimization problems Based on finding optimal local solutions.
October 31, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
November 14, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Aalborg University
Data Structures and Algorithms Graphs Single-Source Shortest Paths (Dijkstra’s Algorithm) PLSD210(ii)
Introduction to Algorithms 6.046J/18.401J/SMA5503
CS38 Introduction to Algorithms Lecture 5 April 15, 2014.
State Space Search Algorithms CSE 472 Introduction to Artificial Intelligence Autumn 2003.
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
Shortest Paths Definitions Single Source Algorithms –Bellman Ford –DAG shortest path algorithm –Dijkstra All Pairs Algorithms –Using Single Source Algorithms.
1 Chapter 4 Greedy Algorithms Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
Shortest Paths Definitions Single Source Algorithms
CSE 780 Algorithms Advanced Algorithms SSSP Dijkstra’s algorithm SSSP in DAGs.
Tirgul 13. Unweighted Graphs Wishful Thinking – you decide to go to work on your sun-tan in ‘ Hatzuk ’ beach in Tel-Aviv. Therefore, you take your swimming.
Princeton University COS 423 Theory of Algorithms Spring 2002 Kevin Wayne Greed: Shortest Path s 3 t
1 Shortest Path Algorithms. 2 Routing Algorithms Shortest path routing What is a shortest path? –Minimum number of hops? –Minimum distance? There is a.
Theory of Computing Lecture 7 MAS 714 Hartmut Klauck.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
Advanced Algorithms Piyush Kumar (Lecture 5: Weighted Matching) Welcome to COT5405 Based on Kevin Wayne’s slides.
David Luebke 1 9/10/2015 ITCS 6114 Single-Source Shortest Path.
1 GRAPHS - ADVANCED APPLICATIONS Minimim Spanning Trees Shortest Path Transitive Closure.
9/10/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Adam Smith Algorithm Design and Analysis L ECTURE 8 Greedy Graph.
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
9/10/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Adam Smith Algorithm Design and Analysis L ECTURE 7 Greedy Graph.
Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph. Want to compute a shortest path for each possible.
Introduction to Algorithms Jiafen Liu Sept
1 Chapter 4 Minimum Spanning Trees Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
1 Introduction to Algorithms L ECTURE 17 (Chap. 24) Shortest paths I 24.2 Single-source shortest paths 24.3 Dijkstra’s algorithm Edsger Wybe Dijkstra
Lecture 13 Algorithm Analysis
CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 12.
1 Chapter 5-1 Greedy Algorithms Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
1 Chapter 4 Greedy Algorithms Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
Shortest Path Algorithm 2 Prof. Sin-Min Lee Department of Computer Science.
1 Sections 2.5 and 4.4 Priority Queues and Dijkstra’s Algorithm For Shortest Paths Some Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley.
CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 10.
Single Source Shortest Paths Chapter 24 CSc 4520/6520 Fall 2013 Slides adapted from George Bebis, University of Reno, Nevada.
1 Chapter 7 Network Flow Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
Greedy Algorithms Lecture 10 Asst. Prof. Dr. İlker Kocabaş 1.
TIRGUL 10 Dijkstra’s algorithm Bellman-Ford Algorithm 1.
Chapter 4 Greedy Algorithms
Analysis of Algorithms CS 477/677
Algorithms and Data Structures Lecture XIII
Minimum Spanning Tree Shortest Paths
Chapter 4 Greedy Algorithms
Greedy Algorithms / Interval Scheduling Yin Tat Lee
Chapter 7 Network Flow Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
Chapter 4 Greedy Algorithms
Data Structures and Algorithms CMPSC 465
Greedy Algorithms / Dijkstra’s Algorithm Yin Tat Lee
4.1 Interval Scheduling.
4.4 Shortest Paths in a Graph
Algorithms and Data Structures Lecture XIII
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
CSC 413/513: Intro to Algorithms
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Fundamental Data Structures and Algorithms
Chapter 4 Greedy Algorithms
Spanning Trees Lecture 20 CS2110 – Spring 2015.
CS 3013: DS & Algorithms Shortest Paths.
Data Structures and Algorithm Analysis Lecture 8
More Graphs Lecture 19 CS2110 – Fall 2009.
Presentation transcript:

1 Chapter 5-2 Greedy Algorithms Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.

4.4 Shortest Paths in a Graph shortest path from Princeton CS department to Einstein's house

3 Shortest Path Problem Shortest path network. n Directed graph G = (V, E). n Source s, destination t. n Length e = length of edge e. Shortest path problem: find shortest directed path from s to t. Cost of path s t = = 48. s 3 t cost of path = sum of edge costs in path

4 Dijkstra's Algorithm Dijkstra's algorithm. n Maintain a set of explored nodes S for which we have determined the shortest path distance d(u) from s to u. n Initialize S = { s }, d(s) = 0. n Repeatedly choose unexplored node v which minimizes add v to S, and set d(v) =  (v). s v u d(u) S e shortest path to some u in explored part, followed by a single edge (u, v)

5 Dijkstra's Algorithm Dijkstra's algorithm. n Maintain a set of explored nodes S for which we have determined the shortest path distance d(u) from s to u. n Initialize S = { s }, d(s) = 0. n Repeatedly choose unexplored node v which minimizes add v to S, and set d(v) =  (v). s v u d(u) shortest path to some u in explored part, followed by a single edge (u, v) S e

6 Dijkstra's Algorithm: Proof of Correctness Invariant. For each node u  S, d(u) is the length of the shortest s-u path. Pf. (by induction on |S|) Base case: |S| = 1 is trivial. Inductive hypothesis: Assume true for |S| = k  1. n Let v be next node added to S, and let u-v be the chosen edge. n The shortest s-u path plus (u, v) is an s-v path of length  (v). n Consider any s-v path P. We'll see that it's no shorter than  (v). n Let x-y be the first edge in P that leaves S, and let P' be the subpath to x. n P is already too long as soon as it leaves S. (P)  (P') + (x,y)  d(x) + (x, y)   (y)   (v) nonnegative weights inductive hypothesis defn of  (y) Dijkstra chose v instead of y S s y v x P u P'

7 Dijkstra's Algorithm: Implementation For each unexplored node, explicitly maintain n Next node to explore = node with minimum  (v). n When exploring v, for each incident edge e = (v, w), update Efficient implementation. Maintain a priority queue of unexplored nodes, prioritized by  (v). † Individual ops are amortized bounds PQ Operation Insert ExtractMin ChangeKey Binary heap log n Fib heap † 1 log n 1 Array n n 1 IsEmpty111 Priority Queue Totalm log nm + n log nn2n2 Dijkstra n n m n d-way Heap d log d n log d n 1 m log m/n n

Extra Slides

Coin Changing Greed is good. Greed is right. Greed works. Greed clarifies, cuts through, and captures the essence of the evolutionary spirit. - Gordon Gecko (Michael Douglas)

10 Coin Changing Goal. Given currency denominations: 1, 5, 10, 25, 100, devise a method to pay amount to customer using fewest number of coins. Ex: 34¢. Cashier's algorithm. At each iteration, add coin of the largest value that does not take us past the amount to be paid. Ex: $2.89.

11 Coin-Changing: Greedy Algorithm Cashier's algorithm. At each iteration, add coin of the largest value that does not take us past the amount to be paid. Q. Is cashier's algorithm optimal? Sort coins denominations by value: c 1 < c 2 < … < c n. S   while (x  0) { let k be largest integer such that c k  x if (k = 0) return "no solution found" x  x - c k S  S  {k} } return S coins selected

12 Coin-Changing: Analysis of Greedy Algorithm Theorem. Greed is optimal for U.S. coinage: 1, 5, 10, 25, 100. Pf. (by induction on x) n Consider optimal way to change c k  x < c k+1 : greedy takes coin k. n We claim that any optimal solution must also take coin k. – if not, it needs enough coins of type c 1, …, c k-1 to add up to x – table below indicates no optimal solution can do this n Problem reduces to coin-changing x - c k cents, which, by induction, is optimally solved by greedy algorithm. ▪ 1 ckck P  4 All optimal solutions must satisfy N + D  2 Q  3 5N  1 no limit k Max value of coins 1, 2, …, k-1 in any OPT = = = 99

13 Coin-Changing: Analysis of Greedy Algorithm Observation. Greedy algorithm is sub-optimal for US postal denominations: 1, 10, 21, 34, 70, 100, 350, 1225, Counterexample. 140¢. n Greedy: 100, 34, 1, 1, 1, 1, 1, 1. n Optimal: 70, 70.

Selecting Breakpoints

15 Selecting Breakpoints Selecting breakpoints. n Road trip from Princeton to Palo Alto along fixed route. n Refueling stations at certain points along the way. n Fuel capacity = C. n Goal: makes as few refueling stops as possible. Greedy algorithm. Go as far as you can before refueling. Princeton Palo Alto 1 C C 2 C 3 C 4 C 5 C 6 C 7

16 Truck driver's algorithm. Implementation. O(n log n) n Use binary search to select each breakpoint p. Selecting Breakpoints: Greedy Algorithm Sort breakpoints so that: 0 = b 0 < b 1 < b 2 <... < b n = L S  {0} x  0 while (x  b n ) let p be largest integer such that b p  x + C if (b p = x) return "no solution" x  b p S  S  {p} return S breakpoints selected current location

17 Selecting Breakpoints: Correctness Theorem. Greedy algorithm is optimal. Pf. (by contradiction) n Assume greedy is not optimal, and let's see what happens. n Let 0 = g 0 < g 1 <... < g p = L denote set of breakpoints chosen by greedy. n Let 0 = f 0 < f 1 <... < f q = L denote set of breakpoints in an optimal solution with f 0 = g 0, f 1 = g 1,..., f r = g r for largest possible value of r. n Note: g r+1 > f r+1 by greedy choice of algorithm.... Greedy: OPT: g0g0 g1g1 g2g2 f0f0 f1f1 f2f2 fqfq grgr frfr why doesn't optimal solution drive a little further? g r+1 f r+1

18 Selecting Breakpoints: Correctness Theorem. Greedy algorithm is optimal. Pf. (by contradiction) n Assume greedy is not optimal, and let's see what happens. n Let 0 = g 0 < g 1 <... < g p = L denote set of breakpoints chosen by greedy. n Let 0 = f 0 < f 1 <... < f q = L denote set of breakpoints in an optimal solution with f 0 = g 0, f 1 = g 1,..., f r = g r for largest possible value of r. n Note: g r+1 > f r+1 by greedy choice of algorithm. another optimal solution has one more breakpoint in common  contradiction... Greedy: OPT: g0g0 g1g1 g2g2 f0f0 f1f1 f2f2 fqfq grgr frfr g r+1

19 Edsger W. Dijkstra The question of whether computers can think is like the question of whether submarines can swim. Do only what only you can do. In their capacity as a tool, computers will be but a ripple on the surface of our culture. In their capacity as intellectual challenge, they are without precedent in the cultural history of mankind. The use of COBOL cripples the mind; its teaching should, therefore, be regarded as a criminal offence. APL is a mistake, carried through to perfection. It is the language of the future for the programming techniques of the past: it creates a new generation of coding bums.

HOMEWORK (Chapter 5) 2-b