You are on page 1of 48

DESIGN AND ANALYSIS OF

ALGORITHM
UNIT-IV
BACKTRACKIN
G

The principle idea of back-tracking is to


construct solutions as component at a
time. And then evaluate such partially
constructed solutions.
Backtracking [animation]

dead end

?
dead end
dead end

?
start ? ?
dead end

dead end

success!
Key Terms:

• State-space tree

• Root

• Components

• Promising & Non-promising

• 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

Figure:-Board for the four-queens problem


1 2 3 4

1
Q Queen-1

2 Q Queen-2

3 Q Queen-3

4 Q Queen-4

Figure:-Board for the four-queens problem


Subset-sum Problem

• Subset-sum Problem: The problem is to find a subset of a given set


S = {s1, s2,- - -, sn} of ‘n’ positive integers
whose sum is equal to a given positive
integer ‘d’.

• Observation : It is convenient to sort the set’s elements in


increasing order, S1 ≤ S2 ≤ ….. ≤ Sn. And each
set of solutions don’t need to be necessarily of
fixed size.
• For S = {3, 5, 6, 7} and d = 15, the solution is
Example : shown below :-
Solution = {3, 5, 7}
0 0

with 3 w/o 3

3 0
3
with 5 w/o 5 with 5 w/o 5

8 3 5 0
5

with 6 with 6 w/o 6 0+6+7<15


w/o 6 with 6 w/o 6 x

14 8 9 3 11 5
6

14+7>15 with 7 w/o 7 9+7>15 3+7<15 11+7>15 5+7<15


x x x x x

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

Problem: This problem is concern about finding


a Hamiltonian circuit in a given graph.

Hamiltonian Hamiltonian circuit is defined as a


circuit: cycle that passes to all the vertices of
the graph exactly once except the
starting and ending vertices that is the
same vertex.
For example consider the given graph
and evaluate the mechanism:-

(a)
(b)

Figure:• (a) Graph.


• (b) State-space tree for finding a Hamiltonian circuit. The
numbers above the nodes of the tree indicate the order the order
in which nodes are generated.
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 :
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:
Now the map can be
colored as shown here:-

Four colors are chosen


as - Red, Green, Blue
and Yellow
Artificial
Intelligence

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:

regions as, X = {WA, NT ,Q, NSW ,V,SA,T}

domain of each variable Di = {red, green, blue}

Constraints: C = {SA WA, SA NT, SA Q, SA NSW, SA V, WA


NT, NT Q, Q NSW , NSW V}
Observation:-

• Once we have chosen {SA = blue}, none of the five neighboring


variables can take on the value blue. So we have only 25 = 32
assignments to look at instead of 35= 243 assignments for the five
neighboring variables.

• Furthermore, we can see why the assignment is not a solution—


we see which variables violate a constraint—so we can focus
attention on the variables that matter.
Now the map can be colored as shown here:-
Conclusion
In conclusion, three things on behalf of backtracking need
to be said:-
• It is typically applied to difficult combinatorial problems
for which no efficient algorithm for finding, exact
solutions possibly exist.
• Backtracking solves each instances of a problem in an
acceptable amount of time.
• It generates all elements of the problem state.
POLYNOMIAL TIME
ALGORITHMS FOR THE
N-QUEEN PROBLEM
OUTLINE
 N-Queen Problem
 Previous Works

 Probabilistic Local Search Algorithms


 QS1, QS2, QS3 and QS4
 Results
N-QUEEN PROBLEM
 A classical combinatorial problem
 n x n chess board

 n queens on the same board

 Queen attacks other at the same row, column or diagonal


line
 No 2 queens attack each other
A SOLUTION FOR 6-QUEEN
PREVIOUS WORKS
 Analytical solution
 Directcomputation, very fast
 Generate only a very restricted class of solutions

 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

 The array queen must contain a permutation of integers {1,


…,n}
 1 queen per column

 2 arrays, dn and dp, of size 2n-1 keep track of number of


queen on negative and positive diagonal lines
 The i-th queen is counted at dn[i+queen[i]] and

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

Go through the attacking


queen only

Reduce cost of bookeeping

C2=32 to maximize the speed for


small N, no effect on large N
QS3
 Improvement on QS2
 Random permutation generates approximately 0.53n
collisions
 Conflict-free initialization
 The position of a new queen is randomly generated until a
conflict-free place is found
 After a certain of queens, m, the remaining c queens are
placed randomly regardless of conflicts
QS4
 Algorithm same as QS1
 Initialization same as QS4

 The fastest algorithm


 3,000,000 queens in less than one minute
RESULTS – STATISTICS OF QS1
Collisions for random permutation

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

Number of conflict-free queens during initialization


RESULTS – TIME COMPLEXITY
RESULTS – TIME COMPLEXITY
RESULTS – TIME COMPLEXITY
RESULTS – TIME COMPLEXITY
RESULTS – TIME COMPLEXITY
BRANCH AND BOUND
 The idea:
Set up a bounding function, which is used to compute a bound (for the
value of the objective function) at a node on a state-space tree and
determine if it is promising
 Promising (if the bound is better than the value of the best solution
so far): expand beyond the node.
 Nonpromising (if the bound is no better than the value of the best
solution so far): not expand beyond the node (pruning the state-space
tree).

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

minimum tour length.

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

You might also like