Nearest-Neighbour Problem

Distribute Education like Computer Geek

Introduction

.

The Nearest Neighbour Problem (NNP) or Traveling Salesman Problem (TSP) by greedy method is a classic algorithmic problem in computer science and operations research.

The goal is simple – “Given a list of cities and the distances between them, find the shortest possible route that visits each city exactly once and returns to the starting point.”

This problem is crucial in fields like logistics, transportation, and manufacturing.

The TSP can be solved using various methods

  1. Brute Force – Check all possible routes and choose the shortest one. This method guarantees the optimal solution but is impractical for large datasets due to its high computational cost.
  2. Greedy Algorithms – Heuristics that make local optimal choices at each step to find a global optimum. It is also called Nearest Neighbour Problem.
  3. Dynamic Programming – Optimizes the problem by solving subproblems and combining their solutions. It is also called Held-Karp algorithm.
  4. Branch & Bound Approach – breaking the problem into smaller subproblems (branching) and calculating a lower bound for each subproblem. If the lower bound exceeds the current best solution, that branch is pruned (ignored).
  5. Approximation Algorithms – Provides near-optimal solutions in a reasonable time, useful for large instances. It is also called Christofides Algorithm.

We first solve the Travelling Salesman Problem by Greedy algorithm, that is Nearest Neighbour Problem.

.

Nearest Neighbour Problem Statement

“Given a set of n cities and the distances between every pair of cities, find a tour that visits each city exactly once and returns to the starting city.

The objective is to construct this tour using the Nearest Neighbour Algorithm, aiming to minimize the total distance travelled, though it may not yield the optimal solution.”

.

History

The concept of finding the nearest neighbour likely originates from the study of Voronoi diagrams in the 19th century by mathematicians like Georgy Voronoy. However, the computational aspects of the problem became a focus in the mid-20th century.

During this time, researchers began exploring the problem in the context of geographic information systems (GIS), clustering, and optimization.

In the 1970s, the problem became more formalized with the advent of computational geometry. The introduction of the k-d tree by Jon Bentley in 1975 was a significant breakthrough. The k-d tree is a data structure that allows efficient search for the nearest neighbour in a multidimensional space. During this period, the problem was also studied in operations research, particularly in the context of the Traveling Salesman Problem (TSP). The nearest neighbour heuristic became a popular method for generating approximate solutions to the TSP.

The nearest neighbour search problem also became crucial in machine learning, pattern recognition, and computer vision. For example, in k-nearest neighbours (k-NN) classification, the goal is to classify a data point based on the majority class of its nearest neighbours.

.

Algorithm NearestNeighborTSP(C, D)

Input

C = {C1, C2, …, Cn}  // Set of n cities

D = Distance matrix where D[i][j] is the distance between city Ci and city Cj

1. Let currentCity = C1 // Start from the first city

2. Mark currentCity as visited

3. Initialize tour = [currentCity]

4. Initialize totalDistance = 0

5. While there are unvisited cities

  • Find the nearest unvisited city (Minimum Distance)
  • Add the distance D[currentCity][nearestCity] to totalDistance
  • Move to nearestCity
  • Mark nearestCity as visited
  • Append nearestCity to tour
  • Set currentCity = nearestCity

6. Add the distance D[currentCity][C1] to totalDistance // Return to the starting city

7. Append the starting city C1 to the end of the tour

8. Output tour and totalDistance

.

Example

Travelling Salesman Problem

.

Example Distance Matrix

Travelling Salesman Problem adjacency matrix

.

Find a tour that visits each city exactly once and returns to the starting city by always moving to the nearest unvisited city.

Steps

1. Initialization

    • Start at City A.
    • Mark City A as visited.
    • Initialize tour with City A.

2. Iterative Process

    • From City A –
      • Unvisited cities = B, C, D, E, F
      • Distances is A → B = 35, A → C = 25, A → D = 30, A → E = 20, A → F = 10
      • Nearest city, F (distance = 10)
      • Move to F, mark F as visited.
      • Total tour distance = 10

Nearest Neighbour Problem 1

.

  • From City F –
    • Unvisited cities = B, C, D, E
    • Distances is F → B = 15, F → C = 20, F → D = 25, F → E = 30
    • Nearest city, B (distance = 15)
    • Move to B, mark B as visited.
    • Total tour distance = 10 + 15 = 25

Nearest Neighbour Problem 2

.

    • From City B –
      • Unvisited cities = C, D, E
      • Distances is B → C = 30, B → D = 20, B → E = 15
      • Nearest city, E (distance = 15)
      • Move to E, mark E as visited.
      • Total tour distance = 25 + 15 = 40

Nearest Neighbour Problem 3

.

  • From City E –
    • Unvisited cities = C, D
    • Distances is E → C = 10, E → D = 25
    • Nearest city, C (distance = 10)
    • Move to C, mark C as visited.
    • Total tour distance = 40 + 10 = 50

Nearest Neighbour Problem 4

.

  • From City C –
    • Unvisited city = D
    • Distance is C → D = 15
    • Move to D, mark D as visited.
    • Total tour distance = 50 + 15 = 65

.

3. Returning to starting city A from D

    • Distance is D → A = 15
    • Final total tour distance = 65 + 30 = 95

Nearest Neighbour Problem 5

So, our tour is A → F → B → E → C → D → A

This solution demonstrates how the Nearest Neighbour Algorithm works and provides an approximate route based on local decisions at each step.

.

Source Code for Nearest Neighbour Problem
Time Complexity for the Nearest Neighbour Algorithm

Initializing arrays and setting up variables is O(n), where n is the number of cities.

The outer loop runs n times and for each iteration of the outer loop, the inner loop checks all remaining unvisited cities to find the nearest one. This is O(n) in the worst case. Therefore, the time complexity for the iterative process is O(n2).

Returning to the starting city and calculating the total distance are O(n) operations.

Thus, the time complexity of the Nearest Neighbour Algorithm is O(n2).

.

Space Complexity for the Nearest Neighbour Algorithm

The space complexity is O(n) due to the storage required for the visited array and the tour array.

.

Advantages for the Nearest Neighbour Algorithm
  1. The Nearest Neighbour Algorithm is straightforward to understand and implement. It follows a simple greedy approach, making it a good starting point for solving the Traveling Salesman Problem (TSP).
  2. With a time-complexity of O(n2), it is relatively fast compared to exact algorithms like dynamic programming, especially for larger numbers of cities.
  3. Requires minimal computational resources compared to more complex algorithms, making it suitable for scenarios with limited processing power.

.

Disadvantages for the Nearest Neighbour Algorithm
  1. The Nearest Neighbour Algorithm does not always produce the optimal solution. It can easily get stuck in local optima because it makes a series of local decisions without considering the global context.
  2. There is no guarantee that the tour produced is the shortest possible. The quality of the solution depends heavily on the starting city and the structure of the distance matrix.
  3. The final tour can vary significantly based on the starting city. Different starting points might lead to different tour lengths.
  4. It does not handle constraints such as varying priorities for different cities or specific travel restrictions well.

.

Application for the Nearest Neighbour Algorithm
  1. Route Planning – Used in applications where a quick, heuristic-based route planning is needed, such as delivery scheduling or logistics where exact solutions are computationally expensive.
  2. Computer Graphics – Helps in various graphics and optimization problems where heuristic solutions are acceptable, such as optimizing the path for drawing complex shapes or patterns.
  3. Robotics – Applied in path planning for robots and automated vehicles, where a fast heuristic solution is useful for real-time applications.
  4. Travel Itinerary Planning – Useful for travel planning applications where a rough estimation of the shortest travel route is required, and an exact solution is not feasible due to time constraints.
  5. Network Design – Can be used in designing efficient network topologies and minimizing the travel distance or connection length in communication networks.

Test Yourself

Q1- What is the Nearest Neighbour Algorithm, and how is it applied to solve the Traveling Salesman Problem (TSP)?

The Nearest Neighbour Algorithm is a greedy heuristic used to find a solution for the Traveling Salesman Problem (TSP). The algorithm starts at a chosen city and repeatedly moves to the nearest unvisited city until all cities are visited. Finally, it returns to the starting city to complete the tour. This approach aims to minimize the total distance travelled but does not guarantee the optimal solution.

Q2- Discuss the time complexity of the Nearest Neighbour Algorithm.

The time complexity of the Nearest Neighbour Algorithm is O(n2), where n is the number of cities.

This is because for each city, the algorithm searches through all unvisited cities to find the nearest one, which takes O(n) time per city. Since this process is repeated for each city, the overall complexity is O(n2).

Q3- Explain the impact of the starting city on the result of the Nearest Neighbour Algorithm.

The starting city in the Nearest Neighbour Algorithm can significantly impact the final tour length. Since the algorithm is greedy and makes local decisions based on the current city, different starting cities can lead to different tours with varying total distances.

The final result is sensitive to the starting point, and the algorithm might not find the shortest possible tour due to this dependency.

Q4- How does the Nearest Neighbour Algorithm compare to other heuristic methods for solving the TSP, such as the Christofides Algorithm?

The Nearest Neighbor Algorithm is simpler and faster than other heuristic methods like the Christofides Algorithm, which provides better approximations for the TSP.

The Christofides Algorithm guarantees a solution within 3/2 times the optimal tour length, whereas the Nearest Neighbor Algorithm may produce solutions that are significantly longer than the optimal.

While Nearest Neighbor is easier to implement, Christofides offers better performance for finding near-optimal solutions.

Q5- What are some limitations of the Nearest Neighbor Algorithm?

Limitations of the Nearest Neighbor Algorithm include:

Suboptimal Solutions – It often produces solutions that are not the shortest possible tour.

Dependence on Starting Point – The result varies depending on the starting city.

Local Optima – The algorithm may get stuck in local optima without considering the global context.

No Optimality Guarantee – There is no guarantee that the tour will be the shortest one.

Q6- Can the Nearest Neighbor Algorithm be improved to provide better solutions for TSP?

Yes, the Nearest Neighbour Algorithm can be improved by combining it with other methods.

For example, it can be used as a starting point for more advanced algorithms like 2-opt or 3-opt, which refine the initial tour by making local improvements.

Additionally, hybrid approaches that use Nearest Neighbour with optimization techniques or metaheuristics like simulated annealing or genetic algorithms can yield better solutions.

Q7- What is the time complexity of the Nearest Neighbor Algorithm for the Traveling Salesman Problem?
  1. O(nlog n)
  2. O(n2)
  3. O(n3)
  4. O(2n)

Ans – (2)

Explanation – The time complexity is O(n2) due to the need to search through all unvisited cities for each city in the tour.

Q8- In the Nearest Neighbour Algorithm, what is the role of the ‘visited’ array?
  1. To store the distances between cities
  2. To keep track of the total distance travelled
  3. To mark cities that have already been included in the tour
  4. To calculate the optimal tour length

Ans – (3)

Explanation – The ‘visited‘ array keeps track of which cities have been visited to avoid revisiting them.

Q9- What is the primary disadvantage of the Nearest Neighbour Algorithm?
  1. It is too complex to implement.
  2. It always produces the optimal solution.
  3. It may get stuck in local optima.
  4. It has a high time complexity.

Ans – (3)

Explanation – The algorithm may not find the optimal tour due to making only local optimal choices.

Q10- Which of the following is not a method to solve the Traveling Salesman Problem?
  1. Brute Force
  2. Nearest Neighbor Algorithm
  3. Genetic Algorithm
  4. Dijkstra’s Algorithm

Ans – (4)

Explanation – Dijkstra’s Algorithm is used for shortest path problems, not TSP.

Q11- In which case is the Nearest Neighbour Algorithm most likely to perform well?
  1. When the number of cities is very large.
  2. When exact optimal solutions are required.
  3. When a quick, heuristic-based solution is acceptable.
  4. When dealing with complex constraints.

Ans – (3)

Explanation – The Nearest Neighbour Algorithm is useful when a fast, heuristic solution is sufficient.

Q12- What happens if there are ties in the distance between cities in the Nearest Neighbor Algorithm?
  1. The algorithm terminates.
  2. The nearest city is chosen randomly.
  3. All tied cities are visited simultaneously.
  4. The algorithm starts over from the beginning.

Ans – (2)

Explanation – If there are ties in distance, the algorithm typically selects one of the tied cities randomly.

BOOKS

Algorithm T H CoremanAlgorithm by Udit AgarwalAlgorithm Ellis HorowitzData Structure & Algorithm Made Easy Narasimha