Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 118 additions & 0 deletions backtracking/knapsack01
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# Function to solve 0/1 Knapsack problem with backtracking
def knapsack_backtracking(weights,values,capacity,n):
stack=[(0,0,0,[])]
best_value=0
best_items=[]
while stack:
current_index,current_weight,current_value,included_items=stack.pop()
if current_index==n:
if current_value>best_value:
best_value=current_value
best_items=included_items[:]
continue
stack.append((current_index+1,current_weight,current_value,included_items[:]))
if current_weight+weights[current_index]<=capacity:
new_list=included_items[:]
new_list.append(current_index)
stack.append((current_index+1,current_weight+weights[current_index],current_value+values
[current_index],new_list))
print("Items included (0-indexed):", best_items)
return best_value
n=int(input('Enter number of items:'))
weights=[]
values=[]
for i in range(n):
x=int(input(f"Enter weight of item-{i}:"))
weights.append(x)
y=int(input(f"Enter profit of item-{i}:"))
values.append(y)
print('weights:',weights)
print('Values/profits:',values)
capacity=int(input('Enter knapsack capacity:'))
max_value=knapsack_backtracking(weights,values,capacity,n)
print('The max profit is:',max_value)
if current_weight+weights[current_index]<=capacity:
new_list=included_items[:]
new_list.append(current_index)

stack.append((current_index+1,current_weight+weights[current_index],current_value+values
[current_index],new_list))
print("Items included (0-indexed):", best_items)
return best_value
n=int(input('Enter number of items:'))
weights=[]
values=[]
for i in range(n):
x=int(input(f"Enter weight of item-{i}:"))
weights.append(x)
y=int(input(f"Enter profit of item-{i}:"))
values.append(y)
print('weights:',weights)
print('Values/profits:',values)
capacity=int(input('Enter knapsack capacity:'))
max_value=knapsack_backtracking(weights,values,capacity,n)
print('The max profit is:',max_value)
if current_weight+weights[current_index]<=capacity:
new_list=included_items[:]
new_list.append(current_index)

stack.append((current_index+1,current_weight+weights[current_index],current_value+values
[current_index],new_list))
print("Items included (0-indexed):", best_items)
return best_value
n=int(input('Enter number of items:'))
weights=[]
values=[]
for i in range(n):
x=int(input(f"Enter weight of item-{i}:"))
weights.append(x)
y=int(input(f"Enter profit of item-{i}:"))
values.append(y)
print('weights:',weights)
print('Values/profits:',values)
capacity=int(input('Enter knapsack capacity:'))
max_value=knapsack_backtracking(weights,values,capacity,n)
print('The max profit is:',max_value)
if current_weight+weights[current_index]<=capacity:
new_list=included_items[:]
new_list.append(current_index)

stack.append((current_index+1,current_weight+weights[current_index],current_value+values
[current_index],new_list))
print("Items included (0-indexed):", best_items)
return best_value
n=int(input('Enter number of items:'))
weights=[]
values=[]
for i in range(n):
x=int(input(f"Enter weight of item-{i}:"))
weights.append(x)
y=int(input(f"Enter profit of item-{i}:"))
values.append(y)
print('weights:',weights)
print('Values/profits:',values)
capacity=int(input('Enter knapsack capacity:'))
max_value=knapsack_backtracking(weights,values,capacity,n)
print('The max profit is:',max_value)
if current_weight+weights[current_index]<=capacity:
new_list=included_items[:]
new_list.append(current_index)

stack.append((current_index+1,current_weight+weights[current_index],current_value+values
[current_index],new_list))
print("Items included (0-indexed):", best_items)
return best_value
n=int(input('Enter number of items:'))
weights=[]
values=[]
for i in range(n):
x=int(input(f"Enter weight of item-{i}:"))
weights.append(x)
y=int(input(f"Enter profit of item-{i}:"))
values.append(y)
print('weights:',weights)
print('Values/profits:',values)
capacity=int(input('Enter knapsack capacity:'))
max_value=knapsack_backtracking(weights,values,capacity,n)
print('The max profit is:',max_value)
# https://condor.depaul.edu/ichu/csc491/notes/wk8/knapsack.html#:~:text=Bounding%20function%20is%20needed%20to,and%20any%20of%20its%20descendants.