Knapsack Problem Greedy vs Brute Force pp 313
Knapsack Problem: Greedy vs. Brute Force pp 313 -317 (Section 7. 6)
Greedy Approach • To solve problems you have to make decisions. • At each decision point, you pick the greedy (or best) option. – i. e. , make an optimal move given what you know • For some problems a greed strategy – produces an optimal solution – produces a very bad solution
Continuous Knapsack Problem • Continuous Knapsack Variation – Input: N chapters – Goal: Maximize importance – Constraint: Report must be 600 pages Chapter Pages Importance 1 120 5 2 150 5 3 200 4 4 150 8 5 140 3
Continuous Knapsack Problem Chapter Pages Importance 1 120 5 2 150 5 3 200 4 4 150 8 5 140 3 Step 1: Pick the chapter with the most importance (Chapter 4). Maximize the importance by choosing all 150 pages
Continuous Knapsack Problem Chapter Pages Importance 1 120 5 2 150 5 3 200 4 5 140 3 Report (600 pages max): All of Chapter 4 (importance 8) 450 pages left Total Importance = 8 Step 2: Greedy choice… pick the most important remaining chapter: Chapter 1 and 2 are tied, pick both, i. e. , 270 pages
Continuous Knapsack Problem Chapter Pages Importance 3 200 4 5 140 3 Report (600 pages max): All of Chapter 4 (importance 8) All of Chapter 1 (importance 5) All of Chapter 2 (importance 5) 180 pages left Total Importance = 8 + 5 Step 3: Greedy choice… pick Chapter 3. However, we can only pick 90% of it, i. e. , 180 out of 200 pages.
Continuous Knapsack Problem Chapter Pages Importance 3 20 4 5 140 3 Report (600 pages max): All of Chapter 4 (importance 8) All of Chapter 1 (importance 5) All of Chapter 2 (importance 5) 90% of Chapter 3 (importance 4) 0 pages left Total Importance = 8 + 5 + 4*0. 9 = 21. 6 Is the final answer optimal? Can you pick page differently and beat 21. 6?
Constrained Optimization Problems • Many different real-world problem can be reduced to constrained optimization problems. – Some factor needs to be maximized or minimized (Goal) – Real-world constraints limit the choices you can make (Constraints)
Constrained Optimization Problems • Examples: – Package routing • Input: N packages and N delivery points • Goal: Minimize the package delivery time • Constrains: X number of trucks – CPU allocation • Input: N processing jobs each with a priority • Goal: Maximize total priority throughput • Constaints: X processor, Y memory, Z time
Non-Continuous Knapsack Problem • Greedy approach does NOT work. • Just call it the Knapsack Problem – Constraint: Sack holds 100 lbs. – Goal: Maximize sack value – Input: Packages Item Weight Value A 3 lbs $4 B 4 lbs $8 C 9 lbs $11 D 10 lbs $10 E 13 lbs $15 F 15 lbs $10 G 25 lbs $25 H 26 lbs $30 I 50 lbs $51 J 55 lbs $54 K 60 lbs $55
Greedy: Lowest Weight First Item Weight Value Density A 3 $4 1. 33 B 4 $8 2. 00 C 9 $11 1. 22 D 10 $10 1. 00 E 13 $15 1. 15 F 15 $10 0. 67 G 25 $25 1. 00 H 26 $30 1. 15 I 50 $51 1. 02 J 55 $54 0. 98 K 60 $55 0. 92 79 lbs $83
Greedy: Lowest Weight First with one Backtracking step Item Weight Value Density A 3 $4 1. 33 B 4 $8 2. 00 C 9 $11 1. 22 D 10 $10 1. 00 E 13 $15 1. 15 F 15 $10 0. 67 G 25 $25 1. 00 H 26 $30 1. 15 I 50 $51 1. 02 J 55 $54 0. 98 K 60 $55 0. 92 79 lbs $83 -------------21 lbs remaining 26 lbs $30 item avail. removing the lowest valued item 5 lbs. or more. -------------95 lbs $103
Greedy: Highest Value First Item Weight Value Density K 60 $55 0. 92 J 55 $54 0. 98 I 50 $51 1. 02 H 26 $30 1. 15 G 25 $25 1 E 13 $15 1. 15 C 9 $11 1. 22 D 10 $10 1 F 15 $10 0. 67 B 4 $8 2 A 3 $4 1. 33 99 lbs $100
Greedy: Highest Value First with one Restart step Item Weight Value Density K 60 $55 0. 92 J 55 $54 0. 98 I 50 $51 1. 02 H 26 $30 1. 15 G 25 $25 1 E 13 $15 1. 15 C 9 $11 1. 22 D 10 $10 1 F 15 $10 0. 67 B 4 $8 2 A 3 $4 1. 33 99 lbs $100 ----------Skip the most valuable item and start again ----------98 lbs $107
Greedy: Highest Density Item Weight Value Density B 4 $8 2 A 3 $4 1. 33 C 9 $11 1. 22 H 26 $30 1. 15 E 13 $15 1. 15 I 50 $51 1. 02 G 25 $25 1 D 10 $10 1 J 55 $54 0. 98 K 60 $55 0. 92 F 15 $10 0. 67 90 lbs $103
Greedy: Highest Density with one Backtracking step Item Weight Value Density B 4 $8 2 A 3 $4 1. 33 C 9 $11 1. 22 H 26 $30 1. 15 E 13 $15 1. 15 I 50 $51 1. 02 G 25 $25 1 D 10 $10 1 J 55 $54 0. 98 K 60 $55 0. 92 F 15 $10 0. 67 90 lbs $103 ---------10 lbs remaining 15 lbs $10 item avail. removing the lowest valued item 5 lbs. or more. -------------96 lbs $102
- Slides: 16