-
Notifications
You must be signed in to change notification settings - Fork 0
/
BruteForce.py
41 lines (31 loc) · 1.09 KB
/
BruteForce.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
import time
import random
def bruteKnapsack(Capacity, weights, values, numItems) -> int:
# Base Case
if numItems == 0 or Capacity == 0:
return 0
if (weights[numItems - 1] > Capacity):
return bruteKnapsack(Capacity, weights, values, numItems - 1)
else:
value = max(values[numItems - 1] + bruteKnapsack(Capacity - weights[numItems - 1], weights, values, numItems - 1),
bruteKnapsack(Capacity, weights, values, numItems - 1))
return value
def printValue(value):
print("Value of items in the knapsack =", value)
Capacity = 300
numItems = 10
Weights = {}
Values = {}
i = 0
for i in range(numItems):
Weights[i] = random.randint(10, 100)
Values[i] = random.randint(10, 100)
valueWeightRatio = []
for index in range(numItems):
valueWeightRatio.append(Values[index] / Weights[index])
start_time = time.time()
print("\nBrute Force Approach")
maxValue = bruteKnapsack(Capacity, Weights, Values, numItems)
printValue(maxValue)
end_time = time.time()
print('time needed: ', end_time - start_time)