![](/img/trans.png)
[英]Finding shortest path distances from a given node s to ALL the nodes in V in a graph with two negative edges
[英]Finding shortest path distances in a graph containing at most two negative edges
我得到一個有向圖,其中每個邊都有成本。利用圖中最多有兩個負邊的事實,我的目標是找到從給定節點s到V中所有節點的最短路徑距離。該算法的時間復雜度應為O(|E| + |V|*log|V|)
,因此我認為我需要應用Dijkstra的算法。
我猜想我需要以某種方式將給定的圖轉換為具有非負權重的新圖,以使該圖中從s到v的最短路徑等同於給定圖中所需的最短路徑。或者也許我需要修改Dijkstra的算法?
我現在正在掙扎。 任何幫助,將不勝感激!
由於Dijkstra的算法過於貪婪,因此不適用於負權重。 為此,您需要其他一些算法,例如Bellman-Ford算法 。
但是,如果您仍然想使用Dijkstra的算法,則有一種已知的方法。 在這種方法中,您需要重新分配成本,以使所有成本變為正數。
為此,您可以查看Johnson算法 。 Johnson的算法包括以下步驟(摘自Wikipedia ):
只是一些定義開始:
令負邊為n1 = (n1s, n1e)
(即從頂點n1s
到頂點n1e
)
和n2 = (n2s, n2e)
。
將我們要查找的最短路徑的起點和終點分別定義為s
和e
。
基本思路:
以負權重邊的起始頂點和結束端點的每個組合為起點,以負權重邊的結束頂點和端點的每個頂點為終點,多次運行Dijkstra算法。找到實際的最短路徑。
算法:
使用Dijkstra的算法確定以下最短路徑,所有路徑均不包括兩個負邊緣:
se = s -> e // shortest path from s to e sn1 = s -> n1s // shortest path from s to n1 sn2 = s -> n2s // shortest path from s to n2 ne1 = n1e -> e // shortest path from n1 to e n1n2 = n1e -> n2s // shortest path from n1 to n2 ne2 = n2e -> e // shortest path from n2 to e n2n1 = n2e -> n1s // shortest path from n2 to n1
現在,只需計算以下項的最小值:
se // s to e sn1 + n1 + ne1 // s to n1 to e sn2 + n2 + ne2 // s to n2 to e sn1 + n1 + n1n2 + n2 + ne2 // s to n1 to n2 to e sn2 + n2 + n2n1 + n1 + ne1 // s to n2 to n1 to e
由於Dijkstra算法有7次恆定運行,
運行時間為O(7(|E| + |V| log |V|))
= O(|E| + |V| log |V|)
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.