现有算法可在非加权图上找到最短路径?

时间:2019-04-04 04:56:59

标签: algorithm vertices

我的任务是编写一个程序,该程序能够找到从一个顶点到另一个顶点的最短移动量。我对于“图形”的唯一数据是顶点链接到什么顶点,没有权重,距离等。

我必须解析输入以最初找到这些链接。此输入最多可以具有1,000,000个顶点。我已经完成了。

我研究了与Dijkstra的算法,Floyd的算法甚至尝试Q学习类似的算法。 Dijkstra和Floyd的算法都依赖于顶点之间的距离,而Q学习在处理成千上万的潜在状态和动作时似乎并不是最实用的方法。不仅如此,程序还必须在提供输入后2秒钟内弄清楚路径,从而裁定任何强化学习都完全无用-除非算法可以在2秒钟内训练成千上万的数据。

我可以使用任何现有算法来实现此目标吗?如果我必须编写自己的指南,这是我应该遵循的一般准则吗?

1 个答案:

答案 0 :(得分:0)

如果给出的图形绝对没有指示每个顶点到末端的距离,我建议使用类似于Dijkstra's的图形。该算法将如下所示:

The Node object should contain:
    LINKEDNODES = Array of linked nodes
    TRAVELLED = Integer for minimum number of nodes traversed from starting node, initialized as -1
NODES = A given array of nodes
START = the starting node
GOAL = the goal node
QUEUE = Create a priority queue that holds nodes and prioritizes based on their associated TRAVELLED
Give START a TRAVELLED value of 0

MAIN LOOP:
Check front of QUEUE, for each linked node:
   If the node is GOAL then set its TRAVELLED to current node + 1, and go to the next phase RETRACE
   Else If the node's TRAVELLED is -1
   Then set it's TRAVELLED value to current node + 1, and add it to QUEUE
   Otherwise, ignore it, since we don't want to check the same nodes twice

RETRACE:
   CNODE = Current node being checked
   PATH = an array of nodes, of size GOAL.TRAVELLED
   Set CNODE to GOAL
   Repeat until CNODE.TRAVELLED is 0 (which is START's TRAVELLED):
      Add CNODE to PATH
      Set CNODE to the LINKEDNODE of CNODE that has a TRAVELLED of CNODE.TRAVELLED - 1

关于第二个问题,我希望您有一台快速的PC!