Docsity

Dynamic Programming, Assignment Problems - Algorithm Design | ECS 122A, Study notes of Computer Science

Toggle sidebar
bg1
ECS122A Handout The 0-1 Knapsack Problem
Problem statement. A thief robbing a store finds nitems; the ith item is worth vidollars
and weights wipounds. He wants to take as valuable a load as possible, but he can carry at
most Wpounds in his knapsack. Here vi,wiand Ware all positive integers. What items
should be taken?
Formally, the problem can be stated as follws:
Given: nitems of values v1, v2,...,vnand of the weight w1, w2,...,wn, and a
total weight W, where vi,wiand Ware positive integers.
Find: a subsect S ⊆ {1,2,...,n}of the items such that
X
i∈S
wi≤Wand X
i∈S
viis maximized.
This is called the 0-1 knapsack problem because each item must either be taken or left
behind; the thief cannot take a fractional amount of an item or take an item more than once.
The knapsack problem is an abstraction of many real problems, from investing to telephone
routing.
Dynamic Programming.
1. The solution is based on the optimal-substructure observation. Let kbe the highest-
numberd item in an optimal solution Sof Wpounds and items {1,2,...,n}. Then
S′=S − {k}is an optimal solution for W−wkpounds and items 1,2,...,k−1, and
the value of the solution Sis vkplus the value of the subproblem solution S′.
2. We can express the optimal-substructure observation in the following recursive formula:
Define c[i, w] to be the value of an optimal solution for items 1...i and maximum weight
w. Then
c[i, w] =
0 if i= 0 or w= 0
max (vi+c[i−1, w −wi], c[i−1, w ]) if i > 0 and wi≤w
c[i−1, w] if i > 0 and wi> w
This says that the value of a solution for item iis
•either includes item i, in which case it is viplus a subproblem solution for i−1
items and the weight excluding wi,
•or does not include item i, in which case it is a subproblem solution of i−1 items
and the same weight.
This is, if the thief picks item i, he takes vivalue, and he can choose from items 1...i −1
up to the weight limit w−wi, and get c[i−1, w −wi] additional value. On the other
hand, if he decides not to take item i, he can choose from items 1...i −1 up to the weight
limit w, and get c[i−1, w] value. The better of these two choices should be made.
1
pf3

Partial preview of the text

Download Dynamic Programming, Assignment Problems - Algorithm Design | ECS 122A and more Study notes Computer Science in PDF only on Docsity!

ECS122A Handout The 0-1 Knapsack Problem

Problem statement. A thief robbing a store finds n items; the ith item is worth vi dollars and weights wi pounds. He wants to take as valuable a load as possible, but he can carry at most W pounds in his knapsack. Here vi, wi and W are all positive integers. What items should be taken? Formally, the problem can be stated as follws:

Given: n items of values v 1 , v 2 ,... , vn and of the weight w 1 , w 2 ,... , wn, and a total weight W , where vi, wi and W are positive integers. Find: a subsect S ⊆ { 1 , 2 ,... , n} of the items such that ∑

i∈S

wi ≤ W and

∑

i∈S

vi is maximized.

This is called the 0-1 knapsack problem because each item must either be taken or left behind; the thief cannot take a fractional amount of an item or take an item more than once. The knapsack problem is an abstraction of many real problems, from investing to telephone routing.

Dynamic Programming.

  1. The solution is based on the optimal-substructure observation. Let k be the highest- numberd item in an optimal solution S of W pounds and items { 1 , 2 ,... , n}. Then S′^ = S − {k} is an optimal solution for W − wk pounds and items 1, 2 ,... , k − 1, and the value of the solution S is vk plus the value of the subproblem solution S′.
  2. We can express the optimal-substructure observation in the following recursive formula: Define c[i, w] to be the value of an optimal solution for items 1...i and maximum weight w. Then

c[i, w] =

  

0 if i = 0 or w = 0 max (vi + c[i − 1 , w − wi], c[i − 1 , w]) if i > 0 and wi ≤ w c[i − 1 , w] if i > 0 and wi > w

This says that the value of a solution for item i is

  • either includes item i, in which case it is vi plus a subproblem solution for i − 1 items and the weight excluding wi,
  • or does not include item i, in which case it is a subproblem solution of i − 1 items and the same weight.

This is, if the thief picks item i, he takes vi value, and he can choose from items 1...i − 1 up to the weight limit w − wi, and get c[i − 1 , w − wi] additional value. On the other hand, if he decides not to take item i, he can choose from items 1...i − 1 up to the weight limit w, and get c[i − 1 , w] value. The better of these two choices should be made.

  1. Although the 0-1 knapsack problem doesn’t seem analogus to the LCS problem, the above formula for c is similar to the LCS formula: initial values are 0, and other values are computed from the inputs and “earlier” values of c. So the 0-1 knapsack problem is like the LCS-length algorithm for finding the LCS of two sequences.
  2. The pseudocode is presented below. The algorithm takes as inputs the maximum weight W , the number of items n, and the two sequences v = 〈v 1 , v 2 ,... vn〉 and w = 〈w 1 , w 2 ,... wn〉. It stores the c[i, w] values in the table of c[0...n, 0 ...W ] whose entries are computed row-major order (This is, the first row of c is filled from left to right, then the second row, and so on.) At the end of the computation, c[n, W ] contains the maximum value the thief can take.

Dynamic-0-1-Knapsack(v,w,n,W) for w = 0 to W c[0,w] = 0 for i = 1 to n c[i,0] = 0 for w = 1 to W if w[i] <= w then if v[i] + c[i-1,w-w[i]] > c[i-1,w] then c[i,w] = v[i] + c[i-1,w-w[i]] else c[i,w] = c[i-1,w] else c[i,w] = c[i-1,w]

  1. The set of items to take can be deduced from the c-table by starting at c[n, W ] and tracing where the optimal values came from. - If c[i, w] = c[i − 1 , w], item i is not part of the solution, and we continue tracing with c[i − 1 , w]. - Otherwise item i is part of the solution, and we continue tracing with c[i− 1 , w−wi].
  2. The above algorithm takes Θ(nW ) time total:
    • Θ(nW ) to fill in the c table – (n + 1) · (W + 1) entries each requiring Θ(1) time to compute.
    • O(n) time to trace the solution (since it starts in row n of the table and moves up 1 row at each step.)