-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnapsack.py
More file actions
51 lines (42 loc) · 1.56 KB
/
Knapsack.py
File metadata and controls
51 lines (42 loc) · 1.56 KB
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
from itertools import combinations
#used to make
def combine(items):
' return combinations of any length from the items '
return ( combine
for r in range(1, len(items)+1)
for combine in combinations(items, r)
)
def TotalValue(combine):
' Totalise a particular combination of items'
totalW = totalV = 0
for item, wgt, val in combine:
totalW += wgt
totalV += val
return (totalV, -totalW) if totalW <= 10 else (0, 0)
#Using Memoization
def KnapSackDP(items, limit):
array = [[0 for w in range(limit + 1)] for j in xrange(len(items) + 1)]
for j in xrange(1, len(items) + 1):
item, wgt, val = items[j-1]
for w in xrange(1, limit + 1):
if wgt > w:
array[j][w] = array[j-1][w]
else:
array[j][w] = max(array[j-1][w],array[j][w-wgt] + val) #DP function
result = []
W = limit
for j in range(len(items), 0, -1):
was_added = (array[j][W] != array[j-1][W]) #appending the results table with the values only from last wgt = W column
if was_added:
item, wgt, val = items[j-1]
result.append(items[j-1])
W -= wgt
return result
items = (
("apple",6,30),("banana",3,14),("carrot",4,16),("knife",2,9)
)
chosen_in_bag = KnapSackDP(items,10)
print("The algorithm choses the following\n " +
'\n '.join(sorted(item for item,_,_ in chosen_in_bag)))
val, wgt = TotalValue(chosen_in_bag)
print("for a total value of %i and a total weight of %i" % (val, -wgt))