single-source shortest paths. 2 shortest-path problem...
Post on 15-Jan-2016
246 views
TRANSCRIPT
Single-Source Shortest Paths
Single-Source Shortest Paths 2
Shortest-path problem
• 即是在一圖上找出兩點間最短路徑。• G=(V,E) 是一個 Weighted Directed Graph( 加權有向圖 ) 透過 Weight function w: ER 界定出每個邊的權重。
• 以 p=(v0,v1,…,vk) 表一個自 v0到 vk的 Path( 路徑 ) 。
Single-Source Shortest Paths 3
Shortest-path problem
• 定義• 定義自 u 到 v 的最短距離
k
i ii vvwpw1 1 ),()(
otherwise. ,
. to frompath a },:)(min{),( vuvupwvup
Single-Source Shortest Paths 4
Shortest-path tree rooted at s
• 對應於圖 G=(V,E) 的 Shortest-path tree rooted at s ( 根於 s 之最短路徑樹 ) G’=(V’,E’) ,滿足下列三點:– V’ 是 s 可達的點集合。– G’ 是一個以 s 為根的 Rooted Tree 。– 在 G’ 中 s 到 v 的 simple path 即為 G 中 s 到 v 的最短路徑。
Single-Source Shortest Paths 5
Shortest-path tree rooted at s範例
s
t
u
v
wx
y
z
s
t
v
wx
y
z
Original Graph G Shortest-path tree rooted at s
10
10
10
10
10
10
1
1
1
1
1
10
10
1
1
1
1
Single-Source Shortest Paths 6
Predecessor graph
• 對一圖 G=(V,E) ,根據一個表 π 來建構的子圖 Gπ
=(Vπ,Eπ) ,滿足以下條件:– π[s]=NIL ,且 s∈Vπ。– 若 π[v]≠NIL 則 (π[v],v)∈Eπ且 v∈Vπ 。
• Shortest-path tree rooted at s 是 Predecessor graph 的特例。
Single-Source Shortest Paths 7
Predecessor graph範例
s
t
u
v
wx
y
z
s
t
v
wx
y
z
Original Graph G Shortest-path tree rooted at s
10
10
10
10
10
10
1
1
1
1
1
10
10
1
1
1
1
π[s] π[t] π[u] π[v] π[w] π[x] π[y] π[z]
NIL s NIL t s t x v
Single-Source Shortest Paths 8
Initialize-Single-Source演算法
• 定義變數 d[v] 代表目前已知之自 s 至 v 的最短距離。
• 定義變數 π[v] 代表目前已知自 s 至 v 的最短路徑上, v 之前的那一點。
• 初始時, d[v]=∞ , π[v]=NIL , d[s]=0 。即除自 s到 s 的最短路徑已知之外,其餘均設為未知。
Single-Source Shortest Paths 9
Initialize-Single-Source演算法
Initialize-Single-Source(G,s)
{ for each vertex v∈V[G]do d[v]∞
π[v]NIL
d[s]0
}
Single-Source Shortest Paths 10
Relaxation 演算法
• 主要的目的在於利用邊 (u,v) 的資訊來更新目前所知的最短路徑。
Relax(u,v,w)
{ if d[v]>d[u]+w(u,v)
then d[v]d[u]+w(u,v)
π[v]u
}
Single-Source Shortest Paths 11
Relaxation 範例
4 7
s
w(u,v)u v
if w(u,v)=2 (<3)
Relax(u,v,w)前
Relax(u,v,w) 後
4 6
s
w(u,v)u v
此時更新 sv 的最短距離以及更新 π[v]u
if w(u,v)=4 (>3)
4 7
s
w(u,v)u v
此時不更新 sv 的最短距離
Single-Source Shortest Paths 12
最短路徑與 Relaxation的性質
• 三角不等式:對所有的邊 (u,v) , δ(s,v)δ(s,u)+w(u,v) 。
• 上限性質:δ(s,v)d[v] ,即 d[v] 總是 sv 的最短距離上限。一旦 d[v]=δ(s,v) ,則 Relaxation 不會更改 d[v] 值。
Single-Source Shortest Paths 13
最短路徑與 Relaxation的性質
• 無路徑性質:如果 s 到 v 並無路徑,則 d[v]=δ(s,v)=∞ 。
• 收斂性質:若 sv 的最短路徑包含邊 (u,v) 且 d[u]=δ(s,u) ,則此時執行 Relax(u,v,w) 會使得 d[v]=δ(s,v) 。
Single-Source Shortest Paths 14
最短路徑與 Relaxation的性質
• Path-relaxation 性質:如 p=(v0,v1,…,vk) 是一個自 s=v0vk的最短路徑, 則依序執行 Relax(v0,v1,w) , Relax(v1,v2,w)… , Relax(vk-1,vk,w) 會使得 d[vk]=δ(s,vk) 。
• Predecessor graph 性質:當經過一連串的 Relaxation 後,對所有的點 v , d[v]=δ(s,v) 時,此時對應的 Predecessor graph Gπ
即是一個 Shortest-path tree rooted at s 。
Single-Source Shortest Paths 15
Bellman-Ford演算法
• 可以計算出沒有負迴圈的圖之最短路徑。Bellman-Ford(G,w,s)
{ Initialize-Single-Source(G,s)
for i = 1 to |V-1|
do for each edge (u,v)∈E
do Relex(u,v,w)
for each edge (u,v)∈E
do if d[v]>d[u]+w(u,v)
then return false //代表有負迴圈return true //代表計算成功
}
Single-Source Shortest Paths 16
Bellman-Ford演算法運作範例
0
∞
∞
∞
∞
(a)6
7
8
9
2
-3
-4
-2
5
7 0
6
7
∞
∞
(b)6
7
8
9
2
-3
-4
-2
5
7s s
Single-Source Shortest Paths 17
Bellman-Ford演算法運作範例
0
6
7
4
2
(c)6
7
8
9
2
-3
-4
-2
5
7 0
2
7
4
-2
(d) (e)6
7
8
9
2
-3
-4
-2
5
7s s
Single-Source Shortest Paths 18
Bellman-Ford演算法分析
• 正確性:對所有的邊作一次 Relaxation 則可將在 Shortest-path tree rooted at s 中一步可及的 path 正確計算出。由 path-relaxation 性質,做完 |V|-1 次之後,所有的 Shortest simple path 終點 v , d[v]=δ(s,v) 。
Single-Source Shortest Paths 19
Bellman-Ford演算法分析
• 就時間複雜度的分析如下:
– Initialize-Single-Source 花去 O(|V|) 的時間。– 對所有的邊做了 O(|V|) 次的 Relaxation ,花去
O(|V||E|) 的時間。– 最後花去 O(|E|) 的時間檢查是否有負迴圈。
• 故總共花去 O(|V||E|) 的時間。
Single-Source Shortest Paths 20
Single-source shortest paths in DAGs
• 跟 Bellman-Ford 不同的是,按照特定的順序作 Relaxation ,可較短的時間內計算出最短路徑。
DAG-Shortest-Path(G,w,s)
{ Topologically sort V[G]
Initialize-Single-Source(G,s)
for each u taken in topological order
do for each v∈adj[u]do Relax(u,v,w)
}
• 以上演算法僅需 O(|V|+|E|) 的時間。
Single-Source Shortest Paths 21
DAG-Shortest-Path 操作範例
∞ 0 ∞ ∞∞ ∞
(a) s5 2 7 -1 -2
6 1
3 42
∞ 0 ∞ ∞∞ ∞
(b) s5 2 7 -1 -2
6 1
3 42
Single-Source Shortest Paths 22
DAG-Shortest-Path 操作範例
∞ 0 ∞ ∞2 6
(c) s5 2 7 -1 -2
6 1
3 42
∞ 0 6 42 6
(d) s5 2 7 -1 -2
6 1
3 42
Single-Source Shortest Paths 23
DAG-Shortest-Path 操作範例
∞ 0 5 42 6
(e) s5 2 7 -1 -2
6 1
3 42
∞ 0 5 32 6
(f)s5 2 7 -1 -2
6 1
3 42
∞ 0 5 32 6
(g) s5 2 7 -1 -2
6 1
3 42
Single-Source Shortest Paths 24
Dijkstra演算法
• 僅能處理無負邊的圖。
• 比 Bellman-ford 演算法快,也是藉由挑選特定順序來做 Relaxation 來達成目的。
• 利用 Priority queue 來實做。
• 主要關鍵想法:利用收斂性質。
Single-Source Shortest Paths 25
Dijkstra演算法Q: Priority queue with d as the key
Dijkstra(G,w,s)
{ Initialize-Single-Source(G,s)
Q=V[G]
while Q is not empty
do u=Extract-Min(Q)
for each v∈adj[u]do Relax(u,v,w)
}
Single-Source Shortest Paths 26
Dijkstra演算法操作範例
0
∞ ∞
∞ ∞
10
5
1
2 3
2
7
9
4 6
(a)
0
10 ∞
5 ∞
10
5
1
2 3
2
7
9
4 6
(b)
s s
Single-Source Shortest Paths 27
Dijkstra演算法操作範例
0
8 11
5 7
10
5
1
2 3
2
7
9
4 6
(d)
s0
8 14
5 7
10
5
1
2 3
2
7
9
4 6
(c)
s
Single-Source Shortest Paths 28
Dijkstra演算法操作範例
0
8 9
5 7
10
5
1
2 3
2
7
9
4 6
(e)
0
8 9
5 7
10
5
1
2 3
2
7
9
4 6
(e)
s s
Single-Source Shortest Paths 29
Dijkstra演算法分析
• 依照使用的 Priority queue 實做方式不同,會有不同的執行時間。
• 使用 Linear array 來實做,消耗 O(|V|2) 的時間。
• 使用 Binary heap 來實做,消耗 O(|E|log|V|) 的時間。
• 使用 Fibonacci heap 來實做,消耗 O(|E|+|V|log|V|)的
時間。