2. Content
Backtracking History.
What is Backtracking?
Who is using Backtracking?
How does Working?
Advantage
Disadvantage
Time complexity
2
3. 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
4. Backtracking Algorithm
Backtracking is methodical way of trying out various sequences of
decisions, until you find one that works.
Backtracking allows us to systematically try all available
Avenues from a certain point after some of them lead to nowhere.
4
5. Who is using Backtracking?
5
Using Backtracking always return to a position which offers other
possibilities for successfully solving the problem.
Example:
• n-queen problem
• graph coloring problem
• hamilton cycle
• subset sum problem
7. N-Queens Backtracking Algorithm
7
•N queens problem is one of the
most common examples of
backtracking. Our goal is to
arrange N queens on an NxN
chessboard such that no queen
can strike down any other queen.
A queen can attack horizontally,
vertically, or diagonally.
9. we will start by placing the first queen in the first row.
9
10. Now, the second step is to place the second queen in a safe position. Also, we can't
place the queen in the first row, so we will try putting the queen in the second row
this time.
10
11. Let's place the third queen in a safe position, somewhere in the third
row.
11
15. Advantage
15
1. Comparison with the Dynamic Programming, Backtracking
Approach is more effective in some cases.
2. Backtracking Algorithm is the best option for solving tactical
problem.
3. In greedy Algorithm, getting the Global Optimal Solution is a
long procedure and depends on user statements but in
Backtracking It Can Easily getable.
4. Backtracking technique is simple to implement and easy to code.
5. The accuracy is granted.
16. Disadvantages
16
1. Backtracking Approach is not efficient for solving strategic
Problem.
2. The overall runtime of Backtracking Algorithm is normally slow
3. To solve Large Problem Sometime it needs to take the help of
other techniques like Branch and bound.
4. Need Large amount of memory space for storing different state
function in the stack for big problem.
17. Pseudo Code
17
boolean findSolutions(n, other params) :
if (found a solution) :
displaySolution();
return true;
for (val = first to last) :
if (isValid(val, n)) :
applyValue(val, n);
if (findSolutions(n+1, other params))
return true;
removeValue(val, n);
return false;
18. Time Complexity
18
The time complexity of the algorithm will be a measure specific to
what problem we are trying to solve.
1. n-queen problem: O(n!)
2. subset sum problem:O(nW)