37-39 Backtracking Algorithms

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 33

Backtracking Algorithms

Design and Analysis of Algorithm (CS1501)

Tarun Jain (Assistant Professor (Senior Scale))


Manipal University Jaipur
Backtracking

• Suppose you have to make a series of decisions, among various choices,


where
• You don’t have enough information to know what to choose

• Each decision leads to a new set of choices

• Some sequence of choices (possibly more than one) may be a solution to your problem

• Backtracking is a methodical way of trying out various sequences of decisions,


until you find the correct one that “works”.
Backtracking

• Backtracking is used to solve problems in which a sequence of objects is


chosen from a specified set so that the sequence satisfies some criterion.

• Backtracking is a modified depth-first search of a tree.

• It is the procedure whereby, after determining that a node can lead to


nothing but dead nodes, we go back (“backtrack”) to the node’s parent
and proceed with the search on the next child.
Backtracking (animation)
dead end

?
dead end
dead end

?
start ? ?
dead end

dead end

success!

4
Terminology
A tree is composed of nodes

There are three kinds of nodes:

The (one) root node


Backtracking can be thought of as
Internal nodes
searching a tree for a particular “goal”
Leaf nodes leaf node

5
Backtrack Algorithm

• Based on depth-first recursive search


• Approach
1. Tests whether solution has been found
2. If found solution, return it
3. Else for each choice that can be made at that moment
a) Make that choice
b) Recur
c) If recursion returns a solution, return it
4. If no choices remain, return failure
• Sometimes called “search tree”
N-Queen Problem

• The N Queen is the problem of placing N chess queens on an N×N


chessboard so that no two queens attack each other. For example,
following is a solution for 4 Queen problem.
Movement of a Queen on a Chessboard
Algorithm for N-queen Problem:
• 1) Start in the leftmost column
• 2) If all queens are placed
• return true
• 3) Try all rows in the current column.
• Do following for every tried row.
• a) If the queen can be placed safely in this row
• then mark this [row, column] as part of the
• solution and recursively check if placing
• queen here leads to a solution.
• b) If placing the queen in [row, column] leads to
• a solution then return true.
• c) If placing queen doesn't lead to a solution then
• unmark this [row, column] (Backtrack) and go to
• step (a) to try other rows.
• 3) If all rows have been tried and nothing worked,
• return false to trigger backtracking. 9
(a) The 4-Queens Problem

• Place 4 Queens on an 4 x 4 chessboard such that no two can


attack each other.

• With no constraints, there are

16 * 15 * 14 * 13 = 43,680 possible placements.

• Constraint (1): Each Queen must be on a different row. Now we


seek the 4-tuples

X = {x1 , x2 , x3 , x4} representing column numbers. There are


4*4*4*4 = 256 such tuples.

Prof. Amr Goneid, AUC 10


The Problem (cont.)

• Constraint(2): Each Queen must be on a different column.

The number of possible tuples reduces to 4*3*2*1 = 4! = 24

• Backtracking will be used to impose the final No Attack constraint so


that no two queens can be placed on the same diagonal

Prof. Amr Goneid, AUC 11


The Permutation Tree

• The root will have 4 children

• In general, each node in level L will have

(4-L+1) children. The total # of nodes in the tree will be

1 + 4 + 4*3 + 4*3*2 + 4*3*2*1 = 65 nodes

Prof. Amr Goneid, AUC 12


The
Permutatio
n Tree

Prof. Amr Goneid, AUC 13


Example: n-Queens 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 on the same diagonal.

1 2 3 4
1 queen 1
2 queen 2
3 queen 3
4 queen 4

Figure: Board for the four-queens problem.


N-Queens Problem
 For n=1, the problem has a trivial solution, and it is easy to see that there is
no solution for n=2 and n=3.

 So let us consider the four-queens problem and solve it by the


backtracking technique.

 We start with the empty board and then place queen 1 in the first possible
position of its row, which is in column 1 of row 1.

 Then we place queen 2, after trying unsuccessfully columns 1 and 2, in the


first acceptable position for it, which is square (2,3), the square in row 2
and column 3. This proves to be a dead end because there is no acceptable
position for queen 3. So, the algorithm backtracks and puts queen 2 in the
next possible position at (2,4).

 Then queen 3 is placed at (3, 2), which proves to be another dead end. The
algorithm then backtracks all the way to queen1 and moves it to (1, 2).

 Queen2 then goes to (2, 4), queen3 to (3, 1), and queen4 to (4, 3) which is a
solution to the problem.
Portion of the Permutation
Tree (State Space Tree)

Figure: State-space tree of solving the four-queens problem by backtracking.


× denotes an unsuccessful attempt to place a queen in the indicated column. The
numbers above the nodes indicate the order in which the nodes are generated.
Portion of the Permutation Tree (State Space Tree)

1 2
1
2 18
4
2 4 1 3
3

3 8 13 19 24 29
2 4 3
2 1

9 11 14 16 30
3 3

15 X = {2,4,1,3} 31
Solutions
Leaf 31 Leaf 39

Q Q
Q Q
Q Q
Q Q

{2,4,1,3} {3,1,4,2}

18
(b)The 8-Queens Problem
• Place 8 Queens on an 8 x 8 chessboard such that no two
can attack each other. There are about 4.4 billion trials.

• Obvious: Each queen must be on a different row. All


solutions are 8-tuples

X = (x1,x2,..,x8) , where xi is the column number of the ith


queen.

19
The 8-Queens Problem (continued)

• Possible Solution: Q
X = (4 , 6 , 8 , 2 , 7 , 1 , 3 , 5) Q
Q
Q
Q
Q
Q
Q

20
The 8-Queens Problem Animation

21
(c)The n-Queens Problem

• Find all possible arrangements of n Queens on an n


x n chessboard such that no two can attack each
other.

22
Graph Coloring Problem

Let G be undirected graph and let c be an integer.

Assignment of colors to the vertices or edges such that no two adjacent vertices are to be similarly
colored.

We want to minimize the number of colors used.

The smallest c such that a c-coloring exists is called the graph’s chromatic number and any such c-
coloring is an optimal coloring

M - colorability decision problem: Whether the nodes of G can be colored in such a way that no two
adjacent nodes have the same color yet only m colors are used.

M-colorability optimization problem: It asks for the smallest integer m for which the graph G can be
colored.
Coloring of Graph

1. The graph coloring optimization problem: find the


minimum number of colors needed to color a graph.
2. The graph coloring decision problem: determine if
there exists a coloring for a given graph which uses
at most m colors.

Two colors No solution with


two colors
Coloring of Graphs

Practical applications: scheduling, time-tabling, register


allocation for compilers, coloring of maps.
A simple graph coloring algorithm - choose a color and an
arbitrary starting vertex and color all the vertices that can be
colored with that color.
Choose next starting vertex and next color and repeat the
coloring until all the vertices are colored.

Four colors Three colors are enough


Graph Colouring

Graph Colouring Problem:


Given a graph, colour all the vertices so that
two adjacent vertices get different colours.

Objective: use minimum number of colours.

3-colourable
Optimal Colouring

Definition. min #colors for G is chromatic number, (G)

What graphs have chromatic number one?

when there are no edges…


Naive Algorithm
In this approach we first find all permutations of colors possible to color every
vertex of the graph using Brute Force Method. Cleary if there is a large number of
vertices, more the time it will take.
Backtracking Algorithm
Backtracking algorithm makes the process to solve the problem more efficient by
avoiding much bad decision that needed to be made in the naive approach.
We start by coloring a single vertex, then we move to its adjacent vertex. We color it with
that color which has not been used to color any of its connected vertices. After coloring it
we then move to its adjacent vertex which is uncolored. We repeat the process until all
vertices of the given graph are colored.
In case we find for a vertex that all its adjacent (connected) are colored with
different colors and no color is left to make it color different from them, then it means
the given number of colors i.e ‘m’, is insufficient to color the graph. Hence,
we require more colors i.e. bigger chromatic number.
Subset-Sum Problem
 The subset-sum problem finds a subset of a given set S=(s1, . . . , sn} of
n positive integers whose sum is equal to a given positive integer d.
For example: for S={1, 2, 5, 6, 8} and d =9, there are two
solutions: {1, 2, 6} and {1, 8}.

 It is convenient to sort the set’s elements in increasing order. So we will


assume that s1 ≤ s2 ≤ . . . ≤ sn.

 The state-space tree can be constructed as a binary tree like that shown
in next slide for the instance S={3, 5, 6, 7} and d=15.

 The root of the tree represents the starting point, with no decisions
about the given elements made as yet.

 Its left and right children represent, respectively, inclusion and


exclusion of s1 in a set being sought. 30
Subset-sum Problem
with 3 0

with 5 3 w/o 5
0
with 5
w/o 5
8 3 5 0
with 6
w/o 6 with 6 with 6
w/o 6 w/o 6 x
14 8 9 3 11 5
0+13<15

x with 7 x x x x
14+7>15 w/o 7 9+7>15 3+7<15 11+7>15 5+7<15
15 8
solution x
8<15
Figure: Complete 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.
31
Subset-Sum Problem
 Similarly, going to the left from a node of the first level corresponds to
inclusion of s2, while going to the right corresponds to its exclusion, and
so on.
 Thus, a path from the root to a node on the ith level of the tree indicates
which of the first i numbers have been included in the subsets
represented by that node. We record the value of s‘, the sum of these
numbers, in the node.
 If s‘ is equal to d, we have a solution to the problem. We can either report
this result and stop or, if all the solutions need to be found, continue by
backtracking to the node’s parent.
 If s‘ is not equal to d, we can terminate the node as nonpromising if either
of the following two inequalities holds:
s‘ + si+1 > d (the sum s’ is too large)
n
s’ + ∑ sj < d (the sum s’ is too small).
j=i+1

32
Subset-Sum Problem

33

You might also like