SlideShare une entreprise Scribd logo
1  sur  30
Backtracking:
Technique & Examples
By,
Fahim Ferdous
Back
Track
Yes Solution
No Solution
Backtracking
History
• ‘Backtrack’ the Word was first introduced by Dr. D.H.
Lehmer in 1950s.
• R.J Walker Was the First man who gave algorithmic
description in 1960.
• Later developed by S. Golamb and L. Baumert.
Backtracking
• What is Backtracking?
----- Backtracking is nothing but the modified process of the brute
force approach. where the technique systematically searches for a
solution to a problem among all available options. It does so by
assuming that the solutions are represented by vectors (v1, ..., in) of
values and by traversing through the domains of the vectors until
the solutions is found.
Backtracking
• The Algorithmic Approach
– Backtracking systematically try and search possibilities to find the
solution. Also it is an important process for solving constraint
satisfaction problem like crossword, Sudoku and many other puzzles. It
can be more continent technique for parsing other combinatorial
optimization problem.
– Basically the process is used when the problem has a number of option
and just one solution have to be selected. After having a new option set
means recursion, the procedure is repeated over and over until final
stage.
Backtracking
Algorithm Backtrack (v1,Vi)
If (V1,……., Vi) is a Solution Then
Return (V1,…, Vi)
For each v DO
If (V1,…….,Vi) is acceptable vector THEN
Sol = try (V1,…,Vi, V)
If sol != () Then
RETURN sol
End
End
Return ( )
Backtracking
• Advantages
– Comparison with the Dynamic Programming, Backtracking Approach is more effective in some cases.
– Backtracking Algorithm is the best option for solving tactical problem.
– Also Backtracking is effective for constraint satisfaction problem.
– In greedy Algorithm, getting the Global Optimal Solution is a long procedure and depends on user
statements but in Backtracking It Can Easily getable.
– Backtracking technique is simple to implement and easy to code.
– Different states are stored into stack so that the data or Info can be usable anytime.
– The accuracy is granted.
Backtracking
• Disadvantages
– Backtracking Approach is not efficient for solving strategic Problem.
– The overall runtime of Backtracking Algorithm is normally slow
– To solve Large Problem Sometime it needs to take the help of other
techniques like Branch and bound.
– Need Large amount of memory space for storing different state function
in the stack for big problem.
– Thrashing is one of the main problem of Backtracking.
– The Basic Approach Detects the conflicts too late.
Backtracking
• Application of Backtracking
• Optimization and tactical problems
• Constraints Satisfaction Problem
• Electrical Engineering
• Robotics
• Artificial Intelligence
• Genetic and bioinformatics Algorithm
• Materials Engineering
• Network Communication
• Solving puzzles and path
Backtracking
• Some Problem Solved with Backtracking Technique
– N- Queens Problem
– Sum of Subset
– Sudoku Puzzle
– Maze Generation
– Hamiltonian Cycle
N-Queens Problem
• History:
First Introduced in 1848 which was known as 8- queens Puzzle. Surprisingly,
The First Solution was created in 1950 by Franz Nauck. Nauck made an 8X8
Chessboard to find the first Feasible Solution.
N-Queens Problem
• Problem Description
In a NxN square board N – number of queens need to be
placed considering three Condition ---
• No two Queens can be placed in same row.
• No two Queens Can be places in same Column
• No two queens Can be placed in same Diagonal.
N-Queens Problem
• Backtracking approach for solution
- - The Algorithm will check each position [i , j] for each queens . If any Suitable places found , It will
place a queen on that position. If not Algorithm will try same approach for next position.
Algorithm Nqueen(K,n){
For i= 1 to n {
If Place(K,i){
X[k] = I;
If(k = n) then
Write x[1:n];
Else
Nqueen(k+1, n) ;
}
}
}
Place(k,i){
For j=1 to k-1{
If((x[j] = i) or abs(x[j] – 1 = abs(j-k)))
Return false;
}
Return true;
}
N-Queens Problem
Maze Generation
Problem Description
A N X N board is made in a way So that 1 or 0 can be placed in
each box where 1 is for valid path for moving towards exit and 0 is
the Closed path. Now, by using Backtracking Algorithm The path to
the exit need to be find.
Maze Generation
• -- Algorithm will try to follow the box which has the value 1 and
generate a path. Again, it will check for 1 at the next box. If not
found it will go through next box. If entire row is complete, then it will
move to the next row and look for 1.
Maze Generation
• Solution Using Backtracking Technique
Algorithm Maze_Gen(graph){
If(Maze_operator(graph, x,y, canvas)
== false)
Write (“No result”);
Return false;
Print_Result(canvas);
Return true;
}
Algorithm Maze_operator(graph, x,y, canvas){
If(x ==N-1 && y == N-1){
Canvas[x][y] = 1;
Return true;
}
If(IsSafe(graph, x,y) == true){
Canvas[x][y] = 1;
If(Maze_operator(graph, x+1,y, canvas))
Return true;
If(Maze_operator(graph, x,y+1, canvas))
Return true;
Canvas [x][y] = 0;
Return true;
}
Return false;
}
Maze Generation
Hamiltonian Cycle
Hamiltonian Cycle is a graph theory problem where the graph cycle
through a graph can visit each node only once. The puzzle was first
devised by Sir William Rowan Hamilton and the Problem is named after
Him.
Condition: The Cycle Started with a Starting Node, and visit all the Nodes in the Graph
(Not necessary to visit in sequential Order and not creating edge that is not given) And
Stop at The Starting Point/Node.
Hamiltonian Cycle
• The Backtracking Approach
The Algorithm First Check the Starting Node, if there is any edge to the next node. If
yes, then the Algorithm will check that node for the edge to the next Node. It will Also
Check If any Node is visited twice by the previous Node. If there is any then the
Algorithm Will Ignore One and Choose the Optimal One for the Solution.
 The Important thing is the tour must finish at the starting point.
Hamiltonian Cycle
Algorithm Hamilton_Cycle(k){
Repeat{
NextVal(k);
If(x[k]==0) then
Return;
If (k ==n) then
Write (x[1:n]);
Else
Hamilton_Cycle(k+1);
}
Until (false);
}
AlgortihmNextVal(k){
Repeat{
X[k] = (x[k]+1) mod (n+1);
If(x[k]=0) then return;
If (G[x[k-1],x[k]] != 0 ) then{
For j = 1 to k-1 do
If(x[j]=x[k]) then
Break;
If(j = k) then
If ((k<n or k=n) && G[x[n],x[1]] != 0)
Then return;
}
}
Until (false);
}
Hamiltonian Cycle
Sudoku Puzzle
• The Word ‘Sudoku’ is a Japanese Word. But The Sudoku Puzzle Was First
Invented in Switzerland
• In a N X N Square Board, The Numbers 1 to N to be Placed in a way that:
– All the Number from 1 to N can be Placed in each row.
– All the Number from 1 to N can be Placed in each Column.
– All The Number from 1 to N Can be Placed in Sub Square boxes of the given box.
Sudoku Puzzle
• Backtracking Approach
The Algorithm Will Check Each Box’s value if the value is in the same
row, or same column or same sub-square box. If not,then it placed the value
in the box. And go to the next box for the next value, and check the Above
Condition. If there is any duplicate value, then The Algorithm check for the
next value.
Sudoku Puzzle
Algorithm Sudoku_Solver(canvas, row,
col){
If(anyEmptyLocation(canvas, row, col))
Return true;
For I = 1 to n do, {
If (find_location(canvas, row,col, i))
{
Canvas[row][col] = i;
If (Sudoku_Solver(canvas))
Return true;
Canvas [row][col] = -1;
}
}
Return false;
}
Sum of Subset
• The Sum of Subset Problem is, there will be a set of distinct positive
Numbers X and a given Number N. Now, we have to find all the
combination of numbers from X that sum up to N. Make a Set of
those Number.
W = {4,5,6,3}
M = 13
Sum of Subset
Sum of Subset
• Backtracking Approach
First, organize the numbers in non decreasing order. Then generating a tree, we
can find the solution by adding the weight of the nodes. Note that, here more than
necessary nodes can be generated. The algorithm will search for the weighted amount
of the nodes. If it goes beyond given Number N, then it stops generating nodes and
move to other parent nodes for finding solution.
Algorithm SumOfSubset(s,k,y){
X[k] = 1;
If(s+w[k] = m)
Write (x[1:n]);
Else if((s+w[k] + w[k+1]) <= m)
SumOfSubset(s+w[k], k+1, y-w[k]);
If ((s+ y-w[k]>=m) &&(s =w[k+1] <=m)) {
X[k] =0;
SumOfSubset(s,k+1,y-w[k]);
}
}
Sum of Subset
Here M = 53
W = {8,9,14,15,16,22}
References
• Chapter 6, Backtracking, Design and Analysis of Algorithms – V.V. Muniswami
• Chapter – 4, Non- Chronological Backtracking, Handbook on Constraint Programming – F.
Rossi, Van Beek
• web.cse.ohio-state.edu/~gurari/course/cis680/cis680Ch19.html
• www.classle.net/faq/comparisons-divide-and-conquergreedy-methoddynamic-
programmingbacktracking-and-branch-and-bound-
• Backtracking algorithms for constraint satisfaction problems; a survey
R. Dechter, D. Frost, in Constraints, International Journal, 1998.
• Bessiere C., Maestre A., Meseguer P., Distributed dynamic backtracking, in M.C. Silaghi, editor,
Proceedings of the IJCAI’01 workshop on Distributed Constraint Reasoning, Seattle WA,
2001, pp. 9–16.
• A First Look at Graph Theory – Jhon Clark, Allan Holton
• Principles and Practice of Constraint Programming- CP 2001: 7th International Conference,
CP 2001, Paphos, Cyprus, November 26 - December 1, 2001
Thank you!!

Contenu connexe

Tendances

Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cycle
Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cycleBacktracking-N Queens Problem-Graph Coloring-Hamiltonian cycle
Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cyclevarun arora
 
Elements of dynamic programming
Elements of dynamic programmingElements of dynamic programming
Elements of dynamic programmingTafhim Islam
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy methodhodcsencet
 
Problem solving agents
Problem solving agentsProblem solving agents
Problem solving agentsMegha Sharma
 
State Space Representation and Search
State Space Representation and SearchState Space Representation and Search
State Space Representation and SearchHitesh Mohapatra
 
Algorithm and pseudocode conventions
Algorithm and pseudocode conventionsAlgorithm and pseudocode conventions
Algorithm and pseudocode conventionssaranyatdr
 
01 knapsack using backtracking
01 knapsack using backtracking01 knapsack using backtracking
01 knapsack using backtrackingmandlapure
 
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-HardAnimesh Chaturvedi
 
Vc dimension in Machine Learning
Vc dimension in Machine LearningVc dimension in Machine Learning
Vc dimension in Machine LearningVARUN KUMAR
 
15 puzzle problem using branch and bound
15 puzzle problem using branch and bound15 puzzle problem using branch and bound
15 puzzle problem using branch and boundAbhishek Singh
 
Greedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemMadhu Bala
 
Job sequencing with deadline
Job sequencing with deadlineJob sequencing with deadline
Job sequencing with deadlineArafat Hossan
 
Conceptual dependency
Conceptual dependencyConceptual dependency
Conceptual dependencyJismy .K.Jose
 

Tendances (20)

Daa unit 1
Daa unit 1Daa unit 1
Daa unit 1
 
Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cycle
Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cycleBacktracking-N Queens Problem-Graph Coloring-Hamiltonian cycle
Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cycle
 
Recognition-of-tokens
Recognition-of-tokensRecognition-of-tokens
Recognition-of-tokens
 
Elements of dynamic programming
Elements of dynamic programmingElements of dynamic programming
Elements of dynamic programming
 
N queen problem
N queen problemN queen problem
N queen problem
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
 
Problem solving agents
Problem solving agentsProblem solving agents
Problem solving agents
 
State Space Representation and Search
State Space Representation and SearchState Space Representation and Search
State Space Representation and Search
 
Algorithm and pseudocode conventions
Algorithm and pseudocode conventionsAlgorithm and pseudocode conventions
Algorithm and pseudocode conventions
 
And or graph
And or graphAnd or graph
And or graph
 
01 knapsack using backtracking
01 knapsack using backtracking01 knapsack using backtracking
01 knapsack using backtracking
 
Daa notes 1
Daa notes 1Daa notes 1
Daa notes 1
 
Randomized algorithms ver 1.0
Randomized algorithms ver 1.0Randomized algorithms ver 1.0
Randomized algorithms ver 1.0
 
Graph coloring problem
Graph coloring problemGraph coloring problem
Graph coloring problem
 
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
 
Vc dimension in Machine Learning
Vc dimension in Machine LearningVc dimension in Machine Learning
Vc dimension in Machine Learning
 
15 puzzle problem using branch and bound
15 puzzle problem using branch and bound15 puzzle problem using branch and bound
15 puzzle problem using branch and bound
 
Greedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack Problem
 
Job sequencing with deadline
Job sequencing with deadlineJob sequencing with deadline
Job sequencing with deadline
 
Conceptual dependency
Conceptual dependencyConceptual dependency
Conceptual dependency
 

Similaire à BackTracking Algorithm: Technique and Examples

Data Analysis and Algorithms Lecture 1: Introduction
 Data Analysis and Algorithms Lecture 1: Introduction Data Analysis and Algorithms Lecture 1: Introduction
Data Analysis and Algorithms Lecture 1: IntroductionTayyabSattar5
 
module5_backtrackingnbranchnbound_2022.pdf
module5_backtrackingnbranchnbound_2022.pdfmodule5_backtrackingnbranchnbound_2022.pdf
module5_backtrackingnbranchnbound_2022.pdfShiwani Gupta
 
unit-4-dynamic programming
unit-4-dynamic programmingunit-4-dynamic programming
unit-4-dynamic programminghodcsencet
 
Back tracking and branch and bound class 20
Back tracking and branch and bound class 20Back tracking and branch and bound class 20
Back tracking and branch and bound class 20Kumar
 
Branch and bound
Branch and boundBranch and bound
Branch and boundAcad
 
AAC ch 3 Advance strategies (Dynamic Programming).pptx
AAC ch 3 Advance strategies (Dynamic Programming).pptxAAC ch 3 Advance strategies (Dynamic Programming).pptx
AAC ch 3 Advance strategies (Dynamic Programming).pptxHarshitSingh334328
 
5.5 back track
5.5 back track5.5 back track
5.5 back trackKrish_ver2
 
Optimization problems
Optimization problemsOptimization problems
Optimization problemsRuchika Sinha
 
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
 
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
 
Unit 3- Greedy Method.pptx
Unit 3- Greedy Method.pptxUnit 3- Greedy Method.pptx
Unit 3- Greedy Method.pptxMaryJacob24
 
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.pptdakccse
 
N-queens.pptx
N-queens.pptxN-queens.pptx
N-queens.pptxanoop2708
 
Unit 3 greedy method
Unit 3  greedy methodUnit 3  greedy method
Unit 3 greedy methodMaryJacob24
 

Similaire à BackTracking Algorithm: Technique and Examples (20)

algorithm Unit 4
algorithm Unit 4 algorithm Unit 4
algorithm Unit 4
 
Unit 4 jwfiles
Unit 4 jwfilesUnit 4 jwfiles
Unit 4 jwfiles
 
Data Analysis and Algorithms Lecture 1: Introduction
 Data Analysis and Algorithms Lecture 1: Introduction Data Analysis and Algorithms Lecture 1: Introduction
Data Analysis and Algorithms Lecture 1: Introduction
 
module5_backtrackingnbranchnbound_2022.pdf
module5_backtrackingnbranchnbound_2022.pdfmodule5_backtrackingnbranchnbound_2022.pdf
module5_backtrackingnbranchnbound_2022.pdf
 
backtracking 8 Queen.pptx
backtracking 8 Queen.pptxbacktracking 8 Queen.pptx
backtracking 8 Queen.pptx
 
unit-4-dynamic programming
unit-4-dynamic programmingunit-4-dynamic programming
unit-4-dynamic programming
 
Back tracking and branch and bound class 20
Back tracking and branch and bound class 20Back tracking and branch and bound class 20
Back tracking and branch and bound class 20
 
Branch and bound
Branch and boundBranch and bound
Branch and bound
 
AAC ch 3 Advance strategies (Dynamic Programming).pptx
AAC ch 3 Advance strategies (Dynamic Programming).pptxAAC ch 3 Advance strategies (Dynamic Programming).pptx
AAC ch 3 Advance strategies (Dynamic Programming).pptx
 
5.5 back track
5.5 back track5.5 back track
5.5 back track
 
Optimization problems
Optimization problemsOptimization problems
Optimization problems
 
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
 
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
 
Knapsack problem using fixed tuple
Knapsack problem using fixed tupleKnapsack problem using fixed tuple
Knapsack problem using fixed tuple
 
Unit 3- Greedy Method.pptx
Unit 3- Greedy Method.pptxUnit 3- Greedy Method.pptx
Unit 3- Greedy Method.pptx
 
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
 
Sudoku
SudokuSudoku
Sudoku
 
N-queens.pptx
N-queens.pptxN-queens.pptx
N-queens.pptx
 
Unit 3 greedy method
Unit 3  greedy methodUnit 3  greedy method
Unit 3 greedy method
 

Dernier

9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort servicejennyeacort
 
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPramod Kumar Srivastava
 
How we prevented account sharing with MFA
How we prevented account sharing with MFAHow we prevented account sharing with MFA
How we prevented account sharing with MFAAndrei Kaleshka
 
IMA MSN - Medical Students Network (2).pptx
IMA MSN - Medical Students Network (2).pptxIMA MSN - Medical Students Network (2).pptx
IMA MSN - Medical Students Network (2).pptxdolaknnilon
 
Call Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceCall Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceSapana Sha
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...Florian Roscheck
 
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Sapana Sha
 
Multiple time frame trading analysis -brianshannon.pdf
Multiple time frame trading analysis -brianshannon.pdfMultiple time frame trading analysis -brianshannon.pdf
Multiple time frame trading analysis -brianshannon.pdfchwongval
 
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档208367051
 
GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]📊 Markus Baersch
 
MK KOMUNIKASI DATA (TI)komdat komdat.docx
MK KOMUNIKASI DATA (TI)komdat komdat.docxMK KOMUNIKASI DATA (TI)komdat komdat.docx
MK KOMUNIKASI DATA (TI)komdat komdat.docxUnduhUnggah1
 
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptxEMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptxthyngster
 
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Jack DiGiovanna
 
2006_GasProcessing_HB (1).pdf HYDROCARBON PROCESSING
2006_GasProcessing_HB (1).pdf HYDROCARBON PROCESSING2006_GasProcessing_HB (1).pdf HYDROCARBON PROCESSING
2006_GasProcessing_HB (1).pdf HYDROCARBON PROCESSINGmarianagonzalez07
 
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfKantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfSocial Samosa
 
9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home ServiceSapana Sha
 
DBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfDBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfJohn Sterrett
 
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...soniya singh
 
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝soniya singh
 

Dernier (20)

Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
Deep Generative Learning for All - The Gen AI Hype (Spring 2024)Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
 
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
9711147426✨Call In girls Gurgaon Sector 31. SCO 25 escort service
 
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
 
How we prevented account sharing with MFA
How we prevented account sharing with MFAHow we prevented account sharing with MFA
How we prevented account sharing with MFA
 
IMA MSN - Medical Students Network (2).pptx
IMA MSN - Medical Students Network (2).pptxIMA MSN - Medical Students Network (2).pptx
IMA MSN - Medical Students Network (2).pptx
 
Call Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts ServiceCall Girls In Dwarka 9654467111 Escorts Service
Call Girls In Dwarka 9654467111 Escorts Service
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
 
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
 
Multiple time frame trading analysis -brianshannon.pdf
Multiple time frame trading analysis -brianshannon.pdfMultiple time frame trading analysis -brianshannon.pdf
Multiple time frame trading analysis -brianshannon.pdf
 
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档
 
GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]
 
MK KOMUNIKASI DATA (TI)komdat komdat.docx
MK KOMUNIKASI DATA (TI)komdat komdat.docxMK KOMUNIKASI DATA (TI)komdat komdat.docx
MK KOMUNIKASI DATA (TI)komdat komdat.docx
 
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptxEMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM  TRACKING WITH GOOGLE ANALYTICS.pptx
EMERCE - 2024 - AMSTERDAM - CROSS-PLATFORM TRACKING WITH GOOGLE ANALYTICS.pptx
 
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
 
2006_GasProcessing_HB (1).pdf HYDROCARBON PROCESSING
2006_GasProcessing_HB (1).pdf HYDROCARBON PROCESSING2006_GasProcessing_HB (1).pdf HYDROCARBON PROCESSING
2006_GasProcessing_HB (1).pdf HYDROCARBON PROCESSING
 
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfKantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
 
9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service9654467111 Call Girls In Munirka Hotel And Home Service
9654467111 Call Girls In Munirka Hotel And Home Service
 
DBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfDBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdf
 
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
 
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Defence Colony Delhi 💯Call Us 🔝8264348440🔝
 

BackTracking Algorithm: Technique and Examples

  • 1. Backtracking: Technique & Examples By, Fahim Ferdous Back Track Yes Solution No Solution
  • 2. Backtracking History • ‘Backtrack’ the Word was first introduced by Dr. D.H. Lehmer in 1950s. • R.J Walker Was the First man who gave algorithmic description in 1960. • Later developed by S. Golamb and L. Baumert.
  • 3. Backtracking • What is Backtracking? ----- Backtracking is nothing but the modified process of the brute force approach. where the technique systematically searches for a solution to a problem among all available options. It does so by assuming that the solutions are represented by vectors (v1, ..., in) of values and by traversing through the domains of the vectors until the solutions is found.
  • 4. Backtracking • The Algorithmic Approach – Backtracking systematically try and search possibilities to find the solution. Also it is an important process for solving constraint satisfaction problem like crossword, Sudoku and many other puzzles. It can be more continent technique for parsing other combinatorial optimization problem. – Basically the process is used when the problem has a number of option and just one solution have to be selected. After having a new option set means recursion, the procedure is repeated over and over until final stage.
  • 5. Backtracking Algorithm Backtrack (v1,Vi) If (V1,……., Vi) is a Solution Then Return (V1,…, Vi) For each v DO If (V1,…….,Vi) is acceptable vector THEN Sol = try (V1,…,Vi, V) If sol != () Then RETURN sol End End Return ( )
  • 6. Backtracking • Advantages – Comparison with the Dynamic Programming, Backtracking Approach is more effective in some cases. – Backtracking Algorithm is the best option for solving tactical problem. – Also Backtracking is effective for constraint satisfaction problem. – In greedy Algorithm, getting the Global Optimal Solution is a long procedure and depends on user statements but in Backtracking It Can Easily getable. – Backtracking technique is simple to implement and easy to code. – Different states are stored into stack so that the data or Info can be usable anytime. – The accuracy is granted.
  • 7. Backtracking • Disadvantages – Backtracking Approach is not efficient for solving strategic Problem. – The overall runtime of Backtracking Algorithm is normally slow – To solve Large Problem Sometime it needs to take the help of other techniques like Branch and bound. – Need Large amount of memory space for storing different state function in the stack for big problem. – Thrashing is one of the main problem of Backtracking. – The Basic Approach Detects the conflicts too late.
  • 8. Backtracking • Application of Backtracking • Optimization and tactical problems • Constraints Satisfaction Problem • Electrical Engineering • Robotics • Artificial Intelligence • Genetic and bioinformatics Algorithm • Materials Engineering • Network Communication • Solving puzzles and path
  • 9. Backtracking • Some Problem Solved with Backtracking Technique – N- Queens Problem – Sum of Subset – Sudoku Puzzle – Maze Generation – Hamiltonian Cycle
  • 10. N-Queens Problem • History: First Introduced in 1848 which was known as 8- queens Puzzle. Surprisingly, The First Solution was created in 1950 by Franz Nauck. Nauck made an 8X8 Chessboard to find the first Feasible Solution.
  • 11. N-Queens Problem • Problem Description In a NxN square board N – number of queens need to be placed considering three Condition --- • No two Queens can be placed in same row. • No two Queens Can be places in same Column • No two queens Can be placed in same Diagonal.
  • 12. N-Queens Problem • Backtracking approach for solution - - The Algorithm will check each position [i , j] for each queens . If any Suitable places found , It will place a queen on that position. If not Algorithm will try same approach for next position. Algorithm Nqueen(K,n){ For i= 1 to n { If Place(K,i){ X[k] = I; If(k = n) then Write x[1:n]; Else Nqueen(k+1, n) ; } } } Place(k,i){ For j=1 to k-1{ If((x[j] = i) or abs(x[j] – 1 = abs(j-k))) Return false; } Return true; }
  • 14. Maze Generation Problem Description A N X N board is made in a way So that 1 or 0 can be placed in each box where 1 is for valid path for moving towards exit and 0 is the Closed path. Now, by using Backtracking Algorithm The path to the exit need to be find.
  • 15. Maze Generation • -- Algorithm will try to follow the box which has the value 1 and generate a path. Again, it will check for 1 at the next box. If not found it will go through next box. If entire row is complete, then it will move to the next row and look for 1.
  • 16. Maze Generation • Solution Using Backtracking Technique Algorithm Maze_Gen(graph){ If(Maze_operator(graph, x,y, canvas) == false) Write (“No result”); Return false; Print_Result(canvas); Return true; } Algorithm Maze_operator(graph, x,y, canvas){ If(x ==N-1 && y == N-1){ Canvas[x][y] = 1; Return true; } If(IsSafe(graph, x,y) == true){ Canvas[x][y] = 1; If(Maze_operator(graph, x+1,y, canvas)) Return true; If(Maze_operator(graph, x,y+1, canvas)) Return true; Canvas [x][y] = 0; Return true; } Return false; }
  • 18. Hamiltonian Cycle Hamiltonian Cycle is a graph theory problem where the graph cycle through a graph can visit each node only once. The puzzle was first devised by Sir William Rowan Hamilton and the Problem is named after Him. Condition: The Cycle Started with a Starting Node, and visit all the Nodes in the Graph (Not necessary to visit in sequential Order and not creating edge that is not given) And Stop at The Starting Point/Node.
  • 19. Hamiltonian Cycle • The Backtracking Approach The Algorithm First Check the Starting Node, if there is any edge to the next node. If yes, then the Algorithm will check that node for the edge to the next Node. It will Also Check If any Node is visited twice by the previous Node. If there is any then the Algorithm Will Ignore One and Choose the Optimal One for the Solution.  The Important thing is the tour must finish at the starting point.
  • 20. Hamiltonian Cycle Algorithm Hamilton_Cycle(k){ Repeat{ NextVal(k); If(x[k]==0) then Return; If (k ==n) then Write (x[1:n]); Else Hamilton_Cycle(k+1); } Until (false); } AlgortihmNextVal(k){ Repeat{ X[k] = (x[k]+1) mod (n+1); If(x[k]=0) then return; If (G[x[k-1],x[k]] != 0 ) then{ For j = 1 to k-1 do If(x[j]=x[k]) then Break; If(j = k) then If ((k<n or k=n) && G[x[n],x[1]] != 0) Then return; } } Until (false); }
  • 22. Sudoku Puzzle • The Word ‘Sudoku’ is a Japanese Word. But The Sudoku Puzzle Was First Invented in Switzerland • In a N X N Square Board, The Numbers 1 to N to be Placed in a way that: – All the Number from 1 to N can be Placed in each row. – All the Number from 1 to N can be Placed in each Column. – All The Number from 1 to N Can be Placed in Sub Square boxes of the given box.
  • 23. Sudoku Puzzle • Backtracking Approach The Algorithm Will Check Each Box’s value if the value is in the same row, or same column or same sub-square box. If not,then it placed the value in the box. And go to the next box for the next value, and check the Above Condition. If there is any duplicate value, then The Algorithm check for the next value.
  • 24. Sudoku Puzzle Algorithm Sudoku_Solver(canvas, row, col){ If(anyEmptyLocation(canvas, row, col)) Return true; For I = 1 to n do, { If (find_location(canvas, row,col, i)) { Canvas[row][col] = i; If (Sudoku_Solver(canvas)) Return true; Canvas [row][col] = -1; } } Return false; }
  • 25. Sum of Subset • The Sum of Subset Problem is, there will be a set of distinct positive Numbers X and a given Number N. Now, we have to find all the combination of numbers from X that sum up to N. Make a Set of those Number. W = {4,5,6,3} M = 13
  • 27. Sum of Subset • Backtracking Approach First, organize the numbers in non decreasing order. Then generating a tree, we can find the solution by adding the weight of the nodes. Note that, here more than necessary nodes can be generated. The algorithm will search for the weighted amount of the nodes. If it goes beyond given Number N, then it stops generating nodes and move to other parent nodes for finding solution. Algorithm SumOfSubset(s,k,y){ X[k] = 1; If(s+w[k] = m) Write (x[1:n]); Else if((s+w[k] + w[k+1]) <= m) SumOfSubset(s+w[k], k+1, y-w[k]); If ((s+ y-w[k]>=m) &&(s =w[k+1] <=m)) { X[k] =0; SumOfSubset(s,k+1,y-w[k]); } }
  • 28. Sum of Subset Here M = 53 W = {8,9,14,15,16,22}
  • 29. References • Chapter 6, Backtracking, Design and Analysis of Algorithms – V.V. Muniswami • Chapter – 4, Non- Chronological Backtracking, Handbook on Constraint Programming – F. Rossi, Van Beek • web.cse.ohio-state.edu/~gurari/course/cis680/cis680Ch19.html • www.classle.net/faq/comparisons-divide-and-conquergreedy-methoddynamic- programmingbacktracking-and-branch-and-bound- • Backtracking algorithms for constraint satisfaction problems; a survey R. Dechter, D. Frost, in Constraints, International Journal, 1998. • Bessiere C., Maestre A., Meseguer P., Distributed dynamic backtracking, in M.C. Silaghi, editor, Proceedings of the IJCAI’01 workshop on Distributed Constraint Reasoning, Seattle WA, 2001, pp. 9–16. • A First Look at Graph Theory – Jhon Clark, Allan Holton • Principles and Practice of Constraint Programming- CP 2001: 7th International Conference, CP 2001, Paphos, Cyprus, November 26 - December 1, 2001