Professional Documents
Culture Documents
ALGORITHM
UNIT-IV
BACKTRACKIN
G
dead end
?
dead end
dead end
?
start ? ?
dead end
dead end
success!
Key Terms:
• State-space tree
• Root
• Components
• Leaves
N-Queen
Problem
Problem:- The problem is to place n queens on an
n-by-n chessboard so that no two
queens attack each other by being in the
same row, or in the same column, or in
the same diagonal.
Observation:- Case 1 : n=1
Case 2 : n=2
Case 3 : n=3
Case 4 : n=4
• Case 4: For example to explain the n-
Queen problem we Consider n=4 using a 4-
by-4 chessboard where 4-Queens have to be
placed in such a way so that no two queen
can attack each other.
1 2 3 4
1
2
3
4
0
Q
Q
5
1
Q Q x x x Q
x x Q Q Q
2 3 6
x x x x
Q x x Q
Q Q
Q 4 Q 7
x x x x x x Q
Q
Q 8
Q
• Using this above mechanism we can obtain two
solutions shown in the two consecutive figures:-
1 2 3 4
1
Q Queen-1
2 Q Queen-2
3 Q Queen-3
4 Q Queen-4
1
Q Queen-1
2 Q Queen-2
3 Q Queen-3
4 Q Queen-4
with 3 w/o 3
3 0
3
with 5 w/o 5 with 5 w/o 5
8 3 5 0
5
14 8 9 3 11 5
6
7 15 8
solution 8<15
x
Figure : Compete state-space tree of the backtracking algorithm applied to the instance S =
{3, 5, 6, 7} and d = 15 of the subset-sum problem. The number inside a node is the
sum of the elements already included in subsets represented by the node. The
inequality below a leaf indicates the reason for its termination.
Hamiltonian Circuit Problem
(a)
(b)
Figure: (a) The principal states and territories of Australia. Coloring this map can
be viewed as a constraint satisfaction problem (CSP). The goal is to assign colors
to each region so that no neighboring regions have the same color. (b) The map-
coloring problem represented as a constraint graph.
Problem: We are given the task of coloring each region either red, green,
or blue in such a way that no neighboring regions have the
same color. To formulate this as a CSP the following
assumptions are made:
Backtracking Search
Generate all possible solutions
Exponential time complexity
Can only solve for n<100
QS1
Data Structure
The i-th queen is placed at row i and column queen[i]
1 queen per row
dp[i-queen[i]]
Problem remains
Resolve any collision on the diagonal lines
QS1
Pseudo-code
QS1
Gradient-Based Heuristic
QS2
Data Structure
Queen placement same as QS1
An array attack is maintained
Store the row indexes of queens that are under attack
QS2
Pseudo-code
QS2
Max no. and min no. of queens on the most populated diagonal in a
random permutation
Permutation Statistics
Swap Statistics
RESULTS – STATISTICS OF QS2
Permutation Statistics
Swap Statistics
RESULTS – STATISTICS OF QS3
Swap Statistics
44
TRAVELING SALESMAN PROBLEM
Construct the state-space tree:
A node = a vertex: a vertex in the graph.
A node that is not a leaf represents all the tours that start with the path stored
at that node; each leaf represents a tour (or non-promising node).
Branch-and-bound: we need to determine a lower bound for each node
For example, to determine a lower bound for node [1, 2] means to
determine a lower bound on the length of any tour that starts with edge 1—
2.
Expand each promising node, and stop when all the promising nodes have
been expanded. During this procedure, prune all the nonpromising nodes.
Promising node: the node’s lower bound is less than current minimum tour
length.
Non-promising node: the node’s lower bound is NO less than current
45
TRAVELING SALESMAN PROBLEM—BOUNDING
FUNCTION 1
Because a tour must leave every vertex exactly once, a lower bound on
the length of a tour is b (lower bound) minimum cost of leaving every
vertex.
The lower bound on the cost of leaving vertex v1 is given by the
minimum of all the nonzero entries in row 1 of the adjacency
matrix.
…
The lower bound on the cost of leaving vertex v is given by the
n
minimum of all the nonzero entries in row n of the adjacency
matrix.
Note: This is not to say that there is a tour with this length. Rather, it
says that there can be no shorter tour.
Assume that the tour starts with v1.
46
TRAVELING SALESMAN PROBLEM—BOUNDING
Because every vertex must be entered and exited exactly once, a lower bound on
FUNCTION
the length of2a tour is the sum of the minimum cost of entering and leaving
every vertex.
For a given edge (u, v), think of half of its weight as the exiting cost of u, and
half of its weight as the entering cost of v.
The total length of a tour = the total cost of visiting( entering and exiting)
every vertex exactly once.
The lower bound of the length of a tour = the lower bound of the total cost of
visiting (entering and exiting ) every vertex exactly once.
Calculation:
for each vertex, pick top two shortest adjacent edges (their sum divided
by 2 is the lower bound of the total cost of entering and exiting the
vertex);
add up these summations for all the vertices.
Assume that the tour starts with vertex a and that b is visited before c.
47
TRAVELING SALESMAN EXAMPLE 2
48