0% found this document useful (0 votes)
24 views1 page

Tuf M

vdbsnkaml;

Uploaded by

Desh Deepak kant
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views1 page

Tuf M

vdbsnkaml;

Uploaded by

Desh Deepak kant
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Knapsack DP ( Memoization and Tabulation )

dp [i] [c] = max( dp [i-1] [c], profits [i] + dp [i-1] [c - weights[i] ] )

Colour Representation

Green dp [i] [c]

Yellow profits [i] + dp [i-1] [c - weights[i] ]

Red dp [i-1] [c]

Capacity

profit [ ] weight [ ] index 0 1 2 3 4 5 6 7

1 1 0 0

6 2 1 0

10 3 2 0

16 5 3 0

Capacity = 0
Max profit for every subarray = 0

Capacity

profit [ ] weight [ ] index 0 1 2 3 4 5 6 7

1 1 0 0 1 1 1 1 1 1 1

6 2 1 0

10 3 2 0

16 5 3 0

Capacity = 1-7, Index = 0


i.e., if we consider the sub-array till index '0', this means we have only
one item to put in the knapsack, we will take it if it is not more than the
capacity

Capacity

profit [ ] weight [ ] index 0 1 2 3 4 5 6 7

1 1 0 0 1 1 1 1 1 1 1

6 2 1 0 1

10 3 2 0

16 5 3 0

Capacity = 1, Index =1
since item at index '1' has weight '2', which is greater than the capacity
'1', so we will take the dp[index-1] [capacity]

Capacity

profit [ ] weight [ ] index 0 1 2 3 4 5 6 7

1 1 0 0 1 1 1 1 1 1 1

6 2 1 0 1 6

10 3 2 0

16 5 3 0

Capacity = 2. Index = 1
profit[i] + dp[i- 1][c-weight[i]] > dp[i-1][c]
profit[1] + dp[1- 1][2-weight[1]] >dp[1-1][2]
6+0>1

Capacity

profit [ ] weight [ ] index 0 1 2 3 4 5 6 7

1 1 0 0 1 1 1 1 1 1 1

6 2 1 0 1 6 7

10 3 2 0

16 5 3 0

Capacity = 3. Index = 1
profit[i] + dp[i- 1][c-weight[i]] > dp[i-1][c]
profit[1] + dp[1- 1][3-weight[1]] >dp[1-1][3]
6+1>1

Capacity

profit [ ] weight [ ] index 0 1 2 3 4 5 6 7

1 1 0 0 1 1 1 1 1 1 1

6 2 1 0 1 6 7 7 7 7 7

10 3 2 0

16 5 3 0

Capacity = 4 - 7, Index = 1
Max weight till [index 1] = 3
[ ind 4- find 7 ] Profit will be same as of capacity=2, index=1

Capacity

profit [ ] weight [ ] index 0 1 2 3 4 5 6 7

1 1 0 0 1 1 1 1 1 1 1

6 2 1 0 1 6 7 7 7 7 7

10 3 2 0 1 6

16 5 3 0

Capacity = 1-2, Index =2


since item at index '2' has weight = 2, which is greater than equal to the
capacity '2', so we will take the dp[i-1] [c]

Capacity

profit [ ] weight [ ] index 0 1 2 3 4 5 6 7

1 1 0 0 1 1 1 1 1 1 1

6 2 1 0 1 6 7 7 7 7 7

10 3 2 0 1 6 10

16 5 3 0

Capacity = 3. Index = 2
profit[i] + dp[i- 1][c-weight[i]] > dp[i-1][c]
profit[2] + dp[2- 1][3-weight[1]] >dp[2-1][3]
10 + 0 > 7

Capacity

profit [ ] weight [ ] index 0 1 2 3 4 5 6 7

1 1 0 0 1 1 1 1 1 1 1

6 2 1 0 1 6 7 7 7 7 7

10 3 2 0 1 6 10 11

16 5 3 0

Capacity = 4. Index = 2
profit[i] + dp[i- 1][c-weight[i]] > dp[i-1][c]
profit[2] + dp[2- 1][4-weight[1]] >dp[2-1][4]
10 + 1 > 7

Capacity

profit [ ] weight [ ] index 0 1 2 3 4 5 6 7

1 1 0 0 1 1 1 1 1 1 1

6 2 1 0 1 6 7 7 7 7 7

10 3 2 0 1 6 10 11 16

16 5 3 0

Capacity = 5, Index = 2
profit[i] + dp[i- 1][c-weight[i]] > dp[i-1][c]
profit[2] + dp[2- 1][5-weight[1]] >dp[2-1][5]
10 + 6 > 7

Capacity

profit [ ] weight [ ] index 0 1 2 3 4 5 6 7

1 1 0 0 1 1 1 1 1 1 1

6 2 1 0 1 6 7 7 7 7 7

10 3 2 0 1 6 10 11 16 17

16 5 3 0

Capacity = 6, Index = 2
profit[i] + dp[i- 1][c-weight[i]] > dp[i-1][c]
profit[2] + dp[2- 1][6-weight[1]] >dp[2-1][6]
10 + 7 > 7

Capacity

profit [ ] weight [ ] index 0 1 2 3 4 5 6 7

1 1 0 0 1 1 1 1 1 1 1

6 2 1 0 1 6 7 7 7 7 7

10 3 2 0 1 6 10 11 16 17 17

16 5 3 0

Capacity = 7, Index = 2
the max weight till index 2 is 6, so the profit will be same as of
capacity=6, index=2

Capacity

profit [ ] weight [ ] index 0 1 2 3 4 5 6 7

1 1 0 0 1 1 1 1 1 1 1

6 2 1 0 1 6 7 7 7 7 7

10 3 2 0 1 6 10 11 16 17 17

16 5 3 0 1 6 10 11 16

Capacity = 1-5, Index =3,


since item at index '3' has weight '5', which is lesser than or equal to
the capacity '', so we will take the dp[i-1] [c]

Capacity

profit [ ] weight [ ] index 0 1 2 3 4 5 6 7

1 1 0 0 1 1 1 1 1 1 1

6 2 1 0 1 6 7 7 7 7 7

10 3 2 0 1 6 10 11 16 17 17

16 5 3 0 1 6 10 11 16 17

Capacity = 6, Index = 3
profit[i] + dp[i- 1][c-weight[i]] > dp[i-1][c]
profit[3] + dp[3- 1][6-weight[3]] == dp[3-1][6]
16 + 1 == 17

Capacity

profit [ ] weight [ ] index 0 1 2 3 4 5 6 7

1 1 0 0 1 1 1 1 1 1 1

6 2 1 0 1 6 7 7 7 7 7

10 3 2 0 1 6 10 11 16 17 17

16 5 3 0 1 6 10 11 16 17 22

Capacity = 7, Index = 3
profit[i] + dp[i- 1][c-weight[i]] > dp[i-1][c]
profit[3] + dp[3- 1][7-weight[3]] >dp[3-1][7]
16 + 6 > 17

So, the maximum profit for sack having capacity 7 is 22

You might also like