Skip to content

Commit 817ad9d

Browse files
committed
Add knapsack problem.
1 parent 80433eb commit 817ad9d

File tree

1 file changed

+46
-0
lines changed
  • src/algorithms/sets/knapsack-problem

1 file changed

+46
-0
lines changed

src/algorithms/sets/knapsack-problem/README.md

+46
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,52 @@ while still keeping the overall weight under or equal to 15 kg?
1717

1818
![knapsack problem](https://upload.wikimedia.org/wikipedia/commons/f/fd/Knapsack.svg)
1919

20+
## Definition
21+
22+
### 0/1 knapsack problem
23+
24+
The most common problem being solved is the **0/1 knapsack problem**,
25+
which restricts the number `xi` of copies of each kind of item to zero or one.
26+
27+
Given a set of n items numbered from `1` up to `n`, each with a
28+
weight `wi` and a value `vi`, along with a maximum weight
29+
capacity `W`,
30+
31+
maximize ![0/1 knapsack](https://wikimedia.org/api/rest_v1/media/math/render/svg/85620037d368d2136fb3361702df6a489416931b)
32+
33+
subject to ![0/1 knapsack](https://wikimedia.org/api/rest_v1/media/math/render/svg/dd6e7c9bca4397980976ea6d19237500ce3b8176)
34+
and ![0/1 knapsack](https://wikimedia.org/api/rest_v1/media/math/render/svg/07dda71da2a630762c7b21b51ea54f86f422f951)
35+
36+
Here `xi` represents the number of instances of item `i` to
37+
include in the knapsack. Informally, the problem is to maximize
38+
the sum of the values of the items in the knapsack so that the
39+
sum of the weights is less than or equal to the knapsack's
40+
capacity.
41+
42+
### Bounded knapsack problem (BKP)
43+
44+
The **bounded knapsack problem (BKP)** removes the restriction
45+
that there is only one of each item, but restricts the number
46+
`xi` of copies of each kind of item to a maximum non-negative
47+
integer value `c`:
48+
49+
maximize ![bounded knapsack](https://wikimedia.org/api/rest_v1/media/math/render/svg/85620037d368d2136fb3361702df6a489416931b)
50+
51+
subject to ![bounded knapsack](https://wikimedia.org/api/rest_v1/media/math/render/svg/dd6e7c9bca4397980976ea6d19237500ce3b8176)
52+
and ![bounded knapsack](https://wikimedia.org/api/rest_v1/media/math/render/svg/6c8c5ac4f8247b3b8e01e89de76a1df0ea969821)
53+
54+
### Unbounded knapsack problem (UKP)
55+
56+
The **unbounded knapsack problem (UKP)** places no upper bound
57+
on the number of copies of each kind of item and can be
58+
formulated as above except for that the only restriction
59+
on `xi` is that it is a non-negative integer.
60+
61+
maximize ![unbounded knapsack](https://wikimedia.org/api/rest_v1/media/math/render/svg/85620037d368d2136fb3361702df6a489416931b)
62+
63+
subject to ![unbounded knapsack](https://wikimedia.org/api/rest_v1/media/math/render/svg/dd6e7c9bca4397980976ea6d19237500ce3b8176)
64+
and ![unbounded knapsack](https://wikimedia.org/api/rest_v1/media/math/render/svg/90a99710f61d5dea19e49ae5b31164d2b56b07e3)
65+
2066
## References
2167

2268
- [Wikipedia](https://en.wikipedia.org/wiki/Knapsack_problem)

0 commit comments

Comments
 (0)