Dynamic Programming in Computer Science
Dynamic Programming in Computer Science
1979
his Technical Report is brought to you for free and open access by the School of Computer Science at Research Showcase @ CMU. It has been
accepted for inclusion in Computer Science Department by an authorized administrator of Research Showcase @ CMU. For more information, please
contact research-showcase@andrew.cmu.edu.
NOTICE WARNING CONCERNING COPYRIGHT RESTRICTIONS:
The copyright law of the United States (title 17, U.S. Code) governs the making
o f photocopies or other reproductions of copyrighted material. Any copying of this
document without permission of its author may be prohibited by law.
CMU-CS-79-106
DEPARTMENT
of
COMPUTER SCIENCE
510.7808
C28R
79-106
T a b l e of C o n t e n t s
1. I n t r o d u c t i o n
2. A n Example
3 . B r i e f S u m m a r y of t h e T h e o r y
4 . S i n g l e S o u r c e S h o r t e s t Path Problems
5. Optimal Parenthesization Problems
5.1 O p t i m i z i n g t h e M u l t i p l i c a t i o n of N M a t r i c e s
5.2 C o n t e x t F r e e Language Recognition
5.3 O p t i m a l A l p h a b e t i c E n c o d i n g and Optimal S e a r c h T r e e s
5.4 O p t i m i z i n g t h e S e a r c h f o r N O r d e r Statistics
1. I n t r o d u c t i o n
" D y n a m i c p r o g r a m m i n g is an important t e c h n i q u e f o r t h e s o l u t i o n o f p r o b l e m s
i n v o l v i n g t h e o p t i m i z a t i o n of a s e q u e n c e of d e c i s i o n s . T h e simple i d e a u n d e r l y i n g
t h i s t e c h n i q u e is to r e p r e s e n t the p r o b l e m b y a p r o c e s s w h i c h e v o l v e s f r o m s t a t e
t o s t a t e in r e s p o n s e to d e c i s i o n s . A t y p i c a l o p t i m i z a t i o n p r o b l e m t h e n b e c o m e s
o n e o f g u i d i n g t h e s y s t e m to a terminal s t a t e at minimum c o s t . W h e n t h e c o s t
s t r u c t u r e is a p p r o p r i a t e , t h e d e t e r m i n a t i o n of an optimal p o l i c y ( s e q u e n c e o f
d e c i s i o n s ) m a y b e r e d u c e d to the s o l u t i o n of a functional e q u a t i o n in w h i c h t h e
s t a t e a p p e a r s as an i n d e p e n d e n t v a r i a b l e . " Karp and Held, 1 9 6 7 .
T h i s p a p e r is i n t e n d e d t o b e a r e f e r e n c e to the uses of d y n a m i c p r o g r a m m i n g in c o m p u t e r
science. M a n y p r o b l e m s a r e c o v e r e d , along w i t h the DP a l g o r i t h m s and r e f e r e n c e s t o r e l a t e d
p r o b l e m s and more advanced results. W e i n t r o d u c e dynami c p r o g r a m m i n g in t h e f o r m o f an
example in Chapter 2 below, and in C h a p t e r 3 present some of t h e t h e o r y of DP and
subsequently apply it t o t h e p r o b l e m of C h a p t e r 2. In the f o l l o w i n g four chapters we
p r e s e n t a n d a n a l y z e e x a m p l e s f r o m s e v e r a l s u b c l a s s e s of DP p r o b l e m s . These subclasses are
(for the most part) defined by the f o r m of t h e recursion for breaking a problem into
subproblems. C h a p t e r 4 c o v e r s Single S o u r c e S h o r t e s t Path P r o b l e m s , C h a p t e r 5 d e s c r i b e s
O p t i m a l P a r e n t h e s i z a t i o n P r o b l e m s , Optimal P a r t i t i o n P r o b l e m s a r e a n a l y z e d in C h a p t e r 6, i n
C h a p t e r 7 w e p r e s e n t Optimal M a t c h i n g P r o b l e m s , and ' H a r d ' C o m b i n a t o r i a l P r o b l e m s a r e t h e
t o p i c o f C h a p t e r 8. E a c h of t h e s e c h a p t e r s a r e i n d e p e n d e n t and c a n b e r e a d s e p a r a t e l y from
the others. ( I t is s u g g e s t e d , h o w e v e r , that C h a p t e r s 2 and 3 b e r e a d b e f o r e a t t a c k i n g a n y o f
t h e material in t h e later chapters.)
PAGE 3
2. A n E x a m p l e
B e f o r e d i v i n g i n t o t h e t h e o r y , it will b e helpful to g i v e some of t h e f l a v o r o f D P a l g o r i t h m s
by g i v i n g an example. T h e d i a g r a m in F i g u r e 2-1 is a g r a p h w i t h ( n o n - n e g a t i v e ) weighted
edges. These weights a r e i n t e r p r e t e d as the " d i s t a n c e s " along t h e e d g e s c o n n e c t i n g the
nodes. T h u s , t h e d i s t a n c e f r o m n o d e (1,1) to n o d e (2,1) is 3 and t h e d i s t a n c e f r o m n o d e ( 2 , 1 )
to n o d e ( 3 , 2 ) is 2. ( T h e d i s t a n c e f r o m n o d e (2,1) to n o d e ( 1 , 1 ) , o n t h e o t h e r h a n d , is not
d e f i n e d i n t h e g r a p h a n d c a n t h u s b e assumed to b e infinite.) T h e d i s t a n c e a l o n g p a t h ( 1 , 1 ) -
( 2 , 1 ) - ( 3 , 2 ) is 3 + 2 = 5. T h e p r o b l e m is to f i n d t h e s h o r t e s t p a t h f r o m n o d e ( 0 , 1 ) t o n o d e ( 4 ,
1).
F i g u r e 2 - 1 : F i n d t h e s h o r t e s t p a t h f r o m Node (0,1) t o N o d e ( 4 , 1 )
A d y n a m i c p r o g r a m m i n g a p p r o a c h t u r n s o u t to b e j u s t w h a t w e n e e d . Let
- t h e ( n o n n e g a t i v e ) " d i s t a n c e " f r o m n o d e (i,j) t o n o d e ( i + l , k )
PAGE 4
a n d let
M j j = l e n g t h of the s h o r t e s t p a t h f r o m n o d e (0,1) to n o d e ( i , j ) .
T h e o p t i m i z a t i o n f o r t h e p r o b l e m in F i g u r e 2-1 p r o c e e d s in f o u r stages. In e a c h s t a g e i w e
f i n d t h e l e n g t h s o f t h e s h o r t e s t p a t h s f r o m n o d e (0,1) to t h e n o d e s (i,j), f o r ail j , w h i c h we
d e n o t e b y Mjj. S t a g e 1 is e a s y since t h e r e is o n l y o n e p a t h f r o m n o d e ( 0 , 1 ) t o e a c h o f the
n o d e s (1,1), (1,2), and (1,3). Thus, w e have
l l 011» 1 2 M
012' 1 3
= C
013- M = C M = C
S t a g e 2, h o w e v e r , r e q u i r e s some c o m p u t a t i o n since t h e r e a r e t h r e e p a t h s f r o m n o d e ( 0 , 1 ) t o
e a c h o f t h e n o d e s ( 2 , 1 ) , ( 2 , 2 ) , and (2,3). Thus,
M
21 = m i n
< 011
c + C
lll> C
012 + C
121> 0 1 3 C + C
131 *
M 2 2 = min ( C Q 1 1 + C n 2 , C 0 1 2 + 122' 013
c C + C
132 *
M 2 3 = min ( C 0 1 1 + C 1 1 3 , C Q 1 2 + C 1 2 3 , C Q 1 3 + C 1 3 3 )
I n s t a g e 3 w e f i n d t h e l e n g t h s of the s h o r t e s t path s f r o m n o d e (0,1) to n o d e s ( 3 , 1 ) , ( 3 , 2 ) , a n d
(3,3). H e r e is w h e r e t h e g r e a t a d v a n t a g e of dynamic p r o g r a m m i n g b e c o m e s a p p a r e n t . It is
n o t n e c e s s a r y t o e x a m i n e ail p a t h s f r o m n o d e (0,1) to n o d e s (3,1), (3,2), a n d ( 3 , 3 ) b e c a u s e we
c a n u s e t h e r e s u l t s f r o m s t a g e 2 as f o l l o w s :
M | = min ( M j ^ + C ^ | , M
3 2 2 2 2 * ^ 2 2 1 ' ^ 2 3 **" ^ 2 3 1 ^
M 3 2 = min ( M ^ + ^212* ^ 2 2 2 ^222* ^ 2 3 +
^232 ^
M 3 3 = min ( M j ^ + C j ^ , M 2 2 3 2 2 + ^223* ^ 2 3 ^233 ^
S i m i l a r l y , in s t a g e 4 w e c a n u s e the r e s u l t s f r o m s t a g e 3.
M 4 1 - min ( M 3 1 + C 3 1 1 , M 3 2 + C 3 2 1 , M 3 3 + C 3 3 1 )
T h e s o l u t i o n f o r F i g u r e 2 - 1 is p a t h (0,1) - (1,1) - (2,2) - (3,3) - (4,1) of l e n g t h 7. N o t e t h a t it
is s t r a i g h t f o r w a r d t o d e t e r m i n e that the l e n g t h M ^ j = 7, but r e c o v e r i n g t h e corresponding
path (or paths) is a slightly different problem. It is, h o w e v e r , s i m p l y s o l v e d by simply
recording a pointer Pjj w i t h e a c h d i s t a n c e Mjj w h i c h p o i n t s back to t h e n o d e ( i - l , k ) from
w h i c h t h e s h o r t e s t p a t h to n o d e (i,j) came.
M
ij =
T ( M
<i-i),k + c
<i-n,k,j >
w h e r e M . i t = 0 , f o r all k. I n t h e n e x t c h a p t e r w e s h o w h o w to d e r i v e t h i s e q u a t i o n .
n
PAGE 6
3 . B r i e f S u m m a r y of the T h e o r y
By the mid 1960's several people had s o l v e d the first problem by showing that the
P r i n c i p l e o f O p t i m a l i t y d e r i v e s f r o m a m o n o t o n i c i t y p r o p e r t y of t h e c o s t f u n c t i o n s . (See Karp
a n d H e l d [ 4 4 ] f o r r e f e r e n c e s . ) K a r p and Held s u b s e q u e n t l y made c o n s i d e r a b l e p r o g r e s s o n t h e
second problem. In their f o r m u l a t i o n , a c o m b i n a t o r i a l d e c i s i o n p r o b l e m is a s s u m e d t o be
expressed in t h e f o r m o f a discrete decision process (DDP). T h i s f o r m is u s u a l l y a fairly
natural way to express the problem. The dynamic programming algorithm to solve the
p r o b l e m , o n t h e o t h e r h a n d , c o r r e s p o n d s to a sequential decision process ( S D P ) . Karp and
Held describe the t r a n s f o r m (if it e x i s t s ) f r o m a d i s c r e t e d e c i s i o n p r o c e s s t o a s e q u e n t i a l
decision process.
T h e r e s u l t o f K a r p a n d Held c a n be u s e d to d e r i v e the f u n c t i o n a l e q u a t i o n s f o r t h e p r o b l e m
of Figure 2-1. Their r e s u l t , h o w e v e r , will not be f o l l o w e d e x a c t l y b e c a u s e it is l o n g and
complicated to e x p l a i n (and thus discouraging for readers). T h u s , to d e r i v e t h e functional
e q u a t i o n s as p a i n l e s s l y as p o s s i b l e , a modified a p p r o a c h will be d e s c r i b e d w h i c h s h o u l d n ' t go
too far wrong. The f i r s t s t e p is to d e f i n e the d i s c r e t e d e c i s i o n p r o c e s s . The D D P is a
f o u r - t u p l e D « ( A , S, P, f ) w h e r e
A = { p r i m i t i v e d e c i s i o n s },
S = { f e a s i b l e p o l i c i e s } = { tuples of p r i m i t i v e d e c i s i o n s w h i c h t r a n s f o r m the
s t a r t s t a t e to a final s t a t e },
P = { s e t s o f d a t a u s e d b y the c o s t f u n c t i o n f }, and
f = c o s t f u n c t i o n , d e f i n e d o v e r all x £ A*,
A = { aj, a 2 a 3 } w h e r e aj t r a n s f o r m s t h e s t a t e f r o m n o d e ( i - l , k ) t o n o d e ( i , j ) ,
S = { a a a ai j k 1 | i,j,k < { 1 , 2 , 3 } },
Z is a f i n i t e s t a t e a u t o m a t o n Z = ( A , Q, qQ, F, \ ) w h e r e
Q = { s t a t e s },
q Q = initial s t a t e ,
X = transition function,
h = c o s t f u n c t i o n d e f i n e d b y : h(r,q,a,p) = cost of r e a c h i n g s t a t e X ( q , a ) b y f i r s t
r e a c h i n g s t a t e q at a c o s t r and t h e n a p p l y i n g t h e s e q u e n c e of d e c i s i o n s a. T h e
f u n c t i o n h r e l a t e s to f b y : h(r,q,a,p) » f ( x a , p ) w h e r e f ( x , p ) = r and X ( q Q , x ) - q .
k ( p ) is t h e c o s t o f t h e null s e q u e n c e (of p r i m i t i v e d e c i s i o n s ) e.
T h e S D P f o r t h e p r o b l e m o f F i g u r e 2 - 1 is a f o u r - t u p l e W = ( Z, P, h, k ) w h e r e
Z is an f s a Z * ( A , Q, qQ, F, X ) w h e r e
A = { aj, a , a 2 3 },
Q 88
( 01>
s s
l 1' 1 2 > 13> 21> 22> 23> 31> 32> 33> 4 1 J»
s s s s s s s s s
^0 =
f s
01
F - { s 4 i },
X is d e f i n e d b y : X(sjj,a^) • s ^ + i ) ^
h is d e f i n e d b y : h ( r , S j j , a ^ , p ) = r + C j j ^ , and
k is d e f i n e d b y : k ( p ) = 0.
N o w t h a t w e h a v e d e f i n e d t h e S D P , if t h e m o n o t o n e p r o p e r t y ( d e s c r i b e d b e l o w ) is s a t i s f i e d
1
Karp and Hold use a complicated argument that involves a (finite rank) equiresponse relation of an automaton
w h i c h the states are derived.
PAGE 8
( F o r t h e p r o b l e m o f F i g u r e 2 - 1 this is e a s i l y s a t i s f i e d b e c a u s e o f t h e a d d i t i v i t y o f h ( r , q , a ,
p ) . ) T h e f u n c t i o n a l e q u a t i o n s d e f i n e a f u n c t i o n G s u c h that
G
< ^ > - { * | x C > - q } < <e>'V<>P>- h k
Functional Equations:
G ( q , p ) = m i n ( k(p),
0 { ( S ( a ) | x ^ ) = q o } h<G<s,p),q,a,p> )
G ( q
' p ) =
{<s,a)|X<T,a)-q} < < >P>.q»a>P> h G s f o r
<* * % •
F o r t h e p r o b l e m o f F i g u r e 2 - 1 this is
G(s ,p) = 0
0 1
G ( S
" P ) =
<W i > ( G(S
«-W P) + >
L e t t i n g M y = G ( s j j , p ) , this is
M Q 1 = 0
M
ii = t n ( M
(i-l),k + C
( -l).K,i '
i
) f 0 r (
^^ ( 0 1 )
-
T h i s e q u a t i o n is t h e same as t h e f u n c t i o n a l e q u a t i o n g i v e n f o r this p r o b l e m in C h a p t e r 2.
I n t h e f o l l o w i n g c h a p t e r s w e will a p p l y t h e t e c h n i q u e s p r e s e n t e d in this c h a p t e r t o s e v e r a l
subclasses of DP problems. S i n c e t h e n o t a t i o n is s o m e w h a t c u m b e r s o m e a small c o m p r o m i s e
has b e e n made. T h e c o s t f u n c t i o n s f ( x , p ) and h(r,q,a,p) shall b e ( m o r e c o n v e n i e n t l y , a l t h o u g h
l e s s c o r r e c t l y ) d e n o t e d b y s i m p l y f ( x ) and h(r,q,a), r e s p e c t i v e l y .
PAGE 9
4. S i n g l e S o u r c e S h o r t e s t Path Problems
T h e p r o b l e m o f C h a p t e r 2 is a s p e c i a l case of the single s o u r c e s h o r t e s t p a t h p r o b l e m . But
b e f o r e d e s c r i b i n g t h e g e n e r a l p r o b l e m , w e s h o u l d f i r s t n o t e some o t h e r i n t e r e s t i n g problems
that fall into this special case. O n e of the b e t t e r known problems of this type is the
s t a g e c o a c h p r o b l e m ( [ 7 2 ] ] ) , w h i c h is commonly u s e d as an i n t r o d u c t i o n to D P in operations
research textbooks. A r a t h e r c u r i o u s e x a m p l e of this t y p e of a l g o r i t h m is t h e o r d i n a r y , g r a d e
school a l g o r i t h m f o r long division [8]. But of more i n t e r e s t to c o m p u t e r s c i e n t i s t s is the
V i t e r b i algorithm. T h e V i t e r b i a l g o r i t h m [71, 19] is f o r m a l l y a s t a g e c o a c h t y p e o f p r o b l e m , s o
it is n o t n e c e s s a r y t o d e s c r i b e it in detail h e r e . Its a p p l i c a t i o n s , h o w e v e r , i n c l u d e n o t only
e s t i m a t i o n p r o b l e m s in digital communications [ 9 ] b u t also t e x t r e c o g n i t i o n [ 1 9 ] a n d s y n t a c t i c
pattern recognition [61]. The s t a g e c o a c h p r o b l e m has also b e e n e n c o u n t e r e d in speech
r e c o g n i t i o n [ 3 ] a n d in image u n d e r s t a n d i n g s y s t e m s [ 1 6 ] .
We will now describe the general single source shortest path problem. Let G be a
( d i r e c t e d ) g r a p h o f N n o d e s and E e d g e s . T h e e d g e (if a n y ) f r o m n o d e Nj t o n o d e N j is e d g e
Ejj. T h e c o s t s t r u c t u r e is d e f i n e d b y the w e i g h t s C j j > 0 c o r r e s p o n d i n g to t h e e d g e s E j j . The
p r o b l e m is t o f i n d t h e s h o r t e s t p a t h f r o m node N j to node N^. F o r n o w w e w i l l a s s u m e t h a t
t h e g r a p h G is a c o m p l e t e g r a p h . ( F o r e a c h pair of n o d e s Nj and Nj t h e r e is an e d g e E j j . ) If a
particular g r a p h is n o t a l r e a d y c o m p l e t e , w e can e a s i l y make it so b y augmenting it with
e d g e s Ejj w i t h w e i g h t s C j j = oo.
T o o b t a i n t h e f u n c t i o n a l e q u a t i o n s the first s t e p is to d e f i n e t h e d i s c r e t e d e c i s i o n p r o c e s s
( D D P ) D = ( A , S , P, f ). F o r t h e s i n g l e s o u r c e s h o r t e s t p a t h p r o b l e m this i s :
T h e c o r r e s p o n d i n g S D P W - ( Z, P, h, k ) is
PAGE 10
Z = f s a ( A , Q , q Q , F, X ) w h e r e
A = s a m e as f o r t h e DDP,
qo =
{ s
l )>
I 7
= { s N },
X(Sj,aj) = Sj,
P = s a m e as f o r t h e DDP,
h(r,Sj,aj) = r + C j j , and
M p ) = 0.
G(s ) = 0{
G ( s
i> - h ( G ( s
iVi) " T < < i> G s + C
Ji>
L e t t i n g M ( i ) = G ( s j ) t h i s is
M(1) = 0
M(i) = ^ ( M ( j ) + C,.)
W e w i l l n o w s h o w h o w to s o l v e t h e s e functional e q u a t i o n s .
It is n o t as i n t u i t i v e l y o b v i o u s h o w to s o l v e t h e s e e q u a t i o n s as it w a s f o r t h e stagecoach
problems. T h e s o l u t i o n is k n o w n as D i j k s t r a ' s algorithm [ 1 2 , 6, 6 0 ] and r e q u i r e s O ( N ^ ) time
and storage. ( F o r a g e n e r a l g r a p h this is the b e s t that o n e c a n d o b e c a u s e t h e r e a r e O(N^)
edges and any shortest p a t h a l g o r i t h m w i l l , in the w o r s t c a s e , h a v e t o l o o k at all o f the
edges.) The a l g o r i t h m r u n s in N - 1 s t a g e s and in e a c h s t a g e i f i n d s t h e i t h c l o s e s t n o d e to
node Nj.. But e a c h stage requires a comparison b e t w e e n 0(N) nodes, so the total time is
0(N ).
2
PAGE 11
A l g o r i t h m f o r S i n g l e S o u r c e S h o r t e s t Path
I n p u t : C o m p l e t e g r a p h G w i t h i n t e r n o d e distance s C ( I , J ) .
O u t p u t : G ( I ) = d i s t a n c e of s h o r t e s t p a t h f r o m Node 1 to Node I.
H(I> = last n o d e b e f o r e Node I in an optimal path f r o m N o d e 1 t o N o d e I.
Time: 0 ( N ) , Storage: 0 ( N )
2 2
/ Initialization;
For I « - 1 t h r u N d o
b e g i n G ( I ) <- C ( 1 , I ) ; H(I) <- 1; e n d ;
N O D E S <- { 1, 2, 3 , . . . N );
/ Optimization;
F o r I <- 2 t h r u N d o
begin
M I N C O S T <- co;
F o r J < N O D E S - S do
begin
N E W C O S T <- G ( i , N E W ) + C ( N E W , J ) ;
If N E W C O S T < G ( J ) t h e n
begin
G ( J ) <- N E W C O S T ; H(J) <- N E W ;
end;
If G ( J ) < M I N C O S T t h e n
begin
M I N C O S T <- G ( J ) ; M I N J 4 - J ;
end
end;
S 4 - S U { M I N J }; N E W <- M I N J ;
end;
5. O p t i m a l Parenthesization Problems
T h e s e p r o b l e m s i n c l u d e o p t i m i z a t i o n of the multiplication of N m a t r i c e s , c o n t e x t f r e e l a n g u a g e
recognition, various k i n d s o f optimal s e a r c h t r e e s , and o p t i m i z i n g t h e s e a r c h f o r N order
statistics.
It c a n b e s h o w n that X ( N ) > 2 ~ N 2
( [ 2 ] , p.73).
M., = m , n
ik i<j<k ijk
= f ( M
i j ' ( j * l ) , k >•
M
T h e o p t i m a l p a r e n t h e s i z a t i o n f o r t h e s u b s t r i n g (i,k) is d e t e r m i n e d b y e x a m i n i n g t h e k - i p a i r s
of Mjj and + ( w h i c h have already been computed). Since t h e r e a r e 0 ( N ) s u b s t r i n g s ( i ,
2
For two of the p r o b l e m s , optimal alphabetic encoding and optimal s e a r c h for N order
s t a t i s t i c s , t h e 0 ( N ) time c a n b e i m p r o v e d to 0 ( N log N) time.
3
Unfortunately, this speedup
r e l i e s o n p r o p e r t i e s t h a t a r e p e c u l i a r to t h e s e individual p r o b l e m s and c a n n o t b e a p p l i e d t o
all of the parenthesization problems. A nice decomposition into parallel computations,
h o w e v e r , is a v a i l a b l e f o r a n y optimal p a r e n t h e s i z a t i o n p r o b l e m . Guibas, Kung, and T h o m p s o n
[ 2 5 ] h a v e s h o w n h o w ( N + 1) ( N + 2) / 2 - 0 ( N ) mesh-connected parallel p r o c e s s o r s
2
can
solve an optimal parenthesization p r o b l e m in 0 ( N ) time. (In general, a K b y K triangular
m e s h - c o n n e c t e d p r o c e s s o r c a n i m p r o v e the time to 0 ( N / K ) . ) 3 2
PAGE 13
M = M 1 x M 2 x M 3 x M 4 x M .
5
M = ( M 1 x ( M 2 x M 3 ) ) x ( M 4 x M 5 )
Let
M = Mj x M 2 x . . . x Mjvj
D - ( A , S , P, f ) is d e f i n e d b y
A = { a j j k | l < i < j < k < N } , where a^ means "multiply the result of M± x M^+j
x ... x Mj by the result of Mj+i % x x
f ( e ) = 0 , f ( x a j j ) - f ( x ) + C j . j * Cj * c .
k k
for this problem is the set of all partial parenthesizations of N elements. The SDP
W = ( Z, P, h , k ) is t h e n d e f i n e d b y
^ h i n [ 1 0 ] describes a non-DP O(N) time approximation algorithm that is never w o r s e than a factor of 5/4 f r o m
optimal.
PAGE 14
Z = ( A , Q , q Q , F, \ ), ( T h e c o m p o n e n t s are o b v i o u s . )
P = s a m e as f o r t h e DDP,
h ( r , q , a j j ^ ) = r + c , _ i * Cj * c^, and
k ( p ) = 0.
M o n o t o n i c i t y o f t h e S D P is a s s u r e d b y the a d d i t i v i t y of h. T h u s t h e f u n c t i o n a l e q u a t i o n s a r e
G ( q ) » 0, and
Q
G
<«> = {(s,a)|Ms a)= } ( q
h ( G ( s )
' '
S a )
The o p t i m i z a t i o n p r o c e d u r e s u g g e s t e d b y t h e s e e q u a t i o n s will c e r t a i n l y p r o d u c e an o p t i m a l
parenthesization. T h e time s p e n t , h o w e v e r , will be p r o h i b i t i v e . T h i s is b e c a u s e t h e number
of states that will have t o b e e x a m i n e d is the n u m b e r of p a r t i a l p a r e n t h e s i z a t i o n s of N
e l e m e n t s , w h i c h is at l e a s t as g r e a t as the Nth C a t a l a n n u m b e r X ( N ) . This does not compare
w e l l w i t h t h e 0 ( N ) time that w a s p r o m i s e d a b o v e .
3
Obviously, the choice of states will h a v e
t o b e made m o r e c a r e f u l l y . T h e n a t u r a l choice is not a l w a y s the b e s t .
parenthesized."
U n f o r t u n a t e l y , t h e r e is n o w a y that a p a r e n t h e s i z a t i o n s u c h a s ( ( M x M ) x ( M x M ) ) can
b e p r o d u c e d b y the primitive o p e r a t o r s with only these states Sjj. Thus, another property
p e c u l i a r t o p a r e n t h e s i z a t i o n p r o b l e m s will h a v e to b e p u l l e d out of a hat. T h i s o n e is a k i n d
of decomposition property and it looks more like divide-and-conquer [2] than dynamic
programming. T h e p r o p e r t y is that the p a r e n t h e s i z a t i o n of a s u b s t r i n g (i,j) is i n d e p e n d e n t of
t h e p a r e n t h e s i z a t i o n o f a s u b s t r i n g (k,Z) iff i < j < k < L o r k < L < i < j . T h u s , t h e p r o b l e m o f
p r o d u c i n g a s t a t e , call it ( S j j u S g + j ^ ) , f o r w h i c h a j ^ f u l l y p a r e n t h e s i z e s s u b s t r i n g ( i , k ) ( a n d
produces s t a t e S j ^ ) c a n b e d e c o m p o s e d into the p r o b l e m of s o l v i n g Sjj a n d s o l v i n g S^j+i)^-
S i n c e t h e c o s t f u n c t i o n is a d d i t i v e ,
G ( q ) = 0, and
Q
G
< i k > = (S, u 1 " , )
S
M k
G ( ( S
ii U S
<M>^ + C
-i * i* *
C C
- m
j n
G( S j j ) + G(S ( M ) > k ) + c M * c, * c . k
G H = 0, and
G., = m i n
G-. + G/- . + c , * c. * c . .
ik j u <j*l ),k i-l j n
k
w h i c h is t h e t y p e o f r e c u r r e n c e desired. H e r e is a p s e u d o - A l g o l a l g o r i t h m t h a t solves
functional equations:
I n p u t : i n t e g e r N > 0 , array C [ 1 : N + 1 ] of p o s i t i v e i n t e g e r s
Output: A r r a y G w h e r e G ( i , j ) = minimum c o s t of p a r e n t h e s i z i n g s u b s t r i n g (i,j>.
A r r a y H w h e r e H(i,j) = s u b s c r i p t K w h e r e optimal p a r e n t h e s i s f o r s u b s t r i n g ( i , j ) i s
located.
Time: 0(N ), Storage: 0 ( N )
3 2
F o r L <- 1 t h r u N d o
G(L,L) 4- 0;
F o r L <- 2 t h r u N d o / L = length of substrings optimized
F o r I 4- 1 t h r u N + l - L do
begin
M I N C O S T <- oo;
J 4- I + L -1;
F o r K 4- I t h r u J - l do / Find optimal paren for substring (IJ)
begin
C O S T 4 - G(I,K) + G ( K + 1 , J ) + C ( I - l ) * C ( K ) * C ( J ) ;
If C O S T < M I N C O S T t h e n
begin
MINCOST 4- COST;
G(I,J) 4- C O S T ;
H(I,J) 4 - K
end
end
end;
PAGE 16
algorithm for unambiguous context free language recognition and E a r l y [15] produced an
algorithm that not only recognizes general context free languages in O ( N ^ ) t i m e , b u t also
u n a m b i g u o u s l a n g u a g e s in O ( N ^ ) time, and some f u r t h e r r e s t r i c t e d c l a s s e s of l a n g u a g e s (such
a s L R ( k ) ) in 0 ( N ) time. S i n c e t h e n Valiant [ 7 0 ] c o n s t r u c t e d an 0 ( N ^ 1 ) 1 time g e n e r a l context
f r e e r e c o g n i t i o n a l g o r i t h m ( b y using S t r a s s e n matrix multiplication), a n d G r a h a m , H a r r i s o n , a n d
Ruzzo [24] produced an 0(N^/logN) time on-line algorithm for general context free
recognition. I n t h i s p a p e r w e d e s c r i b e the simplest O ( N ^ ) time a l g o r i t h m f o r g e n e r a l context
f r e e language recognition.
F o r e x a m p l e , if x « a
233 123 455 135>
a a a t h e n
Prefixfoa^g)- a
233 123 455*
a a T h e D D P i s
D - ( A , S , P, f ):
A = { a j f k | 1 < i < j < k < N } where ajj k produces the set { B | B - » C D and
c
^ * i i +l~ j
z z z a n d D
- > * j + l j - f 2 - k }»
z z z
S - { A*a l j N },
N o w improved to 0 < N ' ) with tho new matrix multiplication algorithm of Pan [54].
2 7 9
PAGE 17
P = { p r o d u c t i o n s }, a n d
f(e) = e, f<xajj ) k = { B | B->CD w h e r e 3p C ( f(Prefix(x,a j)) j p and 3q D (
f(Prefix(x,a ( j + 1 ) > q > k ) ) }.
a
ipk w
'" c e r
^ ' 'y
a n
be p r e f e r r e d over a | q k . If, h o w e v e r , f ( x a j ) = { B, D, E } a n d f ( x a j ) = { C
p k r k
Z = f s a ( A , Q , q Q , F, X ) w h e r e
A » s a m e as f o r t h e DDP,
Q =» { S j j , ( S j j U S k Z ) },
qo - ( Jt e
F = { S 1 N } ,
P = s a m e as f o r t h e D D P ,
k ( p ) = e.
B e f o r e w e d e r i v e t h e f u n c t i o n a l e q u a t i o n s w e must f i r s t s h o w that t h e m o n o t o n i c i t y property
is s a t i s f i e d . N o t e t h a t t h e c o s t f u n c t i o n is not s i m p l y a d d i t i v e as in t h e p r e v i o u s p r o b l e m s so
m o n o t o n i c i t y d o e s n o t c o m e q u i t e as automatically. T h u s , w e must s h o w t h a t if x , y « A * , t h e n
If X ( q , x ) - X ( q , y ) and f ( x ) > f ( y ) t h e n f ( x w ) > f ( y w ) , V w < A * .
0 0
G ( q ) = e, and
Q
G ( S
^ =
,o ,c U
,W G(S
ii (i*i),K>> <V <M), >> a , , p )
US S
k j k
H e r e is a n a l g o r i t h m f o r s o l v i n g the f u n c t i o n a l e q u a t i o n s :
A l g o r i t h m F o r G e n e r a l C o n t e x t F r e e Language Recognition
F o r I «- 1 t h r u N do
M(I,I) 4 - { A | " A - ^ a f < Productions }
F o r L *- 2 t h r u N d o / L = length of string;
F o r I <- 1 t h r u N + l - L do
begin
K « - I + L - 1;
M(I,K) «- e; / e = empty set
F o r J <- I t h r u K - l d o
M ( I , K ) <- M(I,K) u { A | 3 ^ M ( I , J ) , / S € M ( J + 1 , K ) ( A ^ / S ) }
end;
N o t e t h a t t h e i n n e r l o o p is n o t i m p l e m e n t e d t h e same h e r e as f o r t h e p r e v i o u s t w o algorithms.
T h i s is b e c a u s e t h e p a r t i a l o r d e r r e l a t i o n f o r c e s us to use t h e u n i o n o p e r a t i o n r a t h e r t h a n a
min. S i n c e t h e s e t o f n o n t e r m i n a l s is f i n i t e , the s i z e of e a c h of t h e s e t s M ( I , K ) is b o u n d e d by
a constant. T h u s , t h e e n t i r e a l g o r i t h m still r e q u i r e s o n l y O ( N ^ ) time.
N
min T\ i
D
OPTIMAL
ALPHABETIC
OPTIMAL ALPHABETIC TREE
CHARACTER PROBABILITY CODE
A 0.15 000
B 0.10 001 ,
C 0.15 010
D 0.10 Oil
E 0.20 10
F 0.05 1100
0.10 1101
G
0.15 111
H
F i g u r e 5 - 1 : E x a m p l e o f Optimal A l p h a b e t i c E n c o d i n g and R e l a t e d D e c i s i o n T r e e
For this presentation w e will assume that r - 2, thus making the codes binary codes.
l
PAGE 20
T h e f i r s t s t e p t o w a r d s o l v i n g the optimal a l p h a b e t i c e n c o d i n g p r o b l e m is t o d e f i n e t h e D D P .
L e t t h e a l p h a b e t A L P H = ( z j , z , . . . z ) . T h e DDP D = ( A , S, P, f ) is
2 N
S = { s e q u e n c e s o f a ^ c o r r e s p o n d i n g to total p a r e n t h e s i z a t i o n s of N e l e m e n t s },
P = { (p ,p , . . .
x 2 p ) « (Reals )
N
+ N
}, and
f(e) = 0, f ( x a j j k ) = f(x) + X P . m
m=i,k
Z = ( A , Q , q , F, X ) w h e r e
Q
A = s a m e as f o r t h e DDP,
Q - { S j j , ( S j j u S ) | l<i<j<k<l<N } U { e },
K I
= e
% '
F = { S 1 N } ,
P = s a m e as f o r t h e D D P ,
h( r, ( S . . u S ( > U k ) f a. ) = r +
jk S P , and
m
k ( p ) = 0.
G ( e ) = 0, a n d
« V - (S US 8 "i" ),a, ) * « V W
k ik V W '*>
k
= 1" G
ii +
W +
2 P m .
m=i
L e t t i n g Gjj = G ( S j j ) this becomes
Gjj - 0, and
k
G
iK - T G
ii + G
(M).K +
2 P -
m
• s h o w s h o w t h a t is d o n e .
PAGE 22
A l g o r i t h m f o r O p t i m a l A l p h a b e t i c Encoding
I n p u t : i n t e g e r N > 0 , a r r a y P [ 1 : N ] of p o s i t i v e r e a l s
O u t p u t : G ( I , K ) = c o s t of o p t i m a l e n c o d i n g of 2jZj+^...z«.
H ( I , K ) = s u b s c r i p t J f o r optimal p a r t i t i o n of ZjZj ^...z^.
+
Time: 0 ( N ) , Storage: 0 ( N )
3 2
/ Initialization of G and C;
C ( 0 ) 4 - 0;
F o r I «- 1 t h r u N do
begin
G ( I , I ) «- 0; C ( I ) «- C ( I - l ) + P(I)
end;
/ Optimization over all pairs (I>K).;
F o r L *- 2 t h r u N d o / L = length of string;
F o r I «- L t h r u N + l - L do
begin
K <~ I + L - 1;
M I N C O S T <- oo;
F o r J «- I t h r u K - l d o
begin
C O S T «- G ( I , J ) + G(J+1,K) + C ( K ) - C ( I - l ) ;
If C O S T < M I N C O S T t h e n
begin
M I N C O S T <- C O S T
G(I,K) <- C O S T ;
H(I,K) *- J ;
end
end
end;
D - < A , S , P, f ):
A = { aQ, a ^ , a2> . . . ajvj, ajvj+j }, / where a^ 1 < i < N finds the order statistic
s
i* 0
a
~ ^» anc
^ N+l
a =
M+i.
S = { x € A * | all aj a r e in x },
p = { a < M N
}, a n d
f ( e ) = 0 , f ( a j ) = M, f ( x a j ) = f ( x ) + a k - aj w h e r e j = max s u b s c r i p t o f a in x t h a t is
l e s s t h a n i, a n d k = min s u b s c r i p t of a in x that is g r e a t e r t h a n i.
w e w i l l p r e s e n t o n l y t h e r e c u r r e n c e r e l a t i o n and the a l g o r i t h m .
F u n c t i o n a l E q u a t i o n s f o r O p t i m i z i n g the S e a r c h f o r N O r d e r S t a t i s t i c s
G(i,i+1) = 0
F o r L <- 0 t h r u N d o
G ( L , L + 1 ) <- 0;
F o r L <- 2 t h r u N + l d o / L =» Length of substrings optimized
F o r I <- 0 t h r u N + i - L d o
begin
M I N C O S T <- oo;
K <- I + L;
/ Find optimal paren for substring (7,K;)
F o r J *- 1+1 t h r u K - l do
begin
C O S T <- G ( I , J ) + G(J,K) + A ( K ) - A ( I ) - 1;
If C O S T < M I N C O S T t h e n
begin
G ( I , K ) <- M I N C O S T f - C O S T ;
H(I,K) <- J
end
end
end;
W e c a n i m p r o v e t h i s a l g o r i t h m to O ( N l o g N ) time and 0 ( N ) s t o r a g e . T h i s is b e c a u s e it c a n b e
put it i n t h e f o r m o f an o p t i m a l a l p h a b e t i c e n c o d i n g p r o b l e m . In the f u n c t i o n a l equations
above, the quantity G(i,k) is i n t e r p r e t e d to equal the minimum c o s t of finding all order
s t a t i s t i c s a j , i < j < k, g i v e n t h a t aj and a k have already been determined. C h a n g e this to
N = 4
M = 100 G 0 1 2 CO 4 5 H 0 1 2 3 4 5
0 0 34 103 177 229 0 1 2 2 2
ORDER
1 0 49 118 159 l 2 3 3
STATISTICS
2 0 54 95 2 3 3
20 3 0 30 3 4
35 4 0 4
70 5 5
F i g u r e 5 - 2 : E x a m p l e of f i n d i n g optimal u s e of a linear s e l e c t i o n a l g o r i t h m .
G(i,i) = 0, and
k
J m=i
6 . O p t i m a l Partition Problems
G Q = 0, and
G. = m a x
G. + C
i l l ! )
w h i c h is v e r y s i m i l a r t o t h e s o l u t i o n i o r the single s o u r c e s h o r t e s t p a t h p r o b l e m d e s c r i b e d i n
Chapter 4. Because of this similarity this p r o b l e m will not b e d i s c u s s e d further. The
r e m a i n d e r o f t h i s c h a p t e r w i l l i n s t e a d d e s c r i b e an e x a m p l e w h e r e K is a g i v e n c o n s t a n t .
T h e a u t h o r k n o w s o f t w o n o n t r i v i a l p r o b l e m s w h i c h a r e of t h e optimal p a r t i t i o n t y p e (with
a given value for the required n u m b e r of p a r t i t i o n s K). One is the resource allocation
problem ( [ 1 3 ] , c h . 3 ) , b u t t h e b e s t example of this t y p e of p r o b l e m is t h e o n e s o l v e d by
Fisher's a l g o r i t h m [ 1 7 , 2 7 ] . * T o d e s c r i b e the p r o b l e m w e will f i r s t h a v e t o i n t r o d u c e some
notation. L e t t h e i n p u t s t r i n g of N elements be d e n o t e d x j , x , X 3 , . . . X|\j, w h e r e Xj is a r e a l .
2
A cluster ( i , j ) is s i m p l y t h e s u b s t r i n g (xj,Xj + j , . . . X j ) . C o r r e s p o n d i n g to e a c h c l u s t e r ( i , j ) is a
diameter D ( i , j ) > 0 t h a t is a f u n c t i o n of ( x j , X j j , . . . X j ) . T h e d i a m e t e r f u n c t i o n t h a t w i l l b e u s e d
+
i n t h i s d e s c r i p t i o n is
D(i,j) = jL ( x . - x ) , w h e r e x » ^2
k=i " k
" k-i (
J- , + 1 )
Vast
algorithms f o r several special cases of Fisher's clustering problem will be presented b y Shamos, B r o w n . Saxe
and Weide [66]. '
PAGE 27
k
m ,
^n
D ( I +1 I )
{K-partitions} ~ u
^j ' j.l -
+ i 1 ;
O n e m e t h o d o f s o l u t i o n is t o s i m p l y g e n e r a t e all p o s s i b l e K - p a r t i t i o n s a n d c h o o s e t h e one
( N - 1)!
(ID- ( N - K)! * (K - 1)!
possible partitions, which for large K and N will be prohibitive. Dynamic programming
D - ( A , S , P, f ):
P = { N - t u p l e s o f r e a l s }, and
D P a l g o r i t h m t h a t c o s t s 0 ( K N ) time and 0 ( K N ) s t o r a g e .
3 2
A n o t h e r c h o i c e o f s t a t e s is a v a i l a b l e ,
( l , j ) is p a r t i t i o n e d i n t o h s e t s of c o n s e c u t i v e e l e m e n t s . " T h u s , t h e S D P W = ( Z, P, h, k) is
Z = ( A , Q , q , F, X ) w h e r e Q
A = s a m e as f o r t h e DDP,
qo = > e
F - { S K N },
x ( s
hi' (i+l),j
a ) = S
(h+l),j'
P = s a m e as f o r t h e D D P ,
h(r,S ,a(
h i j + 1 ) j ) = r + D(i,j), and
k(p) = 0.
M o n o t o n i c i t y o f t h e S D P f o l l o w s f r o m a d d i t i v i t y of t h e c o s t f u n c t i o n D and t h e f a c t t h a t D ( i , j ) >
G ( e ) = 0, a n d
G ( S
^ =
<Vui"(i.l>,i> h ( G (
Vl>,i>. W a
(i.D,i>
-1" G
<Vn,i> +
°< 'i>i+1
Fisher's version of the algorithm for solving these equations first computes all of the
diameters D ( i , j ) a n d s t o r e s t h e m in an a r r a y , c o s t i n g O ( N ^ ) s t o r a g e . The algorithm below
c r e a t e s v e c t o r s S U M a n d S U M S Q , w h i c h e n a b l e the diameter to b e c o m p u t e d in 0 ( N ) s t o r a g e
at n o e x t r a e x p e n s e in time. ( T h i s trick d o e s not w o r k f o r ail d i a m e t e r f u n c t i o n s , t h o u g h . )
PAGE 29
I n p u t : X « N - t u p l e o f r e a l s , K = no. of s e t s in the p a r t i t i o n .
O u t p u t : A r r a y G s u c h t h a t G ( h , j ) = minimum cost f o r p a r t i t i o n i n g x j X 2 . » X j i n t o h sets
c o n s e c u t i v e elements. A r r a y H s u c h that H(h,j) = s u b s c r i p t k f o r t h e h'th
s e t o f t h e o p t i m a l p a r t i t i o n of x^x^y-y
T i m e : 0 ( K N ) , S t o r a g e : 0<KN)
2
The only trick is t o eliminate unnecessary storage that is b e i n g used. The diameter
f u n c t i o n is a l r e a d y c o m p u t e d in 0 ( N ) s t o r a g e . O n l y the a r r a y G c o s t s 0 ( K N ) s t o r a g e . (Forget
a b o u t H f o r n o w s i n c e o n l y t h e optimal cost is b e i n g c o m p u t e d . ) B u t in t h e i n n e r t w o l o o p s of
t h e a b o v e a l g o r i t h m o n l y t h e v e c t o r s G ( l - 1 , * ) and G(L,*) are r e f e r e n c e d . T h a t i s , at s t a g e L
o n l y t h e v e c t o r s G ( L - 1 , * ) a n d G(L,*) a r e n e e d e d . T h u s , the K x N a r r a y G c a n b e r e p l a c e d by
t w o v e c t o r s o f l e n g t h N a n d t h e c o s t of t h e optimal p a r t i t i o n c a n b e c o m p u t e d in O ( N ) s t o r a g e
PAGE 30
a n d 0 ( K I \ n time.
the N elements.
T(I,J) = I J 2
logK 2 ' 1 1
U(K,N> = I I [ 2 T
( | . W ' " Q-W->) l +
°( iM2'-'
I
" ^j-Dk/a-)]
l o g K [" 2 j l
"J
i=l L J = l 2' J
logK
< S |"o<> N +
-^T N1- 2
0(KN )2
7 . O p t i m a l Matching Problems
One of the major differences between optimal matching problems and optimal
p a r e n t h e s i z a t i o n o r p a r t i t i o n p r o b l e m s is that i n s t e a d of j u s t o n e s t r i n g of e l e m e n t s , t w o (or
m o r e ) are involved. T h e a u t h o r K n o w s of t w o p r o b l e m s in this class. The first, and most
i m p o r t a n t , is t h e l o n g e s t c o m m o n s u b s e q u e n c e ( L C S ) p r o b l e m ( a n d its v a r i a t i o n s ) , f o r which
DP produces a g o o d (if n o t o p t i m a l ) algorithm. T h e s e c o n d p r o b l e m is t h e g a m e o f N I M , f o r
w h i c h a m u c h f a s t e r a l g o r i t h m is k n o w n and thus will not be d e s c r i b e d f u r t h e r [ 2 1 ] . We will
n o w describe the LCS problem.
L e t Y = y 1Y2 • • • N y a n c
* ^ - z
i 2 • • •
z
be * w 0 s t r
' S
n s
°* l e n g t h N w h o s e e l e m e n t s y- f and
Zj a r e c h o s e n f r o m a finite alphabet ALPH. ( T h e s t r i n g s Y and Z a r e c h o s e n t o b e t h e s a m e
length only for c o n v e n i e n c e in p r e s e n t a t i o n . ) A s t r i n g B is a subsequence o f Y if B c a n be
o b t a i n e d b y d e l e t i n g c h a r a c t e r s f r o m Y. F o r e x a m p l e , if Y = "dyanamic p r o g r a m m i n g " , t h e n B =
" m a i n " is a s u b s e q u e n c e of Y. T h e l o n g e s t common s u b s e q u e n c e p r o b l e m ( f o r t h e t w o strings
Y a n d Z ) is t o f i n d t h e ( n o t n e c e s s a r i l y u n i q u e ) l o n g e s t s t r i n g B that is a s u b s e q u e n c e o f b o t h
Y a n d Z.
T h e L C S p r o b l e m c a n b e s o l v e d in 0 ( N ) time and s p a c e [ 2 9 ] .
2
The string editing p r o b l e m , a
g e n e r a l i z a t i o n o f t h e L C S p r o b l e m , is to d e t e r m i n e the minimum n u m b e r of c h a n g e s , i n s e r t i o n s ,
o r d e l e t i o n s t o t r a n s f o r m a s t r i n g Y to a s t r i n g Z. T h i s p r o b l e m c a n also b e s o l v e d in 0(N ) 2
W e w i l l n o w p r e s e n t t h e 0 ( N ) time and 0 ( N ) s p a c e a l g o r i t h m f o r t h e L C S p r o b l e m .
2 2
The
t w o s t r i n g s a r e , as b e f o r e , Y and Z, e a c h c o n s i s t i n g of N c h a r a c t e r s f r o m t h e a l p h a b e t ALPH.
T h e D D P D - ( A , S , P, f ) is d e f i n e d b y
A = { ajj } w h e r e aj j c o m p a r e s y j and Z j f o r e q u a l i t y ,
P = { 2 * N - t u p l e s o f e l e m e n t s in the a l p h a b e t A L P H }, and
w h e r e C ( y j , z j > = 1 if y j = Z j and C ( y j , Z j ) = 0 o t h e r w i s e .
Note that the cost function f is w r i t t e n s o that t h e o b j e c t ' is t o maximize f rather than
m i n i m i z e it. L e t t h e s t a t e s o f t h e SDP b e w r i t t e n Sjj and be i n t e r p r e t e d " ( y i , y 2 > — Y j ) h a s been
compared with (Z|Z2-..Zj) by a sequence ending in a j j . " T h u s , t h e S D P is W = ( V , P, h , k )
where
V = ( A , Q , q , F, X ) w h e r e
Q
A « s a m e as f o r t h e DDP,
^ 0 oo> = s
F = { S N N },
P = s a m e as f o r t h e D D P ,
k(p) = 0.
G(S 0 0 ) = 0 , and
G ( S
ii> =
{ ( S ^ ) | K <i and K j J
m 3 G ( S
M> + C
^i' i>-
z
This gives an 0 ( N ) 4
time a l g o r i t h m . H o w e v e r , a monotone p r o p e r t y of the f u n c t i o n G c a n
minimize the search for max in the e q u a t i o n a b o v e . Since t h e s u b s c r i p t s of the ajj are
r e q u i r e d t o b e s t r i c t l y i n c r e a s i n g in e a c h legal s e q u e n c e of c o m p a r i s o n s , it f o l l o w s t h a t
T h u s , t o f i n d G ( S j j ) it is s u f f i c i e n t to c o m p u t e
PAGE 34
G ( i , j ) <- M A X G ( i - l , j - l ) + C ( y j , z : )
M A X G ( i J ) <- G ( i , j )
M A X G ( i + l , j ) <- max( M A X G ( i + l , j ) , G(i,j) )
M A X G ( i , j + l ) <- max( M A X G ( i , j + l ) , G(i,j) ).
A n o t h e r w a y t o a c c o m p l i s h t h e p r o p a g a t i o n is to make a small c h a n g e in t h e d e f i n i t i o n o f G
so t h a t it a c c o m p l i s h e s t h e same task as M A X G , thus s a v i n g o n e a r r a y [ 2 9 ] . T h i s is w h a t is
d o n e in t h e f o l l o w i n g algorithm.
A l g o r i t h m f o r t h e L o n g e s t C o m m o n S u b s e q u e n c e of T w o S t r i n g s
I n p u t : i n t e g e r N > 0, a r r a y s Y [ 1 : N ] , Z[1:N]
O u t p u t : A r r a y G s u c h t h a t G ( I , J ) = l e n g t h of longest common s u b s e q u e n c e o f (yj,y2>—Yj)
and (zj,Z2,.»Zj). ^rray H s u c h that H(I,J) = p o i n t e r to e i t h e r ( I , J - 1 ) o r
( I - 1 , J ) , w h i c h e v e r has the l a r g e s t v a l u e of G.
Time: 0(N >, Storage: 0(N )
2 2
/ initialization;
F o r I <r- 0 t h r u N d o
F o r J 4- 0 t h r u N do
G ( I , J ) <- H(I,J) 4 - 0;
/ optimization;
F o r I <- 1 t h r u N do
F o r J «- 1 t h r u N do
begin
If Y ( I ) = Z ( J ) t h e n
G(I,J) G(I-1,J-1) + 1
else
G ( I , J ) <- max( G ( I , J - 1 ) , G ( I - 1 , J ) );
If G ( I , J - 1 ) > G ( I - 1 , J ) t h e n
H ( I , J ) <- ( I , J - 1 )
else
H ( I , J ) «- ( I - 1 . J )
end;
PAGE 35
G B A B B A H B A B B A
A 0 1 1 1 1 A 0,0 0,0 1,2 1,3 1,4
B A B B A B 1 1 2 2 2 B 0,0 2,1 2,2 2,3 2,4
\\\ I
A B A B A A 1 2 2 2 3 A 2,1 3,1 3,2 3,3 3,4
B 1 2 3 3 3 B 3,1 3,2 4,2 4,3 4,4
F i g u r e 7 - 1 : T h e m a t r i c e s G and H p r o d u c e d f o r the s t r i n g s A B A B A a n d B A B B A .
PAGE 36
T h e p r o b l e m s d e s c r i b e d in t h e p r e v i o u s s e c t i o n s can all be s o l v e d in 0 ( N ) , O ( N ^ ) , o r
2
more
g e n e r a l l y t i m e t h a t is b o u n d e d a b o v e b y a p o l y n o m i a l in N. T h e p r o b l e m s in t h i s s e c t i o n h a v e
n e v e r b e e n s o l v e d in l e s s t h a n 0 ( 2 ^ ) time. M a n y of t h e s e p r o b l e m s a r e N P - C o m p l e t e [ 4 3 , 2 ] .
( T h e o t h e r p r o b l e m s a p p e a r to be at least this hard.) F o r p r o b l e m s w h e r e a n a i v e algorithm
c o s t s 0 ( N ! ) t i m e , D P c a n t y p i c a l l y b r i n g an i m p r o v e m e n t to 0 ( P ( N ) * 2 ) time w h e r e P ( N ) is a
N
p o l y n o m i a l in N. F o r e x a m p l e , the t r a v e l i n g s a l e s p e r s o n p r o b l e m ( T S P ) r u n s in t i m e 0(N^2'v
[28, 6]. An improved DP algorithm for the TSP and knapsack problems that utilizes
b r a n c h - a n d - b o u n d is d e s c r i b e d b y M o r i n [ 5 3 ] . C o n s t r u c t i n g optimal d e c i s i o n t r e e s f r o m a s e t
o f y e s / n o t e s t s is N P - c o m p l e t e [ 3 7 ] and the DP algorithm r u n s in 0 ( 3 ^ ) time a n d s p a c e [ 6 4 ] .
( L e w [ 5 0 ] d e s c r i b e s a s o l u t i o n to a g e n e r a l i z a t i o n of this p r o b l e m . ) B a y e s [ 4 ] d e s c r i b e s a d i s k
f i l e p l a c e m e n t p r o b l e m - a v a r i a t i o n of the module placement p r o b l e m in K a r p a n d H e l d [ 4 4 ] -
f o r w h i c h a D P a l g o r i t h m s p e n d s 0 ( 2 ) time and 0 ( C ( N , N / 2 ) ) s t o r a g e .
N
Kohler and Steiglitz [49]
and Kohler [48] describe DP ( a n d b r a n c h - a n d - b o u n d ) s o l u t i o n s of N P - C o m p l e t e scheduling
problems. G i l m o r e [ 2 3 ] s h o w s interconnections b e t w e e n cutting stock, linear programming,
k n a p s a c k i n g , d y n a m i c p r o g r a m m i n g , and i n t e g e r p r o g r a m m i n g . Shapiro [67] s h o w s h o w the
knapsack problem can be reduced to a (huge) shortest path problem, and Lloyd [51]
d e s c r i b e s a D P a l g o r i t h m f o r t h e minimum w e i g h t t r i a n g u l a t i o n p r o b l e m .
S = { p e r m u t a t i o n s o f t h e aj e n d i n g in a^ },
f ( e ) = 0, f ( ) = D
a j u> f ( x a j a j ) = f ( x a j ) + Djj.
Z = ( A , Q , q , F, \ ) w h e r e
Q
A = s a m e as f o r t h e DDP,
Q - { S M > i | M c C and i (• M },
^0 = S
{1},1'
F
- « s , i }, c
x ( s
M,i' j a ) = S
Mu{j},j'
P = s a m e as f o r t h e D D P ,
k(p) - 0.
equations are
G(S { 1 } ) » 0, and
G(S ,) = j
M
m n
G(S .
M { i } i J ) • D j f
H e r e is a n a l g o r i t h m t o s o l v e t h e functional e q u a t i o n s :
PAGE 38
/ main routine;
! Initialization;
C « - { 2, 3 , 4, . . . N };
F o r I *- 1 t h r u N d o
G ( { I } , I ) «- D(1,I);
/ Optimization;
F o r K « - 2 t h r u N - l d o / K = size of subsets;
F o r all M c C s u c h that |M| = K do
F o r all I c M d o
MINIMIZE(G,H,DMI)j
MINIMIZE(G,HACu {!},!);
PAGE 39
9. C o n c l u s i o n
D y n a m i c P r o g r a m m i n g is a u s e f u l p r o b l e m s o l v i n g m e t h o d f o r many p r o b l e m s in c o m p u t e r
science. I n t h i s p a p e r w e h a v e d e s c r i b e d p r o b l e m s in s e v e r a l c a t e g o r i e s o f D P : Shortest
Path Problems, Optimal Parenthesization Problems, Optimal Partition Problems, Optimal
Matching P r o b l e m s , and ' H a r d ' Combinatorial Problems. T h e a p p r o a c h t a k e n in t h i s paper
t o w a r d s o l u t i o n o f t h e s e p r o b l e m s is a modification of the t r e a t m e n t in K a r p a n d H e l d [ 4 4 ] .
E a c h a n a l y s i s o f a p r o b l e m p r o c e e d s in the f o l l o w i n g s t e p s :
1. P r o d u c e t h e d i s c r e t e d e c i s i o n p r o c e s s f o r the p r o b l e m ,
3. Check the monotonicity conditions and (if satisfied) produce the functional
equations, and
4. F r o m t h e f u n c t i o n a l e q u a t i o n s c o n s t r u c t a d y n a m i c p r o g r a m m i n g algorithm that
solves the problem.
Acknowledgments
References
[I] A . V . A h o , D.S. H i r s c h b e r g , and J.D. Ullman, Bounds on the Complexity of the Longest
Common Subsequence Problem, J . A s s o c . C o m p . Mach., 23 ( 1 9 7 6 ) , p p . 1 - 1 2 .
[8] D.W. B o y d , Long Division: An Example of Dynamic Programming, Amer. Inst. Indust.
E n g i n . T r a n s . , volume (1974), pp. 365-366.
[12] E.W. D i j k s t r a , A Note on Two Problems in Connection With Graphs, Numer. Math., 1
(1959), pp. 269-271.
[20] M.L. F r e d m a n , On the Decision Tree Complexity of the Shortest Path Problems, 1 6 t h
A n n u a l S y m p o s i u m o n F o u n d a t i o n s of C o m p u t e r S c i e n c e , I E E E , L o n g B e a c h , C A . , O c t . ,
• 1975, p p . 9 8 - 9 9 .
[21] M. G a r d n e r , Concerning the game of nim and its mathematical analysis* Mathematical
G a m e s s e c t i o n of S c i e n t i f i c A m e r i c a n , 198 ( 1 9 5 8 ) , p p . 1 0 4 - 1 1 1 .
[27] J . A . H a r t i g a n , Clustering Algorithms, John Wiley & Sons, New York, 1975. Fisher
Algorithm on pages 130-140.
[31] D.S. H i r s c h b e r g , An information - theoretic lower bound for the longest common
subsequence problem, I n f o r m a t i o n P r o c e s s i n g Lett., 7 ( 1 9 7 8 ) , p p . 4 0 - 4 1 .
[34] D.A. H u f f m a n , A Method for the Construction of Minimum Redundancy Codes, Proc.
Inst. Radio Engin., 40 (1952), pp. 1098-1101.
[53] T . L . M o r i n , Branch and Bound Strategies for Dynamic Programming, Operations Res .
24 (1976), pp. 611-627.
PAGE 43
[55] M.S. P a t e r s o n , U n i v e r s i t y of W a r w i c k , E n g l a n d .
[67] J . F . S h a p i r o , Shortest Route Methods for Finite State Space Deterministic Dynamic
Programming Problems, S I A M J . , 16 ( 1 9 6 8 ) , p p . 1 2 3 2 - 1 2 5 0 .
[69] M.E. T h o m a s , A Survey of the State of the Art in Dynamic Programming, Amer. Inst,
of Indust. Engin., 8 (1976), pp. 59-69.
[74] C K . W o n g a n d A.K. C h a n d r a , Bounds for the String Editing Problem, J. Assoc. Comp.
Mach., 23 (1976), pp. 13-16.
CMU-CS-79-106
5. TYPE OF REPORT & PERIOD COVERED
4. T I T L E (and Subtitle)
• Interim
DYNAMIC PROGRAMMING IN COMPUTER SCIENCE 6. PERFORMING ORG. REPORT NUMBER
9. PERFORMING ORGANIZATION NAME AND ADDRESS 10. PROGRAM ELEMENT. PROJECT, TASK
A R E A 4 WORK U N I T N U M B E R S
Carnegie-Mellon University
Computer Science Department
Pittsburgh, PA 15213
11. CONTROLLING OFFICE NAME ANO ADORESS _ • 12. REPORT DATE
February 1979
Office of Naval Research
13. NUMBER OF PAGES
Arlington, VA 22217 •
49
14. MONITORING AGENCY N A M E & A D O R E S S f / / dltieront from Controlling Office) 15. SECURITY C L A S S , (of thla report)
UNCLASSIFIED
Same as above 15a. D E C L ASSI F l C A T I O N / DOWN G R A D I N G
SCHEDULE
17. DISTRIBUTION STATEMENT (of the abstract entered In Block 20, it different from Report)
19. KEY W O R D S (Continue on reverse aide if neceaeary and identify by block number)
DD i jan M
73 1473 E D I T I O N O F 1 N O V 6 5 IS O B S O L E T E UNCLASSIFIED
S/N 0102-014- 6601 | S E C U R I T Y C L A S S I F I C A T I O N O F T H I S P A G E (When Data Entered)