views:

295

answers:

3

Hi all !

Here is my problem : I have an array of points, the points have three properties : the "x" and "y" coordinates, and a sequence number "n". The "x" and "y" are defined for all the points, the "n" are not. You can access and write them calling points[i]->x, points[i]->y, points[i]->n. i.e. :

points[i]->n = var
var = points[i]->n

So the title maybe ruined the surprise, but I'm looking for a possible implementation of a solution to the Hamiltonian path problem : I need to set the "n" number of each point, so that the sequence is the shortest path (not cycle, the edges have to be disjoint) that goes exactly once through each point. I looked for a solution and I found The Bellman Ford Algorithm but I think it doesn't work since the problem doesn't specify that it has to go through all of the points is it correct ?

If it is, does somebody has another algorithm and the implementation ? If the Bellman Ford Algorithm works, how would I implement it ?

Thanks a lot,

Julien

Edit : the problem is that I have to recreate a list of geographic points which represent bus stops, and I have to figure out a realistic sequence. Performance is not important at all since the goal is only to populate a database.

Edit : Here is a picture :My Hamiltonian Path Problem

+2  A: 

The Bellman-Ford algorithm is often used to solve single-source shortest path problems.

The shortest Hamiltonian Path problem belongs to a class of problems known as NP-hard. This means that you have to try all of the permutations to guarantee to find the shortest path. This approach is only practical for small problems, within a normal human life span.

You can use the Bellman-Ford algorithm to produce a set of single source shortest paths for each node to help you to solve the problem, but you might find the Floyd-Warshall algorithm preferable. Floyd-Warshall will give you the shortest path from every node in the graph to each of the other nodes. It's an O(N^3) algorithm with O(N^2) memory requirements.

Once you have the shortest paths between the nodes then you can either try all of the permutations to find the shortest Hamilton Path, or you can use a heuristic algorithm to start from a basic feasible solution and iterate to an approximation of the shortest path.

I've had some success with Simulated Annealing and Tabu Search. There was also a rather interesting approach based on ant trails that was published around the last time I had to solve this type of problem.

Good luck.

richj
Thank's for the help, it's very interesting, the problem is that I don't understand the vocabulary of the graph theory, so I can't figure out how i can implement the pseudo-code found on : http://en.wikipedia.org/wiki/Floyd–Warshall_algorithm could you help ? I'm editing my question to make it more accurate, will be done in minutes. Thank you
Julien
The "Implementations" section in that same article has some good links. You didn't mention which language you are using, but from the "->" I would guess at either C or C++. The Wikipedia article links to an implementation by Joshua Robinson, and indicates that the Boost Graph Library contains a C++ implementation. It would take me several days to produce a commercial quality C implementation from scratch, and I've done it once before - so don't feel bad if it takes you some time.
richj
+1  A: 

The Hamiltonian path problem is NP-complete, so obviously you can't find a solution using the Bellman-Ford algorithm, which is polynomial.

You can use the following steps to solve the Hamiltonian Path Problem:

  • First transform the Hamiltonian Path Problem into the Hamiltonian Cycle Problem. To achieve this, add a vertex to your initial graph and connect it to all existing vertices.

  • Then transform the Hamiltonian Cycle Problem into the Travelling Salesman Problem, by creating a complete graph and assigning weight = 0 to the edges present in the previous graph and weight = 1 to the rest of the edges.

  • Finally, solve the TSP by using a known algorithm (e.g. dynamic programming).

Edit: I just realised that you seek the shortest path. With the above you can only answer if a graph does have a Hamiltonian path (and probably find one).

3lectrologos
+3  A: 

This is called Euclidean Travelling Salesman (in 2-dimensions) and is also NP-Complete like TSP.

The other answers are inaccurate as they are doing the opposite: reducing your problem to Hamiltonian Path, while it should be the other way round, to show NP-Completeness. Sorry to say this, but it seems to be a pretty common problem on this site.

We can say that this fundamentally differs from the normal TSP in the following sense:

If P != NP,

There are other algorithms which guarantee 2-approximation (using Minimum Spanning Trees) and 3/2-approximation and might be simpler. Arora's paper mentions those and you should be able to track down using the references in Arora's paper.

Moron
Oh thank's ! I'll study that
Julien
@Julien: The implementation you need depends on what approximation are you willing to live with, as trying to get the optimal can quickly get out of hand (as the number of points increases).
Moron
I think even checking for the existence of a Hamiltonian cycle in a graph is NP complete.
ldog
@gmatt: Yes the decision problem of Hamiltonian Path/Cycle is NP-Complete. In fact that was one of the first few graph problems to have been proven NP-Complete. The problem OP is asking is different though, it is a very specific case of the search version of the weighted Hamiltonian Path problem, and has also been shown to be NP-Complete.
Moron