SlideShare a Scribd company logo
7
Most read
9
Most read
20
Most read
0/1 Knapsack Problem
(using BRANCH & BOUND)
Presented by
41.ABHISHEK KUMAR SINGH
0/1 Knapsack Problem
Given two integer arrays val[0..n-1] and wt[0..n-1] that represent values and weights
associated with n items respectively.
Find out the maximum value subset of val[] such that sum of the weights of this
subset is smaller than or equal to Knapsack capacity W.
We have ‘n’ items with value v1 , v2 . . . vn and weight of the corresponding items
is w1 , w2 . . . Wn .
Max capacity is W .
We can either choose or not choose an item. We have x1 , x2 . . . xn.
Here xi = { 1 , 0 }.
xi = 1 , item chosen
xi = 0 , item not chosen
Different approaches of this problem :
 Dynamic programming
 Brute force
 Backtracking
 Branch and bound
Why branch and bound ?
 Greedy approach works only for fractional knapsack problem.
 If weights are not integers , dynamic programming will not work.
 There are 2n possible combinations of item , complexity for brute force
goes exponentially.
What is branch and bound ?
 Branch and bound is an algorithm design paradigm which is
generally used for solving combinatorial optimization
problems.
 These problems typically exponential in terms of time
complexity and may require exploring all possible
permutations in worst case.
 Branch and Bound solve these problems relatively quickly.
 Combinatorial optimization problem is an
optimization problem, where an optimal solution
has to be identified from a finite set of solutions.
 Sort all items in decreasing order of ratio of value per unit
weight so that an upper bound can be computed using Greedy
Approach.
 Initialize maximum profit, maxProfit = 0
 Create an empty queue, Q.
 Create a dummy node of decision tree and enqueue it to Q.
Profit and weight of dummy node are 0.

ALGORITHM
 Do following while Q is not empty.

 Extract an item from Q. Let the extracted item be u.
 Compute profit of next level node. If the profit is more than maxProfit, then update
maxProfit.
 Compute bound of next level node. If bound is more than maxProfit, then add next
level node to Q.
 Consider the case when next level node is not considered as part of solution and
add a node to queue with level as next, but weight and profit without considering
next level nodes.

EXAMPLE :
$90
12
$98
Max weight = 16
maxProfit = 0
$ 0
0
$115
$90
12
$98
$115
2
$40
Max weight = 16
maxProfit = 40
$90
12
$98
Max weight = 16
maxProfit = 40
$ 0
0
$82
$90
12
$98
Max weight = 16
maxProfit = 70
$70
7
$115
$90
12
$98
2
$40
$98
Max weight = 16
maxProfit = 70
$90
12
$98
Max weight = 16
maxProfit = 70
$30
5
$82
$90
12
$98
Max weight = 16
maxProfit = 70
$ 0
0
$60
$90
12
$98
Max weight = 16
maxProfit = 70
$120
17
$0
$90
12
$98
Max weight = 16
maxProfit = 70
$70
7
$80
Max weight = 16
maxProfit = 90
struct Item
{
float weight;
int value;
}
Node structure to store information of decision tree
struct Node
{
int level, profit, bound;
float weight;
// level ---> Level of node in decision tree (or index ) in arr[]
// profit ---> Profit of nodes on path from root to this node (including this node)
// bound ---> Upper bound of maximum profit in subtree of this node
}
Data items used in the Algorithm :
knapsack(int W, Item arr[], int n)
queue<Node> Q
Node u, v //u.level = -1
Q.push(u) //u.profit = u.weight = 0
while ( !Q.empty() ) //int maxProfit = 0
u = Q.front() & Q.pop()
v.level = u.level + 1 // selecting the item
v.weight = u.weight + arr[v.level].weight
v.profit = u.profit + arr[v.level].value
if (v.weight <= W && v.profit > maxProfit)
maxProfit = v.profit
v.bound = bound(v, n, W, arr)
if (v.bound > maxProfit)
Q.push(v)
v.weight = u.weight // not selecting the item
v.profit = u.profit
v.bound = bound(v, n, W, arr)
If (v.bound > maxProfit)
Q.push(v)
return (maxProfit)
Algorithm for maxProfit :
bound(Node u, int n, int W, Item a[])
if (u.weight >= W)
return (0)
int u_bound <- u.profit
int j <- u.level + 1
int totweight <- u.weight
while ((j < n) && (totweight + a[j].weight <= W))
totweight <- totweight + a[j].weight
u_bound <- u_bound + a[j].value
j++
if (j < n)
u_bound <- u_bound + ( W - totweight ) * a[j].value /a[j].weight
return (u_bound)
Procedure to calculate upper bound :
THANK
YOU

More Related Content

PPTX
PPT
User Interface Design in Software Engineering SE15
PPTX
Tree - Data Structure
PPTX
Theory of automata and formal language
PDF
Artificial Intelligence Notes Unit 2
PPT
Active directory
PPT
Solving problems by searching
PPTX
Introduction to Linear Discriminant Analysis
User Interface Design in Software Engineering SE15
Tree - Data Structure
Theory of automata and formal language
Artificial Intelligence Notes Unit 2
Active directory
Solving problems by searching
Introduction to Linear Discriminant Analysis

What's hot (20)

PPTX
Knapsack problem using greedy approach
PPT
Greedy Algorihm
PPTX
The n Queen Problem
PPTX
Greedy Algorithm - Knapsack Problem
PPTX
Travelling salesman dynamic programming
PPTX
Travelling Salesman
PPTX
Greedy algorithms
PPT
BackTracking Algorithm: Technique and Examples
PPTX
Sum of subset problem.pptx
PPTX
Knapsack Problem
PPTX
N queen problem
PPTX
sum of subset problem using Backtracking
PPT
Graph coloring problem
PPT
Binary Search
PPT
Sum of subsets problem by backtracking 
PDF
Symbol table in compiler Design
PPTX
8 queens problem using back tracking
DOC
Time and space complexity
PPTX
Problem reduction AND OR GRAPH & AO* algorithm.ppt
PPTX
Process synchronization in Operating Systems
Knapsack problem using greedy approach
Greedy Algorihm
The n Queen Problem
Greedy Algorithm - Knapsack Problem
Travelling salesman dynamic programming
Travelling Salesman
Greedy algorithms
BackTracking Algorithm: Technique and Examples
Sum of subset problem.pptx
Knapsack Problem
N queen problem
sum of subset problem using Backtracking
Graph coloring problem
Binary Search
Sum of subsets problem by backtracking 
Symbol table in compiler Design
8 queens problem using back tracking
Time and space complexity
Problem reduction AND OR GRAPH & AO* algorithm.ppt
Process synchronization in Operating Systems
Ad

Similar to 0 1 knapsack using branch and bound (20)

PPTX
5b.Greedy Technique - Fractional Knapsack+Coin change Problem.pptx
PPTX
0 1 knapsack using naive recursive approach and top-down dynamic programming ...
PPTX
Fractional Knapsack Problem
PPTX
Knapsack Problem (DP & GREEDY)
PPT
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
PPTX
Unit 3- Greedy Method.pptx
PPT
Optimization problems
PPT
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
PPT
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
PPT
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
PPTX
0-1 Knapsack Problem: Optimizing Value with Constraints
PDF
Knapsack dp
PPT
4.Support Vector Machines.ppt machine learning and development
PPT
PERFORMANCE EVALUATION PARAMETERS FOR MACHINE LEARNING
PDF
Unit 3 greedy method
PDF
Unit 3 - Greedy Method
PPTX
Module 3_DAA (2).pptx
PPT
Unit 3-Greedy Method
PPTX
Knapsack Dynamic
PPTX
Knapsack problem algorithm, greedy algorithm
5b.Greedy Technique - Fractional Knapsack+Coin change Problem.pptx
0 1 knapsack using naive recursive approach and top-down dynamic programming ...
Fractional Knapsack Problem
Knapsack Problem (DP & GREEDY)
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Unit 3- Greedy Method.pptx
Optimization problems
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
0-1 Knapsack Problem: Optimizing Value with Constraints
Knapsack dp
4.Support Vector Machines.ppt machine learning and development
PERFORMANCE EVALUATION PARAMETERS FOR MACHINE LEARNING
Unit 3 greedy method
Unit 3 - Greedy Method
Module 3_DAA (2).pptx
Unit 3-Greedy Method
Knapsack Dynamic
Knapsack problem algorithm, greedy algorithm
Ad

More from Abhishek Singh (6)

PPTX
Knights tour on chessboard using backtracking
PPTX
RABIN KARP ALGORITHM STRING MATCHING
PPTX
Naive string matching
PPTX
15 puzzle problem using branch and bound
PPTX
kmp_algorithm (string matching)
PPTX
Knights tour on chessboard using backtracking
RABIN KARP ALGORITHM STRING MATCHING
Naive string matching
15 puzzle problem using branch and bound
kmp_algorithm (string matching)

Recently uploaded (20)

PPTX
Sustainable Sites - Green Building Construction
PDF
Digital Logic Computer Design lecture notes
DOCX
573137875-Attendance-Management-System-original
PPTX
Lecture Notes Electrical Wiring System Components
PDF
Well-logging-methods_new................
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
Welding lecture in detail for understanding
PDF
PPT on Performance Review to get promotions
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
UNIT 4 Total Quality Management .pptx
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
Sustainable Sites - Green Building Construction
Digital Logic Computer Design lecture notes
573137875-Attendance-Management-System-original
Lecture Notes Electrical Wiring System Components
Well-logging-methods_new................
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Welding lecture in detail for understanding
PPT on Performance Review to get promotions
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Lesson 3_Tessellation.pptx finite Mathematics
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
UNIT 4 Total Quality Management .pptx
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
CYBER-CRIMES AND SECURITY A guide to understanding
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Strings in CPP - Strings in C++ are sequences of characters used to store and...

0 1 knapsack using branch and bound

  • 1. 0/1 Knapsack Problem (using BRANCH & BOUND) Presented by 41.ABHISHEK KUMAR SINGH
  • 2. 0/1 Knapsack Problem Given two integer arrays val[0..n-1] and wt[0..n-1] that represent values and weights associated with n items respectively. Find out the maximum value subset of val[] such that sum of the weights of this subset is smaller than or equal to Knapsack capacity W. We have ‘n’ items with value v1 , v2 . . . vn and weight of the corresponding items is w1 , w2 . . . Wn . Max capacity is W . We can either choose or not choose an item. We have x1 , x2 . . . xn. Here xi = { 1 , 0 }. xi = 1 , item chosen xi = 0 , item not chosen
  • 3. Different approaches of this problem :  Dynamic programming  Brute force  Backtracking  Branch and bound
  • 4. Why branch and bound ?  Greedy approach works only for fractional knapsack problem.  If weights are not integers , dynamic programming will not work.  There are 2n possible combinations of item , complexity for brute force goes exponentially.
  • 5. What is branch and bound ?  Branch and bound is an algorithm design paradigm which is generally used for solving combinatorial optimization problems.  These problems typically exponential in terms of time complexity and may require exploring all possible permutations in worst case.  Branch and Bound solve these problems relatively quickly.
  • 6.  Combinatorial optimization problem is an optimization problem, where an optimal solution has to be identified from a finite set of solutions.
  • 7.  Sort all items in decreasing order of ratio of value per unit weight so that an upper bound can be computed using Greedy Approach.  Initialize maximum profit, maxProfit = 0  Create an empty queue, Q.  Create a dummy node of decision tree and enqueue it to Q. Profit and weight of dummy node are 0.  ALGORITHM
  • 8.  Do following while Q is not empty.   Extract an item from Q. Let the extracted item be u.  Compute profit of next level node. If the profit is more than maxProfit, then update maxProfit.  Compute bound of next level node. If bound is more than maxProfit, then add next level node to Q.  Consider the case when next level node is not considered as part of solution and add a node to queue with level as next, but weight and profit without considering next level nodes.  EXAMPLE :
  • 9. $90 12 $98 Max weight = 16 maxProfit = 0 $ 0 0 $115
  • 11. $90 12 $98 Max weight = 16 maxProfit = 40 $ 0 0 $82
  • 12. $90 12 $98 Max weight = 16 maxProfit = 70 $70 7 $115
  • 14. $90 12 $98 Max weight = 16 maxProfit = 70 $30 5 $82
  • 15. $90 12 $98 Max weight = 16 maxProfit = 70 $ 0 0 $60
  • 16. $90 12 $98 Max weight = 16 maxProfit = 70 $120 17 $0
  • 17. $90 12 $98 Max weight = 16 maxProfit = 70 $70 7 $80
  • 18. Max weight = 16 maxProfit = 90
  • 19. struct Item { float weight; int value; } Node structure to store information of decision tree struct Node { int level, profit, bound; float weight; // level ---> Level of node in decision tree (or index ) in arr[] // profit ---> Profit of nodes on path from root to this node (including this node) // bound ---> Upper bound of maximum profit in subtree of this node } Data items used in the Algorithm :
  • 20. knapsack(int W, Item arr[], int n) queue<Node> Q Node u, v //u.level = -1 Q.push(u) //u.profit = u.weight = 0 while ( !Q.empty() ) //int maxProfit = 0 u = Q.front() & Q.pop() v.level = u.level + 1 // selecting the item v.weight = u.weight + arr[v.level].weight v.profit = u.profit + arr[v.level].value if (v.weight <= W && v.profit > maxProfit) maxProfit = v.profit v.bound = bound(v, n, W, arr) if (v.bound > maxProfit) Q.push(v) v.weight = u.weight // not selecting the item v.profit = u.profit v.bound = bound(v, n, W, arr) If (v.bound > maxProfit) Q.push(v) return (maxProfit) Algorithm for maxProfit :
  • 21. bound(Node u, int n, int W, Item a[]) if (u.weight >= W) return (0) int u_bound <- u.profit int j <- u.level + 1 int totweight <- u.weight while ((j < n) && (totweight + a[j].weight <= W)) totweight <- totweight + a[j].weight u_bound <- u_bound + a[j].value j++ if (j < n) u_bound <- u_bound + ( W - totweight ) * a[j].value /a[j].weight return (u_bound) Procedure to calculate upper bound :