• :00Days
  • :00Hours
  • :00Mins
  • 00Seconds
A new era for learning is coming soonSign up for free
Log In Start studying!

Select your language

Suggested languages for you:
Answers without the blur. Sign up and see all textbooks for free! Illustration

Q14SE

Expert-verified
Discrete Mathematics and its Applications
Found in: Page 567
Discrete Mathematics and its Applications

Discrete Mathematics and its Applications

Book edition 7th
Author(s) Kenneth H. Rosen
Pages 808 pages
ISBN 9780073383095

Answers without the blur.

Just sign up for free and you're in.

Illustration

Short Answer

In this exercise we construct a dynamic programming algorithm for solving the problem of finding a subset S of items chosen from a set of n items where item i has a weight , which is a positive integer, so that the total weight of the items in S is a maximum but does no exceed a fixed weight limit W. Let M(j,w) denote the maximum total weight of the items in a subset of the first j items such that this total weight does not exceed w. This problem is known as the knapsack problem.

a) Show that if wj>w, then M(j,w)=M(j-1,w). b) Show that if wjw, then M(j,w)=max(M(j-1,w),wj+Mj-1,w-wj). c) Use (a) and (b) to construct a dynamic programming algorithm for determining the maximum total weight of items so that this total weight does not exceed W. In your algorithm store the values M(j,w) as they are found. d) Explain how you can use the values M(j,w)computed by the algorithm in part (c) to find a subset of items with maximum total weight not exceeding W.

The solutions are

  1. Mj,w=M(j-1,w)
  2. M(j,w)=M(j-1,w),wj+Mj-1,w-wj
  3. M(j,w)=M(j-1,w),wj>w M(j-1,w),wj+Mj-1,w-wj,wjw
  4. S(j,w)=S(j-1,w), if M(j,w)=M(j-1,w)Sj-1,w-wjj, if M(j,w)=wj+Mj-1,w-wj
See the step by step solution

Step by Step Solution

Step 1: Given data

Let M(j,w)denote the maximum total weight of the items in a subset of the first j items such that this total weight does not exceed w.

Step 2: Definition

A recurrence relation is an equation that recursively defines a sequence where the next term is a function of the previous terms.

Step 3: Show the following equation of part (a)

Given: Set of n items with item i having weight wi(which we assume is an integer).

Mj,w Maximum total weight of the items in a subset of the first j items such that this total weight does not exceed w.If wj>w, then this means that the weight of item j exceeds w and thus we cannot include item j(as it weights too much). The maximum total weight among the first j items will then have to be the same as the maximum total weight among the first j-1 items (as we cannot include item j in the subset corresponding to the maximum total weight).

Mj,w=M(j-1,w)

Step 4: Show the following equation of part (b)

If wjw,then this means that the weight of item j exceeds w and thus we can include item j (but we don't have to when there is a maximum total weight when not including j ).

First case We include item j in the maximum total weight, then the maximum total weight will be the maximum total weight of the first j-1items increased by the weight of item j . Note: The maximum total weight of the first items cannot exceed w-wj, because else the maximum total weight including item will exceed w .

wj+Mj-1,w-wj

Second case We do not include item j in the maximum total weight, then the maximum total weight will be the maximum total weight of the first j-1items (as we do not include item j ).

M(j-1,w)

Conclusion We want to maximize the total weight, while we know that either the first or second case has the maximum total weight. Thus, to maximum the total weight, we can then take the maximum of the two cases.

M(j,w)=M(j-1,w),wj+Mj-1,w-wj

Step 5: Show the following equation of part (c)

Initially, when the set contains only 1 item, then the maximum total weight is the weight of the item (if the weight does not exceed w ).

M(1,w)=w1w1w0 otherwise

When w is zero, then the maximum total weight is also 0 (because the maximum total weight is nonnegative and cannot exceed w ).

Mj,0=0

We then use the result of part (a) and (b) to compute the maximum total weight for j=2,3,...,nwhile for each j-value we loop through the weights 1,2,...w.

M(j,w)=M(j-1,w),wj>w M(j-1,w),wj+Mj-1,w-wj,wjw

Step 6: Show the following equation of part (d)

You can record the subsets with maximum total weight along with the value M(j,w).
When M(1,w)=w1 , then the subset S(1,w)contains {1} .

S(1,w)=1

When M(j,0)=0 , then the subset S(j,0) is the empty set .

S(j,0)=0

When M(j,w)=M(j-1,w), then the subset is the same as the subset for the first j-1 items (as the th item is not included).

When M(j,w)=wj+M(j-1,w-wj), then item j is added to the subset.

S(j,w)=S(j-1,w), if M(j,w)=M(j-1,w)Sj-1,w-wjj, if M(j,w)=wj+Mj-1,w-wj

Recommended explanations on Math Textbooks

94% of StudySmarter users get better grades.

Sign up for free
94% of StudySmarter users get better grades.