Skip to content

Commit 98d8c7c

Browse files
[new] created README.md
- new readme.md in PyAlgo-Tree/Dynamic Programming/0 1 Knapsack/
1 parent 2cad27b commit 98d8c7c

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# 0/1 Knapsack
2+
Language used : **Python3**
3+
4+
## 🎯 Aim
5+
To maximize profit/value in 0/1 Knapsack Problem
6+
7+
## 🌟 Purpose
8+
To show the implementation of Top Down Dynamic Programming approach to solve 0/1 Knapsack Problem.
9+
10+
## 📄 Description
11+
The knapsack problem is a problem in combinatorial optimization: Given a set of items, each with a weight and a value, take items to include in a knapsack such that the total weight is less than or equal to a given limit and the total value is as large as possible. Print the total value.
12+
13+
Example:
14+
```
15+
Input :
16+
val = [60, 100, 120 ]
17+
wt = [10, 20, 30 ]
18+
W = 50
19+
20+
Output :
21+
220
22+
```
23+
24+
## Explanation
25+
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:
26+
```
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.
46+
```
47+
48+
## Author
49+
Neeraj Pratap Hazarika [@NeerajHazarika](https://github.com/NeerajHazarika)
50+
51+
[![forthebadge made-with-python](http://ForTheBadge.com/images/badges/made-with-python.svg)](https://www.python.org/)

0 commit comments

Comments
 (0)