0% found this document useful (0 votes)
3 views7 pages

AADA Expt-8

The document outlines an experiment on implementing the 0/1 knapsack problem using backtracking and branch & bound approaches at Somaiya Vidyavihar University. It details objectives, expected outcomes, related theories, implementation codes, and a conclusion comparing the efficiency of both methods. The branch & bound method is highlighted as more efficient for larger datasets due to its ability to prune non-promising branches.

Uploaded by

VIDIT SHAH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views7 pages

AADA Expt-8

The document outlines an experiment on implementing the 0/1 knapsack problem using backtracking and branch & bound approaches at Somaiya Vidyavihar University. It details objectives, expected outcomes, related theories, implementation codes, and a conclusion comparing the efficiency of both methods. The branch & bound method is highlighted as more efficient for larger datasets due to its ability to prune non-promising branches.

Uploaded by

VIDIT SHAH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Somaiya Vidyavihar University

(Constituent College – K J Somaiya College of Engineering)

Batch: A Roll No.:16030724019

Experiment/assignment / tutorial
No. 8

Grade: AA / AB / BB / BC / CC / CD /DD

Signature of the Staff In-charge with date

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.

Expected Outcome of Experiment:

CO .

Expected Outcomes of the Experiment (Short Points)


1. Optimal Solutions:
• Successfully derive the maximum value that can be carried in the knapsack for
given weights and values using both backtracking and branch and bound
approaches.
2. Performance Comparison:
• Analyze and compare the execution times and efficiencies of the backtracking
and branch and bound methods, showcasing the efficiency of branch and bound
for larger datasets.

Department of Computer Engineering


Page No M.Tech.Comp CLab-1 Sem I / Aug 2024
Somaiya Vidyavihar University
(Constituent College – K J Somaiya College of Engineering)

3. Space Complexity Analysis:


• Examine the space utilization of both approaches, noting that backtracking may
use more space due to recursive calls, while branch and bound's priority queue
may be more efficient.
4. Algorithm Effectiveness:
• Confirm that both algorithms yield correct results, demonstrating the robustness
of each method in solving the 0/1 knapsack problem.

Books/ Papers/Websites referred:

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/

Pre Lab/ Prior Concepts:

1. Basic Problem Definition:


• Understanding the 0/1 knapsack problem, where given a set of items, each with a
weight and a value, the goal is to maximize the total value of items that can be included
in a knapsack of fixed capacity, with the constraint that each item can either be
included or excluded (0 or 1).
2. Dynamic Programming:
• Familiarity with dynamic programming principles, particularly how they can be applied
to optimization problems.
• Understanding overlapping subproblems and optimal substructure properties that
characterize the knapsack problem.
3. Backtracking Technique:
• Knowledge of backtracking as a systematic way to explore possible solutions by
making choices, and understanding how to prune the search space to avoid unnecessary
calculations.
4. Branch and Bound Method:
• Understanding the branch and bound approach, including how it systematically
explores feasible solutions while using bounds to prune non-promising branches.

Department of Computer Engineering


Page No M.Tech.Comp CLab-1 Sem I / Aug 2024
Somaiya Vidyavihar University
(Constituent College – K J Somaiya College of Engineering)

• 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:

Department of Computer Engineering


Page No M.Tech.Comp CLab-1 Sem I / Aug 2024
Somaiya Vidyavihar University
(Constituent College – K J Somaiya College of Engineering)

• 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

# If weight of the nth item is more than the current capacity,


exclude it
if weights[n - 1] > capacity:
return knapsack(weights, values, capacity, n - 1)
else:

include_item = values[n - 1] + knapsack(weights, values, capacity


- weights[n - 1], n - 1)
exclude_item = knapsack(weights, values, capacity, n - 1)
# Return the maximum of including or excluding the item
return max(include_item, exclude_item)

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()

Department of Computer Engineering


Page No M.Tech.Comp CLab-1 Sem I / Aug 2024
Somaiya Vidyavihar University
(Constituent College – K J Somaiya College of Engineering)
Output:
PS D:\notes\College\M.Tech\sem-1\AADA\practical> python -u
Maximum value in the knapsack: 7

2. Using Branch And Bound

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

def calculate_bound(u, capacity, n, items):


if u.weight >= capacity:
return 0

profit_bound = u.profit
j = u.level + 1
total_weight = u.weight

# Accumulate profit while total weight is within capacity


while j < n and total_weight + items[j].weight <= capacity:
total_weight += items[j].weight
profit_bound += items[j].value
j += 1

# If there's room left, add fraction of the next item


if j < n:
profit_bound += (capacity - total_weight) * (items[j].value /
items[j].weight)

return profit_bound

def knapsack_branch_and_bound(capacity, items):


# Sort items by value-to-weight ratio in descending order

Department of Computer Engineering


Page No M.Tech.Comp CLab-1 Sem I / Aug 2024
Somaiya Vidyavihar University
(Constituent College – K J Somaiya College of Engineering)

items.sort(key=lambda x: x.value / x.weight, reverse=True)

# Initialize the queue with the root node


queue = Queue()
root = Node(level=-1, profit=0, weight=0,
bound=calculate_bound(Node(-1, 0, 0, 0), capacity, len(items), items))
queue.put(root)

max_profit = 0

# Process the nodes in the queue


while not queue.empty():
u = queue.get()

if u.level == -1:
u.level = 0

if u.level == len(items) - 1:
continue

# Node v when including the next item


v = Node(level=u.level + 1,
profit=u.profit + items[u.level + 1].value,
weight=u.weight + items[u.level + 1].weight,
bound=0)

if v.weight <= capacity and v.profit > max_profit:


max_profit = v.profit

v.bound = calculate_bound(v, capacity, len(items), items)


if v.bound > max_profit:
queue.put(v)

# Node v when excluding the next item


v = Node(level=u.level + 1,
profit=u.profit,
weight=u.weight,
bound=calculate_bound(u, capacity, len(items), items))

if v.bound > max_profit:


queue.put(v)

return max_profit

def main():
items = [Item(2, 3), Item(3, 4), Item(4, 5), Item(5, 6)]

Department of Computer Engineering


Page No M.Tech.Comp CLab-1 Sem I / Aug 2024
Somaiya Vidyavihar University
(Constituent College – K J Somaiya College of Engineering)

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.

Date: Signature of faculty in-charge

Department of Computer Engineering


Page No M.Tech.Comp CLab-1 Sem I / Aug 2024

You might also like