KNIGHT’S TOUR ON
CHESSBOARD
USING BACKTRACKING
STRATEGY
By :
ABHISHEK KUMAR
SINGH
What is Knights Tour?
–The problem is that if” The knight is
placed on any block of an empty chess
board and, moving according to the
rules of chess, must visit each square
exactly once.”
–This Problem is an application of
Hamiltonian Path or cycle
BACKTRAKING STRATEGY
• Backtracking is an algorithmic paradigm that tries
different solutions until finds a solution that “works”.
Problems which are typically solved using
backtracking technique have following property in
common. These problems can only be solved by
trying every possible configuration and each
configuration is tried only once. A Naive solution for
these problems is to try all configurations and output
a configuration that follows given problem
constraints. Backtracking works in incremental way
and is an optimization over the Naive solution where
all possible configurations are generated and tried.
P
O
S
S
I
B
L
E
M
O
V
E
S
F
O
R
K
N
I
G
H
T
• The number of moves possible
depends on the position of the
knight on the board are :
In the corners there are only two
legal moves, on the squares
adjacent to the corners there are
three and in the middle of the
board there are eight.
Knights tour
This is the pictorial representation of the knights tour on the chessboard
Open Tour
Closed tour
26,534,728,821,064
Are the total no of possibilities for closed tour on 8*8
Chessboard.
Algorithm
Naïve approach to Knight’s tour problem
The Naive Algorithm is to generate all tours one by
one and check if the generated tour satisfies the
constraints.
while there are untried tours
{
generate the next tour;
if this tour covers all squares
{
print this path;
}
}
SOLVEKT()
initialise sol[N][N]
for x = 0 to N-1
for y = 0 to N-1
sol[x][y] = -1
initialise xMove[8] = { 2, 1, -1, -2, -2, -1, 1, 2 }
initialise yMove[8] = { 1, 2, 2, 1, -1, -2, -2, -1 }
sol[0][0] = 0
if SOLVEKTUTIL(0, 0, 1, sol, xMove, yMove) =false
print Solution does not exist
return false
else
printSolution(sol)
return true
SOLVEKTUTIL(x, y, movei, sol[N][N],
xMove[N], yMove[N])
int k, next_x, next_y
if movei = N*N
return true
for k = 0 to 8
next_x = x + xMove[k]
next_y = y + yMove[k]
if isSafe(next_x, next_y, sol)=true
sol[next_x][next_y] = movei
if SOLVEKTUTIL(next_x, next_y, movei+1, sol,
xMove, yMove) = true
return true
else
sol[next_x][next_y] = -1
return false
isSafe(int x, int y, int sol[N][N] )
return (x>=0 && x<N && y>=0 && Y<N &&sol[x][y]==-1)
---
• The reason for this is that the knight’s tour
problem as we have implemented it so far is an
exponential algorithm of size O(K^N), where N is
the number of squares on the chessboard,
where k is a small constant.
• Best case : In any step no backtracking is found
necessary, then Time complexity is O(N), in an
n*n chessboard. (N is no of squares on
chessboard).
TIME COMPLEXITY

Knights tour on chessboard using backtracking

  • 1.
    KNIGHT’S TOUR ON CHESSBOARD USINGBACKTRACKING STRATEGY By : ABHISHEK KUMAR SINGH
  • 2.
    What is KnightsTour? –The problem is that if” The knight is placed on any block of an empty chess board and, moving according to the rules of chess, must visit each square exactly once.” –This Problem is an application of Hamiltonian Path or cycle
  • 3.
    BACKTRAKING STRATEGY • Backtrackingis an algorithmic paradigm that tries different solutions until finds a solution that “works”. Problems which are typically solved using backtracking technique have following property in common. These problems can only be solved by trying every possible configuration and each configuration is tried only once. A Naive solution for these problems is to try all configurations and output a configuration that follows given problem constraints. Backtracking works in incremental way and is an optimization over the Naive solution where all possible configurations are generated and tried.
  • 4.
  • 5.
    • The numberof moves possible depends on the position of the knight on the board are : In the corners there are only two legal moves, on the squares adjacent to the corners there are three and in the middle of the board there are eight.
  • 6.
    Knights tour This isthe pictorial representation of the knights tour on the chessboard
  • 7.
  • 8.
    Closed tour 26,534,728,821,064 Are thetotal no of possibilities for closed tour on 8*8 Chessboard.
  • 9.
    Algorithm Naïve approach toKnight’s tour problem The Naive Algorithm is to generate all tours one by one and check if the generated tour satisfies the constraints. while there are untried tours { generate the next tour; if this tour covers all squares { print this path; } }
  • 10.
    SOLVEKT() initialise sol[N][N] for x= 0 to N-1 for y = 0 to N-1 sol[x][y] = -1 initialise xMove[8] = { 2, 1, -1, -2, -2, -1, 1, 2 } initialise yMove[8] = { 1, 2, 2, 1, -1, -2, -2, -1 } sol[0][0] = 0 if SOLVEKTUTIL(0, 0, 1, sol, xMove, yMove) =false print Solution does not exist return false else printSolution(sol) return true
  • 11.
    SOLVEKTUTIL(x, y, movei,sol[N][N], xMove[N], yMove[N]) int k, next_x, next_y if movei = N*N return true for k = 0 to 8 next_x = x + xMove[k] next_y = y + yMove[k] if isSafe(next_x, next_y, sol)=true sol[next_x][next_y] = movei if SOLVEKTUTIL(next_x, next_y, movei+1, sol, xMove, yMove) = true return true else sol[next_x][next_y] = -1 return false isSafe(int x, int y, int sol[N][N] ) return (x>=0 && x<N && y>=0 && Y<N &&sol[x][y]==-1)
  • 12.
    --- • The reasonfor this is that the knight’s tour problem as we have implemented it so far is an exponential algorithm of size O(K^N), where N is the number of squares on the chessboard, where k is a small constant. • Best case : In any step no backtracking is found necessary, then Time complexity is O(N), in an n*n chessboard. (N is no of squares on chessboard). TIME COMPLEXITY

Editor's Notes