Skip to content

Commit 1fa7ae8

Browse files
Merge pull request #163 from abhisheks008/main
0-1 Knapsack Problem
2 parents 5fa6cc2 + d1ae323 commit 1fa7ae8

File tree

8 files changed

+356
-0
lines changed

8 files changed

+356
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# A naive recursive implementation
2+
# of 0-1 Knapsack Problem
3+
# Brute Force Method
4+
5+
# Problem Statement : Returns the maximum value that
6+
# can be put in a knapsack of
7+
# capacity W
8+
9+
# Author : Abhishek Sharma, 2021
10+
11+
# -----------------------------------------------------------------
12+
13+
# Solution for the above problem statement using Python 3 language.
14+
# Approach : Brute Force Method
15+
16+
# -----------------------------------------------------------------
17+
18+
# This is the main function which will provide us maximum value that
19+
# can be put in the capacity of W
20+
21+
def knapSack(W, wt, val, n):
22+
23+
# Base Case
24+
if n == 0 or W == 0:
25+
return 0
26+
27+
# If weight of the nth item is
28+
# more than Knapsack of capacity W,
29+
# then this item cannot be included
30+
# in the optimal solution
31+
32+
if (wt[n-1] > W):
33+
return knapSack(W, wt, val, n-1)
34+
35+
# return the maximum of two cases:
36+
# (1) nth item included
37+
# (2) not included
38+
39+
else:
40+
return max(
41+
val[n-1] + knapSack(
42+
W-wt[n-1], wt, val, n-1),
43+
knapSack(W, wt, val, n-1))
44+
45+
46+
# end of function knapSack
47+
48+
49+
#Driver Code
50+
val = [60, 100, 120]
51+
wt = [10, 20, 30]
52+
W = 50
53+
n = len(val)
54+
print ("- 0-1 Knapsack Problem using Brute Force Method - ")
55+
print ("-----------------------------------------------------")
56+
print ()
57+
print ("The values given : ")
58+
print (val)
59+
print ("-----------------------------------------------------")
60+
print ("The corresponding weights are :")
61+
print (wt)
62+
print ("-----------------------------------------------------")
63+
print ("The maximum capacity can be : ")
64+
print (W)
65+
print ()
66+
print ("-----------------------------------------------------")
67+
print ("Output :\nMaximum total value of the Knapsack : ")
68+
print (knapSack(W, wt, val, n))
69+
70+
# -----------------------------------------------------------------
71+
# Input given :
72+
# The values given :
73+
# [60, 100, 120]
74+
# -----------------------------------------------------
75+
# The corresponding weights are :
76+
# [10, 20, 30]
77+
# -----------------------------------------------------
78+
# The maximum capacity can be :
79+
# 50
80+
81+
# -----------------------------------------------------
82+
# Output :
83+
# Maximum total value of the Knapsack :
84+
# 220
85+
86+
# -----------------------------------------------------------------
87+
88+
# Code contributed by, Abhishek Sharma, 2021
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# 0-1 Knapsack Problem using Brute Force Method
2+
Language used : Python 3
3+
4+
## 📃 Approach
5+
A simple solution is to consider all subsets of items and calculate the total weight and value of all subsets. Consider the only subsets whose total weight is smaller than W. From all such subsets, pick the maximum value subset.
6+
Optimal Sub-structure: To consider all subsets of items, there can be two cases for every item.
7+
8+
- **Case 1:** The item is included in the optimal subset.
9+
- **Case 2:** The item is not included in the optimal set.
10+
11+
Therefore, the maximum value that can be obtained from ‘n’ items is the max of the following two values.
12+
- Maximum value obtained by n-1 items and W weight (excluding nth item).
13+
- Value of nth item plus maximum value obtained by n-1 items and W minus the weight of the nth item (including nth item).
14+
15+
If the weight of ‘nth’ item is greater than ‘W’, then the nth item cannot be included and Case 1 is the only possibility.
16+
17+
## 🧮 Algorithm
18+
It should be noted that the above function computes the same sub-problems again and again. See the following recursion tree, K(1, 1) is being evaluated twice. The time complexity of this naive recursive solution is exponential (2^n).
19+
20+
```
21+
In the following recursion tree, K() refers
22+
to knapSack(). The two parameters indicated in the
23+
following recursion tree are n and W.
24+
The recursion tree is for following sample inputs.
25+
wt[] = {1, 1, 1}, W = 2, val[] = {10, 20, 30}
26+
K(n, W)
27+
K(3, 2)
28+
/ \
29+
/ \
30+
K(2, 2) K(2, 1)
31+
/ \ / \
32+
/ \ / \
33+
K(1, 2) K(1, 1) K(1, 1) K(1, 0)
34+
/ \ / \ / \
35+
/ \ / \ / \
36+
K(0, 2) K(0, 1) K(0, 1) K(0, 0) K(0, 1) K(0, 0)
37+
Recursion tree for Knapsack capacity 2
38+
units and 3 items of 1 unit weight.
39+
```
40+
41+
## 💻 Input and Output
42+
![](https://github.com/abhisheks008/PyAlgo-Tree/blob/main/Knapsack/0-1%20Knapsack/Images/knapsack2.PNG)
43+
44+
45+
## ⏰ Complexity Analysis:
46+
- **Time Complexity:** `O(2^n)`. [As there are redundant subproblems.]
47+
- **Auxiliary Space :** `O(1)`. [As no extra data structure has been used for storing values.]
48+
49+
---------------------------------------------------------------
50+
## 🖋️ Author
51+
**Code contributed by, _Abhishek Sharma_, 2021 [@abhisheks008](github.com/abhisheks008)**
52+
53+
[![forthebadge made-with-python](http://ForTheBadge.com/images/badges/made-with-python.svg)](https://www.python.org/)
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# A Dynamic Programming based Python
2+
# Program for 0-1 Knapsack problem
3+
# Returns the maximum value that can
4+
# be put in a knapsack of capacity W
5+
6+
# Problem Statement : Returns the maximum value that
7+
# can be put in a knapsack of
8+
# capacity W
9+
10+
# Author : Abhishek Sharma, 2021
11+
12+
# -----------------------------------------------------------------
13+
14+
# Solution for the above problem statement using Python 3 language.
15+
# Approach : Dynamic Method
16+
17+
# -----------------------------------------------------------------
18+
19+
# This is the main function which will provide us maximum value that
20+
# can be put in the capacity of W
21+
22+
def knapSack(W, wt, val, n):
23+
K = [[0 for x in range(W + 1)] for x in range(n + 1)]
24+
25+
# Build table K[][] in bottom up manner
26+
for i in range(n + 1):
27+
for w in range(W + 1):
28+
if i == 0 or w == 0:
29+
K[i][w] = 0
30+
elif wt[i-1] <= w:
31+
K[i][w] = max(val[i-1]
32+
+ K[i-1][w-wt[i-1]],
33+
K[i-1][w])
34+
else:
35+
K[i][w] = K[i-1][w]
36+
37+
return K[n][W]
38+
39+
40+
# Driver code
41+
val = [60, 100, 120]
42+
wt = [10, 20, 30]
43+
W = 50
44+
n = len(val)
45+
print ("- 0-1 Knapsack Problem using Dynamic Method - ")
46+
print ("-----------------------------------------------------")
47+
print ()
48+
print ("The values given : ")
49+
print (val)
50+
print ("-----------------------------------------------------")
51+
print ("The corresponding weights are :")
52+
print (wt)
53+
print ("-----------------------------------------------------")
54+
print ("The maximum capacity can be : ")
55+
print (W)
56+
print ()
57+
print ("-----------------------------------------------------")
58+
print ("Output :\nMaximum total value of the Knapsack : ")
59+
print (knapSack(W, wt, val, n))
60+
61+
# -----------------------------------------------------------------
62+
# Input given :
63+
# The values given :
64+
# [60, 100, 120]
65+
# -----------------------------------------------------
66+
# The corresponding weights are :
67+
# [10, 20, 30]
68+
# -----------------------------------------------------
69+
# The maximum capacity can be :
70+
# 50
71+
72+
# -----------------------------------------------------
73+
# Output :
74+
# Maximum total value of the Knapsack :
75+
# 220
76+
77+
# -----------------------------------------------------------------
78+
79+
# Code contributed by, Abhishek Sharma, 2021
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# 0-1 Knapsack Problem using Dynamic Method
2+
Language used : Python 3
3+
4+
## 📃 Approach
5+
In the Dynamic programming we will work considering the same cases as mentioned in the recursive approach. In a `DP[][]` table let’s consider all the possible weights from ‘1’ to ‘W’ as the columns and weights that can be kept as the rows.
6+
7+
The state `DP[i][j]` will denote maximum value of ‘j-weight’ considering all values from ‘1 to ith’. So if we consider `‘wi’` (weight in ‘ith’ row) we can fill it in all columns which have ‘weight values > wi’. Now two possibilities can take place:
8+
9+
- Fill `‘wi’` in the given column.
10+
- Do not fill ‘wi’ in the given column.
11+
12+
Now we have to take a maximum of these two possibilities, formally if we do not fill `‘ith’` weight in `‘jth’`column then `DP[i][j]` state will be same as `DP[i-1][j]` but if we fill the weight, `DP[i][j]` will be equal to the value of `‘wi’+` value of the column weighing `‘j-wi’` in the previous row. So we take the maximum of these two possibilities to fill the current state.
13+
14+
## 🧮 Algorithm
15+
Let's visualize the algorithm here,
16+
17+
```
18+
Let weight elements = {1, 2, 3}
19+
Let weight values = {10, 15, 40}
20+
Capacity=6
21+
22+
0 1 2 3 4 5 6
23+
24+
0 0 0 0 0 0 0 0
25+
26+
1 0 10 10 10 10 10 10
27+
28+
2 0 10 15 25 25 25 25
29+
30+
3 0
31+
32+
Explanation:
33+
For filling 'weight = 2' we come
34+
across 'j = 3' in which
35+
we take maximum of
36+
(10, 15 + DP[1][3-2]) = 25
37+
| |
38+
'2' '2 filled'
39+
not filled
40+
41+
0 1 2 3 4 5 6
42+
43+
0 0 0 0 0 0 0 0
44+
45+
1 0 10 10 10 10 10 10
46+
47+
2 0 10 15 25 25 25 25
48+
49+
3 0 10 15 40 50 55 65
50+
51+
Explanation:
52+
For filling 'weight=3',
53+
we come across 'j=4' in which
54+
we take maximum of (25, 40 + DP[2][4-3])
55+
= 50
56+
57+
For filling 'weight=3'
58+
we come across 'j=5' in which
59+
we take maximum of (25, 40 + DP[2][5-3])
60+
= 55
61+
62+
For filling 'weight=3'
63+
we come across 'j=6' in which
64+
we take maximum of (25, 40 + DP[2][6-3])
65+
= 65
66+
```
67+
68+
## 💻 Input and Output
69+
![](https://github.com/abhisheks008/PyAlgo-Tree/blob/main/Knapsack/0-1%20Knapsack/Images/knapsack1.PNG)
70+
71+
## ⏰ Complexity Analysis
72+
- **Time Complexity:** `O(N*W)`. [where ‘N’ is the number of weight element and ‘W’ is capacity. As for every weight element we traverse through all weight capacities 1<=w<=W.]
73+
- **Auxiliary Space :** `O(N*W)`. [The use of 2-D array of size `‘N*W’`.]
74+
75+
---------------------------------------------------------------
76+
## 🖋️ Author
77+
**Code contributed by, _Abhishek Sharma_, 2021 [@abhisheks008](github.com/abhisheks008)**
78+
79+
[![forthebadge made-with-python](http://ForTheBadge.com/images/badges/made-with-python.svg)](https://www.python.org/)
10.2 KB
Loading
10.3 KB
Loading
26 KB
Loading

Knapsack/0-1 Knapsack/README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# 0-1 Knapsack Problem
2+
Language used : **Python 3**
3+
4+
![](https://github.com/abhisheks008/PyAlgo-Tree/blob/main/Knapsack/0-1%20Knapsack/Images/knapsack3.jpg)
5+
6+
## 🎯 Aim
7+
The aim of this script is to find out the maximum value subset of val[] such that sum of the weights of this subset is smaller than or equal to W.
8+
9+
## 👉 Purpose
10+
The main purpose of this script is to show the implementation of Brute Force method and Dynamic Programming method to solve the 0-1 Knapsack problem.
11+
12+
## 📄 Description
13+
Given weights and values of `n` items, put these items in a knapsack of capacity `W` to get the maximum total value in the knapsack. In other words, given two integer arrays `val[0..n-1]` and `wt[0..n-1]` which represent values and weights associated with `n` items respectively. Also given an integer `W` which represents knapsack capacity, find out the maximum value subset of `val[]` such that sum of the weights of this subset is smaller than or equal to `W`. You cannot break an item, either pick the complete item or don’t pick it `(0-1 property)`.
14+
15+
## 📈 Workflow of the script
16+
- `knapsack` - This is the main function which will provide us maximum value that can be put in the capacity of W.
17+
- `main` - This is the driver code for this code/script.
18+
19+
## 🧮 Algorithms and Explanation
20+
Here to solve the 0-1 Knapsack problem, I have used two different types of methods,
21+
- **Brute Force Method** - The brute force approach is a guaranteed way to find the correct solution by listing all the possible candidate solutions for the problem. It is a generic method and not limited to any specific domain of problems. The brute force method is ideal for solving small and simpler problems.
22+
23+
🔴 **You can find out the approach of Brute Force Method to solve the 0-1 Knapsack problem, [here ✔️](https://github.com/abhisheks008/PyAlgo-Tree/tree/main/Knapsack/0-1%20Knapsack/Brute%20Force%20Method)**
24+
25+
26+
- **Dynamic Method** - Dynamic programming is a technique that breaks the problems into sub-problems, and saves the result for future purposes so that we do not need to compute the result again. The subproblems are optimized to optimize the overall solution is known as optimal substructure property. The main use of dynamic programming is to solve optimization problems. Here, optimization problems mean that when we are trying to find out the minimum or the maximum solution of a problem. The dynamic programming guarantees to find the optimal solution of a problem if the solution exists. The definition of dynamic programming says that it is a technique for solving a complex problem by first breaking into a collection of simpler subproblems, solving each subproblem just once, and then storing their solutions to avoid repetitive computations.
27+
28+
🔴 **You can find out the approach of Dynamic Method to solve the 0-1 Knapsack problem, [here ✔️](https://github.com/abhisheks008/PyAlgo-Tree/tree/main/Knapsack/0-1%20Knapsack/Dynamic%20Method)**
29+
30+
## 💻 Input and Output
31+
```
32+
The values given :
33+
[60, 100, 120]
34+
-----------------------------------------------------
35+
The corresponding weights are :
36+
[10, 20, 30]
37+
-----------------------------------------------------
38+
The maximum capacity can be :
39+
50
40+
41+
-----------------------------------------------------
42+
Output :
43+
Maximum total value of the Knapsack :
44+
220
45+
```
46+
47+
## 📊 Comparison of two different methods
48+
We have implemented both the algorithms and methods and also have checked the time and space complexities of them. The **Brute force method** is having the time complexity of `O(2^n)` and the **Dynamic Method** is having the time complexity of `O(N*W)`. From this, it is clearly visible that using **Dynamic Method** provides less time complexity which means that the program can be run smoothly with less time spent.
49+
50+
Hence, **Dynamic Method ✔️** is having the upper hand in between these two types of methods of solving!
51+
52+
---------------------------------------------------------------
53+
## 🖋️ Author
54+
**Code contributed by, _Abhishek Sharma_, 2021 [@abhisheks008](github.com/abhisheks008)**
55+
56+
[![forthebadge made-with-python](http://ForTheBadge.com/images/badges/made-with-python.svg)](https://www.python.org/)
57+

0 commit comments

Comments
 (0)