-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEXP5.py
40 lines (34 loc) · 1.16 KB
/
EXP5.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
def knapSack(W, wt, val):
n = len(val)
table = [[0 for x in range(W + 1)] for x in range(n + 1)]
for i in range(n + 1):
for j in range(W + 1):
if i == 0 or j == 0:
table[i][j] = 0
elif wt[i - 1] <= j:
table[i][j] = max(val[i - 1] + table[i - 1][j - wt[i - 1]], table[i - 1][j])
else:
table[i][j] = table[i - 1][j]
# Print the full calculation table
print("Calculation Table:")
for row in table:
print(row)
# Backtrace to find the selected items
selected_items = []
i, j = n, W
while i > 0 and j > 0:
if table[i][j] != table[i - 1][j]:
selected_items.append(i - 1)
j -= wt[i - 1]
i -= 1
print("\nSelected Items:")
for item in selected_items[::-1]:
print(f"Weight: {wt[item]}, Value: {val[item]}")
return table[n][W]
print('Enter the profits:')
values = list(map(int, input().split(' ')))
print('Enter the weights:')
weights = list(map(int, input().split(' ')))
C = int(input('Enter the maximum capacity:'))
n = len(weights)
print("\nMaximum Value:", knapSack(C, weights, values))