Notes on TSP Algorithms 5/04

Ways to find optimal Hamilton Circuits in Weighted Graphs
The Brute Force Algorithm: what we tried Thursday
1. Make a list of ALL the possible Hamilton circuits of the graph.
2. For each Hamilton circuit, calculate its total weight (ie. add up all the weights of the
edges in the circuit).
3. Choose an optimal circuit (lowest weight). Note there is always more than one
optimal circuit to choose from.
Advantage to Brute Force:
You will find the absolute cheapest circuit in the graph.
Disadvantage to Brute Force:
There are (v − 1! Hamilton circuits in K v . That quickly becomes a lot of calculations,
since even with just 8 vertices to the graph, you’re looking at 7!  5040 different
circuits, and you have to add up the weights of the edges on each one. If the problem
has more than 18 vertices, you’re looking at a number of circuits to check (17! 
355 687 428 096 000) that even with a computer will take a prohibitively long time. And if
we add one more vertex, there become 19 times as many circuits to check, which
would mean it takes 19 times as long to solve. If we’re looking at a UPS route then
surely there are more than 18 delivery points on any given day; doing such a large
number of calculations every day would take too long. We need a faster algorithm,
even if it doesn’t give the absolute cheapest route.
The Nearest Neighbor Algorithm: we mentioned briefly on Thursday
Start: Start at the designated starting vertex. If there isn’t a designated starting
vertex, pick any vertex.
First step: From the starting vertex go to its "nearest neighbor" (ie. the vertex for which
the corresponding edge has the smallest weight).
Middle steps: From each vertex go to it nearest neighbor among the vertices that
haven’t yet been visited. Keep doing this until all the vertices have been visited.
Last step: From the last vertex return to the starting vertex.
Advantage to Nearest Neighbor:
Speed! At each step we only have to compare to each other vertex. So for instance, if
we have 8 vertices, we will have 7  6  5  4  3  2  1  28 checks of costs of edges
to do. As opposed to 7!  5040 calculations to do with Brute Force. Way faster, since
as we add a vertex, we only add v calculations, instead of having v times as many
calculations.
Disadvantage to Nearest Neighbor:
It’s too rough an estimate. It gives a cheap circuit, but very seldomly is it the cheapest
circuit.
1
So we want: an algorithm that is still much faster than Brute Force, but one that has a
better chance of being the cheapest.
Solutions: 2 more algorithms
The Repetitive Nearest-Neighbor Algorithm
 Let X be any vertex. Find the nearest-neighbor circuit using X as the starting
vertex. Calculate the cost of the circuit.
 Repeat the process with each of the other vertices of the graph as the starting
vertex.
 Of the nearest-neighbor circuits we find, keep the best one (cheapest). If there
is a designated starting vertex, rewrite the circuit using that vertex as the
reference point.
Example with Traveling Salesman graph:
Starting at A: ACEDBA119  120  199  150  185  773
starting at B: BCEADB121  120  133  152  150  676
starting at C: CAEDBC119  133  199  150  121  722
starting at D: DBCAED150  121  119  133  199  722
starting at E: EACBDE133  119  121  150  199  722
cheapest is BCEADB with $676 but need to "reroute this to start and end at A:
ADBCEA is the re-ordered same circuit, 152  150  121  120  133  676 with same
cost. Notice with this case, we actually found the cheapest circuit overall, which we
know since we calculated all of them using Brute Force last week.
2
Example: Suppose you want to go from home to the bank, post office, and market.
Here is a chart of distances between each location:
Home Bank Post Office Market
Home
*
3
5.5
3.5
Bank
3
*
4
5
Post Office 5.5
4
*
5
Market
5
5
*
3.5
Graph:
Let’s use Repetitive Nearest Neighbor method to find the cheapest route starting and
ending at home.
starting at home, cheapest is H,B, P.O., M, H 3  4  5  3. 5  15. 5
start at Bank: B,H, M, P.O.,B3  3. 5  5  4  15. 5
start at P.O.: PO, B, H, M, PO 4  3  3. 5  5  15. 5
start at Market: M, H, B, PO, M3. 5  3  4  5  15. 5
all of these have the same cost, so the order we’ll go with is H,B, P.O., M, H since it
starts and ends at Home. notice all the others were just new orderings of the same
circuit!
New Algorithm: The Cheapest Link Algorithm
How it works: Look at the entire graph and choose the cheapest edge of the graph,
wherever that may be. Then pick the next cheapest edge, even if it’s not adjacent to
the edge you chose before. Continue this way, following two rules: i) do not allow a
circuit to form except at the very end, and ii) do not allow three edges to come
together at a vertex. Following these two rules will guarantee a Hamilton circuit.
Show by example: our 5-city traveling salesman problem
the cheapest edge overall is AC, $119.we’ll mark it red. The next cheapest is CE,
$120. Mark it red. Next cheapest is BC which is $121 but that’s too many edges
3
touching C so we can’t pick it. The next cheapest is AE, $133, but that would cause a
circuit ACEA. So go up to the next cheapest, which is BD $150. Mark it red. next
cheapest is AD $152. pick that. Finally we’ve hit every vertex, so we just close up the
circuit with BE, $200. Our cost is 741. Not the cheapest, but cheaper than one
nearest-neighbor at A would give us.
Cheapest Link Algorithm Steps:
Step 1. Pick the cheapest link. Mark it with a color/symbol.
Step 2. Pick the next cheapest link available and mark it.
Steps 3,4,...v − 1. Continue picking and marking the cheapest unmarked link available
that does not
a) close a circuit, or
b) create three edges coming out of a single vertex.
Step v. Connect the last two vertices to close the marked circuit.
4