0% found this document useful (0 votes)
8 views44 pages

L4-Backtracking N-Queen HTC

The document discusses backtracking and branch and bound techniques, providing examples such as the Travelling Salesman Problem, Graph Coloring, n-Queen Problem, Sum of Subsets, and Hamiltonian Circuits. It explains the 8-Queens problem in detail, illustrating how backtracking can be used to place queens on a chessboard without conflicts. Additionally, it covers algorithms for solving the Sum of Subsets and Graph Coloring problems, emphasizing the use of recursive backtracking to find solutions.

Uploaded by

cse21216
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)
8 views44 pages

L4-Backtracking N-Queen HTC

The document discusses backtracking and branch and bound techniques, providing examples such as the Travelling Salesman Problem, Graph Coloring, n-Queen Problem, Sum of Subsets, and Hamiltonian Circuits. It explains the 8-Queens problem in detail, illustrating how backtracking can be used to place queens on a chessboard without conflicts. Additionally, it covers algorithms for solving the Sum of Subsets and Graph Coloring problems, emphasizing the use of recursive backtracking to find solutions.

Uploaded by

cse21216
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/ 44

Backtracking, Branch and Bound with examples such as

a) Travelling Salesman Problem,


b) Graph Coloring,
c) n-Queen Problem,
d) Sum of subsets.
e) Hamiltonian circuit

Faculty: Dr Sansar S Chauhan


Backtracking
• A strategy for guessing at a solution and backing up when a deadlock is
reached
• Recursion and backtracking can be combined to solve problems.

Faculty: Dr Sansar S Chauhan


Backtracking EXAMPLE—8 Queens Problem

• The 8-queens problem is a classical combinatorial problem in which it is


required to place eight queens on an 8 x 8 chessboard so no two can
attack each other.

• A queen can attack another queen if it exists in the same row, column or
diagonal as the queen.

Faculty: Dr Sansar S Chauhan


Backtracking EXAMPLE—8 Queens problem(cont…)
• This problem can be solved by trying to place the first queen, then the
second queen so that it cannot attack the first, and then the third so that it
is not conflicting with previously placed queens.

Faculty: Dr Sansar S Chauhan


Backtracking EXAMPLE—8 Queens
Problem(cont…)

• It is an empty 8 x 8 chess board. We have to place


the queens in this board.

Faculty: Dr Sansar S Chauhan


BACKTRACKING DEMO FOR 4
QUEENS

Faculty: Dr Sansar S Chauhan


Faculty: Dr Sansar S Chauhan
Faculty: Dr Sansar S Chauhan
Faculty: Dr Sansar S Chauhan
Faculty: Dr Sansar S Chauhan
Faculty: Dr Sansar S Chauhan
Faculty: Dr Sansar S Chauhan
Faculty: Dr Sansar S Chauhan
Faculty: Dr Sansar S Chauhan
Faculty: Dr Sansar S Chauhan
Faculty: Dr Sansar S Chauhan
Faculty: Dr Sansar S Chauhan
Faculty: Dr Sansar S Chauhan
Faculty: Dr Sansar S Chauhan
Faculty: Dr Sansar S Chauhan
Faculty: Dr Sansar S Chauhan
Faculty: Dr Sansar S Chauhan
Faculty: Dr Sansar S Chauhan
Faculty: Dr Sansar S Chauhan
Faculty: Dr Sansar S Chauhan
Faculty: Dr Sansar S Chauhan
Faculty: Dr Sansar S Chauhan
Faculty: Dr Sansar S Chauhan
Faculty: Dr Sansar S Chauhan
Faculty: Dr Sansar S Chauhan
Faculty: Dr Sansar S Chauhan
Backtracking EXAMPLE—8 Queens
Problem(cont…)
• Backtracking provides the hope to solve some problem instances of nontrivial
sizes by pruning non-promising branches of the state-space tree.

• The success of backtracking varies from problem to problem and from instance to
instance.

• Backtracking possibly generates all possible candidates in an exponentially


growing state-space tree.

Faculty: Dr Sansar S Chauhan


Algorithm
nQueens(k, n) :
// Using backtracking, this procedure prints all
// possible placements of n queens on an n×n
// chessboard so that they are nonattacking.
// Initially, k=1 and n is the number of queens
//x[k]=i k is column and i is row.
{
for i:= 1 to n do
{
if Place(k, i) then //k is column and i is row
{
x[k] = i;
if (k == n) write (x[1:n]);
else nQueens(k+1, n);
}
}
}

Faculty: Dr Sansar S Chauhan


Place(k, i)
// Returns true if a queen can be placed.
//Otherwise, it returns false. X[] is a global array
// whose first (k-1) values have been set. Abs( ) returns the absolute value of r
{
for j := 1 to k-1 do
// Two in the same column
// or in the same diagonal
if ((x[j] == i) or (abs(x[j] – i) = Abs(j – k))) then
return false;
return true;
}

//x[j]==i checks that no queen should be placed on the same row as i (in the past)
//(abs(x[j] – i) = Abs(j – k))) check for diagonal placement (previous row – current row == previous column -
current column)

Faculty: Dr Sansar S Chauhan


Sum of Subset
• Problem: Given N positive integers w1, w2, w3, . . . Wn and positive
integer M. Find all subsets of w1, w2,w3, . . . .wn that sum to S.
• Ex: n=3, S=6, w1=2,w2=4, w3=6
• Solution: {2,4} and {6}
• We will assume a binary state space tree
• The nodes at depth 1 are for including {yes, no} item1, the nodes at
depth 2 are for item2, etc.
• The left branch includes wi, and the right branch excludes wi.
• The nodes contain the sum of the weights included so far.

Faculty: Dr Sansar S Chauhan


Faculty: Dr Sansar S Chauhan
SumOfSubset(s, k, r)
{
//M=sum required, r = sum of w[j] to w[n]
// s=weight so far, k=iteration
//x[k]=array containing solution
x[k] =1; //Generating left child
if(s+w[k]==M)
Display x[1….k] //and remaining zeros
else if(s+w[k]+w[k+1]<=m)
SumOfSubset(s+w[k], k+1, r-w[k])
if ((s+r-w[k]>=m) && (s+w[k+1]<=m))
{
x [k]=0;
SumOfSubset(s, k+1, r-w[k]);
}
}
Faculty: Dr Sansar S Chauhan
Coloring a map

• Problem:
• Let G be a graph and m be a given positive integer. We want to discover whether the nodes of G
can be colored in such a way that no two adjacent node have the same color yet only m colors are used.
This technique is broadly used in “map-coloring”; Four-color map is the main objective.
• Consider the following map and it can be easily decomposed into the following planner graph
beside it :

Faculty: Dr Sansar S Chauhan


This map-coloring problem of the given map can be solved from
the planner graph, using the mechanism of backtracking.
The state- space tree for this above map is shown below:

Faculty: Dr Sansar S Chauhan


Now the map can be
colored as shown here:-

Four colors are chosen as


- Red, Green, Blue and
Yellow

Faculty: Dr Sansar S Chauhan


The graph is represented by its Boolean adjacency matrix G[1:n,1:n].
All assignments of 1,2,...m to the vertices of the graph such that adjacent vertices are assigned distinct integers
are printed.
K is the index of the next vertex to color.

K is the node we want to color.


J is all vertex adjacent to k (i.e. if j reaches to n+1and no match
color is found the return successful
Otherwise, break in between
Faculty: Dr Sansar S Chauhan
HAMILTONIAN CYCLES

• Let G=(V,E) be a connected graph with n vertices.


• A Hamiltonian cycle is a round trip path along n edges of G that visits every
vertex once and returns to its starting position.
• If a Hamiltonian cycle begins at some vertex v1 ∈ G and the vertices of G are
visited in the order v1, v2,...vn+1 then the edges (vi,vi+1) are in E, 1<=i<=n, and
the vi are distinct except for v1 and vn+1,

Faculty: Dr Sansar S Chauhan


The algorithm Hamiltonian() 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.

Faculty: Dr Sansar S Chauhan


// x[1:k-1] is a path of k-1 distinct vertices
// if x[k]=0 then no vertex has yet been assigned to x[k]
// after execution x[k] is assigned to the next highest
// numbered vertex which does not already appear in
// x[1:k-1]. Otherwise x[k]=0.
// if k=n then in addition x[k] is connected to x[1].

Faculty: Dr Sansar S Chauhan

You might also like