17
0
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
What if answer do extra work that make complexity larger but smaller on the
n
? – l4m2 – 2018-12-10T13:54:44.357require a,b,k>=0? – l4m2 – 2018-12-10T13:56:09.750
@l4m2 yes`````` – PyRulez – 2018-12-10T13:57:29.787
So is O(2.00000000000000000000000001^n) fine lol with extra code – l4m2 – 2018-12-11T04:42:33.647
4But how do you say someone's code is
O(n!)
but notO(sqrt(n)*n^n/e^n)
norO(n!/100000000000000000000)
? – jimmy23013 – 2014-06-16T22:09:25.6201@user23013 One solution is to say the only options are
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. – isaacg – 2014-06-16T22:50:24.3102@Geobits As shown in the comic, the dynamic programing solution is
O(n^2*2^n)
, which is much less thanO(n!)
for large n. – isaacg – 2014-06-16T22:52:00.273@proud haskeller okay (it's actually been out for a while and i just wanted to accept it because it was the best despite having nearly no votes, but if you get something better go ahead.) – PyRulez – 2014-09-25T22:12:29.943
@PyRulez well whatever I try I convince myself it has complexity of O(n!)... it's complex – proud haskeller – 2014-09-25T23:16:15.213
well now I'm convinced my code has complexity of O(n^2*2^n) and I Just noticed that the solutions that were shorter had worse complexity, so I have the best score overall! – proud haskeller – 2014-09-25T23:31:53.870