Dijkstra's (or other shortest path algorithm) where end node can be start node

时间秒杀一切 提交于 2020-01-07 07:11:13

问题


The traditional* implementation of Dijkstra's does not handle this case well. I think I've come up with some solutions that will work, but they are not particularly elegant**. Is this is a known problem with a standard solution?

This is assuming the non-trivial solution i.e. a path like A->B->C->A rather than just A->A.

* When I say traditional I mean marking each node as visited.

** Storing the number of times each node has been visited and basing the terminating conditions on whether the end node is the start node.


回答1:


Split A into two nodes, called START and GOAL.

For any edge A->x add an edge START->x

For any edge y->A add an edge y->GOAL

Keep all other edges unchanged.

Then use normal Dijkstra to solve for the path from START to GOAL



来源:https://stackoverflow.com/questions/25408477/dijkstras-or-other-shortest-path-algorithm-where-end-node-can-be-start-node

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