0

Recently, I participated in a coding competition. The problem sounded like TSP. I am not looking for a solution here. I just want to find out if the problem was a variant of TSP and if it was solvable.

Here is the summary:

We were given a Complete weighted graph. Each city (vertex) was connected with remaining cities. All connecting distances were positive integers. The graph was undirected.

Problem was to compute maximum total distance to visit all cities. No cycles allowed. No starting point provided. One peculiar thing was that once we travel from A to B with cost c, immediate return to A from B was allowed with 0 cost. For example, this is a valid path. A --> B --> A --> C --> D

Constraints: No. of Cities <= 100000 No. of Paths <= 1000000 Distance between cities <= 1000

Time limit for a single test case 0.5 seconds.

P.S. : I used nearest neighbors algorithm, passed sample test cases but couldn't meet the 0.5 second time limit for larger test cases.

Ravi M Patel
  • 2,905
  • 2
  • 23
  • 32
  • Would this be a valid path? A -> B -> C -> B -> A? Or were you only allowed to take one step back immediately after taking a step, but never again afterwards? – orlp Feb 14 '21 at 21:42
  • Because if you're allowed to backtrack at any point in time, the answer would be a maximum spanning tree - I think it's likely they were looking for that. – orlp Feb 14 '21 at 21:46
  • I agree with @orlp. This is a maximum spanning tree problem. As you are asked `to compute maximum total distance to visit all cities`. Sort all the edges based on the decreasing order of the edge weight. Then loop over the edge list and consider an edge as part of the solution if it connects two nodes that are in different component. – biqarboy Feb 14 '21 at 22:57
  • @orlp, A -> B -> C -> B -> A would be a cycle and that is not allowed. Otherwise you could go in loop and max cost would be infinite. – Ravi M Patel Feb 16 '21 at 09:46
  • @RaviMPatel No, the backtracking edges would have cost 0, like you described. Why is A -> B -> C -> B -> A a cycle but A -> B -> A isn't? – orlp Feb 16 '21 at 09:46
  • @orlp, you are right, cost would be 0, but it was clearly mentioned that cycles were not allowed. – Ravi M Patel Feb 16 '21 at 09:47
  • @biqarboy, if this is Max spanning tree, if we just invert the weights, use your method, we can find min cost path and that would give us solution to TSP!! – Ravi M Patel Feb 16 '21 at 10:01
  • @RaviMPatel This isn't TSP depending on how the 'no cycles allowed' is interpreted. The problem is badly defined. A -> B -> A is a cycle. So why would that be allowed, but A -> B -> C -> B -> A isn't? You're only going backwards on the path you've already done. If you're allowed to backtrack as far as needed, then you end up with a maximum spanning tree. Considering the time limit and sizes of graphs involved I think it's the only reasonable explanation. – orlp Feb 16 '21 at 10:07
  • @orlp, thank your for your insights. When you say problem is badly defined, are you referring to my post or the link I have shared with you? – Ravi M Patel Feb 16 '21 at 10:13
  • @RaviMPatel Both. The simple sentence 'Note: No cycle is allowed.' is contradictory with the previous story about how 'travelling back has no cost'. – orlp Feb 16 '21 at 10:13
  • @orlp, ok, i had no option to post what was asked, otherwise it would defeat the purpose of the post. As you can see, "No cycles allowed" is clearly mentioned in the problem statement. I didn't want to share the link as the competition was live at the time, but this ruined my Sunday so wanted to know where i can correct myself. Thanks for your time. – Ravi M Patel Feb 16 '21 at 10:16

0 Answers0