1 chapter 5-2 greedy algorithms slides by kevin wayne. copyright © 2005 pearson-addison wesley. all...

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

Upload: mabel-henderson

Post on 08-Jan-2018

219 views

Category:

Documents


0 download

DESCRIPTION

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

TRANSCRIPT

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

1

Chapter 5-2

GreedyAlgorithms

Slides by Kevin Wayne.Copyright © 2005 Pearson-Addison Wesley.All rights reserved.

Page 2: 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

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

3

Shortest Path Problem

Shortest path network. Directed graph G = (V, E). Source s, destination t. Length e = length of edge e.

Shortest path problem: find shortest directed path from s to t.

Cost of path s-2-3-5-t = 9 + 23 + 2 + 16 = 48.

s

3

t

2

6

7

45

23

18 2

9

14

15 5

30

20

44

16

11

6

19

6

cost of path = sum of edge costs in path

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

4

Dijkstra's Algorithm

Dijkstra's algorithm. Maintain a set of explored nodes S for which we have

determined the shortest path distance d(u) from s to u. Initialize S = { s }, d(s) = 0. Repeatedly choose unexplored node v which minimizes

add v to S, and set d(v) = (v).

,)(min)(:),( eSuvue

udv

s

v

ud(u)

S

e

shortest path to some u in explored part, followed by a single edge (u, v)

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

5

Dijkstra's Algorithm

Dijkstra's algorithm. Maintain a set of explored nodes S for which we have

determined the shortest path distance d(u) from s to u. Initialize S = { s }, d(s) = 0. Repeatedly choose unexplored node v which minimizes

add v to S, and set d(v) = (v).

,)(min)(:),( eSuvue

udv

s

v

ud(u)

shortest path to some u in explored part, followed by a single edge (u, v)

S

e

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

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.

Let v be next node added to S, and let u-v be the chosen edge. The shortest s-u path plus (u, v) is an s-v path of length (v). Consider any s-v path P. We'll see that it's no shorter than (v). Let x-y be the first edge in P that leaves S,

and let P' be the subpath to x. P is already too long as soon as it leaves S.

(P) (P') + (x,y) d(x) + (x, y) (y) (v)

nonnegativeweights

inductivehypothesis

defn of (y) Dijkstra chose vinstead of y

Ss

y

v

x

P

u

P'

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

7

Dijkstra's Algorithm: Implementation

For each unexplored node, explicitly maintain

Next node to explore = node with minimum (v). 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 OperationInsert

ExtractMinChangeKey

Binary heaplog nlog nlog n

Fib heap †

1log n

1

Arraynn1

IsEmpty 1 11

Priority Queue

Total m log n m + n log nn2

Dijkstrannmn

d-way Heapd log d nd log d nlog d n

1m log m/n n

(v) mine (u,v) : u S

d (u) e .

(w) min { (w), (v)e }.

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

Extra Slides

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

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)

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

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.

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

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: c1 < c2 < … < cn.

S while (x 0) { let k be largest integer such that ck x if (k = 0) return "no solution found" x x - ck

S S {k}}return S

coins selected

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

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)

Consider optimal way to change ck x < ck+1 : greedy takes coin k.

We claim that any optimal solution must also take coin k.– if not, it needs enough coins of type c1, …, ck-1 to add up to x– table below indicates no optimal solution can do this

Problem reduces to coin-changing x - ck cents, which, by induction, is optimally solved by greedy algorithm. ▪

1

ck

1025

100

P 4

All optimal solutionsmust satisfy

N + D 2Q 3

5 N 1

no limit

k

1

345

2-

Max value of coins1, 2, …, k-1 in any OPT

4 + 5 = 920 + 4 = 24

4

75 + 24 = 99

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

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, 1500.

Counterexample. 140¢. Greedy: 100, 34, 1, 1, 1, 1, 1, 1. Optimal: 70, 70.

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

Selecting Breakpoints

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

15

Selecting Breakpoints

Selecting breakpoints. Road trip from Princeton to Palo Alto along fixed route. Refueling stations at certain points along the way. Fuel capacity = C. 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

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

16

Truck driver's algorithm.

Implementation. O(n log n) Use binary search to select each breakpoint p.

Selecting Breakpoints: Greedy Algorithm

Sort breakpoints so that: 0 = b0 < b1 < b2 < ... < bn = L

S {0} x 0

while (x bn) let p be largest integer such that bp x + C if (bp = x) return "no solution" x bp

S S {p}return S

breakpoints selectedcurrent location

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

17

Selecting Breakpoints: Correctness

Theorem. Greedy algorithm is optimal.

Pf. (by contradiction) Assume greedy is not optimal, and let's see what happens. Let 0 = g0 < g1 < . . . < gp = L denote set of breakpoints chosen by

greedy. Let 0 = f0 < f1 < . . . < fq = L denote set of breakpoints in an optimal

solution with f0 = g0, f1= g1 , . . . , fr = gr for largest possible value of r.

Note: gr+1 > fr+1 by greedy choice of algorithm.

. . .

Greedy:

OPT:

g0 g1 g2

f0 f1 f2 fq

gr

frwhy doesn't optimal solution drive a little further?

gr+1

fr+1

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

18

Selecting Breakpoints: Correctness

Theorem. Greedy algorithm is optimal.

Pf. (by contradiction) Assume greedy is not optimal, and let's see what happens. Let 0 = g0 < g1 < . . . < gp = L denote set of breakpoints chosen by

greedy. Let 0 = f0 < f1 < . . . < fq = L denote set of breakpoints in an optimal

solution with f0 = g0, f1= g1 , . . . , fr = gr for largest possible value of r.

Note: gr+1 > fr+1 by greedy choice of algorithm.

another optimal solution hasone more breakpoint in common contradiction

. . .

Greedy:

OPT:

g0 g1 g2

f0 f1 f2 fq

gr

fr

gr+1

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

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.

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

HOMEWORK (Chapter 5)

2-b5712