Transpn Res. Vol. 2, pp. l-11.
Pergamon
Press 1968. Printed in Great Britain
THE k SHORTEST
CHAINS
IN A GRAPH
M. SAKAROVITCH Minis&e
de l’Equipement,
Service des Etudes et Recherches Arcueil/Seine, France
sur la Circulation
Rout&e,
(Received 24 April 1967) 1. INTRODUCTION
and traffic assignment are both basic steps of any traffic study. A new approach to the modal split problem will probably imply the computation of the k shortest chains. Similarly it is well known that the so-called “all or nothing” traffic assignment technique cannot, in general, simulate reality in a satisfactory way. More sophisticated processes may imply the computation of more than one shortest chain between origin destination pairs (Pollack, 1961; McLaughlin, 1966). We (Sakarovitch, 1966) have given algorithms to find the k shortest routes (a route may include a cycle) and the k shortest chains allowing the distances to be negative. Here we will concentrate, in view of practical applications to traffic engineering, on the k shortest chains problem. We admit negative lengths provided there is no “negative” cycle (a cycle of negative length) since this does not add any complexity to the algorithm. In a given graph G = (N, A) where N represents the set of nodes and A the set of directed arcs, a length a(xv) is associated with every arc (xu) E A. A chain from x0 to x, is a sequence of nodes and arcs P = [x03(XIIXI), x1, (x1 x,)7 ...,X,-l, (X,-l x,)9 x,1 such that x~EN, i=O,...,n MODAL
split
(Xi--lx,.) E 4
i = 1, . . ..n
All the xi’s are different. The length of chain P is 12
w = S-1 c 4X$-lXi) A chain P will also be considered as a partial subgraph (see Berge, 1962) of G containing nodes x,,, . . ., x, and arcs (x0 x1), . . ., (x,__~x,). Let 0 and D denote two special nodes of G. Pa will denote the qth shortest chain from 0 to D. Along with the definition of chains PI, . . ., Pq we will define a graph G, and we will always have : P,u...uP,cG, Let G, = (N,, 4) Aq=A-A, k, = min{k, maximum number of O-D-chains in Ge}
The combinatorial problem of finding the k shortest chain is not, conceptually, difficult one and the idea on which the algorithm is based is quite straightforward.
a
M. SAKAROVITCH
2
At any stage of the algorithm to know the shortest chains in G,. Then a chain P* is found we have : if p;l+1 P q+1= if i P”
list S, ={Pr, . . . . Pg.,P&,, . . . . P&j of the k, which has at least one arc outside G, and f(P;+,*) d l(P “) Z(P *) < l(P;+,)
G, and the list S, are then updated and the process begins again. Although the combinatorial problem is very simple, the computation may become very tedious. The merit of the method presented here (if it has any) is to reduce the amount of computation to what we believe is a minimum and to perform the successive computations in a very ordered way which will be quite easy to code on a computer and at the same time will not take too many memory positions. As an order of magnitude, once the shortest chain has been found, the second shortest and the third shortest chains are to be computed in about the same time as the shortest chain. One never has to keep track of more than k chains. This method is different from the one given by Hoffman and Pavley (1959), Bellman and Kalaba (1960), Pollack (1961), Clarke et af. (1963) and Cruon and Kaufman (1964). The algorithm is initiated by finding the shortest chains from 0 to every node using an algorithm such as the one in Dantzig (1951) if all lengths are non-negative and the one in Ford and Fulkerson (1962) if there are negative lengths. In the next section an algorithm to construct the k shortest chains from 0 to D is described and it is justified in Section 3. An example of application is given in Section 4. Notice that minor modifications of this algorithm allow one to find the chains shorter than a given length. If capacities were to be considered, and immediate application of this last problem could be: What quantity can be shipped from 0 to D if we demand that the maximum time to travel be less than or equal to a certain given number? Notice also that, by taking k = ~13,this algorithm gives a way of finding all the chains from 0 to D in a graph. _ 2.
ALGORITHM
FOR
FINDING
THE
k SHORTEST
CHAINS
Step 0 Find all the shortest chains from 0 to D: PI, . . . . Pq. If q 3 k : terminate. Determine for all x E N:
Note that P;,, infinite). Let
T(X) = shortest
distance
from 0 to x.
q(x) = shortest
distance
from s to D.
does not exist (the length
of a chain
which does not exist is taken
as
G,=P,uP,u...uP, s, = {PI, . . ..P.} Step 1 Let S,(x) =
min [n(y) +a(~x)] (2/,5) EZQ I T(X)
(
for
x E NT
for
x$N,
(1)
E = x E Nl5,W + 44 = ;;; [5,(v)+ &Y)I =
-,,I
(2)
The k shortest chains in a graph
3
(1.1). If 7~~= co and k, < k : terminate. The problem is infeasible. The k, shortest chains have been found, there is no other chain. (1.2). If 7rITp c co: determine m by KP$+,) G rp < I@+,+3 Let Pnti=P&,
i-
I,..., m
(1.2.1). If q + m > k : terminate. The solution has been found. (1.2.2). If q+m< k: go to step 2. Step 2 Let x E E. Backtrack in the definition of c,(x) and of n(z) up to a node u E G,, u# x (if there is more than one predecessor for a node, choose one). Similarly backtrack in the definitions of q(x) up to a node (possibly x itself) DE G, (if there is more than one successor for a node, choose one). Label the arcs used in the backtracking processes. Let C(u; v) be the chain from u to u including only labelled arcs. Delete the labels on labelled arcs. From the sequence S, = {PI, . . .. Pg, Pp+l, . . ., f’q+m,P&,,_l, . . . . P&J we generate two sequences S,’ and S,“. Sn’ is the sequence of chains from 0 to u which are parts of those chains in S, which contam u. Sp.Mis the sequence of chains from v to D which are part of those chains in S, which contam v. Chains in S,’ and SqMare supposed to be ordered according to increasing lengths. Let s,, = (Pi, . . . . P/>, q’ f k, < k s/
= (PI”, . . . . P,.“}, q”
Define the auxiliary bipartite graph H = (X, U) where X= X’+X”,
IX’l=q’,
(x’x”) E U if
x’ E X’
lx”l=qn and
X” E X”
Note the nodes in X’ and X” respectively x,‘, . . ., xq,’ and x1”, . . ., xq.“. Assign to each node x E X a weight: w(x) =
u-27, if x = x,’
l
l(P*“), if x = xi’ Colour in red all the arcs originating in x1’. Label all the nodes of X” with v(xj”) = w(x~‘)+w(x~“). Go to step 3. Step 3 LetjE{l,
. . .. q”} such that 0(x,“) =
min
s-1, .... f
{v(x,“)}
(3.1). If v(x,“) = co: go to step 5. (3.2). If 0(x,“)
M. SAKAROVITCH
4
Go to step 4. (3.2.2). If i = q’: let u(xj”) = co. Go to step 4. Step 4 (4.1). If Pi’ u C (u; v) upi” contains a cycle. Go to step 3. (4.2). If Pi’ u C (u; U) u Pi” makes a chain (in G). Insert this chain in the sequence S, in such a way that the newly constructed sequence be still ordered according to increasing lengths and do not contain more than k chains (keep the k first). (4.2.1). If the rank of the newly created chain in S, is greater than or equal to k, go to step 5. (4.2.2). If the rank of the newly created chain in S, is less than k, go to step 3. Step 5 Let n be the number chains :
of chains
of length
n, added
to S, (may be n = 0). Note these
Pfi+m+1,.‘.) pq-+m+n. and the following
chains
in the sequence: pq+mtn
q+m+n+l’ “.’
where the new sequence
pq+m+.n h,,+m+n
at most k chains.
S, contains
Note Sq+aL+n this new sequence
and
G ,r+m+n = G,u C(u; 0) Let 4 = Sq+nl,-n (5.1). If q k: terminate. A solution has been obtained. 3. JUSTIFICATION
OF
THE
ALGORITHM
Lemma 1 In step 0 there is no other chain completely included in G, than PI, . . . . Pq. Proof. If (xY)EA q: n(Y) -n(x) = a(xy). Then for any chain P* from 0 to D in G,, l(P *> = Z(P,) = . . . = Z(Pq) and P* is one of the chains PI, ..,, Pq. Lemma 2 nq, as defined in equation (2), is less than or equal to the length of any chain from 0 to D not completely included in G,. Proof. It is sufficient to show that for each route P* which contains at least an arc (x,Y) in A,, E(P *) 2 rq. From the definition of 7~ and 7 we have, if (x y) E P* n Aq KP *) 2 4x) +
a(xy) + T(Y)
But from (1) n(x) + &Y) and the property
of shortest
2 5,Q
distances m(Y) G n(x) + &Y)
Then, from (2), 5qw + 70
2 rq
(3)
The k
shortest chains in a graph
5
which together with (3) implies Lemma 3
In step (3.2) if v (xi”)
At the end of step 3 either U(Xj”)< COand a new arc ending in Xj” is coloured or no arc ending is xj” is coloured in red and V(Xj”)is taken to be infinite. Lemma 4
If we consider that an arc (x’x”) E U has a length &x”) = MJ(x’)+w(x”), the process of step 3-step 4 colours successively in black the arcs of U in the order of increasing lengths. Proof. Suppose we are at a certain stage up to which the lemma is true. For each node x8” E X”, v(x,“) represents the length of the shortest arc ending in x8” not yet coloured in black, i.e. coloured in red [see (3.2.1), (3.2.2)]. Then by taking v(xj”) = min {u(x,“)} we will choose the red arc of minimum length. 8=1.
. . . . P’
Lemma 5
At each passage in step 5 a new sequence S, is generated which contains, ordered according to increasing lengths, the k, shortest chains in G,. The q first chains of the sequence are the shortest chains in G. Proof. From Lemma 1, this is true (with k, = q) at the beginning. Suppose the lemma is true at some stage, we will prove it is still true at the next stage. From Lemma 2, the updating of S, at the end of step 1 is licit. Sequences Sp’ and S,” contain respectively all the chains in G, from 0 to u and from u to D respectively (or at least those chains which by the process of step 3 may generate chains from 0 to D inserting in the k first elements of S,). If in step 4 it is found that Pi’ u C(u; V)u Pj” is a chain, the length of this chain is ol(x,‘x,“) + 1[C(u; u)]. From Lemma 4, the chains thus constructed are created according to increasing lengths. Since there is a one-to-one correspondence between the arcs in U and the possible combinations of a chain in SQ’and a chain in S,“, all the chains in G, u C(U; v) and containing C(u; U) (or at least a sufficient number of themt) are constructed. Now the chains in G, u C(U; v) and not containing C(U; v) are in G, and we have already got them. Finally, from Lemma 2 the updating of sequence S, in step 5 is licit. Theorem
In a finite number of steps the algorithm leads to a solution or shows that no solution exists. Proof. Graph H is finite since q’, q” < k. Each time we pass through step 3 an arc of H is coloured in black. When all arcs are coloured in black we go to step 5 [see (3.1)]. We cannot cycle indefinitely in steps 3 and 4. Each time we pass through step 5 at least one arc is added to G,. Since the graph G is finite we can pass only a finite number of times through step 5. Thus the algorithm is finite. The algorithm can terminate in (l.l), (1.2.1), (5.2). If it terminates in (1.2.1) or in(5.2) an optimal solution has been obtained from Lemma 5. t Since we create them according to increasing lengths we can truncate the sequence when we have got k of them, which is what we do in step (4.2.1).
6
M. SAKAROVITCH
If it terminates in (1. l), this means from Lemma2 that there is no chain from 0 to D except the ones already found, the number of which is less than k. In this case the problem is infeasible. 4. EXAMPLE
Suppose
we want to find the four shortest
chains in the following
graph:
2 5
5
0’
I-
D
O q 7
2
2 3 Step 0
The shortest
chain is unique. PI = (0, I, 2,3, D), G, = PI,
/(PI) = 5
S, = (PI}.
k, = 1
a(0)
Step
= 0,
z(l) = 1,
n(2) = 2,
7r(3) = 3,
T(D)
~(0) = 5,
~(1) = 4,
~(2) = 3,
~(3) = 2,
q(D) = 0
Nodes
n(x)
q(x)
i-,(x>
rlw + 5,(x>
0 1
0 1
2 3 D
2 3 5
= 5
1
5
co
co
4 3 2 0
.5 5 7 7
9 8 9 7
E=(D) ?Tl = 7
since Pzl does not exist
m = 0 Step 2
x=D &(D)
= ~(2)+a(2D)
= 7
u=2 v=D s,’ = ((0, 1,2)}, S,” = ((D, D)},
PI’ = (0,1,2),
l(P,‘) = 2,
q’ = 1
PI” = (D, D)
l(P,“) = 0,
qn = 1
The k shortest chains in a graph
P I,
w
XI
H=
0 I
(x,“l = 0
Iv (,;I=
Xl
2
Colour arc (xi x1”) in red. Step 3 (3.2). Colour (x,‘x~“) in black. (3.2.2). i = 1, t)(x,“) = co. Go to step 4. Step 4 Pi
u
((20))
U PI” =
1(0,1,2,D)
(0, 1,2,D) is a chain
= 7
S, = ((0, 1,2,3,@;
((Al,29 0))
(4.2.2). Go to step 3. Step 3 v(x,“) = co. (3.1). Go to step 5. Step 5 n=l,
q=2
Pz=(O,1,2,D) Ps2 does not exist. ‘32
=
G
s2
=
VI,
u N2@1 p21
(5.1). Go to step 1. Step 1
5&I
Nodes
4x1
?1(4
b&4
0
0
:
co
co
1 2 :,
1 2
3
:
f
5” 7 a,
t 9 cc
E = (2) ?r2= 8 m = 0 since Ps2 does not exist.
$4
+
8
M.SAKAROVITCH
Step 2 x=2 52(2) = n(0)+a(02)
= 5
u=o v=2 PI’ = (0, 0)s PI” = (2,3, D),
S,’ = ((0, O)), Sz” = {(2,3,D);
(2, D)>,
v Pz” = (2, D),
I,
XI
H-
l(P,‘) = 0,
q’ = 1
Z(P,“) = 3 l(P,“) = 5,
q” = 2
I,
X2
XI’
Colour
arcs (xl’xI”)
and (xI’xz”) in red.
Step 3
1(x1”) = 3 = min (0(x1”), v(x,“)} (3.2). Colour (xl’xl”) in black. (3.2.2). i = 1, u(x~“) = co. Go to step 4. Step 4 PI’ u ((02)) U PI” = (0,2,3,D) 1(0,2,3,
D) = 8 S, = wv,2,3,D);
(4.2.2).
is a chain (0,1,2,
Go to step 3.
Step 3 u(xzn) = 5 = min {co, 5) (3.2). Colour (xI’xz”) in black (3.2.2). i = 1, 0(x2”) = co. Go to step 4. Step 4 (0,2,
D) is a chain. 1(0,2, D) = 10
D); (~2~3,
m
The k shortest chains in a graph (4.2.1). Go to step 5. Step 5
n=l,
q=3
Pa = (0, 2,3,D) Pd3 = (0,2, D) G3
=
G3
s3
=
VI,
u HO,% pe,
pa,
p4”>
(5.1). Go to step 1. Step 1
Nodes
r(x)
rlw
5&I
?I@)+ 5,ta
0 1
0 1
2 3
5 4
co 5
al 9
2 3 5
W
W
: 0
D
7
9
W
W
E={1,3} ?r3= 9 m = 0 since Z(Pa3)> 9. Step 2
x=1 u=3 v=l s,’ = {(0,1,2,3);
S3”
=
-XL
(0,2,3)}, (1,2, D)),
2,3,D);
P; = (0, 1,2,3),
Z(P,‘) = 3
pz’ = (0,2,3),
Z(P,‘) = 6,
PI” = (1,2,3, D),
l(P,“) = 4,
Pa” = (1,2, D),
l(P,“) = 6,
H=
Colour arcs (x1’ x1”) and (x1’ x2”) in red. Step 3
u(x,“) = 7 = min (7,9} (3.2). Colour (xi x1”) in black. (3.2.1). Colour (xix,“) in red v(x,“) = 10. Go to step 4. Step 4
(4.1). (0,1,2,3,1,2,3,
D) is not a chain. Go to step 3.
q’ = 2
q” = 2
M. SAFCAROWTCH
10
Step 3 u(xS”) = 9 = min{l0,9} (3.2). Colour (xI’xz”) in black. (3.2.1). Colour (xi x2”) in red. u(x,“) = 12 Go to step 4.
Step 4 (4.1).
(0, 1,2,3,1,2,
D) is not a chain.
Go to step 3.
Step 3 u(xzn) = 12 = min{cq
12)
(3.2). Colour (xZ’xZ”) in black. (3.2.1). u(xz”) = co. Go to step 4.
Step 4 (4.1).
(0,2,3,1,2,
D) is not a chain.
Go to step 3.
Step 3 (3.1).
Step 5
n=o,
q=3
G, = %u{(31)) Go to step 1.
Step 1
E = (3) “3 = 9
m = 0 since
I(PdS)> 9
Step 2 x=3 u=o v=3 s,’ = ((0, O)},
P1’ = (0, O),
I(&‘) = 0,
q’ = 1
S,” = ((3, D)},
PI” = (3, D),
Z(P,? = 2,
q” = 1
The k shortest chains in a graph
Colour
11
arc (x1)x1”) in red.
Step 3 (3.2). Colour (xl’xI”) in black. (3.2.2). v(.xl”) = c/3. Go to step 4. Step 4 (0,3,
D) is a chain.
1(0,3, D) = 9
~,={(0,1,2,3,~);(0,1,2,~);(0,2,3,~);(0,3,~)} (4.2.1).
The rank of the newly created
chain is 4; go to step 5.
Step 5 n=l Pa = (093, D) (5.2).
An optimal
solution: PI = (0, 1,2,3, D) Pz = (0, 1,2, D) Ps = (0,293,
D)
P4 = (033, D) has been found. REFERENCES BELLMANR. and KALABAR. (1960). On kth best policies. J. Sot. ind. appf. Math. 8, 582-588. BERGEC. (1962). Thdorie des Graphes et Applications. Dunod, Paris. CLARKE S., KRIKORIANA. and RAUSENJ. (1963). Computing the N best loopless paths in a network. .I. Sot. ind. uppl. Math. 11, 1096-1102. CRUON R. and KAUFMAN A. (1964). Etude de la sensibilite en programme dynamique; politiques k-optimales en avenir certain. Revue FranGuise de Recherche OpCrationnelle, No. 32. DANTZIG G. B. (1951). The shortest route problem. Ops Res. 5, 27&273. FORD L. R. and FULKERSOND. R. (1962). Flows in Networks, pp. 130-134. Princeton University Press, New Jersey. HOFFMANW. and PAVLEYR. (1959). A method for the solution of the Nth best path problem. J. Ass. cornput.
Mach.
6, 506-514.
MCLAUGHLIN W. A. (1966). Multi-path system traffic assignment algorithm. Report RB 108, Department of Highways, Ontario, Canada. OVERGAARDK. R. (1966). Traffic estimation in urban transportation planning. Traffic Engineering and Town Planning Report No. 34, Department of Road Construction, Technical University of Denmark. POLLACK M. (1961). Solution of the kth best route through a network. J. math. Analysis Applic. 3, 547-559.
SAKAROVITCHM. (1966). The k shortest routes and the k shortest chains in a graph. Operations Center Report No. 66-32, College of Engineering, University of California, Berkeley.
Research