问题
I am trying to use Java to implement the nearest neighbour algorithm for the travelling salesman problem. Before I knew precisely what I was looking for, I asked a similar question here, but received no assistance. I am starting with a matrix table[i][j] == table[j][i]
of doubles, representing the distance between each node. I want to use dynamic programming to find the shortest path starting from each node back to itself by travelling to every other node once.
I have done a significant amount of research, but none of the websites for "nearest neighbour algorithm for travelling salesman problem" present a clear Java implementation that is explained to a novice such as myself. I don't just want an implementation; I want to understand the implementation.
I am using this video as a reference for this algorithm. I have watched the video many times, and I understand it. However, all of my attempts at an implementation have been a failure. Since we are iterating over a matrix, it seems to me that we require two for
loops, which then implies a worst-case time complexity of O(n^2).
All of my attempts have resembled something like the following:
public int[] test(double[][] table)
{
for (int i = 0; i < table[0].length; i++) {
for (int j = 0; j < table[0].length; j++) {
if () {
}
}
}
}
I have been totally unable to make any progress on this. Regardless of how many videos I watch of the algorithm, I am seemingly unable to understand how to implement it.
来源:https://stackoverflow.com/questions/63427956/java-implementation-of-nearest-neighbour-algorithm-for-the-travelling-salesman-p