SlideShare a Scribd company logo
1 of 28
Download to read offline
BACK TRACKING
&
BRANCH AND BOUND
ALGORITHM
SUBMITTED BY: SUBMITTED TO:
VIPUL CHAUHAN DR. SUMIT KALRA
M18CSE005 ASSISTANT PROFESSOR
IIT JODHPUR
BACK TRACKING ALGORITHM
• Finds all(or some) solutions to some computational problems, notably
constraint satisfaction problem.
• Incrementally builds candidates to the solutions, and abandons a candidate
(“backtracks”) as soon as it determines the candidate will not lead to a valid
solution.
• Can be applied only for problems which admit the concept of a “partial
candidate solution” and a relatively quick test for completeness to a valid
solution.
• Based on Depth First Recursive Search.
PICTORIAL EXPLANATION :
Invalid solution
(Backtrack)
Invalid solution
(Backtrack)
Invalid solution
(Backtrack)
Sol 1
Optimal value1
Sol 2
Optimal value2
Sol 3
Optimal value3
A
B1 B2
C1 C2 C3 C1 C2
PSEUDO CODE:
boolean pathFound(Position p){
if(p is finish) return true;
for each option O from p {
Boolean isThereAPath= pathFound(O);
if(isThereAPath)
return true;
}
return false;
}
EXAMPLE : N QUEEN PUZZLE
• PROBLEM DESCRIPTION: In a N X N square Board , place N queens so that no
two queens threaten each other(Non Attacking Position).
• Let N=4
So, on 4X4 board we have to place 4 queens
Q3
Q1
Q4
Q2
SOLUTION BY BACKTRACKING :
Q1 X X X
Q1 X
Q1 X
Q1 X
Q1 X X X
Q1 X X
Q1 Q2 X X
Q1 Q2 X X
Q1 X X X
Q1 X X
Q1 Q2 X
Q1 Q2 X X
Step 1for Q1 Step 2 for Q2
No Place for
Q3 hence
backtrack to
other positon of
Q2
Q1 X X X
Q1 X Q3 X
Q1 Q2 X X
Q1 Q2 X X
No Place for Q4
hence backtrack to
other positon of
Q3->Q2->Q1
Step 4 for Q3Step 3 for next position of Q2
CONTINUE..
Q1 X
Q1 X X X
Q1 X
Q1 X
Q1 X X
Q1 X X X
Q1 X X
Q1 Q2 X X
Q1 X Q3 X
Q1 X X X
Q1 X X
Q1 Q2 X X
Q1 X Q3 X
Q1 X X X
Q1 X X Q4
Q1 Q2 X X
Reached
Solution

Step 5 for another position of Q1 Step 6 for Q2
Step 7 for Q3 Step 8 for Q4
WHY BACKTRACKING :
• Whenever applicable, Backtracking is often much faster than brute force
enumeration of all complete candidates, since it eliminates a large no of
candidates with a single test.
• Simple and easy to code
• Different states are stored into stack so that data can be useful anytime.
BRANCH AND BOUND ALGORITHM
• Algorithm consist of a systematic enumeration of candidate solutions by
means of state space search (in which successive states are considered
with the intention of finding goal state with desired property).
• Before enumerating the candidate solution of a branch, the branch is
checked against upper(lower) bounds on the optimal solution, and
discard if it cannot produce a better solution than the best one found so
far.
• Based on Breadth First Search
PSEUDO CODE:
//maximizing an arbitrary objective function f
//requires bounding function g
1. Let B be the best possible value achieved. Initialize B <- 0;
2. Initialize a priority queue to hold a partial solution with none of
variables of the problem assigned
3. Loop until the queue is empty:
1. take a node N (with highest bound value) from the queue.
2. if N represent a single candidate solution x and f(x)>B
then x is the best solution. Set B=f(x).
3. else branch on N to produce new nodes Ni . For each of these:
1. if g(Ni) < B . Discard it.
2. else store Ni on the queue.
PICTORIAL EXPLANATION:
b=0
U=55
b=16
U=35
b=40
U=55
b=40
U=40
b=40
U=40
b=16
U=55 b=0
U=30
1
2
3
Not feasible
Not feasible
4 Killed as U<B
Killed as U<B
6
5
Solution with optimal value 40
B= 0 16 40
EXAMPLE :TRAVELLING SALESMAN
PROBLEM
• PROBLEM DISCRIPTION : Given a set of cities and distance between every
pair of cities, the problem is to find the shortest possible tour that the
salesman must take to visit every city exactly once and return to the starting
point.
1
34
2
10
9
6
10
solution
SOLUTION BY BRANCH AND BOUND :
Compute weight matrix :
∞ 10 15 20
5 ∞ 9 10
6 13 ∞ 12
8 8 9 ∞
Lets compute the reduced matrix which provide the shortest
distance from the matrix
Let Start with City 1 . Calculation of Reduced Cost for node 1
∞ 10 15 20
5 ∞ 9 10
6 13 ∞ 12
8 8 9 ∞
min
8
6
5
10
4321
4
3
2
1
∞ 0 5 10
0 ∞ 4 5
0 7 ∞ 6
0 0 1 ∞
29
min 0 510
4
3
2
1
4321
29+6
=35
∞ 0 4 5
0 ∞ 3 0
0 7 ∞ 1
0 0 0 ∞4
3
2
1
4321
Reduced cost
4321
4
3
2
1
CONTINUE.. 1
C=35
3
C=40
4
C=40
6
C=35
5
C=39
7
C=35
2
C=35
432
3 4
3
∞ 0 4 5
0 ∞ 3 0
0 7 ∞ 1
0 0 0 ∞4
3
2
1
4321
∞ ∞ ∞ ∞
∞ ∞ 3 0
0 ∞ ∞ 1
0 ∞ 0 ∞4
3
2
1
4321 min
0
0
0
-
0
∞ ∞ ∞ ∞
∞ ∞ ∞ ∞
∞ ∞ ∞ 1
0 ∞ ∞ ∞4
3
2
1
4321
min 0 000
C(node 2)= W[1][2]+ C(Node 1) + Reduce cost = 35
min
0
1
-
-
1
∞ ∞ ∞ ∞
∞ ∞ ∞ ∞
∞ ∞ ∞ 0
0 ∞ ∞ ∞4
3
2
1
4321
C(node 5)= W[2][3]+ C(Node 2) + Reduce cost = 3+35+1=39
Similarly we calculate C for node 3,
4 and put them in priority queue
Similarly we calculate C for node 6 and put
them in queue
After node 7, we found out the nodes in queue are having C > MinCost so we kill them.
MinCost =35
1
WHY BRANCH AND BOUND ?
• An enhancement of backtracking
• Determine the bound as tight as possible
• Doesn’t try all possible combinations and hence reach the solution fast.
• Solve some problems which are not solvable by Dynamic programming.
0/1 KNAPSACK PROBLEM
PROBLEM DESCRIPTION: Given weight and values of N items, put these items in
a Knapsack of capacity C to get the maximum total value in the Knapsack. You
cannot break an item, either pick the complete or don’t pick it (0-1 property)
Complexity : Brute Force O(2N)
Dynamic Programming O(NC)
Note: Dynamic can solve the problem with integer weights only.
EXAMPLE :
Item no. Weight (in Kg) Value (in Rs)
1 1 40
2 3 100
3 4 60
4 2 20
Capacity= 5kg
NAÏVE METHOD :
• Trying out all the possible combination (24) and compute the profit over feasible solution and return the solution with max
profit.
• Psuedo Code :
for i = 1 to 2ndo
j=n
tempWeight =0
tempValue =0
while ( A[j] != 0 and j > 0)
A[j]=0
j=j – 1
A[j] =1
for k =1 to n do
if (A[k] = 1) then
tempWeight =tempWeight + Weights[k]
tempValue =tempValue + Values[k]
if ((tempValue > bestValue) AND (tempWeight <=Capacity)) then
bestValue =tempValue
bestWeight =tempWeight
bestChoice =A
return bestChoice
PROGRAM AND EXECUTION:
Click to Code Click to execute program
BACKTRACKING
V=0
W=0
V=20
W=2
V=60
W=4
killed
W=6
V=100
W=3
V=120
W=5
V=40
W=1
V=60
W=3
V=100
W=5
killed
W=7
V=140
W=4
killed
W=6
V=40
W=1
V=60
W=4
V=100
W=3
killed
W=7
V=40
W=1
V=100
W=5
V=140
W=4
killed
W=9
V=0
W=0
V=140
W=4
V=40
W=1
V=0
W=0
V=0
W=0
V=100
W=3
V=0
W=0
X4=1X4=0X4=1X4=0X4=1
X3=0X3=1
X2=1
X1=1
X3=0X3=1X3=0X3=1 X3=0X3=1
X4=0X4=1X4=0X4=1
X4=0X4=1
X2=0X2=1
X1=0
X2=0
X4=0
Item
no.
Weig
ht (in
Kg)
Value
(in
Rs)
1 1 40
2 3 100
3 4 60
4 2 20
Capacity= 5kg
PSEUDO CODE:
void knapsack(level,value,weight){
if(level<n){
l=level;
level=level+1;
if(weight+w[l]<=c){
v2= value+v[l];
w2=weight+w[l];
if(v2>maxValue){
maxValue=v2;
maxWeight=w2;
}
knapsack(level,v2,w2);
}
else{
//"next level node killed"
}
knapsack(level,value,weight);
}
}
PROGRAM AND EXECUTION:
Click to code Click to execute program
BRANCH AND BOUND :
V=0
W=0
B=155
killed
W=6
V=140
W=4
B=150
killed
W=8
V=140
W=4
B=155
V=40
W=1
B=155
V=40
W=1
B=100
V=140
W=4
B=140
V=0
W=0
B=130
Item
no.
Weig
ht (in
Kg)
Value
(in
Rs)
1 1 40
2 3 100
3 4 60
4 2 20
Capacity= 5kg
MaxValue= 140B<MaxValue
killed
B<MaxValue
killed
X4=0X4=1
X3=0X3=1
X2=1
X1=1 X1=0
X2=0
PSEUDO CODE:
//assumption: items are arranged in non increasing order of their profit by weight ratio
priority_queue<Node> Q;
Node N1,N2;
N1.level=0;
N1.value=0;
N1.weight=0;
N1.bound=boundCal(N1.level,N1.value,N1.weight);
Q.push(N1);
while(!Q.empty())
N1=Q.top();
Q.pop();
if(N1.level<=n)
l=N1.level;
if(N1.weight+w[l]<=c)
N2.level=N1.level+1;
N2.weight=N1.weight+w[l];
N2.value=N1.value+v[l];
N2.bound=boundCal(N2.level,N2.value,N2.weight);
if(N2.value>maxValue)
maxValue=N2.value;
maxWeight=N2.weight;
Q.push(N2);
PSEUDO CODE: CONTINUE..
N2.level=N1.level+1;
N2.weight=N1.weight;
N2.value=N1.value;
N2.bound=boundCal(N2.level,N2.value,N2.weight);
if(N2.bound>=maxValue)
Q.push(N2);
boundCal(level,value,weight)
upperbound= value;
for(i=level;i<n;i++)
if(weight+w[level]<=c)
weight+=w[level];
upperbound+=v[level];
else
w2=c-weight;
upperbound+=w2*v[level]/w[level];
break;
return upperbound;
PROGRAM AND EXECUTION:
Click to code Click to execute program
REFERENCES :
• https://www.wikipedia.org
• https://www.geeksforgeeks.org/
• https://www.youtube.com/
• Thomas H. Cormen
Backtracking & branch and bound

More Related Content

What's hot (20)

Greedy Algorithms
Greedy AlgorithmsGreedy Algorithms
Greedy Algorithms
 
Branch & bound
Branch & boundBranch & bound
Branch & bound
 
Algorithm
AlgorithmAlgorithm
Algorithm
 
01 Knapsack using Dynamic Programming
01 Knapsack using Dynamic Programming01 Knapsack using Dynamic Programming
01 Knapsack using Dynamic Programming
 
Optimal binary search tree dynamic programming
Optimal binary search tree   dynamic programmingOptimal binary search tree   dynamic programming
Optimal binary search tree dynamic programming
 
Knapsack problem
Knapsack problemKnapsack problem
Knapsack problem
 
Daa:Dynamic Programing
Daa:Dynamic ProgramingDaa:Dynamic Programing
Daa:Dynamic Programing
 
9. chapter 8 np hard and np complete problems
9. chapter 8   np hard and np complete problems9. chapter 8   np hard and np complete problems
9. chapter 8 np hard and np complete problems
 
Divide and Conquer
Divide and ConquerDivide and Conquer
Divide and Conquer
 
Travelling salesman problem
Travelling salesman problemTravelling salesman problem
Travelling salesman problem
 
Lecture optimal binary search tree
Lecture optimal binary search tree Lecture optimal binary search tree
Lecture optimal binary search tree
 
Knapsack Problem (DP & GREEDY)
Knapsack Problem (DP & GREEDY)Knapsack Problem (DP & GREEDY)
Knapsack Problem (DP & GREEDY)
 
Randomized algorithms ver 1.0
Randomized algorithms ver 1.0Randomized algorithms ver 1.0
Randomized algorithms ver 1.0
 
0/1 knapsack
0/1 knapsack0/1 knapsack
0/1 knapsack
 
P, NP, NP-Complete, and NP-Hard
P, NP, NP-Complete, and NP-HardP, NP, NP-Complete, and NP-Hard
P, NP, NP-Complete, and NP-Hard
 
Greedy algorithm
Greedy algorithmGreedy algorithm
Greedy algorithm
 
Job sequencing with deadline
Job sequencing with deadlineJob sequencing with deadline
Job sequencing with deadline
 
Dijkstra s algorithm
Dijkstra s algorithmDijkstra s algorithm
Dijkstra s algorithm
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1
 
Fractional Knapsack Problem
Fractional Knapsack ProblemFractional Knapsack Problem
Fractional Knapsack Problem
 

Similar to Backtracking & branch and bound

Greedy algorithms -Making change-Knapsack-Prim's-Kruskal's
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal'sGreedy algorithms -Making change-Knapsack-Prim's-Kruskal's
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal'sJay Patel
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy methodhodcsencet
 
Optimization problems
Optimization problemsOptimization problems
Optimization problemsRuchika Sinha
 
final-ppts-daa-unit-iii-greedy-method.pdf
final-ppts-daa-unit-iii-greedy-method.pdffinal-ppts-daa-unit-iii-greedy-method.pdf
final-ppts-daa-unit-iii-greedy-method.pdfJasmineSayyed3
 
LPP, Duality and Game Theory
LPP, Duality and Game TheoryLPP, Duality and Game Theory
LPP, Duality and Game TheoryPurnima Pandit
 
Undecidable Problems and Approximation Algorithms
Undecidable Problems and Approximation AlgorithmsUndecidable Problems and Approximation Algorithms
Undecidable Problems and Approximation AlgorithmsMuthu Vinayagam
 
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.pptParallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.pptBinayakMukherjee4
 
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.pptParallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.pptdakccse
 
Optimum Engineering Design - Day 4 - Clasical methods of optimization
Optimum Engineering Design - Day 4 - Clasical methods of optimizationOptimum Engineering Design - Day 4 - Clasical methods of optimization
Optimum Engineering Design - Day 4 - Clasical methods of optimizationSantiagoGarridoBulln
 
lab-8 (1).pptx
lab-8 (1).pptxlab-8 (1).pptx
lab-8 (1).pptxShimoFcis
 
Integer Linear Programming
Integer Linear ProgrammingInteger Linear Programming
Integer Linear ProgrammingSukhpalRamanand
 
Firefly exact MCMC for Big Data
Firefly exact MCMC for Big DataFirefly exact MCMC for Big Data
Firefly exact MCMC for Big DataGianvito Siciliano
 
CI L11 Optimization 3 GlobalOptimization.pdf
CI L11 Optimization 3 GlobalOptimization.pdfCI L11 Optimization 3 GlobalOptimization.pdf
CI L11 Optimization 3 GlobalOptimization.pdfSantiagoGarridoBulln
 
Ap for b.tech. (mechanical) Assignment Problem
Ap for b.tech. (mechanical) Assignment Problem Ap for b.tech. (mechanical) Assignment Problem
Ap for b.tech. (mechanical) Assignment Problem Prashant Khandelwal
 
Chapter 3.Simplex Method hand out last.pdf
Chapter 3.Simplex Method hand out last.pdfChapter 3.Simplex Method hand out last.pdf
Chapter 3.Simplex Method hand out last.pdfTsegay Berhe
 
Support Vector Machines is the the the the the the the the the
Support Vector Machines is the the the the the the the the theSupport Vector Machines is the the the the the the the the the
Support Vector Machines is the the the the the the the the thesanjaibalajeessn
 
Undecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWER
Undecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWERUndecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWER
Undecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWERmuthukrishnavinayaga
 

Similar to Backtracking & branch and bound (20)

Greedy algorithms -Making change-Knapsack-Prim's-Kruskal's
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal'sGreedy algorithms -Making change-Knapsack-Prim's-Kruskal's
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal's
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
 
Optimization problems
Optimization problemsOptimization problems
Optimization problems
 
final-ppts-daa-unit-iii-greedy-method.pdf
final-ppts-daa-unit-iii-greedy-method.pdffinal-ppts-daa-unit-iii-greedy-method.pdf
final-ppts-daa-unit-iii-greedy-method.pdf
 
LPP, Duality and Game Theory
LPP, Duality and Game TheoryLPP, Duality and Game Theory
LPP, Duality and Game Theory
 
Undecidable Problems and Approximation Algorithms
Undecidable Problems and Approximation AlgorithmsUndecidable Problems and Approximation Algorithms
Undecidable Problems and Approximation Algorithms
 
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.pptParallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
 
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.pptParallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
 
Optimum Engineering Design - Day 4 - Clasical methods of optimization
Optimum Engineering Design - Day 4 - Clasical methods of optimizationOptimum Engineering Design - Day 4 - Clasical methods of optimization
Optimum Engineering Design - Day 4 - Clasical methods of optimization
 
lab-8 (1).pptx
lab-8 (1).pptxlab-8 (1).pptx
lab-8 (1).pptx
 
Integer Linear Programming
Integer Linear ProgrammingInteger Linear Programming
Integer Linear Programming
 
Firefly exact MCMC for Big Data
Firefly exact MCMC for Big DataFirefly exact MCMC for Big Data
Firefly exact MCMC for Big Data
 
CI L11 Optimization 3 GlobalOptimization.pdf
CI L11 Optimization 3 GlobalOptimization.pdfCI L11 Optimization 3 GlobalOptimization.pdf
CI L11 Optimization 3 GlobalOptimization.pdf
 
Ap for b.tech. (mechanical) Assignment Problem
Ap for b.tech. (mechanical) Assignment Problem Ap for b.tech. (mechanical) Assignment Problem
Ap for b.tech. (mechanical) Assignment Problem
 
Chapter 3.Simplex Method hand out last.pdf
Chapter 3.Simplex Method hand out last.pdfChapter 3.Simplex Method hand out last.pdf
Chapter 3.Simplex Method hand out last.pdf
 
Support Vector Machines is the the the the the the the the the
Support Vector Machines is the the the the the the the the theSupport Vector Machines is the the the the the the the the the
Support Vector Machines is the the the the the the the the the
 
Undecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWER
Undecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWERUndecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWER
Undecidable Problems - COPING WITH THE LIMITATIONS OF ALGORITHM POWER
 
Daa unit 1
Daa unit 1Daa unit 1
Daa unit 1
 
Mini project
Mini projectMini project
Mini project
 
Sudoku solver
Sudoku solverSudoku solver
Sudoku solver
 

Recently uploaded

Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 

Recently uploaded (20)

★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 

Backtracking & branch and bound

  • 1. BACK TRACKING & BRANCH AND BOUND ALGORITHM SUBMITTED BY: SUBMITTED TO: VIPUL CHAUHAN DR. SUMIT KALRA M18CSE005 ASSISTANT PROFESSOR IIT JODHPUR
  • 2. BACK TRACKING ALGORITHM • Finds all(or some) solutions to some computational problems, notably constraint satisfaction problem. • Incrementally builds candidates to the solutions, and abandons a candidate (“backtracks”) as soon as it determines the candidate will not lead to a valid solution. • Can be applied only for problems which admit the concept of a “partial candidate solution” and a relatively quick test for completeness to a valid solution. • Based on Depth First Recursive Search.
  • 3. PICTORIAL EXPLANATION : Invalid solution (Backtrack) Invalid solution (Backtrack) Invalid solution (Backtrack) Sol 1 Optimal value1 Sol 2 Optimal value2 Sol 3 Optimal value3 A B1 B2 C1 C2 C3 C1 C2
  • 4. PSEUDO CODE: boolean pathFound(Position p){ if(p is finish) return true; for each option O from p { Boolean isThereAPath= pathFound(O); if(isThereAPath) return true; } return false; }
  • 5. EXAMPLE : N QUEEN PUZZLE • PROBLEM DESCRIPTION: In a N X N square Board , place N queens so that no two queens threaten each other(Non Attacking Position). • Let N=4 So, on 4X4 board we have to place 4 queens Q3 Q1 Q4 Q2
  • 6. SOLUTION BY BACKTRACKING : Q1 X X X Q1 X Q1 X Q1 X Q1 X X X Q1 X X Q1 Q2 X X Q1 Q2 X X Q1 X X X Q1 X X Q1 Q2 X Q1 Q2 X X Step 1for Q1 Step 2 for Q2 No Place for Q3 hence backtrack to other positon of Q2 Q1 X X X Q1 X Q3 X Q1 Q2 X X Q1 Q2 X X No Place for Q4 hence backtrack to other positon of Q3->Q2->Q1 Step 4 for Q3Step 3 for next position of Q2
  • 7. CONTINUE.. Q1 X Q1 X X X Q1 X Q1 X Q1 X X Q1 X X X Q1 X X Q1 Q2 X X Q1 X Q3 X Q1 X X X Q1 X X Q1 Q2 X X Q1 X Q3 X Q1 X X X Q1 X X Q4 Q1 Q2 X X Reached Solution  Step 5 for another position of Q1 Step 6 for Q2 Step 7 for Q3 Step 8 for Q4
  • 8. WHY BACKTRACKING : • Whenever applicable, Backtracking is often much faster than brute force enumeration of all complete candidates, since it eliminates a large no of candidates with a single test. • Simple and easy to code • Different states are stored into stack so that data can be useful anytime.
  • 9. BRANCH AND BOUND ALGORITHM • Algorithm consist of a systematic enumeration of candidate solutions by means of state space search (in which successive states are considered with the intention of finding goal state with desired property). • Before enumerating the candidate solution of a branch, the branch is checked against upper(lower) bounds on the optimal solution, and discard if it cannot produce a better solution than the best one found so far. • Based on Breadth First Search
  • 10. PSEUDO CODE: //maximizing an arbitrary objective function f //requires bounding function g 1. Let B be the best possible value achieved. Initialize B <- 0; 2. Initialize a priority queue to hold a partial solution with none of variables of the problem assigned 3. Loop until the queue is empty: 1. take a node N (with highest bound value) from the queue. 2. if N represent a single candidate solution x and f(x)>B then x is the best solution. Set B=f(x). 3. else branch on N to produce new nodes Ni . For each of these: 1. if g(Ni) < B . Discard it. 2. else store Ni on the queue.
  • 11. PICTORIAL EXPLANATION: b=0 U=55 b=16 U=35 b=40 U=55 b=40 U=40 b=40 U=40 b=16 U=55 b=0 U=30 1 2 3 Not feasible Not feasible 4 Killed as U<B Killed as U<B 6 5 Solution with optimal value 40 B= 0 16 40
  • 12. EXAMPLE :TRAVELLING SALESMAN PROBLEM • PROBLEM DISCRIPTION : Given a set of cities and distance between every pair of cities, the problem is to find the shortest possible tour that the salesman must take to visit every city exactly once and return to the starting point. 1 34 2 10 9 6 10 solution
  • 13. SOLUTION BY BRANCH AND BOUND : Compute weight matrix : ∞ 10 15 20 5 ∞ 9 10 6 13 ∞ 12 8 8 9 ∞ Lets compute the reduced matrix which provide the shortest distance from the matrix Let Start with City 1 . Calculation of Reduced Cost for node 1 ∞ 10 15 20 5 ∞ 9 10 6 13 ∞ 12 8 8 9 ∞ min 8 6 5 10 4321 4 3 2 1 ∞ 0 5 10 0 ∞ 4 5 0 7 ∞ 6 0 0 1 ∞ 29 min 0 510 4 3 2 1 4321 29+6 =35 ∞ 0 4 5 0 ∞ 3 0 0 7 ∞ 1 0 0 0 ∞4 3 2 1 4321 Reduced cost 4321 4 3 2 1
  • 14. CONTINUE.. 1 C=35 3 C=40 4 C=40 6 C=35 5 C=39 7 C=35 2 C=35 432 3 4 3 ∞ 0 4 5 0 ∞ 3 0 0 7 ∞ 1 0 0 0 ∞4 3 2 1 4321 ∞ ∞ ∞ ∞ ∞ ∞ 3 0 0 ∞ ∞ 1 0 ∞ 0 ∞4 3 2 1 4321 min 0 0 0 - 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 1 0 ∞ ∞ ∞4 3 2 1 4321 min 0 000 C(node 2)= W[1][2]+ C(Node 1) + Reduce cost = 35 min 0 1 - - 1 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 0 0 ∞ ∞ ∞4 3 2 1 4321 C(node 5)= W[2][3]+ C(Node 2) + Reduce cost = 3+35+1=39 Similarly we calculate C for node 3, 4 and put them in priority queue Similarly we calculate C for node 6 and put them in queue After node 7, we found out the nodes in queue are having C > MinCost so we kill them. MinCost =35 1
  • 15. WHY BRANCH AND BOUND ? • An enhancement of backtracking • Determine the bound as tight as possible • Doesn’t try all possible combinations and hence reach the solution fast. • Solve some problems which are not solvable by Dynamic programming.
  • 16. 0/1 KNAPSACK PROBLEM PROBLEM DESCRIPTION: Given weight and values of N items, put these items in a Knapsack of capacity C to get the maximum total value in the Knapsack. You cannot break an item, either pick the complete or don’t pick it (0-1 property) Complexity : Brute Force O(2N) Dynamic Programming O(NC) Note: Dynamic can solve the problem with integer weights only.
  • 17. EXAMPLE : Item no. Weight (in Kg) Value (in Rs) 1 1 40 2 3 100 3 4 60 4 2 20 Capacity= 5kg
  • 18. NAÏVE METHOD : • Trying out all the possible combination (24) and compute the profit over feasible solution and return the solution with max profit. • Psuedo Code : for i = 1 to 2ndo j=n tempWeight =0 tempValue =0 while ( A[j] != 0 and j > 0) A[j]=0 j=j – 1 A[j] =1 for k =1 to n do if (A[k] = 1) then tempWeight =tempWeight + Weights[k] tempValue =tempValue + Values[k] if ((tempValue > bestValue) AND (tempWeight <=Capacity)) then bestValue =tempValue bestWeight =tempWeight bestChoice =A return bestChoice
  • 19. PROGRAM AND EXECUTION: Click to Code Click to execute program
  • 21. PSEUDO CODE: void knapsack(level,value,weight){ if(level<n){ l=level; level=level+1; if(weight+w[l]<=c){ v2= value+v[l]; w2=weight+w[l]; if(v2>maxValue){ maxValue=v2; maxWeight=w2; } knapsack(level,v2,w2); } else{ //"next level node killed" } knapsack(level,value,weight); } }
  • 22. PROGRAM AND EXECUTION: Click to code Click to execute program
  • 23. BRANCH AND BOUND : V=0 W=0 B=155 killed W=6 V=140 W=4 B=150 killed W=8 V=140 W=4 B=155 V=40 W=1 B=155 V=40 W=1 B=100 V=140 W=4 B=140 V=0 W=0 B=130 Item no. Weig ht (in Kg) Value (in Rs) 1 1 40 2 3 100 3 4 60 4 2 20 Capacity= 5kg MaxValue= 140B<MaxValue killed B<MaxValue killed X4=0X4=1 X3=0X3=1 X2=1 X1=1 X1=0 X2=0
  • 24. PSEUDO CODE: //assumption: items are arranged in non increasing order of their profit by weight ratio priority_queue<Node> Q; Node N1,N2; N1.level=0; N1.value=0; N1.weight=0; N1.bound=boundCal(N1.level,N1.value,N1.weight); Q.push(N1); while(!Q.empty()) N1=Q.top(); Q.pop(); if(N1.level<=n) l=N1.level; if(N1.weight+w[l]<=c) N2.level=N1.level+1; N2.weight=N1.weight+w[l]; N2.value=N1.value+v[l]; N2.bound=boundCal(N2.level,N2.value,N2.weight); if(N2.value>maxValue) maxValue=N2.value; maxWeight=N2.weight; Q.push(N2);
  • 25. PSEUDO CODE: CONTINUE.. N2.level=N1.level+1; N2.weight=N1.weight; N2.value=N1.value; N2.bound=boundCal(N2.level,N2.value,N2.weight); if(N2.bound>=maxValue) Q.push(N2); boundCal(level,value,weight) upperbound= value; for(i=level;i<n;i++) if(weight+w[level]<=c) weight+=w[level]; upperbound+=v[level]; else w2=c-weight; upperbound+=w2*v[level]/w[level]; break; return upperbound;
  • 26. PROGRAM AND EXECUTION: Click to code Click to execute program
  • 27. REFERENCES : • https://www.wikipedia.org • https://www.geeksforgeeks.org/ • https://www.youtube.com/ • Thomas H. Cormen