0% found this document useful (0 votes)
302 views16 pages

Daa Unit-Iv

The document discusses backtracking algorithms and their applications. Backtracking is a general algorithmic technique that considers all possible combinations to solve a problem by incrementally building solutions and removing those that violate constraints. It is useful when not enough information is available to make optimal choices or when decisions lead to new choices. Applications discussed include the n-queens problem, subset sum problem, graph coloring, and Hamiltonian cycles.

Uploaded by

sivakanthvaddi61
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)
302 views16 pages

Daa Unit-Iv

The document discusses backtracking algorithms and their applications. Backtracking is a general algorithmic technique that considers all possible combinations to solve a problem by incrementally building solutions and removing those that violate constraints. It is useful when not enough information is available to make optimal choices or when decisions lead to new choices. Applications discussed include the n-queens problem, subset sum problem, graph coloring, and Hamiltonian cycles.

Uploaded by

sivakanthvaddi61
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/ 16

UNIT IV: Backtracking

The General Method, the 8-Queens problem, sum of subsets, Graph coloring, Hamiltonian cycles, knapsack
problem.

……………………………………………………………………………………………………………………………..

1. The General Method

Backtracking can be defined as a general algorithmic technique that considers searching every possible
combination in order to solve a computational problem.

What is Backtracking Algorithm?

Backtracking is an algorithmic technique for solving problems recursively by trying to build a solution
incrementally, one piece at a time, removing those solutions that fail to satisfy the constraints of the problem
at any point of time (by time, here, is referred to the time elapsed till reaching any level of the search tree).

Types of Backtracking Algorithm

There are three types of problems in backtracking –

1. Decision Problem – In this, we search for a feasible solution.

2. Optimization Problem – In this, we search for the best solution.

3. Enumeration Problem – In this, we find all feasible solutions.

When can be Backtracking Algorithm used?

For example, consider the SudoKo solving Problem, we try filling digits one by one. Whenever we find that
current digit cannot lead to a solution, we remove it (backtrack) and try next digit. This is better than naive
approach (generating all possible combinations of digits and then trying every combination one by one) as it
drops a set of permutations whenever it backtracks.

• Brute Force approach finds all the possible solutions and selects desired solution per given the
constraints.
• Dynamic Programming also uses Brute Force approach to find the OPTIMUM solution, either
maximum or minimum.
• Backtracking also uses Brute Force approach but to find ALL the solutions.
• Solutions to the Backtracking problems can be represented as State-Space Tree.
• The constrained applied to find the solution is called Bounding function.
• Backtracking follows Depth-First Search method.
• Branch and Bound is also a Brute Force approach, which uses Breadth-First Search method.
When to use a 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:

• 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.

• 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.

The terms related to the backtracking are:

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

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

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

• 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:

• Implicit constraint: It is a rule in which how each element in a tuple is related.

• Explicit constraint: The rules that restrict each element to be chosen from the given set.

Applications of Backtracking

• N-queen problem

• Sum of subset problem

• Graph coloring

• Hamiliton cycle

Difference between the Backtracking and Recursion

Recursion is a technique that calls the same function again and again until you 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.
2. The N-Queens problem
• A classic combinational problem is to place n queens on a n*n chess board so that no two attack, 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.

The explicit constraints are 4 queens are to be placed on 4*4 chessboards in 44 ways.

The implicit constraints are no two queens are in the same row column or diagonal.

Let{x1, x2, x3, x4} be the solution vector where x1 column on which the queen i is placed.

First queen is placed in first row and second column

As the above combination was not possible, we will go back and go for the next iteration. This means we
will change the position of the second queen.
In this, we found a solution.

Now let's take a look at the backtracking algorithm and see how it works:

The idea is to place the queens one after the other in columns, and check if previously placed queens cannot
attack the current queen we're about to place.

If we find such a row, we return true and put the row and column as part of the solution matrix. If such a
column does not exist, we return false and backtrack*

Hence the solution of to 4-queens’s problem is x1=2, x2=4, x3=1, x4=3, i.,e first queen is placed in 2nd
column, second queen is placed in 4th column and third queen is placed in first column and fourth queen is
placed in third column.

Algorithm

START

1. begin from the leftmost column

2. if all the queens are placed, return true/ print configuration

3. check for all rows in the current column

a) if queen placed safely, mark row and column; and recursively check if we approach in the current

configuration, do we obtain a solution or not

b) if placing yields, a solution, return true

c) if placing does not yield a solution, unmark and try other rows

4. if all rows tried and solution not obtained, return false and backtrack

END

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.

N ow, 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
the recurrence relation is:

T(n) = O(n^2) + n * T(n-1)

solving the above recurrence by iteration or recursion tree, the time complexity of the nQueen problem is =
O(N!)

Finding solutions through State Space Tree:

State space tree can be drawn using backtracking and by this, we will be able to find the all possible solution
for the 4 queen problem and not only 4 queens by this we can find all possible solutions to N queen
problem

State Space Tree of 4 Queen Problem

• The simple logic of generating the state space tree is to keep exploring all possible solutions using
backtracking and stop exploring that solution where ever two queens are attacking each other.
For all the positions(columns) in the current row:

• Check for all the previous rows is there a queen or not?

• Check for all the previous diagonal columns is there a queen or not?

• If any of these conditions are true, then backtrack to the previous row and move the previous queen
1 step forward.

• Otherwise, put the current queen in the position and move to the next row.

3. Sum of subset

Subset sum problem is the problem of finding a subset such that the sum of elements equal a given number.
The backtracking approach generates all permutations in the worst case but in general, performs better than
the recursive approach towards subset sum problem.

A subset A of n positive integers and a value sum(d) is given, find whether or not there exists any subset of
the given set, the sum of whose elements is equal to the given value of sum.

Steps:

1. Start with an empty set

2. Add the next element from the list to the set

3. If the subset is having sum M, then stop with that subset as solution.

4. If the subset is not feasible or if we have reached the end of the set, then backtrack through the subset
until we find the most suitable value.

5. If the subset is feasible (sum of subset < d) then go to step 2.

6. If we have visited all the elements without finding a suitable subset and if no backtracking is possible then
stop without solution.

Example: S = {3,5,6,7} and d = 15, Find the sum of subsets by using backtracking

Time Complexity: O(N * sum) where N is the size of the array.

Space Complexity: O(N * sum) where N is the size of the array.


3. Graph coloring (for planar graphs)
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
termed the m-colorabiltiy decision problem. The m-colorability optimization problem asks for the smallest
integer m for which the graph G can be colored.

Given any map, if the regions are to be colored in such a way that no two adjacent regions have the same
color, only four colors are needed.

For many years it was known that five colors were sufficient to color any map, but no map that required more
than four colors had ever been found. After several hundred years, this problem was solved by a group of
mathematicians with the help of a computer. They showed that in fact four colors are sufficient for planar
graphs.

The function m-coloring will begin by first assigning the graph to its adjacency matrix, setting the array x [] to
zero. The colors are represented by the integers 1, 2, . . . , m and the solutions are given by the n-tuple (x1,
x2, . . ., xn), where xi is the color of node i.

A recursive backtracking algorithm for graph coloring is carried out by invoking the statement mcoloring(1);

If ‘d’ is the degree of the node them you can color that node using ‘d+1’ colors
Algorithm mcoloring (k)

// This algorithm was formed using the recursive backtracking schema. The graph is

// represented by its Boolean adjacency matrix G [1: n, 1: n]. All assignments of

// 1, 2, ......... , m to the vertices of the graph such that adjacent vertices are assigned

// distinct integers are printed. k is the index of the next vertex to color.

repeat

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

NextValue (k); // Assign to x [k] a legal color. If (x [k] = 0) then return; // No new color possible If (k = n)
then // at most m colors have been

// used to color the n vertices.

write (x [1: n]);

else mcoloring (k+1);

} until (false);

Algorithm NextValue (k)

// x [1] , ........ x [k-1] have been assigned integer values in the range [1, m] such that

// adjacent vertices have distinct integers. A value for x [k] is determined in the range

// [0, m].x[k] is assigned the next highest numbered color while maintaining distinctness

// from the adjacent vertices of vertex k. If no such color exists, then x [k] is 0.

repeat

x [k]: = (x [k] +1) mod (m+1) // Next highest color.

If (x [k] = 0) then return; // All colors have been used for j := 1 to n do

{ // check if this color is distinct from adjacent colors if ((G [k, j] 0) and (x [k] = x [j]))

// If (k, j) is and edge and if adj. vertices have the same color. then break;

if (j = n+1) then return; // New color found

} until (false); // Otherwise try to find another color.

}
Example:

Color the graph given below with minimum number of colors(m=3) by backtracking using state space tree

Applications of Graph Coloring:

The graph coloring problem has huge number of applications.

1) Making Schedule or Time Table: Suppose we want to make am exam schedule for a university. We have list
different subjects and students enrolled in every subject. Many subjects would have common students (of same
batch, some backlog students, etc). How do we schedule the exam so that no two exams with a common
student are scheduled at same time? How many minimum time slots are needed to schedule all exams? This
problem can be represented as a graph where every vertex is a subject and an edge between two vertices
mean there is a common student. So this is a graph coloring problem where minimum number of time slots is
equal to the chromatic number of the graph.

2) Mobile Radio Frequency Assignment: When frequencies are assigned to towers, frequencies assigned to all
towers at the same location must be different. How to assign frequencies with this constraint? What is the
minimum number of frequencies needed? This problem is also an instance of graph coloring problem where
every tower represents a vertex and an edge between two towers represents that they are in range of each
other.

3) Sudoku: Sudoku is also a variation of Graph coloring problem where every cell represents a vertex. There
is an edge between two vertices if they are in same row or same column or same block.

4) Register Allocation: In compiler optimization, register allocation is the process of assigning a large number
of target program variables onto a small number of CPU registers. This problem is also a graph coloring
problem.

5) Bipartite Graphs: We can check if a graph is Bipartite or not by coloring the graph using two colors. If a
given graph is 2-colorable, then it is Bipartite, otherwise not. See this for more details.

6) Map Coloring: Geographical maps of countries or states where no two adjacent cities cannot be assigned
same color. Four colors are sufficient to color any map
4. Hamiltonian cycles
Hamiltonian Path in an undirected graph is a path that visits each vertex exactly once. A Hamiltonian cycle (or
Hamiltonian circuit) is a Hamiltonian Path such that there is an edge (in the graph) from the last vertex to the
first vertex of the Hamiltonian Path.

For example, a Hamiltonian Cycle in the following graph is {0, 1, 2, 4, 3, 0}.

(0)--(1)--(2)

| /\ |

| / \ |

|/ \|

(3)-------(4)

And the following graph doesn’t contain any Hamiltonian Cycle.

(0)--(1)--(2)

| /\ |

| / \ |

|/ \|

(3) (4)
Given a graph G = (V, E) we have to find the Hamiltonian Circuit using Backtracking approach. We start our
search from any arbitrary vertex say 'a.' This vertex 'a' becomes the root of our implicit tree. The first element
of our partial solution is the first intermediate vertex of the Hamiltonian Cycle that is to be constructed. The
next adjacent vertex is selected by alphabetical order. If at any stage any arbitrary vertex makes a cycle with
any vertex other than vertex 'a' then we say that dead end is reached. In this case, we backtrack one step,
and again the search begins by selecting another vertex and backtrack the element from the partial; solution
must be removed. The search using backtracking is successful if a Hamiltonian Cycle is obtained.

Example: Consider a graph G = (V, E) shown in fig. we have to find a Hamiltonian circuit using Backtracking
method.

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.

5. knapsack problem.
Previous Year Questions

1. Write control abstraction for backtracking. Explain with an example.


2. Why Backtracking always produces an optimal solution? Justify
3. Draw the state-space tree along with answer nodes for 4-queens problem
4. What is a backtracking? Give the explicit and implicit constraints in 8 queen’s problem.
5. State N-Queens problem and solve 8-Queens problem using backtracking
6. Find all possible subsets of w that sum to m. Let w={5,7,10,12,15,18,20}and m=35 and draw the
portion of the state space tree that is generated using backtracking.
7. Give the statement of sum –of subsets problem. Find all sum of subsets for n=4, (w1, w2, w3, w4) =
(11, 13, 24, 7) and M=31. Draw the portion of the state space tree using fixed –tuple sized approach.
8. Solve the following instance of sum of subsets problem using backtracking. W = (5, 7, 10, 12, 15); M
= 15.
9. Explain the Graph–Coloring problem and draw the state space tree for m= 3 colors and n=4 vertices
graph. Discuss the time and space complexity.
10. State and explain m- colourability decision problem.
11. Write an algorithm for finding m-coloring of a graph and explain with an example.
12. What are the applications of graph coloring? Explain in detail.
13. What is a Hamiltonian Cycle? Explain how to find Hamiltonian path and cycle using backtracking
algorithm?
14. Write an algorithm to determine the Hamiltonian Cycle in a given graph using backtracking.

You might also like