forked from pritikmshaw/Python-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnapsack.py
More file actions
31 lines (21 loc) · 713 Bytes
/
Knapsack.py
File metadata and controls
31 lines (21 loc) · 713 Bytes
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
def knapsack(weights, values, W):
n = len(weights)
tab = [[0 for i in range(W + 1)] for j in range(n + 1)]
for i in range(1, n + 1):
# check all possible maxWeight values from 1 to W
for j in range(1, W + 1):
if weights[i - 1] <= j:
tab[i][j] = max(
tab[i - 1][j], values[i - 1] + tab[i - 1][j - weights[i - 1]]
)
else:
tab[i][j] = tab[i - 1][j]
return tab[n][W]
def main():
values = [12, 1000, 30, 10, 1000]
weights = [19, 120, 20, 1, 120]
W = 40
print("Result - {}".format(knapsack(weights, values, W)))
return
if __name__ == "__main__":
main()