You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A basic brute-force solution could be to try all combinations of the given items (as we did above), allowing us to choose the one with maximum profit and a weight that doesn’t exceed ‘W.’ Take the example of 3 items (A, B, C, and D), as shown in the diagram below. To try all the combinations, our algorithm will look like:
24
+
## 🧮 Explanation
25
+
A basic brute-force solution could be to try all combinations of the given items, allowing us to choose the one with maximum profit and a weight that doesn’t exceed ‘W.’ Take the example of 3 items, as shown in the diagram below. To try all the combinations, our algorithm will look like:
We can optimize this algorithm further with a 2D matrix. We can overcome the problem of calculating redundant cases and thus increased complexity. We can solve this problem by simply creating a 2-D array that can store a particular state (n, w) if we get it the first time. Now if we come across the same state (n, w) again instead of calculating it in exponential complexity we can directly return its result stored in the table in constant time. This method gives an edge over the recursive approach in this aspect.
32
+
26
33
```
27
-
In the following recursion tree, K() refers to knapSack().
28
-
The two parameters indicated in the following recursion tree are n and W.
29
-
The recursion tree is for following sample inputs.
30
-
31
-
wt[] = {10, 20, 30}, W = 50, val[] = {10, 20, 30}
32
-
33
-
K(n, W)
34
-
K(3, 50)
35
-
/ \
36
-
/ \
37
-
K(2, 40) K(2, 50)
38
-
/ \ / \
39
-
/ \ / \
40
-
K(1, 20) K(1, 40) K(1, 1) K(1, 0)
41
-
/ \ / \ / \
42
-
/ \ / \ / \
43
-
K(0, -10) K(0, 1) K(0, 1) K(0, 0) K(0, 1) K(0, 0)
44
-
45
-
Recursion tree for Knapsack capacity 2 units and 3 items of 1 unit weight.
34
+
Let weight elements = {1, 2, 3}
35
+
Let weight values = {10, 15, 40}
36
+
Capacity=6
37
+
38
+
We intialize a 2D MATRIX called DP with 0 to 3(no of items) rows and 0 to 6(max capacities possible)
39
+
Each cell of this DP will represent max profit by selecting certain combination of items(through recursion)
40
+
which is below or equal to max capacity of it's column, We initialize all cell values as -1.
41
+
42
+
Since we know our 2d dp matrix will be as shown below irrespective of the values of weights as
43
+
not taking any element or max capacity=0 will give max profit = 0 so 0th row and 0th column are
44
+
0, consider -1 to be null values:
45
+
0 1 2 3 4 5 6 (max capacities)
46
+
- - - - - - -
47
+
0 | 0 0 0 0 0 0 0
48
+
49
+
1 | 0
50
+
51
+
2 | 0
52
+
53
+
3 | 0
54
+
(item no.)
55
+
56
+
Filling 1st row:
57
+
For filling 'weight(i) = 1' we come across 'j = 1 to 6' in which we take maximum of :
58
+
max(0, 0 + DP[1-1][j-1]) = 25, from j = 1 to 6, where 0=DP[i-1][j] in 1 not taken.
59
+
| |
60
+
'1' '1 taken'
61
+
not taken
62
+
63
+
0 1 2 3 4 5 6
64
+
- - - - - - -
65
+
0 | 0 0 0 0 0 0 0
66
+
67
+
1 | 0 10 10 10 10 10 10
68
+
69
+
2 | 0
70
+
71
+
3 | 0
72
+
73
+
Filling 2nd row:
74
+
For filling 'weight(i) = 2' at 'j = 1' we take maximum of :
75
+
max(10, 15 + DP[2-1][1-2]) = 10, where 10 = DP[i-1][j] and since for "15 + DP[2-1][1-2]" we will reach max weight limit
76
+
| | so it cannot be taken, that why DP[2-1][1-2] is undefined and also not accessed
77
+
'2' '2 taken'
78
+
not taken
79
+
80
+
For filling 'weight(i) = 2' at 'j = 2' we take maximum of :
81
+
max(10, 15 + DP[2-1][2-2]) = 15, where 10 = DP[i-1][j]
82
+
| |
83
+
'2' '2 taken'
84
+
not taken
85
+
86
+
For filling 'weight(i) = 2' at 'j = 3 to 6' we take maximum of :
87
+
max(10, 15 + DP[2-1][j-2]) = 25, where 10 = DP[i-1][j], and since all DP[1][1] to DP[1][6] has value 10, so 15+10=25
88
+
| |
89
+
'2' '2 taken'
90
+
not taken
91
+
92
+
0 1 2 3 4 5 6
93
+
- - - - - - -
94
+
0 | 0 0 0 0 0 0 0
95
+
96
+
1 | 0 10 10 10 10 10 10
97
+
98
+
2 | 0 10 15 25 25 25 25
99
+
100
+
3 | 0
101
+
102
+
Filling 3rd row:
103
+
For filling 'weight(i) = 3' at 'j = 1' we take maximum of :
104
+
max(10, 40 + DP[3-1][1-3]) = 15, where 10 = DP[i-1][j], and since DP[3-1][1-3] isnt valid we take 10 as max
105
+
| |
106
+
'3' '3 taken'
107
+
not taken
108
+
109
+
For filling 'weight(i) = 3' at 'j = 2' we take maximum of :
110
+
max(15, 40 + DP[3-1][2-3]) = 15, where 10 = DP[i-1][j], and since DP[3-1][2-3] isnt valid we take 15 as max
111
+
| |
112
+
'3' '3 taken'
113
+
not taken
114
+
115
+
For filling 'weight(i) = 3' at 'j = 3' we take maximum of :
116
+
max(25, 40 + DP[3-1][3-3]) = 15, where 10 = DP[i-1][j], and since DP[3-1][3-3] is valid we take 40+0 as max
117
+
| |
118
+
'3' '3 taken'
119
+
not taken
120
+
121
+
similarly, when we reach 'weight(i) = 3' at 'j = 6' we take maximum of :
122
+
max(25, 40 + DP[3-1][3-3]) = 15, where 10 = DP[i-1][j], and since DP[6-1][6-3] is valid we take 40+25 as max
123
+
| |
124
+
'3' '3 taken'
125
+
not taken
126
+
We have reached our solution for max profit (with max weight capacity = 6) at DP[3][6] (the last cell of our 2d matrix)
0 commit comments