|
| 1 | +# Function to solve 0/1 Knapsack problem with backtracking |
| 2 | +def knapsack_backtracking(weights,values,capacity,n): |
| 3 | + stack=[(0,0,0,[])] |
| 4 | + best_value=0 |
| 5 | + best_items=[] |
| 6 | + while stack: |
| 7 | + current_index,current_weight,current_value,included_items=stack.pop() |
| 8 | + if current_index==n: |
| 9 | + if current_value>best_value: |
| 10 | + best_value=current_value |
| 11 | + best_items=included_items[:] |
| 12 | + continue |
| 13 | + stack.append((current_index+1,current_weight,current_value,included_items[:])) |
| 14 | + if current_weight+weights[current_index]<=capacity: |
| 15 | + new_list=included_items[:] |
| 16 | + new_list.append(current_index) |
| 17 | + stack.append((current_index+1,current_weight+weights[current_index],current_value+values |
| 18 | + [current_index],new_list)) |
| 19 | + print("Items included (0-indexed):", best_items) |
| 20 | + return best_value |
| 21 | +n=int(input('Enter number of items:')) |
| 22 | +weights=[] |
| 23 | +values=[] |
| 24 | +for i in range(n): |
| 25 | + x=int(input(f"Enter weight of item-{i}:")) |
| 26 | + weights.append(x) |
| 27 | + y=int(input(f"Enter profit of item-{i}:")) |
| 28 | + values.append(y) |
| 29 | +print('weights:',weights) |
| 30 | +print('Values/profits:',values) |
| 31 | +capacity=int(input('Enter knapsack capacity:')) |
| 32 | +max_value=knapsack_backtracking(weights,values,capacity,n) |
| 33 | +print('The max profit is:',max_value) |
| 34 | + if current_weight+weights[current_index]<=capacity: |
| 35 | + new_list=included_items[:] |
| 36 | + new_list.append(current_index) |
| 37 | + |
| 38 | +stack.append((current_index+1,current_weight+weights[current_index],current_value+values |
| 39 | + [current_index],new_list)) |
| 40 | + print("Items included (0-indexed):", best_items) |
| 41 | + return best_value |
| 42 | +n=int(input('Enter number of items:')) |
| 43 | +weights=[] |
| 44 | +values=[] |
| 45 | +for i in range(n): |
| 46 | + x=int(input(f"Enter weight of item-{i}:")) |
| 47 | + weights.append(x) |
| 48 | + y=int(input(f"Enter profit of item-{i}:")) |
| 49 | + values.append(y) |
| 50 | +print('weights:',weights) |
| 51 | +print('Values/profits:',values) |
| 52 | +capacity=int(input('Enter knapsack capacity:')) |
| 53 | +max_value=knapsack_backtracking(weights,values,capacity,n) |
| 54 | +print('The max profit is:',max_value) |
| 55 | + if current_weight+weights[current_index]<=capacity: |
| 56 | + new_list=included_items[:] |
| 57 | + new_list.append(current_index) |
| 58 | + |
| 59 | +stack.append((current_index+1,current_weight+weights[current_index],current_value+values |
| 60 | + [current_index],new_list)) |
| 61 | + print("Items included (0-indexed):", best_items) |
| 62 | + return best_value |
| 63 | +n=int(input('Enter number of items:')) |
| 64 | +weights=[] |
| 65 | +values=[] |
| 66 | +for i in range(n): |
| 67 | + x=int(input(f"Enter weight of item-{i}:")) |
| 68 | + weights.append(x) |
| 69 | + y=int(input(f"Enter profit of item-{i}:")) |
| 70 | + values.append(y) |
| 71 | +print('weights:',weights) |
| 72 | +print('Values/profits:',values) |
| 73 | +capacity=int(input('Enter knapsack capacity:')) |
| 74 | +max_value=knapsack_backtracking(weights,values,capacity,n) |
| 75 | +print('The max profit is:',max_value) |
| 76 | + if current_weight+weights[current_index]<=capacity: |
| 77 | + new_list=included_items[:] |
| 78 | + new_list.append(current_index) |
| 79 | + |
| 80 | +stack.append((current_index+1,current_weight+weights[current_index],current_value+values |
| 81 | + [current_index],new_list)) |
| 82 | + print("Items included (0-indexed):", best_items) |
| 83 | + return best_value |
| 84 | +n=int(input('Enter number of items:')) |
| 85 | +weights=[] |
| 86 | +values=[] |
| 87 | +for i in range(n): |
| 88 | + x=int(input(f"Enter weight of item-{i}:")) |
| 89 | + weights.append(x) |
| 90 | + y=int(input(f"Enter profit of item-{i}:")) |
| 91 | + values.append(y) |
| 92 | +print('weights:',weights) |
| 93 | +print('Values/profits:',values) |
| 94 | +capacity=int(input('Enter knapsack capacity:')) |
| 95 | +max_value=knapsack_backtracking(weights,values,capacity,n) |
| 96 | +print('The max profit is:',max_value) |
| 97 | + if current_weight+weights[current_index]<=capacity: |
| 98 | + new_list=included_items[:] |
| 99 | + new_list.append(current_index) |
| 100 | + |
| 101 | +stack.append((current_index+1,current_weight+weights[current_index],current_value+values |
| 102 | + [current_index],new_list)) |
| 103 | + print("Items included (0-indexed):", best_items) |
| 104 | + return best_value |
| 105 | +n=int(input('Enter number of items:')) |
| 106 | +weights=[] |
| 107 | +values=[] |
| 108 | +for i in range(n): |
| 109 | + x=int(input(f"Enter weight of item-{i}:")) |
| 110 | + weights.append(x) |
| 111 | + y=int(input(f"Enter profit of item-{i}:")) |
| 112 | + values.append(y) |
| 113 | +print('weights:',weights) |
| 114 | +print('Values/profits:',values) |
| 115 | +capacity=int(input('Enter knapsack capacity:')) |
| 116 | +max_value=knapsack_backtracking(weights,values,capacity,n) |
| 117 | +print('The max profit is:',max_value) |
| 118 | +# https://condor.depaul.edu/ichu/csc491/notes/wk8/knapsack.html#:~:text=Bounding%20function%20is%20needed%20to,and%20any%20of%20its%20descendants. |
0 commit comments