Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Unit-4

Backtracking:
General Method:

Sum of subsets:

Algorithm:
Let, S is a set of elements and m is the expected sum of subsets. Then:
1. Start with an empty set.
2. Add to the subset, the next element from the list.
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 then repeat 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.

Time complexity:
In the state-space tree, at level i, the tree has 2i nodes. So, given n items, the total
number of nodes in the tree would be 1 + 2 + 22 + 23 + .. 2n.

T(n) = 1 + 2 + 22 + 23 + .. 2n = 2n+1 – 1 = O(2n)


0/1 Knapsack Problem:

Time complexity: O(2n/2)


Graph Coloring:

Algorithm or 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.
Time complexity: The number of a node increases exponentially at every level in
state space tree. With M colors and n vertices, total number of nodes in state space
tree would be 1 + M + M2 + M3 + …. + Mn
Hence, T(n) = 1 + M + M2 + M3 + …. + Mn
So, T(n) = O(Mn). Thus, the graph coloring algorithm runs in exponential time.
Hamiltonian Cycles:

Algorithm:
1. Create an empty path array.
2. Add the vertex 0 to the array.
3. Start adding vertex 1 and other connected nodes and check if the current
vertex can be included in the array or not.
4. This can be done by using a visiting array and check if the vertex has already
been visited or is adjacent to previously added vertex.
5. If any such vertex is found, add it to the array and backtrack from that node.
6. Try every possible combinations and if a path returns false, ignore the
vertex and start iterating from the next vertex till all the nodes has been
visited.
Time Complexity: O(N!), where N is number of vertices.
N-Queens 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, i.e no two queens attack each other by being
in same row, column or diagonal”.
Algorithm for N queen problem:
Following is the backtracking algorithm for solving the N-Queen problem
1. Initialize an empty chessboard of size NxN.
2. Start with the leftmost column and place a queen in the first row of that
column.
3. Move to the next column and place a queen in the first row of that column.
4. Repeat step 3 until either all N queens have been placed or it is impossible to
place a queen in the current column without violating the rules of the
problem.
5. If all N queens have been placed, print the solution.
6. If it is not possible to place a queen in the current column without violating
the rules of the problem, backtrack to the previous column.
7. Remove the queen from the previous column and move it down one row.
8. Repeat steps 4-7 until all possible configurations have been tried.

For N = 1, this is trivial case. For N = 2 and N = 3, solution is not possible. So we start
with N = 4 and we will generalize it for N queens.
Time complexity: For finding a single solution where the first queen Q has been
assigned the first column and can be put on N positions, the second queen has been
assigned the second column and would choose from N-1 possible positions and so
on; the time complexity is O ( N ) * ( N - 1 ) * ( N - 2 ) * … 1 ). i.e The worst-case time
complexity is O ( N! ).
4-Queens Problem:
Given 4 x 4 chessboard, arrange four queens in a way, such that no two queens
attack each other. That is, no two queens are placed in the same row, column, or
diagonal.
We have to arrange four queens, Q1, Q2, Q3 and Q4 in 4 x 4 chess board. We will
put ith queen in ith row.
 Let us start with position (1, 1). Q1 is the only queen, so there is no issue.
partial solution is <1>
 We cannot place Q2 at positions (2, 1) or (2, 2). Position (2, 3) is acceptable.
partial solution is <1, 3>.
 Next, Q3 cannot be placed in position (3, 1) as Q1 attacks her. And it cannot
be placed at (3, 2), (3, 3) or (3, 4) as Q2 attacks her. There is no way to put Q3
in third row. Hence, the algorithm backtracks and goes back to the previous
solution and readjusts the position of queen Q2. Q2 is moved from positions
(2, 3) to (2, 4). Partial solution is <1, 4>
 Now, Q3 can be placed at position (3, 2). Partial solution is <1, 4, 3>.
 Queen Q4 cannot be placed anywhere in row four. So again, backtrack to the
previous solution and readjust the position of Q3. Q3 cannot be placed on (3,
3) or(3, 4). So the algorithm backtracks even further.
 All possible choices for Q2 are already explored, hence the algorithm goes
back to partial solution <1> and moves the queen Q1 from (1, 1) to (1, 2). And
this process continues until a solution is found.
 All possible solutions ( (2, 4, 1, 3) & (3, 1, 4, 2) ) for 4-queen are shown in
 fig (a) & fig. (b)
State space tree:
The solution tree or State space tree for the 4-queen problem is shown in below
Fig.

It can be seen that all the solutions to the 4 queens problem can be represented as
4 - tuples (x1, x2, x3, x4) where xi represents the column on which queen "qi" is
placed.

8-Queens Problem:
Given an 8 x 8 chessboard, arrange 8 queens in a way such that no two queens
attack each other.
One possible solution for 8 queens problem is shown in below fig:
Thus, the solution for 8 -queen problem is (4, 6, 8, 2, 7, 1, 3, 5).
If two queens are placed at position (i, j) and (k, l).
Then they are on same diagonal only if j - l = i - k.

You might also like