You are given, as a list or vector or whatever, a bunch of 3-tuples or whatever, where the first two things are strings, and the third thing is a number. The strings are cities, and the number is the distance between them. The order of the cities in the tuple is arbitrary (i.e. it doesn't matter which comes first and which comes second) since it is the same distance each way. Also, there is exactly one tuple for each pair of connected cites. Not all cities may be connected. Also, the distance is always positive (not 0
). You do not need to check these conditions, you may assume input will be well formed. Your job is to return the cities in a cyclic sequence, such that, if you start at any one city, and go around the sequence back to the same city, the total of the distances between the cities will be minimum (exactly and in all cases.) You may assume a solution exists. For example, let us say you are given
[("New York", "Detroit", 2.2), ("New York", "Dillsburg", 3.7), ("Hong Kong", "Dillsburg", 4), ("Hong Kong", "Detroit", 4), ("Dillsburg", "Detroit", 9000.1), ("New York", "Hong Kong", 9000.01)]
You could output any of the following (but you only need to output one):
["Detroit","Hong Kong","Dillsburg","New York"]
["Hong Kong","Dillsburg","New York","Detroit"]
["Dillsburg","New York","Detroit","Hong Kong"]
["New York","Detroit","Hong Kong","Dillsburg"]
["Dillsburg","Hong Kong","Detroit","New York"]
["New York","Dillsburg","Hong Kong","Detroit"]
["Detroit","New York","Dillsburg","Hong Kong"]
["Hong Kong","Detroit","New York","Dillsburg"]
because it is the shortest trip: 13.9
but not
["Dillburg","Detroit","New York","Hong Kong"]
because it is not the shortest.
See en.wikipedia.org/wiki/Travelling_salesman_problem
Scoring
This is where it gets interesting. You take the number of characters you have, and then plug them into the bare worst case O-notation formula. For example, let us say you write a brute force program that is 42 characters. As we all know, the worst case is n!
where n
is the number of cities. 42!=1405006117752879898543142606244511569936384000000000, so that is your score. The lowest score wins.
Note: I relieved this afterwards too, but wasn't sure how to solve it and hoped that no one would notice. People did, so I will go with issacg's suggestion:
the only options are O(n!) and O(b^nn^aln(n)^k), and all bounds must be as tight as possible given that notation
O(n!)
but notO(sqrt(n)*n^n/e^n)
norO(n!/100000000000000000000)
? \$\endgroup\$O(n!)
andO(b^n*n^a*ln(n)^k)
, and all bounds must be as tight as possible given that notation. OP should clarify, though. \$\endgroup\$O(n^2*2^n)
, which is much less thanO(n!)
for large n. \$\endgroup\$