Ch5Notes01

advertisement
Optimality Conditions for Shortest Path Distances
d ( j) j  N
UB on shortest path from s to j (represents
some path from s to j)
Necessary optimality conditions
d ( j )  d (i)  cij i, j   A
(1)
They are also sufficient conditions. If (1) is true for any set of
d(1), d(2), … d(n) then they are optimal values for d().
1
Suppose we have d(1) … d(n) satisfying (1).
Let s = i1 - i2 - … - ik = j be any directed path P from s to j.
Then
d ( j )  d (ik )  d (ik 1 )  cik 1ik
d (ik 1 )  d (ik  2 )  cik 2 k 1

d (i2 )  d (i1 )  ci1i2  ci1i2
The last inequality is true because
Thus
d (i1 )  d ( s)  0
d ( j )  d (ik )  cik 1ik  cik  z ik 1  ci1i2   ci, j
(i , j )P
Theorem 5.1 Shortest Path Optimality conditions. For every node jN,
let d(j) denote the length of some directed path from the source node to node
j. Then the numbers d(j) represent shortest path distances if and only if they
satisfy the following shortest path optimality conditions:
d ( j )  d (i)  cij (i, j )  A
2
To construct an algorithm to find the optimal values of d() we will use
the following observations.
Define
cijd = reduced arc length of (i, j)
cij  d (i)  d ( j )
Property 5.2
a) For any directed cycle W
c
( i , j )W
d
ij

c
( i , j )W
ij
b) For any directed path P from node k to node l,

( i , j )P
cijd 
c
( i , j )P
ij
 d (k )  d (l )
c) If d() represents the optimal shortest path distances,
cijd  0 i, j   A
This is another optimality condition
3
Generic label correcting
Assume no negative cycles.
Maintain d() at each stage


d ( j )   if there is no path from s to j
d ( j )  UB on the length of the path from s to j
Let Pred (j) be the predecessor to j for a path with length d(j)
Use this to trace the path at the end
algorithm label-correcting;
begin
d(s): = 0 and pred(s): = 0;
d(j): =  for each j  N  s ;
while some arc (i, j) satisfies
begin
d  j   d (i)  cij
do
d ( j ) : d (i)  cij ;
pred(j): = i;
end;
end
4
2
2
4
6
2
4
1
3
3
4
5
1
6
6
5
Correctness of the label correcting algorithm (cont’d)
When the label correcting algorithm stops each (i, j) in the predecessor graph
has
cijd  0
Why?
Implies that the length of the path from s to k = d(k)
Prop 5.2 (c)
d() optimal if
cijd  0 i, j   A
 The predecessor graph is a shortest path tree.
(So we know the algorithm does find an opt. solution)
6
Finite convergence of the label correcting algorithm
Know only finite steps when data integral.
7
Modified Label Correcting
How do we find an arc violating the optimality conditions?
1. Scan whole list of arcs O(m); SLOW
2. Keep list of arcs that might violate the optimality conditions
a. LIST = then optimal
b. Else
1. Remove (i, j) from LIST that violates optimality condition;
update d(j).
2. decrease d(j) may make some
c djk  0
note all
c djk  d ( j )  d (k )  c jk
cijd still optimal
 incoming arcs still optimal, but emanating may not be
 if d(j) decreases, add arcs in A(j) to LIST
3. Instead of LIST being arcs, just keep nodes that had d(j)
decrease. Then look at A(j)
8
algorithm modified label-correcting;
begin
d(s): = 0 and pred(s): = 0;
d(j): =  for each j  N  s ;
LIST : = {s}:
while LIST   do
begin
remove an element i from LIST;
for each arc (i, j)  A(i) do
if d  j   d i   cij then
begin
d ( j ) : d (i)  cij ;
pred(j): = i;
if j LIST then add node j to LIST;
end;
end;
end;
Can prove correctness by induction
O(nmC)
d(j) updated 2nC times max,
 2nC A(i)  OnmC 
iN
9
Special Implementations of Label Correcting
A. O(nm)
Scan all arcs in A
if
d ( j )  d (i)  cij
then update
Can show max n-1 passes
B. Dequeue - read on your own.
10
Detecting Negative Cycles
1. Generic label correcting
a) if d(i) for any i < -nC => negative cycle
Why?
b) Periodically look at predecessor index
Start at node k, trace predecessor until hit marked
node l
if k = l => negative cycle
2. FIFO Label correcting

if any node updated more than n-1 times

or 1 b)
11
All pairs shortest path problem
One method, Repeated Shortest path algorithm
1. All
cij  0
do single source n times
O(nS(n,m,C))
2. Arbitrary cij
Do 1 FIFO label correcting for some s, consider reduced arc costs
cijd  cij  d i   d  j 
Now solve
cij  0
(Using
c ijd ) n times
O(nm + n S (n, m, C))
12
All pairs shortest path optimality conditions
Theorem 5.5 (All-Pairs Shortest Path Optimality Conditions). For every
pair of nodes [i, j]  N x N, let d [i, j] represent the length of some directed
path from node i to node j. These distances represent all-pairs shortest path
distances if and only if they satisfy the following all-pairs shortest path
optimality conditions:
d[i, j ]  d[i, k ]  d[k , j ]
for all nodes i, j, and k
All-Pairs shortest path generic label correcting algorithm
algorithm all-pairs label-correcting;
begin
set d[i, j]: =  for all [i, j]  N x N;
set d[i, i]: = 0 for all i  N;
for each (i, j)  A do d[i, j]: = cij;
while the network contains three nodes i, j, and k
satisfying d i, j   d i, k   d k , j 
do d i, j  : d i, k   d k , j ;
end;
13
Floyd-Warshall Algorithm
Based on DP idea
d k i, j   shortest path from i to j using only nodes 1,  k-1
as intermediates
Want
Start
d n1 i, j 
 i, j
d 1 i, j 
d 2 i, j 

d n1 i, j 
Prop 5.6


d k 1 i, j   min d k i, j , d k i, k   d k k , j 
Why correct?
Floyd-Warshall good for dense networks
Easier then n shortest paths but requires more memory
14
algorithm Floyd-Warshall;
begin
for all node pairs [i, j]  N x N do
d i, j  :  and pred [i, j]:=0;
for all nodes i  N do d[i, i]: =0;
for each arc (i, j)  A do d[i, j]:=cij and pred [i, j]:=i;
for each k : = 1 to n do
for each [i, j]  N x N do
if d[i, j]>d[i, k]+d[k, j] then
begin
d[i, j]:=d[i, k] + d[k, j];
pred [i, j]:=pred [k, j];
end;
end;
15
Finding negative cycles
1) d i, i   0
2)
d i, j   nC
Floyd-Warshall uses 1)
Generic all pairs 1), 2)
16
Download