幾個常用最短路徑演算法

2021-08-26 19:37:15 字數 1073 閱讀 9437

今天來總結一下常用的最短路徑演算法。符號簡稱:e -- # of edges; v -- # of vertexes

single source shortest path: 

on non-weighted graph: bredth first search

non-negative-cylic graph: dikstra -- bfs + priority queue  -- o(e) + o(v log v)

graph with negative cycle: bellman ford's: relax node for n-1 times, then check if can still relax node (negative cycle)

time complexity: o(e * v)

還有國產的shortest path faster algorithm: 用乙個佇列記錄當前relex的node。每次拿出要relex的node來繼續relex。如果有乙個node relex的次數超過了n次,那麼說明有負環 —— o(ke),一般情況下k < 2。國產的東西就是好。

minimum spaning tree:

prim's -- o(e log v): 找當前點可見邊中,連線沒有visit過的點的最短的。

kruskal's -- o(e log(e)) = o(e log(v)):首先sort一下所有邊。然後從weight最小的兩條邊一點點加點進來,直到包含了所有點。

all pair shortest path:

floyd warshall (v^3): 只要記住這個recursive formula就會寫code了:

define function: shortest(i,j,k) ==> the shortest path from node i to node j, only pass by element from set

shortestpath(i,j,0) = w(i,j)

shortestpath(i,j,k) = min(shortestpath(i,j,k-1), shortestpath(i,k,k-1) + shortestpath(k,j,k-1))

最短路徑演算法 最短路

在每年的校賽裡,所有進入決賽的同學都會獲得一件很漂亮的t shirt。但是每當我們的工作人員把上百件的衣服從商店運回到賽場的時候,卻是非常累的!所以現在他們想要尋找最短的從商店到賽場的路線,你可以幫助他們嗎?input 輸入包括多組資料。每組資料第一行是兩個整數n m n 100,m 10000 n...

最短路徑演算法

floyd演算法 012345 001 54 1108 1 2 801 3 3 1035 45 302 5413520 floyd 演算法過程描述如下 首先 以邊集 初始化,得到所有的直接連通代價 依次考慮第 k個結點,對於 中的每乙個 i j 判斷是否滿足 s i j s i k s k j 如果...

最短路徑演算法

個人覺得下面 有代表性 最短路徑演算法原始碼 vb 本人載 開發gis,遊自編的最短路徑查詢程式,速度特快,3萬節點,35000條路全部遍歷,只需1秒。現將最短路徑的思路告訴大家,希望大家在優化,並用不同語言編制,我正在學delphi,準備用delphi做成庫,本例以由拓撲關係的arc info 檔...