mirror of
https://github.com/trekhleb/javascript-algorithms.git
synced 2025-07-08 18:40:56 +08:00
Add knapsack problem.
This commit is contained in:
@ -17,6 +17,52 @@ while still keeping the overall weight under or equal to 15 kg?
|
|||||||
|
|
||||||

|

|
||||||
|
|
||||||
|
## Definition
|
||||||
|
|
||||||
|
### 0/1 knapsack problem
|
||||||
|
|
||||||
|
The most common problem being solved is the **0/1 knapsack problem**,
|
||||||
|
which restricts the number `xi` of copies of each kind of item to zero or one.
|
||||||
|
|
||||||
|
Given a set of n items numbered from `1` up to `n`, each with a
|
||||||
|
weight `wi` and a value `vi`, along with a maximum weight
|
||||||
|
capacity `W`,
|
||||||
|
|
||||||
|
maximize 
|
||||||
|
|
||||||
|
subject to 
|
||||||
|
and 
|
||||||
|
|
||||||
|
Here `xi` represents the number of instances of item `i` to
|
||||||
|
include in the knapsack. Informally, the problem is to maximize
|
||||||
|
the sum of the values of the items in the knapsack so that the
|
||||||
|
sum of the weights is less than or equal to the knapsack's
|
||||||
|
capacity.
|
||||||
|
|
||||||
|
### Bounded knapsack problem (BKP)
|
||||||
|
|
||||||
|
The **bounded knapsack problem (BKP)** removes the restriction
|
||||||
|
that there is only one of each item, but restricts the number
|
||||||
|
`xi` of copies of each kind of item to a maximum non-negative
|
||||||
|
integer value `c`:
|
||||||
|
|
||||||
|
maximize 
|
||||||
|
|
||||||
|
subject to 
|
||||||
|
and 
|
||||||
|
|
||||||
|
### Unbounded knapsack problem (UKP)
|
||||||
|
|
||||||
|
The **unbounded knapsack problem (UKP)** places no upper bound
|
||||||
|
on the number of copies of each kind of item and can be
|
||||||
|
formulated as above except for that the only restriction
|
||||||
|
on `xi` is that it is a non-negative integer.
|
||||||
|
|
||||||
|
maximize 
|
||||||
|
|
||||||
|
subject to 
|
||||||
|
and 
|
||||||
|
|
||||||
## References
|
## References
|
||||||
|
|
||||||
- [Wikipedia](https://en.wikipedia.org/wiki/Knapsack_problem)
|
- [Wikipedia](https://en.wikipedia.org/wiki/Knapsack_problem)
|
||||||
|
Reference in New Issue
Block a user