0% found this document useful (0 votes)
3 views38 pages

DAA - Unit 5 (PG)

The document discusses the backtracking algorithm, a method for solving problems by exploring all possible solutions and returning to previous states when a dead end is reached. It covers specific applications such as the N-Queens problem, the Sum of Subsets problem, and provides examples and algorithms for each. Additionally, it explains key concepts like live nodes, success nodes, and the state space tree related to backtracking.

Uploaded by

balaji1806j
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)
3 views38 pages

DAA - Unit 5 (PG)

The document discusses the backtracking algorithm, a method for solving problems by exploring all possible solutions and returning to previous states when a dead end is reached. It covers specific applications such as the N-Queens problem, the Sum of Subsets problem, and provides examples and algorithms for each. Additionally, it explains key concepts like live nodes, success nodes, and the state space tree related to backtracking.

Uploaded by

balaji1806j
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/ 38

DR. R. K.

SHANMUGAM COLLEGE OF ARTS AND SCIENCE


INDILI, KALLAKURICHI 606 213

ANALYSIS & DESIGN OF ALGORITHMS


(23PCSCC11)
DEPARTMENT OF COMPUTER SCIENCE

CLASS: I M. Sc., COMPUTER SCIENCE


SEMESTER: 1
ANALYSIS & DESIGN OF ALGORITHMS
(23PCSCC11)
UNIT – 5
BACKTRACKING
 General Method
 8-Queens Problem
 Sum Of Subsets
 Graph Coloring
 Hamiltonian Cycles
Branch And Bound
 The Method
 Traveling Salesperson
ANALYSIS & DESIGN OF ALGORITHMS (23PCSCC11) UNIT – 5
I M. Sc., Computer Science Semester: 1
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.
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.
Working of Backtracking method:
Backtracking is a systematic method of trying out various sequences of
decisions until we find out that works. Let's understand through an 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.

DR. R. K. SHANMUGAM COLLEGE OF ARTS AND SCIENCE, KALLAKURICHI 1


ANALYSIS & DESIGN OF ALGORITHMS (23PCSCC11) UNIT – 5
I M. Sc., Computer Science Semester: 1

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.

DR. R. K. SHANMUGAM COLLEGE OF ARTS AND SCIENCE, KALLAKURICHI 2


ANALYSIS & DESIGN OF ALGORITHMS (23PCSCC11) UNIT – 5
I M. Sc., Computer Science Semester: 1
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.

DR. R. K. SHANMUGAM COLLEGE OF ARTS AND SCIENCE, KALLAKURICHI 3


ANALYSIS & DESIGN OF ALGORITHMS (23PCSCC11) UNIT – 5
I M. Sc., Computer Science Semester: 1
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.
Applications of Backtracking
 N-queen problem
 Sum of subset problem
 Graph coloring
 Hamiltonian cycle
State Space Tree: A space state tree is a tree representing all the possible states
(solution or non-solution) of the problem from the root as an initial state to the leaf
as a terminal state.

DR. R. K. SHANMUGAM COLLEGE OF ARTS AND SCIENCE, KALLAKURICHI 4


ANALYSIS & DESIGN OF ALGORITHMS (23PCSCC11) UNIT – 5
I M. Sc., Computer Science Semester: 1
Backtracking Algorithm
Backtrack(x)
if x is not a solution
return false
if x is a new solution
add to list of solutions
backtrack(expand x)
8 QUEENS PROBLEM
N - Queens problem is to place n - queens in such a manner on an n x n
chessboard that no queens attack each other by being in the same row, column or
diagonal.
It can be seen that for n =1, the problem has a trivial solution, and no solution
exists for n =2 and n =3. So first we will consider the 4 queens problem and then
generate it to n - queens problem.
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 q1
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 q2
so that both these queens do not attack each other.
We find that if we place q2 in column 1 and 2,
then the dead end is encountered. Thus the first acceptable position for q2 in column
3, i.e. (2, 3) but then no position is left for placing queen 'q3' 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 'q3' which is (3, 2). But later this position also leads to

DR. R. K. SHANMUGAM COLLEGE OF ARTS AND SCIENCE, KALLAKURICHI 5


ANALYSIS & DESIGN OF ALGORITHMS (23PCSCC11) UNIT – 5
I M. Sc., Computer Science Semester: 1
a dead end, and no place is found where 'q4' can be placed safely. Then we have to
backtrack till 'q1' 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 - queens 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:

DR. R. K. SHANMUGAM COLLEGE OF ARTS AND SCIENCE, KALLAKURICHI 6


ANALYSIS & DESIGN OF ALGORITHMS (23PCSCC11) UNIT – 5
I M. Sc., Computer Science Semester: 1

DR. R. K. SHANMUGAM COLLEGE OF ARTS AND SCIENCE, KALLAKURICHI 7


ANALYSIS & DESIGN OF ALGORITHMS (23PCSCC11) UNIT – 5
I M. Sc., Computer Science Semester: 1
One possible solution for 8 queens problem is.,

Thus, the solution for 8 -queen problem for (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 (i - j) = k - l or i + j = k + l.
The first equation implies that j - l = i - k.
The second equation implies that j - l = k - i.
Therefore, two queens lie on the duplicate diagonal if and only if |j-l|=|i-k|

DR. R. K. SHANMUGAM COLLEGE OF ARTS AND SCIENCE, KALLAKURICHI 8


ANALYSIS & DESIGN OF ALGORITHMS (23PCSCC11) UNIT – 5
I M. Sc., Computer Science Semester: 1
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.
Algorithm for Queens Problem:
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);
}
}
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;
}

DR. R. K. SHANMUGAM COLLEGE OF ARTS AND SCIENCE, KALLAKURICHI 9


ANALYSIS & DESIGN OF ALGORITHMS (23PCSCC11) UNIT – 5
I M. Sc., Computer Science Semester: 1
SUM OF SUBSETS
It is one of the most important problems in complexity theory. The problem is
given an A set of integers a1, a2,…., an upto n integers. The question arises that is
there a non-empty subset such that the sum of the subset is given as M integer?.
For example, the set is given as [5, 2, 1, 3, 9], and the sum of the subset is 9;
the answer is YES as the sum of the subset [5, 3, 1] is equal to 9. This is an NP-
complete problem again. It is the special case of knapsack.
Example:
We have a set of 5 integers given below:
N = 4, -2, 2, 3, 1
We want to find out the subset whose sum is equal to 5. There are many solutions to
this problem.
The naïve approach, i.e., brute-force search generates all the possible subsets
of the original array, i.e., there are 2n possible states. Here the running time
complexity would be exponential. Then, we consider all these subsets in O(N) linear
running time and checks whether the sum of the items is M or not.
It is intuitive to derive the complexity of sum of the subset problem. 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)

DR. R. K. SHANMUGAM COLLEGE OF ARTS AND SCIENCE, KALLAKURICHI 10


ANALYSIS & DESIGN OF ALGORITHMS (23PCSCC11) UNIT – 5
I M. Sc., Computer Science Semester: 1

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

DR. R. K. SHANMUGAM COLLEGE OF ARTS AND SCIENCE, KALLAKURICHI 11


ANALYSIS & DESIGN OF ALGORITHMS (23PCSCC11) UNIT – 5
I M. Sc., Computer Science Semester: 1
Example:
Consider the sum-of-subset problem, n = 4, Sum = 13, and w1 = 3, w2 = 4, w3 = 5
and w4 = 6. Find a solution to the problem using backtracking. Show the state-space
tree leading to the solution. Also, number the nodes in the tree in the order of
recursion calls.
Solution:
The correct combination to get the sum of M = 13 for given W = {3, 4, 5, 6} is [3, 4,
6]. The solution vector for [3, 4, 6] would be X = [1, 1, 0, 1] because element 5 is not
chosen, so X[3] = 0. Let’s derive the solution using backtracking. The numbers in W
are already sorted.
Set X = [0, 0, 0, 0]
Set Sum = 0. Sum indicates summation of selected numbers from W.
Step 1 : i = 1, Adding item wi
Sum = Sum + wi = Sum + w1 = 0 + 3 = 3
Sum ≤ M, so add item i to solution set.
X[i] = X[1] = 1 ⇒ X =[1, 0, 0, 0]
Step 2 : i = 2, Adding item w2
Sum = Sum + wi = Sum + w2 = 3 + 4 = 7
Sum ≤ M, so add item i to solution set.
X[i] = X[2] = 1 ⇒ X =[1, 1, 0, 0]
Step 3 : i = 3, Adding item w3
Sum = Sum + wi = Sum + w3 = 7 + 5 = 12
Sum ≤ M, so add item i to solution set.
X[i] = X[3] = 1 ⇒ X =[1, 1, 1, 0]
Step 4 : i = 4, Adding item w4
Sum = Sum + wi = Sum + w4 = 12 + 6 = 18
Sum > M, so backtrack and remove the previously added item from the solution set.
X[i] = X[3] = 0 ⇒ X =[1, 1, 0, 0].
Update Sum accordingly. So, Sum = Sum – w3 = 12 – 5 = 7

DR. R. K. SHANMUGAM COLLEGE OF ARTS AND SCIENCE, KALLAKURICHI 12


ANALYSIS & DESIGN OF ALGORITHMS (23PCSCC11) UNIT – 5
I M. Sc., Computer Science Semester: 1
And don’t increment i.
Step 5 : i = 4, Adding item w4
Sum = Sum + wi = Sum + w4 = 7 + 6 = 13
Sum = M, so solution is found and add item i to solution set.
X[i] = X[4] = 1 ⇒ X =[1, 1, 0, 1]
A complete state space tree for given data is

DR. R. K. SHANMUGAM COLLEGE OF ARTS AND SCIENCE, KALLAKURICHI 13


ANALYSIS & DESIGN OF ALGORITHMS (23PCSCC11) UNIT – 5
I M. Sc., Computer Science Semester: 1
GRAPH COLORING
Graph coloring refers to the problem of coloring vertices of a graph in such a
way that no two adjacent vertices have the same color. This is also called the vertex
coloring problem. If coloring is done using at most k colors, it is called k-coloring.
The smallest number of colors required for coloring graph is called its chromatic
number. The chromatic number is denoted by X(G).
Finding the chromatic number for the graph is NP-complete problem.
Graph coloring problem is both, decision problem as well as an optimization
problem.
Applications of Graph Coloring Problem
 Design a timetable
 Sudoku
 Register allocation in the compiler
 Map coloring
 Mobile radio frequency assignment
Algorithm
GRAPH_COLORING(G, COLOR, i)
// Description : Solve the graph coloring problem using backtracking
Input : Graph G with n vertices, list of colors, initial vertex i
COLOR[1…n] is the array of n different colors
Output : Colored graph with minimum color

DR. R. K. SHANMUGAM COLLEGE OF ARTS AND SCIENCE, KALLAKURICHI 14


ANALYSIS & DESIGN OF ALGORITHMS (23PCSCC11) UNIT – 5
I M. Sc., Computer Science Semester: 1

 Non Optimal Solution


(uses 5 colors)


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.

DR. R. K. SHANMUGAM COLLEGE OF ARTS AND SCIENCE, KALLAKURICHI 15


ANALYSIS & DESIGN OF ALGORITHMS (23PCSCC11) UNIT – 5
I M. Sc., Computer Science Semester: 1
Chromatic Number
The chromatic number can be described as the minimum number of colors
required to properly color any graph. In other words, the chromatic number can be
described as a minimum number of colors that are needed to color any graph in such
a way that no two adjacent vertices of a graph will be assigned the same color.
Example:
Find the chromatic number for the following:

From the above table,


In the above graph, we are required minimum 4 numbers of colors to color the
graph.
Therefore, we can say that the Chromatic number of above graph = 4

DR. R. K. SHANMUGAM COLLEGE OF ARTS AND SCIENCE, KALLAKURICHI 16


ANALYSIS & DESIGN OF ALGORITHMS (23PCSCC11) UNIT – 5
I M. Sc., Computer Science Semester: 1
HAMILTONIAN CYCLES
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.
The Hamiltonian cycle is the cycle in the graph which visits all the vertices in
graph exactly once and terminates at the starting node. It may not include all the
edges.
The Hamiltonian cycle problem is the problem of finding a Hamiltonian cycle
in a graph if there exists any such cycle.
The input to the problem is an undirected, connected graph. For the graph, a
path A – B – E – D – C – A forms a Hamiltonian cycle. It visits all the vertices
exactly once, but does not visit the edges <B, D>.

DR. R. K. SHANMUGAM COLLEGE OF ARTS AND SCIENCE, KALLAKURICHI 17


ANALYSIS & DESIGN OF ALGORITHMS (23PCSCC11) UNIT – 5
I M. Sc., Computer Science Semester: 1
The Hamiltonian cycle problem is also both, decision problem and an
optimization problem.
We can define the constraint for the Hamiltonian cycle problem as follows:
 In any path, vertex i and (i + 1) must be adjacent.
 1st and (n – 1)th vertex must be adjacent (nth of cycle is the initial
vertex itself).
 Vertex i must not appear in the first (i – 1) vertices of any path.
 With the adjacency matrix representation of the graph, the adjacency of
two vertices can be verified in constant time.
Algorithm for Hamiltonian Cycle Problem using Backtracking
HAMILTONIAN (i)
// Description : Solve Hamiltonian cycle problem using backtracking.
// Input : Undirected, connected graph G = <V, E> and initial vertex i
// Output : Hamiltonian cycle
if FEASIBLE(i) then
if (i == n - 1) then
Print V[0… n – 1]
else
j←2
while (j ≤ n)
do
V[i] ← j
HAMILTONIAN(i + 1)
j←j+1
end
end
end

DR. R. K. SHANMUGAM COLLEGE OF ARTS AND SCIENCE, KALLAKURICHI 18


ANALYSIS & DESIGN OF ALGORITHMS (23PCSCC11) UNIT – 5
I M. Sc., Computer Science Semester: 1
function
FEASIBLE(i)
flag ← 1
for j ← 1 to i – 1
do
if Adjacent(Vi, Vj) then
flag ← 0
end
end
if Adjacent (Vi, Vi-1) then
flag ← 1
else
flag ← 0
end
return flag
Complexity Analysis
Looking at the state space graph, in worst case, total number of nodes in tree would
be,
T(n) = 1 + (n – 1) + (n – 1)2 + (n – 1)3 + … + (n – 1)n – 1
= frac(n-1)n - 1n - 2
T(n) = O(nn).
Thus, the Hamiltonian cycle algorithm runs in exponential time.

DR. R. K. SHANMUGAM COLLEGE OF ARTS AND SCIENCE, KALLAKURICHI 19


ANALYSIS & DESIGN OF ALGORITHMS (23PCSCC11) UNIT – 5
I M. Sc., Computer Science Semester: 1
Example: Show that given graph has a Hamiltonian cycle

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.

DR. R. K. SHANMUGAM COLLEGE OF ARTS AND SCIENCE, KALLAKURICHI 20


ANALYSIS & DESIGN OF ALGORITHMS (23PCSCC11) UNIT – 5
I M. Sc., Computer Science Semester: 1

Backtrack and go to B. Now B does not have any more neighbors, so


backtrack and go to A. Explore the next neighbor of A i.e. C. By repeating the same
procedure, we get the state space trees as shown below. And path A – C – B – D – A
is detected as the Hamiltonian cycle of the input graph.

Another Hamiltonian cycle with A as a start vertex is A – D – B – C – A.

DR. R. K. SHANMUGAM COLLEGE OF ARTS AND SCIENCE, KALLAKURICHI 21


ANALYSIS & DESIGN OF ALGORITHMS (23PCSCC11) UNIT – 5
I M. Sc., Computer Science Semester: 1
BRANCH & BOUND
The branch and bound technique is used to solve optimization problems,
whereas the backtracking method is used to solve decision problems.
 Branch and bound build the state space tree, and find the optimal solution
quickly by pruning a few of the branches of the tree that do not satisfy the
bound.
 Branch and bound build the state space tree, one can find the optimal solution
quickly by pruning a few of the tree branches that do not satisfy the bound.
 Branch and bound can be useful where some other optimization techniques
like greedy or dynamic programming fail. Such algorithms are typically slower
than their counterparts. In the worst case, it may run in exponential time, but
careful selection of bounds and branches makes an algorithm run reasonably
faster.
Branch and bound technique is useful to solve problems like :
 Knapsack problem
 Travelling salesman problem
 Assignment problem
 Job scheduling problem
Backtracking Vs Branch & Bound
Backtracking Branch & Bound
Backtracking is normally used to solve Branch and bound is used to solve
decision problems optimization problems
Nodes in the state-space tree are explored Nodes in the tree may be explored in
in depth-first order in the backtracking depth-first or breadth-first order in branch
method and bound method
It realizes that it has made a bad choice It realizes that it already has a better
& undoes the last choice by backing up. optimal solution that the pre-solution
leads to so it abandons that pre-solution.

DR. R. K. SHANMUGAM COLLEGE OF ARTS AND SCIENCE, KALLAKURICHI 22


ANALYSIS & DESIGN OF ALGORITHMS (23PCSCC11) UNIT – 5
I M. Sc., Computer Science Semester: 1
The feasibility function is used in Branch-and-Bound involves a bounding
backtracking. function.
The next move from the current state can The next move is always towards a better
lead to a bad choice solution
On successful search of a solution in The entire state space tree is searched in
state-space tree, the search stops order to find the optimal solution
Backtracking is more efficient. Branch-and-Bound is less efficient.

Dynamic Programming Vs Branch & Bound


Dynamic Programming Branch & Bound
Constructs the solution in form of a table. Constructs the solution in form of a tree.
Solves all possible instances of problem Only solves promising instances from the
of size n. set of instances at any given point.
Does not require a bounding function. Needs to compute and apply a bounding
function at each node.
After constructing the table, it needs to The solution sequence is implicit, a leaf
be traced back to find the solution node of the tree is the final solution.
sequence.

TRAVELLING SALESMAN PROBLEM


Given the vertices, the problem here is that we have to travel each vertex
exactly once and reach back to the starting point. Consider the below graph:

DR. R. K. SHANMUGAM COLLEGE OF ARTS AND SCIENCE, KALLAKURICHI 23


ANALYSIS & DESIGN OF ALGORITHMS (23PCSCC11) UNIT – 5
I M. Sc., Computer Science Semester: 1

Step-01: Write the initial cost matrix and reduce it-

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.

DR. R. K. SHANMUGAM COLLEGE OF ARTS AND SCIENCE, KALLAKURICHI 24


ANALYSIS & DESIGN OF ALGORITHMS (23PCSCC11) UNIT – 5
I M. Sc., Computer Science Semester: 1
If the row does not contains an entry ‘0’, then-
Reduce that particular row.
Select the least value element from that row.
Subtract that element from each element of that row.
This will create an entry ‘0’ in that row, thus reducing that row.
Following this, we have-
Reduce the elements of row-1 by 4.
Reduce the elements of row-2 by 5.
Reduce the elements of row-3 by 6.
Reduce the elements of row-4 by 2.
Performing this, we obtain the following row-reduced matrix-

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.

DR. R. K. SHANMUGAM COLLEGE OF ARTS AND SCIENCE, KALLAKURICHI 25


ANALYSIS & DESIGN OF ALGORITHMS (23PCSCC11) UNIT – 5
I M. Sc., Computer Science Semester: 1
Following this, we have-
There is no need to reduce column-1.
There is no need to reduce column-2.
Reduce the elements of column-3 by 1.
There is no need to reduce column-4.
Performing this, we obtain the following column-reduced matrix-

Finally, the initial distance matrix is


completely reduced.
Now, we calculate the cost of node-1 by
adding all the reduction elements.
Cost(1) = Sum of all reduction elements
=4+5+6+2+1
= 18
Step-02:
We consider all other vertices one by one.
We select the best vertex where we can land upon to minimize the tour cost.
Choosing To Go To Vertex-B: Node-2 (Path
A → B)
From the reduced matrix of step-01, M[A,B]
=0
Set row-A and column-B to ∞
Set M[B,A] = ∞
Now, resulting cost matrix is-

Now,
 We reduce this matrix.
 Then, we find out the cost of node-02.

DR. R. K. SHANMUGAM COLLEGE OF ARTS AND SCIENCE, KALLAKURICHI 26


ANALYSIS & DESIGN OF ALGORITHMS (23PCSCC11) UNIT – 5
I M. Sc., Computer Science Semester: 1
Row Reduction-
We can not reduce row-1 as all its elements are
∞.
Reduce all the elements of row-2 by 13.
There is no need to reduce row-3.
There is no need to reduce row-4.
Performing this, we obtain the following row-
reduced matrix-
Column Reduction-
Reduce the elements of column-1 by 5.
We can not reduce column-2 as all its elements are ∞.
There is no need to reduce column-3.
There is no need to reduce column-4.
Performing this, we obtain the following column-
reduced matrix-

Finally, the matrix is completely reduced.


Now, we calculate the cost of node-2.
Cost(2) = Cost(1) + Sum of reduction elements +
M[A,B]
= 18 + (13 + 5) + 0
= 36
Choosing To Go To Vertex-C: Node-3 (Path A → C)
From the reduced matrix of step-01, M[A,C] = 7
Set row-A and column-C to ∞
Set M[C,A] = ∞
Now, resulting cost matrix is-

DR. R. K. SHANMUGAM COLLEGE OF ARTS AND SCIENCE, KALLAKURICHI 27


ANALYSIS & DESIGN OF ALGORITHMS (23PCSCC11) UNIT – 5
I M. Sc., Computer Science Semester: 1

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] = ∞

DR. R. K. SHANMUGAM COLLEGE OF ARTS AND SCIENCE, KALLAKURICHI 28


ANALYSIS & DESIGN OF ALGORITHMS (23PCSCC11) UNIT – 5
I M. Sc., Computer Science Semester: 1
Now, resulting cost matrix is-
Now,
We reduce this matrix.
Then, we find out the cost of node-04.
Row Reduction-
We cannot reduce row-1 as all its elements are ∞.
There is no need to reduce row-2.
Reduce all the elements of row-3 by 5.
There is no need to reduce row-4.
Performing this, we obtain the following row-reduced matrix-
Column Reduction-
There is no need to reduce column-1.
There is no need to reduce column-2.
There is no need to reduce column-3.
We cannot reduce column-4 as all its elements are
∞.
Thus, the matrix is already column-reduced.
Finally, the matrix is completely reduced.
Now, we calculate the cost of node-4.
Cost(4) = Cost(1) + Sum of reduction elements +
M[A,D]
= 18 + 5 + 3
= 26
Thus, we have-
Cost(2) = 36 (for Path A → B)
Cost(3) = 25 (for Path A → C)
Cost(4) = 26 (for Path A → D)
We choose the node with the lowest cost.
Since cost for node-3 is lowest, so we prefer to visit node-3.

DR. R. K. SHANMUGAM COLLEGE OF ARTS AND SCIENCE, KALLAKURICHI 29


ANALYSIS & DESIGN OF ALGORITHMS (23PCSCC11) UNIT – 5
I M. Sc., Computer Science Semester: 1
Thus, we choose node-3 i.e. path A → C.
Step-03:
We explore the vertices B and D from node-3.
We now start from the cost matrix at node-3 which is-
Choosing To Go To Vertex-B: Node-5 (Path A → C → B)
From the reduced matrix of step-02, M[C,B] =

Set row-C and column-B to ∞
Set M[B,A] = ∞
Now, resulting cost matrix is-
Now,
We reduce this matrix.
Then, we find out the cost of node-5.
Row Reduction-
We can not reduce row-1 as all its elements are
∞.
Reduce all the elements of row-2 by 13.
We can not reduce row-3 as all its elements are
∞.
Reduce all the elements of row-4 by 8.
Performing this, we obtain the following row-
reduced matrix-
Column Reduction-
There is no need to reduce column-1.
We can not reduce column-2 as all its elements
are ∞.
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.

DR. R. K. SHANMUGAM COLLEGE OF ARTS AND SCIENCE, KALLAKURICHI 30


ANALYSIS & DESIGN OF ALGORITHMS (23PCSCC11) UNIT – 5
I M. Sc., Computer Science Semester: 1
Finally, the matrix is completely reduced.
Now, we calculate the cost of node-5.
Cost(5)= cost(3) + Sum of reduction elements + M[C,B]
= 25 + (13 + 8) + ∞
=∞
Choosing To Go To Vertex-D: Node-6 (Path A → C → D)
From the reduced matrix of step-02, M[C,D] = ∞
Set row-C and column-D to ∞
Set M[D,A] = ∞
Now, resulting cost matrix is-
Now,
We reduce this matrix.
Then, we find out the cost of node-6.
Row Reduction-
We cannot reduce row-1 as all its elements
are ∞.
There is no need to reduce row-2.
We cannot reduce row-3 as all its elements
are ∞.
We cannot reduce row-4 as all its elements
are ∞.
Thus, the matrix is already row reduced.
Column Reduction-
There is no need to reduce column-1.
We cannot reduce column-2 as all its elements are ∞.
We cannot reduce column-3 as all its elements are ∞.
We cannot reduce column-4 as all its elements are ∞.
Thus, the matrix is already column reduced.
Finally, the matrix is completely reduced.

DR. R. K. SHANMUGAM COLLEGE OF ARTS AND SCIENCE, KALLAKURICHI 31


ANALYSIS & DESIGN OF ALGORITHMS (23PCSCC11) UNIT – 5
I M. Sc., Computer Science Semester: 1
Now, we calculate the cost of node-6.
Cost(6) = cost(3) + Sum of reduction
elements + M[C,D]
= 25 + 0 + 0
= 25
Thus, we have-
Cost(5) = ∞ (for Path A → C → B)
Cost(6) = 25 (for Path A → C → D)
We choose the node with the lowest cost.
Since cost for node-6 is lowest, so we prefer
to visit node-6.
Thus, we choose node-6 i.e. path C → D.
Step-04:
We explore vertex B from node-6.
We start with the cost matrix at node-6 which is-

Choosing To Go To Vertex-B: Node-7 (Path A →


C → D → B)
From the reduced matrix of step-03, M[D,B] = 0
Set row-D and column-B to ∞
Set M[B,A] = ∞
Now, resulting cost matrix is-
Now,
We reduce this matrix.
Then, we find out the cost of node-7.
Row Reduction-
We cannot reduce row-1 as all its elements are ∞.
We cannot reduce row-2 as all its elements are ∞.
We cannot reduce row-3 as all its elements are ∞.

DR. R. K. SHANMUGAM COLLEGE OF ARTS AND SCIENCE, KALLAKURICHI 32


ANALYSIS & DESIGN OF ALGORITHMS (23PCSCC11) UNIT – 5
I M. Sc., Computer Science Semester: 1
We cannot reduce row-4 as all its elements are ∞.
Column Reduction-
We cannot reduce column-1 as all its elements are ∞.
We cannot reduce column-2 as all its elements are ∞.
We cannot reduce column-3 as all its elements are ∞.
We cannot reduce column-4 as all its elements are ∞.
Thus, the matrix is already column reduced.
Finally, the matrix is completely reduced.
All the entries have become ∞.
Now, we calculate the cost of node-7.
Cost(7) = cost(6) + Sum of reduction elements + M[D,B]
= 25 + 0 + 0
= 25
Thus,
Optimal path is: A → C → D → B → A
Cost of Optimal path = 25 units

DR. R. K. SHANMUGAM COLLEGE OF ARTS AND SCIENCE, KALLAKURICHI 33


ANALYSIS & DESIGN OF ALGORITHMS (23PCSCC11) UNIT – 5
I M. Sc., Computer Science Semester: 1
Algorithm / Steps
 Cost of a tour T = (1/2) * ? (Sum of cost of two edges adjacent to u and in the
tour T)
 For every vertex u, if we consider two edges through it in T, and sum their
costs. The overall sum for all vertices would be twice of cost of tour T (We
have considered every edge twice.)
 (Sum of two tour edges adjacent to u) >= (sum of minimum weight two edges
adjacent to u)
 Cost of any tour >= 1/2) * ? (Sum of cost of two minimum weight edges
adjacent to u)

DR. R. K. SHANMUGAM COLLEGE OF ARTS AND SCIENCE, KALLAKURICHI 34


ANALYSIS & DESIGN OF ALGORITHMS (23PCSCC11) UNIT – 5
I M. Sc., Computer Science Semester: 1

DR. R. K. SHANMUGAM COLLEGE OF ARTS AND SCIENCE, KALLAKURICHI 35


DR. R. K. SHANMUGAM COLLEGE OF ARTS AND SCIENCE
INDILI, KALLAKURICHI 606 213

ANALYSIS & DESIGN OF ALGORITHMS


(23PCSCC11)
DEPARTMENT OF COMPUTER SCIENCE

CLASS: I M. Sc., COMPUTER SCIENCE


SEMESTER: 1

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy