57
Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

Embed Size (px)

Citation preview

Page 1: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

Greedy Algorithms

15-211 Fundamental Data Structures and Algorithms

Margaret Reid-Miller

25 March 2004

Page 2: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

2

Announcements

• HW6 Parts 1 & 2 due Today, 11:59pm! All of Hw6 due Monday, April 5, 11:59

• Quiz #2 To be completed within one hour on a

web browser• actually, only a 15-minute quiz

Available Wednesday, 1:30pm Must be completed by Thursday,

April 1, 11:59pm

Page 3: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

Greed is Good

Page 4: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

4

Example 1: Counting change

• Suppose we want to give out change, using the minimal number of bills and coins.

Page 5: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

5

A change-counting algorithm

• An easy algorithm for giving out N cents in change:

Choose the largest bill or coin that is N. Subtract the value of the chosen bill/coin from N,

to get a new value of N. Repeat until a total of N cents has been counted.

• Does this work? I.e., does this really give out the minimal number of coins and bills?

Page 6: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

6

Our simple algorithm

• For US currency, this simple algorithm actually works.

• Why do we call this a greedy algorithm?

Page 7: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

7

Greedy algorithms

• At every step, a greedy algorithm

makes a locally optimal decision,

with the idea that in the end it all adds up to a globally optimal solution.

• Being optimistic like this usually leads to very simple algorithms (i.e., easy to code).

Page 8: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

8

Change counting is greedy

• Makes a locally optimal decision.

Uses the next-largest bill or coin. But once a coin is accepted, it is permanently included in the solution. Once a coin is rejected, it is permanently excluded from the solution.

• To reach a globally optimal solution.

Can you prove it for US currency?

Page 9: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

9

But…

• What happens if we have a 12-cent coin?

Page 10: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

10

Hill-climbing

• Greedy algorithms are often visualized as “hill-climbing”. Suppose you want to reach the summit,

but can only see 10 yards ahead and behind (due to thick fog).

Which way?

Page 11: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

11

Hill-climbing

• Greedy algorithms are often visualized as “hill-climbing”. Suppose you want to reach the summit,

but can only see 10 yards ahead and behind (due to thick fog).

Which way?

Page 12: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

12

Hill-climbing, cont’d

• Making the locally-best guess is efficient and easy, but doesn’t always work.

Page 13: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

13

Example 2: Fractional Knapsack Problem (FKP)

• You rob a store: find n kinds of items Gold dust. Wheat. Beer.

Page 14: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004
Page 15: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

15

Example 2: Fractional knapsack problem (FKP)

• You rob a store: find n kinds of items Gold dust. Wheat. Beer.

• The total inventory for the i th kind of item:

Weight: wi pounds Value: vi dollars

• Knapsack can hold a maximum of W pounds.

• Q: how much of each kind of item should you take?

(Can take fractional weight)

Page 16: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

16

FKP: solution

• Greedy solution 1: Get a bigger knapsack! Build up extra muscles if necessary.

Page 17: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

17

FKP: solution

• Greedy solution 1: Get a bigger knapsack! Build up extra muscles if necessary.

But seriously folks…

Page 18: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

18

FKP: solution

• Greedy solution 1: Get a bigger knapsack! Build up extra muscles if necessary.

But seriously folks…

• Greedy solution 2: Fill knapsack with “most valuable” item

until all is taken. •Most valuable = vi /wi (dollars per pound)

Then next “most valuable” item, etc. Until knapsack is full.

Page 19: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

19

Ingredients of a greedy alg.

1. Optimization problem Of the many feasible solutions, finds the

minimum or maximum solution.

2. Can only proceed in stages no direct solution available

3. Greedy-choice property:A locally optimal (greedy) choice will lead to a globally optimal solution.

4. Optimal substructure:An optimal solution contains within it optimal solutions to subproblems.

Show the proof

Page 20: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

20

FKP is greedy

• An optimization problem: Maximize value of loot, subject to

maximum weight W. (constrained optimization)

• Proceeds in stages: Knapsack is filled with one item at a

time.

Page 21: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

21

FKP is greedy

• Greedy-choice property: A locally greedy choice will lead to a globally optimal solution.

• Proof:Step 1: Prove that an optimal solution contains the greedy choice.

Step 2: Prove that the greedy choice can always be made first.

Page 22: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

22

FKP: Greedy-choice proof: step 1

We want to show that the optimal solution alwayscontains the greedy choice.

• Consider total value, V, of knapsack.

• Knapsack must contain item h: Item h is the item with highest $/lb.

• Why? Because if h is not included, we can replace some other item in knapsack with an equivalent weight of h, and increase V.

• This can continue until knapsack is full, or all of h is taken.

Therefore any optimal solution must include greedy-choice.

Page 23: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

23

For item i let wi be the total inventory, vi be the total value, ki be the weight in knapsack.

Let item h be the item with highest $/lb. If kh<wh, and kj>0 for some jh, then replace j with an equal weight of h. Let new total value = V’.

Difference in total value:

since, by definition of h,

Therefore all of item h should be taken.

More rigorously…

∑=

⎟⎟⎠

⎞⎜⎜⎝

⎛=

n

i i

ii w

vkV

1

0≥⎟⎟⎠

⎞⎜⎜⎝

⎛−⎟⎟

⎞⎜⎜⎝

⎛=−′

j

jj

h

hj w

vk

wv

kVV

h

h

j

j

w

v

w

v≤

Assume total optimal value:

Page 24: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

24

FKP: Greedy-choice proof: step 2

Next, we want to show that we can always make the greedy choice first.

• If item h is more than what knapsack can hold, then fill knapsack completely with h. No other item gives higher total value.

• Otherwise, knapsack contains all of h and some other item. We can always make h the first choice, without changing total value V.

In either case the greedy-choice can always be made FIRST.

Page 25: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

25

More rigorously…

• Case I: wh W Fill knapsack completely with h. No other item gives higher total value.

• Case II: wh < W Let 1st choice be item i, and kth choice be h,

then we can always swap our 1st and kth choices, and total value V remains unchanged.

• Therefore greedy-choice can always be made first.

Page 26: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

26

FKP: Optimal substructure

• The optimal substructure property:An optimal solution contains within it optimal solutions to subproblems.

• If we remove weight w of one item i from the optimal load, then the remaining load must be optimal solution using the remaining items.

• The subproblem is the most valuable load of maximum weight W-w from n-1 items and wi - w of item i.

Page 27: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

27

FKP: Optimal substructure proof

• We want to show that an optimal solution contains within it optimal solutions to subproblems.

• Consider the most valuable load L, weighing W lbs.

• Remove w pounds of some item i .

• Remaining load L’ must be the most valuable load for a smaller fractional knapsack problem:

Maximum weight is W – w lbs.

Only n-1 items and wi - w lbs. of item i.

• Why? Because otherwise we can find a load, L’’, more valuable than L’, add w pounds of item i, and this will be more valuable than L. (Contradiction!)

Page 28: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

28

Example 3: Binary knapsack problem (BKP)

• Variation on FKP. The “Supermarket Shopping Spree”!

• Suppose, instead, that you can only take an item wholly, or not at all (no fractions allowed). Diamond rings. Laptops. Watches.

• Q: How many of each item to take?

• Will the greedy approach still work?

Surprisingly, no.

Page 29: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

29

The Binary Knapsack Problem

• You win the Supermarket Shopping Spree contest. You are given a shopping cart with

capacity C. You are allowed to fill it with any

items you want from Giant Eagle. Giant Eagle has items 1, 2, … n,

which have values v1, v2, …, vn, and sizes s1, s2, …, sn.

How do you (efficiently) maximize the value of the items in your cart?

Page 30: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

                

Page 31: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

31

BKP is not greedy

• The obvious greedy strategy of taking the maximum value item that still fits in the cart does not work.

• Consider: Suppose item i has size si = C and value

vi. It can happen that there are items j and

k with combined size sj+sk C but vj+vk > vi.

Page 32: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

32

BKP: Greedy approach fails

item 1 item 2 item 3 knapsack

$60, 10 lbs

$100, 20 lbs

$120, 30 lbs

Maximum weight = 50 lbs

Dollars/pound

Item 1 $6

Item 2 $5

Item 3 $4

BKP has optimal substructure, but not greedy-choice property: optimal solution does not contain greedy choice.

$160 $180 $220

(optimal)

Page 33: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

33

Succeeding with greed.

4 ingredients needed:1. Optimization problem

2. Can only proceed in stages

3. Greedy-choice property:A greedy choice will lead to a globally optimal solution.

4. Optimal substructure:An optimal solution contains within it optimal solutions to subproblems.

Page 34: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

Data Compression

Page 35: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

35

Example 4: Huffman coding

• Compresses data using prefix codes, where no codeword is a prefix of some other code word.

• Huffman codes are optimal prefix codes.

• Huffman algorithm is greedy.

Page 36: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

36

Example

Page 37: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

37

Example

Page 38: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

38

Example

Page 39: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

39

Huffman coding is greedy

• Optimization problem: Find a prefix code to compress a file to a minimum size.

• Proceeds in stages: For each iteration “merge” two trees with the minimum

frequency.

• A greedy choice will lead to a globally optimal solution: There exists an optimal prefix code in which the two

characters with minimum frequency had codewords that differ only in the last bit.

• An optimal solution contains optimal solutions to subproblems: Replace the two characters with minimum frequency

with a new character with frequency that is the sum of the frequencies of the replaced characters.

Page 40: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

40

Huffman: Greedy-choice property

• Suppose tree T represents an optimal prefix code.

• Modify T so that the two characters with lowest frequencies, x and y, are sibling leaves of maximum depth.

• Swap x and y with leaves that are of maximum depth. Each swap exchanges a lower frequency letter with a higher frequency letter that has a longer code. Thus, each swap does not increase the cost of compression.

Page 41: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

41

Huffman: optimal substructure

• If we replace any two characters of sibling leaves in T with a character with same total frequency, then the new tree T’ is an optimal prefix code with the new characters.

B(T) = 45*1+12*3+13*3+5*4+9*4+16*3

z:14

B(T’) = 45*1+12*3+13*3+(5+9)*3+16*3= B(T) - 5 - 9

Page 42: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

Minimum Spanning Trees

Page 43: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

43

Prim’s algorithm

• Pick a vertex now we have a minimum spanning tree

of the chosen vertex

• Grow the tree until all vertices are included Each step, add the edge (u,v) s.t. the

cost of (u,v) is minimum among all edges where u is in the tree and v is not in the tree

• Prim’s algorithm is greedy.

Page 44: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

44

e a b c d

0

Prim’s algorithm

a

ce

d

b2

44

9

6

4

5

3

b d c a

3 4 5

Vertex Parente -b ec ed e

The MST initially consists of the vertex e, and we updatethe distances and parent for its adjacent vertices

Vertex Parente -b -c -d -

Page 45: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

45

Prim’s algorithm

a

ce

d

b2

44

9

6

4

5

3

d c a

4 5 9

Vertex Parente -b ec ed ea b

b d c a

3 4 5

Vertex Parente -b ec ed e

Page 46: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

46

Vertex Parente -b ec ed ea b

Prim’s algorithm

a

ce

d

b2

44

9

6

4

5

3

a c

2 4

Vertex Parente -b ec dd ea d

d c a

4 5 9

Page 47: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

47

Another Approach – Kruskal’s

a

ce

d

b2

44

9

6

4

5

3

• Create a forest of trees from the vertices.• Repeatedly merge trees by adding “safe edges”

until only one tree remains.• A “safe edge” is an edge of minimum weight which

does not create a cycle.

forest: {a}, {b}, {c}, {d}, {e}

Kruskal’s algorithm is greedy.

Page 48: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

48

Kruskal’s algorithm

For each edge (u,v) E in increasing order while more than one set remains:

If u and v, belong to different sets a. A = A {(u,v)} b. merge the sets containing u and v

Return A

• Use Union-Find algorithm to efficiently determine if u and v belong to different sets.

Page 49: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

49

Kruskal’s algorithm

E = {(a,d), (b,e), (c,d), (a,c), (d,e), (c,e), (b,d), (a,b)}

Forest{a}, {b}, {c}, {d}, {e}{a,d}, {b}, {c}, {e}

T{(a,d)}

a

ce

d

b2

44

9

6

4

5

3

Page 50: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

50

Kruskal’s algorithm

E = {(a,d), (b,e), (c,d), (a,c), (d,e), (c,e), (b,d), (a,b)}

Forest{a}, {b}, {c}, {d}, {e}{a,d}, {b}, {c}, {e}{a,d}, {b,e}, {c}

T{(a,d)}{(a,d), (b,e)}

a

ce

d

b2

44

9

6

4

5

3

Page 51: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

51

Kruskal’s algorithm

E = {(a,d), (b,e), (c,d), (a,c), (d,e), (c,e), (b,d), (a,b)}

Forest{a}, {b}, {c}, {d}, {e}{a,d}, {b}, {c}, {e}{a,d}, {b,e}, {c}{a,d,c,}, {b,e}

T{(a,d)}{(a,d), (b,e)}{(a,d), (b,e), (c,d)}

a

ce

d

b2

44

9

6

4

5

3

Page 52: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

Weighted Single-SourceShortest Path Algorithm

(Dijkstra’s Algorithm)

Page 53: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

53

Dijkstra’s Single-Source Shortest Path

s

f

a

b

d

e

c

g

4

2

5

1

11

4

2

2

Given: a directed graph G = (V,E)weight(u,v) 0 for all edges (u,v) E

Find: shortest path from start vertex s to every other vertex in G

Dijkstra’s algorithm is greedy.

Page 54: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

54

Dijkstra’s algorithm

s a b c d e f g

0

Visited

s

f

a

b

d

e

c

g

4

2

5

1

11

4

2

2

Page 55: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

55

Dijkstra’s algorithm

Visiteds (D = 0)

b c a d e f g

2 4 5

s

f

a

b

d

e

c

g

4

2

5

1

11

4

2

2

Page 56: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

56

Dijkstra’s algorithm

Visiteds (D = 0)b (D = 2)s

f

a

b

d

e

c

g

4

2

5

1

11

4

2

2

d c a e f g

3 4 5 6

Page 57: Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004

57

Greedy Algorithms

• Many optimization problems can be solved using a greedy approach. The basic principle is that local optimal

decisions will lead to a global optimal solution.

But the greedy approach may not always lead to an optimal solution for all problems.

The key is knowing which problems will work with this approach and which will not.

• Next time we will look at problems for which it will not work.