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.

Euler1

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 Traveling salesman problem.

[ad type=”banner”]

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.

[ad type=”banner”]

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++ programming:

[pastacode lang=”cpp” manual=”%2F%2F%20C%2B%2B%20program%20to%20solve%20Traveling%20Salesman%20Problem%0A%2F%2F%20using%20Branch%20and%20Bound.%0A%23include%20%3Cbits%2Fstdc%2B%2B.h%3E%0Ausing%20namespace%20std%3B%0Aconst%20int%20N%20%3D%204%3B%0A%20%0A%2F%2F%20final_path%5B%5D%20stores%20the%20final%20solution%20ie%2C%20the%0A%2F%2F%20path%20of%20the%20salesman.%0Aint%20final_path%5BN%2B1%5D%3B%0A%20%0A%2F%2F%20visited%5B%5D%20keeps%20track%20of%20the%20already%20visited%20nodes%0A%2F%2F%20in%20a%20particular%20path%0Abool%20visited%5BN%5D%3B%0A%20%0A%2F%2F%20Stores%20the%20final%20minimum%20weight%20of%20shortest%20tour.%0Aint%20final_res%20%3D%20INT_MAX%3B%0A%20%0A%2F%2F%20Function%20to%20copy%20temporary%20solution%20to%0A%2F%2F%20the%20final%20solution%0Avoid%20copyToFinal(int%20curr_path%5B%5D)%0A%7B%0A%20%20%20%20for%20(int%20i%3D0%3B%20i%3CN%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20final_path%5Bi%5D%20%3D%20curr_path%5Bi%5D%3B%0A%20%20%20%20final_path%5BN%5D%20%3D%20curr_path%5B0%5D%3B%0A%7D%0A%20%0A%2F%2F%20Function%20to%20find%20the%20minimum%20edge%20cost%0A%2F%2F%20having%20an%20end%20at%20the%20vertex%20i%0Aint%20firstMin(int%20adj%5BN%5D%5BN%5D%2C%20int%20i)%0A%7B%0A%20%20%20%20int%20min%20%3D%20INT_MAX%3B%0A%20%20%20%20for%20(int%20k%3D0%3B%20k%3CN%3B%20k%2B%2B)%0A%20%20%20%20%20%20%20%20if%20(adj%5Bi%5D%5Bk%5D%3Cmin%20%26%26%20i%20!%3D%20k)%0A%20%20%20%20%20%20%20%20%20%20%20%20min%20%3D%20adj%5Bi%5D%5Bk%5D%3B%0A%20%20%20%20return%20min%3B%0A%7D%0A%20%0A%2F%2F%20function%20to%20find%20the%20second%20minimum%20edge%20cost%0A%2F%2F%20having%20an%20end%20at%20the%20vertex%20i%0Aint%20secondMin(int%20adj%5BN%5D%5BN%5D%2C%20int%20i)%0A%7B%0A%20%20%20%20int%20first%20%3D%20INT_MAX%2C%20second%20%3D%20INT_MAX%3B%0A%20%20%20%20for%20(int%20j%3D0%3B%20j%3CN%3B%20j%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20if%20(i%20%3D%3D%20j)%0A%20%20%20%20%20%20%20%20%20%20%20%20continue%3B%0A%20%0A%20%20%20%20%20%20%20%20if%20(adj%5Bi%5D%5Bj%5D%20%3C%3D%20first)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20second%20%3D%20first%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20first%20%3D%20adj%5Bi%5D%5Bj%5D%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20else%20if%20(adj%5Bi%5D%5Bj%5D%20%3C%3D%20second%20%26%26%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20adj%5Bi%5D%5Bj%5D%20!%3D%20first)%0A%20%20%20%20%20%20%20%20%20%20%20%20second%20%3D%20adj%5Bi%5D%5Bj%5D%3B%0A%20%20%20%20%7D%0A%20%20%20%20return%20second%3B%0A%7D%0A%20%0A%2F%2F%20function%20that%20takes%20as%20arguments%3A%0A%2F%2F%20curr_bound%20-%3E%20lower%20bound%20of%20the%20root%20node%0A%2F%2F%20curr_weight-%3E%20stores%20the%20weight%20of%20the%20path%20so%20far%0A%2F%2F%20level-%3E%20current%20level%20while%20moving%20in%20the%20search%0A%2F%2F%20%20%20%20%20%20%20%20%20space%20tree%0A%2F%2F%20curr_path%5B%5D%20-%3E%20where%20the%20solution%20is%20being%20stored%20which%0A%2F%2F%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20would%20later%20be%20copied%20to%20final_path%5B%5D%0Avoid%20TSPRec(int%20adj%5BN%5D%5BN%5D%2C%20int%20curr_bound%2C%20int%20curr_weight%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20int%20level%2C%20int%20curr_path%5B%5D)%0A%7B%0A%20%20%20%20%2F%2F%20base%20case%20is%20when%20we%20have%20reached%20level%20N%20which%0A%20%20%20%20%2F%2F%20means%20we%20have%20covered%20all%20the%20nodes%20once%0A%20%20%20%20if%20(level%3D%3DN)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20check%20if%20there%20is%20an%20edge%20from%20last%20vertex%20in%0A%20%20%20%20%20%20%20%20%2F%2F%20path%20back%20to%20the%20first%20vertex%0A%20%20%20%20%20%20%20%20if%20(adj%5Bcurr_path%5Blevel-1%5D%5D%5Bcurr_path%5B0%5D%5D%20!%3D%200)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20curr_res%20has%20the%20total%20weight%20of%20the%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20solution%20we%20got%0A%20%20%20%20%20%20%20%20%20%20%20%20int%20curr_res%20%3D%20curr_weight%20%2B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20adj%5Bcurr_path%5Blevel-1%5D%5D%5Bcurr_path%5B0%5D%5D%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Update%20final%20result%20and%20final%20path%20if%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20current%20result%20is%20better.%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(curr_res%20%3C%20final_res)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20copyToFinal(curr_path)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20final_res%20%3D%20curr_res%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20return%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20for%20any%20other%20level%20iterate%20for%20all%20vertices%20to%0A%20%20%20%20%2F%2F%20build%20the%20search%20space%20tree%20recursively%0A%20%20%20%20for%20(int%20i%3D0%3B%20i%3CN%3B%20i%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20Consider%20next%20vertex%20if%20it%20is%20not%20same%20(diagonal%0A%20%20%20%20%20%20%20%20%2F%2F%20entry%20in%20adjacency%20matrix%20and%20not%20visited%0A%20%20%20%20%20%20%20%20%2F%2F%20already)%0A%20%20%20%20%20%20%20%20if%20(adj%5Bcurr_path%5Blevel-1%5D%5D%5Bi%5D%20!%3D%200%20%26%26%0A%20%20%20%20%20%20%20%20%20%20%20%20visited%5Bi%5D%20%3D%3D%20false)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20int%20temp%20%3D%20curr_bound%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20curr_weight%20%2B%3D%20adj%5Bcurr_path%5Blevel-1%5D%5D%5Bi%5D%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20different%20computation%20of%20curr_bound%20for%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20level%202%20from%20the%20other%20levels%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(level%3D%3D1)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20curr_bound%20-%3D%20((firstMin(adj%2C%20curr_path%5Blevel-1%5D)%20%2B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20firstMin(adj%2C%20i))%2F2)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20curr_bound%20-%3D%20((secondMin(adj%2C%20curr_path%5Blevel-1%5D)%20%2B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20firstMin(adj%2C%20i))%2F2)%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20curr_bound%20%2B%20curr_weight%20is%20the%20actual%20lower%20bound%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20for%20the%20node%20that%20we%20have%20arrived%20on%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20If%20current%20lower%20bound%20%3C%20final_res%2C%20we%20need%20to%20explore%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20the%20node%20further%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(curr_bound%20%2B%20curr_weight%20%3C%20final_res)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20curr_path%5Blevel%5D%20%3D%20i%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20visited%5Bi%5D%20%3D%20true%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20call%20TSPRec%20for%20the%20next%20level%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20TSPRec(adj%2C%20curr_bound%2C%20curr_weight%2C%20level%2B1%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20curr_path)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Else%20we%20have%20to%20prune%20the%20node%20by%20resetting%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20all%20changes%20to%20curr_weight%20and%20curr_bound%0A%20%20%20%20%20%20%20%20%20%20%20%20curr_weight%20-%3D%20adj%5Bcurr_path%5Blevel-1%5D%5D%5Bi%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20curr_bound%20%3D%20temp%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Also%20reset%20the%20visited%20array%0A%20%20%20%20%20%20%20%20%20%20%20%20memset(visited%2C%20false%2C%20sizeof(visited))%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20(int%20j%3D0%3B%20j%3C%3Dlevel-1%3B%20j%2B%2B)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20visited%5Bcurr_path%5Bj%5D%5D%20%3D%20true%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%0A%20%0A%2F%2F%20This%20function%20sets%20up%20final_path%5B%5D%20%0Avoid%20TSP(int%20adj%5BN%5D%5BN%5D)%0A%7B%0A%20%20%20%20int%20curr_path%5BN%2B1%5D%3B%0A%20%0A%20%20%20%20%2F%2F%20Calculate%20initial%20lower%20bound%20for%20the%20root%20node%0A%20%20%20%20%2F%2F%20using%20the%20formula%201%2F2%20*%20(sum%20of%20first%20min%20%2B%0A%20%20%20%20%2F%2F%20second%20min)%20for%20all%20edges.%0A%20%20%20%20%2F%2F%20Also%20initialize%20the%20curr_path%20and%20visited%20array%0A%20%20%20%20int%20curr_bound%20%3D%200%3B%0A%20%20%20%20memset(curr_path%2C%20-1%2C%20sizeof(curr_path))%3B%0A%20%20%20%20memset(visited%2C%200%2C%20sizeof(curr_path))%3B%0A%20%0A%20%20%20%20%2F%2F%20Compute%20initial%20bound%0A%20%20%20%20for%20(int%20i%3D0%3B%20i%3CN%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20curr_bound%20%2B%3D%20(firstMin(adj%2C%20i)%20%2B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20secondMin(adj%2C%20i))%3B%0A%20%0A%20%20%20%20%2F%2F%20Rounding%20off%20the%20lower%20bound%20to%20an%20integer%0A%20%20%20%20curr_bound%20%3D%20(curr_bound%261)%3F%20curr_bound%2F2%20%2B%201%20%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20curr_bound%2F2%3B%0A%20%0A%20%20%20%20%2F%2F%20We%20start%20at%20vertex%201%20so%20the%20first%20vertex%0A%20%20%20%20%2F%2F%20in%20curr_path%5B%5D%20is%200%0A%20%20%20%20visited%5B0%5D%20%3D%20true%3B%0A%20%20%20%20curr_path%5B0%5D%20%3D%200%3B%0A%20%0A%20%20%20%20%2F%2F%20Call%20to%20TSPRec%20for%20curr_weight%20equal%20to%0A%20%20%20%20%2F%2F%200%20and%20level%201%0A%20%20%20%20TSPRec(adj%2C%20curr_bound%2C%200%2C%201%2C%20curr_path)%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20code%0Aint%20main()%0A%7B%0A%20%20%20%20%2F%2FAdjacency%20matrix%20for%20the%20given%20graph%0A%20%20%20%20int%20adj%5BN%5D%5BN%5D%20%3D%20%7B%20%7B0%2C%2010%2C%2015%2C%2020%7D%2C%0A%20%20%20%20%20%20%20%20%7B10%2C%200%2C%2035%2C%2025%7D%2C%0A%20%20%20%20%20%20%20%20%7B15%2C%2035%2C%200%2C%2030%7D%2C%0A%20%20%20%20%20%20%20%20%7B20%2C%2025%2C%2030%2C%200%7D%0A%20%20%20%20%7D%3B%0A%20%0A%20%20%20%20TSP(adj)%3B%0A%20%0A%20%20%20%20printf(%22Minimum%20cost%20%3A%20%25d%5Cn%22%2C%20final_res)%3B%0A%20%20%20%20printf(%22Path%20Taken%20%3A%20%22)%3B%0A%20%20%20%20for%20(int%20i%3D0%3B%20i%3C%3DN%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20printf(%22%25d%20%22%2C%20final_path%5Bi%5D)%3B%0A%20%0A%20%20%20%20return%200%3B%0A%7D%0A” message=”” highlight=”” provider=”manual”/]

 

Output :

Minimum cost : 80
Path Taken : 0 1 3 2 0 

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.

[ad type=”banner”]