Vous êtes sur la page 1sur 10

VISVESVARAYA TECHNOLOGICAL UNIVERSITY

JNANASANGAMA, BELAGAVI-590018

DESIGN AND ANALYSIS OF ALGORITHMS REPORT


ON
“RAT IN A MAZE PROBLEM USING
BACKTRACKING”
Submitted in partial fulfillment of the requirements for the 4th Semester
INFORMATION SCIENCE AND ENGINEERING

Submitted by

SPARSH SAWA 1BI17IS075

Under the guidance of

Mrs. S Mercy

Assistant Professor

Dept. of ISE,BIT

2018-19

DEPARTMENT OF INFORMATION SCIENCE AND ENGINEERING


BANGALORE INSTITUTE OF TECHNOLOGY
K.R.Road,V.V.Puram,Bengaluru-560004
DESIGN AND ANALYSIS OF ALGORITHMS(17CS43)

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.

Thus, the general steps of backtracking are:


• start with a sub-solution
• check if this sub-solution will lead to the solution or not
• If not, then come back and change the sub-solution and continue again

RAT IN A MAZE PROBLEM

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:

while there are untried paths


{
generate the next path
if this path has all blocks as 1
{
print this path;
}
}

Dept. of ISE,BIT Page 2


DESIGN AND ANALYSIS OF ALGORITHMS(17CS43)
If destination is reached
print the solution matrix
else
a) Mark current cell in solution matrix as 1.
b) Move forward in the horizontal direction and recursively check if this
move leads to a solution.
c) If the move chosen in the above step doesn't lead to a solution
then move down and check if this move leads to a solution.
d) If none of the above solutions works then unmark this cell as 0
(BACKTRACK) and return false.

RAT IN A 8x8 MAZE

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 */

public class RatMaze {

// Size of the maze


static int N;

/* A utility function to print solution matrix


sol[N][N] */
void printSolution(int sol[][])
{
for (int i = 0; i < N; i++) {

Dept. of ISE,BIT Page 3


DESIGN AND ANALYSIS OF ALGORITHMS(17CS43)
for (int j = 0; j < N; j++)
System.out.print(" " + sol[i][j] + " ");
System.out.println();
}
}

/* A utility function to check if x, y is valid


index for N*N maze */
boolean isSafe(int maze[][], int x, int y)
{
// if (x, y outside maze) return false
return (x >= 0 && x < N && y >= 0 && y < N && maze[x][y] == 1);
}

/* This function solves the Maze problem using


Backtracking. It mainly uses solveMazeUtil()
to solve the problem. It returns false if no
path is possible, otherwise return true and
prints the path in the form of 1s. Please note
that there may be more than one solutions, this
function prints one of the feasible solutions.*/
boolean solveMaze(int maze[][])
{
int sol[][] = new int[N][N];

if (solveMazeUtil(maze, 0, 0, sol) == false) {


System.out.print("Solution doesn't exist");
return false;
}

printSolution(sol);
return true;
}

Dept. of ISE,BIT Page 4


DESIGN AND ANALYSIS OF ALGORITHMS(17CS43)
/* A recursive utility function to solve Maze
problem */
boolean solveMazeUtil(int maze[][], int x, int y,
int sol[][])
{
// if (x, y is goal) return true
if (x == N - 1 && y == N - 1) {
sol[x][y] = 1;
return true;
}

// Check if maze[x][y] is valid


if (isSafe(maze, x, y) == true) {
// mark x, y as part of solution path
sol[x][y] = 1;

/* Move forward in x direction */


if (solveMazeUtil(maze, x + 1, y, sol))
return true; /* If moving in x direction doesn't give
solution then Move down in y direction */
if (solveMazeUtil(maze, x, y + 1, sol))
return true;

/* If none of the above movements works then


BACKTRACK: unmark x, y as part of solution
path */
sol[x][y] = 0;
return false;
}

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);
}
}

Explanation of the code

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.

Dept. of ISE,BIT Page 6


DESIGN
For each invocation AND ANALYSIS
of the solveMazeUtil method, OF
thereALGORITHMS(17CS43)
is a loop which runs for O(N!)
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

Dept. of ISE,BIT Page 8


DESIGN AND ANALYSIS OF ALGORITHMS(17CS43)

Dept. of ISE,BIT Page


DESIGN AND ANALYSIS OF ALGORITHMS(17CS43)

Dept. of ISE,BIT Page 10

Vous aimerez peut-être aussi