A Generalization of Dijkstra's Algorithm.
A Generalization of Dijkstra's Algorithm.
Donald E. KNUTM
Computer Science Department. Stanford Univers!ty,Stanford, California94305, U.S.A.
Shortest paths, AND/OR graphs, context-free grammars, simultaneous minimax equations, decision !rees,
dynamic programming
E.W. Dijkstra [3] has introduced an important al- where Y, X1, . ... Xk are nonterminal symbols, g is a
gorithm for finding shortest pzths in a graph, when terminal symbol corresponding to a superior function
the distances on each arc of the graph are nonnegative. (possibly a different superior function for each produc-
The purpose of this note is to present an algorithm tion), and the parentheses and commas are also ter-
which generalizes Dijkstra’s in essentially the same minal symbols. If k > 0, there are k -- 1 commas; if
way that tree structures generalize linear lists, or that k = 0 the right-hand side of the production is simply
context-free languages generalize regular languages. written “g”, a terminal symbol which corresponds to
a nonnegative real constant.
For example, the follotiing productions define a
1. The problem in general superior context-free grammar on the nonterminal
symbols A BC and the terminal symbols (), a b cd ef:
Let R, be the nonnegative real numbers extended
with the value +oo. We shall say that a function g(xi ) A-+a, =‘c(A), C+e,
... . xk) from Rf into R+ is a superiorfunction if it is
A -‘b&C) 9 B-+d(A,C,A), C-, f(l?, A) .
monotone nondecreasing in each variable and if
.. . . xk) 3 ma(xl, . . . . xk) for all x1, .... xk . Werea, b, c, d, e, f are supposed to correspond to
&l¶
superior functions, and we may for example define
When k = 0 the functiong is simply a constant element a=4, d(x,y,z)=x+max@,z), .
of R+, and it is trivially considered to be a superior
function. When k = 2 several familiar functions g(x, y) b(x,y)=max(x,y), e=9,
such as max (x, y) and x + y and max (1, x)* max( 1,y) c(x)=x+ 1) flx,y)=$(x+_,,+max(x,y)).
are all superior. Any functional composition of supe-
rior functions is a superior function of the variables For every nonterminal symbol Y of a superior con-
occurring in the composition. text-free grammar over the terminal alphabet T we let
A superior con text-free grammar is a con text-free L(Y) = {ok E P and Y +* a)
grammar in which all productions are of the general
form be the set of terminal strings derivable from Y. Every
string Q in L(Y) is a composition of supprior functions,
y+g(x,, l
,x,) t so it corresponds to a uniquely-defined nonnegative
real number which we shall call val(or). Thus, in the
* This research was supported in part by Nation,; Science above example we have
Foundation grant MCS 72-03752 A03, by the OCfice of
Naval Research contract NO001476424330, and by IBM UA) = <a, b(c(a), 4, b(c(4, f@(a), a)),
Corporation. Reproduction in whole OF in part is permitted
for any purpose of the United States Government. b(c(N.c4a), e)), 4, l
)
1
I INFORMATIONPROCESSJNGLETTERS February 1977
and the corresponding numerical values are Then m(Y) is the length of the shortest string deriv-
able from Y in the given grammar. Alternatively if we
(4.5 7, 10, ...I.
Iet
The problem we shall solve is to compute the smal-
lest values corresponding to these languages, namely gtr(x* , one,xk) = ma (xl, l-e)xk) + 1 ,
m(Y)= mm (val (&)I0E L(n) , then m(Y) is the minimum height of a parse tree for a
string derivable from Y in the given grammar.
for each nonterminal symbol Y. Finally, if we let
2
Volume 6, number I INI:ORMATIONPRWISSING LETTERS Iicbruary 1977
P_ _ __.._ _ _________
3
VoSumc 6, number 1 IN FORM ATICIN PROCESSING LETTERS 1:ebruary 1977
&(X1 ), .... m(Xk)) +(val (a] ), .**,va.l(aF()) 111A.V. Aho and SC. Johnson, Optimal code generation for
expression trees, 3. Assoc. Comput. Mach. 23 (1976)
= val (ar)= m(Y). 488-501.
!21 Y. Bar-Hillel, M. Qerlcs bnd E. Shamir, On formal proper-
In the second place, the solution is unique: Suppose ties of simple phase structure grammars, 2. f. Phonetic,
fi and f2 are distinct solutions 10 the simultaneous Sprachwissenschaft und Kommunikationsforschung 14
(1961) 143- 172, CR 1450 (September-October 1963)
minimization equations above. Let Y be a nonterminal
2 13-2 14. Reprinted in Yehoshua Bar-Hillel, Language
such that fl ( y) f .fz(Y’)and min VI (Y), fi (‘0) is as and Information (Addison-Wesley, Reading, MA, 1964)
small as possible. Withcut loss of generality assume 116-150.
thatfiV’)<fi(Y); thenfi(X)=fi(X)for aMsuch 131E.W. Dijkstra, A note on two problems in connexion
that fl (X) < fl (Y). There must exist a production with graphs, Numer. Math. 1 (1959) 269-271.
VI R.W. Floyd, Flowchart levels (preliminary draft), un-
Y+g(X,, . .. . X,) such that published manuscript (July 1965).
Volume 6, number 1 1NFOK~nTIO.N PROCESSINGLETTERS February 1977