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

1

Design and Analysis of


Algorithms
CHAPTER FIVE: BACKTRACKING
Scenario… 2
 To easily grasp the idea of backtracking consider how blind people walks in new
environment!
 They walks on the road…
 If they find any obstacle in their way?
 They would just move backward and proceed in other direction.
 How they move backward? …by intelligence.
 The same can be applies for algorithms.
 Suppose we have to make decisions, among various choices, where we don’t have enough
information to know what to chose and each decision leads to a new set of choices.
 Some sequence of choices (possibly more than one) may be a solution to our problem.
Backtracking 3
 Backtracking is a refinement of brute force search. It used to solve problems in
which a sequence of objects is chosen from a specified set so that the sequence
satisfy some criterion.
 Backtracking is used when there is a sequence of decisions to be made, from a
number of available choices, where
 Sufficient information is not available on the best choice.
 Each decision leads to a new set of choices.
 Some sequence of choices (possibly more than one) may be a solution to your problem.
 How? or when to backtrack?
 Backtracking is a systematic method of trying out various sequences of decisions, until
you find one that “works”.
Cont… 4
 Backtracking finds the solution by assuming that the solutions are represented by
vectors (v1, ..., vm) of values and by traversing, in a depth first manner, the domains
of the vectors until the solutions are found.
 When invoked, the algorithm starts with an empty vector.
 At each stage it extends the partial vector with a new value.
 Upon reaching a partial vector (v1, ..., vi) which can’t represent a partial solution, the
algorithm backtracks by removing the trailing value from the vector, and then proceeds
by trying to extend the vector with alternative values.
 The traversal of the solution space can be represented by a depth-first traversal of a tree. The
tree itself is rarely entirely stored by the algorithm in discourse, instead just a path toward a
root is stored, to enable the backtracking.
Cont… 5
 In case of greedy and dynamic programming techniques, we will use Brute force
approach. It means, we will evaluate all possible solutions, among which, we select
one solution as optimal solution.
 In backtracking technique, we will get all possible solutions using a bounding
functions (criterion functions), implicit and explicit conditions.
1. Explicit constraints: are rules which restrict each xi to take on values only from a
given set.
 E.g. in 4 queens problem, the 4 queens can be placed in 4x4 chess board in 4 4 ways.
2. Implicit constraints: are rules which determine which of the tuples in the solution space satisfy
criterion function.
 E.g. in 4 queens problem, the implicit constraints are no 2 queens can be on the same row, same column
and same diagonal.
Backtracking 6

Backtracking uses DFS (Depth-First Search) to generate state space tree.


Graph coloring problem 7
 Traditionally graph coloring problem is categorized into two types—Vertex coloring and edge
coloring.
 These two coloring problems are moreover similar and can be defined as given.
 Given G an undirected graph and a number m, determine if the graph can be colored with m colors such that
no two adjacent vertices (or edges) of the graph are colored with same color.
 The convention of using colors originates from coloring the countries of a map, where each face is
literally colored. This was generalized to coloring the faces of a graph embedded in the plane.
 Moreover graph coloring enjoys many practical as well as theoretical challenges and it is still a very
active field of research.
Cont… 8
 M-coloring decision problem
 Provided a graph G and m number of colors
 M-coloring decision problem will determine whether the graph G can be colored with the provided number
of colors or not.
 M-coloring permutation problem
 Provided a graph G and m number of colors
 M-coloring permutation problem will determine all possible ways of coloring a given graph with a list of
available m-colors.
 M-coloring optimization problem
 Provided a graph G
 M-coloring optimization problem will determine the chromatic number (which is minimum number of m-
colors) enough to color the given graph.
Cont… 9
Algorithm mColoring(k) Algorithm NextValue(k)
1. Repeat{ 1. Repeat{
2. NextValue(k) 2. x[k]= (x[k]+1) % (m+1)
3. if x[k]==0: 3. if x[k]==0:
4. return 4. return
5. else if k==n 5. for j=1 upsto n:
6. print x[1:n] 6. if(G[k, j] !=0 and x[k]==x[j])
7. else 7. break
8. mColoring(k+1) 8. if j==n+1
9. }until(false) 9. return
10. }until(false)
N-queens problem 10
 The N-queens problem is the problem of placing N chess queens on an N × N chessboard so that no
two queens attack each other.
 Two queens attack each other by being on the:
1. same row
2. same column
3. or on same diagonal.
 If there exists any solution to N queens problem, then there exists one queen in each row. Therefore,
we will represent our possible solution using an array Q [1........ n], where Q[i] indicates which
square in row i contains a queen, or zero if no queen has been placed in row i.
 To get a solution, we need to put queens on the board row by row, starting from the top. We can give
a partial solution for the array Q[1........n] whose first r – 1 entries are positive and whose last n – r +
1 entries are all zeros, for some integer r.
N-Queens… 11
Algorithm N-QUEENS (k, n) Algorithm isPlaceable (k, i)
1. for i=1 upsto n 1. for j=1 upsto k-1
2. if( isPlaceable(k, i)) 2. if((x[j]== i) OR (Abs(x[j]-i) == Abs(j-k))
3. x[k]= i 3. return false
4. if (k==n) 4. return true
5. print (x[1: n])
6. else
7. N-QUEENS( k+1, n)
Hamiltonian Cycle 12
 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.
 In other words if a Hamiltonian cycle begins at some vertex v 1  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, which are equal.

A. Graph containing Hamiltonian cycle B. Graph containing no Hamiltonian


cycle
Cont… 13
 To check whether there is a Hamiltonian cycle or not we may use backtracking method. The graph
may be directed or undirected. Only distinct cycles are output.
 The backtracking solution vector (X1,X2,X3,…Xn) is defined so that xi represents the ith visited vertex
of the proposed cycle.
 Now all we need to do is determine how to compute the set of possible vertices for x k if x1...xk-1 have
already been chosen. If k=1 then x1 can be any of the n vertices.
 The algorithm nextvalue(k) which determines a possible next vertex for the proposed cycle. Using
nextvalue we can particularize the recursive backtracking schema to find all Hamiltonian cycles.
Hamiltonian Cycle… 14

Algorithm Hamiltonian (k) Algorithm nextvalue (k)


1. repeat 1. Repeat
2. nextvalue(k); 2. x[k]=(x[k]+1)mod(n+1);
3. if (x[k]=0) 3. if (x[k]=0) then
4. return; 4. return;
5. if (k=n) 5. if (G[x[k-1],x[k]]]!=0) then
6. print (x[1:n]); 6. for j=1 to k-1 do
7. else 7. if (x[j]=x[k])
8. Hamiltonian(k+1); 8. break;
9. until (false) 9. if (j=k) then
10. if((k<n) or ((k=n)and G[x[n],x[1]]!=0))
11. return;
12. until (false);
Cont… 15
 Consider the time complexity of all the above algorithms discussed in backtracking!

You might also like