Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
using Backtracking
BACK TRACKING TECHNIQUE
• Backtracking is a designing technique used to solve a series of sub-problems of
each of which may have many solutions to a sub problem.
• From the various solutions, choose one solution for the first sub-problem this may
affect the possible solutions of later sub-problems.
• So, to solve the first sub-problem, and then solve other sub-problem based on this
solution in a recursive manner.
• This algorithm terminates when there are no more solutions to the first sub-
problem.
• The application that uses
• n queen problem,
• Hamiltonian Cycle Problem,
• 9Graph Coloring problem ,
• Tower of Hanoi problem, etc.
N-Queens Problem
• N-Queens problem is to place n-queens in such a manner on an n x n
chessboard that no two queens attack each other by being in the
same row, column or diagonal.
• It can be seen that —
• For n=1 then the problem has a trivial solution
• For n=2 then no solution exists
• For n=3 then no solution exists
• So, first we will consider the 4-queens problem and then generalize it
to n-queens problem.
4 Queens Problem
• In 4- queens problem, we have 4 queens to be placed on a 4*4
chessboard, satisfying the constraint that no two queens should be in
the same row, same column, or in same diagonal.
• The solution space according to the external constraints consists of 4
to the power 4, 4-tuples
• i.e., Si = {1, 2, 3, 4} and 1<= I <=4, whereas according to the internal
constraints they consist of 4! solutions i.e., permutation of 4.
Solution of 4 – queen’s using backtracking