DAA - Unit 5 (PG)
DAA - Unit 5 (PG)
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 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 implicit tree for 4 - queen problem for a solution (2, 4, 1, 3) is as follows:
Algorithm
SUB_SET_PROBLEM(i, sum, W, remSum)
// Description : Solve sub of subset problem using backtracking
// Input :
W: Number for which subset is to be computed
i: Item index
sum : Sum of integers selected so far
remSum : Size of remaining problem i.e. (W – sum)
// Output : Solution tuple X
Optimal Solution (uses 2 colors)
Eg: C1 – Red, Cs – Blue (none of
the adjacent vertex gets the same
color)
Complexity Analysis
The number of anode 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
= fracMn+1 - 1M - 1
So, T(n) = O(Mn).
Thus, the graph coloring algorithm runs in exponential time.
Solution:
We can start with any random vertex. Let us start with vertex A. Neighbors of
A are {B, C, D}. The inclusion of B does not lead to a complete solution. So explore
it.
Adjacent vertices to B are {C, D}. The inclusion of C does not lead to a
complete solution. All adjacent vertices of C are already members of the path formed
so far. So it leads to a dead end. Backtrack and go to B and explore its next neighbor
i.e. D.
The inclusion of D does not lead to a complete solution, and all adjacent
vertices of D are already a member of the path formed so far. So it leads to a dead
end.
Rules
To reduce a matrix, perform the row reduction and column reduction of the
matrix separately.
A row or a column is said to be reduced if it contains at least one entry ‘0’ in it.
Row Reduction-
Consider the rows of above matrix one by one.
If the row already contains an entry ‘0’, then-
There is no need to reduce that row.
Column Reduction-
Consider the columns of above row-reduced matrix one by one.
If the column already contains an entry ‘0’, then-
There is no need to reduce that column.
If the column does not contains an entry ‘0’, then-
Reduce that particular column.
Select the least value element from that column.
Subtract that element from each element of that column.
This will create an entry ‘0’ in that column, thus reducing that column.
Now,
We reduce this matrix.
Then, we find out the cost of node-02.
Now,
We reduce this matrix.
Then, we find out the cost of node-03.
Row Reduction-
We can not reduce row-1 as all its elements are
∞.
There is no need to reduce row-2.
There is no need to reduce row-3.
There is no need to reduce row-4.
Thus, the matrix is already row-reduced.
Column Reduction-
There is no need to reduce column-1.
There is no need to reduce column-2.
We can not reduce column-3 as all its elements are ∞.
There is no need to reduce column-4.
Thus, the matrix is already column reduced.
Finally, the matrix is completely reduced.
Now, we calculate the cost of node-3.
Cost(3) = Cost(1) + Sum of reduction elements
+ M[A,C]
= 18 + 0 + 7
= 25
Choosing To Go To Vertex-D: Node-4 (Path A
→ D)
From the reduced matrix of step-01, M[A,D] =
3
Set row-A and column-D to ∞
Set M[D,A] = ∞