Backtracking is an algorithmic paradigm that tries different solutions until finds a solution that “works”. Problems that are typically solved using the backtracking technique have the 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 incrementally and is an optimization over the Naive solution where all possible configurations are generated and tried.
For example, consider the following Knight’s Tour problem.
Problem Statement:
Given a N*N board with the Knight placed on the first block of an empty board. Moving according to the rules of chess knight must visit each square exactly once. Print the order of each cell in which they are visited.
Example:
Input :
N = 8
Output:
0 59 38 33 30 17 8 63
37 34 31 60 9 62 29 16
58 1 36 39 32 27 18 7
35 48 41 26 61 10 15 28
42 57 2 49 40 23 6 19
47 50 45 54 25 20 11 14
56 43 52 3 22 13 24 5
51 46 55 44 53 4 21 12
The path followed by Knight to cover all the cells
Following is a chessboard with 8 x 8 cells. Numbers in cells indicate the move number of Knight.

Let us first discuss the Naive algorithm for this problem and then the Backtracking algorithm.
Naive Algorithm for Knight’s tour
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;
}
}
Backtracking works in an incremental way to attack problems. Typically, we start from an empty solution vector and one by one add items (Meaning of item varies from problem to problem. In the context of Knight’s tour problem, an item is a Knight’s move). When we add an item, we check if adding the current item violates the problem constraint, if it does then we remove the item and try other alternatives. If none of the alternatives works out then we go to the previous stage and remove the item added in the previous stage. If we reach the initial stage back then we say that no solution exists. If adding an item doesn’t violate constraints then we recursively add items one by one. If the solution vector becomes complete then we print the solution.
Backtracking Algorithm for Knight’s tour
Following is the Backtracking algorithm for Knight’s tour problem.
If all squares are visited
print the solution
Else
a) Add one of the next moves to solution vector and recursively
check if this move leads to a solution. (A Knight can make maximum
eight moves. We choose one of the 8 moves in this step).
b) If the move chosen in the above step doesn't lead to a solution
then remove this move from the solution vector and try other
alternative moves.
c) If none of the alternatives work then return false (Returning false
will remove the previously added item in recursion and if false is
returned by the initial call of recursion then "no solution exists" )
Following are implementations for Knight’s tour problem. It prints one of the possible solutions in 2D matrix form. Basically, the output is a 2D 8*8 matrix with numbers from 0 to 63 and these numbers show steps made by Knight.
C++
#include <bits/stdc++.h>
using namespace std;
#define N 8
int solveKTUtil( int x, int y, int movei, int sol[N][N],
int xMove[], int yMove[]);
int isSafe( int x, int y, int sol[N][N])
{
return (x >= 0 && x < N && y >= 0 && y < N
&& sol[x][y] == -1);
}
void printSolution( int sol[N][N])
{
for ( int x = 0; x < N; x++) {
for ( int y = 0; y < N; y++)
cout << " " << setw(2) << sol[x][y] << " " ;
cout << endl;
}
}
int solveKT()
{
int sol[N][N];
for ( int x = 0; x < N; x++)
for ( int y = 0; y < N; y++)
sol[x][y] = -1;
int xMove[8] = { 2, 1, -1, -2, -2, -1, 1, 2 };
int yMove[8] = { 1, 2, 2, 1, -1, -2, -2, -1 };
sol[0][0] = 0;
if (solveKTUtil(0, 0, 1, sol, xMove, yMove) == 0) {
cout << "Solution does not exist" ;
return 0;
}
else
printSolution(sol);
return 1;
}
int solveKTUtil( int x, int y, int movei, int sol[N][N],
int xMove[8], int yMove[8])
{
int k, next_x, next_y;
if (movei == N * N)
return 1;
for (k = 0; k < 8; k++) {
next_x = x + xMove[k];
next_y = y + yMove[k];
if (isSafe(next_x, next_y, sol)) {
sol[next_x][next_y] = movei;
if (solveKTUtil(next_x, next_y, movei + 1, sol,
xMove, yMove)
== 1)
return 1;
else
sol[next_x][next_y] = -1;
}
}
return 0;
}
int main()
{
solveKT();
return 0;
}
|
C
#include <stdio.h>
#define N 8
int solveKTUtil( int x, int y, int movei, int sol[N][N],
int xMove[], int yMove[]);
int isSafe( int x, int y, int sol[N][N])
{
return (x >= 0 && x < N && y >= 0 && y < N
&& sol[x][y] == -1);
}
void printSolution( int sol[N][N])
{
for ( int x = 0; x < N; x++) {
for ( int y = 0; y < N; y++)
printf ( " %2d " , sol[x][y]);
printf ( "\n" );
}
}
int solveKT()
{
int sol[N][N];
for ( int x = 0; x < N; x++)
for ( int y = 0; y < N; y++)
sol[x][y] = -1;
int xMove[8] = { 2, 1, -1, -2, -2, -1, 1, 2 };
int yMove[8] = { 1, 2, 2, 1, -1, -2, -2, -1 };
sol[0][0] = 0;
if (solveKTUtil(0, 0, 1, sol, xMove, yMove) == 0) {
printf ( "Solution does not exist" );
return 0;
}
else
printSolution(sol);
return 1;
}
int solveKTUtil( int x, int y, int movei, int sol[N][N],
int xMove[N], int yMove[N])
{
int k, next_x, next_y;
if (movei == N * N)
return 1;
for (k = 0; k < 8; k++) {
next_x = x + xMove[k];
next_y = y + yMove[k];
if (isSafe(next_x, next_y, sol)) {
sol[next_x][next_y] = movei;
if (solveKTUtil(next_x, next_y, movei + 1, sol,
xMove, yMove)
== 1)
return 1;
else
sol[next_x][next_y] = -1;
}
}
return 0;
}
int main()
{
solveKT();
return 0;
}
|
Java
class KnightTour {
static int N = 8 ;
static boolean isSafe( int x, int y, int sol[][])
{
return (x >= 0 && x < N && y >= 0 && y < N
&& sol[x][y] == - 1 );
}
static void printSolution( int sol[][])
{
for ( int x = 0 ; x < N; x++) {
for ( int y = 0 ; y < N; y++)
System.out.print(sol[x][y] + " " );
System.out.println();
}
}
static boolean solveKT()
{
int sol[][] = new int [ 8 ][ 8 ];
for ( int x = 0 ; x < N; x++)
for ( int y = 0 ; y < N; y++)
sol[x][y] = - 1 ;
int xMove[] = { 2 , 1 , - 1 , - 2 , - 2 , - 1 , 1 , 2 };
int yMove[] = { 1 , 2 , 2 , 1 , - 1 , - 2 , - 2 , - 1 };
sol[ 0 ][ 0 ] = 0 ;
if (!solveKTUtil( 0 , 0 , 1 , sol, xMove, yMove)) {
System.out.println( "Solution does not exist" );
return false ;
}
else
printSolution(sol);
return true ;
}
static boolean solveKTUtil( int x, int y, int movei,
int sol[][], int xMove[],
int yMove[])
{
int k, next_x, next_y;
if (movei == N * N)
return true ;
for (k = 0 ; k < 8 ; k++) {
next_x = x + xMove[k];
next_y = y + yMove[k];
if (isSafe(next_x, next_y, sol)) {
sol[next_x][next_y] = movei;
if (solveKTUtil(next_x, next_y, movei + 1 ,
sol, xMove, yMove))
return true ;
else
sol[next_x][next_y]
= - 1 ;
}
}
return false ;
}
public static void main(String args[])
{
solveKT();
}
}
|
Python3
n = 8
def isSafe(x, y, board):
if (x > = 0 and y > = 0 and x < n and y < n and board[x][y] = = - 1 ):
return True
return False
def printSolution(n, board):
for i in range (n):
for j in range (n):
print (board[i][j], end = ' ' )
print ()
def solveKT(n):
board = [[ - 1 for i in range (n)] for i in range (n)]
move_x = [ 2 , 1 , - 1 , - 2 , - 2 , - 1 , 1 , 2 ]
move_y = [ 1 , 2 , 2 , 1 , - 1 , - 2 , - 2 , - 1 ]
board[ 0 ][ 0 ] = 0
pos = 1
if ( not solveKTUtil(n, board, 0 , 0 , move_x, move_y, pos)):
print ( "Solution does not exist" )
else :
printSolution(n, board)
def solveKTUtil(n, board, curr_x, curr_y, move_x, move_y, pos):
if (pos = = n * * 2 ):
return True
for i in range ( 8 ):
new_x = curr_x + move_x[i]
new_y = curr_y + move_y[i]
if (isSafe(new_x, new_y, board)):
board[new_x][new_y] = pos
if (solveKTUtil(n, board, new_x, new_y, move_x, move_y, pos + 1 )):
return True
board[new_x][new_y] = - 1
return False
if __name__ = = "__main__" :
solveKT(n)
|
C#
using System;
class GFG {
static int N = 8;
static bool isSafe( int x, int y, int [, ] sol)
{
return (x >= 0 && x < N && y >= 0 && y < N
&& sol[x, y] == -1);
}
static void printSolution( int [, ] sol)
{
for ( int x = 0; x < N; x++) {
for ( int y = 0; y < N; y++)
Console.Write(sol[x, y] + " " );
Console.WriteLine();
}
}
static bool solveKT()
{
int [, ] sol = new int [8, 8];
for ( int x = 0; x < N; x++)
for ( int y = 0; y < N; y++)
sol[x, y] = -1;
int [] xMove = { 2, 1, -1, -2, -2, -1, 1, 2 };
int [] yMove = { 1, 2, 2, 1, -1, -2, -2, -1 };
sol[0, 0] = 0;
if (!solveKTUtil(0, 0, 1, sol, xMove, yMove)) {
Console.WriteLine( "Solution does "
+ "not exist" );
return false ;
}
else
printSolution(sol);
return true ;
}
static bool solveKTUtil( int x, int y, int movei,
int [, ] sol, int [] xMove,
int [] yMove)
{
int k, next_x, next_y;
if (movei == N * N)
return true ;
for (k = 0; k < 8; k++) {
next_x = x + xMove[k];
next_y = y + yMove[k];
if (isSafe(next_x, next_y, sol)) {
sol[next_x, next_y] = movei;
if (solveKTUtil(next_x, next_y, movei + 1,
sol, xMove, yMove))
return true ;
else
sol[next_x, next_y] = -1;
}
}
return false ;
}
public static void Main()
{
solveKT();
}
}
|
Javascript
<script>
let N = 8;
function isSafe(x, y, sol)
{
return (x >= 0 && x < N && y >= 0 &&
y < N && sol[x][y] == -1);
}
function printSolution(sol)
{
for (let x = 0; x < N; x++)
{
for (let y = 0; y < N; y++)
document.write(sol[x][y] + " " );
document.write( "<br/>" );
}
}
function solveKT()
{
let sol = new Array(8);
for ( var i = 0; i < sol.length; i++)
{
sol[i] = new Array(2);
}
for (let x = 0; x < N; x++)
for (let y = 0; y < N; y++)
sol[x][y] = -1;
let xMove = [ 2, 1, -1, -2, -2, -1, 1, 2 ];
let yMove = [ 1, 2, 2, 1, -1, -2, -2, -1 ];
sol[0][0] = 0;
if (!solveKTUtil(0, 0, 1, sol, xMove, yMove))
{
document.write( "Solution does not exist" );
return false ;
}
else
printSolution(sol);
return true ;
}
function solveKTUtil(x, y, movei, sol, xMove, yMove)
{
let k, next_x, next_y;
if (movei == N * N)
return true ;
for (k = 0; k < 8; k++)
{
next_x = x + xMove[k];
next_y = y + yMove[k];
if (isSafe(next_x, next_y, sol))
{
sol[next_x][next_y] = movei;
if (solveKTUtil(next_x, next_y, movei + 1,
sol, xMove, yMove))
return true ;
else
sol[next_x][next_y] = -1;
}
}
return false ;
}
solveKT();
</script>
|
Output 0 59 38 33 30 17 8 63
37 34 31 60 9 62 29 16
58 1 36 39 32 27 18 7
35 48 41 26 61 10 15 28
42 57 2 49 40 23 6 19
47 50 45 54 25 20 11 14
56 43 52 3 22 13 24 5
51 46 55 44 53 4 21 12
Time Complexity :
There are N2 Cells and for each, we have a maximum of 8 possible moves to choose from, so the worst running time is O(8N^2).
Auxiliary Space: O(N2)
Important Note:
No order of the xMove, yMove is wrong, but they will affect the running time of the algorithm drastically. For example, think of the case where the 8th choice of the move is the correct one, and before that our code ran 7 different wrong paths. It’s always a good idea a have a heuristic than to try backtracking randomly. Like, in this case, we know the next step would probably be in the south or east direction, then checking the paths which lead their first is a better strategy.
Note that Backtracking is not the best solution for the Knight’s tour problem. See the below article for other better solutions. The purpose of this post is to explain Backtracking with an example.
Warnsdorff’s algorithm for Knight’s tour problem
References:
http://see.stanford.edu/materials/icspacs106b/H19-RecBacktrackExamples.pdf
http://www.cis.upenn.edu/~matuszek/cit594-2009/Lectures/35-backtracking.ppt
http://mathworld.wolfram.com/KnightsTour.html
http://en.wikipedia.org/wiki/Knight%27s_tour
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.