• :00Days
• :00Hours
• :00Mins
• 00Seconds
A new era for learning is coming soon Suggested languages for you:

Europe

Answers without the blur. Sign up and see all textbooks for free! Q14SE

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

Book edition 7th
Author(s) Kenneth H. Rosen
Pages 808 pages
ISBN 9780073383095 # 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 ${\mathbf{M}}{\mathbf{\left(}}{\mathbf{j}}{\mathbf{,}}{\mathbf{w}}{\mathbf{\right)}}$ 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 ${{\mathbit{w}}}_{{\mathbf{j}}}{\mathbf{>}}{\mathbit{w}}$, then ${\mathbit{M}}\left(j,w\right){\mathbf{=}}{\mathbit{M}}\left(j-1,w\right){\mathbf{.}}$ b) Show that if ${{\mathbit{w}}}_{{\mathbf{j}}}{\mathbf{\le }}{\mathbit{w}}$, then $\mathbf{M}\mathbf{\left(}\mathbf{j}\mathbf{,}\mathbf{w}\mathbf{\right)}\mathbf{=}\mathbf{m}\mathbf{a}\mathbf{x}\left(M\left(j-1,w\right),{w}_{j}+M\left(j-1,w-{w}_{j}\right)\right)$. 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 ${\mathbf{M}}{\mathbf{\left(}}{\mathbf{j}}{\mathbf{,}}{\mathbf{w}}{\mathbf{\right)}}$ as they are found. d) Explain how you can use the values ${\mathbf{M}}{\mathbf{\left(}}{\mathbf{j}}{\mathbf{,}}{\mathbf{w}}{\mathbf{\right)}}$computed by the algorithm in part (c) to find a subset of items with maximum total weight not exceeding W.

The solutions are

1. $M\left(j,w\right)=M\left(j-1,w\right)$
2. $M\left(j,w\right)= \left(M\left(j-1,w\right),{w}_{j}+M\left(j-1,w-{w}_{j}\right)\right)$
3. $M\left(j,w\right)=\left\{\begin{array}{cc}M\left(j-1,w\right),& {w}_{j}>w\\ \left(M\left(j-1,w\right),{w}_{j}+M\left(j-1,w-{w}_{j}\right)\right),& {w}_{j}\le w\\ & \phantom{\rule{0ex}{0ex}}\end{array}\right\$
4. $S\left(j,w\right)=\left\{\begin{array}{cc}S\left(j-1,w\right),& \text{if}M\left(j,w\right)=M\left(j-1,w\right)\\ S\left(j-1,w-{w}_{j}\right)\cup j,& \text{if}M\left(j,w\right)={w}_{j}+M\left(j-1,w-{w}_{j}\right)\\ & \phantom{\rule{0ex}{0ex}}\end{array}\right\$
See the step by step solution

## Step 1: Given data

Let $M\left(j,w\right)$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 ${w}_{i}$(which we assume is an integer).

$M\left(j,w\right)$ Maximum total weight of the items in a subset of the first $j$ items such that this total weight does not exceed $w$.If ${w}_{j}>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).

$M\left(j,w\right)=M\left(j-1,w\right)$

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

If ${w}_{j}\le w,$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-1$items increased by the weight of item $j$ . Note: The maximum total weight of the first items cannot exceed $w-{w}_{j}$, because else the maximum total weight including item will exceed $w$ .

${w}_{j}+M\left(j-1,w-{w}_{j}\right)$

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-1$items (as we do not include item $j$ ).

$M\left(j-1,w\right)$

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\left(j,w\right)= \left(M\left(j-1,w\right),{w}_{j}+M\left(j-1,w-{w}_{j}\right)\right)$

## 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\left(1,w\right)=\left\{\begin{array}{cc}{w}_{1}& {w}_{1}\le w\\ 0& \text{otherwise}\\ & \phantom{\rule{0ex}{0ex}}\end{array}\right\$

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

$M\left(j,0\right)=0$

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

$M\left(j,w\right)=\left\{\begin{array}{cc}M\left(j-1,w\right),& {w}_{j}>w\\ \left(M\left(j-1,w\right),{w}_{j}+M\left(j-1,w-{w}_{j}\right)\right),& {w}_{j}\le w\\ & \phantom{\rule{0ex}{0ex}}\end{array}\right\$

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

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

$S\left(1,w\right)=1$

When $M\left(j,0\right)=0$ , then the subset $S\left(j,0\right)$ is the empty set $\varnothing$.

$S\left(j,0\right)=0$

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

When $M\left(j,w\right)={w}_{j}+M\left(j-1,w-{w}_{j}\right)$, then item $j$ is added to the subset.

$S\left(j,w\right)=\left\{\begin{array}{cc}S\left(j-1,w\right),& \text{if}M\left(j,w\right)=M\left(j-1,w\right)\\ S\left(j-1,w-{w}_{j}\right)\cup j,& \text{if}M\left(j,w\right)={w}_{j}+M\left(j-1,w-{w}_{j}\right)\\ & \phantom{\rule{0ex}{0ex}}\end{array}\right\$ ### Want to see more solutions like these? 