C xin he university at buffalo cse 431531 algorithm

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: that contains the item1 (the lightest item). c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 24 / 49 0/1 Knapsack Problem For this greedy strategy, we can still show the Optimal Substructure Property holds: if S is an optimal solution, that contains the item1 , for the original input, then S - {item1 } is an optimal solution for the input consisting of item2 , item3 , , itemn and the knapsack with capacity K - w[1]. However, we cannot prove the Greedy Choice Property: We are not able to show there is an optimal solution that contains the item1 (the lightest item). Without this property, there is no guarantee this strategy would work. (As the counter example has shown, it doesn't work.) c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 24 / 49 0/1 Knapsack Problem Greedy Strategy 2 Since the goal is to maximize the profit without exceeding the capacity, we fill the items in the order of decreasing profits. Namely: Sort the items by decreasing item profit: p[1] p[2] . Fill the knapsack in the order item1 , item2 , ... until no more items can be put into the knapsack without exceeding the capacity. c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 25 / 49 0/1 Knapsack Problem Greedy Strategy 2 Since the goal is to maximize the profit without exceeding the capacity, we fill the items in the order of decreasing profits. Namely: Sort the items by decreasing item profit: p[1] p[2] . Fill the knapsack in the order item1 , item2 , ... until no more items can be put into the knapsack without exceeding the capacity. Counter Example: n = 3, p[1] = 3, p[2] = 2, p[3] = 2, w[1] = 3, w[2] = 2, w[3] = 2, K = 4. This strategy puts item1 into the knapsack with total profit 3. The optimal solution: put item2 and item3 into the knapsack with total profit 4. c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 25 / 49 0/1 Knapsack Problem Greedy Strategy 3 Since the goal is to maximize the profit without exceeding the capacity, we fill the items in the order of decreasing unit profit. Namely: Sort the items by decreasing item unit profit: p[1] w[1] p[2] w[2] p[3] w[1] Fill the knapsack in the order item1 , item2 , ... until no more items can be put into the knapsack without exceeding the capacity. c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 26 / 49 0/1 Knapsack Problem Greedy Strategy 3 Since the goal is to maximize the profit without exceeding the capacity, we fill the items in the order of decreasing unit profit. Namely: Sort the items by decreasing item unit profit: p[1] w[1] p[2] w[2] p[3] w[1] Fill the knapsack in the order item1 , item2 , ... until no more items can be put into the knapsack without exceeding the capacity. Counter Example: n = 2, w[1] = 2, w[2] = 4, p[1] = 2, p[2] = 3, K = 4. We have: p[1] w[1] = 2 2 =1 p[2] w[2] = 3. 4 This strategy puts item1 into knapsack with total profit 2. The optimal solution: put item2 into knapsack with total profit 3. c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 26 / 49 Fractional Knapsack Problem Fractional Knapsack Problem Input: n itemi (1 i n). Each itemi has an integer weight w[i] 0 and a profit p[i] 0. A knapsack with an integer capacity K. Find: A subset of items to put into the knapsack. We can select a fraction of an item. The goal is the same: the total weight of the selected items is at most K, and the total profit is maximized. c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 27 / 49 Fractional Knapsack Problem Fractional Knapsack Problem Input: n itemi (1 i n). Each itemi has an integer weight w[i] 0 and a profit p[i] 0. A knapsack with an integer capacity K. Find: A subset of items to put into the knapsack. We can select a fraction of an item. The goal is the same: the total weight of the selected items is at most K, and the total profit is maximized. Mathematical description of Fractional Knapsack Problem Input: 2n + 1 integers p[1], p[2], , p[n], w[1], w[2], , w[n], K Find: a vector (x1 , x2 , . . . , xn ) such that: 0 xi 1 for 1 i n n i=1 xi n i=1 xi w[i] K p[i] is maximized. CSE 431/531 Algorithm Analysis and Design 27 / 49 c Xin He (University at Buffalo) Fractional Knapsack Problem Although the Fractional Knapsack Problem looks very similar to the 0/1 Knapsack Problem, it is much much easier. c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 28 / 49 Fractional Knapsack Problem Although the Fractional Knapsack Problem looks very similar to the 0/1 Knapsack Problem, it is much much easier. The Greedy Strategy 3 works. c Xin He (University at Buffalo) CSE 431/531 Algorithm Analysis and Design 28 / 49 Fractional Knapsack Problem Although the Fractional Knapsack Problem looks very similar to the 0/1 Knapsack Problem, it is much much easier. The Greedy Strategy 3 works. Greedy-Fractional-Knapsack 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: p[1] w[1] p[2] w[2] p[3] w[3] Sort the ite...
View Full Document

This note was uploaded on 02/27/2012 for the course CSE 431/531 taught by Professor Xinhe during the Fall '11 term at SUNY Buffalo.

Ask a homework question - tutors are online