Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Minimal Distance Hamiltonian Path Javascript

I know this is a fairly frequent question (tsp in general), but I've been stumped by it for awhile now. I'm looking to find the minimal distance hamiltonian path given a set of x,y coordinates. The start and end point are completely arbitrary but it must NOT cycle, so standard tsp is out (although supposedly adding a dummy point at 0 distance to all other nodes and then removing it later works, i have no idea how I'd do that).

There are plenty of links to math papers and the like discussing algorithms to solve similar problems, but I'd much rather work with code than complex equations and i'd really rather not reinvent the wheel.

Surely there is a fairly straightforward implementation in a major language java,c#,c++,ruby,javascript,php,etc that can solve a ~20 node version of my problem.

Edit: I'm also looking for as accurate as possible, obviously it can't be completely accurate as 20! is a lot of permutations, but equal to or better than what a human could do in a couple minutes would be perfect.

Edit2: Also to clarify, I'm working with standard 2d coordinates on an unweighted graph. The distance A->B == B->A

Edit3: To eliminate confusion, here's a visual example with just a few points to show how tsp can be suboptimal (this case is an easy fix but with more nodes it can be more extreme).

TSP Minus Longest Segment (red line)

TSP Minus Longest Segment (red line)

Desired Output

Desired Output

like image 263
stumpedcoder Avatar asked Sep 07 '11 23:09

stumpedcoder


1 Answers

You can solve the bitonic euclidean traveling-salesman problem. Is a simplified version of the tsp solvable through dynamic programming in O(n^2): http://en.wikipedia.org/wiki/Bitonic_tour

like image 180
Simone Avatar answered Oct 26 '22 22:10

Simone