0% found this document useful (0 votes)
63 views81 pages

Backtracking

Uploaded by

Trishla Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views81 pages

Backtracking

Uploaded by

Trishla Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 81

Design and Analysis of Algorithm

Backtracking
(Hamiltonian Cycle, Graph Coloring,
Sum of subset and N-Queen)

Lecture – 36-39
Overview

• Backtracking is a general algorithmic technique


that consider searching every possible
combinations in order to solve an optimization
problem.
• ‘Backtrack’ the Word was first introduced by Dr.
D.H. Lehmer in 1950s. R.J Walker Was the First
man who gave algorithmic description in 1960.
Later developed by S. Golamb and L. Baumert.
• Based on DFS search.
Backtracking
• What is Backtracking?
• Backtracking is nothing but the modified process
of the brute force approach. where the technique
systematically searches for a solution to a
problem among all available options. It does so by
assuming that the solutions are represented by
vectors (v1, ..., in) of values and by traversing
through the domains of the vectors until the
solutions is found.
Backtracking
• Algorithmic Approach
1. Test whether solution has been found?
2. If found solution , return it
3. Else for each choice that can be made
a) Make that choice
b) Recur
c) If recurrence returns a solution, return it.
4. If no choice remain, return failure.
Some time called “search tree”.
Backtracking
• Problems
1. Hamiltonian cycle
2. Graph Coloring
3. Sum of subset
4. N-Queen
Backtracking
• Problems
1. Hamiltonian cycle
2. Graph Coloring
3. Sum of subset
4. N-Queen
Backtracking
Backtracking
Backtracking
• Problem 1 : Hamiltonian cycle

1 2 3 1234561
1265431
1625431
6 5 4

2 3
123541
5 124531
1 4
Backtracking
• Problem 1 : Hamiltonian cycle

1 2 3

Hamiltonian cycle is
4 not exist in this
graph
5 6 7

1 5
Hamiltonian cycle is
2 3 not exist in this graph
because of Articulation
6 point vertex.
4
Backtracking
• Problem 1 : Hamiltonian cycle

1
Hamiltonian cycle is
2 3 not exist in this graph
because of Pendent
vertex.
4

5 6

Let’s find how backtracking is help us for finding


the Hamiltonian cycle.
Backtracking
Backtracking
• Problem 1 : Hamiltonian cycle
Algorithm:
Recursive algorithm that finds all Hamiltonian cycles
void Hamiltonian(int k)
// This algorithm uses the recursive formulation of backtracking to find all the Hamiltonian
// cycles of a graph. The graph is stored as an adjacency matrix G[1:n][1:n]. All cycles
// begin at node 1.
{
repeat
{ // Generate values for x[k].
NextValue(k); // Assign a legal next value to x[k].
if (x[k]==0) the return;
if (k == n) then print([1:n])
else Hamiltonian(k+1);
} until (false);
}
Backtracking
• Problem 1 : Hamiltonian cycle
Algorithm:
void NextValue(int k)
// x[1],...,x[k-1] is a path of k-1 distinct vertices. If x[k]==0, then no vertex has as yet
// been assigned to x[k]. After execution x[k] is assigned to the lowest numbered vertex
// which does not already appear in x[1],x[2],...,x[k-1]; and is connected by an edge to
// [k-1] Otherwise x[k]==0, If k==n, then in addition x[k] is connected to x[1].
{
repeat
{
x[k] = (x[k]+1) % (n+1); // Next vertex
if (x[k]==0) then return;
if (G[x[k-1]][x[k]] != 0) then
{ // Is there an edge?
for (j=1 to k-1) do
if (x[j]==x[k]) then break;
// Check for distinctness.
if (j==k) then // If true, then the vertex is distinct.
if ((k<n) || ((k==n) && G[x[n]][x[1]]!=0))
then return;
}
} until(false);
}
Backtracking
• Problem 1 : Hamiltonian cycle
• Example 1: Find the Hamiltonian cycle of the given
graph by using backtracking.

1 2

5 4
Backtracking
• Problem 1 : Hamiltonian cycle
• Example 1: Find the Hamiltonian cycle of the given
graph by using backtracking.
1 2 3 4 5
1 0 1 1 0 1
1 2 2 1 0 1 1 1
3 1 1 0 1 0
3
4 0 1 1 0 1
5 4 5 1 1 0 1 0
Adjacent Matrix
Backtracking
• Problem 1 : Hamiltonian cycle
• Example 1: Find the Hamiltonian cycle of the given
graph by using backtracking.
1 2 3 4 5
1 2
1 0 1 1 0 1
2 1 0 1 1 1
3
3 1 1 0 1 0
5 4 4 0 1 1 0 1
5 1 1 0 1 0
Adjacent Matrix
1 2 3 4 5
0 0 0 0 0

State Space Tree


Backtracking
• Problem 1 : Hamiltonian cycle
• Example 1: Find the Hamiltonian cycle of the given
graph by using backtracking.
1 2 3 4 5
1
1 2
1 0 1 1 0 1
2 1 0 1 1 1
3
3 1 1 0 1 0
5 4 4 0 1 1 0 1
5 1 1 0 1 0
Adjacent Matrix
1 2 3 4 5
1 0 0 0 0

State Space Tree


Backtracking
• Problem 1 : Hamiltonian cycle
• Example 1: Find the Hamiltonian cycle of the given
graph by using backtracking.
1 2 3 4 5
1
1 2
1 0 1 1 0 1
2 1 0 1 1 1
3
3 1 1 0 1 0
5 4 4 0 1 1 0 1
5 1 1 0 1 0
Adjacent Matrix
1 2 3 4 5
1 1 0 0 0

Node 1 already selected so we


can’t select it again. So we go
State Space Tree for node 2.
Backtracking
• Problem 1 : Hamiltonian cycle
• Example 1: Find the Hamiltonian cycle of the given
graph by using backtracking.
1 2 3 4 5
1
1 2
1 0 1 1 0 1
2 2 1 0 1 1 1
3
3 1 1 0 1 0
5 4 4 0 1 1 0 1
5 1 1 0 1 0
Adjacent Matrix
1 2 3 4 5
1 2 0 0 0

Node 1 already selected so we


can’t select it again. So we go
State Space Tree for node 2.
Backtracking
• Problem 1 : Hamiltonian cycle
• Example 1: Find the Hamiltonian cycle of the given
graph by using backtracking.
1 2 3 4 5
1
1 2
1 0 1 1 0 1
2 2 1 0 1 1 1
3
3 1 1 0 1 0
5 4 4 0 1 1 0 1
5 1 1 0 1 0
Adjacent Matrix
1 2 3 4 5
1 2 0 0 0

Node 1 already selected so we


can’t select it again. So we go
State Space Tree for node 2.
Backtracking
• Problem 1 : Hamiltonian cycle
• Example 1: Find the Hamiltonian cycle of the given
graph by using backtracking.
1 2 3 4 5
1
1 2
1 0 1 1 0 1
2 2 1 0 1 1 1
3
3 1 1 0 1 0
5 4 4 0 1 1 0 1
5 1 1 0 1 0
Adjacent Matrix
1 2 3 4 5
1 2 1 0 0

Node 1 already selected so we


can’t select it again. So we go
State Space Tree for node 2.
Backtracking
• Problem 1 : Hamiltonian cycle
• Example 1: Find the Hamiltonian cycle of the given
graph by using backtracking.
1 2 3 4 5
1
1 2
1 0 1 1 0 1
2 2 1 0 1 1 1
3
3 1 1 0 1 0
5 4 4 0 1 1 0 1
5 1 1 0 1 0
Adjacent Matrix
1 2 3 4 5
1 2 2 0 0

Node 2 already selected so we


can’t select it again. So we go
State Space Tree for node 3.
Backtracking
• Problem 1 : Hamiltonian cycle
• Example 1: Find the Hamiltonian cycle of the given
graph by using backtracking.
1 2 3 4 5
1
1 2
1 0 1 1 0 1
2 2 1 0 1 1 1
3
3 1 1 0 1 0
3 5 4 4 0 1 1 0 1
5 1 1 0 1 0
Adjacent Matrix
1 2 3 4 5
1 2 3 0 0

Node 2 already selected so we


can’t select it again. So we go
State Space Tree for node 3.
Backtracking
• Problem 1 : Hamiltonian cycle
• Example 1: Find the Hamiltonian cycle of the given
graph by using backtracking.
1 2 3 4 5
1
1 2
1 0 1 1 0 1
2 2 1 0 1 1 1
3
3 1 1 0 1 0
3 5 4 4 0 1 1 0 1
5 1 1 0 1 0
Adjacent Matrix
1 2 3 4 5
1 2 3 0 0

Node 2 already selected so we


can’t select it again. So we go
State Space Tree for node 3.
Backtracking
• Problem 1 : Hamiltonian cycle
• Example 1: Find the Hamiltonian cycle of the given
graph by using backtracking.
1 2 3 4 5
1
1 2
1 0 1 1 0 1
2 2 1 0 1 1 1
3
3 1 1 0 1 0
3 5 4 4 0 1 1 0 1
5 1 1 0 1 0
Adjacent Matrix
1 2 3 4 5
1 2 3 1 0

Node 1 already selected so we


can’t select it again. So we go
State Space Tree for node 2.
Backtracking
• Problem 1 : Hamiltonian cycle
• Example 1: Find the Hamiltonian cycle of the given
graph by using backtracking.
1 2 3 4 5
1
1 2
1 0 1 1 0 1
2 2 1 0 1 1 1
3
3 1 1 0 1 0
3 5 4 4 0 1 1 0 1
5 1 1 0 1 0
Adjacent Matrix
1 2 3 4 5
1 2 3 2 0

Node 2 already selected so we


can’t select it again. So we go
State Space Tree for node 3.
Backtracking
• Problem 1 : Hamiltonian cycle
• Example 1: Find the Hamiltonian cycle of the given
graph by using backtracking.
1 2 3 4 5
1
1 2
1 0 1 1 0 1
2 2 1 0 1 1 1
3
3 1 1 0 1 0
3 5 4 4 0 1 1 0 1
5 1 1 0 1 0
Adjacent Matrix
1 2 3 4 5
1 2 3 3 0

Node 3 already selected so we


can’t select it again. So we go
State Space Tree for node 4.
Backtracking
• Problem 1 : Hamiltonian cycle
• Example 1: Find the Hamiltonian cycle of the given
graph by using backtracking.
1 2 3 4 5
1
1 2
1 0 1 1 0 1
2 2 1 0 1 1 1
3
3 1 1 0 1 0
3 5 4 4 0 1 1 0 1
5 1 1 0 1 0
Adjacent Matrix
4
1 2 3 4 5
1 2 3 4 0

Node 3 already selected so we


can’t select it again. So we go
State Space Tree for node 4.
Backtracking
• Problem 1 : Hamiltonian cycle
• Example 1: Find the Hamiltonian cycle of the given
graph by using backtracking.
1 2 3 4 5
1
1 2
1 0 1 1 0 1
2 2 1 0 1 1 1
3
3 1 1 0 1 0
3 5 4 4 0 1 1 0 1
5 1 1 0 1 0
Adjacent Matrix
4
1 2 3 4 5
1 2 3 4 1

Node 1 already selected so we


can’t select it again. So we go
State Space Tree for node 2.
Backtracking
• Problem 1 : Hamiltonian cycle
• Example 1: Find the Hamiltonian cycle of the given
graph by using backtracking.
1 2 3 4 5
1
1 2
1 0 1 1 0 1
2 2 1 0 1 1 1
3
3 1 1 0 1 0
3 5 4 4 0 1 1 0 1
5 1 1 0 1 0
Adjacent Matrix
4
1 2 3 4 5
1 2 3 4 2

Node 2 already selected so we


can’t select it again. So we go
State Space Tree for node 3.
Backtracking
• Problem 1 : Hamiltonian cycle
• Example 1: Find the Hamiltonian cycle of the given
graph by using backtracking.
1 2 3 4 5
1
1 2
1 0 1 1 0 1
2 2 1 0 1 1 1
3
3 1 1 0 1 0
3 5 4 4 0 1 1 0 1
5 1 1 0 1 0
Adjacent Matrix
4
1 2 3 4 5
1 2 3 4 3

Node 3 already selected so we


can’t select it again. So we go
State Space Tree for node 4.
Backtracking
• Problem 1 : Hamiltonian cycle
• Example 1: Find the Hamiltonian cycle of the given
graph by using backtracking.
1 2 3 4 5
1
1 2
1 0 1 1 0 1
2 2 1 0 1 1 1
3
3 1 1 0 1 0
3 5 4 4 0 1 1 0 1
5 1 1 0 1 0
Adjacent Matrix
4
1 2 3 4 5
1 2 3 4 5
5

Node 4 already selected so we


can’t select it again. So we go
State Space Tree for node 5.
Backtracking
• Problem 1 : Hamiltonian cycle
• Example 1: Find the Hamiltonian cycle of the given
graph by using backtracking.
1 2 3 4 5
1
1 2
1 0 1 1 0 1
2 2 1 0 1 1 1
3
3 1 1 0 1 0
3 5 4 4 0 1 1 0 1
5 1 1 0 1 0
Adjacent Matrix
4
1 2 3 4 5
1 2 3 4 5
5

State Space Tree


Backtracking
• Problem 1 : Hamiltonian cycle
• Example 1: Find the Hamiltonian cycle of the given
graph by using backtracking.
1 2 3 4 5
1
1 2
1 0 1 1 0 1
2 2 1 0 1 1 1
3
3 1 1 0 1 0
3 5 4 4 0 1 1 0 1
5 1 1 0 1 0
Adjacent Matrix
4
1 2 3 4 5
1 2 3 4 5
5
State Space Tree
The complete state space tree is shown in next slide.
Backtracking
• Problem 1 : Hamiltonian cycle
• Example 1: Find the Hamiltonian cycle of the given
graph by using backtracking.
1 2 3 4 5
1
1 2
1 0 1 1 0 1
2 2 1 0 1 1 1
3
3 1 1 0 1 0
3 4 5 5 4 4 0 1 1 0 1
5 1 1 0 1 0
Adjacent Matrix
3 3 4
4
1 2 3 4 5
1 2 3 4 5
5 5
State Space Tree
Similarly do it for starting node 2.(Self practice)
Backtracking
• Problem 1 : Hamiltonian cycle
• Example 1: Find the Hamiltonian cycle of the given
graph by using backtracking.
1 2 3 4 5
1
1 2
1 0 1 1 0 1
2 2 1 0 1 1 1
3
3 1 1 0 1 0
3 4 5 5 4 4 0 1 1 0 1
5 1 1 0 1 0
Adjacent Matrix
3 5 4
4
1 2 3 4 5
1 2 3 4 5
5 3
State Space Tree
Similarly do it for starting node 2.(Self practice)
Backtracking
• Problems
1. Hamiltonian cycle
2. Graph Coloring
3. Sum of subset
4. N-Queen
Backtracking
• Problem 2 : Graph Coloring
• Given an undirected graph and a number m,
determine if the graph can be colored with at most
m colors such that no two adjacent vertices of the
graph are colored with same color. (Hear coloring
of a graph means assignment of colors to all
vertices)
Backtracking
Backtracking
Backtracking
• Problem 2 : Graph Coloring
• Example: Colored the following graph G by
using three colour (Red, Green and Blue) and
draw the state space tree by using Brute force
method.

1 2

4 3
Backtracking
• Problem 2 : Graph Coloring
• Example: Colored the following graph G by using three colour
(Red, Green and Blue) and draw the state space tree by
using Brute force method.
Root
1 2

4 3
Graph G

State space tree


(Brute force method)
Backtracking
• Problem 2 : Graph Coloring
• Example: Colored the following graph G by using three colour
(Red, Green and Blue) and draw the state space tree by
using Brute force method.
Root
1 2

4 3
Graph G

State space tree


(Brute force method)
Backtracking
• Problem 2 : Graph Coloring
• Example: Colored the following graph G by using three colour
(Red, Green and Blue) and draw the state space tree by
using Backtracking method.
Root 1 2

4 3
Graph G

State space tree (Backtracking)


Backtracking
• Problem 2 : Graph Coloring
• Example: Colored the following graph G by using three colour
(Red, Green and Blue) and draw the state space tree by
using Backtracking method.
Root 1 2

4 3
Graph G

State space tree (Backtracking)


(Possible Solutions)
Backtracking
• Problem 2 : Graph Coloring
• Example: Colored the following graph G by using three colour
(Red, Green and Blue) and draw the state space tree by
using Backtracking method.
Root 1 2

4 3
Graph G

State space tree (Backtracking)


(Possible Solutions)
Backtracking
• Problem 2 : Graph Coloring
Algorithm:
Recursive algorithm that finds all Hamiltonian cycles
mColoring( k)
// This program was formed using the recursive backtracking scheme. The graph is
// represented by its Boolean adjacency matrix G[1:n][1:n]. All assignments of 1,2,….,m
// to find the vertices of the graph such that adjacent vertices are assigned distinct integers
// are printed. Hear k is the next vertex to color.
{
repeat { // Generate values for x[k].
NextValue(k); // Assign a legal next value to x[k].
if (x[k]=0) return;
if (k = n) {
write(x[1:n])
}
else mColoring(k+1);
} untile (false);
}
Backtracking
• Problem 2 : Graph Coloring
Algorithm:
NextValue( k)
// x[1],...,x[k-1] have been assigned integer values in the range[1,m] such that adjacent
// vertices have distinct integers. A value for x[k] determined in the range [0,m]. X[k] is
// assigned the next highest numbered color while maintain distinctness from the adjacent
// vertices of vertex k. If no such color exits, then x[k]=0.
{repeat
{
x[k] := (x[k]+1) mod (n+1); // Next vertex
if (x[k]=0) return;
for j := 1 to n do
{
// Check if this color is distinct from adjacent colors
if ( (G[k][j] !=0 ) and (x[k] = x[j]))
//if (k,j) is an edge and if adjacent vertices have the same color
then break;
}
if (j = n+1) then return;// New color found
} until(false); // Otherwise try to find another color
}
Backtracking
• Problems
1. Hamiltonian cycle
2. Graph Coloring
3. Sum of subset
4. N-Queen
Backtracking
Backtracking
Backtracking

S K R 1 2 3 4
0 1 55
7 11 13 24
S K R S K R
7 1 48 0 1 48
Backtracking

S K R 1 2 3 4
0 1 55
7 11 13 24
S K R S K R
7 1 48 0 1 48

S K R S K R S K R S K R
18 2 37 7 2 37 11 2 37 0 2 37
Backtracking

S K R 1 2 3 4
0 1 55
7 11 13 24
S K R S K R
7 1 48 0 1 48

S K R S K R S K R S K R
18 2 37 7 2 37 11 2 37 0 2 37

S K R S K R S K R S K R S K R S K R S K R S K R
31 3 24 18 3 24 20 3 24 7 3 24 24 3 24 11 3 24 13 3 24 0 3 24
Backtracking

S K R 1 2 3 4
0 1 55
7 11 13 24
S K R S K R
7 1 48 0 1 48

S K R S K R S K R S K R
18 2 37 7 2 37 11 2 37 0 2 37

S K R S K R S K R S K R S K R S K R S K R S K R
31 3 24 18 3 24 20 3 24 7 3 24 24 3 24 11 3 24 13 3 24 0 3 24

S K R S K R
55 4 0 42 4 0
S K R S K R
31 4 0 18 4 0
Backtracking

S K R 1 2 3 4
0 1 55
7 11 13 24
S K R S K R
7 1 48 0 1 48

S K R S K R S K R S K R
18 2 37 7 2 37 11 2 37 0 2 37

S K R S K R S K R S K R S K R S K R S K R S K R
31 3 24 18 3 24 20 3 24 7 3 24 24 3 24 11 3 24 13 3 24 0 3 24

S K R S K R S K R S K R
55 4 0 42 4 0 44 4 0 31 4 0
S K R S K R S K R S K R
31 4 0 18 4 0 20 4 0 7 4 0
Backtracking

S K R 1 2 3 4
0 1 55
7 11 13 24
S K R S K R
7 1 48 0 1 48

S K R S K R S K R S K R
18 2 37 7 2 37 11 2 37 0 2 37

S K R S K R S K R S K R S K R S K R S K R S K R
31 3 24 18 3 24 20 3 24 7 3 24 24 3 24 11 3 24 13 3 24 0 3 24

S K R S K R S K R S K R S K R S K R
55 4 0 42 4 0 44 4 0 31 4 0 48 4 0 35 4 0
S K R S K R S K R S K R S K R S K R
31 4 0 18 4 0 20 4 0 7 4 0 24 4 0 11 4 0
Backtracking

S K R 1 2 3 4
0 1 55
7 11 13 24
S K R S K R
7 1 48 0 1 48

S K R S K R S K R S K R
18 2 37 7 2 37 11 2 37 0 2 37

S K R S K R S K R S K R S K R S K R S K R S K R
31 3 24 18 3 24 20 3 24 7 3 24 24 3 24 11 3 24 13 3 24 0 3 24

S K R S K R S K R S K R S K R S K R S K R S K R
55 4 0 42 4 0 44 4 0 31 4 0 48 4 0 35 4 0 37 4 0 24 4 0
S K R S K R S K R S K R S K R S K R S K R
S K R
31 4 0 18 4 0 20 4 0 7 4 0 24 4 0 13 4 0 0 4 0
11 4 0
Backtracking

S K R 1 2 3 4
0 1 55
7 11 13 24
S K R S K R
7 1 48 0 1 48
It was observed that all possible solutions of
this example are between S K R S K R
S K R S K R
18 2 37 7 2 37 0 0 0 0 11 2 37 0 2 37
…..
S K R S K R S K R S K R …. S K R S K R S K R S K R
31 3 24 18 3 24 20 3 24 7 3 24 24 3 24 11 3 24 13 3 24 0 3 24
….
S K R S K R S K R S K R 1 1S1K 1R S K R S K R S K R
55 4 0 42 4 0 44 4 0 31 4 0 48 4 0 35 4 0 37 4 0 24 4 0
S K R S K R S K R S K R S K R S K R S K R
S K R
31 4 0 18 4 0 20 4 0 7 4 0 24 4 0 13 4 0 0 4 0
11 4 0
Backtracking

S K R 1 2 3 4
0 1 55
7 11 13 24
S K R S K R
7 1 48 0 1 48

S K R S K R S K R S K R
18 2 37 7 2 37 11 2 37 0 2 37

S K R S K R S K R S K R S K R S K R S K R S K R
31 3 24 18 3 24 20 3 24 7 3 24 24 3 24 11 3 24 13 3 24 0 3 24

S K R S K R S K R S K R S K R S K R S K R S K R
55 4 0 42 4 0 44 4 0 31 4 0 48 4 0 35 4 0 37 4 0 24 4 0
S K R S K R S K R S K R S K R S K R S K R
S K R
31 4 0 18 4 0 20 4 0 7 4 0 24 4 0 13 4 0 0 4 0
11 4 0
Backtracking
Backtracking
• Problem 3 : Sum of Subset
Algorithm:
Backtracking
• Problems
1. Hamiltonian cycle
2. Graph Coloring
3. Sum of subset
4. N-Queen
Backtracking
• Problem 4 : N Queen
• N-Queen problem is based on chess games.
• The problem is based on arranging the queens on
chessboard in such a way that no two queens can
attack each other.
• The N-Queen problem states as consider a n x n
chessboard on which we have to place n queens
so that no two queens attack each other by being
in the same row or in the same column or on the
same diagonal.
Backtracking
• Problem 4 : N Queen
• 2 – Queen’s problem is not solvable because 2 –
Queens can be placed on 2 x 2 chess board as
shown in below.

Q Q Q Q
Q Q

Q Q
Q Q Q Q
Backtracking
• Problem 4 : N Queen
• How to solve N-Queen Problem.
1. Let us take the example of 4 – Queens and 4 x 4
chessboard.
2. Start with an empty chessboard.
3. Place queen 1 in the first possible position of its row
i.e. on 1st row and 1st column

Q
Backtracking
• Problem 4 : N Queen
• How to solve N-Queen Problem.
4. Then place queen 2 after trying unsuccessful place
(1, 2), (2, 1), (2, 2) at (2, 3) i.e. 2nd row and 3rd
column.

Q
Q
Backtracking
• Problem 4 : N Queen
• How to solve N-Queen Problem.
5. This is a dead end because a 3rd queen cannot be
placed in next column, as there is no acceptable
position for queen 3. Hence algorithm backtracks and
places the 2nd queen at (2, 4) position..

Q Q
Q Q
Backtracking
• Problem 4 : N Queen
• How to solve N-Queen Problem.
6. Then place 3rd queen at (3,2) but it again another
dead lock end as next queen (4th queen) cannot be
placed at permissible position.

Q
Q
Q
Backtracking
• Problem 4 : N Queen
• How to solve N-Queen Problem.
7. Hence we need to backtrack all the way up to queen 1
and move it to (1, 2). viii. Place queen 1 at (1, 2), queen
2 at (2, 4), queen 3 at (3, 1) and queen 4 at (4, 3).

Q Q Q Q
Q Q Q
Q Q
Q
8. Thus the solution is obtained (2, 4, 1, 3) in row wise
manner in x array.
Backtracking
Backtracking
• Problem 4 : N Queen
One of the solution to 8-Queen Problem

Q
Q
Q
Q
Q
Q
Q
Q
Backtracking
• Problem 4 : N Queen
• N Queens are placed on n by n chessboard so that no
two attack (no two queens are on the same row,
column, or diagonal).
• A tree bellow shows the state space tree for n=4.
1

2 18 34 50

3 8 13 19 24 29 35 40 45 51 56 61

4 6 9 11 14 16 20 22 25 27 30 32 36 38 41 43 46 48 52 54 57 59 62 64

5 7 10 12 15 17 21 23 26 28 31 33 37 39 42 44 47 49 53 55 58 60 63 65
Backtracking
• Problem 4 : N Queen
• N Queens are placed on n by n chessboardofso that no
two attack (no two queens are on othe ns same row,
i
column, or diagonal). l ut
s o
• A tree bellow shows the state space b le tree for n=4.
s si n!)
1l p
o O(
a l is
a t le
2 18 th mp 34 50
d a
rve s ex
se hi
3 8 13 ob19 t 24 29 35 40 45 51 56 61
a s
w 16 20 22 25 27 30 32 36 38 41 43 46 48 52 54 57 59 62 64
4 6 9 11 t14
I
5 7 10 12 15 17 21 23 26 28 31 33 37 39 42 44 47 49 53 55 58 60 63 65
Backtracking
Backtracking
• Problem 4 : N Queen
Logic behind the place() of N-Queen Problem
1 2 3 4 5 6 7 8 4. The Queen is available in [i,j] =[4,2]
1 5. The squares that are diagonal to that
queen from upper left to lower right
2 are [3,1], [5,3], [6,4], [7,5], and
3 [8,6]. (Note : All these squares have
4 Q a “row-column” value of 2)
5 6. Similarly, the squares that are
diagonal to that queen from upper
6 right to lower left are [1,5], [2,4],
7 [3,3], and [5,1]. (Note : All these
8 squares have a “row+column” value
of 6)
Backtracking
Backtracking
• Problem 2 : N Queen (Algorithm)
Recursive algorithm for N-Queen
NQueens(k, n)
// Using backtracking, this procedure prints all possible placements of n queens
// on an n x n chessboard so that they are nonattacking.
{
for i := 1 to n do
{
if (Place(k, i))
{
x[k] := i;
if (k = n)
write (x[1:n])
else
NQueens(k+1, n);
}
}
}
Backtracking
• Problem 2 : N Queen (Algorithm)
Recursive algorithm for N-Queen
Algorithm Place( k, i)
// Returns true if a queen can be placed in kth row and ith column. Otherwise it
// returns false. x[] is a global array whose first (k-1) values have been set. abs(r)
// returns the absolute value of r.
{
for j := 1 to k-1 do
if ( (x[j] = i) or // Two in the same column
|| (abs(x[j] - i) = abs(j-k)) ) // or in the same diagonal
then return false;
return true;
}

You might also like