AADA Expt-8
AADA Expt-8
Experiment/assignment / tutorial
No. 8
Grade: AA / AB / BB / BC / CC / CD /DD
Experiment No.: 8
Title: To implement 0/1 knapsack problem using backtracking and branch & bound
approach
Objectives:
1. Understanding Dynamic Programming Concepts:
• Grasp the fundamental principles of dynamic programming and how they apply
to optimization problems like the knapsack problem.
2. Exploring Backtracking Technique:
• Implement a backtracking approach to solve the 0/1 knapsack problem,
enabling an understanding of how to explore all possible combinations of items
to determine the optimal solution.
3. Applying Branch and Bound Method:
• Utilize the branch and bound technique to efficiently solve the 0/1 knapsack
problem by systematically exploring feasible solutions while avoiding
unnecessary computations.
CO .
1. https://www.geeksforgeeks.org/0-1-knapsack-using-branch-and-bound/
2. https://www.javatpoint.com/0-1-knapsack-problem
3. https://techfincast.in/knapsack-problem-in-daa/
• Familiarity with priority queues, which are often used to manage nodes in branch and
bound algorithms based on their potential value.
Related Theory:
1. Problem Statement:
• Given a set of items, each with a weight and a value, the goal is to
determine the maximum value that can be accommodated in a knapsack of
limited capacity, with the constraint that each item can either be included or
excluded.
2. Dynamic Programming Approach:
• The problem can be solved using dynamic programming, which breaks
down the problem into simpler subproblems and stores their solutions to
avoid redundant calculations.
• The dynamic programming table (usually a 2D array) keeps track of the
maximum value that can be obtained with a certain capacity and number of
items.
3. Backtracking Technique:
• Backtracking is a brute-force approach to solving the 0/1 knapsack problem
by exploring all possible combinations of items.
• It systematically considers including or excluding each item and backtracks
when it exceeds the capacity or when all items have been considered.
4. Branch and Bound Method:
• This approach enhances the backtracking method by using bounds to
eliminate certain branches of the search space that cannot yield better
solutions than already found ones.
• It employs a priority queue to explore nodes based on their potential value,
significantly improving efficiency, especially for larger datasets.
5. Greedy Approach vs. Dynamic Programming:
• The greedy algorithm is often used for the fractional knapsack problem,
where items can be divided. However, it is not optimal for the 0/1 knapsack
problem due to its binary decision-making for each item.
Implementation details:
1. Using Backtracking
Code:
def knapsack(weights, values, capacity, n):
if n == 0 or capacity == 0:
return 0
def main():
weights = [2, 3, 4, 5]
values = [3, 4, 5, 6]
capacity = 5
n = len(values)
max_profit = knapsack(weights, values, capacity, n)
print("Maximum value in the knapsack:", max_profit)
if __name__ == "__main__":
main()
Code:
from queue import Queue
class Item:
def __init__(self, weight, value):
self.weight = weight
self.value = value
class Node:
def __init__(self, level, profit, weight, bound):
self.level = level
self.profit = profit
self.weight = weight
self.bound = bound
profit_bound = u.profit
j = u.level + 1
total_weight = u.weight
return profit_bound
max_profit = 0
if u.level == -1:
u.level = 0
if u.level == len(items) - 1:
continue
return max_profit
def main():
items = [Item(2, 3), Item(3, 4), Item(4, 5), Item(5, 6)]
capacity = 5
max_profit = knapsack_branch_and_bound(capacity, items)
print("Maximum profit:", max_profit)
if __name__ == "__main__":
main()
Output:
PS D:\notes\College\M.Tech\sem-1\AADA\practical> python -u
Maximum profit: 6
Conclusion:
The implementation of the 0/1 Knapsack problem using backtracking and branch &
bound highlights two distinct approaches for solving optimization problems.
Backtracking explores all possible combinations to guarantee the optimal solution but
can be inefficient due to its exponential time complexity. In contrast, the branch &
bound method enhances efficiency by pruning non-promising branches based on
calculated bounds, reducing the search space significantly. While both methods ensure
the best solution, branch & bound is generally more practical for larger datasets due to
its ability to avoid unnecessary computations, making it the preferred approach in such
scenarios.