forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a naive recursive implementation of 0-1 Knapsack Problem (TheAlgo…
…rithms#2743) * Add naive recursive implementation of 0-1 Knapsack problem * Fix shadowing * Add doctest * Fix type hints * Add link to wiki * Blacked the file * Fix isort * Move knapsack / add readme and more tests * Add missed main in tests
- Loading branch information
1 parent
79d5755
commit 802ac83
Showing
4 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# A naive recursive implementation of 0-1 Knapsack Problem | ||
|
||
This overview is taken from: | ||
|
||
https://en.wikipedia.org/wiki/Knapsack_problem | ||
|
||
--- | ||
|
||
## Overview | ||
|
||
The knapsack problem is a problem in combinatorial optimization: Given a set of items, each with a weight and a value, determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit and the total value is as large as possible. It derives its name from the problem faced by someone who is constrained by a fixed-size knapsack and must fill it with the most valuable items. The problem often arises in resource allocation where the decision makers have to choose from a set of non-divisible projects or tasks under a fixed budget or time constraint, respectively. | ||
|
||
The knapsack problem has been studied for more than a century, with early works dating as far back as 1897 The name "knapsack problem" dates back to the early works of mathematician Tobias Dantzig (1884–1956), and refers to the commonplace problem of packing the most valuable or useful items without overloading the luggage. | ||
|
||
--- | ||
|
||
## Documentation | ||
|
||
This module uses docstrings to enable the use of Python's in-built `help(...)` function. | ||
For instance, try `help(Vector)`, `help(unitBasisVector)`, and `help(CLASSNAME.METHODNAME)`. | ||
|
||
--- | ||
|
||
## Usage | ||
|
||
Import the module `knapsack.py` from the **.** directory into your project. | ||
|
||
--- | ||
|
||
## Tests | ||
|
||
`.` contains Python unit tests which can be run with `python3 -m unittest -v`. |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from typing import List | ||
|
||
""" A naive recursive implementation of 0-1 Knapsack Problem | ||
https://en.wikipedia.org/wiki/Knapsack_problem | ||
""" | ||
|
||
|
||
def knapsack(capacity: int, weights: List[int], values: List[int], counter: int) -> int: | ||
""" | ||
Returns the maximum value that can be put in a knapsack of a capacity cap, | ||
whereby each weight w has a specific value val. | ||
>>> cap = 50 | ||
>>> val = [60, 100, 120] | ||
>>> w = [10, 20, 30] | ||
>>> c = len(val) | ||
>>> knapsack(cap, w, val, c) | ||
220 | ||
The result is 220 cause the values of 100 and 120 got the weight of 50 | ||
which is the limit of the capacity. | ||
""" | ||
|
||
# Base Case | ||
if counter == 0 or capacity == 0: | ||
return 0 | ||
|
||
# If weight of the nth item is more than Knapsack of capacity, | ||
# then this item cannot be included in the optimal solution, | ||
# else return the maximum of two cases: | ||
# (1) nth item included | ||
# (2) not included | ||
if weights[counter - 1] > capacity: | ||
return knapsack(capacity, weights, values, counter - 1) | ||
else: | ||
left_capacity = capacity - weights[counter - 1] | ||
new_value_included = values[counter - 1] + knapsack( | ||
left_capacity, weights, values, counter - 1 | ||
) | ||
without_new_value = knapsack(capacity, weights, values, counter - 1) | ||
return max(new_value_included, without_new_value) | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
""" | ||
Created on Fri Oct 16 09:31:07 2020 | ||
@author: Dr. Tobias Schröder | ||
@license: MIT-license | ||
This file contains the test-suite for the knapsack problem. | ||
""" | ||
import unittest | ||
|
||
from knapsack import knapsack as k | ||
|
||
|
||
class Test(unittest.TestCase): | ||
def test_base_case(self): | ||
""" | ||
test for the base case | ||
""" | ||
cap = 0 | ||
val = [0] | ||
w = [0] | ||
c = len(val) | ||
self.assertEqual(k.knapsack(cap, w, val, c), 0) | ||
|
||
val = [60] | ||
w = [10] | ||
c = len(val) | ||
self.assertEqual(k.knapsack(cap, w, val, c), 0) | ||
|
||
def test_easy_case(self): | ||
""" | ||
test for the base case | ||
""" | ||
cap = 3 | ||
val = [1, 2, 3] | ||
w = [3, 2, 1] | ||
c = len(val) | ||
self.assertEqual(k.knapsack(cap, w, val, c), 5) | ||
|
||
def test_knapsack(self): | ||
""" | ||
test for the knapsack | ||
""" | ||
cap = 50 | ||
val = [60, 100, 120] | ||
w = [10, 20, 30] | ||
c = len(val) | ||
self.assertEqual(k.knapsack(cap, w, val, c), 220) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |