Dynamic Programming Application to the 0/1 Knapsack Problem, Study notes of Data Structures and Algorithms

The concept of dynamic programming and its application to the 0/1 knapsack problem. The sequence of decisions, problem state, principle of optimality, and dynamic programming recurrence equations. It also provides a recursive code implementation and discusses time complexity and reducing run time.

Typology: Study notes

Pre 2010

Uploaded on 09/17/2009

koofers-user-tlm
koofers-user-tlm 🇺🇸

5

(1)

10 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Dynamic Programming
Sequence of decisions.
Problem state.
Principle of optimality.
Dynamic Programming Recurrence
Equations.
Solution of recurrence equations.
Sequence Of Decisions
As in the greedy method, the solution to a
problem is viewed as the result of a
sequence of decisions.
Unlike the greedy method, decisions are not
made in a greedy and binding manner.
0/1 Knapsack Problem
Let xi= 1 when item i is selected and let xi= 0
when item iis not selected.
i = 1
npixi
maximize
i = 1
nwixi<= c
subject to
and xi= 0 or 1 for all i
All profits and weights are positive.
Sequence Of Decisions
Decide the xivalues in the order x1, x2, x3, …, xn.
Decide the xivalues in the order xn,xn-1,xn-2, …,
x1.
Decide the xivalues in the order x1,xn, x2,xn-1,
Or any other order.
pf3
pf4
pf5

Partial preview of the text

Download Dynamic Programming Application to the 0/1 Knapsack Problem and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Dynamic Programming

  • Sequence of decisions.
  • Problem state.
  • Principle of optimality.
  • Dynamic Programming Recurrence Equations.
  • Solution of recurrence equations.

Sequence Of Decisions

  • As in the greedy method, the solution to a problem is viewed as the result of a sequence of decisions.
  • Unlike the greedy method, decisions are not made in a greedy and binding manner.

0/1 Knapsack Problem

Let xi = 1 when item i is selected and let xi = 0 when item i is not selected.

i = 1

n maximize pi^ xi

i = 1

n subject to wi^ xi^ <= c

and xi = 0 or 1 for all i All profits and weights are positive.

Sequence Of Decisions

  • Decide the xi values in the order x 1 , x 2 , x 3 , …, xn.
  • Decide the xi values in the order xn, xn-1, xn-2, …, x 1.
  • Decide the xi values in the order x 1 , xn, x 2 , xn-1, …
  • Or any other order.

Problem State

  • The state of the 0/1 knapsack problem is given by

ƒ the weights and profits of the available items ƒ the capacity of the knapsack

  • When a decision on one of the xi values is made, the problem state changes. ƒ item i is no longer available ƒ the remaining knapsack capacity may be less

Problem State

  • Suppose that decisions are made in the order x 1 , x 2 , x 3 , …, xn.
  • The initial state of the problem is described by the pair (1, c). ƒ Items 1 through n are available (the weights, profits and n are implicit). ƒ The available knapsack capacity is c.
  • Following the first decision the state becomes one of the following: ƒ (2, c) … when the decision is to set x 1 = 0. ƒ (2, c-w 1 ) … when the decision is to set x 1 = 1.

Problem State

  • Suppose that decisions are made in the order xn, xn-1, xn-2, …, x 1.
  • The initial state of the problem is described by the pair (n, c). ƒ Items 1 through n are available (the weights, profits and first item index are implicit). ƒ The available knapsack capacity is c.
  • Following the first decision the state becomes one of the following: ƒ (n-1, c) … when the decision is to set xn= 0. ƒ (n-1, c-wn) … when the decision is to set xn= 1.

Principle Of Optimality

  • An optimal solution satisfies the following property: ƒ No matter what the first decision, the remaining decisions are optimal with respect to the state that results from this decision.
  • Dynamic programming may be used only when the principle of optimality holds.

x 1 = a 1 = 1

  • If not, this instance has a better solution b 2 , b 3 , …, bn.

i = 2

n maximize pi^ xi

i = 2

n subject to wi^ xi^ <= c- w^1

and xi = 0 or 1 for all i

i = 2

n pi bi > i = 2

n pi ai

x 1 = a 1 = 1

  • x 1 = a 1 , x 2 = b 2 , x 3 = b 3 , …, xn = bn is a better solution to the original instance than is x 1 = a 1 , x 2 = a 2 , x 3 = a 3 , …, xn = an.
  • So x 1 = a 1 , x 2 = a 2 , x 3 = a 3 , …, xn = an cannot be an optimal solution … a contradiction with the assumption that it is optimal.

0/1 Knapsack Problem

  • Therefore, no matter what the first decision, the remaining decisions are optimal with respect to the state that results from this decision.
  • The principle of optimality holds and dynamic programming may be applied.

Dynamic Programming Recurrence

  • Let f(i,y) be the profit value of the optimal solution to the knapsack instance defined by the state (i,y). ƒ Items i through n are available. ƒ Available capacity is y.
  • For the time being assume that we wish to determine only the value of the best solution. ƒ Later we will worry about determining the xis that yield this maximum value.
  • Under this assumption, our task is to determine f(1,c).

Dynamic Programming Recurrence

  • f(n,y) is the value of the optimal solution to the knapsack instance defined by the state (n,y). ƒ Only item n is available. ƒ Available capacity is y.
  • If wn <= y, f(n,y) = pn.
  • If wn > y, f(n,y) = 0.

Dynamic Programming Recurrence

  • Suppose that i < n.
  • f(i,y) is the value of the optimal solution to the knapsack instance defined by the state (i,y). ƒ Items i through n are available. ƒ Available capacity is y.
  • Suppose that in the optimal solution for the state (i,y), the first decision is to set xi= 0.
  • From the principle of optimality (we have shown that this principle holds for the knapsack problem), it follows that f(i,y) = f(i+1,y).

Dynamic Programming Recurrence

  • The only other possibility for the first decision is xi= 1.
  • The case xi= 1 can arise only when y >= wi.
  • From the principle of optimality, it follows that f(i,y) = f(i+1,y-wi) + pi.
  • Combining the two cases, we get ƒ f(i,y) = f(i+1,y) whenever y < wi. ƒ f(i,y) = max{f(i+1,y), f(i+1,y-wi) + pi}, y >= wi.

Recursive Code

/** @return f(i,y) */ private static int f(int i, int y) { if (i == n) return (y < w[n])? 0 : p[n]; if (y < w[i]) return f(i + 1, y); return Math.max(f(i + 1, y), f(i + 1, y - w[i]) + p[i]); }

Integer Weights

  • Assume that each weight is an integer.
  • The knapsack capacity c may also be assumed to be an integer.
  • Only f(i,y)s with 1 <= i <= n and 0 <= y <= c are of interest.
  • Even though level i of the recursion tree has up to 2 i-1^ nodes, at most c+1 represent different f(i,y)s.

Integer Weights Dictionary

  • Use an array fArray[][] as the dictionary.
  • fArray[1:n][0:c]
  • fArray[i][y] = -1 iff f(i,y) not yet computed.
  • This initialization is done before the recursive method is invoked.
  • The initialization takes O(cn) time.

No Recomputation Code

private static int f(int i, int y)

{

if (fArray[i][y] >= 0) return fArray[i][y]; if (i == n) {fArray[i][y] = (y < w[n])? 0 : p[n]; return fArray[i][y];} if (y < w[i]) fArray[i][y] = f(i + 1, y); else fArray[i][y] = Math.max(f(i + 1, y), f(i + 1, y - w[i]) + p[i]); return fArray[i][y];

}

Time Complexity

  • t(n) = O(cn).
  • Analysis done in text.
  • Good when cn is small relative to 2 n.
  • n = 3, c = 1010101 w = [100102, 1000321 , 6327] p = [102, 505 , 5]
  • 2 n^ = 8
  • cn = 3030303