0% found this document useful (0 votes)
35 views22 pages

Chapter Five - Backtracking

nn

Uploaded by

ermiyasgr27
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)
35 views22 pages

Chapter Five - Backtracking

nn

Uploaded by

ermiyasgr27
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/ 22

Backtracking

Araba Aman
[email protected]

Haramaya University
Backtracking?
▪Many problems with searching a set of solutions or which ask for
an optimal solution satisfying some constraints can be solved using
the backtracking formulation.
▪In many applications of the backtrack method, the desired solution
is expressible as an n-tuple (xi,…,xn),where the xi are chosen from
some finite set Si.
▪Often the problem to be solved calls for finding one vector that
maximizes (or minimizes or satisfies) a criterion function P(x1, x2,
.., xn). Some time its seeks all vectors that satisfy P.
▪Many of the problems we solve using backtracking require that all
the solutions satisfy a complex set of constraints.

1/16/2022 qubee-tp.com 2
Cont.
▪For any problem these constraints can be classified into two
categories: explicit and implicit.
▪The explicit constraints depend on the particular instance I of
the problem being solved.
▪All tuples that satisfy the explicit constraints define a possible
solution space for I.
▪The implicit constraints are the rules that determine which of the
tuples in the solution space of I satisfy the criterion function.

▪Thus implicit constraints describe the way in which xi must


relate to each other.

1/16/2022 qubee-tp.com 3
Cont.
▪Explicit constraints are rules that restrict each Xi to take on values
only from a given set.

1/16/2022 qubee-tp.com 4
E.g.,
▪8-queens problem: the problem is place 8 queens on 8X8 chess
board, such that there is no attack. The attack happens when two
queens placed on the same row, column and diagonal.
Constraints
▪ Explicit constraints
▪ – Si={1,2,…,8}
▪ – So, solution space consists of 88 8-tuples
▪ Implicit constraints
▪ – No two xi‘s can be the same (By this, solution space reduced
from 88 to 8!)
▪ – No two queens can be on the same diagonal

1/16/2022 qubee-tp.com 5
Cont.

1/16/2022 qubee-tp.com 6
Cont.
▪ Greedy Algorithm:

▪ Dynamic Programming:

▪ Backtracking:

1/16/2022 qubee-tp.com 7
▪ In backtracking, the basic idea is to build up the solution vector one
component at a time
▪ and to use modified criterion functions Pi (x1,…xi) (sometimes called
bounding functions) to test whether the vector being formed has any
chance of success.
▪ The major advantage of this method is this: if it is realized that the partial
vector (x1, x2,….., xi) can in no way lead to an optimal solution, then
mi+1……mn possible test vectors can be ignored entirely.

▪ Backtracking algorithms determine problem solutions by systematically


searching the solution space for the given problem instance.

▪ This search is facilitated by using a tree organization for the solution space.
1/16/2022 qubee-tp.com 8
Cont.
▪ N-QUEENS problem; it is generalization of 8-queens problem. That is
placing n queen on nXn chess board without attacks.
▪ The solution space consists of all n! permutation of n-tuples. The tree is
called permutation tree.
▪ Edges are labeled by possible values of Xi.
▪ Edges from level 1 to level 2 nodes specify the values for x1.
▪ Edges from level i to i+1 are labeled with values of xi.
▪The solution space is defined by all the paths from the root node
to a leaf node.
▪ For e.g., If n=4, then there will be 4! =24 leaf nodes in the tree.

1/16/2022 qubee-tp.com 9
1/16/2022 qubee-tp.com 10
1/16/2022 qubee-tp.com 11
1/16/2022 qubee-tp.com 12
▪ 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
nodes have the same color yet only m colors are used - this is called m-
colorability decision problem.
▪ The m-colorability optimization problem asks for the smallest integer m for
which the graph can be colored. This integer is called chromatic number of
the graph.
▪ Chromatic number of this graph is 3.

1/16/2022 qubee-tp.com 14
Cont.
▪ E.g., if we apply graph coloring to map coloring, region of the map becomes
node, and if two regions are adjacent, then corresponding nodes are joined by
edges.
▪ E.g., Map regions, and corresponding graph nodes and edges.

▪ For many years it was known that five colors are enough to color any map, but no
map that required more than four colors have ever found.

1/16/2022 qubee-tp.com 15
Cont.
▪Suppose we represent a graph by its adjacency matrix G[1:n,
1:n], where G[i, j]=1 if (i, j) is an edge of G, and G[i, j]=0
otherwise.
▪The colors are represented by the integers 1, 2, 3, .. , m and the
solutions are given by the n-tuple (x1, x2, …, xn), where xi is
the color of node i.
▪The underlying state space tree used is a tree of degree m
and height n+1.
▪That means, each node at level i has m children corresponding to
the m possible assignments to xi, 1<=i<=n.
▪ Nodes at level n+1 are leaf nodes.

1/16/2022 qubee-tp.com 16
Cont.

▪NB: if I choose x1=1 for first vertex out of three colors (1, 2, 3),
then still I can choose x2 of vertex two any one of the three
colors.
1/16/2022 qubee-tp.com 17
0 2 0 3
2 0 4 1
9 8 0 3
0 0 2 1

1/16/2022 qubee-tp.com 18
Cont.
▪Means that, at each stage, or vertex I have three options (colors)
to choose from.
▪ The total time: O(nmn)
So, how to apply DP to Graph Coloring?
▪ Apply bound condition (constraint) if can be colored, choose xi
for a vertex i. Stop expanding the tree otherwise.
▪Try coloring vertex next to vertex i with other color, keep on
expanding the tree until you find right answer for xn. Where
1<=i<=n.
▪ The constraint is: not to adjacent vertices have the same color.

1/16/2022 qubee-tp.com 19
E.g.,
▪ Let take n=3 and m=3 (R, G, B) or say Red, Green and Blue colors.
Steps:
▪ Draw a root node.
▪ Know that, X1 holds color value of vertex 1.
At start X1 three options; Red, Green, Blue.
Choose X1 = Red.
▪ Then expand the tree to third level, for second vertex
Find X2 out of the three. Try X2=Red, since vertex 1 is adjacent to vertex 2 X2
can't take Red color, then stop expanding that way. Then choose Green, check
if colorable, yes. It can be colored.
1. a

▪ Then move this direction, expand tree to fourth level. Find X3, try the two
colors Red, Green one at a time. If colorable do it, else try third color Blue.
1/16/2022 qubee-tp.com 20
Homework
▪ Find at least the first four solutions

1/16/2022 qubee-tp.com 21

You might also like