SlideShare a Scribd company logo
17
Most read
20
Most read
21
Most read
Backtracking Technique Eg. Big Castle  –  Large Rooms &  “ Sleeping Beauty ” Systematic search - BFS, DFS Many paths led to nothing but  “ dead-ends ”   Can we avoid these  unnecessary labor ? ( bounding function  or  promising function ) DFS with bounding function (promising function) Worst-Case : Exhaustive Search
Backtracking Technique Useful because it is efficient for  “ many ”  large instances NP-Complete problems Eg. 0-1 knapsack problem D.P. :  O (min(2 n , nW)) Backtracking : can be very efficient if good bounding function(promising function) Recursion
Outline of Backtracking Approach Backtracking : DFS of a tree  except that nodes are visited if  promising DFS(Depth First Search) Procedure  d_f_s_tree ( v : node) var  u  : node { visit  v ;  // some action  // for each child  u  of  v   do  d_f_s_tree ( u ); } 1 2 3 5 7 11 4 6 8 9 10
N-Queens Problem
N-Queens Problem n ⅹ n  Chess board n -Queens Eg. 8-Queens problem Q Q Q Q 1  2  3  4  5  6  7  8 1 2 3 4 5 6 7 8
N-Queens Problem: DFS State Space Tree for 4-Queens Problem
N-Queens Problem: DFS (Same Col/Row x) State Space Tree for 4-Queens Problem X 1 =1 X 2 =2 3 4 4 3 4 3 4 2 3 2 3 2 2 4 3 2 3 4 1 4 1 3 2 4 1 3 4 1 2 4 3 2 1 2 3 4 (x 1 , x 2 , x 3 , x 4 )=(2, 4, 1, 3) #leaf nodes : n!=4! DFS :  “ Backtrack ”  at dead ends  –  4! leaf nodes (n!) Cf. Backtracking :  “ Backtrack ”  if non-promising ( pruning )
N-Queens Problem: Backtracking Promising Function(Bounding Function) (i) same row ⅹ (ii) same column ⅹ (col(i) ≠ col(k)) (iii) same diagonal ⅹ (|col(i)-col(k)|≠|i-k|) Q  (i, col(i)) Q  (k, col(k)) Q (i,col(i)) Q (k,col(k))
N-Queens Problem: Backtracking State Space Tree for 4-Queens Problem
N-Queens Problem Better (faster) Algorithm Monte Carlo Algorithm “ place almost all queens randomly,  then do remaining queens using backtracking. ” #Nodes Checked DFS   (n n )  #Nodes Checked DFS (same col/row X) (n!) #Nodes Checked Backtracking 341 19,173,961 9.73 ⅹ10 12 1.20ⅹ 10 16 24 40,320 4.79 ⅹ10 8 8.72ⅹ 10 10 61 15,721 1.01 ⅹ10 7 3.78ⅹ 10 8 4 8 12 14 n
Graph Coloring M-coloring problem: Color undirected graph with  ≤ m  colors 2 adjacent vertices : different colors (Eg) V 1 V 2 V 4 V 3 2-coloring  X 3-coloring  O
Graph Coloring State-Space Tree :  m n  leaves Promising function : check adjacent vertices  for the same color start 1 2 3 2 1 3 2 1 3 2 1 3 X X X X X
Hamiltonian Circuits Problem TSP problem in chapter 3. Brute-force: n!, (n-1)! D.P. :  When  n =20, B.F.    3,800 years D.P.   45 sec. More efficient solution?  See chapter 6 Given an undirected or directed graph, find a tour(HC).  (need not be S.P.)
Hamiltonian Circuits Problem State-Space Tree (n-1)! Leaves : worst-case Promising function: 1.  i -th node  ( i +1)-th node 2.(n-1)-th node   0-th node 3.  i -th node  ≠ 0 ~ ( i -1)-th node HC : X (Eg) V 1 V 2 V 6 V 5 V 3 V 7 V 4 V 8 V 1 V 2 V 5 V 3 V 4
Sum-of-Subsets Problem A special case of 0/1-knapsack S = {item 1 , item 2 , … , item n }, w[1..n] = (w 1 , w 2 ,  … , w n ) weights & W Find a subset A⊆S s.t. ∑w i =W (Eg) n=3, w[1..3]=(2,4,5), W=6 Solution : {w 1 ,w 2 } NP-Complete State Space Tree :  2 n  leaves A w 1 w 2 w 3 w 2 w 3 w 3 w 3 O O O O O O O {w 1 ,w 2 }
Sum-of-Subsets Problem Assumption : weights are in  sorted order Promising function ⅹ  weight(up_to_that_node) + w  i+1  >  W  (at i-th level) ⅹ  weight(up_to_that_node) + total(remaining) < W (Eg) n=4, (w 1 ,w 2 ,w 3 ,w 4 )=(3,4,5,6), W=13 ⅹ 12 7 3 9 7 8 13 4 0 0 3 3 0 4 5 0 0 4 5 5 6 0 0 0 0 ⅹ ⅹ :  0+5+6=11<13 ⅹ ⅹ : 9+6=15>13 ⅹ ⅹ w1=3 w2=4 w3=5 w4=6 0 4 3 7
0-1 Knapsack Problem w[1..n] = (w 1 , w 2 , … ,  w n ) p[1..n] = (p 1 , p 2 , … ,  p n ) W: Knapsack size Determine A⊆S maximizing State-Space Tree 2 n  leaves x 1 =1 x 2 =1 x 3 =1 x 4 =1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 1 1 1 1 1 x 2 =1
Branch-and-Bound Backtracking Non-optimization P.   n-Queens, m-coloring … Optimization Prob.    0/1 Knapsack Compute promising fcn. Branch & Bound Optimization Prob.- compute promising fcn. Maximization Prob. -> upper bound in each node Minimization Prob. -> lower bound in each node BFS with  B&B  Best-First-Search with  B&B
Breadth First Search procedure BFS(T : tree); { initialize(Q); v = root of T; visit v; enqueue(Q,v); while not empty(Q) do { dequeue(Q,v); for each child u of v do {   visit u;  enqueue(Q,u); } } } BFS of a graph BFS of a tree 1 2 3 4 5 6 7 8 9 1 2 5 6 7 8 9 3 4 10 11 12 13 14 15
0-1 Knapsack Problem BFS  with B&B pruning (Eg) n=4,  p[1 … 4]=(40,30,50,10) ,  w[1 … 4]=(2,5,10,5) ,  W=16 0 0 115 40 2 115 0 0 82 70 7 115 40 2 98 120 17 0 70 7 80 80 12 80 70 7 70 90 12 98 40 2 50 100 17 0 90 12 90 30 5 82 80 15 82 30 5 40 0 0 60 p1=40 ,  w1=2 p2=30 ,  w2=5 p3=50 ,  w3=10 p4=10 ,  w4=5
0-1 Knapsack Problem Bound  = current  profit  + profit of  remaining  “ fractional ”  K.S. Non-promising  if  bound ≤ max profit   (or  weight ≥W ) (Eg) n=4,  p[1 … 4]=(40,30,50,10) ,  w[1 … 4]=(2,5,10,5) ,  W=16 0 0 115 40 2 115 0 0 82 70 7 115 40 2 98 120 17 0 70 7 80 80 12 80 70 7 70 90 12 98 40 2 50 100 17 0 90 12 90 30 5 82 80 15 82 30 5 40 0 0 60 p1=40 ,  w1=2 p2=30 ,  w2=5 p3=50 ,  w3=10 p4=10 ,  w4=5
0-1 Knapsack Problem Best First Search  with B&B pruning 0 0 115 40 2 115 0 0 82 70 7 115 40 2 98 120 17 0 70 7 80 90 12 98 40 2 50 100 17 0 p1=40 ,  w1=2 p2=30 ,  w2=5 p3=50 ,  w3=10 p4=10 ,  w4=5 90 12 90

More Related Content

What's hot (20)

PPT
0/1 knapsack
Amin Omi
 
PPTX
0 1 knapsack using branch and bound
Abhishek Singh
 
PDF
Artificial Intelligence - Hill climbing.
StephenTec
 
PPTX
Graph coloring using backtracking
shashidharPapishetty
 
PPTX
Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cycle
varun arora
 
PPT
Branch and bound
Dr Shashikant Athawale
 
PPTX
NP completeness
Amrinder Arora
 
PPT
Classical problem of synchronization
Shakshi Ranawat
 
PPTX
Graph in data structure
Abrish06
 
PPTX
Dijkstra's Algorithm
Rashik Ishrak Nahian
 
PPT
Randomized algorithms ver 1.0
Dr. C.V. Suresh Babu
 
PPTX
Sum of subset problem.pptx
V.V.Vanniaperumal College for Women
 
PPTX
Topological Sorting
ShahDhruv21
 
PPTX
Bfs and Dfs
Masud Parvaze
 
PPT
Recursion tree method
Rajendran
 
PPT
Time complexity
Katang Isip
 
PPTX
SCHEDULING ALGORITHMS
Dhaval Sakhiya
 
PPTX
Greedy Algorithm - Knapsack Problem
Madhu Bala
 
PPT
Heaps & priority queues
Pedro Hugo Valencia Morales
 
PDF
Shortest Path in Graph
Dr Sandeep Kumar Poonia
 
0/1 knapsack
Amin Omi
 
0 1 knapsack using branch and bound
Abhishek Singh
 
Artificial Intelligence - Hill climbing.
StephenTec
 
Graph coloring using backtracking
shashidharPapishetty
 
Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cycle
varun arora
 
Branch and bound
Dr Shashikant Athawale
 
NP completeness
Amrinder Arora
 
Classical problem of synchronization
Shakshi Ranawat
 
Graph in data structure
Abrish06
 
Dijkstra's Algorithm
Rashik Ishrak Nahian
 
Randomized algorithms ver 1.0
Dr. C.V. Suresh Babu
 
Sum of subset problem.pptx
V.V.Vanniaperumal College for Women
 
Topological Sorting
ShahDhruv21
 
Bfs and Dfs
Masud Parvaze
 
Recursion tree method
Rajendran
 
Time complexity
Katang Isip
 
SCHEDULING ALGORITHMS
Dhaval Sakhiya
 
Greedy Algorithm - Knapsack Problem
Madhu Bala
 
Heaps & priority queues
Pedro Hugo Valencia Morales
 
Shortest Path in Graph
Dr Sandeep Kumar Poonia
 

Viewers also liked (10)

PPTX
Knapsack
Karthik Chetla
 
PPT
1 blind search
Rahel Amanda
 
PPT
Knapsack problem using dynamic programming
khush_boo31
 
PPTX
0 1 knapsack problem using dynamic programming
Maher Alshammari
 
PPTX
Kruskal Algorithm
Snehasis Panigrahi
 
PPT
Genetic Algorithm based Approach to solve Non-Fractional (0/1) Knapsack Optim...
International Islamic University
 
PPT
DESIGN AND ANALYSIS OF ALGORITHMS
Gayathri Gaayu
 
PPT
Knapsack problem
Vikas Sharma
 
PPTX
Knapsack Problem
Jenny Galino
 
PPT
Knapsack problem using fixed tuple
Mohanlal Sukhadia University (MLSU)
 
Knapsack
Karthik Chetla
 
1 blind search
Rahel Amanda
 
Knapsack problem using dynamic programming
khush_boo31
 
0 1 knapsack problem using dynamic programming
Maher Alshammari
 
Kruskal Algorithm
Snehasis Panigrahi
 
Genetic Algorithm based Approach to solve Non-Fractional (0/1) Knapsack Optim...
International Islamic University
 
DESIGN AND ANALYSIS OF ALGORITHMS
Gayathri Gaayu
 
Knapsack problem
Vikas Sharma
 
Knapsack Problem
Jenny Galino
 
Knapsack problem using fixed tuple
Mohanlal Sukhadia University (MLSU)
 
Ad

Similar to 01 knapsack using backtracking (20)

PPT
Dynamic Programming for 4th sem cse students
DeepakGowda357858
 
PPT
Disjoint sets
Core Condor
 
PDF
2018 MUMS Fall Course - Statistical Representation of Model Input (EDITED) - ...
The Statistical and Applied Mathematical Sciences Institute
 
PPT
CS 354 Graphics Math
Mark Kilgard
 
PDF
Recursion - Computer Algorithms
Alaa Al-Makhzoomy
 
PPTX
Dynamic Programming.pptx
Thanga Ramya S
 
DOCX
Divide-and-Conquer & Dynamic ProgrammingDivide-and-Conqu.docx
jacksnathalie
 
DOC
Mathematics 9 Quadratic Functions (Module 1)
Juan Miguel Palero
 
PDF
7-Backtracking.pdfsssssssssssssssssssssssssssssssssss
trinh123profc
 
PPTX
Backtracking
Sally Salem
 
PDF
Integer_Functions .pdf
JainggaPotla
 
PDF
Fast parallelizable scenario-based stochastic optimization
Pantelis Sopasakis
 
PDF
Module 1 quadratic functions
dionesioable
 
PPT
AOA ppt.ppt
SaimaShaheen14
 
PDF
Bellman ford
Kiran K
 
PPTX
designanalysisalgorithm_unit-v-part2.pptx
arifimad15
 
PPT
Newton Raphson method for load flow analysis
divyanshuprakashrock
 
PDF
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
PDF
Daa chpater14
B.Kirron Reddi
 
PPTX
Polynomials and Curve Fitting in MATLAB
Shameer Ahmed Koya
 
Dynamic Programming for 4th sem cse students
DeepakGowda357858
 
Disjoint sets
Core Condor
 
2018 MUMS Fall Course - Statistical Representation of Model Input (EDITED) - ...
The Statistical and Applied Mathematical Sciences Institute
 
CS 354 Graphics Math
Mark Kilgard
 
Recursion - Computer Algorithms
Alaa Al-Makhzoomy
 
Dynamic Programming.pptx
Thanga Ramya S
 
Divide-and-Conquer & Dynamic ProgrammingDivide-and-Conqu.docx
jacksnathalie
 
Mathematics 9 Quadratic Functions (Module 1)
Juan Miguel Palero
 
7-Backtracking.pdfsssssssssssssssssssssssssssssssssss
trinh123profc
 
Backtracking
Sally Salem
 
Integer_Functions .pdf
JainggaPotla
 
Fast parallelizable scenario-based stochastic optimization
Pantelis Sopasakis
 
Module 1 quadratic functions
dionesioable
 
AOA ppt.ppt
SaimaShaheen14
 
Bellman ford
Kiran K
 
designanalysisalgorithm_unit-v-part2.pptx
arifimad15
 
Newton Raphson method for load flow analysis
divyanshuprakashrock
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
Daa chpater14
B.Kirron Reddi
 
Polynomials and Curve Fitting in MATLAB
Shameer Ahmed Koya
 
Ad

Recently uploaded (20)

PDF
Timothy Rottach - Ramp up on AI Use Cases, from Vector Search to AI Agents wi...
AWS Chicago
 
PPTX
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PDF
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
Timothy Rottach - Ramp up on AI Use Cases, from Vector Search to AI Agents wi...
AWS Chicago
 
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 

01 knapsack using backtracking

  • 1. Backtracking Technique Eg. Big Castle – Large Rooms & “ Sleeping Beauty ” Systematic search - BFS, DFS Many paths led to nothing but “ dead-ends ” Can we avoid these unnecessary labor ? ( bounding function or promising function ) DFS with bounding function (promising function) Worst-Case : Exhaustive Search
  • 2. Backtracking Technique Useful because it is efficient for “ many ” large instances NP-Complete problems Eg. 0-1 knapsack problem D.P. : O (min(2 n , nW)) Backtracking : can be very efficient if good bounding function(promising function) Recursion
  • 3. Outline of Backtracking Approach Backtracking : DFS of a tree except that nodes are visited if promising DFS(Depth First Search) Procedure d_f_s_tree ( v : node) var u : node { visit v ; // some action // for each child u of v do d_f_s_tree ( u ); } 1 2 3 5 7 11 4 6 8 9 10
  • 5. N-Queens Problem n ⅹ n Chess board n -Queens Eg. 8-Queens problem Q Q Q Q 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
  • 6. N-Queens Problem: DFS State Space Tree for 4-Queens Problem
  • 7. N-Queens Problem: DFS (Same Col/Row x) State Space Tree for 4-Queens Problem X 1 =1 X 2 =2 3 4 4 3 4 3 4 2 3 2 3 2 2 4 3 2 3 4 1 4 1 3 2 4 1 3 4 1 2 4 3 2 1 2 3 4 (x 1 , x 2 , x 3 , x 4 )=(2, 4, 1, 3) #leaf nodes : n!=4! DFS : “ Backtrack ” at dead ends – 4! leaf nodes (n!) Cf. Backtracking : “ Backtrack ” if non-promising ( pruning )
  • 8. N-Queens Problem: Backtracking Promising Function(Bounding Function) (i) same row ⅹ (ii) same column ⅹ (col(i) ≠ col(k)) (iii) same diagonal ⅹ (|col(i)-col(k)|≠|i-k|) Q (i, col(i)) Q (k, col(k)) Q (i,col(i)) Q (k,col(k))
  • 9. N-Queens Problem: Backtracking State Space Tree for 4-Queens Problem
  • 10. N-Queens Problem Better (faster) Algorithm Monte Carlo Algorithm “ place almost all queens randomly, then do remaining queens using backtracking. ” #Nodes Checked DFS (n n ) #Nodes Checked DFS (same col/row X) (n!) #Nodes Checked Backtracking 341 19,173,961 9.73 ⅹ10 12 1.20ⅹ 10 16 24 40,320 4.79 ⅹ10 8 8.72ⅹ 10 10 61 15,721 1.01 ⅹ10 7 3.78ⅹ 10 8 4 8 12 14 n
  • 11. Graph Coloring M-coloring problem: Color undirected graph with ≤ m colors 2 adjacent vertices : different colors (Eg) V 1 V 2 V 4 V 3 2-coloring X 3-coloring O
  • 12. Graph Coloring State-Space Tree : m n leaves Promising function : check adjacent vertices for the same color start 1 2 3 2 1 3 2 1 3 2 1 3 X X X X X
  • 13. Hamiltonian Circuits Problem TSP problem in chapter 3. Brute-force: n!, (n-1)! D.P. : When n =20, B.F.  3,800 years D.P.  45 sec. More efficient solution? See chapter 6 Given an undirected or directed graph, find a tour(HC). (need not be S.P.)
  • 14. Hamiltonian Circuits Problem State-Space Tree (n-1)! Leaves : worst-case Promising function: 1. i -th node ( i +1)-th node 2.(n-1)-th node 0-th node 3. i -th node ≠ 0 ~ ( i -1)-th node HC : X (Eg) V 1 V 2 V 6 V 5 V 3 V 7 V 4 V 8 V 1 V 2 V 5 V 3 V 4
  • 15. Sum-of-Subsets Problem A special case of 0/1-knapsack S = {item 1 , item 2 , … , item n }, w[1..n] = (w 1 , w 2 , … , w n ) weights & W Find a subset A⊆S s.t. ∑w i =W (Eg) n=3, w[1..3]=(2,4,5), W=6 Solution : {w 1 ,w 2 } NP-Complete State Space Tree : 2 n leaves A w 1 w 2 w 3 w 2 w 3 w 3 w 3 O O O O O O O {w 1 ,w 2 }
  • 16. Sum-of-Subsets Problem Assumption : weights are in sorted order Promising function ⅹ weight(up_to_that_node) + w i+1 > W (at i-th level) ⅹ weight(up_to_that_node) + total(remaining) < W (Eg) n=4, (w 1 ,w 2 ,w 3 ,w 4 )=(3,4,5,6), W=13 ⅹ 12 7 3 9 7 8 13 4 0 0 3 3 0 4 5 0 0 4 5 5 6 0 0 0 0 ⅹ ⅹ : 0+5+6=11<13 ⅹ ⅹ : 9+6=15>13 ⅹ ⅹ w1=3 w2=4 w3=5 w4=6 0 4 3 7
  • 17. 0-1 Knapsack Problem w[1..n] = (w 1 , w 2 , … , w n ) p[1..n] = (p 1 , p 2 , … , p n ) W: Knapsack size Determine A⊆S maximizing State-Space Tree 2 n leaves x 1 =1 x 2 =1 x 3 =1 x 4 =1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 1 1 1 1 1 x 2 =1
  • 18. Branch-and-Bound Backtracking Non-optimization P.  n-Queens, m-coloring … Optimization Prob.  0/1 Knapsack Compute promising fcn. Branch & Bound Optimization Prob.- compute promising fcn. Maximization Prob. -> upper bound in each node Minimization Prob. -> lower bound in each node BFS with B&B Best-First-Search with B&B
  • 19. Breadth First Search procedure BFS(T : tree); { initialize(Q); v = root of T; visit v; enqueue(Q,v); while not empty(Q) do { dequeue(Q,v); for each child u of v do { visit u; enqueue(Q,u); } } } BFS of a graph BFS of a tree 1 2 3 4 5 6 7 8 9 1 2 5 6 7 8 9 3 4 10 11 12 13 14 15
  • 20. 0-1 Knapsack Problem BFS with B&B pruning (Eg) n=4, p[1 … 4]=(40,30,50,10) , w[1 … 4]=(2,5,10,5) , W=16 0 0 115 40 2 115 0 0 82 70 7 115 40 2 98 120 17 0 70 7 80 80 12 80 70 7 70 90 12 98 40 2 50 100 17 0 90 12 90 30 5 82 80 15 82 30 5 40 0 0 60 p1=40 , w1=2 p2=30 , w2=5 p3=50 , w3=10 p4=10 , w4=5
  • 21. 0-1 Knapsack Problem Bound = current profit + profit of remaining “ fractional ” K.S. Non-promising if bound ≤ max profit (or weight ≥W ) (Eg) n=4, p[1 … 4]=(40,30,50,10) , w[1 … 4]=(2,5,10,5) , W=16 0 0 115 40 2 115 0 0 82 70 7 115 40 2 98 120 17 0 70 7 80 80 12 80 70 7 70 90 12 98 40 2 50 100 17 0 90 12 90 30 5 82 80 15 82 30 5 40 0 0 60 p1=40 , w1=2 p2=30 , w2=5 p3=50 , w3=10 p4=10 , w4=5
  • 22. 0-1 Knapsack Problem Best First Search with B&B pruning 0 0 115 40 2 115 0 0 82 70 7 115 40 2 98 120 17 0 70 7 80 90 12 98 40 2 50 100 17 0 p1=40 , w1=2 p2=30 , w2=5 p3=50 , w3=10 p4=10 , w4=5 90 12 90