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

PPTX
Knapsack Problem
DOC
Branch and bound
PPTX
sum of subset problem using Backtracking
PPT
Iterative deepening search
PDF
I. Mini-Max Algorithm in AI
PPTX
The n Queen Problem
PDF
9. chapter 8 np hard and np complete problems
PPT
Greedy Algorihm
Knapsack Problem
Branch and bound
sum of subset problem using Backtracking
Iterative deepening search
I. Mini-Max Algorithm in AI
The n Queen Problem
9. chapter 8 np hard and np complete problems
Greedy Algorihm

What's hot (20)

PPTX
Graph coloring using backtracking
PPTX
8 queens problem using back tracking
PPTX
Sequential multiplication
PPTX
Time space trade off
PDF
Classification in Data Mining
PDF
Algorithms Lecture 7: Graph Algorithms
PPTX
Artificial Intelligence Searching Techniques
PPTX
Certinity Factor and Dempster-shafer theory .pptx
PPT
5.1 greedy
PPTX
Dag representation of basic blocks
PPTX
Control Strategies in AI
PPTX
Process synchronization in Operating Systems
PPTX
1.10. pumping lemma for regular sets
PPTX
AI_Session 7 Greedy Best first search algorithm.pptx
PPT
Dinive conquer algorithm
PPTX
Webinar : P, NP, NP-Hard , NP - Complete problems
PPTX
Lecture optimal binary search tree
PDF
I.BEST FIRST SEARCH IN AI
PPTX
Binary search
PPTX
0 1 knapsack using branch and bound
Graph coloring using backtracking
8 queens problem using back tracking
Sequential multiplication
Time space trade off
Classification in Data Mining
Algorithms Lecture 7: Graph Algorithms
Artificial Intelligence Searching Techniques
Certinity Factor and Dempster-shafer theory .pptx
5.1 greedy
Dag representation of basic blocks
Control Strategies in AI
Process synchronization in Operating Systems
1.10. pumping lemma for regular sets
AI_Session 7 Greedy Best first search algorithm.pptx
Dinive conquer algorithm
Webinar : P, NP, NP-Hard , NP - Complete problems
Lecture optimal binary search tree
I.BEST FIRST SEARCH IN AI
Binary search
0 1 knapsack using branch and bound
Ad

Viewers also liked (10)

PPTX
Knapsack
PPT
1 blind search
PPT
Knapsack problem using dynamic programming
PPTX
0 1 knapsack problem using dynamic programming
PPTX
Kruskal Algorithm
PPT
Genetic Algorithm based Approach to solve Non-Fractional (0/1) Knapsack Optim...
PPT
DESIGN AND ANALYSIS OF ALGORITHMS
PPT
Knapsack problem
PPTX
Greedy Algorithm - Knapsack Problem
PPT
Knapsack problem using fixed tuple
Knapsack
1 blind search
Knapsack problem using dynamic programming
0 1 knapsack problem using dynamic programming
Kruskal Algorithm
Genetic Algorithm based Approach to solve Non-Fractional (0/1) Knapsack Optim...
DESIGN AND ANALYSIS OF ALGORITHMS
Knapsack problem
Greedy Algorithm - Knapsack Problem
Knapsack problem using fixed tuple
Ad

Similar to 01 knapsack using backtracking (20)

PPT
Dynamic Programming for 4th sem cse students
PPT
Disjoint sets
PDF
2018 MUMS Fall Course - Statistical Representation of Model Input (EDITED) - ...
PPT
CS 354 Graphics Math
PDF
Recursion - Computer Algorithms
PPTX
Dynamic Programming.pptx
DOCX
Divide-and-Conquer & Dynamic ProgrammingDivide-and-Conqu.docx
DOC
Mathematics 9 Quadratic Functions (Module 1)
PDF
7-Backtracking.pdfsssssssssssssssssssssssssssssssssss
PPTX
Backtracking
PDF
Integer_Functions .pdf
PDF
Fast parallelizable scenario-based stochastic optimization
PDF
Module 1 quadratic functions
PPT
AOA ppt.ppt
PDF
Bellman ford
PPTX
designanalysisalgorithm_unit-v-part2.pptx
PPT
Newton Raphson method for load flow analysis
PDF
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
PDF
Daa chpater14
PPTX
Polynomials and Curve Fitting in MATLAB
Dynamic Programming for 4th sem cse students
Disjoint sets
2018 MUMS Fall Course - Statistical Representation of Model Input (EDITED) - ...
CS 354 Graphics Math
Recursion - Computer Algorithms
Dynamic Programming.pptx
Divide-and-Conquer & Dynamic ProgrammingDivide-and-Conqu.docx
Mathematics 9 Quadratic Functions (Module 1)
7-Backtracking.pdfsssssssssssssssssssssssssssssssssss
Backtracking
Integer_Functions .pdf
Fast parallelizable scenario-based stochastic optimization
Module 1 quadratic functions
AOA ppt.ppt
Bellman ford
designanalysisalgorithm_unit-v-part2.pptx
Newton Raphson method for load flow analysis
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Daa chpater14
Polynomials and Curve Fitting in MATLAB

Recently uploaded (20)

PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Cloud computing and distributed systems.
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
cuic standard and advanced reporting.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
sap open course for s4hana steps from ECC to s4
PPTX
Spectroscopy.pptx food analysis technology
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
Big Data Technologies - Introduction.pptx
PDF
Approach and Philosophy of On baking technology
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
MYSQL Presentation for SQL database connectivity
Cloud computing and distributed systems.
Per capita expenditure prediction using model stacking based on satellite ima...
Programs and apps: productivity, graphics, security and other tools
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
cuic standard and advanced reporting.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Building Integrated photovoltaic BIPV_UPV.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
Review of recent advances in non-invasive hemoglobin estimation
Encapsulation_ Review paper, used for researhc scholars
20250228 LYD VKU AI Blended-Learning.pptx
NewMind AI Weekly Chronicles - August'25 Week I
sap open course for s4hana steps from ECC to s4
Spectroscopy.pptx food analysis technology
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Big Data Technologies - Introduction.pptx
Approach and Philosophy of On baking technology
Build a system with the filesystem maintained by OSTree @ COSCUP 2025

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