-
Notifications
You must be signed in to change notification settings - Fork 33
/
knapsack.py
67 lines (49 loc) · 1.85 KB
/
knapsack.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
58
59
60
61
62
63
64
65
66
67
# This file was found online, but I am sorry that I don’t know who the original author is now.
# http://www.geeksforgeeks.org/knapsack-problem/
import numpy as np
def knapsack(v, w, max_weight):
rows = len(v) + 1
cols = max_weight + 1
# adding dummy values as later on we consider these values as indexed from 1 for convinence
v = np.r_[[0], v]
w = np.r_[[0], w]
# row : values , #col : weights
dp_array = [[0 for i in range(cols)] for j in range(rows)]
# 0th row and 0th column have value 0
# values
for i in range(1, rows):
# weights
for j in range(1, cols):
# if this weight exceeds max_weight at that point
if j - w[i] < 0:
dp_array[i][j] = dp_array[i - 1][j]
# max of -> last ele taken | this ele taken + max of previous values possible
else:
dp_array[i][j] = max(dp_array[i - 1][j], v[i] + dp_array[i - 1][j - w[i]])
# return dp_array[rows][cols] : will have the max value possible for given wieghts
chosen = []
i = rows - 1
j = cols - 1
# Get the items to be picked
while i > 0 and j > 0:
# ith element is added
if dp_array[i][j] != dp_array[i - 1][j]:
# add the value
chosen.append(i-1)
# decrease the weight possible (j)
j = j - w[i]
# go to previous row
i = i - 1
else:
i = i - 1
return dp_array[rows - 1][cols - 1], chosen
# main
if __name__ == "__main__":
values = list(map(int, input().split()))
weights = list(map(int, input().split()))
max_weight = int(input())
max_value, chosen = knapsack(values, weights, max_weight)
print("The max value possible is")
print(max_value)
print("The index chosen for these are")
print(' '.join(str(x) for x in chosen))