Implementation of a particular Travelling-Salesman variation

自闭症网瘾萝莉.ら 提交于 2019-12-06 16:32:18

An easy reduction of the problem to TSP would be:

  1. For each (u,v) that "must be visited", find the distance d(u,v) between them. This can be done efficiently using Floyd-Warshall Algorithm to find all-to-all shortest path.
  2. Create a new graph G' consisting only of those nodes, with all edges existing, with the distances as calculated on (1).
  3. Run standard TSP algorithm to solve the problem on the reduced graph.
Jared Windover

I think that in addition to amit's answer, you'll want to increase the cost of the edges that have A or B as endpoints by a sufficient amount (the total cost of the graph + 1 would probably be sufficient) to ensure that you don't end up with a path that goes through A or B (instead of ending at A and B).

A--10--X--0--B
|      |     |
|      10    |
|      |     |
+---0--Y--0--+

The above case would result in a path from A to Y to B to X, unless you increase the cost of the A and B edges (by 21).

A--31--X--21--B
|      |      |
|      10     |
|      |      |
+---21--Y--21-+

Now it goes from A to X to Y to B. Also make sure you remove any edges (A,B) (if they exist).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!