具有容量约束的Bellman-ford算法-Python

时间:2018-11-16 14:18:41

标签: python

我正在实现Bellman-ford最短路径算法。根据源节点和目标节点,它输出最短距离以及通过网络的路径。

现在,我需要向算法中添加一个容量组件。因此,如果需求为2但容量为1,则该路径不再可用。

我最初的想法是为容量添加一个字典,为需求添加一个变量。然后,如果需求超出了节点的容量,则路径的长度将非常大。我在想类似的东西:

WITH RECURSIVE GraphPath AS (
SELECT G2.Node, ARRAY[G1.Node, G2.Node] AS GraphPath /* Start with 2 elements */
FROM GraphNode G1
JOIN GraphNode G2 ON G1.Node + 1 = G2.Node
UNION ALL
SELECT N.Node, P.GraphPath || N.Node
FROM GraphNode N
JOIN GraphPath P ON N.Node = 1 + P.Node 
), GraphNode AS (
SELECT UNNEST(ARRAY[1,2,3]) AS Node
)
SELECT GraphPath
FROM GraphPath
ORDER BY GraphPath

这会给我以下错误消息:

           if capacity[neighbour] < demand:
                distance[neighbour], predecessor[neighbour] = 999

是否有解决此问题的方法,或者我可能以更智能的方式添加需求约束?

完整代码:

TypeError: '<' not supported between instances of 'dict' and 'int'

0 个答案:

没有答案