0% found this document useful (0 votes)
54 views

A Generalization of Dijkstra's Algorithm.

1. The document presents an algorithm that generalizes Dijkstra's algorithm for finding shortest paths in a graph to problems represented by superior context-free grammars. 2. In a superior context-free grammar, productions take the form of a nonterminal symbol producing a string of terminal and nonterminal symbols, where the terminal symbols represent superior functions. 3. The generalized algorithm computes m(Y), the minimum value corresponding to the language generated by each nonterminal symbol Y, solving problems such as finding shortest paths, parsing tree heights, AND/OR graph solutions, and probabilistic grammars.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

A Generalization of Dijkstra's Algorithm.

1. The document presents an algorithm that generalizes Dijkstra's algorithm for finding shortest paths in a graph to problems represented by superior context-free grammars. 2. In a superior context-free grammar, productions take the form of a nonterminal symbol producing a string of terminal and nonterminal symbols, where the terminal symbols represent superior functions. 3. The generalized algorithm computes m(Y), the minimum value corresponding to the language generated by each nonterminal symbol Y, solving problems such as finding shortest paths, parsing tree heights, AND/OR graph solutions, and probabilistic grammars.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

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 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

g&t , .... xk) = max (x1 , . ... xk) .


2. Applications of the general problem
WChave m( I’) = 0 or 00according as E( Y’)is nonemp ty
or empty. The algorithm we shall develop for the gen-
(A) Consider n + 1 cities {ce, cl, .. . . c,) connected
eral problem reduces to the classicalemptiness-testing
by a network of roads. For each road from q to ci, in-
algorithms of Bar-Hillel,Perles, and Shamir [2] or
troduce the production
Creibach [5] in this special case.
ci -V&j) , (C) Consider the AND/OR graphs which arise in
artificial intelhgence applications as in Nilsson [9] ,
and the corresponding superior function section 4-5. In this case we use one nonterminal sym-
bol for each “‘problem” to be solved. If some problem-
&i(X)= d/j + x 9
reduction operator shows that problem Y could be
where djj 2 0 is the length of the road. Then add one solved if we,could solve all of problems X1, . .. . Xk,
more production then we introduce the production

CO303 Y”g(X,, -e-,x,) ,

where 0 corresponds to the constant function zero.


where g(x l ,..., xk)=x, + ... + xk + (cost of solving
Y given solutions to Xt , ... . &). Then m(Y) is the
In this superior context-free grammar, the elements of
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,
For example, if there is a path from c3 to cd to c2 to problem solutions are considered independent). Our
algorithm will therefore find a smallest AND/OR
ce , one of the elements of L(C3) will be g34(g42(g20(0))),
and its corresponding value is d34 + de2 + d2o + 0. graph in this sense; but it is of limited utility for A.I.
Therefore m(Cj) is the length of the shortest path applications because it deals with the set of a21“easy”
from Cj t0 CO, for all i. sublproblems,and this set is usually too large.
The algorithm we shah develop for the general A special case of the algorithm to be described
problem reduces to Dijkstra’s algorithm in this speical here has been published by Martelli and Montanari.
case. They deal only with AND/OR graphs whose functions
(B) Given a context-free grammar, replace each & 1 .. .. xk) have the form x1 + ... t xk + c With c > 0;
,
production I --*8 in which the nonterminal symbols furthermore they restrict their discussion to acyclic
of string 8 are Xr,... . Xk from left to right (including AND/OR graphs. The algorithm below works also in
repetitions) by the presence of cycles, and in this sense it is more gen-
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.’
1,i:-* 6lJ“(ci, k- 1 t ck,j) forO<i<k<j<n;

2
Volume 6, number I INI:ORMATIONPRWISSING LETTERS Iicbruary 1977

and let Iable 1


- _I~____ __
I__-

gij(x, J9) = x’ + J’ + pi+ ] + ma


+ pi + fJi + ... + 4i . n DIAI PIBI lLlC1 44 WI WI
l

P_ _ __.._ _ _________

Then m(Co,n) + p I + ,.. f p,, is the expected number 0 - - __ 4 9


of comparisons in an optimum binary search tree de- A} 4 - - - ; 9
fine by the given probabilities, in the sense of [6, tA,& 4 5 - - _- 7
-__.- --__
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 are also possible, since all productions with Y on the
problems, such as that of constructing optimum pru- left-hand side can be deleted from memory as soon as
grams for decision tables. p[ Y] has been defined.
(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 4. Proof of the algorithm
f with asymmetric registers.
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 follow- will then be invariant throughout the algorithm.
ing algorithm determines m(Y) for all nonterminal Y. It is clear that v[ Y] Z m(Y) in step 3); for if v[ Y]
The algorithm operates on elements p[ Y] and V[Y] < 00, (*) k-plies that
.-for each Y; initially all these elements are undefined,
Y[Y] = v:’ &a,. .... CQ)), where
but when the algorithm terminates p[ Y] will equal
m(Y) for all Y. The set D represents those Y for which Y-*,4(X1 ) .... X,) +* g& , ...) Qk) ,
p[ Y] has been defined.
1) Set D to the empty set. zrnd the terminal strings (x1, . . . . elk satisfy val (oi) 2
2) If all nonterminals are in D, stop. m(Xij for all i. Therefore the algorithm will be valrd
3) For each nonterminal Y 4 D, compute unless p[ Y] > m(Y) at some occurrence ol‘ stzp S).
In that case there will exist a terminal string (x such
Y[Y] =min j&[Xr] ,..., j&])IY+g(Xr ,..., X&s that Y +* a and val (cu)< p[Y].
a production and {X,, . ... X,} E D} . Assume that or is the shortest terminal string such
that 2 +* cyfor some nonterminal 2 $ED and such
(If this set is empty, V[Y] is infinite.) that val (or)< cl[Y] ,at some occurrence of step 5).
4) Choose Y $ D such that u[Y] is minimum, and set Then o =g(q , ... . ak) for some ol, .... cyk, where the
Iul + WI l
grammar contains the production 2 -+ g(X, , . ... X,)
5) Include Y in D, and return to step 2). and Xi -+* ai for all i. Ff {Xr,. ... AT,) ED we have
For example, consider the grammar given in sect. 1. val (oi) 2 m(Xi) = p[Xi] for all i, by definition of
The computation proceeds as in table 1, viewed in m(Xi) and (*), hence val (cu)2 g(pt[Xl ] , .... p[X,] ),
step 3). Finally p[q + 7. by the monotonicity ofg in each variable Btir
Actually&e values v/Y] do not need to be com- @[XI I 9 .‘.Vp[X,]) 2 v[Z] by step 3), and Y[Z]
puted explicitly, they have been introduced here only >v[Y] = PLY] by step 4), contradicting val (ar)<r_llyr .
for convenience in stating and proving the algorithm. Therefore Xi $ D for some i. But now val (CQ)G val ((Y’J
By maintaining a priority queue of the current values by the superiority ofg, hence ai is a shorter string
of p[ Y] with Ye D, the running time of this algorithm . having the stated properties of cu;this contradiction
is bounded by a constant times m log 11+ t, where completes the proof.
there are m productions and n nonterminais, and the it is easy to prove that the algorithm defines the
total length of all productions is C.Further rrfinements p[ Y] In increasing order of their value, since the ney

3
VoSumc 6, number 1 IN FORM ATICIN PROCESSING LETTERS 1:ebruary 1977

&rlrent Y itltioducd into D by steps 4) and 5) can-


r\ot make any of the V’Sless than ~.r[Y] in the next oc-
currence of step 3). The usual proofs of Dijkstra’s al-
gorithm rely on this monotonicity property; but the
above proof shows that it isn’t really a crucial fact,
But the strict inequality condition above implies that
ever4 when the algorithm has been substantially gen-
jr1(Xi) < fi (Y) for 1 G i < k, hence fr (Xi) = fi(4)
for all i by our construction, and this is impossible.
eralized.
Note that something like the strict inequality con-
dition is necessary to guarantee uniqueness, because
5. Anuther application of the following example grammar:
A+a cl = 5,
If the functionsg of a superior context-free gram-
mar satisfy the additional condition of strict inequai- A --) b(B) , b(x)=x,

ity, B+c(A), c(x) = x .


&, , -‘*9xk) > max (x1, . ..) xk) ,
The simultaneous minimization equations are
we shaii prove that there is a trnique solution to the
SCA)=min(%f(BL f(B) =JiIA) 9
set of sitiul taneous equations
hence the solutions are
fT’i) = min MIT& 1, ...A&)) 1Y
f(A)=fcB)=x forOGxG5.
-v(Xt , **‘,Xk) is a production},
The application discussed here arises for example
for all nonterminal Y , in the study of “levels” in a flowchart, as defined by
Floyd [4] and Mont-Reynaud [8] .
namely fly) = m(Y) for all Y.
Iln the firs: place, m(Y) is 3 solution: For if Y +
Ml, . . . . Xk) is any production, then g(m(&), .. ..
Acknowledgments
m(Xk) is infimte or equal to val (cll)for some terminal
string 01,where
1 wish to thank Edsger W. Dijkstra and Nils J. Nils-
Y -+g(X, , ..‘, Xk) +* g(q , . . . . Q) = Q
son for their helpful comments on the first draft of
this paper.
for some al, . . . . ak* Thus g(m(X, ), .-a>m(xk))
-2 m(Y) by definition of m(Y). Conversely if m(Y)
= vd (a) lflen a! has the form g(ol, , .. . . a&-), where
References
there is a production Y +g(X1 , ... . &); and

&(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

[s 1 Sheila Greibach, Inverses of phdsc StrUCtUre g’ .TafOrS,

Math Rcpore International Joint Cont‘cr-


ence 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).

You might also like