-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathknapsack.py
More file actions
44 lines (35 loc) · 1.15 KB
/
knapsack.py
File metadata and controls
44 lines (35 loc) · 1.15 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
from math import inf
from time import perf_counter
from random import randrange
import sys
class KnapSack:
def __init__(self, items):
self.n = items
self.wt = [0] * items
self.val = [0] * items
total_weight = 0
for i in range(items):
self.wt[i] = randrange(1, 100)
self.val[i] = randrange(1, 100)
total_weight += self.wt[i]
self.W = int(total_weight / 2)
def slow_thief(self):
self._knapsack_slow(self.W, self.n)
def _knapsack_slow(self, capacity, n):
if n == 0 or self.W == 0:
return 0
if self.wt[n-1] > capacity:
return self._knapsack_slow(capacity, n-1)
else:
return max(self.val[n-1] + self._knapsack_slow(capacity - self.wt[n-1], n-1),
self._knapsack_slow(capacity, n-1))
def main():
for i in range(2, 100):
thief = KnapSack(i)
start = perf_counter()
thief.slow_thief()
stop = perf_counter()
print(f"Elapsed time in seconds for a rod of length {i} : {stop-start}")
if __name__ == "__main__":
sys.setrecursionlimit(10000)
main()