Volume 6, number 1
INFORMATION PRBCF%I:J@
LlZ..T’?-;‘RS
February 1977
A GENERALIZATION OF DIJKSTRA’S ALGORITHM *
Donald E. KNUTM
Computer Science Department. Stanford Univers!ty,Stanford, California94305, U.S.A.
Received 26 July 1976
Shortest paths, AND/OR graphs, context-free grammars, simultaneous
dynamic programming
E.W. Dijkstra [3] has introduced an important algorithm for finding shortest pzths in a graph, when
the distances on each arc of the graph are nonnegative.
The purpose of this note is to present an algorithm
which generalizes Dijkstra’s in essentially the same
way that tree structures generalize linear lists, or that
context-free languages generalize regular languages.
Let R, be the nonnegative real numbers extended
with the value +oo. We shall say that a function g(xi )
... . xk) from Rf into R+ is a superiorfunction if it is
monotone nondecreasing in each variable and if
.. . . xk) 3 ma(xl,
. . . . xk)
A-+a,
A -‘b&C)
l
,x,)
9
=‘c(A),
C+e,
B-+d(A,C,A),
C-, f(l?, A) .
Werea, b, c, d, e, f are supposed to correspond to
superior functions, and we may for example define
for all x1, .... xk .
When k = 0 the functiong is simply a constant element
of R+, and it is trivially considered to be a superior
function. When k = 2 several familiar functions g(x, y)
such as max (x, y) and x + y and max (1, x)* max( 1,y)
are all superior. Any functional composition of superior functions is a superior function of the variables
occurring in the composition.
A superior con text-free grammar is a con text-free
grammar in which all productions are of the general
form
y+g(x,,
decision !rees,
where Y, X1, . ... Xk are nonterminal symbols, g is a
terminal symbol corresponding to a superior function
(possibly a different superior function for each production), and the parentheses and commas are also terminal symbols. If k > 0, there are k -- 1 commas; if
k = 0 the right-hand side of the production is simply
written “g”, a terminal symbol which corresponds to
a nonnegative real constant.
For example, the follotiing productions define a
superior context-free grammar on the nonterminal
symbols A BC and the terminal symbols (), a b cd ef:
1. The problem in general
&l¶
minimax equations,
a=4,
d(x,y,z)=x+max@,z),
b(x,y)=max(x,y),
e=9,
c(x)=x+
1)
.
flx,y)=$(x+_,,+max(x,y)).
For every nonterminal symbol Y of a superior context-free grammar over the terminal alphabet T we let
L(Y) = {ok E P and Y +* a)
be the set of terminal strings derivable from Y. Every
string Q in L(Y) is a composition of supprior functions,
so it corresponds to a uniquely-defined nonnegative
real number which we shall call val(or). Thus, in the
above example we have
t
* This research was supported in part by Nation,; Science
Foundation grant MCS 72-03752 A03, by the OCfice of
Naval Research contract NO001476424330, and by IBM
Corporation. Reproduction in whole OF in part is permitted
for any purpose of the United States Government.
UA) = <a, b(c(a), 4, b(c(4, f@(a), a)),
b(c(N.c4a), e)), 4,
1
l
)
I
and
INFORMATIONPROCESSJNGLETTERS
the corresponding numerical values are
February 1977
Then m(Y) is the length of the shortest string deriv-
(4.5 7, 10, ...I.
able from Y in the given grammar. Alternatively if we
Iet
The problem we shall solve is to compute the smallest values corresponding to these languages, namely
gtr(x* , one,xk) = ma (xl,
m(Y)= mm (val (&)I0E L(n) ,
for each nonterminal symbol Y.
-e)xk) + 1 ,
l
then m(Y) is the minimum height of a parse tree for a
string derivable from Y in the given grammar.
Finally, if we let
g&t , .... xk) = max (x1 , . ... xk) .
2. Applications of the general problem
WChave m( I’) = 0
where djj 2 0 is the length of the road. Then add one
more production
or 00according as E( Y’)is nonemp ty
or empty. The algorithm we shall develop for the general problem reduces to the classicalemptiness-testing
algorithms of Bar-Hillel,Perles, and Shamir [2] or
Creibach [5] in this special case.
(C) Consider the AND/OR graphs which arise in
artificial intelhgence applications as in Nilsson [9] ,
section 4-5. In this case we use one nonterminal symbol for each “‘problem” to be solved. If some problemreduction operator shows that problem Y could be
solved if we,could solve all of problems X1, . .. . Xk,
then we introduce the production
CO303
Y”g(X,,
(A) Consider n + 1 cities {ce, cl, .. . . c,) connected
by a network of roads. For each road from q to ci, introduce the production
ci -V&j)
,
and the corresponding superior function
&i(X)= d/j + x 9
-e-,x,)
,
where g(x l ,..., xk)=x, + ... + xk + (cost of solving
Y given solutions to Xt , ... . &). Then m(Y) is the
minimum cost of a solution to Y, provided that the
L(Cj) correspond to the paths from ci to co, and the
cost of solving common subproblems is replicated (all
corresponding values will be the length? of those paths,
problem
solutions are considered independent). Our
For example, if there is a path from c3 to cd to c2 to
algorithm will therefore find a smallest AND/OR
ce , one of the elements of L(C3) will be g34(g42(g20(0))),
graph in this sense; but it is of limited utility for A.I.
and its corresponding value is d34 + de2 + d2o + 0.
applications because it deals with the set of a21“easy”
Therefore m(Cj) is the length of the shortest path
sublproblems,and this set is usually too large.
from Cj t0 CO, for all i.
A special case of the algorithm to be described
The algorithm we shah develop for the general
here
has been published by Martelli and Montanari.
problem reduces to Dijkstra’s algorithm in this speical
They
deal only with AND/OR graphs whose functions
case.
,
& 1 .. .. xk) have the form x1 + ... t xk + c With c > 0;
(B) Given a context-free grammar, replace each
furthermore they restrict their discussion to acyclic
production I --*8 in which the nonterminal symbols
AND/OR
graphs. The algorithm below works also in
of string 8 are Xr,... . Xk from left to right (including
the
presence
of cycles, and in this sense it is more genrepetitions) by
eral than the usual “dynamic programming” approach.
(D) given 212+ 1 probabilities pl , .. . . pn , 40, . .. . q,,
which sum to 1, construct the context free grammar
with nonterminal symbols Cs,j for 0 < i <i < n, where
the productions are
&3(x1 , .*,xk)=xl + ... +xk
Ci I:-*0
forO<i<n,
+ (the number of terminal symbols in 0) .
C.’
forO<i<k<j<n;
1,i:-* 6lJ“(ci, k- 1 t ck,j)
where 0 corresponds to the constant function zero.
In this superior context-free grammar, the elements of
2
Volume 6, number I
INI:ORMATIONPRWISSING LETTERS
and let
Iable
1
-
gij(x, J9) = x’ + J’ + pi+ ] + ma
+ pi + fJi + ... + 4i .
l
Then m(Co,n) + p I + ,.. f p,, is the expected number
of comparisons in an optimum binary search tree define by the given probabilities, in the sense of [6,
434-4351. The general algorithm we shall describe
here is not competitive with the special one in [6] ;
but it appears to be useful in connection with similar
problems, such as that of constructing optimum prugrams for decision tables.
(E) The author has successfully used this spproach
to generalize the optimum code-generation algorithm
of Aho and Johnson [ I] , treating the case of machines
f with asymmetric registers.
1977
Iicbruary
n
DIAI
P_
_
0
A}
tA,&
__.._
4
4
_I~____
__
PIBI lLlC1
_
44
I__-
WI
WI
_________
5
-__.-
__
-
4
-
;
_--__
9
9
7
are also possible, since all productions with Y on the
left-hand side can be deleted from memory as soon as
p[ Y] has been defined.
4. Proof of the algorithm
We must show that c([Y] = m(Y) whenever step 5)
is performed, since the relation
3. The generalalgorithm
(*)X ED implies ~1x1 = m(X) ,
Given a superior context-free grammar, the following algorithm determines m(Y) for all nonterminal Y.
The algorithm operates on elements p[ Y] and V[Y]
.-for each Y; initially all these elements are undefined,
but when the algorithm terminates p[ Y] will equal
m(Y) for all Y. The set D represents those Y for which
p[ Y] has been defined.
1) Set D to the empty set.
2) If all nonterminals are in D, stop.
3) For each nonterminal Y 4 D, compute
Y[Y] =min j&[Xr]
,..., j&])IY+g(Xr
,..., X&s
a production and {X,, . ... X,} E D} .
(If this set is empty, V[Y] is infinite.)
4) Choose Y $ D such that u[Y] is minimum, and set
Iul + WI
5) Include Y in D, and return to step 2).
l
For example, consider the grammar given in sect. 1.
The computation proceeds as in table 1, viewed in
step 3). Finally p[q + 7.
Actually&e values v/Y] do not need to be computed explicitly, they have been introduced here only
for convenience in stating and proving the algorithm.
By maintaining a priority queue of the current values
of p[ Y] with Ye D, the running time of this algorithm
is bounded by a constant times m log 11+ t, where
there are m productions and n nonterminais, and the
total length of all productions is C.Further rrfinements
will then be invariant throughout the algorithm.
It is clear that v[ Y] Z m(Y) in step 3); for if v[ Y]
< 00, (*) k-plies that
Y[Y] = v:’ &a,.
.... CQ)), where
Y-*,4(X1 ) .... X,) +* g&
, ...) Qk) ,
zrnd the terminal strings (x1, . . . . elk satisfy val (oi) 2
m(Xij for all i. Therefore the algorithm will be valrd
unless p[ Y] > m(Y) at some occurrence ol‘ stzp S).
In that case there will exist a terminal string (x such
that Y +* a and val (cu)< p[Y].
Assume that or is the shortest terminal string such
that 2 +* cyfor some nonterminal 2 $ED and such
that val (or)< cl[Y]
,at some occurrence of step 5).
Then o =g(q , ... . ak) for some ol, .... cyk, where the
grammar contains the production 2 -+ g(X, , . ... X,)
and Xi -+* ai for all i. Ff {Xr,. ... AT,) ED we have
val (oi) 2 m(Xi) = p[Xi] for all i, by definition of
m(Xi) and (*), hence val (cu)2 g(pt[Xl ] , .... p[X,] ),
by the monotonicity ofg in each variable Btir
@[XI I 9 .‘.Vp[X,]) 2 v[Z] by step 3), and Y[Z]
>v[Y] = PLY] by step 4), contradicting val (ar)<r_llyr
.
Therefore Xi $ D for some i. But now val (CQ)G val ((Y’J
by the superiority ofg, hence ai is a shorter string
. having the stated properties of cu;this contradiction
completes the proof.
it is easy to prove that the algorithm defines the
p[ Y] In increasing order of their value, since the ney
3
&rlrent
Y itltioducd
1:ebruary 1977
IN FORM ATICIN PROCESSING LETTERS
VoSumc 6, number 1
into D by steps 4) and 5) can-
make any of the V’Sless than ~.r[Y] in the next occurrence of step 3). The usual proofs of Dijkstra’s algorithm rely on this monotonicity property; but the
above proof shows that it isn’t really a crucial fact,
ever4 when the algorithm has been substantially generalized.
r\ot
5. Anuther application
If the functionsg of a superior context-free grammar satisfy the additional condition of strict inequaiity,
&, , -‘*9xk) > max (x1, . ..) xk) ,
prove that there is a trnique solution to the
set of sitiul taneous equations
we shaii
fT’i) = min MIT& 1, ...A&))
-v(Xt
1Y
, **‘,Xk) is a production},
for all nonterminal Y ,
namely fly) = m(Y) for all Y.
Iln the firs: place, m(Y) is 3 solution: For if Y +
. . . . Xk) is any production, then g(m(&), .. ..
Ml,
m(Xk) is infimte or equal to val (cll)for some terminal
string 01,where
Y -+g(X, , ..‘, Xk) +* g(q , . . . . Q) = Q
for some al, . . . . ak* Thus g(m(X, ), .-a>m(xk))
by definition of m(Y). Conversely if m(Y)
= vd (a) lflen a! has the form g(ol, , .. . . a&-), where
there is a production Y +g(X1 , ... . &); and
But the strict inequality condition above implies that
jr1(Xi) < fi (Y) for 1 G i < k, hence fr (Xi) = fi(4)
for all i by our construction, and this is impossible.
Note that something like the strict inequality condition is necessary to guarantee uniqueness, because
of the following example grammar:
A+a
cl = 5,
A --) b(B) ,
b(x)=x,
B+c(A),
c(x) = x .
The simultaneous minimization equations are
SCA)=min(%f(BL
f(B) =JiIA) 9
hence the solutions are
f(A)=fcB)=x
forOGxG5.
The application discussed here arises for example
in the study of “levels” in a flowchart, as defined by
Floyd [4] and Mont-Reynaud [8] .
Acknowledgments
1 wish to thank Edsger W. Dijkstra and Nils J. Nilsson for their helpful comments on the first draft of
this paper.
-2 m(Y)
&(X1 ), .... m(Xk)) +(val (a] ), .**,va.l(aF())
= val (ar)= m(Y).
In the second place, the solution is unique: Suppose
fi and f2 are distinct solutions 10 the simultaneous
minimization equations above. Let Y be a nonterminal
such that fl ( y) f .fz(Y’)and min VI (Y), fi (‘0) is as
small as possible. Withcut loss of generality assume
thatfiV’)<fi(Y);
thenfi(X)=fi(X)for
aMsuch
that fl (X) < fl (Y). There must exist a production
Y+g(X,, . .. . X,)
such that
References
111A.V. Aho and SC. Johnson, Optimal code generation for
expression trees, 3. Assoc. Comput. Mach. 23 (1976)
488-501.
!21 Y. Bar-Hillel, M. Qerlcs bnd E. Shamir, On formal properties of simple phase structure grammars, 2. f. Phonetic,
Sprachwissenschaft und Kommunikationsforschung 14
(1961) 143- 172, CR 1450 (September-October 1963)
2 13-2 14. Reprinted in Yehoshua Bar-Hillel, Language
and Information (Addison-Wesley, Reading, MA, 1964)
116-150.
131E.W. Dijkstra, A note on two problems in connexion
with graphs, Numer. Math. 1 (1959) 269-271.
VI R.W. Floyd, Flowchart levels (preliminary draft), unpublished manuscript (July 1965).
1NFOK~nTIO.N PROCESSINGLETTERS
Volume 6, number 1
[s 1 Sheila Greibach, Inverses of
Math
phdsc
StrUCtUre
February 1977
g’ .TafOrS,
Rcpore
International Joint Cont‘crence on Artificial Intelligence (1973) 1- 11.
(8 ] 5. Mont-Reyntizd,
communicarion (July 1976).
191 Nils J. Nrissorl, Probi,om-Solving
(McGraw-Hill, New York, 1971).
© Copyright 2026 Paperzz