1


I'm trying to develop a Travelling Salesman-type program (in Java) and am trying to figure out the logic for a certain part, a brute-force approach for calculating the most efficient route between a set of nodes (Cities) where each node is touched only once.

I've defined a City class with the x/y co-ords and have an array of City instances, and a grid class to draw the grid. Cities are visible on a grid, and numbered 0-i for their index in the City array
(In the sample grid there are 4 cities defined):

· · · · · · · · · · · · · · · ·
· · · · · · · · · 1 · · · · · ·
· · 0 · · · · · · · · · · · · ·
· · · · · · · · · · · 3 · · · ·
· · · · · · 2 · · · · · · · · ·
· · · · · · · · · · · · · · · ·
· · · · · · · · · · · · · · · ·

There are many routes visible already (I believe 4! = 24):

  • 0-1-2-3
  • 0-2-1-3
  • 2-0-1-3
  • 2-3-1-0
  • etc..

Is there a simple iterative/recursive method to obtain every possible path, given an array of cities and their co-ordinates, which I can use to determine the distance and list the most efficient route(s)?

baharini
  • 217
  • 2
  • 11

1 Answers1

1

it's Faculty(n)/2 if your distances are undirected or Faculty(n) if your connections are directed (meaning: distance a-->b differs from b-->a). It's called Permutation

dont forget 13! = 6227020800 and more than Integer.max_value, even 13! / 2 is more!

Martin Frank
  • 3,445
  • 1
  • 27
  • 47
  • Thank you Martin! I got it working by implementing a "base-n" function with each "digit" an item in an integer array being the node number; the base n is the number of nodes. I ignore any array which contains the same "digit" (node number) twice - therefore ending up with arrays containing different orders of different nodes, and distance calculation and sorting was straightforward. – baharini Jun 06 '16 at 16:29