Algorith Analysis - TSP With Backtracking
Algorith Analysis - TSP With Backtracking
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:
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
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
To use LCBB we need cost function c(.) such that c’(r) ≤ c(r) ≤ u(r) for all
nodes r.
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.
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.