forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknapsack_dp.py
57 lines (45 loc) · 1.51 KB
/
knapsack_dp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
def knapsack_dp(capacity: int, weights: list[int], values: list[int]) -> int:
"""
Returns the maximum value that can be put in a knapsack of a given capacity,
with each weight having a specific value.
Uses a dynamic programming approach to solve the 0/1 Knapsack Problem.
>>> capacity = 50
>>> values = [60, 100, 120]
>>> weights = [10, 20, 30]
>>> knapsack_dp(capacity, weights, values)
220
>>> capacity = 0
>>> values = [60, 100, 120]
>>> weights = [10, 20, 30]
>>> knapsack_dp(capacity, weights, values)
0
>>> capacity = 10
>>> values = [10, 10, 10]
>>> weights = [5, 5, 5]
>>> knapsack_dp(capacity, weights, values)
20
>>> capacity = 100
>>> values = [60, 100, 120, 80, 30]
>>> weights = [10, 20, 30, 40, 50]
>>> knapsack_dp(capacity, weights, values)
360
>>> capacity = 7
>>> values = [1, 4, 5, 7]
>>> weights = [1, 3, 4, 5]
>>> knapsack_dp(capacity, weights, values)
9
"""
n = len(weights)
dp = [[0 for _ in range(capacity + 1)] for _ in range(n + 1)]
for i in range(1, n + 1):
for w in range(1, capacity + 1):
if weights[i - 1] <= w:
value_included = values[i - 1] + dp[i - 1][w - weights[i - 1]]
value_excluded = dp[i - 1][w]
dp[i][w] = max(value_included, value_excluded)
else:
dp[i][w] = dp[i - 1][w]
return dp[n][capacity]
if __name__ == "__main__":
import doctest
doctest.testmod()