0

I have a complete weighted graph with 1000 nodes and need to find the longest possible Hamiltonian path in the graph (the sequence of nodes, to be more precise). I am supposed to fit in 5 sec (for Java), the memory limit is big enough. Finding the longest Hamiltonian path doesn't look much different from finding solution for TSP (travelling salesman). Of course, an optimal solution is out of question, so I'm looking for a good heuristic. My best solution so far is using the Nearest Neighbour algorithm, which is easy to implement and runs in polynomial time (takes ~0.7 seconds for 1000 nodes graph). It's a bit far from the optimal solution though. So I'm looking for a better heuristic that still runs relatively fast. I see mentioned Tabu Search, Simulated Annealing, Ant Colony, Genetics, Branch and Bound, MST based algorithms and others. The problem is, as their implementation is not exactly trivial, it's hard to find their time complexity to decide which can fit in the 5 sec. time limit; e.g. run in polynomial time. For some algorithms like Christofides' I see that the complexity is O(V^4), where V is the number of vertices, which apparently makes it impossible to fit.

I came across the Bitonic Tour solution, usually used for finding the shortest Hamiltonian path in Euclidean graphs, but seems kind of OK for finding the longest path in non-Euclidean graphs too:

    public static void minCostTour(int[][] graph) {
        int n = graph.length;
        int[][] dp = new int[n][n];
        dp[0][1] = graph[0][1];

        for (int i = 0; i < n - 1; i++) {
            for (int j = i + 1; j < n; j++)
                if (i == (j - 1) && i != 0) {
                    dp[i][j] = dp[0][j-1] + graph[0][j];
                    for (int k = 1; k <= j - 2; k++)
                        if ((dp[k][j-1] + graph[k][j] < dp[i][j])) { 
                            dp[i][j] = dp[k][j-1] + graph[k][j];
                        }
                } else if (i != 0 || j != 1) {
                    dp[i][j] = dp[i][j-1] + graph[j-1][j];
                }
        }
        System.out.println("Optimal Tour Cost: " + (dp[n-2][n-1] + graph[n-2][n-1]));
    }

The standard algorithm includes an initial sorting of coordinates, which I skipped, as apparently there are no coordinates to sort (the graph is non-Euclidean). This dynamic programming solution runs in O(V^2) so it might be good. The problem is that it outputs the Hamiltonian path length and I need the sequence of nodes. I can't really understand how to restore the path from the above algorithm (if possible at all).

TL DR version: Can the Bitonic Tour algorithm above be used for finding the sequence of nodes on the longest Hamiltonian path in a complete weighted graph? If not, can you recommend an algorithm with similar (polynomial) time complexity for that task?

Rnam
  • 65
  • 1
  • 7
  • 1
    You can't compare the run time of dissimilar algorithms by comparing the O() of an algorithm. An O(n^4) algorithm that executes in the on-chip-cache and which has a favorable memory access pattern can smoke a more complicated O(n^2) algorithm that doesn't benefit from any (execution or data) caching. For some, like simulated annealing you can, in any case, tune the run time to be precisely 5s at the expense of quality. – Ian Mercer Jun 11 '19 at 20:47
  • Sure, you're right, but in my case all algorithms will run on my laptop in similar conditions. About the Simulated Annealing do you think 5 seconds of running can wield better results compared to e.g. nearest neighbour? – Rnam Jun 11 '19 at 20:55
  • 1
    All you need to do is store the "came from node" information when updating the `dp` table. – user3386109 Jun 11 '19 at 21:04
  • It isn't about running under same conditions, it's whether the algorithm loop fits in the on-chip-cache and how it accesses memory. Two algorithms on same hardware may have wildly different performance depending on that http://i.imgur.com/k0t1e.png and 1000^2x as many operations may be dwarfed by these factors. – Ian Mercer Jun 11 '19 at 22:06
  • You best option would be to implement a few and compare unless someone has already done it and can offer benchmarks on similar hardware. – Ian Mercer Jun 11 '19 at 22:07
  • Thanks for the suggestion, but can you be more specific how to update the came from node? – Rnam Jun 11 '19 at 22:07
  • 1
    There is a (small) body of literature on "maximum TSP", e.g., https://www.sciencedirect.com/science/article/pii/S0020019000000971. Maybe that will help? – LarrySnyder610 Jun 12 '19 at 00:20

0 Answers0