# [Community Puzzle] The Travelling Salesman Problem

Coding Games and Programming Challenges to Code Better

I started the traveling sales man problem.
Here is my solution so far which unfortunately doesnâ€™t work.
What I do : look for closest point to current point , once identified look for the closest point to the last visited city.
I donâ€™t think that this is the way I am supposed to deal with the problem since I donâ€™t pass any of the tests.
Hints?

The puzzle describes precisely what algorithm should be used. If you introduce any variation, your result will most likely differ from what is expected.
The aim is not to find the best solution to the travelling salesman problem, just to follow the algorithm to the letter.

The greedy algorithm starts at the first input given and always chooses the nearest point next to it. This continues until no points are left and the last point is connected to the first point.

Well, this is my interpretation of the description. What I think I misunderstood is the second sentence:

This continues until no points are left and the last point is connected to the first point.

1 Like

it just means you should process all the points.
At each step you eliminate one more point from the list by connecting it to the previous one.
The last point being connected to the first is just a logical consequence of that (and the whole point of the TSP )
You might want to lookup the travelling salesman problem on wiki, itâ€™s a classic and it is well documented. That might help you grasp the general idea.

So basically, what it is required is for the actual iâ€™th point, identify the i+1 pointâ€™s coordinates , compute the distance and add the result to the sum of the distances?

Basically yes. Thus being said, I find the problem definition pretty vague. Iâ€™d add the following precisions so that people donâ€™t waste time on insignificant details:

• the expected distance includes the return leg (the segment from the last point to the first that closes the path)
• rounding should occur only at the end, Cumulated distance should be computed as floating point and the final result rounded to the nearest integer.
1 Like

I tried what was suggest in the poste above. However I wasnâ€™t even close, so I tried this approach:

Edit: Code removed

Well Iâ€™m relatively new here, but I think posting your code is not the done thing. The point of these challenges is to learn by confronting problems. Iâ€™m all for explaining a problem definition or giving a few hints, but people are supposed to work out the solution by themselves.

This really has me stumped. My code seems to follow the algorithm exactly but gets answers that are very slightly off. For example, on the 5 city test case my calculated distance was off by 2 from the â€ścorrectâ€ť solution. I did the math out by hand, and this agreed with my solution. Has anyone successfully completed this game?

For reference, here is my process:

1. Take all the city coordinates in the order they are input.
2 From the first on the list, find the closest city and calculate the distance.
2. From the second city, find the next closest city (excluding all previously visited cities), find the distance and add it to the combined distance of previous legs.
3. Repeat this process until the Nth city, then reconnect to city number 1.

Sounds like maybe youâ€™re rounding the distances at each step. I used the same algorithm but didnâ€™t do any rounding until the very end, and it worked.

1 Like

Ahh I finally figured it out! I wasnâ€™t rounding at each step, the issue was an indexing error. When connecting back to the first city on the list, my algorithm wasnâ€™t using the proper start point. So instead of connecting city[N] to city[1], it was connecting city [N-1] to city[1] and adding that distance to my total.

I had a mistake in my algorithm as well.

All the information is in the description, I would encourage reading twice.

My mistake, was then when I was compare the next city, I kept comparing distance to the first, not the most recent one that was visited.

Thank you a lot, you saved me at least 30 minuts of debugging.
For those who are very near to solution, choose to never round result (use double during all the â€ścomputingâ€ť).
Use Math.round only at the end (in the sys out).

Bonjour Ă  tous,

Jâ€™ai essayĂ© de rĂ©soudre ce problĂ¨me The Travelling Salesman Problem , mais mes solutions sont infĂ©rieur Ă  celles attendues. Pourtant quand je debug mes rĂ©sultats je suis bien passĂ© dans toutes les villes. Le premier test passe correctement mais les autres pour 10, 12, 14 villes sont donc faux.
Je me demande si le problĂ¨me peux venir du calcul des distances euclidiennes, mais le fait que le premier test soit correct me dit que non.

Voila voila, si quelquâ€™un a une idĂ©e Ă  me proposer, Ă§a mâ€™intĂ©resse.

Merci

Hi everyone,
i have tried to resolve the puzzle The Travelling Salesman Problem, but all my solutions are inferior than expected. However when i debug my results i went well in all cities.
The first test is ok, but the others for 10, 12, 14 cities are wrong.
I ask me if the problem can comes form the calculation of Euclidean distances, but the fact of the first test is ok say me to not.

So, if anyone have an idea to propose me, iâ€™m interested.

Thanks

Maybe you have a rounding problem? Maybe you are rounding the result after each step, instead of rounding the final result?

1 Like

Maybe you have a rounding problem? Maybe you are rounding the result after each step, instead of rounding the final result?

But this is un-clear in the problem description

I donâ€™t know any situation where you must compute a sum step by step and it would be a good idea to round it at each step.

Iâ€™ve witnessed people working in non scientific fields (cough history teachers cough) trying to compute cumulative frequencies like this, ending up with 98% at the end of their last column, and having absolutely no clue of why they wouldnâ€™t get 100% as expected.

But if youâ€™re in the CS field, come on, it shouldnâ€™t even cross your mind to do it that way.

â€śComputer scienceâ€ť doesnâ€™t necessarily mean â€śscienceâ€ť.
Furthermore, being registered on CodinGame doesnâ€™t mean that you are in the computer science field.
And being in the computer science field doesnâ€™t mean that you have any mathematic background.
Thereâ€™s students (even from secondary), hobbyistsâ€¦
So donâ€™t be so patronizing please.

Yeah sorry I didnâ€™t mean to make anyone feel dumb, weâ€™re all here to learn, I just wanted to point out how a bad idea rounding at each step is in general.
But I got a bit carried away ^^

2 Likes