maths.free › Combinatorics & Graph Theory › 12. Graph Theory › Traveling Salesperson Problem
Traveling Salesperson Problem
Distinguish between brute force algorithms and greedy algorithms.
Learning Objectives
After completing this section, you should be able to:
- Distinguish between brute force algorithms and greedy algorithms.
- List all distinct Hamilton cycles of a complete graph.
- Apply brute force method to solve traveling salesperson applications.
- Apply nearest neighbor method to solve traveling salesperson applications.
Brute Force and Greedy Algorithms
An algorithm is a sequence of steps that can be used to solve a particular problem. We have solved many problems in this chapter, and the procedures that we used were different types of algorithms. In this section, we will use two common types of algorithms, a brute force algorithm and a greedy algorithm. A brute force algorithm begins by listing every possible solution and applying each one until the best solution is found. A greedy algorithm approaches a problem in stages, making the apparent best choice at each stage, then linking the choices together into an overall solution which may or may not be the best solution.
To understand the difference between these two algorithms, consider the tree diagram in . Suppose we want to find the path from left to right with the largest total sum. For example, branch A in the tree diagram has a sum of \(10+2+11+13=36\).
To be certain that you pick the branch with greatest sum, you could list each sum from each of the different branches:
A: \(10+2+11+13=36\)
B: \(10+2+11+8=31\)
C: \(10+2+15+1=28\)
D: \(10+2+15+6=33\)
Condensed — the full section is in OpenStax Contemporary Mathematics.
The Traveling Salesperson Problem
Now let’s focus our attention on the graph theory application known as the traveling salesperson problem (TSP) in which we must find the shortest route to visit a number of locations and return to the starting point.
Recall from Hamilton Cycles, the officer in the U.S. Air Force who is stationed at Vandenberg Air Force base and must drive to visit three other California Air Force bases before returning to Vandenberg. The officer needed to visit each base once. We looked at the weighted graph in representing the four U.S. Air Force bases: Vandenberg, Edwards, Los Angeles, and Beal and the distances between them.
Any route that visits each base and returns to the start would be a Hamilton cycle on the graph. If the officer wants to travel the shortest distance, this will correspond to a Hamilton cycle of lowest weight. We saw in that there are six distinct Hamilton cycles (directed cycles) in a complete graph with four vertices, but some lie on the same cycle (undirected cycle) in the graph.
| Complete Graph | Cycle | Cycle | Cycle |
| Clockwise Hamilton Cycle |
a → b → c → d → a |
a → b → d → c → a |
a → c → b → d → a |
| Counterclockwise Hamilton Cycle |
a → d → c → b → a |
a → c → d → b → a |
a → d → b → c → a |
Since the distance between bases is the same in either direction, it does not matter if the officer travels clockwise or counterclockwise. So, there are really only three possible distances as shown in .
The possible distances are:
\[\begin{array}{l}396+410+106+159=1071 \\ 207+410+439+159=1215 \\ 396+439+106+207=1148\end{array}\]So, a Hamilton cycle of least weight is V → B → E → L → V (or the reverse direction). The officer should travel from Vandenberg to Beal to Edwards, to Los Angeles, and back to Vandenberg.
Finding Weights of All Hamilton Cycles in Complete Graphs
Notice that we listed all of the Hamilton cycles and found their weights when we solved the TSP about the officer from Vandenberg. This is a skill you will need to practice. To make sure you don't miss any, you can calculate the number of possible Hamilton cycles in a complete graph. It is also helpful to know that half of the directed cycles in a complete graph are the same cycle in reverse direction, so, you only have to calculate half the number of possible weights, and the rest are duplicates.
Calculating Possible Weights of Hamilton Cycles
Try it.
Suppose you have a complete weighted graph with vertices N, M, O, and P.
- Use the formula \((n-1)!\) to calculate the number of distinct Hamilton cycles in the graph.
- Use the formula \(\frac{(n-1)!}{2}\) to calculate the greatest number of different weights possible for the Hamilton cycles.
- Are all of the distinct Hamilton cycles listed here? How do you know?
Cycle 1: N → M → O → P → N
Cycle 2: N → M → P → O → N
Cycle 3: N → O → M → P → N
Cycle 4: N → O → P → M → N
Cycle 5: N → P → M → O → N
Cycle 6: N → P → O → M → N - Which pairs of cycles must have the same weights? How do you know?
Solution
- There are 4 vertices; so, \(n=4\). This means there are \((n-1)!=(4-1)!=3⋅2⋅1=6\) distinct Hamilton cycles beginning at any given vertex.
- Since \(n=4\), there are \(\frac{(n-1)!}{2}=\frac{(4-1)!}{2}=\frac{6}{2}=3\) possible weights.
- Yes, they are all distinct cycles and there are 6 of them.
- Cycles 1 and 6 have the same weight, Cycles 2 and 4 have the same weight, and Cycles 3 and 5 have the same weight, because these pairs follow the same route through the graph but in reverse.
The Brute Force Method
The method we have been using to find a Hamilton cycle of least weight in a complete graph is a brute force algorithm, so it is called the brute force method. The steps in the brute force method are:
Step 1: Calculate the number of distinct Hamilton cycles and the number of possible weights.
Step 2: List all possible Hamilton cycles.
Step 3: Find the weight of each cycle.
Step 4: Identify the Hamilton cycle of lowest weight.
Applying the Brute Force Method
Try it.
On the next assignment, the air force officer must leave from Travis Air Force base, visit Beal, Edwards, and Vandenberg Air Force bases each exactly once and return to Travis Air Force base. There is no need to visit Los Angeles Air Force base. Use to find the shortest route.
Solution
Step 1: Since there are 4 vertices, there will be \((4-1)!=3!=6\) cycles, but half of them will be the reverse of the others; so, there will be \(\frac{(4-1)!}{2}=\frac{6}{2}=3\) possible distances.
Step 2: List all the Hamilton cycles in the subgraph of the graph in .
To find the 6 cycles, focus on the three vertices in the middle, B, E, and V. The arrangements of these vertices are BEV, BVE, EBV, EVB, VBE, and VEB. These would correspond to the 6 cycles:
1: T → B → E → V → T
2: T → B → V → E → T
3: T → E → B → V → T
4: T → E → V → B → T
5: T → V → B → E → T
6: T → V → E → B → T
Step 3: Find the weight of each path. You can reduce your work by observing the cycles that are reverses of each other.
1: \(84+410+207+396=1097\)
2: \(84+396+207+370=1071\)
3: \(370+410+396+396=1572\)
4: Reverse of cycle 2, 1071
5: Reverse of cycle 3, 1572
6: Reverse of cycle 1, 1097
Step 4: Identify a Hamilton cycle of least weight.
The second path, T → B → V → E → T, and its reverse, T → E → V → B → T, have the least weight. The solution is that the officer should travel from Travis Air Force base to Beal Air Force Base, to Vandenberg Air Force base, to Edwards Air Force base, and return to Travis Air Force base, or the same route in reverse.
Now suppose that the officer needed a cycle that visited all 5 of the Air Force bases in . There would be \((5-1)!=4!=4\times 3\times 2\times 1=24\) different arrangements of vertices and \(\frac{(5-1)!}{2}=\frac{4!}{2}=\frac{24}{2}=12\) distances to compare using the brute force method. If you consider 10 Air Force bases, there would be \((10-1)!=9!=9⋅8⋅7⋅6⋅5⋅4⋅3⋅2⋅1=362,880\) different arrangements and \(\frac{(10-1)!}{2}=\frac{9!}{2}=\frac{9⋅8⋅7⋅6⋅5⋅4⋅3⋅2⋅1}{2}=181,440\) distances to consider. There must be another way!
The Nearest Neighbor Method
When the brute force method is impractical for solving a traveling salesperson problem, an alternative is a greedy algorithm known as the nearest neighbor method, which always visit the closest or least costly place first. This method finds a Hamilton cycle of relatively low weight in a complete graph in which, at each phase, the next vertex is chosen by comparing the edges between the current vertex and the remaining vertices to find the lowest weight. Since the nearest neighbor method is a greedy algorithm, it usually doesn’t give the best solution, but it usually gives a solution that is "good enough." Most importantly, the number of steps will be the number of vertices. That’s right! A problem with 10 vertices requires 10 steps, not 362,880. Let’s look at an example to see how it works.
Suppose that a candidate for governor wants to hold rallies around the state. They plan to leave their home in city A, visit cities B, C, D, E, and F each once, and return home. The airfare between cities is indicated in the graph in .
Let’s help the candidate keep costs of travel down by applying the nearest neighbor method to find a Hamilton cycle that has a reasonably low weight. Begin by marking starting vertex as \({V}_{1}\) for "visited 1st." Then to compare the weights of the edges between A and vertices adjacent to A: $250, $210, $300, $200, and $100 as shown in . The lowest of these is $100, which is the edge between A and F.
Mark F as \({V}_{2}\) for "visited 2nd" then compare the weights of the edges between F and the remaining vertices adjacent to F: $170, $330, $150 and $350 as shown in . The lowest of these is $150, which is the edge between F and D.
Mark D as \({V}_{3}\) for "visited 3rd." Next, compare the weights of the edges between D and the remaining vertices adjacent to D: $120, $310, and $270 as shown in . The lowest of these is $120, which is the edge between D and B.
So, mark B as \({V}_{4}\) for "visited 4th." Finally, compare the weights of the edges between B and the remaining vertices adjacent to B: $160 and $220 as shown in . The lower amount is $160, which is the edge between B and E.
Now you can mark E as \({V}_{5}\) and mark the only remaining vertex, which is C, as \({V}_{6}\). This is shown in . Make a note of the weight of the edge from E to C, which is $180, and from C back to A, which is $210.
Condensed — the full section is in OpenStax Contemporary Mathematics.
Key Concepts
- A brute force algorithm always finds the ideal solution but can be impractical whereas a greedy algorithm is efficient but usually does not lead to the ideal solution.
- A Hamilton cycle of lowest weight is a solution to the traveling salesperson problem.
- The brute force method finds a Hamilton cycle of lowest weight in a complete graph.
- The nearest neighbor method is a greedy algorithm that finds a Hamilton cycle of relatively low weight in a complete graph.
Formulas
- In a complete graph with \(n\) vertices, the number of distinct Hamilton cycles is \((n-1)!\).
- In a complete graph with \(n\) vertices, there are at most \(\frac{(n-1)!}{2}\) different weights of Hamilton cycles.
Practice (3)
Try each one on paper first. Reveal the answer to check; verified ones can be opened in the solver for every step.
-
Suppose you have a complete weighted graph with vertices N, M, O, and P.
- Use the formula \((n-1)!\) to calculate the number of distinct Hamilton cycles in the graph.
- Use the formula \(\frac{(n-1)!}{2}\) to calculate the greatest number of different weights possible for the Hamilton cycles.
- Are all of the distinct Hamilton cycles listed here? How do you know?
Cycle 1: N → M → O → P → N
Cycle 2: N → M → P → O → N
Cycle 3: N → O → M → P → N
Cycle 4: N → O → P → M → N
Cycle 5: N → P → M → O → N
Cycle 6: N → P → O → M → N - Which pairs of cycles must have the same weights? How do you know?
Одкриј го одговорот
- There are 4 vertices; so, \(n=4\). This means there are \((n-1)!=(4-1)!=3⋅2⋅1=6\) distinct Hamilton cycles beginning at any given vertex.
- Since \(n=4\), there are \(\frac{(n-1)!}{2}=\frac{(4-1)!}{2}=\frac{6}{2}=3\) possible weights.
- Yes, they are all distinct cycles and there are 6 of them.
- Cycles 1 and 6 have the same weight, Cycles 2 and 4 have the same weight, and Cycles 3 and 5 have the same weight, because these pairs follow the same route through the graph but in reverse.
-
On the next assignment, the air force officer must leave from Travis Air Force base, visit Beal, Edwards, and Vandenberg Air Force bases each exactly once and return to Travis Air Force base. There is no need to visit Los Angeles Air Force base. Use to find the shortest route.
Одкриј го одговорот
Step 1: Since there are 4 vertices, there will be \((4-1)!=3!=6\) cycles, but half of them will be the reverse of the others; so, there will be \(\frac{(4-1)!}{2}=\frac{6}{2}=3\) possible distances.
Step 2: List all the Hamilton cycles in the subgraph of the graph in .
To find the 6 cycles, focus on the three vertices in the middle, B, E, and V. The arrangements of these vertices are BEV, BVE, EBV, EVB, VBE, and VEB. These would correspond to the 6 cycles:
1: T → B → E → V → T
2: T → B → V → E → T
3: T → E → B → V → T
4: T → E → V → B → T
5: T → V → B → E → T
6: T → V → E → B → T
Step 3: Find the weight of each path. You can reduce your work by observing the cycles that are reverses of each other.
1: \(84+410+207+396=1097\)
2: \(84+396+207+370=1071\)
3: \(370+410+396+396=1572\)
4: Reverse of cycle 2, 1071
5: Reverse of cycle 3, 1572
6: Reverse of cycle 1, 1097
Step 4: Identify a Hamilton cycle of least weight.
The second path, T → B → V → E → T, and its reverse, T → E → V → B → T, have the least weight. The solution is that the officer should travel from Travis Air Force base to Beal Air Force Base, to Vandenberg Air Force base, to Edwards Air Force base, and return to Travis Air Force base, or the same route in reverse.
-
Suppose that the candidate for governor wants to hold rallies around the state but time before the election is very limited. They would like to leave their home in city A, visit cities B, C, D, E, and F each once, and return home. The airfare between cities is not as important as the time of travel, which is indicated in . Use the nearest neighbor method to find a route with relatively low travel time. What is the total travel time of the route that you found?
Одкриј го одговорот
Step 1: Label vertex A as \({V}_{1}\). The edge of lowest weight between A and the remaining vertices is 85 min between A and D.
Step 2: Label vertex D as \({V}_{2}\). The edge of lowest weight between D and the vertices that remain to be visited, B, C, E, and F, is 70 min between D and F.
Repeat Step 2: Label vertex F as \({V}_{3}\). The edge of lowest weight between F and the vertices that remain to be visited, B, C, and E, is 75 min between F and C.
Repeat Step 2: Label vertex C as \({V}_{4}\). The edge of lowest weight between C and the vertices that remain to be visited, B and E, is 100 min between C and B.
Repeat Step 2: Label vertex B as \({V}_{5}\). The only vertex that remains to be visited is E. The weight of the edge between B and E is 95 min.
Step 3: A Hamilton cycle of low weight is A → D → F → C → B → E → A. So, a route of relatively low travel time is A to D to F to C to B to E and back to A. The total travel time of this route is: \(85\ \min +70\ \min +75\ \min +100\ \min +95\ \min +90\ \min =515\ \min \text{or}\ 8\ \text{hrs}\ 35\ \min\)
Symbols used here
n × (n−1) × … × 1; the number of orderings of n things. 0! = 1.
Number of k-element subsets of n things: n!/(k!(n−k)!).
Add a_k for k = 1 up to n.
Multiply a_k for k = 1 up to n.
The set with no elements; the number of elements of A.
How to: Traveling Salesperson Problem
- Distinguish between brute force algorithms and greedy algorithms.
- List all distinct Hamilton cycles of a complete graph.
- Apply brute force method to solve traveling salesperson applications.
- Apply nearest neighbor method to solve traveling salesperson applications.
- Design of fiber optic networks
- Minimizing fuel expenses for repositioning satellites
- Development of semi-conductors for microchips
- A technique for mapping mammalian chromosomes in genome sequencing
Questions people ask
Permutation or combination?
Ask whether order matters. A lock code is a permutation (order matters); a hand of cards is a combination (it does not).
What is a graph in this sense?
Dots (vertices) joined by lines (edges) — not a plot. Road maps, social networks and molecules are graphs; questions like "is there a route" and "how few colours" are graph theory.
Обиди се со себе.
Parts of this page are adapted from OpenStax Contemporary Mathematics (CC BY-NC-SA 4.0). Condensed and re-explained here; errors are ours.
Повеќе во Combinatorics & Graph Theory
The counting principlesPigeonhole principle and inclusion–exclusionBinomial coefficients and Pascal's triangleRecurrences and generating functionsGraphs: vertices, edges, degreesPaths, cycles, trees, Euler and HamiltonColouring and planar graphs