Message Boards Message Boards

4
|
10709 Views
|
7 Replies
|
5 Total Likes
View groups...
Share
Share this post:

Shortest hamiltonian path

Posted 9 years ago

Mathematica surprisingly doesn't have a built in find shortest path function for unspecified ends. We do however have FindShortestTour.

I was unimpressed by stack exchanges recommendation of 'try most possibilities'.

A shortest path is essentially a shortest tour where we visit an extra point once. This extra has to be equidistant from all the actual points, so that the path can begin and end anywhere. Visiting the extra must be prohibitively costly, such that it's never a short cut between our actual points. Then at the end we cut the extra out so we have a path through our points, rather than the returned cycle.

As FindShortestTour takes custom distance functions, we can build a function like this easily. Notably this is faster than constructing and computing the graph equivalent.

This code specifies a distance function that is euclidean for numeric coordinates, and 10^10 for symbolic coordinates (appropriate for my application). This distance function is used to find the shortest cycle through a set of points and a symbolic extra, and then the result is cut back to the shortest path.

PuncturedDistance[a_, b_] := 
 If[And[NumericQ[a[[1]]], NumericQ[b[[1]]]], EuclideanDistance[a, b], 
  If[Xor[NumericQ[a[[1]]], NumericQ[b[[1]]]], 10^10, 0]]
findShortestTour[set_] := (
  tour = FindShortestTour[Append[set, {p}], 
    DistanceFunction -> PuncturedDistance][[2]];
  Join[tour[[ Position[tour, Length[set] + 1][[1, 1]] + 1 ;;]],
    tour[[2 ;; Position[tour, Length[set] + 1][[1, 1]] - 1]]])
POSTED BY: David Gathercole
7 Replies

I was less than clear. I tried it using the set of vertex coordinates from CompleteGraph[6] (which are just points in the plane). Also I should add that the hang I encountered is due to a bug at our end, which I have reported.

POSTED BY: Daniel Lichtblau
Posted 9 years ago

Here, edge weights are distances between vertices, not vertex coordinates. You could set EdgeWeights instead of setting DistanceFunction.

POSTED BY: Jaebum Jung

!!

As in my other reply, I was working to get FHP functionality on a set of Euclidean points. Further, I don't think my copy of Mathematica has FHP, my googling had specified FHP as a long standing omission.

POSTED BY: David Gathercole

I put this together needing a FindShortestTour equivalent for points in Euclidean space. Clearly that route blinded me to FSTs conventional use in a graph context! This is completely untested on graphs and likely doesn't work.

In the Euclidean context, p is an unspecified (non numeric) symbol, which triggers the exception in the distance metric.

POSTED BY: David Gathercole

What is p? Should it be something like {10^10,10^10} (assuming vertices have coordinates substantially smaller in magnitude than that)?

I should add that every variation of this example I have tried hangs on the vertex set from CompleteGraph[6]. What examples were used in testing this code?

POSTED BY: Daniel Lichtblau

The original post and this response are both interesting. The use of set needs some clarification though. Here is my understanding: it is intended to be the list of vertex coordinates.

set = 
 VertexCoordinates /. 
  AbsoluteOptions[CompleteGraph[6], VertexCoordinates]

(* Out[161]= {{-0.866025403784, 
  0.5}, {-0.866025403784, -0.5}, {3.82856869893*10^-16, -1.}, \
{0.866025403784, -0.5}, {0.866025403784, 0.5}, {1.83697019872*10^-16, 
  1.}} *)

FindHamiltonianPath[CompleteGraph[6], 
 DistanceFunction -> (EuclideanDistance[set[[#1]], set[[#2]]] &)]

(* Out[162]= {2, 1, 6, 5, 4, 3} *)
POSTED BY: Daniel Lichtblau
Posted 9 years ago

Or you could do:

FindHamiltonianPath[CompleteGraph[6],  DistanceFunction -> (EuclideanDistance[set[[#1]], set[[#2]]] &)]
POSTED BY: Jaebum Jung
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract