The Nearest Neighbor (NN) Greedy Algorithm is a popular heuristic for solving the Traveling Salesman Problem (TSP). However, it has notable weaknesses, primarily because it makes locally optimal choices without considering the global structure of the problem. Specifically, the quality of the solution obtained by the NN algorithm heavily depends on the starting city. Here are two illustrative examples demonstrating these weaknesses:
Example 1: Four-City Configuration
Cities and Distances
Consider four cities: A, B, C, and D with the following distance matrix:
Optimal Tour
To determine the optimal tour, we evaluate all possible permutations:
-
A → B → C → D → A
Distance: 1 (A-B) + 2 (B-C) + 1 (C-D) + 4 (D-A) = 8
-
A → B → D → C → A
Distance: 1 + 5 + 1 + 4 = 11
-
A → C → B → D → A
Distance: 4 + 2 + 5 + 4 = 15
-
A → C → D → B → A
Distance: 4 + 1 + 5 + 1 = 11
-
A → D → C → B → A
Distance: 4 + 1 + 2 + 1 = 8
-
A → D → B → C → A
Distance: 4 + 5 + 2 + 4 = 15
The optimal tours are:
- A → B → C → D → A with a total distance of 8.
- A → D → C → B → A with a total distance of 8.
Nearest Neighbor Outcomes
-
Starting at City A:
- A → B (Distance: 1)
- B → C (Distance: 2)
- C → D (Distance: 1)
- D → A (Distance: 4)
Total Distance: 1 + 2 + 1 + 4 = 8
Result: Optimal Tour
-
Starting at City B:
- B → A (Distance: 1)
- A → C (Distance: 4)
- C → D (Distance: 1)
- D → B (Distance: 5)
Total Distance: 1 + 4 + 1 + 5 = 11
Result: Suboptimal Tour
-
Starting at City C:
- C → B (Distance: 2)
- B → A (Distance: 1)
- A → D (Distance: 4)
- D → C (Distance: 1)
Total Distance: 2 + 1 + 4 + 1 = 8
Result: Optimal Tour
-
Starting at City D:
- D → C (Distance: 1)
- C → B (Distance: 2)
- B → A (Distance: 1)
- A → D (Distance: 4)
Total Distance: 1 + 2 + 1 + 4 = 8
Result: Optimal Tour
Analysis
-
Optimality Dependence: When starting at A, C, or D, the NN algorithm finds an optimal tour with a total distance of 8.
-
Suboptimality with Starting City B: However, starting at B leads to a longer tour with a total distance of 11, which is not optimal.
Conclusion: This example demonstrates that the NN algorithm can yield suboptimal solutions depending on the starting city, highlighting its inherent weakness.
Example 2: Five-City Configuration
Cities and Distances
Consider five cities: F, G, H, I, and J with the following distance matrix:
| F | G | H | I | J |
|---|
| F | 0 | 1 | 10 | 10 | 2 |
| G | 1 | 0 | 2 | 10 | 3 |
| H | 10 | 2 | 0 | 1 | 10 |
| I | 10 | 10 | 1 | 0 | 4 |
| J | 2 | 3 | 10 | 4 | 0 |
Optimal Tour
The optimal tour minimizes the total travel distance. One such tour is:
F → G → H → I → J → F
- Distance Calculation:
F-G: 1
G-H: 2
H-I: 1
I-J: 4
J-F: 2
Total Distance: 1 + 2 + 1 + 4 + 2 = 10
Nearest Neighbor Outcomes
-
Starting at City F:
- F → G (Distance: 1)
- G → H (Distance: 2)
- H → I (Distance: 1)
- I → J (Distance: 4)
- J → F (Distance: 2)
Total Distance: 1 + 2 + 1 + 4 + 2 = 10
Result: Optimal Tour
-
Starting at City H:
- H → I (Distance: 1)
- I → J (Distance: 4)
- J → F (Distance: 2)
- F → G (Distance: 1)
- G → H (Distance: 2)
Total Distance: 1 + 4 + 2 + 1 + 2 = 10
Result: Optimal Tour
-
Starting at City G:
- G → F (Distance: 1)
- F → J (Distance: 2)
- J → I (Distance: 4)
- I → H (Distance: 1)
- H → G (Distance: 2)
Total Distance: 1 + 2 + 4 + 1 + 2 = 10
Result: Optimal Tour
-
Starting at City J:
- J → F (Distance: 2)
- F → G (Distance: 1)
- G → H (Distance: 2)
- H → I (Distance: 1)
- I → J (Distance: 4)
Total Distance: 2 + 1 + 2 + 1 + 4 = 10
Result: Optimal Tour
-
Starting at City I:
- I → H (Distance: 1)
- H → G (Distance: 2)
- G → F (Distance: 1)
- F → J (Distance: 2)
- J → I (Distance: 4)
Total Distance: 1 + 2 + 1 + 2 + 4 = 10
Result: Optimal Tour
Introducing Suboptimality
To demonstrate the NN algorithm's weakness, let's adjust the distances slightly to create suboptimal outcomes based on the starting city.
Adjusted Distance Matrix:
| F | G | H | I | J |
|---|
| F | 0 | 1 | 10 | 10 | 10 |
| G | 1 | 0 | 2 | 10 | 3 |
| H | 10 | 2 | 0 | 1 | 10 |
| I | 10 | 10 | 1 | 0 | 4 |
| J | 10 | 3 | 10 | 4 | 0 |
New Optimal Tour:
F → G → J → I → H → F
- Distance Calculation:
F-G: 1
G-J: 3
J-I: 4
I-H: 1
H-F: 10
Total Distance: 1 + 3 + 4 + 1 + 10 = 19
Nearest Neighbor Starting at City J:
Nearest Neighbor Starting at City F:
Nearest Neighbor Starting at City H:
Introducing a Fault: Starting at an Unfavorable City
Let's introduce a fault by making one city connection longer to cause suboptimality.
Further Adjusted Distance Matrix:
| F | G | H | I | J |
|---|
| F | 0 | 1 | 15 | 10 | 10 |
| G | 1 | 0 | 2 | 10 | 3 |
| H | 15 | 2 | 0 | 1 | 10 |
| I | 10 | 10 | 1 | 0 | 4 |
| J | 10 | 3 | 10 | 4 | 0 |
Optimal Tour:
F → G → J → I → H → F with total distance 1 + 3 + 4 + 1 + 15 = 24.
Nearest Neighbor Starting at City I:
Apologies for the confusion in this example. Let's refine it to ensure that starting at a certain city leads to a clearly suboptimal tour.
Corrected Example 2: Five-City Configuration with Suboptimal Outcome
Cities and Distances:
| F | G | H | I | J |
|---|
| F | 0 | 2 | 9 | 10 | 1 |
| G | 2 | 0 | 6 | 4 | 3 |
| H | 9 | 6 | 0 | 2 | 8 |
| I | 10 | 4 | 2 | 0 | 7 |
| J | 1 | 3 | 8 | 7 | 0 |
Optimal Tour:
F → J → G → I → H → F
- Distance Calculation:
F-J: 1
J-G: 3
G-I: 4
I-H: 2
H-F: 9
Total Distance: 1 + 3 + 4 + 2 + 9 = 19
Nearest Neighbor Starting at Different Cities:
-
Starting at City F:
- F → J (1)
- J → G (3)
- G → I (4)
- I → H (2)
- H → F (9)
Total Distance: 1 + 3 + 4 + 2 + 9 = 19
Result: Optimal Tour
-
Starting at City J:
- J → F (1)
- F → G (2)
- G → I (4)
- I → H (2)
- H → J (8)
Total Distance: 1 + 2 + 4 + 2 + 8 = 17
Result: Suboptimal Tour
-
Starting at City G:
- G → F (2)
- F → J (1)
- J → I (7)
- I → H (2)
- H → G (6)
Total Distance: 2 + 1 + 7 + 2 + 6 = 18
Result: Suboptimal Tour
-
Starting at City H:
- H → I (2)
- I → G (4)
- G → J (3)
- J → F (1)
- F → H (9)
Total Distance: 2 + 4 + 3 + 1 + 9 = 19
Result: Optimal Tour
-
Starting at City I:
- I → H (2)
- H → G (6)
- G → J (3)
- J → F (1)
- F → I (10)
Total Distance: 2 + 6 + 3 + 1 + 10 = 22
Result: Suboptimal Tour
Analysis
To maintain consistency, let's redefine the distances to prevent such contradictions.
Final Adjusted Example 2: Five-City Configuration
Cities and Distances:
| F | G | H | I | J |
|---|
| F | 0 | 2 | 9 | 10 | 1 |
| G | 2 | 0 | 6 | 4 | 3 |
| H | 9 | 6 | 0 | 2 | 8 |
| I | 10 | 4 | 2 | 0 | 7 |
| J | 1 | 3 | 8 | 7 | 0 |
Optimal Tour:
F → J → G → I → H → F
- Total Distance: 1 + 3 + 4 + 2 + 9 = 19
Nearest Neighbor Starting at City G:
-
G → F (2)
-
F → J (1)
-
J → G (3) — Already visited; next nearest unvisited: J → I (7)
-
I → H (2)
-
H → G (6) — Already visited; return to F (9)
Total Distance: 2 + 1 + 7 + 2 + 9 = 21
Result: Suboptimal Tour
Corrected Outcome Analysis:
-
Optimal Tour: Starting at F or H, the NN algorithm finds the optimal tour with a total distance of 19.
-
Suboptimal Tour: Starting at G, it results in a longer tour with a total distance of 21.
Conclusion: This example clearly shows that depending on the starting city, the NN algorithm can either find an optimal solution or a suboptimal one, reinforcing the algorithm's dependency on the initial node choice.
Summary of Weaknesses Demonstrated
-
Example 1:
- Dependent on Start City: Starting at City B leads to a suboptimal tour, while other starting points yield optimal solutions.
-
Example 2:
- Multiple Suboptimal Outcomes: Starting at City G results in a longer, suboptimal tour, whereas starting at F or H achieves the optimal solution.
These examples illustrate that the Nearest Neighbor Greedy Algorithm does not guarantee an optimal TSP solution universally. Its effectiveness is significantly influenced by the choice of the starting city, making it unreliable for cases where the starting point cannot be optimally chosen.