Traveling Salesman Problem using Branch And Bound | C ++ program to solve Traveling Salesman Problem using Branch and Bound . ; final_path [ ] stores the final solution ie , the path of the salesman . ; visited [ ] keeps track of the already visited nodes in a particular path ; Stores the final minimum weight of shortest tour . ; Function to copy temporary solution to the final solution ; Function to find the minimum edge cost having an end at the vertex i ; function to find the second minimum edge cost having an end at the vertex i ; function that takes as arguments : curr_bound -> lower bound of the root node curr_weight -> stores the weight of the path so far level -> current level while moving in the search space tree curr_path [ ] -> where the solution is being stored which would later be copied to final_path [ ] ; base case is when we have reached level N which means we have covered all the nodes once ; check if there is an edge from last vertex in path back to the first vertex ; curr_res has the total weight of the solution we got ; Update final result and final path if current result is better . ; for any other level iterate for all vertices to build the search space tree recursively ; Consider next vertex if it is not same ( diagonal entry in adjacency matrix and not visited already ) ; different computation of curr_bound for level 2 from the other levels ; curr_bound + curr_weight is the actual lower bound for the node that we have arrived on If current lower bound < final_res , we need to explore the node further ; call TSPRec for the next level ; Else we have to prune the node by resetting all changes to curr_weight and curr_bound ; Also reset the visited array ; This function sets up final_path [ ] ; Calculate initial lower bound for the root node using the formula 1 / 2 * ( sum of first min + second min ) for all edges . Also initialize the curr_path and visited array ; Compute initial bound ; Rounding off the lower bound to an integer ; We start at vertex 1 so the first vertex in curr_path [ ] is 0 ; Call to TSPRec for curr_weight equal to 0 and level 1 ; Driver code ; Adjacency matrix for the given graph
#include <bits/stdc++.h> NEW_LINE 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 STRNEWLINE " , final_res ) ; printf ( " Path ▁ Taken ▁ : ▁ " ) ; for ( int i = 0 ; i <= N ; i ++ ) printf ( " % d ▁ " , final_path [ i ] ) ; return 0 ; }