You are on page 1of 16

Backtracking

Introduction to Backtracking
 Backtracking makes it possible to solve at least
some large instances of difficult combinatorial
problems.
 Suppose you have to make a series of decisions
among various choices, where
◦ You don’t have enough information to know what to
choose
◦ Each decision leads to a new set of choices.
◦ Some sequence of choices (more than one choices)
may be a solution to your problem.
 Backtracking is a methodical (Logical) way
of trying out various sequences of decisions,
until you find one that “works”.
Backtracking
 Backtracking is a technique used to solve
problems with a large search space, by
systematically trying and eliminating possibilities.

 A standard example of backtracking would be


going through a maze.
◦ At some point in a maze, you might have two options
of which direction to go:

Portion A
Portion B
Backtracking
 One strategy would be
to try going through
Portion A of the maze.
Portion B
 If you get stuck before
you find your way out,

Portion A
then you "backtrack"
to the junction.

 At this point in time you


know that Portion A
will NOT lead you out
of the maze,
 so you then start
searching in Portion B
Backtracking
 Clearly, at a single junction
you could have even more
than 2 choices.

 The backtracking strategy


says to try each choice, one
after the other,
◦ if you ever get stuck,
"backtrack" to the junction C
and try the next choice. B
A
 If you try all choices and
never found a way out, then
there is NO SOLUTION
to the maze.
 Many of the problems we solve using
backtracking requires that all the
solutions satisfy a complex set of
constraints.
 For any problem these constraints can be
divided into two categories:
◦ Explicit constraints.
◦ Implicit constraints.
 Explicit constraints
◦ Explicit constraints are rules that restrict each
xi to take on values only from a given set
 Implicit constraints
◦ Implicit constraints describe the way in which
the xi must relate to each other
Applications of Backtracking:
 N Queens Problem
 Sum of subsets problem
 Graph coloring
 Hamiltonian cycles.
N queens Problem
 This problem is to find an arrangement of N
queens on a chess board, such that no queen
can attack any other queens on the board.
 The chess queens can attack in any direction as
horizontal, vertical, and diagonal way.
One of the Solution to 4-Queens
problem
Backtracking – Eight Queens Problem
 Find an arrangement of 8
queens on a single chess
board such that no two
queens are attacking one
another.

 In chess, queens can move all


the way down any row,
column or diagonal (so long
as no pieces are in the way).

◦ Due to the first two


restrictions, it's clear that each
row and column of the board
will have exactly one queen.
Backtracking – Eight Queens Problem
 The backtracking strategy is as Q
follows:
1) Place a queen on the first Q
available square in row 1. Q
Q
2) Move onto the next row,
placing a queen on the first Q Q
available square there (that
doesn't conflict with the
previously placed queens). Continue…
3) Continue in this fashion until
either:
a) you have solved the problem, or
b) you get stuck.
 When you get stuck, remove the
queens that got you there, until you
get to a row where there is another
valid square to try.
Backtracking – Eight Queens Problem
 When we carry out backtracking, an easy
way to visualize what is going on is a tree
that shows all the different possibilities
that have been tried.
 On the board we will show a visual
representation of solving the 4 Queens
problem (placing 4 queens on a 4x4 board
where no two attack one another).
 Algorithm:
 Place the queens column wise, start from the left most
column
 If all queens are placed.
◦ return true and print the solution matrix.
 Else
◦ Try all the rows in the current column.
◦ Check if queen can be placed here safely if yes mark the current
cell in solution matrix as 1 and try to solve the rest of the
problem recursively.
◦ If placing the queen in above step leads to the solution, return
true.
◦ If placing the queen in above step does not lead to the solution ,
BACKTRACK, mark the current cell in solution matrix as 0 and
return false.
 If all the rows are tried and nothing worked, return false
and print NO SOLUTION.

You might also like