You are on page 1of 6

Algorithm Analysis and Design

Assignment Amrith Krishna,


Submitted by: Roll No: 5, S6 CSE –A

Q 1. Write short notes on LC Search Control abstraction.


Ans: Control abstraction:

Listnode = record {
Listnode *next, *parent; float cost;
}

Algorithm LCSearch(t)
{
If *t is an answer node then output *t and return;
E := t;
Initialize the list of live nodes to be empty;
Repeat
{
For each child x of E do
{
If x is an answer node then output the path from x to t and return;
Add(x);
(x -> parent) := E;
}
If there are no more live nodes then
{
Write(“No answer Node”); return;
}
E := Least();
}until(false);
}

Let t be a state space tree and c() a cost function for the nodes in t. If x is a node in
it, then c(x) is the minimum cost of any answer node in the subtree with root x. Thus
c(t) is the cost of a minimum cost answer node in t.

The algorithm uses two functions Least() and Add(x) to delete and add a live node
from or to the node is deleted from the list of live nodesand returned. Algorithm
LCSearch outputs the path from answer node it finds to root node t.
Q 2. Explain Travelling Salesman Problem and solve it using
backtracking

Ans:

Traveling Salesman Problem (TSP) - Explanation


With backtracking, it is solved by mehods:
pruning: use best solution found so far to eliminate some choices .

if smallest cost from current city to unvisited city results would result in a tour
cost greater than best found so far. No need to consider any more tours from
this city

Let A be an array of n cities and let 1 l n be a parameter. Let lengthSoFar


be the length of the path A[1], A[2], . . . , A[l] and let minCost > 0 be another
parameter. We want to compute min(minCost, L), where L is the minimum
length
of a tour visiting all cities in A[1..n] under the condition that the tour starts by
visiting the cities from A[1..l] in the given order.

Algorithm TSP Backtrack (A, l, lengthSoFar , minCost)


1. n ← length[A] // number of elements in the array A
2. if l = n
3. then minCost ← min(minCost, lengthSoFar + distance[A[n], A[1]])
4. else for i ← l + 1 to n
5. do Swap A[l + 1] and A[i] // select A[i] as the next city
6. newLength ← lengthSoFar + distance[A[l], A[l + 1]]
7. if newLength minCost // this will never be a better solution
8. then skip // prune
9. else minCost ←
10. min(minCost, TSP Backtrack (A, l + 1, newLength, minCost))
11. Swap A[l + 1] and A[i] // undo the selection
12. return minCost

In line 7, we check if the beginning of the tour is already longer than the best
complete tour found so far. If this is the case, we will not look for tours that
begin in this way, and undo the selection just made. Another way to describe
it, is that we maintain that minCost is an upper bound on the length of the
best tour. This bound is improved every time we find a better tour. Before we
enter any branch of recursion, we first compute a lower bound on the length
of any solutions that could be found in that branch. This lower bound is
newLength. When the lower bound is higher than our current upper bound,
we skip the branch, otherwise we explore the branch to look for a better
upper bound. Backtracking algorithms with pruning are also called branch-
and-bound algorithms .
Solution to TSP using backtracking

The problem can be represented using a graph. Let G=(V,E) be a directed


graph with cost cij, cij >0 for all <i j>  E and cij = ∞ for all <j,t> E. |V| = n
and n>1. We know that tour of a graph includes all vertices in V and cost
of tour is the sum of the cost of all edges on the tour. Hence the traveling
salesman problem is to minimize the cost.

The solution space is given by S = {1,S,1 | S is a permutation of (2,3,…,n)}.


Hence |S| = (n-1)!. Size of S can be reduced by restricting S so that
(1,i1,…in-1, 1)  S
iff < ij, ij+1>  E, 0 ≤ j ≤ n-1 and i0 = in = 1. The state space tree is given by

To use LCBB we need cost function c(.) such that c’(r) ≤ c(r) ≤ u(r) for all
nodes r.

Since both these methods are not practical, choose c’( x) as


c’(x) = reduced cost matrix for G.

A row or column in a given matrix is said to be reduced iff it contains at


least one zero and all remaining entries as non-negative numbers. A matrix
is said to be reduced if all rows and all columns are reduced.

Consider a graph with 5 vertices. Given the cost matrix of the graph. Since
every tour in the graph includes one edge <i, j> with 1 ≤ I ≤ 5 and exactly
one edge <i, j> with 1≤j≤5. We know that subtracting a common value
from all the elements in a row or column will not change the minimum cost
tour, even though it chances the minimum distance traveled.

Now consider the cost matrix of the graph G

Reduced cost matrix of A is generated by


total value reduced from each row and column, which is 10+ 2+2+3+
4+1+3 = 25.

Considering the state space tree, we are starting from node1, which
means we started from vertex 1. Hence vertex 1 is the current E-node.
Now generate children of node 1. Hence it generates nodes 2, 3 , 4 or 5
depending on whether we are selecting vertex 2, 3, 4 or 5. Calculate the
reduced cost matrix for all the child nodes.
To calculate the reduced cost matrix of a child node C with parent P, if C
corresponds to vertex j and P corresponds to vertex i, then reduced cost
matrix is calcualted as per the following steps
 Change all entries in row i and column j to (Prevents the use of any
more vertex leaving i or entering j)
 Set A(j,1) to (Prevents use of edge <j,1>)
 Reduce all rows and columns in the resulting matrix except the one
with all .
Let the resultant matrix be B. If r is the total amount subtracted in step 3,
c’(C) = c’(P) + A(i,j) + r.
Since node 9 has the least cost, select node 9, means select vertex 5. Now
the path is 1,4,2,5. Since only one more vertex remaining select that as the
next vertex. Hence the path is 1,4,2,5,3,1.

You might also like