The previous chapter introduced the famed Travelling Salesperson Problem (TSP). A brute-force solution was presented. Like all exact solutions to this problem, it is computationally intractable. A third-party package was presented along with code for graphically displaying a TSP tour.
This chapter presents another exact solution to TSP using a powerful technique, branch and bound. It too is computationally intractable.
In the next section, we introduce the technique of branch and bound and show how it can be applied to TSP.
18.1 Branch and Bound for TSP
Much of this chapter is based on a paper written by this author and published in the Journal of Object Technology in 2003. The paper is “Branch and Bound Implementation for the Traveling Salesperson Problem” (Richard Wiener, Journal of Object Technology, Vol 2. No. 3, May–June 2003).
We are given a graph that contains distances connecting all cities (each city is connected to every other city with an edge representing the distance between the two cities). The nodes of the graph are the cities, numbered 0, …, n - 1. A tour is a sequence of cities that starts at city 0, visits each of the other cities exactly once, and returns to the starting city 0.
In any tour, the value of an edge when leaving a city must be equal or greater than the value of the shortest edge leaving the city.
This forms the basis for a branch-and-bound solution to TSP.
An Example
Consider a TSP with the following distance matrix (cost matrix):
0 14 4 11 20
14 0 7 8 7
4 7 0 7 16
11 8 7 0 2
20 7 16 2 0
We build a solution tree as follows:
At level 0, the root node represents a partial tour [0].
At level 1, nodes representing the partial tours [0, 1], [0, 2], [0, 3], …, [0, n - 1] are generated.
At level 2, nodes representing the partial tours [0, 1, 2], [0, 1, 3], [0, 1, 4], … are generated.
This pattern continues until at the lowest level, we have every permutation for all tours.
Computation of Lower Bound
For the root node of the preceding graph, with partial tour [0], the lower bound on the costs of leaving the five vertices is
City 0: minimum (14, 4, 11, 20) = 4.
City 1: minimum (14, 7, 8, 7) = 7.
City 2: minimum (4, 7, 7, 16) = 4.
City 3: minimum (11, 8, 7, 2) = 2.
City 4: minimum (20, 7, 16, 2) = 2.
Therefore, the lower bound for the TSP solution based on the partial tour [0] is the sum of these values, which is 19.
Let us compute the lower bound for a partial tour [0, 2, 3].
City 0: 4 (since the tour contains 0 -> 2)
City 1: 7 (cannot touch node already on tour)
City 2: 7 (since the tour contains 2 -> 3)
City 3: 11 (since the tour contains 3 -> 0)
City 4: 7 (cannot touch node already on tour)
Therefore, the lower bound for the partial tour [0, 2, 3] is the sum, which equals 36.
The computational cost of computing the lower bound for a partial tour is low.
At any level in the tree containing partial tours, the nodes can be ranked by their computed lower bounds. We can use a priority queue to hold the tree structure.
Branch-and-Bound Algorithm
Set an initial value for the best tour cost.
Initialize a priority queue (PQ).
Generate the first node with partial tour [0] and compute its lower bound.
Insert this node into PQ.
While the PQ is not empty, remove the first node from the PQ and assign it to parent.
If its lower bound < best tour cost, set its level to the level of parent node + 1.
If this level is N – 1, where N is the number of cities, add starting city 0 to the end of the partial tour and compute the cost of the full tour.
If this cost of full tour < best tour cost, update the best tour cost and save the best tour.
If the level of the parent node + 1 is not equal to N – 1,
For all i such that 1 <= i < N and i is not in the partial tour of the parent,
Copy the partial tour from parent to new node and add i to the end of this partial tour.
Compute the lower bound for this new node.
If this lower bound is less than the best tour cost, insert this new node into the priority queue; otherwise, prune this node.
The Priority Queue
Before a node is inserted into the PQ, it is screened to determine whether its lower bound is less than the currently known best tour. This helps to keep the number of nodes in the PQ to a manageable level.
What priority rules must the queue enforce?
Nodes at a deeper level (higher level number) have priority over nodes at a shallower level in the PQ. This assures that the tree grows downward and that leaf nodes representing complete tours are generated as quickly as possible. In comparing two nodes at the same level, priority is given to the node with the smallest lower bound. In the event of a tie (two nodes with equal lower bound), the sum of the cities in the partial tour is computed. The node with the smaller sum is given a higher priority than the node with the larger sum (a tie cannot occur). The rules just stated disallow two distinct nodes from having an equal priority.
We walk through a portion of the five-city example presented earlier to see how the priority queue is built.
A Walk-Through Part of the Five-City Example Presented Earlier
The full tour [0, 2, 1, 3, 4] is the first to be generated. Since its cost is less than the best so far of 50, we assign the best tour to be [0, 2, 1, 3, 4] and its cost 41.
The front of the PQ contains the node [0, 2, 1, 4] since it is at the deepest level. The algorithm backtracks to that node and then generates another full tour [0, 2, 1, 4, 3] also with cost 41. This allows some of the nodes to be pruned when they are taken off the PQ since their lower bounds are greater than 41.
You may wish to continue this process for this example.
In the next section, we look at the implementation of this algorithm.
18.2 Branch-and-Bound Implementation
The function works exactly as specified in the outline of the algorithm presented in Section 18.1.
Implementation of Priority Queue
Nodes at a deeper level (higher level number) have priority over nodes at a shallower level in the PQ.
Priority is given to the node with the smallest lower bound if the nodes are at the same level.
If two nodes at the same level have the same lower bound, we add up the cities in the node’s tour, and the node with the higher sum has a higher priority.
The priority queue (PQ) holds entities of type Node. Each time we insert a new node into the queue, we sort the queue to ensure that the node with the highest priority is at the front of the queue.
To enable the sorting of nodes in the PQ, we need to implement the interface to the Sort method from package sort.
This is accomplished with the functions Len, Swap, and Less as given earlier. Each of the three priority rules is implemented in the function Less.
The Insert function sorts the items (slice of Node) after appending the node being inserted.
Generating Branch-and-Bound Solution
Branch-and-bound solution to TSP
Data for main
The graph is constructed from data taken from a Rand McNally Atlas of 33 US cities with distances between them specified.
The details in main are omitted here because of limited space. Please download the entire listing from the publisher’s website.
The known solution to this problem is an optimum tour of 10,861 miles.
Results
After running the branch-and-bound program for 18 hours and 42 minutes and generating 4.6 billion nodes at about 70,000 nodes per second, the best tour generated so far is
[0 13 1 2 3 5 4 6 7 8 9 10 11 17 18 19 27 29 30 28 31 32 22 21 23 24 25 26 20 14 15 16 12]
This tour is 11,553 miles, which is an error of about 6 percent. We must remind ourselves that the total number of nodes in the full tree representing this problem is 263,130,836,933,693,530,167,218,012,160,000,000 nodes.
At an average rate of 71,874 nodes per second, it would take about 1.39 × 1030 seconds or about 4.41 × 1022 years to generate all the leaf nodes in this tree.
18.3 Summary
We presented a branch-and-bound algorithm that provides an exact solution to the TSP. The problem is that this is a computationally intractable problem. If allowed to run long enough, it will find an exact solution to the problem.
A priority queue is used to hold a sequence of nodes, where each node contains a tour (may be a partial tour), a lower bound, and a level. The nodes are sorted in the queue by level, lower bound, and, in the case of a tie, sum of city values in the tour.
A 33-city problem is attempted. After two hours of computation, the best tour obtained is about 6 percent higher than the known optimum tour. This best tour so far remains the same after 16 hours.
This sets the stage for the next two chapters in which we present heuristic solutions to the TSP. These heuristic solutions execute in a matter of seconds and produce solutions that are either exact or have small error.
The next chapter presents a heuristic algorithm, simulated annealing.