Download presentation
Presentation is loading. Please wait.
Published byRobyn Hines Modified over 9 years ago
1
Greedy Algorithms 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 25 March 2004
2
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
3
Greed is Good
4
4 Example 1: Counting change Suppose we want to give out change, using the minimal number of bills and coins.
5
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?
6
6 Our simple algorithm For US currency, this simple algorithm actually works. Why do we call this a greedy algorithm?
7
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).
8
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?
9
9 But… What happens if we have a 12-cent coin?
10
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?
11
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?
12
12 Hill-climbing, cont’d Making the locally-best guess is efficient and easy, but doesn’t always work.
13
13 Example 2: Fractional Knapsack Problem (FKP) You rob a store: find n kinds of items Gold dust. Wheat. Beer.
15
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: w i pounds Value: v i dollars Knapsack can hold a maximum of W pounds. Q: how much of each kind of item should you take? (Can take fractional weight)
16
16 FKP: solution Greedy solution 1: Get a bigger knapsack! Build up extra muscles if necessary.
17
17 FKP: solution Greedy solution 1: Get a bigger knapsack! Build up extra muscles if necessary. But seriously folks…
18
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 = v i /w i (dollars per pound) Then next “most valuable” item, etc. Until knapsack is full.
19
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
20
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.
21
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.
22
22 FKP: Greedy-choice proof: step 1 We want to show that the optimal solution always contains 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.
23
23 For item i let w i be the total inventory, v i be the total value, k i be the weight in knapsack. Let item h be the item with highest $/lb. If k h 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… Assume total optimal value :
24
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.
25
25 More rigorously… Case I: w h W Fill knapsack completely with h. No other item gives higher total value. Case II: w h < 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.
26
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 w i - w of item i.
27
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 w i - 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!)
28
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.
29
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 v 1, v 2, …, v n, and sizes s 1, s 2, …, s n. How do you (efficiently) maximize the value of the items in your cart?
31
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 s i = C and value v i. It can happen that there are items j and k with combined size s j +s k C but v j +v k > v i.
32
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)
33
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.
34
Data Compression
35
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.
36
36 Example
37
37 Example
38
38 Example
39
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.
40
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.
41
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
42
Minimum Spanning Trees
43
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.
44
44 eabcd 0 Prim’s algorithm a c e d b 2 44 9 6 4 5 3 bdca 345 VertexParent e- be ce de The MST initially consists of the vertex e, and we update the distances and parent for its adjacent vertices VertexParent e- b- c- d-
45
45 Prim’s algorithm a c e d b 2 44 9 6 4 5 3 dca 459 VertexParent e- be c e de a b bdca 345 VertexParent e- be ce de
46
46 VertexParent e- be c e de a b Prim’s algorithm a c e d b 2 44 9 6 4 5 3 ac 24 VertexParent e- be c d de a d dca 459
47
47 Another Approach – Kruskal’s a c e d b 2 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.
48
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.
49
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 c e d b 2 44 9 6 4 5 3
50
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 c e d b 2 44 9 6 4 5 3
51
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 c e d b 2 44 9 6 4 5 3
52
Weighted Single-Source Shortest Path Algorithm (Dijkstra’s Algorithm)
53
53 Dijkstra’s Single-Source Shortest Path s f a b d e c g 4 2 5 1 1 1 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.
54
54 Dijkstra’s algorithm sabcdefg 0 Visited s f a b d e c g 4 2 5 1 1 1 4 2 2
55
55 Dijkstra’s algorithm Visited s (D = 0) bcadefg 245 s f a b d e c g 4 2 5 1 1 1 4 2 2
56
56 Dijkstra’s algorithm Visited s (D = 0) b (D = 2) s f a b d e c g 4 2 5 1 1 1 4 2 2 dcaefg 3456
57
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.