String Automata

Embed Size (px)

Citation preview

  • 8/3/2019 String Automata

    1/48

    T a b l e o f C o n t e n t s

    A u t o m a t a f o r M a t c h i n g P a t t e r n s

    M a x i m e C r o c h e m o r e a n d C h r i s t o p h e H a n c a r t : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2

    1 . P a t t e r n m a t c h i n g a n d a u t o m a t a : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2

    2 . N o t a t i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 3

    2 . 1 A l p h a b e t a n d w o r d s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 3

    2 . 2 L a n g u a g e s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 3

    2 . 3 R e g u l a r e x p r e s s i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 3

    2 . 4 F i n i t e a u t o m a t a : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 4

    2 . 5 A l g o r i t h m s f o r m a t c h i n g p a t t e r n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 5

    3 . R e p r e s e n t a t i o n s o f d e t e r m i n i s t i c a u t o m a t a : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 6

    3 . 1 T r a n s i t i o n m a t r i x : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 6

    3 . 2 A d j a c e n c y l i s t s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 7

    3 . 3 T r a n s i t i o n l i s t : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 7

    3 . 4 F a i l u r e f u n c t i o n : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 7

    3 . 5 T a b l e - c o m p r e s s i o n : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 8

    4 . M a t c h i n g r e g u l a r e x p r e s s i o n s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 8

    4 . 1 O u t l i n e : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 8

    4 . 2 R e g u l a r - e x p r e s s i o n - m a t c h i n g a u t o m a t a : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 9

    4 . 3 S e a r c h i n g w i t h r e g u l a r - e x p r e s s i o n - m a t c h i n g a u t o m a t a : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 1

    4 . 4 T i m e - s p a c e t r a d e - o : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 2

    5 . M a t c h i n g n i t e s e t s o f w o r d s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 2

    5 . 1 O u t l i n e : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 2

    5 . 2 D i c t i o n a r y - m a t c h i n g a u t o m a t a : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 3

    5 . 3 L i n e a r d i c t i o n a r y - m a t c h i n g a u t o m a t a : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 4

    5 . 4 S e a r c h i n g w i t h l i n e a r d i c t i o n a r y - m a t c h i n g a u t o m a t a : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 7

    6 . M a t c h i n g w o r d s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 8

    6 . 1 O u t l i n e : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 8

    6 . 2 S t r i n g - m a t c h i n g a u t o m a t a : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 1 9

    6 . 3 L i n e a r s t r i n g - m a t c h i n g a u t o m a t a : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 0

    6 . 4 P r o p e r t i e s o f s t r i n g - m a t c h i n g a u t o m a t a : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 2

    6 . 5 S e a r c h i n g w i t h l i n e a r s t r i n g - m a t c h i n g a u t o m a t a : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 4

    7 . S u x a u t o m a t a : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 6

    7 . 1 O u t l i n e : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 6

    7 . 2 S i z e s a n d p r o p e r t i e s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 2 7

    7 . 3 C o n s t r u c t i o n : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 3 1

    7 . 4 A s i n d e x e s : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 3 6

    7 . 5 A s s t r i n g - m a t c h i n g a u t o m a t a : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 3 9

    7 . 6 F a c t o r a u t o m a t a : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : 4 1

  • 8/3/2019 String Automata

    2/48

    A u t o m a t a f o r M a t c h i n g P a t t e r n s

    M a x i m e C r o c h e m o r e

    1

    a n d C h r i s t o p h e H a n c a r t

    2

    1

    I n s t i t u t G a s p a r d M o n g e , U n i v e r s i t e d e M a r n e - l a - V a l l e e , 9 3 1 6 6 N o i s y - l e - G r a n d C e d e x , F r a n c e

    2

    L a b o r a t o i r e d ' I n f o r m a t i q u e d e R o u e n , U n i v e r s i t e d e R o u e n , F a c u l t e d e s S c i e n c e s e t T e c h n i q u e s , 7 6 8 2 1 M o n t -

    S a i n t - A i g n a n C e d e x , F r a n c e

    1 . P a t t e r n m a t c h i n g a n d a u t o m a t a

    T h i s c h a p t e r d e s c r i b e s s e v e r a l m e t h o d s o f w o r d p a t t e r n m a t c h i n g t h a t a r e b a s e d o n t h e u s e o f a u t o m a t a .

    P a t t e r n m a t c h i n g ( i n w o r d s ) i s t h e p r o b l e m o f l o c a t i n g o c c u r r e n c e s o f a p a t t e r n i n a t e x t l e . T h e

    l e i s j u s t a s t r i n g o f s y m b o l s , b u t t h e p a t t e r n c a n b e s p e c i e d i n v a r i o u s w a y s . H e r e , w e o n l y c o n s i d e r

    p a t t e r n s d e s c r i b e d b y r e g u l a r e x p r e s s i o n s o r w e a k e r m e c h a n i s m s .

    S o l u t i o n s t o t h e p r o b l e m a r e b a s i c p a r t s o f m a n y t e x t p r o c e s s i n g t o o l s , s u c h a s e d i t o r s , p a r s e r s ,

    a n d i n f o r m a t i o n r e t r i e v a l s y s t e m s . T h e y a r e a l s o w i d e l y u s e d i n t h e a n a l y s i s o f b i o l o g i c a l s e q u e n c e s . T h e

    a l g o r i t h m s t h a t s o l v e t h e p r o b l e m c l a s s i c a l l y d e c o m p o s e i n t w o s t e p s : a p r e p r o c e s s i n g p h a s e a n d a s e a r c h

    p h a s e . W h e n t h e t e x t l e i s c o n s i d e r e d t o b e d y n a m i c ( a s i n e d i t i n g a p p l i c a t i o n s ) , t h e p r e p r o c e s s i n g i s

    a p p l i e d t o t h e p a t t e r n ( s e e S e c t i o n s 4 , 5 , a n d 6 ) . T h i s l e a d s a p o s t e r i o r i t o a g o o d s o l u t i o n r e g a r d i n g t h e

    e c i e n c y o f t h e a l g o r i t h m s o f t h i s c h a p t e r . W h e n t h e t e x t l e i s s t a t i c ( i f i t i s a d i c t i o n a r y , f o r e x a m p l e )

    t h e p r e p r o c e s s i n g a p p l i e d t o t h e t e x t b u i l d s a n i n d e x t h a t c a n l a t e r s u p p o r t e c i e n t l y s e v e r a l s e r i e s o f

    q u e r i e s ( s e e S e c t i o n 7 ) .

    W e p r e s e n t s o l u t i o n s i n w h i c h t h e s e a r c h p h a s e i s b a s e d o n a u t o m a t a a s o p p o s e d t o s o l u t i o n s b a s e d

    o n c o m b i n a t o r i a l p r o p e r t i e s o f w o r d s . T h u s , t h e a l g o r i t h m s p e r f o r m o n - l i n e s e a r c h e s w i t h a b u e r o n

    t h e t e x t t h a t d o e s n o t n e e d t o s t o r e m o r e t h a n o n e l e t t e r a t a t i m e . T h e s o l u t i o n s a r e a d e q u a t e f o r

    p r o c e s s i n g s e q u e n t i a l - a c c e s s l e s o r s t r e a m s o f s y m b o l s .

    T h e m a i n a l g o r i t h m s o f t h i s c h a p t e r s o l v e s p e c i a l i n s t a n c e s o f t h e d e t e r m i n i z a t i o n o r m i n i m i z a t i o n

    p r o b l e m s o f a u t o m a t a . B a s i c a l l y , g i v e n a n a u t o m a t o n t h a t r e c o g n i z e s t h e l a n g u a g e X o n t h e a l p h a b e t

    A , a l g o r i t h m s b u i l d a d e t e r m i n i s t i c , a n d s o m e t i m e s m i n i m a l , a u t o m a t o n f o r t h e l a n g u a g e A

    X , w h i c h

    i s a p p l i e d a f t e r w a r d s t o s e a r c h e c i e n t l y f o r w o r d s o f X

    T h e t i m e c o m p l e x i t y o f a l g o r i t h m s i s g i v e n a s a f u n c t i o n o f t h e i n p u t , a n d i s t y p i c a l l y l i n e a r i n t h e

    l e n g t h o f t h e i n p u t . T h i s t a k e s i n t o a c c o u n t t h e s e t o f l e t t e r s a c t u a l l y o c c u r r i n g i n t h e i n p u t . B u t t h e

    r u n n i n g t i m e m a y d e p e n d o n t h e o u t p u t a s w e l l . S o , a c a r e f u l s t a t e m e n t o f e a c h p r o b l e m i s n e c e s s a r y ,

    t o a v o i d f o r e x a m p l e q u a d r a t i c - s i z e o u t p u t s t h a t w o u l d o b v i o u s l y i m p l y q u a d r a t i c - t i m e a l g o r i t h m s .

    T h e c o m p l e x i t y o f a l g o r i t h m s i s a n a l y z e d i n a m o d e l o f a m a c h i n e i n w h i c h t h e b a s i c o p e r a t i o n o n

    l e t t e r s i s c o m p a r i s o n i n t h e f o r m l e s s - e q u a l - g r e a t e r . T h e i m p l i c i t o r d e r i n g o n t h e a l p h a b e t i s e x p l o i t e d i n

    s e v e r a l a l g o r i t h m s . T h e a s s u m p t i o n o n t h e m o d e l m a k e s i t p o s s i b l e t o p r o c e s s w o r d s o v e r a p o t e n t i a l l y

    u n b o u n d e d a l p h a b e t . S o m e a l g o r i t h m s f o r t h e s i m p l e s t p a t t e r n - m a t c h i n g p r o b l e m ( s e a r c h i n g f o r o n l y

    o n e w o r d ) o p e r a t e s i n a w e a k e r m o d e l ( c o m p a r i s o n i n t h e f o r m e q u a l - u n e q u a l ) . W e a l s o m e n t i o n h o w t h e

    r u n n i n g t i m e s o f m o s t a l g o r i t h m s a r e a e c t e d w h e n b r a n c h i n g s i n a u t o m a t a a r e p e r f o r m e d b y l o o k i n g

    u p a t r a n s i t i o n t a b l e ( s e e S e c t i o n 3 ) . T h i s i s v a l i d i f t h e a l p h a b e t i s k n o w n i n a d v a n c e a n d i f t h e

    l e t t e r s c a n b e a s s i m i l a t e d t o i n d i c e s o n a t a b l e . O t h e r w i s e , a s t r a i g h t f o r w a r d s i m u l a t i o n i m p l i e s t h a t

    t h e r u n n i n g t i m e s a r e m u l t i p l i e d b y O ( l o g c a r d ( A ) ) , w h i l e i n t h e c o m p a r i s o n m o d e l t h e r u n n i n g t i m e s

    o f s o m e a l g o r i t h m s a r e i n d e p e n d e n t o f t h e a l p h a b e t .

    T h e r e g u l a r - e x p r e s s i o n - m a t c h i n g p r o b l e m ( S e c t i o n 4 ) i s w h e n t h e p a t t e r n i s a g e n e r a l r e g u l a r e x -

    p r e s s i o n . T h e s t a n d a r d s o l u t i o n i s c e r t a i n l y b y T h o m p s o n ( 1 9 6 8 ) . T h e m e c h a n i s m i s o n e o f t h e b a s i c

    f e a t u r e s o f t h e U N I X o p e r a t i n g s y s t e m a n d o f i t s t o o l s .

    W h e n t h e l a n g u a g e d e s c r i b e d b y t h e p a t t e r n r e d u c e s t o a n i t e s e t o f w o r d s ( S e c t i o n 5 ) , c a l l e d a

    d i c t i o n a r y , t h e p a t t e r n - m a t c h i n g a l g o r i t h m r u n s i n l i n e a r t i m e ( o n a x e d a l p h a b e t ) i n s t e a d o f q u a d r a t i c

    t i m e f o r t h e g e n e r a l s o l u t i o n . M o r e o v e r , w h e n t h e p a t t e r n i s o n l y o n e w o r d ( S e c t i o n 6 ) , t h e s a m e r u n n i n g

    t i m e h o l d s , i n d e p e n d e n t l y o f t h e a l p h a b e t .

    T h e s u x a u t o m a t a p r e s e n t e d i n S e c t i o n 7 s e r v e a s i n d e x e s . T h e y p r o v i d e a s o l u t i o n t o t h e p a t t e r n -

    m a t c h i n g i n s t a n c e w h e r e t h e s e a r c h e d t e x t h a s t o b e p r e p r o c e s s e d . T h e m a i n p o i n t o f t h e s e c t i o n i s

  • 8/3/2019 String Automata

    3/48

    A u t o m a t a f o r M a t c h i n g P a t t e r n s 3

    t h e l i n e a r - t i m e c o n s t r u c t i o n o f s u x a u t o m a t a ( o n a x e d a l p h a b e t ) , w h i c h r e s u l t s p a r t i a l l y f r o m t h e i r

    l i n e a r s i z e .

    T h e e c i e n c y o f p a t t e r n - m a t c h i n g a l g o r i t h m s b a s e d o n a u t o m a t a s t r o n g l y r e l i e s o n p a r t i c u l a r r e p -

    r e s e n t a t i o n s o f t h e s e a u t o m a t a . T h i s i s w h y a r e v i e w o f s e v e r a l t e c h n i q u e s i s g i v e n i n S e c t i o n 3 . T h e

    r e g u l a r - e x p r e s s i o n - m a t c h i n g p r o b l e m , t h e d i c t i o n a r y - m a t c h i n g p r o b l e m , a n d t h e s t r i n g - m a t c h i n g p r o b -

    l e m a r e t r e a t e d r e s p e c t i v e l y i n S e c t i o n s 4 , 5 , a n d 6 . S e c t i o n 7 d e a l s w i t h s u x a u t o m a t a a n d t h e i r

    a p p l i c a t i o n s .

    2 . N o t a t i o n s

    T h i s s e c t i o n i s d e v o t e d t o a r e v i e w o f t h e m a t e r i a l u s e d i n t h i s c h a p t e r : a l p h a b e t , w o r d s , l a n g u a g e s ,

    r e g u l a r e x p r e s s i o n s , n i t e a u t o m a t a , a l g o r i t h m s f o r m a t c h i n g p a t t e r n s .

    2 . 1 A l p h a b e t a n d w o r d s

    L e t A b e a n i t e s e t , c a l l e d t h e a l p h a b e t . I t s e l e m e n t s a r e c a l l e d l e t t e r s , a n d , f o r c o n v e n i e n c e , w e d e n o t e

    t h e m b y a b c , a n d s o o n . F u r t h e r m o r e , w e a s s u m e t h a t t h e r e i s a n o r d e r i n g o n t h e a l p h a b e t .

    A w o r d i s a n i t e - l e n g t h s e q u e n c e o f l e t t e r s . T h e l e n g t h o f a w o r d u i s d e n o t e d b y u , a n d i t s j - t h

    l e t t e r b y u

    j

    . T h e s e t o f a l l w o r d s i s d e n o t e d b y A

    , t h e e m p t y w o r d b y " , a n d A

    +

    s t a n d s f o r A

    n f " g

    T h e p r o d u c t o f t w o w o r d s u a n d v , d e n o t e d b y u v o r u v , i s t h e w o r d o b t a i n e d b y w r i t i n g s e q u e n t i a l l y

    t h e l e t t e r s o f u t h e n t h e l e t t e r s o f v . G i v e n a w o r d u , t h e p r o d u c t o f k w o r d s i d e n t i c a l w i t h u i s d e n o t e d

    b y u

    k

    , s e t t i n g u

    0

    = " . D e n o t e d r e s p e c t i v e l y b y u w

    1

    a n d v

    1

    u a r e t h e w o r d s v a n d w w h e n u = v w

    A w o r d v i s s a i d t o b e a f a c t o r o f a w o r d u i f u = u v u f o r s o m e w o r d s u a n d u ; i t i s a p r o p e r

    f a c t o r o f u i f v 6= u a p r e x o f u i f u = " , a n d a s u x o f u i f u = "

    2 . 2 L a n g u a g e s

    A l a n g u a g e i s a n y s u b s e t o f A

    . T h e p r o d u c t o f t w o l a n g u a g e s U a n d V , d e n o t e d b y U V o r U V i s

    t h e l a n g u a g e f u v ( u v ) 2 U V g . D e n o t e d b y U

    k

    i s t h e s e t o f w o r d s o b t a i n e d b y m a k i n g p r o d u c t s

    o f k w o r d s o f U . T h e s t a r o f U , d e n o t e d b y U

    , i s t h e l a n g u a g e

    S

    k > 0

    U

    k

    . B y c o n v e n t i o n , t h e o r d e r

    o f d e c r e a s i n g p r e c e d e n c e f o r l a n g u a g e o p e r a t i o n s i n e x p r e s s i o n s d e n o t i n g l a n g u a g e s i s s t a r o r p o w e r ,

    p r o d u c t , u n i o n . B y m i s u s e , a l a n g u a g e r e d u c e d t o o n l y o n e w o r d u m a y b e d e n o t e d b y u i t s e l f i f n o

    c o n f u s i o n a r i s e s ( w i t h f u r t h e r n o t a t i o n s ) .

    T h e s e t s o f p r e x e s , o f f a c t o r s , a n d o f s u x e s o f a l a n g u a g e U a r e d e n o t e d r e s p e c t i v e l y b y P r e f ( U )

    F a c t ( U ) , a n d S u ( U ) . I f U i s n i t e , U s t a n d s f o r

    P

    u 2 U

    u ( t h e r e f o r e , n o t e t h a t c a r d ( A ) = A )

    T h e r i g h t c o n t e x t o f a w o r d u a c c o r d i n g t o a l a n g u a g e W i s t h e l a n g u a g e f u

    1

    w w 2 W g . T h e

    e q u i v a l e n c e g e n e r a t e d o v e r A

    b y t h e r e l a t i o n s

    u

    1

    W = v

    1

    W ; u ; v 2 A

    i s d e n o t e d b y

    W

    ; i t i s t h e r i g h t s y n t a c t i c c o n g r u e n c e a s s o c i a t e d w i t h t h e l a n g u a g e W

    2 . 3 R e g u l a r e x p r e s s i o n s

    R e g u l a r e x p r e s s i o n s a n d t h e l a n g u a g e s t h e y d e s c r i b e , t h e r e g u l a r l a n g u a g e s , a r e d e n e d i n d u c t i v e l y a s

    f o l l o w s :

    { 0 1 , a n d a a r e r e g u l a r e x p r e s s i o n s a n d d e s c r i b e s r e s p e c t i v e l y ? ( t h e e m p t y s e t ) , f " g , a n d f a g , f o r

    e a c h a 2 A

    { i f u a n d v a r e r e g u l a r e x p r e s s i o n s d e s c r i b i n g r e s p e c t i v e l y t h e r e g u l a r l a n g u a g e s U a n d V , t h e n u + v

    u v , a n d u

    a r e r e g u l a r e x p r e s s i o n s d e s c r i b i n g r e s p e c t i v e l y t h e r e g u l a r l a n g u a g e s U V U V , a n d

    U

  • 8/3/2019 String Automata

    4/48

    A u t o m a t a f o r M a t c h i n g P a t t e r n s 4

    B y c o n v e n t i o n , t h e o r d e r o f d e c r e a s i n g p r e c e d e n c e f o r o p e r a t i o n s i n r e g u l a r e x p r e s s i o n s i s s t a r (

    )

    p r o d u c t ( ) , s u m ( + ) . T h e d o t i s o f t e n o m i t t e d . P a r e n t h e s i z i n g c a n b e u s e d t o c h a n g e t h e p r e c e d e n c e

    o r d e r o f o p e r a t o r s .

    T h e l a n g u a g e d e s c r i b e d b y a r e g u l a r e x p r e s s i o n u i s d e n o t e d b y L a n g ( u ) . T h e l e n g t h u o f a r e g u l a r

    e x p r e s s i o n u i s t h e l e n g t h o f u r e c k o n e d o n t h e a l p h a b e t A f 0 1 +

    g ( p a r e n t h e s e s a n d p r o d u c t

    o p e r a t o r a r e n o t r e c k o n e d ) .

    2 . 4 F i n i t e a u t o m a t a

    A ( n i t e ) a u t o m a t o n ( w i t h o n e i n i t i a l s t a t e ) i s g i v e n b y a n i t e s e t Q , w h o s e e l e m e n t s a r e c a l l e d s t a t e s ,

    a n i n i t i a l s t a t e i , a s u b s e t T Q o f t e r m i n a l s t a t e s , a n d a s e t E Q A Q o f e d g e s

    A n e d g e ( p ; a ; q ) o f t h e a u t o m a t o n ( Q ; i ; T ; E ) i s a n o u t g o i n g e d g e f o r s t a t e p a n d a n i n g o i n g e d g e f o r

    s t a t e q ; s t a t e p i s t h e s o u r c e o f t h i s e d g e , l e t t e r a i t s l a b e l , a n d s t a t e q i t s t a r g e t . T h e n u m b e r o f e d g e s

    o u t g o i n g a s t a t e p i s c a l l e d t h e ( o u t g o i n g ) d e g r e e o f p . W e s a y t h a t t h e r e i s a p a t h l a b e l e d b y u f r o m

    s t a t e p t o s t a t e q i f t h e r e i s a n i t e s e q u e n c e ( r

    j 1

    a

    j

    r

    j

    )

    1 6 j 6 n

    o f e d g e s s u c h t h a t n = u a

    j

    = u

    j

    f o r

    e a c h j 2 f 1 ; : : : ; n g r

    0

    = p a n d r

    n

    = q . O n e a g r e e s t o d e n e a u n i q u e p a t h l a b e l e d b y " f r o m e a c h s t a t e

    p t o i t s e l f .

    A w o r d u i s r e c o g n i z e d b y t h e a u t o m a t o n A = ( Q ; i ; T ; E ) i f t h e r e e x i s t s a p a t h l a b e l e d b y u f r o m

    i t o s o m e s t a t e i n T . T h e s e t o f a l l w o r d s r e c o g n i z e d b y A i s d e n o t e d b y L a n g ( A ) . A l a n g u a g e X i s

    r e c o g n i z a b l e i f t h e r e e x i s t s a n a u t o m a t o n A s u c h t h a t X = L a n g ( A )

    A s a n e x a m p l e , t h e a u t o m a t o n d e p i c t e d i n F i g u r e 2 . 1 r e c o g n i z e s t h e l a n g u a g e f a b g

    a b a a a b . I t s

    i n i t i a l s t a t e i s 0 , a n d i t s o n l y t e r m i n a l s t a t e i s 6 .

    0 1 2 3 4 5 6

    a b a a a b

    b

    a

    b

    b

    b

    a

    b

    a

    F i g . 2 . 1 . A n a u t o m a t o n r e c o g n i z i n g t h e l a n g u a g e f a b g

    a b a a a b

    T h e a u t o m a t o n ( Q ; i ; T ; E ) i s d e t e r m i n i s t i c i f f o r e a c h ( p a ) 2 Q A t h e r e i s a t m o s t o n e s t a t e q

    s u c h t h a t ( p ; a ; q ) 2 E . I t i s c o m p l e t e i f f o r e a c h ( p a ) 2 Q A t h e r e i s a t l e a s t o n e s t a t e q s u c h t h a t

    ( p ; a ; q ) 2 E . I t i s n o r m a l i z e d i f c a r d ( T ) = 1 , t h e i n i t i a l s t a t e h a s n o i n g o i n g e d g e , a n d t h e t e r m i n a l s t a t e

    h a s n o o u t g o i n g e d g e . I t i s m i n i m a l i f i t i s d e t e r m i n i s t i c a n d i f e a c h d e t e r m i n i s t i c a u t o m a t o n r e c o g n i z i n g

    t h e s a m e l a n g u a g e m a p s o n t o i t ; i t h a s t h e m i n i m a l n u m b e r o f s t a t e s . T h e m i n i m a l a u t o m a t o n r e c o g n i z i n g

    t h e l a n g u a g e U i s d e n o t e d b y M ( U ) . I t c a n b e d e n e d w i t h t h e h e l p o f r i g h t c o n t e x t s b y :

    u

    1

    U u 2 A

    U

    u

    1

    U u 2 U

    ( u

    1

    U ; a ; ( u a )

    1

    U ) u 2 A

    a 2 A

    I n c a s e A = ( Q ; i ; T ; E ) i s a d e t e r m i n i s t i c a u t o m a t o n , i t i s c o n v e n i e n t t o c o n s i d e r t h e t r a n s i t i o n

    f u n c t i o n Q A ! Q o f A d e n e d f o r e a c h ( p a ) 2 Q A s u c h t h a t t h e r e i s a n o u t g o i n g e d g e l a b e l e d

    b y a f o r p b y

    ( p a ) = q ( ) ( p ; a ; q ) 2 E

    ( n o t i c e t h a t i s a p a r t i a l f u n c t i o n ) . E q u i v a l e n t l y , t h e q u a d r u p l e ( Q ; i ; T ; ) d e n o t e s t h e a u t o m a t o n A

    I n a n a t u r a l w a y , t h e t r a n s i t i o n f u n c t i o n e x t e n d s t o a f u n c t i o n m a p p i n g f r o m Q A

    t o Q a n d a l s o

    d e n o t e d b y s e t t i n g

  • 8/3/2019 String Automata

    5/48

    A u t o m a t a f o r M a t c h i n g P a t t e r n s 5

    ( p u ) =

    8

    >

    :

    p i f u = "

    ( ( p a ) v ) i f ( p a ) i s d e n e d a n d u = a v

    f o r s o m e ( a v ) 2 A A

    u n d e n e d o t h e r w i s e ,

    f o r e a c h ( p u ) 2 Q A

    I n a l g o r i t h m s t h a t m a n i p u l a t e a u t o m a t a , w e c o n s t a n t l y u s e t h e f u n c t i o n S t a t e - C r e a t i o n d e s c r i b e d

    i n F i g u r e 2 . 2 ( + s t a n d s f o r t h e u n i o n o f s e t s ) . T h i s a v o i d s g o i n g i n t o d e t a i l s o f t h e i m p l e m e n t a t i o n o f

    a u t o m a t a t h a t i s p r e c i s e l y t h e s u b j e c t o f S e c t i o n 3 .

    S t a t e - C r e a t i o n

    1 c h o s e a s t a t e q o u t o f Q

    2 Q Q + f q g

    3 r e t u r n q

    F i g . 2 . 2 . C r e a t i o n o f a n e w s t a t e a n d a d j u n c t i o n t o t h e s e t o f s t a t e s Q

    2 . 5 A l g o r i t h m s f o r m a t c h i n g p a t t e r n s

    T h e p a t t e r n m a t c h i n g p r o b l e m i s t o s e a r c h a n d l o c a t e o c c u r r e n c e s o f p a t t e r n s i n w o r d s ( o r t e x t u a l d a t a ,

    l e s s f o r m a l l y s p e a k i n g ) . A p a t t e r n r e p r e s e n t s a l a n g u a g e a n d i s d e s c r i b e d e i t h e r b y a w o r d , b y a n i t e

    s e t o f w o r d s , o r m o r e g e n e r a l l y , b y a r e g u l a r e x p r e s s i o n . W e d o n o t c o n s i d e r p a t t e r n s d e s c r i b e d b y o t h e r

    m e c h a n i s m s .

    L e t y b e t h e s e a r c h e d w o r d . A n o c c u r r e n c e i n y o f a p a t t e r n r e p r e s e n t e d b y t h e l a n g u a g e X i s a t r i p l e

    ( u ; x ; v ) w h e r e u v 2 A

    x 2 X , a n d s u c h t h a t y = u x v . T h e p o s i t i o n o f t h e o c c u r r e n c e ( u ; x ; v ) o f x i n

    y i s t h e l e n g t h u ; i t i s s o m e t i m e s m o r e c o n v e n i e n t t o c o n s i d e r t h e e n d - p o s i t i o n o f t h e s a m e o c c u r r e n c e ,

    w h i c h i s d e n e d a s t h e l e n g t h u x . O b s e r v e t h a t s e a r c h i n g y f o r w o r d s i n a l a n g u a g e X i s e q u i v a l e n t t o

    s e a r c h f o r p r e x e s o f y t h a t b e l o n g t o t h e l a n g u a g e A

    X ; t h e l a n g u a g e o f m o s t a u t o m a t a c o n s i d e r e d i n

    t h i s c h a p t e r i s o f t h i s f o r m .

    A c c o r d i n g t o a s p e c i c m a t c h i n g p r o b l e m , t h e i n p u t o f a n a l g o r i t h m i s a l a n g u a g e X d e s c r i b e d b y

    a w o r d , b y a n i t e s e t o f w o r d s , o r b y a r e g u l a r e x p r e s s i o n , a n d a w o r d y . T h e o u t p u t c a n h a v e s e v e r a l

    f o r m s . T o i m p l e m e n t a n a l g o r i t h m t h a t t e s t s w h e t h e r t h e p a t t e r n o c c u r s i n t h e w o r d o r n o t , t h e o u t p u t

    i s j u s t t h e b o o l e a n v a l u e t r u e o r f a l s e r e s p e c t i v e l y . I n a n o n - l i n e s e a r c h , w h a t i s d e s i r e d i s t h e w o r d ,

    s a y z , o n t h e a l p h a b e t f 0 1 g t h a t e n c o d e s t h e e x i s t e n c e o f e n d - p o s i t i o n s o f t h e p a t t e r n ; t h e l e n g t h o f

    z i s y + 1 , a n d i t s j + 1 - t h l e t t e r i s 1 e x a c t l y w h e n a n o c c u r r e n c e o f t h e p a t t e r n e n d s a t p o s i t i o n j i n

    y . T h e o u t p u t c a n a l s o b e t h e s e t , s a y P , o f p o s i t i o n s ( o r e n d - p o s i t i o n s ) i n y o f t h e p a t t e r n . T o a v o i d

    p r e s e n t i n g s e v e r a l v a r i a n t s o f a l g o r i t h m s , w e i n t r o d u c e t h e s t a t e m e n t

    o c c u r r e n c e i f e

    w h e r e e i s a n a p p r o p r i a t e p r e d i c a t e . I t c a n b e t r a n s l a t e d b y

    i f e

    t h e n r e t u r n t r u e

    i n t h e r s t c a s e ,

    i f e

    t h e n z z 1

    e l s e z z 0

    i n t h e s e c o n d c a s e , a n d

    i f e

    t h e n P P + f t h e c u r r e n t p o s i t i o n i n y g

  • 8/3/2019 String Automata

    6/48

    A u t o m a t a f o r M a t c h i n g P a t t e r n s 6

    M a t c h e r ( X y )

    P r e p r o c e s s i n g p h a s e

    1 b u i l t a n a u t o m a t o n ( Q ; i ; T ; E ) r e c o g n i z i n g A

    X

    S e a r c h p h a s e

    l e t b e t h e t r a n s i t i o n f u n c t i o n o f ( Q ; i ; T ; E )

    2 p i

    3 o c c u r r e n c e i f p 2 T

    4 f o r l e t t e r a f r o m r s t t o l a s t l e t t e r o f y

    5 l o o p p ( p ; a )

    6 o c c u r r e n c e i f p 2 T

    F i g . 2 . 3 . G i v e n a r e g u l a r l a n g u a g e X a n d a w o r d y , l o c a t e a l l o c c u r r e n c e s o f w o r d s i n X t h a t a r e f a c t o r s o f y

    i n t h e t h i r d c a s e . I n t h e r s t c a s e , t h e \ r e t u r n f a l s e " s t a t e m e n t h a s t o b e i n c l u d e d c o r r e s p o n d i n g l y a t

    t h e e n d o f t h e a l g o r i t h m ; a n d i n t h e o t h e r c a s e s , w o r d z a n d s e t P s h o u l d b e i n i t i a l i z e d a t t h e b e g i n n i n g

    o f t h e a l g o r i t h m a n d r e t u r n e d a t t h e e n d o f t h e a l g o r i t h m . F r o m n o w o n , t h e s t a n d a r d a l g o r i t h m f o r

    m a t c h i n g p a t t e r n s i n w o r d s c a n b e w r i t t e n a s i n F i g u r e 2 . 3 .

    T h e a s y m p t o t i c t i m e a n d s p a c e c o m p l e x i t i e s o f a l g o r i t h m M a t c h e r d e p e n d o n t h e r e p r e s e n t a t i o n o f

    t h e a u t o m a t o n , a n d m o r e s p e c i c a l l y , o n t h e r e p r e s e n t a t i o n o f t h e t r a n s i t i o n f u n c t i o n ( s e e S e c t i o n 3 ) .

    M o r e g e n e r a l l y , t h e c o m p l e x i t i e s o f a l g o r i t h m s , f u n c t i o n s o r p r o c e d u r e s d e v e l o p e d i n t h i s c h a p t e r a r e

    e x p r e s s i o n s o f t h e s i z e o f t h e i n p u t . T h e y i n c l u d e t h e s i z e o f t h e l a n g u a g e , t h e l e n g t h o f t h e s e a r c h e d

    w o r d , a n d t h e s i z e o f t h e a l p h a b e t . W e a s s u m e t h a t t h e \ o c c u r r e n c e i f e " s t a t e m e n t i s p e r f o r m e d i n

    c o n s t a n t t i m e . N e v e r t h e l e s s , a n a d h o c o u t p u t o f t e n u n d e r l i e s t h e c o m p l e x i t y r e s u l t .

    3 . R e p r e s e n t a t i o n s o f d e t e r m i n i s t i c a u t o m a t a

    S e v e r a l p a t t e r n m a t c h i n g a l g o r i t h m s r e l y o n a p a r t i c u l a r r e p r e s e n t a t i o n o f t h e d e t e r m i n i s t i c a u t o m a t o n

    u n d e r l y i n g t h e m e t h o d . I m p l e m e n t i n g a d e t e r m i n i s t i c a u t o m a t o n ( Q ; i ; T ; E ) r e m a i n s t o i m p l e m e n t t h e

    t r a n s i t i o n f u n c t i o n o f t h e a u t o m a t o n , w h i c h i s t h e g e n e r a l p r o b l e m o f r e a l i z i n g p a r t i a l f u n c t i o n s . F i v e

    m e t h o d s a r e d e s c r i b e d i n t h i s s e c t i o n : t r a n s i t i o n m a t r i x , a d j a c e n c y l i s t s , t r a n s i t i o n l i s t , f a i l u r e f u n c t i o n ,

    a n d t a b l e - c o m p r e s s i o n .

    T h e c h o i c e o f t h e r e p r e s e n t a t i o n o f t h e a u t o m a t o n i n u e n c e t h e t i m e n e e d e d t o c o m p u t e a t r a n s i t i o n ,

    i . e . t h e t i m e t o e v a l u a t e ( p a ) , f o r a n y s t a t e p a n d a n y l e t t e r a . T h i s t i m e i s c a l l e d t h e d e l a y , i n t h a t

    i t i s a l s o t h e t i m e s p e n t o n l e t t e r a b e f o r e m o v i n g t o t h e n e x t l e t t e r o f t h e i n p u t w o r d . B a s i c a l l y : o n

    t h e o n e h a n d , t h e t i m e t o e v a l u a t e ( p a ) i s c o n s t a n t i n a m o d e l w h e r e b r a n c h i n g s a r e a l l o w e d a n d a

    t r a n s i t i o n m a t r i x i m p l e m e n t s ; o n t h e o t h e r h a n d , i f c o m p a r i s o n o f l e t t e r s i s t h e o n l y o p e r a t i o n a l l o w e d

    o n t h e m , t h e t i m e t o e v a l u a t e ( p a ) i s O ( l o g c a r d ( A ) ) , a s s u m i n g t h a t a n y t w o l e t t e r s c a n b e c o m p a r e d

    i n o n e u n i t o f t i m e ( u s i n g b i n a r y o p e r a t i o n s = , 6= ) . I n t h e f o l l o w i n g , w e g i v e t h e m e m o r y s p a c e

    a n d t h e d e l a y a s s o c i a t e d t o e a c h t y p e o f r e p r e s e n t a t i o n . T h e r e i s a n o b v i o u s t r a d e - o b e t w e e n t h e s e t w o

    q u a n t i t i e s .

    I n t h e c h a p t e r , h a v i n g a r e p r e s e n t a t i o n R o f t h e t r a n s i t i o n f u n c t i o n , t h e a u t o m a t o n i s i n d i e r e n t l y

    d e n o t e d b y ( Q ; i ; T ; E ) ( Q ; i ; T ; ) , a n d ( Q ; i ; T ; R )

    3 . 1 T r a n s i t i o n m a t r i x

    T h e s i m p l e s t m e t h o d t o i m p l e m e n t t h e t r a n s i t i o n f u n c t i o n i s t o s t o r e i t s v a l u e s i n a Q A - m a t r i x .

    T h i s i s a m e t h o d o f c h o i c e f o r a c o m p l e t e d e t e r m i n i s t i c a u t o m a t o n o n a s m a l l a l p h a b e t a n d w h e n l e t t e r s

    c a n b e a s s i m i l a t e d t o i n d i c e s o n a n a r r a y . T h e s p a c e r e q u i r e d i s O ( c a r d ( Q ) c a r d ( A ) ) a n d t h e d e l a y i s

    O ( 1 ) .

    W h e n t h e a u t o m a t o n i s n o t c o m p l e t e , t h e r e p r e s e n t a t i o n s t i l l w o r k s e x c e p t t h a t t h e s e a r c h i n g p r o -

    c e d u r e c a n s t o p o n a n u n d e n e d t r a n s i t i o n . T h e m a t r i x c a n e v e n b e i n i t i a l i z e d i n t i m e p r o p o r t i o n a l t o

    t h e n u m b e r o f e d g e s o f t h e a u t o m a t o n w i t h t h e h e l p o f a s p a r s e m a t r i x r e p r e s e n t a t i o n t e c h n i q u e . T h e

    a b o v e c o m p l e x i t i e s a r e s t i l l v a l i d i n t h i s s i t u a t i o n .

    T h i s k i n d o f r e p r e s e n t a t i o n i m p l i c i t l y a s s u m e s t h a t t h e w o r k i n g a l p h a b e t i s x e d a n d k n o w n b y

    a d v a n c e . T h i s c o n t r a s t s w i t h t h e r e p r e s e n t a t i o n s o f S e c t i o n s 3 . 2 a n d 3 . 4 f o r w h i c h t h e b a s i c o p e r a t i o n

    o n t h e a l p h a b e t i s c o m p a r i n g l e t t e r s .

  • 8/3/2019 String Automata

    7/48

    A u t o m a t a f o r M a t c h i n g P a t t e r n s 7

    3 . 2 A d j a c e n c y l i s t s

    A t r a d i t i o n a l w a y o f i m p l e m e n t i n g g r a p h s i s t o u s e a d j a c e n c y l i s t s . T h i s a p p l i e s t o a u t o m a t a a s w e l l .

    D o i n g s o , t h e s e t o f c o u p l e s ( a ( p a ) ) , w h e n e v e r ( p a ) i s d e n e d , i s a s s o c i a t e d w i t h e a c h s t a t e p 2 Q

    T h e s p a c e r e q u i r e d t o r e p r e s e n t t h e c a r d ( Q ) a d j a c e n c y l i s t s o f t h e a u t o m a t o n i s O ( c a r d ( Q ) + c a r d ( E ) )

    C o n t r a r y t o t h e p r e v i o u s m e t h o d , t h i s o n e w o r k s e v e n i f t h e o n l y p o s s i b l e e l e m e n t a r y o p e r a t i o n o n l e t t e r s

    i s c o m p a r i s o n . D e n o t i n g b y d t h e m a x i m u m d e g r e e o f s t a t e s o f t h e a u t o m a t o n , t h e d e l a y i s O ( l o g d )

    w h i c h i s a l s o O ( l o g m i n f c a r d ( Q ) c a r d ( A ) g ) , u s i n g a n e c i e n t i m p l e m e n t a t i o n o f s e t s b a s e d f o r i n s t a n c e

    o n b a l a n c e d t r e e s .

    T h e s p a c e c o m p l e x i t y m a y b e f u r t h e r r e d u c e d b y c o n s i d e r i n g a d e f a u l t ( t a r g e t ) s t a t e a s s o c i a t e d t o

    e a c h a d j a c e n c y l i s t ( t h e m o s t f r e q u e n t l y o c c u r r i n g t a r g e t o f a g i v e n a d j a c e n c y l i s t i s a n o b v i o u s c h o i c e

    a s d e f a u l t f o r t h i s a d j a c e n c y l i s t ) . T h e d e l a y c a n e v e n b e i m p r o v e d a t t h e s a m e t i m e b e c a u s e a d j a c e n c y

    l i s t s b e c o m e s m a l l e r .

    W h e n i m p l e m e n t i n g t h e a u t o m a t o n , e a c h a d j a c e n c y l i s t i s s t o r e d i n a n a r r a y G i n d e x e d b y Q . I f t h e

    d e t e r m i n i s t i c a u t o m a t o n i s c o m p l e t e a n d i f t h e i n i t i a l s t a t e i i s t h e u n i f o r m d e f a u l t s t a t e ( i a s d e f a u l t

    s t a t e t s i n p e r f e c t l y w i t h p a t t e r n m a t c h i n g a p p l i c a t i o n s ) , t h e c o m p u t a t i o n o f a t r a n s i t i o n f r o m a n y

    s t a t e p b y a n y l e t t e r a , t h a t i s , t h e c o m p u t a t i o n o f ( p a ) , i s d o n e b y t h e f u n c t i o n o f F i g u r e 3 . 1 .

    A d j a c e n c y L i s t s - T r a n s i t i o n ( p ; a )

    1 p s t a t e o f t h e c o u p l e o f l a b e l a n G p

    2 i f p = n i l

    3 t h e n p i

    4 r e t u r n p

    F i g . 3 . 1 . C o m p u t a t i o n o f t h e t r a n s i t i o n f r o m a s t a t e p b y a l e t t e r a w h e n a n a r r a y G o f a d j a c e n c y l i s t s r e p r e s e n t s

    t h e t r a n s i t i o n f u n c t i o n .

    3 . 3 T r a n s i t i o n l i s t

    T h e t r a n s i t i o n l i s t m e t h o d c o n s i s t s i n i m p l e m e n t i n g t h e l i s t o f t r i p l e s ( p ; a ; q ) o f e d g e s o f t h e a u t o m a t o n .

    T h e s p a c e r e q u i r e d b y t h e i m p l e m e n t a t i o n i s o n l y O ( c a r d ( E ) ) . D o i n g s o , i t i s a s s u m e d t h a t t h e l i s t

    i s s t o r e d i n a h a s h i n g t a b l e t o p r o v i d e f a s t c o m p u t a t i o n s o f t r a n s i t i o n s . T h e c o r r e s p o n d i n g h a s h i n g

    f u n c t i o n i s d e n e d o n c o u p l e s ( p a ) f r o m Q A . T h e n , g i v e n a c o u p l e ( p a ) , t h e a c c e s s t o t h e t r a n s i t i o n

    ( p ; a ; q ) , i f i t a p p e a r s i n t h e l i s t , i s p e r f o r m e d i n c o n s t a n t t i m e o n t h e a v e r a g e u n d e r u s u a l h y p o t h e s e s

    o n t h e t e c h n i q u e .

    3 . 4 F a i l u r e f u n c t i o n

    T h e m a i n i d e a o f t h e f a i l u r e f u n c t i o n m e t h o d i s t o r e d u c e t h e s p a c e n e e d e d b y , b y d e f e r r i n g , i n

    m o s t p o s s i b l e c a s e s , t h e c o m p u t a t i o n o f t h e t r a n s i t i o n f r o m t h e c u r r e n t s t a t e t o t h e c o m p u t a t i o n o f t h e

    t r a n s i t i o n f r o m a n o t h e r g i v e n s t a t e w i t h t h e s a m e i n p u t l e t t e r . I t s e r v e s t o i m p l e m e n t d e t e r m i n i s t i c

    a u t o m a t a i n t h e c o m p a r i s o n m o d e l . I t s m a i n a d v a n t a g e i s t h a t , i n g e n e r a l , i t p r o v i d e s a l i n e a r - s p a c e

    r e p r e s e n t a t i o n , a n d , s i m u l t a n e o u s l y , g i v e s a l i n e a r - t i m e c o s t f o r a s e r i e s o f t r a n s i t i o n s , t h o u g h t h e t i m e

    t o c o m p u t e o n e t r a n s i t i o n i s n o t n e c e s s a r i l y c o n s t a n t .

    W e o n l y c o n s i d e r t h e c a s e w h e r e t h e d e t e r m i n i s t i c a u t o m a t a i s c o m p l e t e a n d w h e r e i i s t h e d e f a u l t

    s t a t e ( e x t e n s i o n s o f t h e f o l l o w i n g s t a t e m e n t a r e n o t n e e d e d i n t h e c h a p t e r ) .

    L e t b e a f u n c t i o n f r o m Q A t o Q , a n d l e t f b e a f u n c t i o n f r o m Q i n t o i t s e l f . W e s a y t h a t t h e

    c o u p l e ( f ) r e p r e s e n t s t h e t r a n s i t i o n f u n c t i o n i f i s a s u b f u n c t i o n o f a n d i f

    ( p a ) =

    8

  • 8/3/2019 String Automata

    8/48

    A u t o m a t a f o r M a t c h i n g P a t t e r n s 8

    A s s u m i n g a r e p r e s e n t a t i o n o f b y a d j a c e n c y l i s t s , t h e s p a c e n e e d e d t o r e p r e s e n t b y t h e c o u p l e

    ( f ) i s O ( c a r d ( Q ) + c a r d ( E ) ) , w h e r e

    E = f ( p ; a ; q ) ( p ; a ; q ) 2 E a n d ( p a ) i s d e n e d g )

    w h i c h i s o f c o u r s e O ( c a r d ( Q ) + c a r d ( E ) ) s i n c e E E . ( N o t i c e t h a t i s t h e t r a n s i t i o n f u n c t i o n o f t h e

    a u t o m a t o n ( Q ; i ; T ; E ) . ) I f d i s t h e m a x i m u m d e g r e e o f s t a t e s o f t h e a u t o m a t o n ( Q ; i ; T ; E ) , t h e d e l a y

    i s t y p i c a l l y O ( c a r d ( Q ) l o g d ) , t h a t i s a l s o O ( c a r d ( Q ) l o g c a r d ( A ) )

    W h e n i m p l e m e n t i n g t h e a u t o m a t o n , t h e v a l u e s o f t h e f a i l u r e f u n c t i o n f a r e s t o r e d i n a n a r r a y F

    i n d e x e d b y Q . T h e c o m p u t a t i o n o f a t r a n s i t i o n i s d o n e b y t h e f u n c t i o n o f F i g u r e 3 . 2 . T h e f u n c t i o n

    a l w a y s s t o p s i f w e a s s u m e t h a t f d e n e s a n o r d e r o n Q

    F a i l u r e F u n c t i o n - T r a n s i t i o n ( p ; a )

    1 w h i l e p 6= n i l a n d ( p ; a ) = n i l

    2 l o o p p F p

    3 i f p 6= n i l

    4 t h e n p ( p ; a )

    5 e l s e p i

    6 r e t u r n p

    F i g . 3 . 2 . C o m p u t a t i o n o f t h e t r a n s i t i o n f r o m a s t a t e p b y a l e t t e r a w h e n a s u b t r a n s i t i o n a n d a n a r r a y F

    c o r r e s p o n d i n g t o a f a i l u r e f u n c t i o n r e p r e s e n t t h e t r a n s i t i o n f u n c t i o n .

    3 . 5 T a b l e - c o m p r e s s i o n

    T h e l a t e s t m e t h o d i s a m i x o f t h e p r e v i o u s o n e s t h a t p r o v i d e s f a s t c o m p u t a t i o n s o f t r a n s i t i o n s v i a a r r a y s

    a n d a c o m p a c t r e p r e s e n t a t i o n o f e d g e s v i a f a i l u r e f u n c t i o n .

    F o u r a r r a y s , d e n o t e d h e r e b y f a i l b a s e t a r g e t , a n d c h e c k , a r e u s e d . T h e f a i l a n d b a s e a r r a y s a r e

    i n d e x e d b y Q , a n d , f o r e a c h ( p a ) 2 Q A b a s e p + a i s a n i n d e x o n t a r g e t a n d c h e c k a r r a y s , a s s i m i l a t i n g

    l e t t e r s t o i n t e g e r s .

    T h e c o m p u t a t i o n o f t h e t r a n s i t i o n f r o m s o m e s t a t e p w i t h s o m e i n p u t l e t t e r a p r o c e e d s a s f o l l o w s :

    l e t k = b a s e p + a ; t h e n , i f c h e c k k = p t a r g e t k ] i s t h e t a r g e t o f t h e e d g e o f s o u r c e p a n d l a b e l a

    o t h e r w i s e , t h i s s t a t e m e n t i s r e p e a t e d r e c u r s i v e l y w i t h s t a t e f a i l p ] a n d l e t t e r a . ( N o t i c e t h a t i t i s c o r r e c t

    i f f a i l d e n e s a n o r d e r o n Q , a s i n S e c t i o n 3 . 4 , a n d i f t h e t a r g e t s f r o m t h e s m a l l e s t e l e m e n t a r e a l l

    d e n e d . ) T h e c o r r e s p o n d i n g f u n c t i o n i s g i v e n i n F i g u r e 3 . 3 .

    T a b l e C o m p r e s s i o n - T r a n s i t i o n ( p ; a )

    1 w h i l e c h e c k b a s e p + a 6= p

    2 l o o p p f a i l p

    3 r e t u r n t a r g e t b a s e p + a

    F i g . 3 . 3 . C o m p u t a t i o n o f t h e t r a n s i t i o n f r o m a s t a t e p b y a l e t t e r a i n t h e t a b l e - c o m p r e s s i o n m e t h o d w i t h

    s u i t a b l e a r r a y s f a i l b a s e t a r g e t , a n d c h e c k

    I n t h e w o r s t c a s e , t h e s p a c e n e e d e d i s O ( c a r d ( Q ) c a r d ( A ) ) a n d t h e d e l a y i s O ( c a r d ( Q ) ) . H o w e v e r

    t h e m e t h o d c a n r e d u c e t h e s p a c e t o O ( c a r d ( Q ) + c a r d ( A ) ) w i t h a n O ( 1 ) d e l a y i n b e s t p o s s i b l e s i t u a t i o n .

    4 . M a t c h i n g r e g u l a r e x p r e s s i o n s

    4 . 1 O u t l i n e

    P r o b l e m 4 . 1 . ( R e g u l a r - e x p r e s s i o n - m a t c h i n g p r o b l e m . ) G i v e n a r e g u l a r e x p r e s s i o n x , p r e p r o c e s s i t i n

    o r d e r t o l o c a t e a l l o c c u r r e n c e s o f w o r d s o f L a n g ( x ) t h a t o c c u r i n a n y g i v e n w o r d y

  • 8/3/2019 String Automata

    9/48

    A u t o m a t a f o r M a t c h i n g P a t t e r n s 9

    A w e l l - k n o w n s o l u t i o n t o t h e a b o v e p r o b l e m i s c o m p o s e d o f t w o p h a s e s . F i r s t , t r a n s f o r m t h e r e g u l a r

    e x p r e s s i o n x i n t o a n o n d e t e r m i n i s t i c a u t o m a t o n t h a t r e c o g n i z e s t h e l a n g u a g e d e s c r i b e d b y x , f o l l o w i n g

    a c o n s t r u c t i o n d u e t o T h o m p s o n . S e c o n d , s i m u l a t e t h e o b t a i n e d a u t o m a t o n w i t h i n p u t w o r d y i n s u c h

    a w a y t h a t i t r e c o g n i z e s e a c h p r e x o f y t h a t b e l o n g s t o A

    L a n g ( x )

    M a i n T h e o r e m 4 . 1 . T h e r e g u l a r e x p r e s s i o n - m a t c h i n g p r o b l e m f o r x a n d y c a n b e a c h i e v e d i n t h e

    f o l l o w i n g t e r m s :

    { a p r e p r o c e s s i n g p h a s e o n x b u i l d i n g a n a u t o m a t o n o f s i z e O ( x ) , p e r f o r m e d i n t i m e O ( x ) a n d O ( x )

    e x t r a - s p a c e ;

    { a s e a r c h p h a s e e x e c u t i n g t h e a u t o m a t o n o n y p e r f o r m e d i n t i m e O ( x y ) a n d O ( x ) s p a c e , t h e t i m e

    s p e n t o n e a c h l e t t e r o f y b e i n g O ( x )

    T h e c o n s t r u c t i o n o f t h e a u t o m a t o n i s g i v e n i n S e c t i o n 4 . 2 . I n S e c t i o n 4 . 3 , w e r s t s h o w h o w t o s o l v e

    t h e m e m b e r s h i p t e s t , n a m e l y , \ d o e s y b e l o n g s t o L a n g ( x ) ? " ; w e t h e n p r e s e n t t h e s o l u t i o n t o t h e s e a r c h

    p h a s e o f t h e r e g u l a r - e x p r e s s i o n - m a t c h i n g p r o b l e m a s a m e r e t r a n s f o r m a t i o n o f t h e p r e v i o u s t e s t . F i n a l l y ,

    i n S e c t i o n 4 . 4 , w e d i s c u s s a p o s s i b l e u s e o f a d e t e r m i n i s t i c a u t o m a t o n t o s o l v e t h e p r o b l e m .

    I n t h e w h o l e s e c t i o n , w e a s s u m e t h a t t h e r e g u l a r e x p r e s s i o n c o n t a i n s n o r e d u n d a n t p a r e n t h e s e s ,

    b e c a u s e o t h e r w i s e t h e p a r s i n g o f t h e e x p r e s s i o n w o u l d n o t b e n e c e s s a r i l y a s y m p t o t i c a l l y l i n e a r i n t h e

    l e n g t h o f t h e e x p r e s s i o n .

    4 . 2 R e g u l a r - e x p r e s s i o n - m a t c h i n g a u t o m a t a

    I n o r d e r t o s o l v e t h e p r o b l e m i n s p a c e l i n e a r i n t h e l e n g t h o f t h e r e g u l a r e x p r e s s i o n , w e c o n s i d e r s p e c i a l

    n o n d e t e r m i n i s t i c a u t o m a t a .

    W e s a y t h a t a n a u t o m a t o n i s e x t e n d e d i f i t i s d e n e d o n t h e e x t e n d e d a l p h a b e t A f " g . O b s e r v e

    t h a t a t r a n s i t i o n f r o m a s t a t e t o a n o t h e r i n a n e x t e n d e d a u t o m a t o n m a y e i t h e r r e s u l t o f t h e r e a d i n g o f

    a l e t t e r f r o m t h e i n p u t w o r d , o r n o t ( " - t r a n s i t i o n ) .

    T h e o r e m 4 . 2 . L e t x b e a r e g u l a r e x p r e s s i o n . T h e r e e x i s t s a n o r m a l i z e d e x t e n d e d a u t o m a t o n r e c o g n i z i n g

    L a n g ( x ) s a t i s f y i n g t h e f o l l o w i n g c o n d i t i o n s :

    ( i ) t h e n u m b e r o f s t a t e s i s b o u n d e d b y 2 x ;

    ( i i ) t h e n u m b e r o f e d g e s l a b e l e d b y l e t t e r s o f A i s b o u n d e d b y x , a n d t h e n u m b e r o f e d g e s l a b e l e d b y "

    i s b o u n d e d b y 4 x ;

    ( i i i ) f o r e a c h s t a t e t h e n u m b e r o f i n g o i n g o r o u t g o i n g e d g e s i s a t m o s t 2 , a n d i t i s e x a c t l y 2 o n l y w h e n

    t h e e d g e s a r e l a b e l e d b y "

    P r o o f . T h e p r o o f i s b y i n d u c t i o n o n t h e l e n g t h o f r e g u l a r e x p r e s s i o n s .

    T h e r e g u l a r e x p r e s s i o n s o f l e n g t h e q u a l t o 1 a r e 0 1 , a n d a , f o r e a c h a 2 A . T h e y a r e r e s p e c t i v e l y

    r e c o g n i z e d b y n o r m a l i z e d e x t e n d e d a u t o m a t a i n t h e f o r m

    i t

    i

    t

    ?

    i t

    i

    t

    ( i ; " ; t )

    a n d

    i t

    i

    t

    ( i ; a ; t )

    w h e r e i a n d t a r e t w o d i s t i n c t s t a t e s . T h e a u t o m a t a a r e d e p i c t e d i n F i g u r e 4 . 1 .

    i

    t

    i

    t

    "

    i

    t

    a

    ( I ) ( I I ) ( I I I )

    F i g . 4 . 1 . N o r m a l i z e d e x t e n d e d a u t o m a t a r e c o g n i z i n g t h e r e g u l a r e x p r e s s i o n s 0 ( I ) , 1 ( I I ) , a n d a ( I I I ) f o r s o m e

    a 2 A

  • 8/3/2019 String Automata

    10/48

    A u t o m a t a f o r M a t c h i n g P a t t e r n s 1 0

    N o w , l e t ( Q i f t g E ) a n d ( Q i f t g E ) b e n o r m a l i z e d e x t e n d e d a u t o m a t a r e c o g n i z i n g r e s p e c -

    t i v e l y t h e r e g u l a r e x p r e s s i o n s u a n d v , a s s u m i n g t h a t Q \ Q = ? . T h e n t h e r e g u l a r e x p r e s s i o n s u + v

    u v , a n d u

    a r e r e s p e c t i v e l y r e c o g n i z e d b y n o r m a l i z e d e x t e n d e d a u t o m a t a i n t h e f o r m

    Q Q

    i t

    i

    t

    E E

    ( i ; " ; i ) ( i ; " ; i ) ( t ; " ; t ) ( t ; " ; t )

    w h e r e i a n d t a r e t w o d i s t i n c t s t a t e s c h o s e n o u t o f Q Q

    Q Q i

    t

    E E

    ( t ; " ; i )

    a n d

    Q

    i t

    i

    t

    E E

    ( i ; " ; i ) ( i ; " ; t ) ( t ; " ; i ) ( t ; " ; t )

    w h e r e i a n d t a r e t w o d i s t i n c t s t a t e s c h o s e n o u t o f Q . T h e a u t o m a t a a r e d e p i c t e d i n F i g u r e 4 . 2 .

    i

    i t

    i t

    t

    "

    "

    "

    "

    i

    i t

    t

    " "

    "

    "

    ( I ) ( I I I )

    i t i t

    "

    ( I I )

    F i g . 4 . 2 . N o r m a l i z e d e x t e n d e d a u t o m a t a r e c o g n i z i n g t h e r e g u l a r e x p r e s s i o n s u + v ( I ) , u v ( I I ) , a n d u

    ( I I I ) ,

    o b t a i n e d f r o m n o r m a l i z e d e x t e n d e d a u t o m a t a ( Q i f t g E ) a n d ( Q i f t g E ) r e c o g n i z i n g r e s p e c t i v e l y t h e

    r e g u l a r e x p r e s s i o n s u a n d v

    T h e a b o v e c o n s t r u c t i o n c l e a r l y p r o v e s t h e e x i s t e n c e o f a n o r m a l i z e d e x t e n d e d a u t o m a t o n r e c o g n i z i n g

    t h e l a n g u a g e d e s c r i b e d b y a n y g i v e n r e g u l a r e x p r e s s i o n . I t r e m a i n s t o c h e c k t h a t t h e a u t o m a t o n s a t i s e s

    c o n d i t i o n s ( i ) t o ( i i i ) . C o n d i t i o n ( i ) h o l d s s i n c e e x a c t l y t w o n o d e s a r e c r e a t e d f o r e a c h l e t t e r o f a r e g u l a r

    e x p r e s s i o n a c c o u n t i n g f o r i t s l e n g t h . C o n d i t i o n ( i i ) i s e a s y t o e s t a b l i s h , u s i n g s i m i l a r a r g u m e n t s . A n d

    t h e l a s t c o n d i t i o n f o l l o w s f r o m c o n s t r u c t i o n . ut

    T h e p r e v i o u s r e s u l t p r o v e s o n e h a l f o f t h e t h e o r e m o f K l e e n e ( t h e s e c o n d h a l f o f t h e p r o o f m a y b e

    f o u n d i n a n y s t a n d a r d t e x t b o o k o n a u t o m a t a o r f o r m a l l a n g u a g e t h e o r y ) .

    T h e o r e m 4 . 3 ( K l e e n e , 1 9 5 6 ) . A l a n g u a g e i s r e c o g n i z a b l e i f a n d o n l y i f i t i s r e g u l a r .

    W e d e n o t e b y E ( x ) t h e n o r m a l i z e d e x t e n d e d a u t o m a t o n c o n s t r u c t e d i n t h e p r o o f o f T h e o r e m 4 . 2

    f r o m t h e r e g u l a r e x p r e s s i o n x , a n d w e c a l l i t t h e r e g u l a r - e x p r e s s i o n - m a t c h i n g a u t o m a t o n o f x

    T o e v a l u a t e t h e t i m e c o m p l e x i t y o f t h e a b o v e c o n s t r u c t i o n , i t i s n e c e s s a r y t o g i v e s o m e h i n t s a b o u t

    t h e d a t a s t r u c t u r e s i n v o l v e d i n t h e r e p r e s e n t a t i o n o f r e g u l a r - e x p r e s s i o n - m a t c h i n g a u t o m a t a . D u e t o t h e

    c o n d i t i o n s s t a t e d i n T h e o r e m 4 . 2 , a s p e c i a l r e p r e s e n t a t i o n o f r e g u l a r - e x p r e s s i o n - m a t c h i n g a u t o m a t a i s

    p o s s i b l e p r o v i d i n g a n e c i e n t i m p l e m e n t a t i o n o f t h e c o n s t r u c t i o n f u n c t i o n . S t a t e s a r e s i m p l y i n d i c e s o n

    a n a r r a y t h a t s t o r e e d g e s ; e a c h c e l l o f t h e a r r a y h a s t o s t o r e a t m o s t t w o e d g e s w h o s e i n g o i n g s t a t e i s

    i t s i n d e x . I n d i c e s o f t h e i n i t i a l s t a t e a n d o f t h e t e r m i n a l s t a t e a r e s t o r e d s e p a r a t e l y . T h i s s h o w s t h a t t h e

    s p a c e r e q u i r e d t o s t o r e E ( x ) i s l i n e a r i n i t s n u m b e r o f s t a t e s , w h i c h i s l i n e a r i n t h e l e n g t h o f x a c c o r d i n g

    t o T h e o r e m 4 . 2 .

    H e n c e , e a c h o f t h e o p e r a t i o n s i n d u c e d b y + , o r

    c a n b e i m p l e m e n t e d t o w o r k i n c o n s t a n t t i m e .

    T h i s p r o v e s t h a t t h e t i m e s p e n t o n e a c h l e t t e r o f x i s c o n s t a n t . I n a d d i t i o n , t h e f u n c t i o n w h i c h b u i l d s

    t h e r e g u l a r - e x p r e s s i o n - m a t c h i n g a u t o m a t o n c o r r e s p o n d i n g t o a g i v e n r e g u l a r e x p r e s s i o n i s d r i v e n b y a

    p a r s e r o f r e g u l a r e x p r e s s i o n s . T h e n , i f p a r e n t h e s i z i n g i n x i s n o t r e d u n d a n t , t h e t i m e a n d t h e s p a c e

    n e e d e d f o r t h e c o n s t r u c t i o n o f E ( x ) i s l i n e a r i n t h e l e n g t h o f x

    W e h a v e e s t a b l i s h e d t h e f o l l o w i n g r e s u l t :

  • 8/3/2019 String Automata

    11/48

    A u t o m a t a f o r M a t c h i n g P a t t e r n s 1 1

    T h e o r e m 4 . 4 . L e t x b e a r e g u l a r e x p r e s s i o n . T h e s p a c e n e e d e d t o r e p r e s e n t E ( x ) i s O ( x ) . T h e c o m -

    p u t a t i o n o f t h e a u t o m a t o n i s p e r f o r m e d i n t i m e a n d s p a c e O ( x )

    4 . 3 S e a r c h i n g w i t h r e g u l a r - e x p r e s s i o n - m a t c h i n g a u t o m a t a

    T h e s e a r c h f o r e n d - p o s i t i o n s o f w o r d s i n L a n g ( x ) i s p e r f o r m e d w i t h a s i m u l a t i o n o f a d e t e r m i n i s t i c

    a u t o m a t o n r e c o g n i z i n g A

    L a n g ( x ) . I n d e e d , t h e d e t e r m i n i z a t i o n i s a v o i d e d b e c a u s e i t m a y l e a d t o a n

    a u t o m a t o n w i t h a n u m b e r o f s t a t e s w h i c h i s e x p o n e n t i a l i n t h e l e n g t h o f t h e r e g u l a r e x p r e s s i o n ( s e e

    S e c t i o n 4 . 4 ) . B u t t h e d e t e r m i n i z a t i o n v i a t h e s u b s e t c o n s t r u c t i o n i s j u s t s i m u l a t e d : a t a g i v e n t i m e , t h e

    a u t o m a t o n i s n o t i n a g i v e n s t a t e , b u t i n a s e t o f s t a t e s . T h i s s u b s e t i s r e c o m p u t e d w h e n e v e r n e c e s s a r y

    i n t h e e x e c u t i o n o f t h e s e a r c h .

    A s f o r t h e d e t e r m i n i z a t i o n o f a u t o m a t a w i t h " - t r a n s i t i o n s , t h e s e a r c h i n g p r o c e d u r e n e e d s t h e n o t i o n

    o f c l o s u r e o f a s e t o f s t a t e s : i f S i s a s e t o f s t a t e s , i t s c l o s u r e i s t h e s e t o f s t a t e s q s u c h t h a t t h e r e e x i s t s

    a p a t h l a b e l e d b y " f r o m a s t a t e o f S t o q . F r o m t h e c l o s u r e o f a s e t o f s t a t e s , i t i s p o s s i b l e t o c o m p u t e

    e e c t i v e l y t h e t r a n s i t i o n s i n d u c e d b y a n y i n p u t l e t t e r .

    T h e s i m u l a t i o n o f a r e g u l a r - e x p r e s s i o n - m a t c h i n g a u t o m a t o n c o n s i s t s i n r e p e a t i n g t h e t w o o p e r a t i o n s

    c l o s u r e a n d c o m p u t a t i o n o f t r a n s i t i o n s o n a s e t o f s t a t e s . T h e s e t w o o p e r a t i o n s a r e r e s p e c t i v e l y p e r f o r m e d

    b y f u n c t i o n s C l o s u r e a n d T r a n s i t i o n s o f F i g u r e s 4 . 3 a n d 4 . 4 . W i t h c a r e f u l i m p l e m e n t a t i o n , b a s e d

    o n s t a n d a r d m a n i p u l a t i o n o f s e t s a n d q u e u e s , t h e t i m e a n d t h e s p a c e r e q u i r e d t o c o m p u t e a c l o s u r e o r

    t h e t r a n s i t i o n s f r o m a c l o s u r e a r e l i n e a r i n t h e s i z e o f i n v o l v e d s e t s o f s t a t e s .

    C l o s u r e ( E S )

    1 R S

    2 # E m p t y Q u e u e

    3 f o r e a c h s t a t e p n S

    4 l o o p E n q u e u e ( # p )

    5 w h i l e n o t Q u e u e I s E m p t y ( # )

    6 l o o p p D e q u e u e ( # )

    7 f o r e a c h s t a t e q s u c h t h a t ( p ; " ; q ) i s i n E

    8 l o o p i f q i s n o t i n R

    9 t h e n R R + f q g

    1 0 E n q u e u e ( # q )

    1 1 r e t u r n R

    F i g . 4 . 3 . C o m p u t a t i o n o f t h e c l o s u r e o f a s e t S o f s t a t e s , w i t h r e s p e c t t o a s e t E o f e d g e s .

    T r a n s i t i o n s ( E ; S ; a )

    1 R ?

    2 f o r e a c h s t a t e p n S

    3 l o o p f o r e a c h s t a t e q s u c h t h a t ( p ; a ; q ) i s i n E

    4 l o o p R R + f q g

    5 r e t u r n R

    F i g . 4 . 4 . C o m p u t a t i o n o f t h e t r a n s i t i o n s b y a l e t t e r a f r o m s t a t e s o f a s e t S , w i t h r e s p e c t t o a s e t E o f e d g e s .

    A b a s i c u s e o f a n a u t o m a t o n c o n s i s t s i n t e s t i n g w h e t h e r i t r e c o g n i z e s s o m e g i v e n w o r d . T e s t i n g

    w h e t h e r y i s i n t h e l a n g u a g e d e s c r i b e d b y x i s i m p l e m e n t e d b y t h e a l g o r i t h m o f F i g u r e 4 . 5 . T h e n e x t

    p r o p o s i t i o n s t a t e s t h e c o m p l e x i t y o f s u c h a t e s t .

    P r o p o s i t i o n 4 . 1 . G i v e n a r e g u l a r e x p r e s s i o n x , t e s t i n g w h e t h e r a w o r d y b e l o n g s t o L a n g ( x ) c a n b e

    p e r f o r m e d i n t i m e O ( x y ) a n d s p a c e O ( x )

    P r o o f . T h e p r o o f i s g i v e n b y a l g o r i t h m T e s t e r o f F i g u r e 4 . 5 f o r w h i c h w e a n a l y z e t h e c o m p l e x i t y .

    A c c o r d i n g t o T h e o r e m 4 . 4 , t h e r e g u l a r - e x p r e s s i o n - m a t c h i n g a u t o m a t o n ( Q ; i ; f t g E ) o f x c a n b e

    b u i l t i n t i m e a n d s p a c e O ( x )

    E a c h c o m p u t a t i o n o f f u n c t i o n s C l o s u r e a n d o f T r a n s i t i o n s r e q u i r e s t i m e a n d s p a c e O ( c a r d ( Q ) )

    w h i c h i s O ( x ) f r o m T h e o r e m 4 . 2 . T h i s i s r e p e a t e d y t i m e s . T h i s g i v e s O ( x y ) t i m e . ut

  • 8/3/2019 String Automata

    12/48

    A u t o m a t a f o r M a t c h i n g P a t t e r n s 1 2

    R e g u l a r E x p r e s s i o n T e s t e r ( x y )

    1 b u i l t t h e r e g u l a r - e x p r e s s i o n - m a t c h i n g a u t o m a t o n ( Q ; i ; f t g E ) o f x

    2 C C l o s u r e ( E f i g )

    3 f o r l e t t e r a f r o m r s t t o l a s t l e t t e r o f y

    4 l o o p C C l o s u r e ( E T r a n s i t i o n s ( E ; C ; a ) )

    5 r e t u r n t 2 C

    F i g . 4 . 5 . A l g o r i t h m f o r t e s t i n g w h e t h e r a w o r d y b e l o n g s t o L a n g ( x ) x b e i n g a r e g u l a r e x p r e s s i o n .

    W e n o w c o m e b a c k t o o u r m a i n p r o b l e m . I t i s s l i g h t l y d i e r e n t t h a n t h e p r e v i o u s o n e , b e c a u s e t h e

    a n s w e r t o t h e t e s t h a s t o b e r e p o r t e d f o r e a c h f a c t o r o f y , a n d n o t o n l y o n y i t s e l f . B u t n o t r a n s f o r m a t i o n

    o f E ( x ) i s n e c e s s a r y . A m e r e t r a n s f o r m a t i o n o f t h e s e a r c h p h a s e o f t h e a l g o r i t h m i s s u c i e n t : a t e a c h

    i t e r a t i o n o f t h e c l o s u r e c o m p u t a t i o n , t h e i n i t i a l s t a t e i s i n t e g r a t e d t o t h e c u r r e n t s e t o f s t a t e s . D o i n g s o ,

    e a c h f a c t o r o f y i s t e s t e d . M o r e o v e r , t h e \ o c c u r r e n c e i f t 2 C " i n s t r u c t i o n i s d o n e a t e a c h s t a g e . T h e

    e n t i r e a l g o r i t h m i s g i v e n i n F i g u r e 4 . 6 . T h e f o l l o w i n g t h e o r e m e s t a b l i s h e d t h e c o m p l e x i t y o f t h e s e a r c h

    p h a s e o f t h e a l g o r i t h m .

    R e g u l a r E x p r e s s i o n M a t c h e r ( x y )

    1 b u i l t t h e r e g u l a r - e x p r e s s i o n - m a t c h i n g a u t o m a t o n ( Q ; i ; f t g E ) o f x

    2 C C l o s u r e ( E f i g )

    3 o c c u r r e n c e i f t 2 C

    4 f o r l e t t e r a f r o m r s t t o l a s t l e t t e r o f y

    5 l o o p C C l o s u r e ( E T r a n s i t i o n s ( E ; C ; a ) + f i g )

    6 o c c u r r e n c e i f t 2 C

    F i g . 4 . 6 . A l g o r i t h m f o r c o m p u t i n g p r e x e s o f a w o r d y t h a t b e l o n g t o A

    L a n g ( x ) x b e i n g a r e g u l a r e x p r e s s i o n .

    T h e o r e m 4 . 5 . L e t x b e a r e g u l a r e x p r e s s i o n a n d y b e a w o r d . F i n d i n g a l l e n d - p o s i t i o n s o f f a c t o r s o f

    y t h a t a r e r e c o g n i z e d b y E ( x ) c a n b e p e r f o r m e d i n t i m e O ( x y ) a n d s p a c e O ( x ) . T h e t i m e s p e n t o n

    e a c h l e t t e r o f y i s O ( x )

    P r o o f . S e e t h e p r o o f o f P r o p o s i t i o n 4 . 1 . T h e s e c o n d p a r t o f t h e s t a t e m e n t c o m e s f r o m t h a t f a c t : t h e

    t i m e s p e n t o n e a c h l e t t e r o f y i s l i n e a r i n t h e t i m e r e q u i r e d b y t h e c o m p u t a t i o n s o f f u n c t i o n s C l o s u r e

    a n d T r a n s i t i o n s ut

    4 . 4 T i m e - s p a c e t r a d e - o

    T h e r e g u l a r - e x p r e s s i o n - m a t c h i n g p r o b l e m f o r a r e g u l a r e x p r e s s i o n x a n d a w o r d y a d m i t s a s o l u t i o n

    b a s e d o n d e t e r m i n i s t i c a u t o m a t a . I t p r o c e e d s a s f o l l o w : b u i l d t h e a u t o m a t o n E ( x ) ; b u i l t a n e q u i v a l e n t

    d e t e r m i n i s t i c a u t o m a t o n ; s e a r c h w i t h t h e d e t e r m i n i s t i c a u t o m a t o n . T h e d r a w b a c k o f t h i s a p p r o a c h i s

    t h a t t h e d e t e r m i n i s t i c a u t o m a t o n c a n h a v e a n u m b e r o f s t a t e s e x p o n e n t i a l i n t h e l e n g t h o f x . T h i s i s

    t h e s i t u a t i o n , f o r e x a m p l e , w h e n

    x = a

    m 1 t i m e s

    z } | {

    ( a + b ) ( a + b )

    f o r s o m e m > 1 ; h e r e , t h e m i n i m a l d e t e r m i n i s t i c a u t o m a t o n r e c o g n i z i n g A

    L a n g ( x ) h a s e x a c t l y 2

    m

    s t a t e s s i n c e t h e r e c o g n i t i o n p r o c e s s h a s t o m e m o r i z e t h e l a s t m l e t t e r s r e a d f r o m t h e i n p u t w o r d y

    H o w e v e r , a l l s t a t e s o f t h e d e t e r m i n i s t i c a u t o m a t o n f o r A

    L a n g ( x ) a r e n o t n e c e s s a r i l y m e t d u r i n g t h e

    s e a r c h p h a s e . S o , a l a z y c o n s t r u c t i o n o f t h e d e t e r m i n i s t i c a u t o m a t o n d u r i n g t h e s e a r c h i s a p o s s i b l e

    c o m p r o m i s e f o r p r a c t i c a l p u r p o s e s .

    5 . M a t c h i n g n i t e s e t s o f w o r d s

    5 . 1 O u t l i n e

    P r o b l e m 5 . 1 . ( D i c t i o n a r y - m a t c h i n g p r o b l e m . ) G i v e n a n i t e s e t o f w o r d s X , t h e d i c t i o n a r y , p r e p r o c e s s

    i t i n o r d e r t o l o c a t e w o r d s o f X t h a t o c c u r i n a n y g i v e n w o r d y

  • 8/3/2019 String Automata

    13/48

    A u t o m a t a f o r M a t c h i n g P a t t e r n s 1 3

    T h e c l a s s i c a l s o l u t i o n t o t h i s p r o b l e m i s d u e t o A h o a n d C o r a s i c k . I t e s s e n t i a l l y c o n s i s t s i n a l i n e a r -

    s p a c e i m p l e m e n t a t i o n o f a c o m p l e t e d e t e r m i n i s t i c a u t o m a t o n r e c o g n i z i n g t h e l a n g u a g e A

    X . T h e i m -

    p l e m e n t a t i o n u s e s b o t h a d j a c e n c y l i s t s a n d a n a p p r o p r i a t e f a i l u r e f u n c t i o n .

    M a i n T h e o r e m 5 . 1 ( A h o a n d C o r a s i c k , 1 9 7 5 ) . T h e d i c t i o n a r y - m a t c h i n g p r o b l e m f o r X a n d y c a n

    b e a c h i e v e d i n t h e f o l l o w i n g t e r m s :

    { a p r e p r o c e s s i n g p h a s e o n X b u i l d i n g a n i m p l e m e n t a t i o n o f s i z e O ( X ) o f a n a u t o m a t o n r e c o g n i z i n g

    A

    X , p e r f o r m e d i n t i m e O ( X l o g c a r d ( A ) ) a n d O ( c a r d ( X ) ) e x t r a - s p a c e ;

    { a s e a r c h p h a s e e x e c u t i n g t h e a u t o m a t o n o n y p e r f o r m e d i n t i m e O ( y l o g c a r d ( A ) ) a n d c o n s t a n t

    e x t r a - s p a c e , t h e d e l a y b e i n g O ( X l o g c a r d ( A ) )

    I f w e a l l o w m o r e e x t r a - s p a c e , t h e a s y m p t o t i c t i m e c o m p l e x i t i e s c a n b e r e d u c e d . T h i s i s a c h i e v e d , f o r

    i n s t a n c e , b y u s i n g t e c h n i q u e s o f S e c t i o n 3 f o r r e p r e s e n t i n g d e t e r m i n i s t i c a u t o m a t a w i t h a s p a r s e m a t r i x ,

    a n d a s s u m i n g t h a t O ( X c a r d ( A ) ) s p a c e i s a v a i l a b l e . T h e t i m e c o m p l e x i t i e s o f t h e p r e p r o c e s s i n g a n d

    s e a r c h p h a s e s a r e r e s p e c t i v e l y r e d u c e d t o O ( X ) a n d O ( y ) , a n d t h e d e l a y t o O ( X ) . N e v e r t h e l e s s ,

    n o t i c e t h a t t h e t i m e s c o m p l e x i t i e s g i v e n i n t h e a b o v e t h e o r e m a r e s t i l l l i n e a r i n X o r y i f w e c o n s i d e r

    x e d a l p h a b e t s .

    T h e m e t h o d b e h i n d T h e o r e m 5 . 1 i s b a s e d o n a s p e c i c a u t o m a t o n r e c o g n i z i n g A

    X : i t s s t a t e s a r e

    t h e p r e x e s o f w o r d s i n X ( t h e i r n u m b e r i s n i t e a s X i s ) . T h e a u t o m a t o n i s n o t m i n i m a l i n t h e g e n e r a l

    c a s e . I t i s p r e s e n t e d i n S e c t i o n 5 . 2 , a n d i t s i m p l e m e n t a t i o n w i t h a f a i l u r e f u n c t i o n i s g i v e n i n S e c t i o n 5 . 3 .

    S e c t i o n 5 . 4 i s d e v o t e d t o t h e s e a r c h f o r X w i t h t h e a u t o m a t o n .

    5 . 2 D i c t i o n a r y - m a t c h i n g a u t o m a t a

    W e g i v e a c o m p l e t e d e t e r m i n i s t i c a u t o m a t o n t h a t r e c o g n i z e s A

    X . I n o r d e r t o f o r m a l i z e t h i s a u t o m a t o n ,

    w e i n t r o d u c e f o r e a c h l a n g u a g e U t h e m a p p i n g h

    U

    A

    ! P r e f ( U ) d e n e d f o r e a c h w o r d v b y

    h

    U

    ( v ) = t h e l o n g e s t s u x o f v t h a t b e l o n g s t o P r e f ( U )

    ( I n t h e w h o l e S e c t i o n 5 , U r e f e r s t o a n o r d i n a r y l a n g u a g e , a n d X r e f e r s t o a n i t e l a n g u a g e . )

    P r o p o s i t i o n 5 . 1 . L e t X b e a n i t e l a n g u a g e . T h e n t h e a u t o m a t o n

    P r e f ( X ) " P r e f ( X ) \ A

    X

    ( p ; a ; h

    X

    ( p a ) ) p 2 P r e f ( X ) a 2 A

    r e c o g n i z e s t h e l a n g u a g e A

    X . T h i s a u t o m a t o n i s d e t e r m i n i s t i c a n d c o m p l e t e .

    I n t h e f o l l o w i n g , w e d e n o t e b y D ( X ) t h e a u t o m a t o n o f P r o p o s i t i o n 5 . 1 a p p l i e d t o X , a n d w e c a l l i t

    t h e d i c t i o n a r y - m a t c h i n g a u t o m a t o n o f X

    T h e p r o o f o f P r o p o s i t i o n 5 . 1 r e l i e s o n t h e f o l l o w i n g r e s u l t .

    L e m m a 5 . 1 . L e t U A

    . T h e n

    ( i ) v 2 A

    U i h

    U

    ( v ) 2 A

    U , f o r e a c h v 2 A

    F u r t h e r m o r e , h

    U

    s a t i s e s t h e r e l a t i o n s :

    ( i i ) h

    U

    ( " ) = " ;

    ( i i i ) h

    U

    ( v a ) = h

    U

    ( h

    U

    ( v ) a ) , f o r e a c h ( v a ) 2 A

    A

    P r o o f . I f v 2 A

    U , t h e n v i s i n t h e f o r m w u w h e r e w 2 A

    a n d u 2 U ; b y d e n i t i o n o f h

    U

    u i s n e c e s s a r i l y

    a s u x o f h

    U

    ( v ) ; t h e r e f o r e h

    U

    ( v ) 2 A

    U . C o n v e r s e l y , i f h

    U

    ( v ) 2 A

    U , w e h a v e a l s o v 2 A

    U , b e c a u s e

    h

    U

    ( v ) i s a s u x o f v . W h i c h p r o v e s ( i ) .

    P r o p e r t y ( i i ) c l e a r l y h o l d s .

    I t r e m a i n s t o p r o v e ( i i i ) . B o t h w o r d s h

    U

    ( v a ) a n d h

    U

    ( v ) a a r e s u x e s o f v a , a n d t h e r e f o r e o n e o f t h e m

    i s a s u x o f t h e o t h e r . T h e n w e d i s t i n g u i s h t w o c a s e s a c c o r d i n g t o w h i c h w o r d i s a s u x o f t h e o t h e r .

    F i r s t c a s e : h

    U

    ( v ) a i s a p r o p e r s u x o f h

    U

    ( v a ) ( h e n c e h

    U

    ( v a ) 6= " ) . C o n s i d e r t h e w o r d w d e n e d b y

    w = h

    U

    ( v a ) a

    1

    . T h u s w e h a v e : h

    U

    ( v ) i s a p r o p e r s u x o f w w i s a s u x o f v , a n d s i n c e h

    U

    ( v a ) 2

  • 8/3/2019 String Automata

    14/48

    A u t o m a t a f o r M a t c h i n g P a t t e r n s 1 4

    P r e f ( U ) w 2 P r e f ( U ) . W h e n c e w i s a s u x o f v t h a t b e l o n g s t o P r e f ( U ) , b u t s t r i c t l y l o n g e s t t h a n

    h

    U

    ( v ) . T h i s c o n t r a d i c t s t h e m a x i m a l i t y o f h

    U

    ( v ) . S o t h i s c a s e i s i m p o s s i b l e .

    S e c o n d c a s e : h

    U

    ( v a ) i s a s u x o f h

    U

    ( v ) a . T h e n , h

    U

    ( v a ) i s a s u x o f h

    U

    ( h

    U

    ( v ) a ) . N o w , s i n c e h

    U

    ( v ) a

    i s a s u x o f v a h

    U

    ( h

    U

    ( v ) a ) i s a s u x o f h

    U

    ( v a ) . B o t h p r o p e r t i e s i m p l i e s h

    U

    ( v a ) = h

    U

    ( h

    U

    ( v ) a ) , a n d

    t h e e x p e c t e d r e s u l t f o l l o w s . ut

    P r o o f o f P r o p o s i t i o n 5 . 1 . L e t v 2 A

    . I t f o l l o w s f r o m p r o p e r t i e s ( i i ) a n d ( i i i ) o f L e m m a 5 . 1 t h a t

    h

    X

    ( v

    1

    v

    2

    v

    j 1

    ) v

    i

    h

    X

    ( v

    1

    v

    2

    v

    j

    )

    1 6 j 6 v

    i s a p a t h l a b e l e d b y v f r o m t h e i n i t i a l s t a t e " t o t h e s t a t e h

    X

    ( v )

    I f v 2 A

    X , w e g e t h

    X

    ( v ) 2 A

    X f r o m ( i ) o f L e m m a 5 . 1 ; w h i c h s h o w s t h a t h

    X

    ( v ) i s a t e r m i n a l s t a t e ,

    a n d n a l l y t h a t v i s r e c o g n i z e d b y t h e a u t o m a t o n .

    C o n v e r s e l y , i f v i s r e c o g n i z e d b y t h e a u t o m a t o n , w e h a v e h

    X

    ( v ) 2 A

    X b y d e n i t i o n o f t h e a u t o m a t o n .

    T h i s i m p l i e s t h a t v 2 A

    X f r o m ( i ) o f L e m m a 5 . 1 a g a i n . ut

    W e s h o w h o w t o i m p l e m e n t t h e a u t o m a t o n D ( X ) i n t h e n e x t s e c t i o n .

    5 . 3 L i n e a r d i c t i o n a r y - m a t c h i n g a u t o m a t a

    T h e a u t o m a t o n D ( X ) i s i m p l e m e n t e d w i t h a f a i l u r e f u n c t i o n . T h e a i m i s t o g e t a r e p r e s e n t a t i o n t h a t

    d o e s n o t d e p e n d o n t h e s i z e o f t h e a l p h a b e t .

    F o r e a c h l a n g u a g e U , l e t f

    U

    P r e f ( U ) ! P r e f ( U ) b e t h e f u n c t i o n d e n e d f o r e a c h n o n e m p t y w o r d u

    i n P r e f ( U ) b y

    f

    U

    ( u ) = t h e l o n g e s t p r o p e r s u x o f u t h a t b e l o n g s t o P r e f ( U )

    L e m m a 5 . 2 . L e t U A

    . F o r e a c h ( u a ) 2 P r e f ( U ) A , w e h a v e :

    h

    U

    ( u a ) =

    8

    f

    U

    ( v ) a , w e g e t t h a t v a

    1

    i s a p r o p e r s u x

    o f u b e l o n g i n g t o P r e f ( U ) ; t h e n v a

    1

    = f

    U

    ( u ) b e c a u s e o f t h e m a x i m a l i t y o f f

    U

    ( u ) . W h i c h a c h i e v e s t h e

    p r o o f . ut

    W e i n t r o d u c e f o r e a c h l a n g u a g e U t h e f u n c t i o n

    U

    P r e f ( U ) A ! P r e f ( U ) a s s o c i a t i n g w i t h e a c h

    ( u a ) 2 P r e f ( U ) A s u c h t h a t u a 2 P r e f ( U ) t h e w o r d u a . T h u s , w i t h c o n v e n t i o n s o f S e c t i o n 3 . 4 , w e

    h a v e :

    P r o p o s i t i o n 5 . 2 . F o r e a c h n i t e l a n g u a g e X , t h e c o u p l e (

    X

    f

    X

    ) r e p r e s e n t s t h e t r a n s i t i o n f u n c t i o n

    o f D ( X ) ; f u n c t i o n

    X

    i s a s u b t r a n s i t i o n f u n c t i o n a n d f u n c t i o n f

    x

    a f a i l u r e f u n c t i o n , a c c o r d i n g t o t h e

    t r a n s i t i o n f u n c t i o n o f D ( X )

    P r o o f . F o l l o w s f r o m L e m m a 5 . 2 . ut

    N o w , l e t u s o b s e r v e t h a t f u n c t i o n

    X

    i s e x a c t l y t h e t r a n s i t i o n f u n c t i o n o f t h e d e t e r m i n i s t i c a u t o m a t o n

    P r e f ( X ) ; " ; X ;

    ( p ; a ; p a ) p 2 P r e f ( X ) a 2 A ; p a 2 P r e f ( X )

    T h i s a u t o m a t o n r e c o g n i z e s t h e l a n g u a g e X , a n d i s c l a s s i c a l l y c a l l e d t h e t r i e o f X , a s a r e f e r e n c e t o

    \ i n f o r m a t i o n r e t r i e v a l " . I t i s b u i l t b y f u n c t i o n T r i e o f F i g u r e 5 . 1 .

    P r o p o s i t i o n 5 . 3 . F u n c t i o n T r i e a p p l i e d t o a n y n i t e l a n g u a g e X b u i l d s t h e t r i e o f X . I f t h e e d g e s o f

    t h e a u t o m a t o n a r e i m p l e m e n t e d v i a a d j a c e n c y l i s t s , t h e s i z e o f t h e t r i e i s O ( X ) , a n d t h e c o n s t r u c t i o n

    i s p e r f o r m e d i n t i m e O ( X l o g d ) w i t h i n c o n s t a n t e x t r a - s p a c e , d b e i n g t h e m a x i m u m d e g r e e o f s t a t e s .

  • 8/3/2019 String Automata

    15/48

    A u t o m a t a f o r M a t c h i n g P a t t e r n s 1 5

    T r i e ( X )

    l e t b e t h e t r a n s i t i o n f u n c t i o n o f ( Q ; i ; T ; E )

    1 ( Q ; T ; E ) ( ? ? ? )

    2 i S t a t e - C r e a t i o n

    3 f o r w o r d x f r o m r s t t o l a s t w o r d o f X

    4 l o o p t i

    5 f o r l e t t e r a f r o m r s t t o l a s t l e t t e r o f x

    6 l o o p q ( t a )

    7 i f q = n i l

    8 t h e n q S t a t e - C r e a t i o n

    9 E E + f ( t ; a ; q ) g

    1 0 t q

    1 1 T T + f t g

    1 2 r e t u r n ( Q ; i ; T ; E )

    F i g . 5 . 1 . C o n s t r u c t i o n o f t h e t r i e o f a n i t e s e t o f w o r d s X

    0

    1

    2

    3

    4 5 6

    7

    a

    b

    b

    a

    b b

    b

    F i g . 5 . 2 . T h e t r i e o f f a b b a b b b b g

    W h e n X = f a b b a b b b b g , t h e t r i e o f X i s a s d e p i c t e d i n F i g u r e 5 . 2 . T h i s e x a m p l e s h a l l b e c o n s i d e r e d

    t w i c e i n t h e f o l l o w i n g .

    T o a c h i e v e t h e g o a l o f i m p l e m e n t i n g D ( X ) i n l i n e a r s i z e , w e u s e P r o p o s i t i o n 5 . 2 . T h e n , i t r e m a i n s

    t o g i v e m e t h o d s f o r c o m p u t i n g f

    X

    a n d f o r m a r k i n g t h e s e t o f t e r m i n a l s t a t e s . T h i s c a n b e d o n e b y a

    b r e a d t h r s t s e a r c h o n t h e g r a p h u n d e r l y i n g t h e t r i e s t a r t i n g a t t h e i n i t i a l s t a t e , a s s h o w n b y t h e t w o

    f o l l o w i n g l e m m a s .

    L e m m a 5 . 3 . L e t U A

    . F o r e a c h ( u a ) 2 P r e f ( U ) A , w e h a v e :

    f

    U

    ( u a ) =

    h

    U

    ( f

    U

    ( u ) a ) i f u 6= "

    " o t h e r w i s e .

    P r o o f . S i m i l a r t o t h e p r o o f o f L e m m a 5 . 2 . ut

    L e m m a 5 . 4 . L e t U A

    . F o r e a c h u 2 P r e f ( U ) , w e h a v e :

    u 2 A

    U ( ) ( u 2 U ) o r ( u 6= " a n d f

    U

    ( u ) 2 A

    U )

    P r o o f . I t i s c l e a r l y s u c i e n t t o p r o v e t h a t

    u 2 ( A

    U ) n U = ) f

    U

    ( u ) 2 A

    U

    S o , l e t u 2 ( A

    U ) n U . T h e w o r d u i s i n t h e f o r m v w w h e r e v 2 A

    a n d w i s a p r o p e r s u x o f u

    b e l o n g i n g t o U . T h e n , b y d e n i t i o n o f f

    U

    w i s a s u x o f f

    U

    ( u ) . T h e r e f o r e f

    U

    ( u ) 2 A

    U . W h i c h e n d s

    t h e p r o o f . ut

    T h e c o m p l e t e f u n c t i o n c o n s t r u c t i n g t h e r e p r e s e n t a t i o n o f D ( X ) w i t h t h e s u b t r a n s i t i o n

    X

    a n d t h e

    f a i l u r e f u n c t i o n f

    X

    i s g i v e n i n F i g u r e 5 . 3 . L e t u s r e c a l l t h a t t h e t r a n s i t i o n f u n c t i o n o f D ( X ) i s a s s u m e d

    t o b e c o m p u t e d b y f u n c t i o n F a i l u r e F u n c t i o n - T r a n s i t i o n o f S e c t i o n 3 . 4 . T h e n e x t t h e o r e m s t a t e s

    t h e c o r r e c t n e s s o f t h e c o n s t r u c t i o n a n d i t s t i m e a n d s p a c e c o m p l e x i t i e s . W e c a l l t h i s r e p r e s e n t a t i o n

    o f D ( X ) t h e l i n e a r d i c t i o n a r y - m a t c h i n g a u t o m a t o n o f X . T h e t e r m \ l i n e a r " ( i n X i s u n d e r s t o o d ) i s

    s u i t a b l e i f w e w o r k w i t h a x e d a l p h a b e t , s i n c e d e g r e e s a r e u p p e r - b o u n d e d b y c a r d ( A )

  • 8/3/2019 String Automata

    16/48

    A u t o m a t a f o r M a t c h i n g P a t t e r n s 1 6

    L i n e a r D i c t i o n a r y M a t c h i n g A u t o m a t o n ( X )

    l e t b e t h e t r a n s i t i o n f u n c t i o n o f ( Q ; i ; T ; E )

    l e t b e t h e t r a n s i t i o n f u n c t i o n o f ( Q ; i ; T ; ( F ) )

    1 ( Q ; i ; T ; E ) T r i e ( X )

    2 F i n i l

    3 # E m p t y Q u e u e

    4 E n q u e u e ( # i )

    5 w h i l e n o t Q u e u e I s E m p t y ( # )

    6 l o o p p D e q u e u e ( # )

    7 f o r e a c h l e t t e r a s u c h t h a t ( p ; a ) 6= n i l

    8 l o o p q ( p ; a )

    9 F q ( F p a )

    1 0 i f F q ] i s i n T

    1 1 t h e n T T + f q g

    1 2 E n q u e u e ( # q )

    1 3 r e t u r n ( Q ; i ; T ; ( F ) )

    F i g . 5 . 3 . C o n s t r u c t i o n o f t h e l i n e a r d i c t i o n a r y - m a t c h i n g a u t o m a t o n o f a n i t e s e t o f w o r d s X

    T h e o r e m 5 . 2 . T h e l i n e a r d i c t i o n a r y - m a t c h i n g a u t o m a t o n o f a n y n i t e l a n g u a g e X i s b u i l t b y f u n c t i o n

    L i n e a r D i c t i o n a r y M a t c h i n g A u t o m a t o n . T h e s i z e o f t h i s r e p r e s e n t a t i o n o f D ( X ) i s O ( X ) . T h e

    c o n s t r u c t i o n i s p e r f o r m e d i n t i m e O ( X l o g d ) w i t h i n O ( c a r d ( X ) ) e x t r a - s p a c e , d b e i n g t h e m a x i m u m

    d e g r e e o f s t a t e s o f t h e t r i e o f X

    P r o o f . T h e c o r r e c t n e s s o f t h e f u n c t i o n a n d t h e o r d e r o f t h e s i z e o f t h e r e p r e s e n t a t i o n i s c o n s e c u t i v e t o

    P r o p o s i t i o n s 5 . 1 , 5 . 2 , a n d 5 . 3 , a n d L e m m a s 5 . 2 , 5 . 3 , a n d 5 . 4 . T h e e x t r a - s p a c e i s l i n e a r i n t h e s i z e o f t h e

    q u e u e # , w h i c h h a s a l w a y s l e s s t h a n c a r d ( X ) e l e m e n t s .

    I n o r d e r t o p r o v e t h e a n n o u n c e d t i m e c o m p l e x i t y , w e s h a l l s e e t h a t t h e l a s t t e s t o f t h e l o o p o f f u n c t i o n

    F a i l u r e F u n c t i o n - T r a n s i t i o n ( f o r c o m p u t i n g ( F p a ) ; s e e S e c t i o n 3 . 4 ) i s e x e c u t e d l e s s t h a n 2 X

    t i m e s . T o a v o i d a m b i g u i t y , t h e s t a t e v a r i a b l e p o f f u n c t i o n F a i l u r e F u n c t i o n - T r a n s i t i o n i s r e n a m e d

    r

    F i r s t . W e r e m a r k t h a t l e s s t e s t s a r e e x e c u t e d o n t h e t r i e t h a n i f t h e w o r d s o f X w e r e c o n s i d e r e d

    s e p a r a t e l y .

    S e c o n d . C o n s i d e r i n g s e p a r a t e l y e a c h w o r d x o f X , a n d a s s i m i l a t i n g v a r i a b l e s p a n d r w i t h t h e p r e x e s

    o f x t h e y r e p r e s e n t , t h e q u a n t i t y 2 p r g r o w s o f a t l e a s t o n e u n i t y b e t w e e n t w o c o n s e c u t i v e t e s t s

    \ ( r a ) = n i l " . W h e n x 6 1 , n o t e s t i s p e r f o r m e d . B u t w h e n x > 2 , t h i s q u a n t i t y i s e q u a l t o 2 b e f o r e

    t h e e x e c u t i o n o f t h e r s t t e s t ( p = 1 r = 0 ) , a n d i s l e s s t h a n 2 x 2 a f t e r t h e e x e c u t i o n o f t h e l a s t

    t e s t ( p = x 1 r > 0 ) ; w h i c h s h o w s t h a t l e s s t h a n 2 x 3 t e s t s a r e e x e c u t e d i n t h i s c a s e .

    T h i s p r o v e s t h e e x p e c t e d r e s u l t o n t h e n u m b e r o f t e s t s .

    N o w , s i n c e e a c h o f t h e s e t e s t s i s p e r f o r m e d i n t i m e O ( l o g d ) , t h e l o o p o f l i n e s 5 { 1 2 o f f u n c t i o n

    L i n e a r D i c t i o n a r y M a t c h i n g A u t o m a t o n i s p e r f o r m e d i n t i m e O ( X l o g d ) . T h i s i s a l s o t h e t i m e

    c o m p l e x i t y o f t h e w h o l e f u n c t i o n , s i n c e l i n e 1 i s a l s o p e r f o r m e d i n t i m e O ( X l o g d ) a c c o r d i n g t o

    P r o p o s i t i o n 5 . 3 . ut

    F i g u r e 5 . 4 d i s p l a y s t h e l i n e a r d i c t i o n a r y - m a t c h i n g a u t o m a t o n o f X w h e n X = f a b b a b b b b g . T h e

    f a i l u r e f u n c t i o n f

    X

    i s d e p i c t e d w i t h n o n - l a b e l e d d i s c o n t i n u o u s e d g e s .

    0

    1

    2

    3

    4 5 6

    7

    a

    b

    b

    a

    b b

    b

    F i g . 5 . 4 . T h e l i n e a r d i c t i o n a r y - m a t c h i n g a u t o m a t o n o f f a b b a b b b b g

  • 8/3/2019 String Automata

    17/48

    A u t o m a t a f o r M a t c h i n g P a t t e r n s 1 7

    T o b e c o m p l e t e , w e a d d t h a t f

    U

    c a n b e e x p r e s s e d i n d e p e n d e n t l y o f h

    U

    f o r a n y l a n g u a g e U

    L e m m a 5 . 5 . L e t U 2 A

    . F o r e a c h ( u a ) 2 P r e f ( U ) A , w e h a v e :

    f

    U

    ( u a ) =

    8

  • 8/3/2019 String Automata

    18/48

    A u t o m a t a f o r M a t c h i n g P a t t e r n s 1 8

    i t i s s e t t h a t

    f

    X

    ( p ) =

    8

  • 8/3/2019 String Automata

    19/48

    A u t o m a t a f o r M a t c h i n g P a t t e r n s 1 9

    W e j u s t h a v e t o h a r k b a c k t o t h e o r d e r o f t h e d e l a y f o r t h e a l g o r i t h m o f K n u t h , M o r r i s a n d P r a t t . I t

    i s p r o v e d t h a t t h e n u m b e r o f t i m e s t h e t r a n s i t i o n f u n c t i o n o f t h e t r i e o f f x g i s p e r f o r m e d o n a n y i n p u t

    l e t t e r c a n n o t e x c e e d b l o g

    ( x + 1 ) c w h e r e =

    1 +

    p

    5

    = 2 i s t h e g o l d e n r a t i o . T h i s u p p e r b o u n d i s

    a c o n s e q u e n c e o f a c o m b i n a t o r i a l p r o p e r t y o f w o r d s d u e t o F i n e a n d W i l f ( k n o w n a s t h e \ p e r i o d i c i t y

    l e m m a " ) . B u t i t i s c l o s e d t o t h e w o r s t - c a s e b o u n d , o b t a i n e d w h e n x i s a p r e x o f t h e i n n i t e F i b o n a c c i

    w o r d ( s e e C h a p t e r \ C o m b i n a t o r i c s o f w o r d s " ) .

    H o w e v e r , a s w e s h a l l s e e , i m p l e m e n t i n g M ( A

    x ) w i t h a d j a c e n c y l i s t s s o l v e s t h e s t r i n g - m a t c h i n g

    p r o b l e m w i t h t h e a d d i t i o n a l f e a t u r e o f h a v i n g a r e a l - t i m e s e a r c h p h a s e o n x e d a l p h a b e t s , i . e . w i t h a

    d e l a y b o u n d e d b y a c o n s t a n t .

    M a i n T h e o r e m 6 . 2 . T h e s t r i n g - m a t c h i n g p r o b l e m f o r x a n d y c a n b e a c h i e v e d i n t h e f o l l o w i n g t e r m s :

    { a p r e p r o c e s s i n g p h a s e o n x b u i l d i n g a n i m p l e m e n t a t i o n o f M ( A

    x ) o f s i z e O ( x ) , p e r f o r m e d i n t i m e

    O ( x ) a n d c o n s t a n t e x t r a - s p a c e ;

    { a s e a r c h p h a s e e x e c u t i n g t h e a u t o m a t o n o n y p e r f o r m e d i n t i m e O ( y ) a n d c o n s t a n t e x t r a - s p a c e , t h e

    d e l a y b e i n g O ( l o g m i n f 1 + b l o g

    2

    x c c a r d ( A ) g )

    U n d e r l y i n g t h e a b o v e r e s u l t a r e i n d e e d o p t i m a l b o u n d s o n t h e c o m p l e x i t y o f s t r i n g - m a t c h