How to fix the start and end points in Travelling Salesmen Problem?

Below is a visualization of the "dummy node" concept. On the left is a normal TSP with the same start and end node, A, and the optimal solution [A, B, E, D, C, A]. On the right is the same TSP but where the start node is A and the end node is E. Its optimal solution [A, B, C, D, E] clearly has nothing to do with the one in the normal case. The way we can find that solution is by "hacking" the distance matrix of the TSP graph. At the bottom of the distance matrix the dummy node is inserted and its distances to node A and E are set to 0 and its distances to all other nodes are set to inf. When the solver then tries to search through the distance matrix to find the optimal sequence of nodes A, DUMMY, E will stay together, e.g. [A, B, C, D, E, DUMMY, A] and this can then be cleaned up to give [A, B, C, D, E].

enter image description here

PS. note that this type of hack can have a severe impact on an exact solver's performance. Exact TSP solvers are set up with various geometric heuristics and putting in zero and inf distances clearly messes with that. I e.g. tried this for Concorde and it was not very happy about it and needed much more time to find optimal solutions sometimes. Didn't find any documentation for it to deal with this specific case but maybe there are other exact solvers that have capability to handle this specific condition.


You can add a dummy node, which connects to start and end node with edges with weight 0. Since the TSP must contain the dummy node, the final result must contain the sequence start - dummy node - end (there is no other way to reach the dummy node). Therefore, you can get the shortest Hamilton path with specified start and end node. This solution should work even if the edges in the graph are negative.