Shortest Path Problems Network Flows Topic 3: F.-Javier Heredia

advertisement
MEIO/UPC-UB : NETWORK FLOWS
Network Flows
UPCOPENCOURSEWARE number 34414
Topic 3:
Shortest Path Problems
F.-Javier Heredia
This work is licensed under the Creative Commons AttributionNonCommercial-NoDerivs 3.0 Unported License. To view a copy of this
license, visit http://creativecommons.org/licenses/by-nc-nd/3.0/ or send a
letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View,
California, 94041, USA.
(Chap. 4&5 Ahuja,Magnanti,Orlin)
•
•
•
Notation and assumptions.
Classification of Shortest Paths Problems.
Label-setting algorithm: Dijkstra.
–
–
–
–
•
Label-correcting algorithm.
–
–
–
•
Description.
Example.
Convergence.
Complexity.
Description.
Example.
Properties, convergence and negative cycles.
Exercises.
F.-Javier Heredia http://gnom.upc.edu/heredia
SPP- 2
Shortest Paths Problems
MEIO/UPC-UB : NETWORK FLOWS
3.- Shortest Paths Problems (SPP)
MEIO/UPC-UB : NETWORK FLOWS
Notation and assumptions.
• SPP: given a directed network G=(N,A) with cost c, the SPP is to
determine how to send one unit flow as cheaply as possible from
node s to each one of the nodes (N-{s}).
z=
∑c x
(i,j)∈ A
∑ xij −
{ j:( i , j )∈A}
ij ij
(4.1a)
i=s
n − 1
x
=
(4.1b)

∑
ji
{
}
i
N
s
−
∀
∈
−
1
{ j:( j ,i )∈A}

xij ≥ 0 ∀(i, j ) ∈ A
(4.1c)
Assumptions :
i.
All costs c are integers.
ii.
iii.
iv.
The network contains a directed path from node s to every other
node in the network.
The network does not contain any negative cycle (i.e. a directed
cycle of negative length).
The network is directed.
F.-Javier Heredia http://gnom.upc.edu/heredia
SPP- 3
Shortest Paths Problems
•
 min

s.to :






• Types of SPP:
– Finding shortest paths from one node to all other
nodes when arc lengths are nonnegative.
– Finding shortest paths from one node to all other
nodes for networks with arbitrary arc lengths.
– Finding shortest paths from every node to every
other node.
• SPP algorithms:
– Label-setting: valid for (a) SPP defined on acyclic
networks (no directed cycles) and (b) for SPP with
nonnegative arc lengths. The fastest SPP alg.
– Label-correcting: valid for all classes of SPP,
including those with negative arc lengths.
F.-Javier Heredia http://gnom.upc.edu/heredia
SPP- 4
Shortest Paths Problems
MEIO/UPC-UB : NETWORK FLOWS
Classification of the SPP and algorithms
Dijkstra’s algorithm: finds the shortest path from the source node s to
all other nodes over the graph G=(N,A) with non-negative arc costs
cij ≥ 0.
begin
S:=∅; S:=N ;
d(i):= ∞ for each node i∈N ; d(s):= 0 ; pred(s):= 0 ;
while | S | < n do
let i∈ S be a node such that d(i) = min{ d( j) : j ∈ S } ;
S:= S ∪{i}; S:= S -{i};
for each (i, j) ∈A(i) do
if d( j) > d( i) + cij then d( j):= d( i) + cij and pred( j):= i ;
end while
end
• d( i) : label that represents the distance from node s to node i.
• pred( j) : pred( j) = i ⇒ the arc (i,j) belong to the shortest path between s
and j .
• A( i) : set of arcs which origin is the node i.
F.-Javier Heredia http://gnom.upc.edu/heredia
SPP- 5
Shortest Paths Problems
MEIO/UPC-UB : NETWORK FLOWS
Dijkstra‘s Algorithm: description.
• We can see the main idea in the following Applet:
– http://www-b2.is.tokushima-u.ac.jp/~ikeda/suuri/dijkstra/DijkstraApp.shtml?demo1
• Solve the following SPP with Dijkstra:
d( i)
i
2
1
cij
2
d( j)
j
∞
4
2
1
3
2
6 ∞
(Solution)
5
∞
F.-Javier Heredia http://gnom.upc.edu/heredia
SPP- 6
Shortest Paths Problems
MEIO/UPC-UB : NETWORK FLOWS
Dijkstra‘s Algorithm: example.
•
•
Convergence Theorem:
“At termination the Dijkstra’s algorithm, the labels d(i)
of nodes i∈ N represent the distances of the shortest
path s→ i.”
Proof: by induction. Assuming that at the iteration k of
it is true that:
a) The distance d( i) of the nodes S is optimal.
b) The distance d( j) of the nodes S is the shortest path length
from the source provided that each internal node in the path
lies in S.
It will be shown that at iteration k+1 both hypothesis
are still true. At the end of the algorithm, S= N and
the hypothesis a) is verified in all nodes
F.-Javier Heredia http://gnom.upc.edu/heredia
SPP- 7
Shortest Paths Problems
MEIO/UPC-UB : NETWORK FLOWS
Dijkstra‘s Algorithm: convergence (I).
Proof a)
• Let i∈S be the node to be transferred to S.
• By the hypothesis b), d(i) is the optimal distance s→ i over S.
• We will show that for any path P s→ i passing through S the distance d(P) is ≥ d(i):
S
S
– Let P=P1+P2 be any path s→ i that contains at least
pred(i)
one node l ∈S , l≠i.
i
– b) ⇒ the length P1 is d(P1 )≥ d(l).
P2
l
s
P1
Proof b)
S
d(i)
S
i
pred(j)
– cij ≥ 0 ⇒ d(P2) ≥ 0 ⇒ d(P)=d(P1+P2 ) ≥ d(i) there is
not any path P over S with a distance lower than
d(i) ⇒ d(i) is the optimal distance s→ i ■
– Let i∈S be the node to be transferred to S.
cij
j
s
– d(i) ≤ d(l) ≤ d(P1)
d(j)
– Let’s assume that b) is true, i.e. the length d(j) of
the nodes of S are minimal over S.
– Once transferred i to S , the labels and
predecessors of S are updated: if d( j) > d( i) + cij
then the shortest path s→ j over S includes i ⇒
d( j):= d( i) + cij is the length of the new shortest
SPP- 8
path s→ j over S ■
F.-Javier Heredia http://gnom.upc.edu/heredia
Shortest Paths Problems
MEIO/UPC-UB : NETWORK FLOWS
Dijkstra‘s Algorithm: convergence (II).
• Running time (algorithmic complexity): the
compational burden of the Dijkstra’s algorithm is due to
two operations:
– Node selection: in every iteration all the nodes of S are
explored in order to select the node to be transferred to S :
n+(n-1)+(n-2)+...+2+1 = (n2+n)/2 = O(n2)
– Distance updates: the algorithm perform this operation |A(i)|
times for node i∈N. Each distance update operation
requires O(1) time: hence total time is O(m). ∑i∈N A(i ) = m
• Dijkstra solve the SPP in O(n2) time (the best possible
time for completely dense networks).
– The simplex algorithm for network flow SPP requires O(n2m),
O(n3) or O (nmlog C) depending on the implementation.
F.-Javier Heredia http://gnom.upc.edu/heredia
SPP- 9
Shortest Paths Problems
MEIO/UPC-UB : NETWORK FLOWS
Dijkstra‘s Algorithm: complexity.
Label-Correcting algorithm: finds the shortest path from the source
node s to the rest of nodes over a graph G=(N,A) without negative
cycles.
begin
d(s):= 0 ; pred(s):= 0 ;
d(j):= ∞ for every node j∈N-{s} ;
cdij::= cij+ d(i) - d(j) ∀(i,j)∈A ;
while ∃(i,j)∈A : cdij < 0 do
d( j):= d( i) + cij ; pred( j):= i ;
cdkl::= ckl+ d(k) - d(l) ∀(k,l)∈A | k=j ó l=j;
end while
end
• cdij = cij+d(i) - d(j) : reduced arc length.
• d( i) : label that represents the distance from s to node i.
• pred( j) : pred( j) = i ⇒ arc (i,j) belongs to the shortest path between
s and j .
F.-Javier Heredia http://gnom.upc.edu/heredia
SPP- 10
Shortest Paths Problems
MEIO/UPC-UB : NETWORK FLOWS
Label-correcting algorithm
d( i)
i
2
( cij )
4
d( j)
j
4
3
1
3
1
5
(Solution)
F.-Javier Heredia http://gnom.upc.edu/heredia
SPP- 11
Shortest Paths Problems
MEIO/UPC-UB : NETWORK FLOWS
Label-correcting algorithm: example.
• The value of the reduced arc length cdij at
the solution represents the increase of the
optimal cost of the SPP if arc (i,j) would
belongs to the solution.
• Computational cost: O(n2C) with
C=max{cij : (i,j)∈A}
– Proof: page 140. AMO
– Dijkstra O(n2)
F.-Javier Heredia http://gnom.upc.edu/heredia
SPP- 12
Shortest Paths Problems
MEIO/UPC-UB : NETWORK FLOWS
Label-Correcting Algorithm: properties.
•
Convergence Theorem:
“Let d(j) be the label of node j∈ N that represents the distance of a path
s→ j. Then d(j) represents the distances of the SPP if and only if the
following optimality conditions are satisfied:
d(j) ≤ d(i) + cij ∀(i,j)∈A ”
•
(1)
Proof:
⇒ If d(j) is the distance of the shortest path s→j, then (1) applies: otherwise the
shortest path s → i → j would improve the distance d(j) ■
⇐ Let’s assume that (1) applies. Let´s consider the directed path
s = i1 → i2 → i3 → ... → ik= j
Applying (1) to nodes i1 to ik we get:
d ( j ) = d (ik ) ≤ d (ik −1 ) + cik −1ik
d (ik −1 ) ≤ d (ik − 2 ) + cik −2ik −1

d (i2 )

≤ d (i1 ) + ci1i2
Adding all the inequalities we have:
d ( j ) ≤ cik −1ik + cik −2ik −1 +  + ci1i2 =
=
∑
( i , j )∈P
Furthermore
cij ∀camí P
d( j) is optimal.
F.-Javier Heredia http://gnom.upc.edu/heredia
SPP- 13
Shortest Paths Problems
MEIO/UPC-UB : NETWORK FLOWS
Label-correcting algorithm: convergence.
• Negative cycle: directed cycle W such that
∑
( i , j )∈W
cij < 0
• Theorem: “ if the graph G=(N,A) contains any negative
cycle, then it is not possible to find a set of optimal
distances d(·) that satisfy (1)”
– Proof: if W is a negative cycle, we can verify that
d
=
c
∑ ij
( i , j )∈W
∑
( i , j )∈W
cij < 0
On the other hand, if d(·) are optimal, (1) imposes
∑
( i , j )∈W
cijd ≥ 0
Which is a contradiction■
F.-Javier Heredia http://gnom.upc.edu/heredia
SPP- 14
Shortest Paths Problems
MEIO/UPC-UB : NETWORK FLOWS
Label-correcting algorithm: negative cycles.
• Solve the SPP of the following networks with the appropriate
algorithm.
s
cij
i
j
3
2
s 1
5
0
4
6
7
7
2
s 1
8
3
30
(c)
11
0
4
5
12
6
9
12
(b)
3
10
5
5
2
4
5
-20 -5
25
1
5
(a)
0
2
7
4
10
2
3
3
5
4
1
2
7
9
3
4
6
3
1
5
1
11
10
4
s 1
-2
-1
4
1
2
3
6
5
5
-4
6
-4
(d)
F.-Javier Heredia http://gnom.upc.edu/heredia
7
SPP- 15
Shortest Paths Problems
MEIO/UPC-UB : NETWORK FLOWS
Dijkstra Algorithm: exercises (I).
• Solve the following exercises from book AMO:
4.1,4.3, 4.6, 4.7, 4.9, 4.13.
• Each one shows an application of the SPP. The
tasks to complete are:
– To formulate the SPP.
– If the statement does not specify the MFP formulation,
complete the network topology with appropriate costs.
– Solve numerically with GIDEN the SPP formulated
• Send the answers (.pdf o .doc i .gdn) by e-mail
before the date: ../../..
F.-Javier Heredia http://gnom.upc.edu/heredia
SPP- 16
Shortest Paths Problems
MEIO/UPC-UB : NETWORK FLOWS
Dijkstra algorithm: exercises (II).
d( i)
i
cij
∞→ 4
0
6 ∞•
2
∞→ 6
2
1
3
6
2
d(1) = min{ d( j) : j ∈ S } = 0
S:= {1}; S:= {2,3,4,5,6}
d:= [0, 4, 6, ∞, ∞, ∞]
pred := [0, 1, 1, -, -, -]
• 2nd iteration: | S | = 1 < n = 6
4
2
1st iteration: | S | = 0 < n = 6
–
–
–
–
5
∞
4
1
– S:=∅; S:={1,2,3,4,5,6}
– d:= [0, ∞, ∞, ∞, ∞, ∞]
– pred := [0, -, -, -, -, -]
1
3
2
∞→ 6
0
j
2
1
• Initialization:
∞
4
2
2
d( j)
5
∞→ 5
6 ∞
–
–
–
–
d(2) = min{ d( j) : j ∈ S } = 4
S:= {1,2}; S:= {3,4,5,6}
d:= [0, 4, 6, 6, 5, ∞]
pred := [0, 1, 1, 2, 2, -]
F.-Javier Heredia http://gnom.upc.edu/heredia
SPP- 17
Shortest Paths Problems
MEIO/UPC-UB : NETWORK FLOWS
Dijkstra algorithm: example (I).
d( i)
i
4
2
0 1
cij
2
• 3rd iteration: | S | = 2 < n = 6
6
4
1
2
5
5
2
6
4
4
2
1
j
2
3
6
0
d( j)
2
2
5
5
d(5) = min{ d( j) : j ∈ S } = 5
S:= {1,2,5}; S:= {3,4, 6}
d:= [0, 4, 6, 6, 5, 12]
pred := [0, 1, 1, 2, 2,5]
• 4th iteration: | S | = 3 < n = 6
1
3
6
6 ∞
↓
12
–
–
–
–
6 12
(not updated)
–
–
–
–
d(3) = min{ d( j) : j ∈ S } = 6
S:= {1,2,3,5}; S:= {4, 6}
d:= [0, 4, 6, 6, 5, 12]
pred := [0, 1, 1, 2, 2,5]
F.-Javier Heredia http://gnom.upc.edu/heredia
SPP- 18
Shortest Paths Problems
MEIO/UPC-UB : NETWORK FLOWS
Dijkstra Algorithm: example (II).
d( i)
i
4
2
0 1
cij
2
4
2
5
5
2
1
2
5
5
–
–
–
–
6
9
d(4) = min{ d( j) : j ∈ S } = 6
S:= {1,2,3,4,5}; S:= {6}
d:= [0, 4, 6, 6, 5, 9]
pred := [0, 1, 1, 2, 2,4]
• 6th iteration: | S | = 5 < n = 6
–
–
–
–
4
2
3
6
6 12
↓
9
6
4
1
6
1
2
• 5th iteration: | S | = 4 < n = 6
j
2
3
6
0
d( j)
d(6) = min{ d( j) : j ∈ S } = 9
S:= {1,2,3,4,5,6}; S:= ∅
d:= [0, 4, 6, 6, 5, 9]
pred := [0, 1, 1, 2, 2,4]
• 7th iteration: | S | = n = 6: STOP
• Shortest path tree:
T = {(1,2),(1,3),(2,4),(2,5),(4,6)}
F.-Javier Heredia http://gnom.upc.edu/heredia
SPP- 19
Shortest Paths Problems
MEIO/UPC-UB : NETWORK FLOWS
Dijkstra Algorithm: example (III).
d( i)
d( j)
( cij ,cdij )
i
j
∞
∞→ 2
(4,-)
2
0
(3,-)
1
3
∞
2
(1,-)
(4,-∞)
2
0
4
∞
4
3
∞→ 2
5
∞
• 1st iteration: cd12 < 0
• 2nd iteration: cd13 < 0
(3,-)
(1,-)
– d:= [0, ∞, ∞, ∞, ∞];
– pred := [0, -, -, -, -, -];
– cd := [-∞, -∞, -,-,-,-];
– d:= [0, 2, ∞, ∞, ∞];
– pred := [0, 1, -, -, -];
– cd := [0, -∞, -∞, -∞,-,-];
5
∞
1
• Initialization:
– d:= [0, 2, 2, ∞, ∞];
– pred := [0, 1, 1, -, -];
– cd := [0, 0, -∞, -∞, -∞,-];
F.-Javier Heredia http://gnom.upc.edu/heredia
SPP- 20
Shortest Paths Problems
MEIO/UPC-UB : NETWORK FLOWS
Label-correcting algorithm: example (I).
d( i)
i
cij
2
2
0
(4,-∞)
j
∞→ 6
4
(3,-)
1
3
2
2
(1,-∞)
(4,0)
2
0
d( j)
5
∞
6
(3,-)
3
2
– d:= [0, 2, 2, 6, ∞];
– pred := [0, 1, 1, 2, -];
– cd := [0, 0, 0, -∞, -∞,-];
• 4th iteration: cd25 < 0
4
1
• 3rd iteration: cd24 < 0
(1,-∞)
5
∞→ 5
– d:= [0, 2, 2, 6, 5];
– pred := [0, 1, 1, 2, 2];
– cd := [0, 0, 0, 0, -2, 4];
F.-Javier Heredia http://gnom.upc.edu/heredia
SPP- 21
Shortest Paths Problems
MEIO/UPC-UB : NETWORK FLOWS
Label-correcting algorithm: example (II).
d( i)
i
d( j)
cij
j
2
2
0
4
(3,4)
1
3
2
(1,-2)
2
• 6th iteration: cd ≥ 0 : STOP
5
5→ 3
2
0
– d:= [0, 2, 2, 6, 3];
– pred := [0, 1, 1, 2, 3];
– cd := [0, 0, 0, 2, 0, 6];
6
(4,0)
• 5th iteration: cd35 < 0
(4,0)
6
4
(3,6)
1
3
(1,0)
2
5
3
F.-Javier Heredia http://gnom.upc.edu/heredia
SPP- 22
Shortest Paths Problems
MEIO/UPC-UB : NETWORK FLOWS
Label-correcting algorithm: example (III).
Download