0

I want to write a java program that solves the traveling salesman problem with the nearest neighbor heuristic. The steps of the algorithm would be something like this:

Step 1: Start with any random vertex, call it current vertex

Step 2: Find an edge which gives minimum distance between the current vertex and an unvisited vertex, call it V

Step 3: Now set that current vertex to unvisited vertex V and mark that vertex V as visited

Step 4:Terminate the condition, if all the vertices are visited atleast once

Step 5: Go to step 2

I have problems to figure out which data structures would be suitable to solve this problem. How can I identify the node with the smallest distance to the current node (step 2)? Would a priority queue be a good choice? What other data structures could I use? How can I make sure, that the second last node in the tour connects to the start node? Should I use a stack? What else could I use?

Jul
  • 141
  • 13

1 Answers1

0

For solving travelling salesman with optimal (best) solution you would have to generate all possible paths in your graph and find the path with least cost (least sum of edges between vertices). Nearest neighbour approach which you are describing is close to using Greedy alrogithms. Assuming you have n vertexes in your graph and every vertex is connected you could solve travelling salesman problem using greedy strategy/nearest neighbour approach following these steps( assume that you have a Set of Vertex class objects) :

  1. Create a Set of initial unvisited vertices.
  2. Create a Map like Map<Vertex, Integer> where the key is starting vertex and value is overall path cost.
  3. Create a Map with key, which is starting vertex of the path and value is List<Vertex> which represent the path (Map<Vertex, List<Vertex>>).
  4. Pick one random vertex which has not been picked before (is present in Set form step 1).
  5. Pick vertex that is closest to vertex picked at step 4 (you will have to check edges cost to all unvisited vertices and pick the least one). Add vertex to the List<Vertex> associated with key which is current starting vertex (picked in step 4) and stored in Map created at step 3. (Note : here you will take into consideration all vertexes different than vertex picked at step 4).
  6. Increase path cost value associated with current staring vertex, that was picked at step 4, which is stored in Map created at step 2.
  7. Repeat steps 5-6 until there is no vertex left to visit, by checking closest edge to the current last vertex (You will have to visit all vertexes starting at vertex picked at step 4).
  8. Remove starting vertex, picked at step 4, form Set created at step 1.
  9. Repeat steps 4-8 until you have no vertex left to pick in step 4.

Then you could sort Map created at step 2 by value and pick the entry with lowest cost. Then you could get it's key and pick the path from the Map created at step 2 by previously picked key, associated with lowest overall cost. It would be the best path calculated with this algorithm.

Michał Krzywański
  • 15,659
  • 4
  • 36
  • 63
  • How can I check the cost of the edges (step 5) to all unvisited vertices and pick the least one? Do I need to create a method for the comparison of the costs or is there a datastructure in java which provides methods for this? – Jul May 08 '19 at 20:28
  • @Jul The edge cost matrix is the essentially the TSP problem instance. Take that matrix `C=(c_ij)` denoting the cost of edges `(i,j)` and sort each row by increasing cost in `O(n log n)` (for a TSP on `n` nodes). Since within each row you will only move in single steps between adjacent elements, sophisticated data structures beyond an array won't help you here. Constraints on the cost matrix often imply more efficient data structures, eg. in metric and Euclidean spaces you might want to use kd-Trees. – collapsar May 15 '19 at 12:40