目录
The Problem
Let G = ( V , E ) G=(V, E) G=(V,E) be a directed graph. Assume that each edge ( i , j ) ∈ E (i, j) \in E (i,j)∈E has associated weight c i j c_{ij} cij.
Dijkstra’s Algorithm
第一种写法
Dijkstra’s Algorithm: Find shortest paths in graphs with positive edge costs from node s s s.
假设 each edge e e e has length l e ≥ 0 l_e\geq 0 le≥0. For a path P P P, the length of P P P - denoted l ( P ) l(P) l(P) - is the sum of the length of all edges in P P P.
此外,引进两个集合 S S S 和 U U U:
- S S S 的作用是记录已求出最短路径的顶点 u u u, 以及相应的最短路径长度 d ( u ) d(u) d(u)。
- U U U 的作用是记录还未求出最短路径的顶点 v v v,那么可以推出
d ′ ( v ) = min e = ( u , v ) : u ∈ S d ( u ) + l e d'(v)=\min_{e=(u, v): u\in S} d(u) + l_e d′(v)=e=(u,v):u∈Smind(u)+le
Dijkstra’s Algorithm ( G , l ) (G, l) (G,l)
\quad
Let
S
S
S be the set of explored nodes
\quad
\quad
For each
u
∈
S
u\in S
u∈S, we store a distance
d
(
u
)
d(u)
d(u)
\quad
Initially
S
=
{
s
}
S=\{s\}
S={s} and
d
(
s
)
=
0
d(s)=0
d(s)=0
\quad
While
S
≠
V
S \neq V
S=V
\quad
\quad
Select a node
v
∉
S
v \notin S
v∈/S with at least one edge from
S
S
S for which
d
′
(
v
)
=
min
e
=
(
u
,
v
)
:
u
∈
S
d
(
u
)
+
l
e
d'(v)=\min_{e=(u, v): u\in S} d(u) + l_e
d′(v)=mine=(u,v):u∈Sd(u)+le is as small as possible
\quad
Add
v
v
v to
S
S
S and define
d
(
v
)
=
d
′
(
v
)
d(v)=d'(v)
d(v)=d′(v)
\quad
EndWhile
第二种写法
Calculate length of shortest path from the start node
v
v
v to each node.
伪代码:
More Complex Problem
Find the shortest paths when costs may be negative.
Negative cost 的实际意义:nodes 代表 agents in a financial setting, c i j c_{ij} cij 代表 the cost of a transaction in which we buy from agent i i i and then immediately sell to agent j j j. 这种设定下,a path would represent a succession of transactions, and edges with negative costs would represent transactions that result in profits.
这里讨论两种问题:
- Given a graph
G
G
G with weights, decide if
G
G
G has a negative cycle - that is, a direct cycle
C
C
C such that
∑ i j ∈ C c i j < 0 \sum_{ij \in C} c_{ij} < 0 ij∈C∑cij<0
( i j ij ij: 由 node i i i 和 node j j j 构成的 edge) - If the graph has no negative cycles, find a path
P
P
P from an origin node
s
s
s to a destination node
t
t
t with minimum total cost:
∑ i j ∈ P c i j \sum_{ij \in P} c_{ij} ij∈P∑cij
Consider the Minimum-Cost s s s- t t t Path Problem under the assumption that there are no negative cycles: 因为,假设存在一个 negative cycle、一条从 s s s 到 cycle 的 path P s P_s Ps、和一条从 cycle 到 t t t 的 path P t P_t Pt,那么如果无限次绕这个 negative cycle,从 s s s 到 t t t 的 total cost 将变得无限小。
Design the Algorithm
如果用 Dijkstra 的思路,每一次将与
S
S
S 相邻且到 node
s
s
s 距离最近的 node
v
v
v 添加进
S
S
S,在有 negative weights 的情况下不成立。因为可以走一条开始 cost 贵的 path,但是后部分 path 是 negative cost,最终导致 total cost 小于开始 cost 便宜的 path。
如果每一次计算 cost 的时候都同时加上一个很大的数
M
M
M,即
c
′
=
c
+
M
c'=c+M
c′=c+M,但是每条 path 所用的 edge 的个数可能不一样,导致额外加的
M
M
M 的个数不同。
定理 (6.22):If G G G has no negative cycles, then there is a shortest path from s s s to t t t that is simple (i.e., does not repeat nodes), and hence has at most n − 1 n-1 n−1 edges.
证明:
∵ \because ∵ every cycle has nonnegative cost
∴ \therefore ∴ the shortest path P P P from s s s to t t t with the fewest number of edges does not repeat any vertex v v v.
若 P P P 中有重复的 vertex v v v
We could remove the portion of P P P between consecutive visits to v v v, resulting in a path of no greater cost and fewer edges.
G G G 有 n n n 个 nodes。
∵ \because ∵ G G G 是 connected 且 does not contain a cycle
∴ \therefore ∴ G G G has n − 1 n-1 n−1 edges
用 O P T ( i , v ) OPT(i, v) OPT(i,v) 表示 the minimum cost of a v v v- t t t path using at most i i i edges.
Original Problem:
O
P
T
(
n
−
1
,
s
)
OPT(n-1, s)
OPT(n−1,s)
Fix an optimal path
P
P
P representing
O
P
T
(
i
,
v
)
OPT(i, v)
OPT(i,v) as depicted below:
- If the path P P P uses at most i − 1 i-1 i−1 edges, then O P T ( i , v ) = O P T ( i − 1 , v ) OPT(i, v)=OPT(i-1, v) OPT(i,v)=OPT(i−1,v).
- If the path P P P uses i i i edges, and the first edge is ( v , w ) (v, w) (v,w), then O P T ( i , v ) = c v w + O P T ( i − 1 , w ) OPT(i, v)=c_{vw}+OPT(i-1, w) OPT(i,v)=cvw+OPT(i−1,w)
定理 (6.23): If
i
>
0
i>0
i>0, then
O
P
T
(
i
,
v
)
=
min
(
O
P
T
(
i
−
1
,
v
)
,
min
w
∈
V
(
O
P
T
(
i
−
1
,
w
)
+
c
v
w
)
)
OPT(i, v)=\min \bigg( OPT(i-1, v), \min_{w\in V} \Big( OPT(i-1, w) + c_{vw} \Big) \bigg)
OPT(i,v)=min(OPT(i−1,v),w∈Vmin(OPT(i−1,w)+cvw))
算法伪代码
Shortest-Path(
G
,
s
,
t
G, s, t
G,s,t)
\quad
n
n
n = number of nodes in
G
G
G
\quad
Array
M
[
0...
n
−
1
,
V
]
M[0...n-1, V]
M[0...n−1,V]
\quad
Define
M
[
0...
n
−
1
,
t
]
=
0
M[0...n-1, t] = 0
M[0...n−1,t]=0 and
M
[
0
,
v
]
=
∞
M[0, v] = \infty
M[0,v]=∞ for all other
v
∈
V
v\in V
v∈V
\quad
For
i
=
1
,
.
.
.
,
n
−
1
i=1, ..., n-1
i=1,...,n−1
\quad
\quad
For
v
∈
V
v\in V
v∈V in any order
\quad
\quad
\quad
Compute
M
[
i
,
v
]
M[i, v]
M[i,v] using recurrence
(
6.23
)
(6.23)
(6.23)
\quad
\quad
EndFor
\quad
EndFor
\quad
Return
M
[
n
−
1
,
s
]
M[n-1, s]
M[n−1,s]
定理 (6.24): The Shortest-Path method correctly computes the minimum cost of an s s s- t t t path in any graph that has no negative cycles, and runs in O ( n 3 ) O(n^3) O(n3) times.
例子
图(a) 所示是一个 directed graph
the Shortest- Path Algorithm constructs the dynamic programming table:
Extensions: Some Basic Improvements to the Algorithm
An Improved Running-Time Analysis
如果有 n n n 个 nodes,一个 directed graph 可以有大约 n 2 n^2 n2 个edges(因为每两个 nodes 之间就可以有一条 edge)。然而一般的 graph 比这种稀疏很多。我们假设有 m m m 条 edges,那么有
定理 (6.25): The Shortest-Path method can be implemented in O ( m n ) O(mn) O(mn) time.
证明:
对于 Array Entry M [ i , v ] M[i, v] M[i,v], 由 recurrence ( 6.23 ) (6.23) (6.23) 可得
M [ i , v ] = min ( M [ i − 1 , v ] , min w ∈ V ( M [ i − 1 , w ] + c v w ) ) M[i, v]=\min \bigg( M[i-1, v], \min_{w\in V} \Big( M[i-1, w] + c_{vw} \Big) \bigg) M[i,v]=min(M[i−1,v],w∈Vmin(M[i−1,w]+cvw)) We assumed it could take up to O ( n ) O(n) O(n) time to compute this minimum, since there are n n n possible nodes w w w.
但是这里只用计算和 n n n 相连的 w w w(因为是 directed graph,只考虑由 n n n 指向 w w w) 的 minimum.
假设有 n v n_v nv denotes the number of edges leaving v v v,那么 it takes time O ( n v ) O(nv) O(nv) to compute the array entry M [ i , v ] M[i, v] M[i,v].
We have to compute an entry for every node v v v and every index 0 ≤ i ≤ n − 1 0≤i≤n−1 0≤i≤n−1, so this gives a running-time bound of
O ( n ∑ v ∈ V n v ) = O ( n m ) O\Big( n \sum_{v \in V} n_v \Big)=O(nm) O(nv∈V∑nv)=O(nm)
∵ \because ∵ 是 directed graph,即 ∑ v ∈ V n v \sum_{v \in V} n_v ∑v∈Vnv 中每一条 edge 都只被数了一次
∴ \therefore ∴ ∑ v ∈ V n v = m \sum_{v \in V} n_v = m ∑v∈Vnv=m
Improving the Memory Requirements
Array M M M has size n 2 n^2 n2, 这里可以将其 reduce 到 O ( n ) O(n) O(n). Rather than recording M [ i , v ] M[i, v] M[i,v] for each value i i i, we will use and update a single value M [ v ] M[v] M[v] for each node v v v, the length of the shortest path from v v v to t t t that we have found so far.
依旧保持
i
=
1
,
2
,
.
.
.
,
n
−
1
i=1, 2, ..., n-1
i=1,2,...,n−1 的 iteration,但是这里
i
i
i 只作为一个 counter。In each iteration, and for each node v, we perform the update
M
[
v
]
=
min
(
M
[
v
]
,
min
w
∈
V
(
c
v
w
+
M
[
w
]
)
)
M[v]=\min \Big( M[v], \min_{w \in V}(c_{vw} + M[w]) \Big)
M[v]=min(M[v],w∈Vmin(cvw+M[w]))
定理 (6.26): Throughout the algorithm M [ v ] M[v] M[v] is the length of some path from v v v to t t t, and after i i i rounds of updates the value M [ v ] M[v] M[v] is no larger than the length of the shortest path from v v v to t t t using at most i i i edges.
Finding the Shortest Paths
How to recover the shortest path?
- Enhance the code by having each node v v v maintain the first node after itself ( v v v 之后的第一个 node,记作 f i r s t [ v ] first[v] first[v]) on its path to the destination t t t.
- 当
M
[
v
]
M[v]
M[v] is updated,同时 update
f
i
r
s
t
[
v
]
first[v]
first[v]。
- Whenever the value of M [ v ] M[v] M[v] is reset to the minimum min w ∈ V ( c v w + M [ w ] ) \min_{w \in V}(c_{vw} + M[w]) minw∈V(cvw+M[w]), we set f i r s t [ v ] first[v] first[v] to the node w w w that attains this minimum.
Let P P P denote the directed “pointer graph” whose nodes are V V V, and whose edges are { ( v , f i r s t [ v ] ) } \{(v, first[v])\} {(v,first[v])}
定理 (6.27): If the pointer graph P P P contains a cycle, then this cycle must have negative cost.
证明
如果 f i r s t [ v ] = w first[v]=w first[v]=w,那么 M [ v ] ≥ c v w + M [ w ] M[v] \geq c_{vw} + M[w] M[v]≥cvw+M[w] (因为 min \min min 中我们取了含有 w w w 的项)
Let v 1 , v 2 , . . . , v k v_1, v_2, ..., v_k v1,v2,...,vk be the nodes along the cycle C C C in the pointer graph, and assume that ( v k , v 1 ) (v_k, v_1) (vk,v1) is the last edge to have been added.
for i = 1 , 2 , . . . , k − 1 i=1, 2, ..., k-1 i=1,2,...,k−1, M [ v i ] ≥ c v i v i + 1 M[v_i]\geq c_{v_i v_{i+1}} M[vi]≥cvivi+1
for i = k i=k i=k, M [ k ] > c v k v 1 + M [ v 1 ] M[k]>c_{v_k v_1} + M[v_1] M[k]>cvkv1+M[v1], since we are about to update M [ v k ] M[v_k] M[vk] and change f i r s t [ v k ] first[v_k] first[vk] to v 1 v1 v1.
将 k k k 个不等式相加
0 > ∑ i = 1 k − 1 c v i v i + 1 + c v k v 1 0>\sum_{i=1}^{k-1} c_{v_i v_{i+1}} + c_{v_k v_1} 0>i=1∑k−1cvivi+1+cvkv1 a negative cycle
定理 (6.28): Suppose G G G has no negative cycles, and consider the pointer graph P P P at the termination of the algorithm. For each node v v v, the path in P P P from v v v to t t t is a shortest v v v- t t t path in G G G.
证明:
Consider a node v v v and f i r s t [ v ] = w first[v] = w first[v]=w
因为算法停止,所以 M [ v ] = c v w + M [ w ] M[v]=c_{vw}+M[w] M[v]=cvw+M[w]
∵ \because ∵ M [ t ] = 0 M[t]=0 M[t]=0 [这个是为什么?难道没有指向 t t t 的 node?]
∴ \therefore ∴ the length of the path traced out by the pointer graph is exactly M [ v ] M[v] M[v], which we know is the shortest-path distance.