p106 Carter

Embed Size (px)

Citation preview

  • 8/10/2019 p106 Carter

    1/7

    U n i v e r s a l C l a s s e s o f H a s h F u n c t i o n s

    ( e x t e n d e d a b s t r a c t)

    J . L a w r e n c e C a r t e r a n d M a r k N . W e g m a n

    I B M T h o m a s J . W a t s o n R e s e a r c h C e n t e r

    Y o r k t o w n H e i g h t s , N e w Y o r k 1 0 5 9 8

    A b s t r a c t :

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

    a n d r e t r i e v a l o n k e y s . T h e a l g o r i t h m m a k e s a ra n d o m c h o i c e o f h a s h f u n c t io n f r o m a

    s u i t a b l e c l a s s o f h a s h f u n c t i o n s . G i v e n a n y s e q u e n c e o f i n p u t s th e e x p e c t e d t i m e

    ( a v e r a g i n g o v e r a l l f u n c t i o n s i n t h e c l a s s ) t o s t o r e a n d r e t r i e v e e l e m e n t s i s l i n e a r in

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

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

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

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

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

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

    I n t r o d u c t i o n :

    O n e m a y v i e w d i f f e r e n t i n p u t s t o a p r o g r a m a s

    e l e m e n t s f r o m a c l as s o f p r o b l e m s . T h e a n s w e r

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

    t o t h e p r o b l e m . O r d i n a r i ly , w h e n o n e t a l k s a b o u t

    t h e a v e r a g e p e r f o r m a n c e o f a p r o g r a m , o n e a v e r -

    a g e s o v e r t h e c l as s o f p r o b l e m s t h e p r o g r a m c a n

    s o lv e . G i l l [ 2 ] , R a b i n [ 7 ] , S t ra s s e n a n d S o l o v a y [ 9 ]

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

    p r o b le m s . T h e y s u g g e st t h a t t h e p r o g r a m r a n d o m l y

    c h o o s e a n a l g o r i t h m f r o m t h e c la s s o f a l g o r i t h m s t o

    s o l v e t h e p r o b l e m . T h e y a r e a b l e t o b o u n d t h e a v -

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

    w o r s t c a s e i n p u t . T h i s a v e r a g e o n t h e w o r s t c a s e

    c a n b e b e t t e r t h a n t h e p e r f o r m a n c e o f a n y k n o w n

    s i n g l e a l g o r i t h m o n i t s w o r s t c a s e. S o m e o f t h e

    p r o b l e m s w h i c h t h i s a p p r o a c h o v e r c o m e s a re t h e

    f o l l o w i n g :

    1 ) C l a s s i c a l a n a l y s i s ( a v e r a g i n g o v e r t h e c l a s s o f

    i n p u ts ) m u s t m a k e a s s u m p t i o n s a b o u t th e d i s t r i b -

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

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

    2 ) A c o n s e q u e n c e o f ( 1 ) is t h a t o n e c a n n o t c l a s s i -

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

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

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

    3 ) I f t h e p r o g r a m i s p r e s e n t e d w i t h a w o r s t - c a s e

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

    p e r f o r m a n c e . H o w e v e r , if o n e h a d a c l a ss o f a l g o r -

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

    p a r t i c u la r a l g o r i t h m w a s r u n n i n g s l o w l y o n a g i v e n

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

    a l g o r i t h m .

    I n t h i s p a p e r , w e a p p l y t h e s e n o t i o n s t o h a s h -

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

    c l a s s o f h as h f u n c t i o n s b e u s e d . W e s h o w t h a t if

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

    a v e r a g e p e r f o r m a n c e o f t h e p r o g r a m o n

    any

    i n p u t

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

    w i t h k n o w l e d g e o f t h e i n p u t, w e r e u se d . W ~ p r e s -

    e n t s e v e r a l c la s s e s o f h a s h f u n c t i o n s w h i c h i n s u r e

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

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

    1 0 6

  • 8/10/2019 p106 Carter

    2/7

    c o m p e n s a t e f o r t h e p o o r p e r f o r m a n c e o f t h e a l g o r -

    i t h m w h e n a n u n l u c k y c h o i c e o f fu n c t i o n i s m a d e .

    A b r i e f o u t l i n e o f o u r p a p e r f o l l o w s . A f t e r i n -

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

    c l a s s o f f u n c t i o n s : u n i v e r s a l 2 . W e s h o w t h a t a n y

    c l a s s o f f u n c t i o n s t h a t i s u n iv e r s a l 2 h a s t h e p r o p e r t y

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

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

    p l e e v e n l y . W e t h e n e x h i b i t s e v e r a l u n i v e r s a l 2

    c l a s s e s o f fu n c t i o n s w h i c h c a n b e e v a l u a t e d e a s i ly .

    F i n a ll y w e g i v e s e v e r a l e x a m p l e s o f t h e u s e o f t h e s e

    f u n c t i o n s .

    N o t a t i o n :

    I f S i s a s e t , I S I w i l l d e n o t e t h e n u m b e r o f

    e l e m e n t s in S . I x ] m e a n s t h e l e a s t i n t e g e r > x .

    I f x a n d y a r e b it s t r in g s , t h e n x ( ~ y i s t h e e x c l u s i v e -

    o r o f x a n d y . Z w i l l r e p r e s e n t t h e i n t e g e r s m o d n .

    A l l h a s h f u n c t i o n s m a p a s e t A i n t o a s e t B . W e w i l l

    a l w a y s a s s u m e I A I > I B I . A i s s o m e t i m e s c a l le d

    t h e

    s e t o f p o s s i b l e k e y s , a n d B t h e s e t o f i n d ic e s . I f

    f i s a h a s h f u n c t i o n a n d x , y ~ A , w e l e t ~ r( x ,y ) b e 1 i f

    x p y a n d f (x ) = f ( y ), a n d 0 o t h e r w i s e . T h u s , 8 f ( x , y )

    i s 1 i f x a n d y a re d i s t in c t e l e m e n t s o f A w h i c h m a p

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

    8 , (x , y ) b y a s e t , w e s u m o v e r a l l t h e e l e m e n t s i n t h e

    s e t . T h u s , i f H i s a c o l l e c t i o n o f h a s h f u n c t i o n s , x ~ A

    a n d S e A t h e n 8 H (X ,S ) m e a n s

    Z ~ 8 f ( x , y ) .

    f~H y~S

    N o t i c e t h a t t h e o r d e r o f s u m m a t i o n d o e s n o t m a t t er .

    P r o p e r t i e s o f U n i v e r s a l C l a s s e s :

    L e t H b e a c l a s s o f f u n c t i o n s f r o m A t o B . W e

    s a y t h a t H is universal i f f o r a l l x , y i n A ,

    8 . ( x , y ) < I H I . T h a t is , H i s u n i v e r s a l 2 i f n o p a i r o f

    - I B I

    d i s t i n c t k e y s a re m a p p e d i n t o t h e s a m e i n d e x b y

    m o r e t h a n o n e I B I 'h o f t h e f u n c t i o n s . P r o p o s i t i o n 1

    s h o w s t h a t t h i s b o u n d o n 8 . ( x , y ) i s t i g h t w h e n

    I A I

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

    f o l l o w s a l m o s t i m m e d i a t e l y f ro m t h e d e f i n it i o n o f

    u n i v e r s a l 2 .

    P r o p o s i t i o n 1 : G i v e n a n y c o l l e c t io n H o f h a sh f u n c -

    t i o n s ( n o t n e c e s s a r i l y u n i v e r s a l 2 ) , t h e r e e x i s t s x , y e A

    s u c h t h a t

    I H I I H I

    8 . ( x , y )

    > -

    I B I I A I

    P r o o f ( s k e t c h ): L e t a = I A I a n d b = I B I . A c o u n -

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

    a 2

    (~f (A, A) > - - - a .

    - b

    T h u s , 8 H ( A ,A ) _> a 2 1 H l ( 1 / b - l / a ) . T h e r e f o r e , b y

    t h e p i g e o n h o l e p r i n c i p le , t h e r e e x i s t s x , y e A s u c h

    t ha t 8H (x , y ) > [ H I ( 1 / b - 1 / a ) . [ ]

    P r o p o s i t i o n 2 : L e t x b e a n y e l e m e n t o f A a n d S a n y

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

    f r o m a u n i v e r s a l 2 c l a s s o f f u n c t i o n s ( w i t h e q u a l

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

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

    I S I

    8, x,S), is _< - -

    I B I

    P r o o f :

    M e a n v a l u e o f 8 f (x , S )

    _ 1

    ~ ' ~

    8 f ( x , S )

    I H I feB

    = 1 ~ _ , 8 H ( x , y ) ( b y n o t a t i o n )

    I H I y ~ s

    1 ~ ~ I H I

    < ~ ~ * * ~ . q ~ - ( b y d e f . o f u n i v e r s a l 2 )

    _ I S l [ ]

    I B I

    I n a d d i t i o n t o b e i n g u s e f u l l a t e r , P r o p o s i t i o n 2

    h a s s o m e d i r e c t a p p l i c a t i o n s . F o r i n s t a n c e , a n o p t i -

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

    s c r i b e d in [ 8 ] . T h i s s y s t e m i s d e s i g n e d t o c h e c k i f

    a w o r d x i s a m e m b e r o f a s e t o f v a l i d w o r d s S .

    T h e s e t { f (y ) l y e S } i s s t o r e d i n m e m o r y . T o t e s t

    w h e t h e r x i s in S , a c h e c k i s m a d e t o s e e i f f ( x ) is i n

    t h e s t o r e d s e t . S i n c e f ( y ) i s g e n e r a l l y s h o r t e r t h a n y,

    a c o n s i d e r a b le a m o u n t o f s p a c e w a s s a v e d . H o w e v -

    e r , t h e r e i s a c h a n c e o f e r r o r ; i f f (x ) = f ( y ) a n d y e S ,

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

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

    w h e n f i s c h o s e n f r o m a c l a s s o f u n i v e r s a l 2 f 0 n c -

    t i o n s .

    1 0 7

  • 8/10/2019 p106 Carter

    3/7

    W e a r e i n te r e s t e d i n t h e c o s t o f u s i n g t h e s e

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

    a s e q u e n c e R o f r e q u e s t s ( i n s e r t io n s o r r e t r i e v a ls ) t o

    s o m e d a t a b a s e , a n d a h as h f u n c t i o n f , w e d e f i n e

    t h e c o s t o f R w i t h r e s p e c t t o f , C ( f, R ), t o b e t h e s u m

    o f t h e c o s t s o f t h e i n d i v i d u a l re q u e s t s . T h e c o s t o f

    a n i n d i v i d u a l r e q u e s t r e f e r r i n g t o a n e l e m e n t x i s o n e

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

    f o r w h i c h f ( x ) = f ( y ) .

    T h i s c o s t fu n c t i o n r e f l e c t s th e w o r s t c a s e c o s t

    o f i n s e r t i n g o r f i n d i n g e l e m e n t s i n a s t o r a g e a n d

    r e t r ie v a l s c h e m e i n w h i c h e a c h e l e m e n t o f B is a s -

    s o c i a t e d w i t h a l i n k e d l i st , a n d a n e l e m e n t x i s

    s t o r e d i n t h e l i s t a s s o c i a t e d w i t h f ( x ) ( s e e [ 1 ] , p a g e

    1 1 1 - 1 1 3 . ) O t h e r c o l l is i o n r e s o l u t i o n s c h e m e s

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

    t h e m . F o r e x a m p l e , if t h e k e y s w i t h t h e s a m e i n d e x

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

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

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

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

    h a s h f u n c t i o n s w i t h t h e l i n k e d l is t m e t h o d f o r r e -

    s o l v i n g c o l l i s i o n s .

    P r o p o s i t i o n 3 : L e t R b e a s e q u e n c e o f r r e q u e s t s

    w h i c h i n c l u d e s k i n s e r t i o n s . S u p p o s e H i s a

    u n i v e r s a l 2 c l a s s o f h a s h f u n c t i o n s . T h e n i f w e

    c h o o s e f a t ra n d o m f r o m H , th e e x p e c t e d c o s t

    k

    C ( f , R ) i s _< r (1 + ~ -~ - ) .

    P r o o f : T h e e x p e c t e d c o s t o f R is t h e s u m o f t h e

    e x p e c t e d c o s t s o f t h e i n d iv i d u a l r e q u e s t s . P r o p o s i -

    t i o n 2 a n d t h e d e f i n i t i o n o f c o s t t e l l u s t h a t a n i n d i -

    v i d u a l r e q u e s t h a s e x p e c t e d c o s t n o g r e a t e r th a n

    k

    1 + - - [ ]

    I B I

    A s p e c i a l c a s e o f t h i s p r o p o s i t i o n i s t h a t i f k i s

    r o u g h l y t h e s i z e o f B t h e n t h e e x p e c t e d c o s t i s 2r .

    N o t i c e t h a t t h i s l in e a r b o u n d h o l d s f o r a n y s e q u e n c e

    o f r e q u e s t s , n o t j u s t f o r t h e a v e r a g e r e q u e s t . F o r

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

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

    b e c l ~ s e n a p p r o p r ia t e l y . I f t h e r e is n o k n o w n u p p e r

    b o u n d i t i s p o s s i b l e t o d y n a m i c a l l y c h o o s e a s iz e ,

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

    R e h a s h i n g c a n b e d o n e i n l in e a r ti m e , a n d e v e n i n

    r e a l - t i m e .

    W e c a n s h o w t h a t t h e e x p e c t e d c o s t ( a v e r a g in g

    o v e r t h e h a s h f u n c t i o n s ) o f a n y r e q u e s t i s v i rt u a l l y

    t h e s a m e a s th e e x p e c t e d c o s t ( a v e ra g i n g o v e r th e

    p o s s i b l e r e q u e s t s ) o f a n y s i n g l e ha s h f u n c t i o n w h e n

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

    h a v e b e e n m a d e . T h e r e a s o n i s a s f o l l o w s : L e t

    a = I A I a n d b = I B I . T h e c o u n t i n g a r g u m e n t

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

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

    t h e n t h e e x p e c t a t i o n o f 8 f (x , y ) i s > ( 1 / b - l / a ) . I t

    f o l l o w s t h a t t h e e x p e c t a t i o n o f 8 f (x , S )

    i s > I S a ( 1 / b - l / a ) , w h e r e S i s t h e r a n d o m s u b -

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

    t h e r . o s t o f t h e r e q u e s t i s a t l e a s t

    1 + I S I ( 1 / b - l / a ) . W h e n A i s m u c h l a r g e r t h a n

    B ( w h ic h w i l l b e t h e c a s e i n m o s t a p p l i c a t i o n s o f

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

    r e q u e s t c i t e d i n th e p r o o f o f P r o p o s i t i o n 3 .

    I t i s a l s o p o s s i b l e t o b o u n d t h e p r o b a b i l i t y t h a t

    g i v e n a s e q u e n c e o f r e q u e s t s R , t h e p e r f o r m a n c e o f

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

    e r a b l e o n R. S i n c e w e k n o w t h a t C ( f , R ) m u s t b e a t

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

    s i z e o f I B I , t h e p r o b a b i l i t y t h a t C ( f , R ) > t . r is l e s s

    t h a n 1 / ( t - 1 ) . F o r s o m e c la s s e s o f h a s h f u n c t i o n s

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

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

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

    f u n c t i o n o n a p a r t ic u l a r R . T h i s a l l o w s u s to g e t a

    m u c h b e t t e r e s t i m a t e o f t h e p r o b a b i l i t y t h a t C ( f, R )

    w i l l b e l a r g e .

    S o m e u n i v e r s a l 2 c l a s s e s :

    T h e f i r s t c l a s s o f u n i v e r s a l 2 h a s h f u n c t i o n s w e

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

    1 0 8

  • 8/10/2019 p106 Carter

    4/7

    s t r i n g s w h i c h r e p r e s e n t t h e k e y s c a n c o n v e n i e n t l y

    b e m u l t i p l i e d b y t h e c o m p u t e r .

    S u p p o s e A = { O , 1 . .. .. a - l } a n d B = { O , 1 . . .. . b - l } .

    L e t p b e a p r i m e w i t h p >_ a. L e t g b e a n y f u n c t i o n

    f r o m Z p t o B w h i c h , a s c l o s e l y a s p o s s i b l e , m a p s t h e

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

    B . F o r m a l l y , w e r e q u i r e

    I { y ~ Z p I g y ) = z } I < [ - ~ ] f o r a ll z ~ B . A

    n a t u r a l c h o i c e f o r g is t h e r e s i d u e m o d u l o b . W h e n

    b = 2 k f o r s o m e k , t h i s a m o u n t s t o t a k i n g t h e l a s t k

    b i t s i n t h e b i n a r y r e p r e s e n t a t i o n o f y .

    L e t m a n d n b e e l e m e n t s o f Z p w i t h m p O . W e

    d e f i n e h m , o : A -- , Z p b y h ( x ) = ( m x + n ) m o d p . N o w

    de f i ne f m .n (X ) = g(hm , ,(X ) ). T he c las s H i s t he s e t

    { f~ ,n I m , n e Z p m # 0 } . I f d e s i r e d , p c a n b e c h o s e n

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

    d i v i s i o n .

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

    t h i s c l a s s i s u n i v e r s a l 2 .

    L e m m a : W h e n H is d e f i n e d a s a b o v e , th e n f o r a n y

    x , y ~ A w i t h x # y , 8 H (x , y) e q u a ls t h e n u m b e r o f o r -

    d e r e d p a i r s ( r , s ) w i t h r , s ~ Z p , r ~ s a n d g ( r ) = g ( s ) .

    P r o o f : T h e r e is a n a t u r a l c o r r e s p o n d e n c e b e t w e e n

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

    r , s e Zp a n d r ~ s . S p e c i f ic a l l y , w e i d e n t i f y t h e f u n c -

    t i on hm , n by t he o rd e re d pa i r ( hm , n(X) ,hm , n (y )) . S inc e

    m ~ O , h m , n ( X) ~ h rn , n (y ) . T h i s c o r r e s p o n d e n c e i s o n e -

    t o - o n e a n d o n t o s i n c e t h e l in e a r e q u a t i o n s

    x m + n ~ r ( m o d p ) a n d y m + n - - s ( m o d p ) h a v e a u n i -

    q u e s o l u t i o n f o r m a n d n in t h e f i e l d Z p .

    I f ( r , s ) i s t he pa i r ( hm , n (X ) , hm , n (y ) ) , t hen

    f m , n (X )= fm , n (y) if and on l y i f g ( r )=g (s ) . T hus , 8H (x , y )

    i s t h e n u m b e r o f s u c h p a i r s . [ ]

    P r o p o s i t i o n 4 : T h e c l a s s H d e f i n e d a b o v e i s

    un i v e rs a l 2 .

    P r o o f : L e t n b e t h e n u m b e r o f e l e m e n t s i n

    [ t e Z p I g ( t) = i } . T h e r e s t r i c t i o n o n g is t h a t f o r

    e a c h i, n i < I - ~ ] . S i n c e p a n d b a r e i n t e g e r s , t h i s

    i m p l i e s t h a t n < p - 1 + 1 . T h u s , f o r a g i v e n r , t h e

    - b

    n u m b e r o f c h o i c e s f o r s su c h t h a t r # s b u t g ( r) = g (s )

    i s < P - - I . S i n c e t h e r e a r e p c h o i c e s f o r r ,

    b

    P p - 1 > t h e n u m b e r o f o r d e r e d p a ir s (r ,s ) s a t i s f y -

    b

    i n g t h e c o n d i t i o n i n t h e l e m m a = 8 H ( x , y ). R e c a l l i n g

    t h a t f o r x = y , 8 H ( x , y ) = O , t h i s s h o w s t h a t H i s

    un i v e rs a l 2 . I - I

    F r e q u e n t l y , a l g o r i t h m s a r e a n a l y z e d m a k i n g t h e

    a s s u m p t i o n t h a t m u l t i p l i c a t i o n ta k e s u n i t t i m e . T h e

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

    t h e a lg o r i t h m . T h i s m o d e l is a p p r o p r i a t e w h e n t h e r e

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

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

    u n i v e r s a l 2 c l a s s o f h a s h f u n c t i o n s i s u s e d , t h e n t h e

    n u m b e r o f m e m o r y r e f e r e n c e s p e r r e q u e s t c a n b e

    b o u n d e d w h e n a v e r a g e d o v e r a ll f u n c t i o n s i n t h e

    c l a s s a s i n P r o p o s i t i o n 3 ( a s s u m i n g k i s n o t u n -

    b o u n d e d w i t h r e s p e c t t o J B I . ) T h e r e f o r e t h e m o d e l

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

    c l a ss g i v e n a b o v e m a y b e a p p l i e d i n c o n s t a n t t i m e .

    T h u s i n t h is m o d e l , f o r a n y s e q u e n c e o f r e q u e s ts ,

    t h e a l g o r i t h m t a k e s a v e r a g e t i m e l i n e a r in th e n u m -

    b e r o f r e q u e s t s .

    I t m a y s e e m t h a t t h e a d d i t i o n o f n i n t h e c l a s s

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

    T h i s is o n l y p a r t l y t r o e . S u p p o s e f o r m ~ Z p w e d e -

    f i ne hm (X) = (m x ) ro od p , and as be f o re de f i n e fm (X)

    as g (hm (X) ) . Le t H = { f rn I m eZ p m ~O } . I t c an

    b e s h o w n t h a t t h i s c l as s o f f u n c t i o n s c o m e s w i t h i n a

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

    ^ I H I

    a n d y , 8 H ( x , y)

  • 8/10/2019 p106 Carter

    5/7

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

    t o b e m u l t i p l i e d u =~ in g a s i n g l e m a c h i n e i n s t r u c t i o n .

    H o w e v e r , t h e n e x t p r o p o s i t i o n g i v e s a m e t h o d o f

    e x t e n d i n g a c la s s o f f u n c t i o n s f o r l o n g k e y s .

    P r o p o s i ti o n 4 : S u p p o s e

    I BI

    i s a p o w e r o f t w o a n d

    H is a c l a s s o f f u n c t i o n s f r o m A t o B w i t h t h e p r o p -

    e r t y t h a t f o r e a c h i ~ B ,

    I { f ~ H I f ( x ) ( ~ f ( y ) = i } I - ] H I R e c a l l t h a t

    I B I

    Q is e x c l u s i v e - o r . T h e n w e ca n d e f i n e a

    u n i v e r s a l 2 c l a s s o f h as h f u n c t i o n f r o m A x A t o B a s

    f o l l ow s . F o r f , geH , d e f i n e h f. g (( x ,y ) ) = f ( x ) (~ g (y ) .

    T h e n t h i s n e w c o l l e c t i o n o f h a s h f u n c t i o n s

    J = { hf .Q ] f , g ~ H } i s u n i v e r s a l 2 a n d a l s o s a t i s f i e s

    t h e c o n d i t i o n o f t h i s p r o p o s i t i o n .

    T h e p r o o f i s q u i t e s i m i l a r t o t h e p r o o f o f P r o p -

    o s i t i o n 6, a n d th e r e f o r e o m i t t e d . P r o p o s i t i o n 5 c a n

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

    a r b i t r a r il y l o n g k ey s . I f t h e f u n c t i o n s i n H c a n b e

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

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

    l e n g t h o f t h e k e y .

    T h e p r o p o s i t i o n d o e s n o t q u i t e a p p l y t o t h e

    u n i v e r s a l 2 c l a s s o f f u n c t i o n s g i v e n e a r l i e r b o t h b e -

    c a u s e H i s n o t a p o w e r o f 2 ( s o I H I / I B I c a n n o t b e

    a n i n te g e r ) a n d b e c a u s e th e n u m b e r o f f u n c t i o n s f o r

    w h ic h f ( x ) (~ f ( y ) = 0 , i . e . 6H (x , y ), i s ac t ua l l y l es s

    t h a n I H I / I B I . B o t h o f t h e s e d i f fe r e n c e s a d d s m a l l

    f a c t o r s t o 8 j ( x , y ) w h i c h b a r e l y p r e v e n t J f r o m b e i n g

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

    s m a l l f a c t o r s d e c r e a s e a s y m p t o t i c a l l y t o 0 a s p i s

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

    pe r .

    T h e f o l l o w i n g i s a c l a s s o f fu n c t i o n s w h i c h d o

    n o t r e q u i r e m u l t i p l ic a t i o n , w h i c h m a y b e b e t t e r f o r

    m a n y a p p l ic a t i o n s, S u p p o s e A c an b e v i e w e d a s

    t h e s e t o f i - d i g ; t n u m b e r s w r i t t e n i n b a s e a , a n d B

    a s t h e s e t o f b i n a r y n u m b e r s o f l e n g t h j . T h e n I A I

    = a ' a n d I B I = 2 . L e t M b e t h e c l a s s o f a r r a y s o f

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

    s t r i n g s o f

    l e n g t h j. F o r m ~ M , l e t m (k ) b e t h e b i t s t ri n g w h i c h

    i s t h e k h e l e m e n t o f m , a n d f o r x t A , l e t x k b e t h e k h

    d i g i t o f x w r i t t e n i n b a s e a . W e d e f i n e

    f ro (x ) - - m (x , + l ) O m (x l+x 2 +2) (~ . . . (~) m ( ~ x k +k ) .

    k l

    T h e c l as s H is th e s e t { f . I m e M } .

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

    p r o g r a m f o r a p p l y i n g a f u n c ti o n t o a n i n p u t x .

    d c l m ( i a ) b i t ( j ) i n i t ( r a n d o m ) ;

    d c l x ( i) d i g i t s b a s e a ;

    d c l v a l u e b i t ( j ) ;

    d i s p : = 0 ;

    v a l u e : = 0 ;

    f o r k : = 1 t o i d o b e g i n

    d i s p : = d i s p + x ( k ) + 1 ;

    v a l u e : = v a l u e (~ ) m ( d i s p ) ;

    e n d ;

    r e t u r n ( v a l u e ) ;

    P r o p o s i t i o n 6 : T h e

    c l a s s

    H d e f i n e d a b o v e is

    u n i v e r s a l 2 .

    P r o o f : F o r x a n d y i n A , s u p p o s e fm (X ) is t h e

    e x c l u s i v e - o r o f t h e r o w s r 1 , r 2 .. . . r s o f m , a n d f ro (Y )

    i s r s . l ( ~ . . . ( ~ ) r t . N o t i c e t h a t f ro ( x ) = f ro (Y ) i f a n d o n l y

    i f r l( ~ ) . . .( ~ ) r t = 0 . A s s u m i n g x ~ y , t h e r e w i l l b e

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

    on l y one o f f m (X) o r f m (y ) . T h en f ro ( x ) w i l l equ a l fr o ( y )

    i f a n d o n l y i f r k is t h e e x c l u s i v e - o r o f t h e o t h e r r i ' s .

    S i n c e t h e r e a r e 2 = B p o s s i b i l i t i e s fo r t h a t r o w , x

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

    t i o n s f m T h u s , t h e c l a s s o f a l l f rn 'S i s u n i v e r s a l 2 .

    [ ]

    F o r a g i v e n B , e a c h f u n c t i o n i n H ta k e s t i m e

    l i n e a r i n t h e l e n g t h o f t h e k e y . I n a d d i t i o n , w e c a n

    m o r e a c c u r a t e l y d e s c r i b e th e d i s t r i b u t i o n o f c o s t s o f

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

    f u n c t io n s . F o r i n st a n c e, M a r k o w s k y [ 6 ] h a s s h o w n

    t h a t f o r a n y s e q u e n c e R o f r r e q u e s t s w i t h k i n s e r -

    t i o n s , a n d a n y p o s i t i v e t , t h e p r o b a b i l i t y t h a t

    k a n d

    C ( f , R ) - - r ( l + ) > r - t i s l e s s t h a n

    tZ l B I

    7 k

    a ls o le s s t h a n - -

    t 3 i B ]

    i i 0

  • 8/10/2019 p106 Carter

    6/7

    i m p o r t a n c e :

    P r o g r a m m e r s s o m e t i m e s s p e n d a c o n s i d e r a b le

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

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

    b e a c h i e v e d ( [ 5 ] , p . 5 0 8 - 5 1 3 ) . T h i s m a y b e d i f f i c u lt

    b e c a u s e i t i s n e c e s s a r y t h a t t h e e x p e c t e d i n p u t s e t

    n o t b e b i a s e d i n s u c h a w a y a s t o m a k e t l~ e h a s h

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

    o f a c l a s s o f u n i v e r s a l 2 f u n c t i o n s i s t h a t w e k n o w

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

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

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

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

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

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

    a n c e a v e r a g e d o v e r a l l ru n s .

    T h e t h e o r e t i c a l i m p o r t a n c e i s t h a t i t a l l o w s o n e

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

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

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

    m i g h t b i a s t h e i n f o r m a t i o n b e i n g s t o r e d a n d r e -

    t r ie v e d t o w a r d s t h o s e c a s e s t h a t a r e d i s t ri b u t e d

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

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

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

    p l a n e, g iv e n t h e c o o r d i n a t e s o f t h e p o i n ts [ 7 ] . T h i s

    a l g o r i th m i n v o lv e s m a k i n g a r a n d o m c h o i c e o f

    p o i n t s , a n d it u se s h a s h in g . I f o n e a ls o r a n d o m l y

    c h o o s e s t h e h a s h f u n c t i o n f r o m a u n i v e r s a l 2 c l a s s ,

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

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

    I n [ 3 ] a n d [ 4 ] a n a l g o r i t h m i s s u g g e s t e d f o r

    m u l t i p l y i n g s p a rs e p o l y n o m i a l s , u s in g h a s h i n g . W e

    c a n s t r e n g t h e n t h e r e s u lt s o f t h e s e p a p e r s . L e t t w o

    p o l y n o m i a l s , P a n d Q , h a v e n a n d m n o n - z e r o t e r m s

    r e s p e c t i v e l y . I f m u l t i p l i e s a n d a C d s a r e v i e w e d a s

    t a k in g c o n s t a n t t i m e , t h e n g i v e n a n y t w o p o l y n o m i -

    a l s P a n d O ., w e c a n m u l t i p l y t h e m i n a v e r a g e t i m e

    O ( n ' m ) . L e t C P 1 , C P 2 . . .. C P n b e t h e c o e f f i c i e n t s o f

    t h e n t e r m s o f P . L e t E P 1 , E P 2 . . .. E Pn b e t h e e x p o -

    ne nts o f t h o s e t e rm s . L e t C Q a n d E Q s t a n d f o r t h e

    s a m e q u a n t i t i e s o f Q . T h e f o l l o w i n g a l g o r i t h m w i ll

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

    S t o r e a n d R e t r i e v e a re i m p l e m e n t e d u s i n g a

    u n i v e r s a l 2 c la s s o f h a s h f u n c t i o n s . T h e i r f i rs t a r g u -

    m e n t i s a k e y , a n d t h e s e c o n d i s t h e v a l u e s t o r e d o r

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

    f o r a g i v e n k e y , a r e t r i e v e w i l l h a v e a z e r o v a l u e .

    B e g i n

    C h o o s e a h a s h f u n c t i o n ;

    F o r i : = 1 t o n do

    F o r j := 1 t o m d o

    B e g i n

    C o e f f i c i e n t : = C P * C Q j ;

    R e t r i e v e ( E P + E Q j , k ) ;

    S t o r e ( E P + E Q i , C o e f f i c i e n t + k ) ;

    E n d ;

    P r i nt a l l k e y s a n d v a l u e s w h i c h h a v e b e e n s t o r e d ;

    E n d ;

    S i n c e a d d i t i o n a n d m u l t i p l ic a t i o n a r e v i e w e d a s

    t a k i n g c o n s t a n t t i m e , t h e f i rs t c l a s s o f fu n c t i o n s w e

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

    F u t u r e r e s e a r c h :

    T h e r e a r e a n u m b e r o f a r e a s w h i c h c a n b e i n -

    v e s t i g a t e d , s u c h a s :

    1 ) I m p r o v e t h e b o u n d s c i t e d h e r e o n t h e p r o b a b i l i t y

    t h a t a p a r ti c u l a r f u n c t i o n f r o m t h e t a b le l o o k - u p

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

    2 ) S u p p o s e t h e s t o r e a n d r e t r i e v e a l g o r i t h m i s

    c h a n g e d s o t h a t t h e l a s t e l e m e n t r e t r i e v e d is m o v e d

    t o t h e fi r s t p o s i t i o n o n t h e l is t . C a n a s m a l l e r b o u n d

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

    s p e c i f i e d v a l u e b e d e r i v e d ?

    3 ) E x t e n d t h e a n a l y s i s t o o t h e r s t o r a g e a n d r e t r ie v a l

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

    h a s h i n g a n d o p e n a d d r e s s i n g .

    4 ) G e n e r a l i z e t h e d e f i n i t i o n o f u n i v e r s a l 2 t o

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

    f u n c t i o n s o n a n y c o l l e c t i o n o f n e l e m e n t s o f A . D e -

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

    i i i

  • 8/10/2019 p106 Carter

    7/7

    a s t ronge r as s um pt i on . (One de f i n i t i on o f un i v e rsa lk

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

    k - e l e m e n t s e t m a p p i n g i n t o a g i v e n e l e m e n t o f B

    w ou l d be b i nom i a l l y d i s t r ibu t ed . )

    5 ) W hen s hou l d~ one dec i de t ha t a pa r t i c u la r f unc t i on

    i s a poo r c ho i c e and i t w ou l d be w or t h t he e f f o r t t o

    c hoos e a new f unc t i on and rehas h?

    A c k n o w l e d g e m e n t s :

    W e w ou l d l i ke to t hank A s hok C h andra f o r he l p i ng

    s ugges t and f o rm u l a t e t he p rob l em ; D av e G l i c k m an

    f o r s u g g e s t in g w e e x a m i n e th e t a b l e l o o k - u p t e c h -

    n i que ; H an i a Ga j ew s k a and D av i d S m i t h f o r s ug -

    ges t i ng rev i s i ons i n an ea r l i e r m anus c r i p t ; W a l t e r

    R os enbaum f o r d i s c us s ions abou t a p rac t i c a l us e o f

    t h i s w o rk ; and George M ark ow s k y f o r he l p i n un -

    de rs t and i ng t he d i s t r ibu t i on o f pe r f o rm anc e o f t he

    c l as s ob t a i ned by ; t ab l e l ook -up .

    References:

    [ 1 ]

    [ 2 ]

    Aho, A .V . , Hop c rof t , J .E . , and U l lma n, J .D . ,

    The Design and Analysis o f Computer Algor-

    ithms Addison-Wesley, Reading, Mass.

    (1974) .

    G i l l, J .T . , I I I , C om pu t a t i on a l C om p l ex i t y o f

    P robab i l i s t ic Tu r i ng M ac h i nes , Proceedings of

    the Sixth AC ~f Symposium on the Theory of

    [ 3 ]

    [ 4 ]

    [ 5 ]

    [ 6 ]

    [ 7 ]

    [ 8 ]

    [ 9 ]

    Comput ing M ay , 1976 , S ea t t le , W as h i ng t on ,

    p . 91 -95 .

    Goto, E . , and Ka nada, Y . , Hashing Lem mas

    o n T i m e C o m p l e x i t ie s w i t h A p p l i c a ti o n s t o

    F o r m u l a M a n i p u l a t i o n , Proceedings o f the

    1976 ACM Symposium on Symbolic and Alge-

    braic Computation August, 1976, Yorktown

    H e i gh t s , N ew Y ork , p . 14 9 -15 3 .

    Gus tavson, F . , and Yun, D .Y .Y . , A r i thmet ic

    C o m p l e x i t y o f U n o r d e r e d o r S p a r se

    P o l y n o m i a l s Proceedings of the 1976 A CM

    Symposium on Symbolic and Algebraic Computa-

    tion

    A ugus t , 1976 , Y o rk t ow n H e i gh t s , N ew

    Y o r k, p . 1 5 4 - 1 5 9 .

    Knuth, D.E. , Sort ing and Searching

    A dd i s on -W es l ey , R ead i ng , M as s . (1973 ) .

    M ark ow s k y , G . , p r i v a t e c om m un i c a t i on .

    Rabin, M.O., Probabili stic algori thms,

    Proceedings of Symposium on New Directions and

    Recent Results in Algorithms and Complexity

    1976 - to appear).

    R os enbaum , W . S . , and H i l li a rd , J . J ,, M u l t i -

    f on t OC R pos t p roc es s i ng s y s t em . I B M J ou r -

    na l

    of Research and Development

    (Ju ly 1975) .

    S t rassen, V . , and Solo vay , R . , A fas t

    M o n t ~ - C a r l o t e s t f o r p r i m a li ty , SlAM Journa l

    on Com put ing ( to appear) .

    1 1 2