且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

计算中间必须要点的两个节点之间的最短路径

更新时间:2023-02-01 22:41:42

我有另一个想法.由于该图是非循环图,因此我们可以将必需节点(源节点和目标节点除外)分为两个节点( A A start ), A end )并在它们之间设置一条边,并将权重设置为-infinity.强制节点 A 的所有传入边缘都将连接到 A start ,而强制节点 A 的所有传出边缘都将连接从 A end 中出来.最后,我们将对源节点到目标节点运行dijkstra算法.由于我们在必选节点中设置了-infinite权重,因此dijkstra必须通过它们以使成本最小化.另外,由于该图是非循环图,因此我们不必担心负循环.

I have another idea. Since the graph is acyclic graph, we can split the mandatory node (except source and destination node) into two node (A to Astart and Aend) and set an edge between them and set the weight as -infinity. All incoming edge to mandatory node A will be connected to Astart and all outgoing edge from mandatory node A will be came out from Aend. Finally we will run a dijkstra algorithm for source node to destination node. Since we set an -infinite weight in mandatory node, dijkstra is bound to go through them to minimize the cost. Also as the the graph is acyclic graph, we don't have to worry about negative cycle.