Traveling Salesman Problem using Branch And Bound
Last Updated :
30 Apr, 2023
Given a set of cities and distance between every pair of cities, the problem is to find the shortest possible tour that visits every city exactly once and returns to the starting point.
For example, consider the graph shown in figure on right side. A TSP tour in the graph is 0-1-3-2-0. The cost of the tour is 10+25+30+15 which is 80.
We have discussed following solutions
1) Naive and Dynamic Programming
2) Approximate solution using MST
Branch and Bound Solution
As seen in the previous articles, in Branch and Bound method, for current node in tree, we compute a bound on best possible solution that we can get if we down this node. If the bound on best possible solution itself is worse than current best (best computed so far), then we ignore the subtree rooted with the node.
Note that the cost through a node includes two costs.
1) Cost of reaching the node from the root (When we reach a node, we have this cost computed)
2) Cost of reaching an answer from current node to a leaf (We compute a bound on this cost to decide whether to ignore subtree with this node or not).
- In cases of a maximization problem, an upper bound tells us the maximum possible solution if we follow the given node. For example in 0/1 knapsack we used Greedy approach to find an upper bound.
- In cases of a minimization problem, a lower bound tells us the minimum possible solution if we follow the given node. For example, in Job Assignment Problem, we get a lower bound by assigning least cost job to a worker.
In branch and bound, the challenging part is figuring out a way to compute a bound on best possible solution. Below is an idea used to compute bounds for Travelling salesman problem.
Cost of any tour can be written as below.
Cost of a tour T = (1/2) * ? (Sum of cost of two edges
adjacent to u and in the
tour T)
where u ? V
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)
where u ? V
For example, consider the above shown graph. Below are minimum cost two edges adjacent to every node.
Node Least cost edges Total cost
0 (0, 1), (0, 2) 25
1 (0, 1), (1, 3) 35
2 (0, 2), (2, 3) 45
3 (0, 3), (1, 3) 45
Thus a lower bound on the cost of any tour =
1/2(25 + 35 + 45 + 45)
= 75
Refer this for one more example.
Now we have an idea about computation of lower bound. Let us see how to how to apply it state space search tree. We start enumerating all possible nodes (preferably in lexicographical order)
1. The Root Node: Without loss of generality, we assume we start at vertex “0” for which the lower bound has been calculated above.
Dealing with Level 2: The next level enumerates all possible vertices we can go to (keeping in mind that in any path a vertex has to occur only once) which are, 1, 2, 3… n (Note that the graph is complete). Consider we are calculating for vertex 1, Since we moved from 0 to 1, our tour has now included the edge 0-1. This allows us to make necessary changes in the lower bound of the root.
Lower Bound for vertex 1 =
Old lower bound - ((minimum edge cost of 0 +
minimum edge cost of 1) / 2)
+ (edge cost 0-1)
How does it work? To include edge 0-1, we add the edge cost of 0-1, and subtract an edge weight such that the lower bound remains as tight as possible which would be the sum of the minimum edges of 0 and 1 divided by 2. Clearly, the edge subtracted can’t be smaller than this.
Dealing with other levels: As we move on to the next level, we again enumerate all possible vertices. For the above case going further after 1, we check out for 2, 3, 4, …n.
Consider lower bound for 2 as we moved from 1 to 1, we include the edge 1-2 to the tour and alter the new lower bound for this node.
Lower bound(2) =
Old lower bound - ((second minimum edge cost of 1 +
minimum edge cost of 2)/2)
+ edge cost 1-2)
Note: The only change in the formula is that this time we have included second minimum edge cost for 1, because the minimum edge cost has already been subtracted in previous level.
C++
#include <bits/stdc++.h>
using namespace std;
const int N = 4;
int final_path[N+1];
bool visited[N];
int final_res = INT_MAX;
void copyToFinal( int curr_path[])
{
for ( int i=0; i<N; i++)
final_path[i] = curr_path[i];
final_path[N] = curr_path[0];
}
int firstMin( int adj[N][N], int i)
{
int min = INT_MAX;
for ( int k=0; k<N; k++)
if (adj[i][k]<min && i != k)
min = adj[i][k];
return min;
}
int secondMin( int adj[N][N], int i)
{
int first = INT_MAX, second = INT_MAX;
for ( int j=0; j<N; j++)
{
if (i == j)
continue ;
if (adj[i][j] <= first)
{
second = first;
first = adj[i][j];
}
else if (adj[i][j] <= second &&
adj[i][j] != first)
second = adj[i][j];
}
return second;
}
void TSPRec( int adj[N][N], int curr_bound, int curr_weight,
int level, int curr_path[])
{
if (level==N)
{
if (adj[curr_path[level-1]][curr_path[0]] != 0)
{
int curr_res = curr_weight +
adj[curr_path[level-1]][curr_path[0]];
if (curr_res < final_res)
{
copyToFinal(curr_path);
final_res = curr_res;
}
}
return ;
}
for ( int i=0; i<N; i++)
{
if (adj[curr_path[level-1]][i] != 0 &&
visited[i] == false )
{
int temp = curr_bound;
curr_weight += adj[curr_path[level-1]][i];
if (level==1)
curr_bound -= ((firstMin(adj, curr_path[level-1]) +
firstMin(adj, i))/2);
else
curr_bound -= ((secondMin(adj, curr_path[level-1]) +
firstMin(adj, i))/2);
if (curr_bound + curr_weight < final_res)
{
curr_path[level] = i;
visited[i] = true ;
TSPRec(adj, curr_bound, curr_weight, level+1,
curr_path);
}
curr_weight -= adj[curr_path[level-1]][i];
curr_bound = temp;
memset (visited, false , sizeof (visited));
for ( int j=0; j<=level-1; j++)
visited[curr_path[j]] = true ;
}
}
}
void TSP( int adj[N][N])
{
int curr_path[N+1];
int curr_bound = 0;
memset (curr_path, -1, sizeof (curr_path));
memset (visited, 0, sizeof (curr_path));
for ( int i=0; i<N; i++)
curr_bound += (firstMin(adj, i) +
secondMin(adj, i));
curr_bound = (curr_bound&1)? curr_bound/2 + 1 :
curr_bound/2;
visited[0] = true ;
curr_path[0] = 0;
TSPRec(adj, curr_bound, 0, 1, curr_path);
}
int main()
{
int adj[N][N] = { {0, 10, 15, 20},
{10, 0, 35, 25},
{15, 35, 0, 30},
{20, 25, 30, 0}
};
TSP(adj);
printf ( "Minimum cost : %d\n" , final_res);
printf ( "Path Taken : " );
for ( int i=0; i<=N; i++)
printf ( "%d " , final_path[i]);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int N = 4 ;
static int final_path[] = new int [N + 1 ];
static boolean visited[] = new boolean [N];
static int final_res = Integer.MAX_VALUE;
static void copyToFinal( int curr_path[])
{
for ( int i = 0 ; i < N; i++)
final_path[i] = curr_path[i];
final_path[N] = curr_path[ 0 ];
}
static int firstMin( int adj[][], int i)
{
int min = Integer.MAX_VALUE;
for ( int k = 0 ; k < N; k++)
if (adj[i][k] < min && i != k)
min = adj[i][k];
return min;
}
static int secondMin( int adj[][], int i)
{
int first = Integer.MAX_VALUE, second = Integer.MAX_VALUE;
for ( int j= 0 ; j<N; j++)
{
if (i == j)
continue ;
if (adj[i][j] <= first)
{
second = first;
first = adj[i][j];
}
else if (adj[i][j] <= second &&
adj[i][j] != first)
second = adj[i][j];
}
return second;
}
static void TSPRec( int adj[][], int curr_bound, int curr_weight,
int level, int curr_path[])
{
if (level == N)
{
if (adj[curr_path[level - 1 ]][curr_path[ 0 ]] != 0 )
{
int curr_res = curr_weight +
adj[curr_path[level- 1 ]][curr_path[ 0 ]];
if (curr_res < final_res)
{
copyToFinal(curr_path);
final_res = curr_res;
}
}
return ;
}
for ( int i = 0 ; i < N; i++)
{
if (adj[curr_path[level- 1 ]][i] != 0 &&
visited[i] == false )
{
int temp = curr_bound;
curr_weight += adj[curr_path[level - 1 ]][i];
if (level== 1 )
curr_bound -= ((firstMin(adj, curr_path[level - 1 ]) +
firstMin(adj, i))/ 2 );
else
curr_bound -= ((secondMin(adj, curr_path[level - 1 ]) +
firstMin(adj, i))/ 2 );
if (curr_bound + curr_weight < final_res)
{
curr_path[level] = i;
visited[i] = true ;
TSPRec(adj, curr_bound, curr_weight, level + 1 ,
curr_path);
}
curr_weight -= adj[curr_path[level- 1 ]][i];
curr_bound = temp;
Arrays.fill(visited, false );
for ( int j = 0 ; j <= level - 1 ; j++)
visited[curr_path[j]] = true ;
}
}
}
static void TSP( int adj[][])
{
int curr_path[] = new int [N + 1 ];
int curr_bound = 0 ;
Arrays.fill(curr_path, - 1 );
Arrays.fill(visited, false );
for ( int i = 0 ; i < N; i++)
curr_bound += (firstMin(adj, i) +
secondMin(adj, i));
curr_bound = (curr_bound== 1 )? curr_bound/ 2 + 1 :
curr_bound/ 2 ;
visited[ 0 ] = true ;
curr_path[ 0 ] = 0 ;
TSPRec(adj, curr_bound, 0 , 1 , curr_path);
}
public static void main(String[] args)
{
int adj[][] = {{ 0 , 10 , 15 , 20 },
{ 10 , 0 , 35 , 25 },
{ 15 , 35 , 0 , 30 },
{ 20 , 25 , 30 , 0 } };
TSP(adj);
System.out.printf( "Minimum cost : %d\n" , final_res);
System.out.printf( "Path Taken : " );
for ( int i = 0 ; i <= N; i++)
{
System.out.printf( "%d " , final_path[i]);
}
}
}
|
Python3
import math
maxsize = float ( 'inf' )
def copyToFinal(curr_path):
final_path[:N + 1 ] = curr_path[:]
final_path[N] = curr_path[ 0 ]
def firstMin(adj, i):
min = maxsize
for k in range (N):
if adj[i][k] < min and i ! = k:
min = adj[i][k]
return min
def secondMin(adj, i):
first, second = maxsize, maxsize
for j in range (N):
if i = = j:
continue
if adj[i][j] < = first:
second = first
first = adj[i][j]
elif (adj[i][j] < = second and
adj[i][j] ! = first):
second = adj[i][j]
return second
def TSPRec(adj, curr_bound, curr_weight,
level, curr_path, visited):
global final_res
if level = = N:
if adj[curr_path[level - 1 ]][curr_path[ 0 ]] ! = 0 :
curr_res = curr_weight + adj[curr_path[level - 1 ]]\
[curr_path[ 0 ]]
if curr_res < final_res:
copyToFinal(curr_path)
final_res = curr_res
return
for i in range (N):
if (adj[curr_path[level - 1 ]][i] ! = 0 and
visited[i] = = False ):
temp = curr_bound
curr_weight + = adj[curr_path[level - 1 ]][i]
if level = = 1 :
curr_bound - = ((firstMin(adj, curr_path[level - 1 ]) +
firstMin(adj, i)) / 2 )
else :
curr_bound - = ((secondMin(adj, curr_path[level - 1 ]) +
firstMin(adj, i)) / 2 )
if curr_bound + curr_weight < final_res:
curr_path[level] = i
visited[i] = True
TSPRec(adj, curr_bound, curr_weight,
level + 1 , curr_path, visited)
curr_weight - = adj[curr_path[level - 1 ]][i]
curr_bound = temp
visited = [ False ] * len (visited)
for j in range (level):
if curr_path[j] ! = - 1 :
visited[curr_path[j]] = True
def TSP(adj):
curr_bound = 0
curr_path = [ - 1 ] * (N + 1 )
visited = [ False ] * N
for i in range (N):
curr_bound + = (firstMin(adj, i) +
secondMin(adj, i))
curr_bound = math.ceil(curr_bound / 2 )
visited[ 0 ] = True
curr_path[ 0 ] = 0
TSPRec(adj, curr_bound, 0 , 1 , curr_path, visited)
adj = [[ 0 , 10 , 15 , 20 ],
[ 10 , 0 , 35 , 25 ],
[ 15 , 35 , 0 , 30 ],
[ 20 , 25 , 30 , 0 ]]
N = 4
final_path = [ None ] * (N + 1 )
visited = [ False ] * N
final_res = maxsize
TSP(adj)
print ( "Minimum cost :" , final_res)
print ( "Path Taken : " , end = ' ' )
for i in range (N + 1 ):
print (final_path[i], end = ' ' )
|
C#
using System;
public class GFG {
static int N = 4;
static int [] final_path = new int [N + 1];
static bool [] visited = new bool [N];
static int final_res = Int32.MaxValue;
static void copyToFinal( int [] curr_path)
{
for ( int i = 0; i < N; i++)
final_path[i] = curr_path[i];
final_path[N] = curr_path[0];
}
static int firstMin( int [, ] adj, int i)
{
int min = Int32.MaxValue;
for ( int k = 0; k < N; k++)
if (adj[i, k] < min && i != k)
min = adj[i, k];
return min;
}
static int secondMin( int [, ] adj, int i)
{
int first = Int32.MaxValue, second = Int32.MaxValue;
for ( int j = 0; j < N; j++) {
if (i == j)
continue ;
if (adj[i, j] <= first) {
second = first;
first = adj[i, j];
}
else if (adj[i, j] <= second
&& adj[i, j] != first)
second = adj[i, j];
}
return second;
}
static void TSPRec( int [, ] adj, int curr_bound,
int curr_weight, int level,
int [] curr_path)
{
if (level == N) {
if (adj[curr_path[level - 1], curr_path[0]]
!= 0) {
int curr_res = curr_weight
+ adj[curr_path[level - 1],
curr_path[0]];
if (curr_res < final_res) {
copyToFinal(curr_path);
final_res = curr_res;
}
}
return ;
}
for ( int i = 0; i < N; i++) {
if (adj[curr_path[level - 1], i] != 0
&& visited[i] == false ) {
int temp = curr_bound;
curr_weight += adj[curr_path[level - 1], i];
if (level == 1)
curr_bound
-= ((firstMin(adj,
curr_path[level - 1])
+ firstMin(adj, i))
/ 2);
else
curr_bound
-= ((secondMin(adj,
curr_path[level - 1])
+ firstMin(adj, i))
/ 2);
if (curr_bound + curr_weight < final_res) {
curr_path[level] = i;
visited[i] = true ;
TSPRec(adj, curr_bound, curr_weight,
level + 1, curr_path);
}
curr_weight -= adj[curr_path[level - 1], i];
curr_bound = temp;
Array.Fill(visited, false );
for ( int j = 0; j <= level - 1; j++)
visited[curr_path[j]] = true ;
}
}
}
static void TSP( int [, ] adj)
{
int [] curr_path = new int [N + 1];
int curr_bound = 0;
Array.Fill(curr_path, -1);
Array.Fill(visited, false );
for ( int i = 0; i < N; i++)
curr_bound
+= (firstMin(adj, i) + secondMin(adj, i));
curr_bound = (curr_bound == 1) ? curr_bound / 2 + 1
: curr_bound / 2;
visited[0] = true ;
curr_path[0] = 0;
TSPRec(adj, curr_bound, 0, 1, curr_path);
}
static public void Main()
{
int [, ] adj = { { 0, 10, 15, 20 },
{ 10, 0, 35, 25 },
{ 15, 35, 0, 30 },
{ 20, 25, 30, 0 } };
TSP(adj);
Console.WriteLine( "Minimum cost : " + final_res);
Console.Write( "Path Taken : " );
for ( int i = 0; i <= N; i++) {
Console.Write(final_path[i] + " " );
}
}
}
|
Javascript
const N = 4;
let final_path = Array (N + 1).fill (-1);
let visited = Array (N).fill ( false );
let final_res = Number.MAX_SAFE_INTEGER;
function copyToFinal (curr_path){
for (let i = 0; i < N; i++){
final_path[i] = curr_path[i];
}
final_path[N] = curr_path[0];
}
function firstMin (adj, i){
let min = Number.MAX_SAFE_INTEGER;
for (let k = 0; k < N; k++){
if (adj[i][k] < min && i !== k){
min = adj[i][k];
}
}
return min;
}
function secondMin (adj, i){
let first = Number.MAX_SAFE_INTEGER;
let second = Number.MAX_SAFE_INTEGER;
for (let j = 0; j < N; j++){
if (i == j){
continue ;
}
if (adj[i][j] <= first){
second = first;
first = adj[i][j];
}
else if (adj[i][j] <= second && adj[i][j] !== first){
second = adj[i][j];
}
}
return second;
}
function TSPRec (adj, curr_bound, curr_weight, level, curr_path)
{
if (level == N)
{
if (adj[curr_path[level - 1]][curr_path[0]] !== 0)
{
let curr_res =
curr_weight + adj[curr_path[level - 1]][curr_path[0]];
if (curr_res < final_res)
{
copyToFinal (curr_path);
final_res = curr_res;
}
}
return ;
}
for (let i = 0; i < N; i++){
if (adj[curr_path[level - 1]][i] !== 0 && !visited[i]){
let temp = curr_bound;
curr_weight += adj[curr_path[level - 1]][i];
if (level == 1){
curr_bound -= (firstMin (adj, curr_path[level - 1]) + firstMin (adj, i)) / 2;
}
else
{
curr_bound -= (secondMin (adj, curr_path[level - 1]) + firstMin (adj, i)) / 2;
}
if (curr_bound + curr_weight < final_res){
curr_path[level] = i;
visited[i] = true ;
TSPRec (adj, curr_bound, curr_weight, level + 1, curr_path);
}
curr_weight -= adj[curr_path[level - 1]][i];
curr_bound = temp;
visited.fill ( false )
for ( var j = 0; j <= level - 1; j++)
visited[curr_path[j]] = true ;
}
}
}
function TSP (adj)
{
let curr_path = Array (N + 1).fill (-1);
let curr_bound = 0;
visited.fill ( false );
for (let i = 0; i < N; i++){
curr_bound += firstMin (adj, i) + secondMin (adj, i);
}
curr_bound = curr_bound == 1 ? (curr_bound / 2) + 1 : (curr_bound / 2);
visited[0] = true ;
curr_path[0] = 0;
TSPRec (adj, curr_bound, 0, 1, curr_path);
}
let adj =[[0, 10, 15, 20],
[10, 0, 35, 25],
[15, 35, 0, 30],
[20, 25, 30, 0]];
TSP (adj);
console.log (`Minimum cost:${final_res}`);
console.log (`Path Taken:${final_path.join ( " " )}`);
|
Output :
Minimum cost : 80
Path Taken : 0 1 3 2 0
The rounding is being done in this line of code:
if (level==1)
curr_bound -= ((firstMin(adj, curr_path[level-1]) +
firstMin(adj, i))/2);
else
curr_bound -= ((secondMin(adj, curr_path[level-1]) +
firstMin(adj, i))/2);
In the Branch and Bound TSP algorithm, we compute a lower bound on the total cost of the optimal solution by adding up the minimum edge costs for each vertex, and then dividing by two. However, this lower bound may not be an integer. To get an integer lower bound, we can use rounding.
In the above code, the curr_bound variable holds the current lower bound on the total cost of the optimal solution. When we visit a new vertex at level level, we compute a new lower bound new_bound by taking the sum of the minimum edge costs for the new vertex and its two closest neighbors. We then update the curr_bound variable by rounding new_bound to the nearest integer.
If level is 1, we round down to the nearest integer. This is because we have only visited one vertex so far, and we want to be conservative in our estimate of the total cost of the optimal solution. If level is greater than 1, we use a more aggressive rounding strategy that takes into account the fact that we have already visited some vertices and can therefore make a more accurate estimate of the total cost of the optimal solution.
Time Complexity: The worst case complexity of Branch and Bound remains same as that of the Brute Force clearly because in worst case, we may never get a chance to prune a node. Whereas, in practice it performs very well depending on the different instance of the TSP. The complexity also depends on the choice of the bounding function as they are the ones deciding how many nodes to be pruned.
References:
http://lcm.csa.iisc.ernet.in/dsa/node187.html
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...