PowerPoint 簡報
Download
Report
Transcript PowerPoint 簡報
All-pairs Shortest Paths
The structure of a shortest path:
All subpaths of a shortest path are shortest paths.
p : a shortest path from vertex i to vertex j with at most m
edges.
P’
Decompose p as :
i kj
p’ has at most m-1 edges.
(i, j) = (i, k) + wkj
A recursive sol. to the all-pairs shortest-paths
problem
dij(m): minimum weight of any path from vertex i to vertex j
that contains at most m edges.
(0)
ij
d
dij(m)
0 if i j
if i j
min(dij(m 1) , min{dik(m 1) wkj })
1k n
= min{dik(m1) wkj }, w jj 0 for all j
1k n
p2.
Computing the shortest-path weights bottom
up:
dij(1)= wij Let D(m)= (dij(m) )
W=(wij)
Given D(m-1) and W, return D(m)
p3.
All-pairs Shortest Paths:
Input : G = (V,E), |V|= n
if i=j
0
W = (wij), wij the weight of (i,j) if (i,j) E
if (i,j) E
Adjacency-matrix
Output : nn matrix D = (dij)
di,j : the weight of a shortest path from vertex i to
vertex j
di,j = (i, j) at termination
(Let (i, j) denote the shortest-path weight from
vertex i to vertex j )
= (ij) : predecessor matrix
j
if either i=j or there is no path from i to j
NIL
ij
ij
some predecessor of j on a shortest path from i
No negative-weight cycle
i
p4.
Print-All-Pairs-Shortest-Path(, i, j)
{ if i=j then print I
else if ij = NIL then print “no path from” i “to” j “exists”
else Print-All-Pairs-Shortest-Path(, i, ij)
print j
}
Dynamic Programming:
Characterize the structure of an optimal sol.
Recursively define the value of an optimal sol.
Compute the value of an optimal sol in a bottom-up fashion
p5.
Extend-Shortest-Paths(D,W)
{ n rows[D]
Very similar to
Let D’ = (dij’) be an nn matrix
matrix-multiplication
for i=1 to n do
for j=1 to n do dij’
for k=1 to n do
dij’ = min (dij’ , dik + wkj)
return D’
}
Slow-All-Pairs-Shortest-Paths(W)
{ n = rows[W]
D(1) = W
4)
(n
for m=2 to n-1 do
D(m) = Extend-Shortest-Paths(D(m-1) , W)
return D(n-1)
}
p6.
Faster-All-Pairs-Shortest-Paths(W)
{ n = rows[W]
D(1) = W
(n3logn)
m=1
While n-1>m do
D(2m) = Extend-Shortest-Paths(D(m), D(m))
(Repeated squaring)
m = 2m
return D(m)
}
p7.
Floyd-Warshall algorithm
Structure of a shortest path:
Intermediate vertex of a simple path p=<v1 , …, v>
is any vertex of p other than v1 or v , that is,
any vertex in the set {v2 , …, v-1}
Let V={1, …, n} and a subset {1, 2, …, k} for some k
For any pair of vertices i, j V, consider all paths from i
to j whose intermediate vertices are all drawn from
{1, 2, …, k} and let p be a minimum-weight path from
among them
p8.
If k is not an intermediate vertex of path p, then all
intermediate vertices of path p are in the set {1, 2, …, k-1}.
Thus a shortest path from vertex i to vertex j with all
intermediate vertices in the set {1, 2, …, k-1} is also a
shortest path from i to j with all intermediate vertices in the
set {1, 2, …, k}
all int. vertices in {1, 2, …, k-1}
p1
k
i
all int. vertices in {1, 2, …, k-1}
p2
j
p : all int. vertices in {1, 2, …, k}
k : intermediate vertex of p
p9.
A recursive solution to the all-pairs shortest-paths problem
dij(k) : the weight of a shortest path from i to j with all
intermediate vertices in the set {1, 2, …, k}
(k )
ij
d
if k=0
wij
(k 1)
(k 1)
(k 1)
min(d
,
d
d
)
ij
ik
kj
Computing the shortest-path weights bottom up
Floyd-Warshall(W)
{ n = rows[W]
D(0) = W
for k=1 to n do
(n3)
for i=1 to n do
for j=1 to n do
dij(k) min(dij(k 1) , dik(k 1) dkj(k 1) )
return D(n) }
p10.
Constructing a shortest path:
ij(k) : the predecessor os vertex j on a shortest path from i
with all intermediate vertices in the set {1, 2, …, k}
(0)
ij
ij(k )
NIL
i
ij(k 1)
(k 1)
kj
if i=j or wij
if i j and wij
if dij(k 1) dik(k 1) dkj(k 1)
if dij(k 1) dik(k 1) dkj(k 1)
p11.
Transitive closure of a directed graph G=(V,E):
G*=(V, E*), E*={ (i, j): there is a path from i to j in G}
For i, j , k = 1, 2, …, n , define tij(k)=1, if a path in G from
i to j with all intermediate vertices in {1, 2, …, k}; otherwise
tij(k)=0
t
(0)
ij
0
1
i j and (i, j) E
i = j or (i, j) E
tij(k) t ij(k 1) (t ik(k 1) t kj(k 1) )
p12.
TC(G)
{ n = | V[G] |;
for i=1 to n do
for j=1 to n do
if i=j or (i, j) E[G]
then tij(0) = 1
else tij(0) = 0
for k=1 to n do
for i=1 to n do
for j=1 to n do
return T(n)
tij(k) t ij(k 1) (t ik(k 1) t kj(k 1) )
}
p13.
Preserving shortest paths by reweighting:
ˆ , which must satisfy 2 properties:
ww
For all pairs of vertices u, vV, a shortest path from u to v
using weight function w is also a shortest path from u to v
using weight function w
ˆ
For all edges (u, v), w
ˆ (u, v) is non-negative
: shortest-path weights with w
ˆ
ˆ
: shortest-path weights with w
p14.
Lemma:
Given G=(V, E) weighted, directed graph with weight
function w: E R , let h: V R be any function mapping
vertices to real numbers.
For each (u,v) E, define w
ˆ (u, v) w(u, v) h(u) h(v)
Let p=<v0, …, vk>
ˆ
Then w(p) (v 0 , vk ) iff w(p)
ˆ
(v 0 , v k )
Also G has a negative cycle with w iff G has a negative
ˆ
cycle with w
pf:
k
k
i 1
i 1
ˆ i1 , v i ) (w(v i1 , v i ) h(v i1 ) h(v i ))
w(p) w(v
=
k
w(v
i 1
i 1
, v i ) h(v 0 ) h(v k )
= w(p) h(v 0 ) h(v k )
p15.
ˆ
Suppose there is a shorter path p’ from v0 to vk with w
ˆ
Thus w(p ') w(p)
ˆ
w(p ') h(v 0 ) h(v k ) w(p ') w(p)
w(p) h(v 0 ) h(v k )
w(p ') w(p)
Similarly for the converse
Consider any cycle c=<v0, v1, …, vk> , where v0 = vk
ˆ (c) w(c) h(v 0 ) h(v k )
w
w(c)
Thus c has negative weight using w iff it has negative
ˆ
weight using w
Producing non-negative weights by reweighting:
Given G=(V,E) with weight function w: ER , make
G’=(V’, E’) , where V’=V{s} , E’=E{(s,v): vV}
Extend w s.t. w(s,v)=0 for all vV
Define h(v)=(s,v) for all vV’
For all edges (u,v)E’ , we have h(v) h(u)+w(u,v)
ˆ (u, v) w(u, v) h(u) h(v) 0
Thus w
p16.
Johnson’s algorithm for sparse graphs:
Input : sparse graph in adj. list representation
Output : either returns a matrix of shortest-path weights for
all pairs
or reports that the input graph contains a negativeweight cycle
p17.
Johnson(G)
{ compute G’, where V[G’]=V[G]{s} and
E[G’]=E[G]{(s,v):vV[G]}
if Bellman-Ford(G’, w, s)=False
then print “ a negative-weight cycle”
else for each vertex v V[G’]
do set h(v)=(s,v) computed by the Bellman-Ford algo.
for each edge (u,v) E[G’]
do w
ˆ (u, v) w(u, v) h(u) h(v)
for each vertex u V[G] do
ˆ , u) to compute ˆ
run Dijkstra(G, w
(u, v) for all v V[G]
for each v V[G]
(u, v) h(v) h(u)
do duv ˆ
return D
}
O(V2logV+VE)
O(VElogV)
Fibonacci heap
binary heap
p18.