SlideShare a Scribd company logo
2
Most read
3
Most read
9
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
0 1 knapsack using branch and bound
PPT
SEARCHING AND SORTING ALGORITHMS
PPTX
Daa:Dynamic Programing
PPT
Knapsack problem using dynamic programming
PPTX
Alpha beta pruning in ai
PPTX
Np hard
PPT
finding Min and max element from given array using divide & conquer
PPT
Heap Sort || Heapify Method || Build Max Heap Algorithm
0 1 knapsack using branch and bound
SEARCHING AND SORTING ALGORITHMS
Daa:Dynamic Programing
Knapsack problem using dynamic programming
Alpha beta pruning in ai
Np hard
finding Min and max element from given array using divide & conquer
Heap Sort || Heapify Method || Build Max Heap Algorithm

What's hot (20)

PPTX
What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...
PPTX
NP completeness
PDF
I. Alpha-Beta Pruning in ai
PPTX
Greedy Algorithm - Knapsack Problem
PPT
Branch and bound
PPT
Greedy Algorihm
PPTX
The n Queen Problem
PDF
Dynamic programming
PPT
KNOWLEDGE REPRESENTATION ISSUES.ppt
PDF
linear classification
PPTX
Alpha-beta pruning (Artificial Intelligence)
PPTX
Design and Analysis of Algorithms
PPTX
Greedy algorithms
PPTX
Approximation algorithms
PPT
PPT
Prim's Algorithm on minimum spanning tree
PPTX
Algorithm analysis (All in one)
PPTX
10.m way search tree
PPT
0/1 knapsack
What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...
NP completeness
I. Alpha-Beta Pruning in ai
Greedy Algorithm - Knapsack Problem
Branch and bound
Greedy Algorihm
The n Queen Problem
Dynamic programming
KNOWLEDGE REPRESENTATION ISSUES.ppt
linear classification
Alpha-beta pruning (Artificial Intelligence)
Design and Analysis of Algorithms
Greedy algorithms
Approximation algorithms
Prim's Algorithm on minimum spanning tree
Algorithm analysis (All in one)
10.m way search tree
0/1 knapsack
Ad

Similar to knapsackusingbranchandbound (20)

PPTX
Data structure and algorithm
DOCX
Homework4 b00346910
PPTX
0-1 Knapsack Problem: Optimizing Value with Constraints
PPTX
daa-unit-3-greedy method
PPT
Knapsack problem and Memory Function
PPTX
Knapsack problem using Greedy method.pptx
PPTX
0 1 knapsack using naive recursive approach and top-down dynamic programming ...
PPT
Knapsack Problem Analysis of Algorithm.ppt
PPTX
Knapsack problem algorithm, greedy algorithm
PPTX
5b.Greedy Technique - Fractional Knapsack+Coin change Problem.pptx
PPTX
Fractional Knapsack Problem
DOC
Data structure notes
PPT
Elak3 need of greedy for design and analysis of algorithms.ppt
PPT
Knapsack problem
PPTX
Knapsack problem using greedy approach
PDF
12 Greeddy Method
PPTX
Knapsack
PPTX
Fractional knapsack problem
PPT
PPTX
THE GREEDY METHOD notes related to education.pptx
Data structure and algorithm
Homework4 b00346910
0-1 Knapsack Problem: Optimizing Value with Constraints
daa-unit-3-greedy method
Knapsack problem and Memory Function
Knapsack problem using Greedy method.pptx
0 1 knapsack using naive recursive approach and top-down dynamic programming ...
Knapsack Problem Analysis of Algorithm.ppt
Knapsack problem algorithm, greedy algorithm
5b.Greedy Technique - Fractional Knapsack+Coin change Problem.pptx
Fractional Knapsack Problem
Data structure notes
Elak3 need of greedy for design and analysis of algorithms.ppt
Knapsack problem
Knapsack problem using greedy approach
12 Greeddy Method
Knapsack
Fractional knapsack problem
THE GREEDY METHOD notes related to education.pptx
Ad

Recently uploaded (20)

PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
Geodesy 1.pptx...............................................
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
Internet of Things (IOT) - A guide to understanding
DOCX
573137875-Attendance-Management-System-original
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPT
Mechanical Engineering MATERIALS Selection
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
additive manufacturing of ss316l using mig welding
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Embodied AI: Ushering in the Next Era of Intelligent Systems
Foundation to blockchain - A guide to Blockchain Tech
UNIT 4 Total Quality Management .pptx
Geodesy 1.pptx...............................................
Operating System & Kernel Study Guide-1 - converted.pdf
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Internet of Things (IOT) - A guide to understanding
573137875-Attendance-Management-System-original
CYBER-CRIMES AND SECURITY A guide to understanding
Mechanical Engineering MATERIALS Selection
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
OOP with Java - Java Introduction (Basics)
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
additive manufacturing of ss316l using mig welding
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Model Code of Practice - Construction Work - 21102022 .pdf

knapsackusingbranchandbound

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