0% found this document useful (0 votes)
15 views20 pages

Unit-4

Backtracking is a problem-solving technique that explores all possible solutions to find the best one, primarily used when multiple solutions are required. It is distinct from dynamic programming, which focuses on optimization problems, and is applicable in scenarios like the N-Queens problem, graph coloring, and Hamiltonian circuits. The algorithm systematically tries different sequences of decisions, backtracking when a dead-end is reached, and is characterized by terms such as live nodes, success nodes, and dead nodes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views20 pages

Unit-4

Backtracking is a problem-solving technique that explores all possible solutions to find the best one, primarily used when multiple solutions are required. It is distinct from dynamic programming, which focuses on optimization problems, and is applicable in scenarios like the N-Queens problem, graph coloring, and Hamiltonian circuits. The algorithm systematically tries different sequences of decisions, backtracking when a dead-end is reached, and is characterized by terms such as live nodes, success nodes, and dead nodes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Unit-4

Back Tracking

Backtracking:
Backtracking is one of the techniques that can be used to solve the problem. We can write the
algorithm using this strategy. It uses the Brute force search to solve the problem, and the brute
force search says that for the given problem, we try to make all the possible solutions and pick
out the best solution from all the desired solutions. This rule is also followed in dynamic
programming, but dynamic programming is used for solving optimization problems. In contrast,
backtracking is not used in solving optimization problems. Backtracking is used when we have
multiple solutions, and we require all those solutions.

Backtracking name itself suggests that we are going back and coming forward; if it satisfies the
condition, then return success, else we go back again. It is used to solve a problem in which a
sequence of objects is chosen from a specified set so that the sequence satisfies some criteria.

Use Of Backtracking Algorithm:


When we have multiple choices, then we make the decisions from the available choices. In the
following cases, we need to use the backtracking algorithm:

o A piece of sufficient information is not available to make the best choice, so we use the
backtracking strategy to try out all the possible solutions.

o Each decision leads to a new set of choices. Then again, we backtrack to make new
decisions. In this case, we need to use the backtracking strategy.

Working Of Backtracking Algorithm:


Backtracking is a systematic method of trying out various sequences of decisions until we find
out that works.

Example:

We start with a start node. First, we move to node A. Since it is not a feasible solution so we
move to the next node, i.e., B. B is also not a feasible solution, and it is a dead-end so we
backtrack from node B to node A.
Suppose another path exists from node A to node C. So, we move from node A to node C. It is
also a dead-end, so again backtrack from node C to node A. We move from node A to the
starting node.

Now we will check any other path exists from the starting node. So, we move from start node to
the node D. Since it is not a feasible solution so we move from node D to node E. The node E is
also not a feasible solution. It is a dead end so we backtrack from node E to node D.
Suppose another path exists from node D to node F. So, we move from node D to node F. Since
it is not a feasible solution and it's a dead-end, we check for another path from node F.
Suppose there is another path exists from the node F to node G so move from node F to node G.
The node G is a success node.

The terms related to the backtracking are:

o Live node: The nodes that can be further generated are known as live nodes.

o E node: The nodes whose children are being generated and become a success node.

o Success node: The node is said to be a success node if it provides a feasible solution.

o Dead node: The node which cannot be further generated and also does not provide a
feasible solution is known as a dead node.

Many problems can be solved by backtracking strategy, and that problems


satisfy complex set of constraints, and these constraints are of two types:

o Implicit constraint: It is a rule in which how each element in a tuple


is related.
o Explicit constraint: The rules that restrict each element to be chosen
from the given set.
Applications of Backtracking:
o N-queen problem

o Sum of subset problem

o Graph coloring

o Hamilton cycle

Difference between the Backtracking and Recursion:


Recursion is a technique that calls the same function again and again until we reach the base
case. Backtracking is an algorithm that finds all the possible solutions and selects the desired
solution from the given set of solutions.

N-Queens Problem:
A classic combinational problem is to place n queens on a n*n chess board so that no two
attacks, i.,e no two queens are on the same row, column or diagonal.

If we take n=4then the problem is called 4 queens problem.

If we take n=8 then the problem is called as 8 queens problem.

4-Queens problem:
Consider a 4*4 chessboard. Let there are 4 queens. The objective is place there 4 queens on 4*4
chessboard in such a way that no two queens should be placed in the same row, same column or

diagonal position.

Given a 4 x 4 chessboard and number the rows and column of the chessboard 1 through 4.
Since, we have to place 4 queens such as q 1 q2 q3 and q4 on the chessboard, such that no two
queens attack each other. In such a conditional each queen must be placed on a different row,
i.e., we put queen "i" on row "i."

Now, we place queen q1 in the very first acceptable position (1, 1). Next, we put queen q 2 so that
both these queens do not attack each other. We find that if we place q 2 in column 1 and 2, then
the dead end is encountered. Thus, the first acceptable position for q 2 in column 3, i.e. (2, 3) but
then no position is left for placing queen 'q 3' safely. So we backtrack one step and place the
queen 'q2' in (2, 4), the next best possible solution. Then we obtain the position for placing 'q 3'
which is (3, 2). But later this position also leads to a dead end, and no place is found where 'q 4'
can be placed safely. Then we have to backtrack till 'q 1' and place it to (1, 2) and then all other
queens are placed safely by moving q2 to (2, 4), q3 to (3, 1) and q4 to (4, 3). That is, we get the
solution (2, 4, 1, 3). This is one possible solution for the 4-queens problem. For another possible
solution, the whole method is repeated for all partial solutions. The other solutions for 4 -
queen’s problems is (3, 1, 4, 2) i.e.
The implicit tree for 4 - queen problem for a solution (2, 4, 1, 3) is as follows:
It can be seen that all the solutions to the 4 queens problem can be represented as 4 - tuples (x 1,
x2, x3, x4) where xi represents the column on which queen "qi" is placed.

8-queens problem:
A classic combinatorial problem is to place 8 queens on a 8*8 chess board so that no two attack,
i.,e no two queens are to the same row, column or diagonal.

Now, we will solve 8 queens problem by using similar procedure adapted for 4 queens problem.
The algorithm of 8 queens problem can be obtained by placing n=8, in N queens algorithm.

If two queens are placed at positions (i,j) and (k,l). They are on the same diagonal only if

i-j=k-l ……………….(1)or

i+j=k+l .....................(2).

From (1) and (2) implies j-l=i-k and j-l=k-i

Two queens lie on the same diagonal iff |j-l|=|i-k|.

The solution of 8 queens problem can be obtained similar to the solution of 4 queens.

problem.X1=4, X2=6, X3=8, X4=2, X5=7, X6=1, X7=3, X8=5, The solution can be shown as
Place (k, i) returns a Boolean value that is true if the kth queen can be placed in column i. It tests
both whether i is distinct from all previous costs x1, x2,....xk-1 and whether there is no other queen
on the same diagonal.

Using place, we give a precise solution to then n- queens problem.

Place (k, i)

For j ← 1 to k - 1

do if (x [j] = i)

or (Abs x [j]) - i) = (Abs (j - k))

then return false;

return true;

Place (k, i) return true if a queen can be placed in the kth row and ith column otherwise return is
false.

x [] is a global array whose final k - 1 values have been set. Abs (r) returns the absolute value of
r.

N - Queens (k, n)

For i ← 1 to n

do if Place (k, i) then

x [k] ← i;

if (k ==n) then

write (x [1....n));

else

N - Queens (k + 1, n);

}
}

Graph Coloring:
Graph coloring is the procedure of assignment of colors to each vertex of a graph G such that no
adjacent vertices get same color. The objective is to minimize the number of colors while
coloring a graph. The smallest number of colors required to color a graph G is called its
chromatic number of that graph.

Graph coloring problem is a NP Complete problem.

Method to Color a Graph:

The steps required to color a graph G with n number of vertices are as follows –

Step 1 − Arrange the vertices of the graph in some order.

Step 2 − Choose the first vertex and color it with the first color.

Step 3 − Choose the next vertex and color it with the lowest numbered color that has not been
colored on any vertices adjacent to it. If all the adjacent vertices are colored with this color,
assign a new color to it. Repeat this step until all the vertices are colored.

Example:

In the above figure, at first vertex a is colored red. As the adjacent vertices of vertex a are again
adjacent, vertex b and vertex d are colored with different color, green and blue respectively.
Then vertex c is colored as red as no adjacent vertex of c is colored red. Hence, we could color
the graph by 3 colors. Hence, the chromatic number of the graph is 3.

Applications of Graph Coloring:

Some applications of graph coloring include –

 Register Allocation

 Map Coloring

 Bipartite Graph Checking

 Mobile Radio Frequency Assignment

 Making time table, etc.

Graph Coloring Algorithm:

1. Algorithm mColoring(k)

2.//This algorithm was formed using the recursive backtracking

3. //schema. The graph is represented by its boolean adjacency

4.//matrix G [1: n,1: n]. k is the index

5.//of the next vertex to color.

6.{

7. repeat

8. {//Generate all legal assignments for x[k].

9. NextValue(k);//Assign to x[k] a legal color.

10. if(x[k]=0) then return; //No new color possible

11. if (k=n) then //At most m colors have been

// used to color the n vertices

12. write (x[1:n]);

13. else mColoring (k+1);

14. } until (false);


15. }

Hamiltonian Circuit Problems:


A Hamiltonian circuit or tour of a graph is a path that starts at a given vertex, visits each vertex
in the graph exactly once, and ends at the starting vertex.

We use the Depth-First Search algorithm to traverse the graph until all the vertices have been
visited.

We traverse the graph starting from a vertex (arbitrary vertex chosen as starting vertex) and at
any point during the traversal we get stuck (i.e., all the neighbor vertices have been visited), we
backtrack to find other paths (i.e., to visit another unvisited vertex).

If we successfully reach back to the starting vertex after visiting all the nodes, it means the graph
has a Hamiltonian cycle otherwise not.

We mark each vertex as visited so that we don’t traverse them more than once.

Example:

Find the Hamiltonian circuits of a given graph using Backtracking.

Solution:

Firstly, we start our search with vertex 'a.' this vertex 'a' becomes the root of our implicit tree.
Next, we choose vertex 'b' adjacent to 'a' as it comes first in lexicographical order (b, c, d).

Next, we select 'c' adjacent to 'b.'

Next, we select 'd' adjacent to 'c.'


Next, we select 'e' adjacent to 'd.'
Next, we select vertex 'f' adjacent to 'e.' The vertex adjacent to 'f' is d and e, but they have already
visited. Thus, we get the dead end, and we backtrack one step and remove the vertex 'f' from
partial solution.
From backtracking, the vertex adjacent to 'e' is b, c, d, and f from which vertex 'f' has already
been checked, and b, c, d have already visited. So, again we backtrack one step. Now, the vertex
adjacent to d are e, f from which e has already been checked, and adjacent of 'f' are d and e. If 'e'
vertex, revisited them we get a dead state. So again, we backtrack one step.

Now, adjacent to c is 'e' and adjacent to 'e' is 'f' and adjacent to 'f' is 'd' and adjacent to 'd' is 'a.'
Here, we get the Hamiltonian Cycle as all the vertex other than the start vertex 'a' is visited only
once. (a - b - c - e - f -d - a).
Again Backtrack
Here we have generated one Hamiltonian circuit, but another Hamiltonian circuit can also be
obtained by considering another vertex.

You might also like