Académique Documents
Professionnel Documents
Culture Documents
JNANASANGAMA, BELAGAVI-590018
Submitted by
Mrs. S Mercy
Assistant Professor
Dept. of ISE,BIT
2018-19
INTRODUCTION
BACKTRACKING
Backtracking is finding the solution of a problem whereby the solution depends on the previous steps taken.
In backtracking, we first take a step and then we see if this step taken is correct or not i.e., whether it will
give a correct answer or not. And if it doesn’t, then we just come back and change our first step. In general,
this is accomplished by recursion. Thus, in backtracking, we first start with a partial sub-solution of the
problem (which may or may not lead us to the solution) and then check if we can proceed further with this
sub-solution or not. If not, then we just come back and change it.
A Maze is given as N*N binary matrix of blocks where source block is the upper left most block i.e.,
maze[0][0] and destination block is lower rightmost block i.e., maze[N-1][N-1]. A rat starts from source and
has to reach the destination. The rat can move only in two directions: forward and down.
In the maze matrix, 0 means the block is a dead end and 1 means the block can be used in the path from source
to destination. Note that this is a simple version of the typical Maze problem. For example, a more complex
version can be that the rat can move in 4 directions and a more complex version can be with a limited number
of moves.
Algorithm:
The eight queens puzzle is the problem of placing eight chess queens on an 8×8 chessboard so that no two
queens threaten each other; thus, a solution requires that no two queens share the same row,
column, or diagonal. The eight queens puzzle is an example of the more general n queens problem of
placing n non- attacking queens on an n×n chessboard, for which solutions exist for all natural numbers n
with the exception of n = 2 and n = 3.
SOURCE CODE
/* Java program to solve Rat in a Maze problem using
backtracking */
printSolution(sol);
return true;
}
return false;
}
public static void main(String args[])
{
Dept. of ISE,BIT Page 5
DESIGN AND ANALYSIS OF ALGORITHMS(17CS43)
RatMaze rat = new RatMaze();
int maze[][] = { { 1, 0, 0, 0 },
{ 1, 1, 0, 1 },
{ 0, 1, 0, 0 },
{ 1, 1, 1, 1 } };
N = maze.length;
rat.solveMaze(maze);
}
}
solveMazeUtil -It returns false if no path is possible, otherwise return true and prints the path in the form of 1s.
This is the function where we are really implementing the backtracking algorithm.
if(n==0) → If there is no space left, it means all spaces have been checked and we have got a solution.
if (isSafe(maze, x, y) == true) → We are just checking if the cell is available for us or the rat to find the path
in order to solve the problem.
OUTPUT
Time complexity
The isSafe method takes O(N!) time as it iterates through our array every time.
In each iteration of this loop, there is isSafe invocation which is O(N!) and a recursive call with a smaller
argument.
If we add all this up and define the run time as T(N). Then T(N) = O(N2) + N*T(N-1). If you draw a
recursion tree using this recurrence, the final term will be something like n3+ n!O(1). By the definition of Big
O, this can be reduced to O(n!) running time.
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.
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
APPLICATIONS
• Optimization and tactical problems
• Constraints Satisfaction Problem
• Electrical Engineering
• Robotics
• Artificial Intelligence
Dept. of ISE,BIT Page 7
DESIGN AND ANALYSIS OF ALGORITHMS(17CS43)
• Materials Engineering
• Network Communication
• Solving puzzles and path