-2

i have seen many solution that try to solve the problem of travelling salesman problem if p!=np ,but i want to know the optimum solution if there is 6 or 5 cities only,which algorithm will give the optimum solution in this case?

1 Answers1

1

For 6 cities, compute the 15 inter-city distances once for all, then choose a starting point and evaluate the possible 5!/2=60 cycles (half of them are identical by direction reversal).

For maximum efficiency, you could hard-code the permutation table. Another possible measure is to arrange the cycle length computation in such a way that some partial sums can be reused, also with the help of hard-coded tables.

Premature abortion of some summations is possible as soon as they exceed the current shortest. It is likely that some more can be gained by trying the shortest segments in the first place.

A thorough exploration of these topics seems an endeavour, and the savings are probably not worth the care, unless you have millions 6-cities problems to solve.

  • Yes, big-oh is only relevant if one reaches a threshold, as is micro-optimisation. – Deduplicator Jan 26 '19 at 14:27
  • @Deduplicator: I have the feeling that by combining some micro-optimizations a factor 2 to 4 could be achieved. Depending on context, the effort may make sense or not. –  Jan 26 '19 at 14:44
  • And you can solve these kind of problems easily with Genetics-Algorithm. – Khan Saab Jan 26 '19 at 20:08
  • @KhanSaab: for small-sized problems, the genetic approach is quite inappropriate. Read the question. –  Jan 27 '19 at 15:47