Upload
junty
View
225
Download
0
Embed Size (px)
Citation preview
8/7/2019 Worst Case Efficient Priority Queues
1/7
W o r s t - C a s e E c i e n t P r i o r i t y Q u e u e s
G e r t h S t l t i n g B r o d a l
y
A b s t r a c t
A n i m p l e m e n t a t i o n o f p r i o r i t y q u e u e s i s p r e s e n t e d t h a t
s u p p o r t s t h e o p e r a t i o n s M a k e Q u e u e , F i n d M i n , I n s e r t ,
M e l d a n d D e c r e a s e K e y i n w o r s t c a s e t i m e O ( 1 ) a n d
D e l e t e M i n a n d D e l e t e i n w o r s t c a s e t i m e O ( l o g n ) . T h e
s p a c e r e q u i r e m e n t i s l i n e a r . T h e d a t a s t r u c t u r e p r e s e n t e d i s
t h e r s t a c h i e v i n g t h i s w o r s t c a s e p e r f o r m a n c e .
1 I n t r o d u c t i o n
W e c o n s i d e r t h e p r o b l e m o f i m p l e m e n t i n g p r i o r i t y q u e -
u e s w h i c h a r e e c i e n t i n t h e w o r s t c a s e s e n s e . T h e o p -
e r a t i o n s w e w a n t t o s u p p o r t a r e t h e f o l l o w i n g c o m m o n l y
n e e d e d p r i o r i t y q u e u e o p e r a t i o n s 1 1 ] .
M a k e Q u e u e c r e a t e s a n d r e t u r n s a n e m p t y p r i o r i t y
q u e u e .
F i n d M i n ( Q ) r e t u r n s t h e m i n i m u m e l e m e n t c o n t a i n e d
i n p r i o r i t y q u e u e Q
I n s e r t ( Q e ) i n s e r t s a n e l e m e n t e i n t o p r i o r i t y q u e u e Q
M e l d ( Q
1
Q
2
) m e l d s p r i o r i t y q u e u e s Q
1
a n d Q
2
t o
a n e w p r i o r i t y q u e u e a n d r e t u r n s t h e r e s u l t i n g
p r i o r i t y q u e u e .
D e c r e a s e K e y ( Q ; e ; e ) r e p l a c e s e l e m e n t e b y e i n
p r i o r i t y q u e u e Q p r o v i d e d e e a n d i t i s k n o w n
w h e r e e i s s t o r e d i n Q
D e l e t e M i n ( Q ) d e l e t e s a n d r e t u r n s t h e m i n i m u m e l e -
m e n t f r o m p r i o r i t y q u e u e Q
D e l e t e ( Q e ) d e l e t e s e l e m e n t e f r o m p r i o r i t y q u e u e Q
p r o v i d e d i t i s k n o w n w h e r e e i s s t o r e d i n Q
T h e c o n s t r u c t i o n o f p r i o r i t y q u e u e s i s a c l a s s i c a l
t o p i c i n d a t a s t r u c t u r e s 1 , 2 , 3 , 4 , 5 , 6 , 7 , 1 0 , 1 2 , 1 5 , 1 6 ,
1 7 ] . A h i s t o r i c a l o v e r v i e w o f i m p l e m e n t a t i o n s c a n b e
f o u n d i n 1 1 ] . T h e r e a r e m a n y a p p l i c a t i o n s o f p r i o r i t y
q u e u e s . T w o o f t h e m o s t p r o m i n e n t e x a m p l e s a r e s o r t -
i n g p r o b l e m s a n d n e t w o r k o p t i m i z a t i o n p r o b l e m s 1 3 ] .
T h i s w o r k w a s p a r t i a l l y s u p p o r t e d b y t h e E S P R I T I I B a s i c
R e s e a r c h A c t i o n s P r o g r a m o f t h e E C u n d e r c o n t r a c t n o . 7 1 4 1
( p r o j e c t A L C O M I I ) a n d b y t h e D a n i s h N a t u r a l S c i e n c e R e s e a r c h
C o u n c i l ( G r a n t N o . 9 4 0 0 0 4 4 ) .
y
B R I C S ( B a s i c R e s e a r c h i n C o m p u t e r S c i e n c e ) , a C e n t r e o f
t h e D a n i s h N a t i o n a l R e s e a r c h F o u n d a t i o n a t : C o m p u t e r S c i e n c e
D e p a r t m e n t , A a r h u s U n i v e r s i t y , N y M u n k e g a d e , D K - 8 0 0 0
A r h u s
C , D e n m a r k . E m a i l : g e r t h @ d a i m i . a a u . d k
I n t h e a m o r t i z e d s e n s e , 1 4 ] , t h e b e s t p e r f o r m a n c e
f o r t h e s e o p e r a t i o n s i s a c h i e v e d b y F i b o n a c c i h e a p s 7 ] .
T h e y a c h i e v e a m o r t i z e d c o n s t a n t t i m e f o r a l l o p e r a t i o n s
e x c e p t f o r t h e t w o d e l e t e o p e r a t i o n s w h i c h r e q u i r e a m o r -
t i z e d t i m e O ( l o g n ) . T h e d a t a s t r u c t u r e w e p r e s e n t
a c h i e v e s m a t c h i n g w o r s t c a s e t i m e b o u n d s f o r a l l o p -
e r a t i o n s . P r e v i o u s l y , t h i s w a s o n l y a c h i e v e d f o r v a r i o u s
s t r i c t s u b s e t s o f t h e l i s t e d o p e r a t i o n s 1 , 2 , 3 , 1 5 ] . F o r
e x a m p l e t h e r e l a x e d h e a p s o f D r i s c o l l e t a l . 3 ] a n d t h e
p r i o r i t y q u e u e s i n 1 ] a c h i e v e t h e a b o v e t i m e b o u n d s i n
t h e w o r s t c a s e s e n s e e x c e p t t h a t i n 3 ] M e l d r e q u i r e s
w o r s t c a s e t i m e ( l o g n ) a n d i n 1 ] D e c r e a s e K e y r e -
q u i r e s w o r s t c a s e t i m e ( l o g n ) . R e f e r t o T a b l e 1 . I f w e
i g n o r e t h e D e l e t e o p e r a t i o n o u r r e s u l t s a r e o p t i m a l
i n t h e f o l l o w i n g s e n s e . A l o w e r b o u n d f o r D e l e t e M i n
i n t h e c o m p a r i s o n m o d e l i s p r o v e d i n 1 ] w h e r e i t i s
p r o v e d t h a t i f M e l d c a n b e p e r f o r m e d i n t i m e o ( n ) t h e n
D e l e t e M i n c a n n o t b e p e r f o r m e d i n t i m e o ( l o g n )
T h e d a t a s t r u c t u r e p r e s e n t e d i n t h i s p a p e r o r i g -
i n a t e s f r o m t h e s a m e i d e a s a s t h e r e l a x e d h e a p s o f
D r i s c o l l e t a l . 3 ] . I n 3 ] t h e d a t a s t r u c t u r e i s b a s e d
o n h e a p o r d e r e d t r e e s w h e r e ( l o g n ) n o d e s m a y v i o l a t e
h e a p o r d e r . W e e x t e n d t h i s t o a l l o w ( n ) h e a p o r d e r
v i o l a t i o n s w h i c h i s a n e c e s s a r y c o n d i t i o n t o b e a b l e t o
s u p p o r t M e l d i n w o r s t c a s e c o n s t a n t t i m e a n d i f w e
a l l o w a n o n c o n s t a n t n u m b e r o f v i o l a t i o n s .
I n S e c t i o n 2 w e d e s c r i b e t h e d a t a s t r u c t u r e r e p r e -
s e n t i n g a p r i o r i t y q u e u e . I n S e c t i o n 3 w e d e s c r i b e a
s p e c i a l d a t a s t r u c t u r e n e e d e d i n t e r n a l l y i n t h e p r i o r i t y
q u e u e c o n s t r u c t i o n . I n S e c t i o n 4 w e s h o w h o w t o i m -
p l e m e n t t h e p r i o r i t y q u e u e o p e r a t i o n s . I n S e c t i o n 5 w e
s u m m a r i z e t h e r e q u i r e d i m p l e m e n t a t i o n d e t a i l s . F i n a l l y
s o m e c o n c l u d i n g r e m a r k s o n o u r c o n s t r u c t i o n a r e g i v e n
i n S e c t i o n 6 .
2 T h e D a t a S t r u c t u r e
I n t h i s s e c t i o n w e d e s c r i b e t h e c o m p o n e n t s o f t h e
d a t a s t r u c t u r e r e p r e s e n t i n g a p r i o r i t y q u e u e . A l o t o f
t e c h n i c a l c o n s t r a i n t s a r e i n v o l v e d i n t h e c o n s t r u c t i o n .
P r i m a r y t h e s e a r e c o n s e q u e n c e s o f t h e t r a n s f o r m a t i o n s
t o b e d e s c r i b e d i n S e c t i o n 3 a n d S e c t i o n 4 . 3 . I n S e c t i o n 5
w e s u m m a r i z e t h e r e q u i r e d p a r t s o f t h e c o n s t r u c t i o n
d e s c r i b e d i n t h e f o l l o w i n g s e c t i o n s .
T h e b a s i c i d e a i s t o r e p r e s e n t a p r i o r i t y q u e u e
1
8/7/2019 Worst Case Efficient Priority Queues
2/7
2 G e r t h S t l t i n g B r o d a l
A m o r t i z e d W o r s t c a s e
F r e d m a n e t a l . 7 ] D r i s c o l l e t a l . 3 ] B r o d a l 1 ] N e w r e s u l t
M a k e Q u e u e O ( 1 ) O ( 1 ) O ( 1 ) O ( 1 )
F i n d M i n O ( 1 ) O ( 1 ) O ( 1 ) O ( 1 )
I n s e r t O ( 1 ) O ( 1 ) O ( 1 ) O ( 1 )
M e l d O ( 1 ) O ( l o g n ) O ( 1 ) O ( 1 )
D e c r e a s e K e y O ( 1 ) O ( 1 ) O ( l o g n ) O ( 1 )
D e l e t e / D e l e t e M i n O ( l o g n ) O ( l o g n ) O ( l o g n ) O ( l o g n )
T a b l e 1 : T i m e b o u n d s f o r t h e p r e v i o u s l y b e s t p r i o r i t y q u e u e i m p l e m e n t a t i o n s .
b y t w o t r e e s T
1
a n d T
2
w h e r e a l l n o d e s c o n t a i n o n e
e l e m e n t a n d h a v e a n o n n e g a t i v e i n t e g e r r a n k a s s i g n e d .
I n t u i t i v e l y t h e r a n k o f a n o d e i s t h e l o g a r i t h m o f t h e
s i z e o f t h e s u b t r e e r o o t e d a t t h e n o d e . T h e d e t a i l s o f
t h e r a n k a s s i g n m e n t a c h i e v i n g t h i s f o l l o w b e l o w .
T h e s o n s o f a n o d e a r e s t o r e d i n a d o u b l y l i n k e d l i s t
i n i n c r e a s i n g r a n k o r d e r f r o m r i g h t t o l e f t . E a c h n o d e
h a s a l s o a p o i n t e r t o i t s l e f t m o s t s o n a n d a p o i n t e r t o
i t s p a r e n t .
T h e n o t a t i o n w e u s e i s t h e f o l l o w i n g . W e m a k e n o
d i s t i n c t i o n b e t w e e n a n o d e a n d t h e e l e m e n t i t c o n t a i n s .
W e l e t x ; y ; : : : d e n o t e n o d e s , p ( x ) t h e p a r e n t o f x r ( x )
t h e r a n k o f x n
i
( x ) t h e n u m b e r o f s o n s o f r a n k i t h a t x
h a s a n d t
i
t h e r o o t o f T
i
. N o d e s w h i c h a r e l a r g e r t h a n
t h e i r p a r e n t s a r e c a l l e d g o o d n o d e s | g o o d b e c a u s e t h e y
s a t i s f y h e a p o r d e r . N o d e s w h i c h a r e n o t g o o d a r e c a l l e d
v i o l a t i n g n o d e s .
T h e i d e a i s t o l e t t
1
b e t h e m i n i m u m e l e m e n t a n d
t o l a z y m e r g e t h e t w o t r e e s T
1
a n d T
2
s u c h t h a t T
2
b e c o m e s e m p t y . S i n c e t
1
i s t h e m i n i m u m e l e m e n t w e c a n
s u p p o r t F i n d M i n i n w o r s t c a s e c o n s t a n t t i m e a n d t h e
l a z y m e r g i n g o f t h e t w o t r e e s c o r r e s p o n d s i n t u i t i v e l y t o
p e r f o r m i n g M e l d i n c r e m e n t a l l y o v e r t h e n e x t s e q u e n c e
o f o p e r a t i o n s . T h e m e r g i n g o f t h e t w o t r e e s i s d o n e
b y i n c r e m e n t a l l y i n c r e a s i n g t h e r a n k o f t
1
b y m o v i n g
t h e s o n s o f t
2
t o t
1
s u c h t h a t T
2
b e c o m e s e m p t y a n d t
1
b e c o m e s t h e n o d e o f m a x i m u m r a n k . T h e a c t u a l d e t a i l s
o f i m p l e m e n t i n g M e l d f o l l o w i n S e c t i o n 4 . 5 .
A s m e n t i o n e d b e f o r e w e h a v e s o m e r e s t r i c t i o n s o n
t h e t r e e s f o r c i n g t h e r a n k o f a n o d e t o b e r e l a t e d t o t h e
s i z e o f t h e s u b t r e e r o o t e d a t t h e n o d e . F o r t h i s p u r p o s e
w e m a i n t a i n t h e i n v a r i a n t s S 1 { S 5 b e l o w f o r a n y n o d e x
S 1 : I f x i s a l e a f , t h e n r ( x ) = 0 ,
S 2 r ( x ) < r ( p ( x ) )
S 3 : i f r ( x ) > 0 , t h e n n
r ( x ) 1
( x ) 2
S 4 n
i
( x ) 2 f 0 2 3 ; : : : ; 7 g
S 5 T
2
= ; o r r ( t
1
) r ( t
2
)
T h e r s t t w o i n v a r i a n t s j u s t s a y t h a t l e a v e s h a v e
r a n k z e r o a n d t h a t t h e r a n k s o f t h e n o d e s s t r i c t l y
i n c r e a s e t o w a r d s t h e r o o t . I n v a r i a n t S 3 s a y s t h a t a
n o d e o f r a n k k h a s a t l e a s t t w o s o n s o f r a n k k 1
T h i s g u a r a n t e e s t h a t t h e s i z e o f t h e s u b t r e e r o o t e d
a t a n o d e i s a t l e a s t e x p o n e n t i a l i n t h e r a n k o f t h e
n o d e ( b y i n d u c t i o n i t f o l l o w s f r o m S 1 a n d S 3 t h a t t h e
s u b t r e e r o o t e d a t n o d e x h a s s i z e a t l e a s t 2
r ( x ) + 1
1 )
I n v a r i a n t S 4 b o u n d s t h e n u m b e r o f s o n s o f a n o d e t h a t
h a v e t h e s a m e r a n k w i t h i n a c o n s t a n t . T h i s i m p l i e s
t h e c r u c i a l f a c t t h a t a l l n o d e s h a v e r a n k a n d d e g r e e
O ( l o g n ) . F i n a l l y S 5 s a y s t h a t e i t h e r T
2
i s e m p t y o r i t s
r o o t h a s r a n k l a r g e r t h a n o r e q u a l t o t h e r a n k o f t h e
r o o t o f T
1
N o t i c e t h a t i n S 4 w e d o n o t a l l o w a n o d e t o h a v e
o n l y a s i n g l e s o n o f a g i v e n r a n k . T h i s i s b e c a u s e
t h i s a l l o w s u s t o c u t o t h e l e f t m o s t s o n s o f a n o d e
s u c h t h a t t h e n o d e c a n g e t a n e w r a n k a s s i g n e d w h e r e
S 3 i s s t i l l s a t i s e d . T h i s p r o p e r t y i s e s s e n t i a l t o t h e
t r a n s f o r m a t i o n s t o b e d e s c r i b e d i n S e c t i o n 4 . 3 . T h e
r e q u i r e m e n t n
i
( x ) 7 i n S 4 i s a c o n s e q u e n c e o f t h e
c o n s t r u c t i o n d e s c r i b e d i n S e c t i o n 3 .
A f t e r h a v i n g d e s c r i b e d t h e c o n d i t i o n s o f h o w n o d e s
a r e a s s i g n e d r a n k s a n d h o w t h i s f o r c e s s t r u c t u r e o n
t h e t r e e s w e n o w t u r n t o c o n s i d e r h o w t o h a n d l e t h e
v i o l a t i n g n o d e s | w h i c h t o g e t h e r w i t h t h e t w o r o o t s
c o u l d b e p o t e n t i a l m i n i m u m e l e m e n t s . T o k e e p t r a c k
o f t h e v i o l a t i n g n o d e s w e a s s o c i a t e t o e a c h n o d e x t w o
s u b s e t s V ( x ) a n d W ( x ) o f n o d e s l a r g e r t h a n x f r o m t h e
t r e e s T
1
a n d T
2
. T h a t i s t h e n o d e s i n V ( x ) a n d W ( x )
a r e g o o d w i t h r e s p e c t t o x . W e d o n o t r e q u i r e t h a t i f
y 2 V ( x ) W ( x ) t h a t x a n d y b e l o n g t o t h e s a m e T
i
t r e e . B u t w e r e q u i r e t h a t a n o d e y b e l o n g s t o a t m o s t
o n e V o r o n e W s e t . A l s o w e d o n o t r e q u i r e t h a t i f
y 2 V ( x ) W ( x ) t h e n r ( y ) r ( x )
T h e V s e t s a n d t h e W s e t s a r e a l l s t o r e d a s d o u b l y
l i n k e d l i s t s . V i o l a t i o n s a d d e d t o a V s e t a r e a l w a y s
a d d e d t o t h e f r o n t o f t h e l i s t . V i o l a t i o n s a d d e d t o a
W s e t a r e a l w a y s a d d e d i n s u c h a w a y t h a t v i o l a t i o n s
o f t h e s a m e r a n k a r e a d j a c e n t . S o i f w e h a v e t o a d d a
v i o l a t i o n t o W ( x ) a n d t h e r e i s a l r e a d y a n o d e i n W ( x )
o f t h e s a m e r a n k , t h e n w e i n s e r t t h e n e w n o d e a d j a c e n t
t o t h i s n o d e . O t h e r w i s e w e j u s t i n s e r t t h e n e w n o d e a t
t h e f r o n t o f W ( x )
W e i m p l e m e n t t h e V ( x ) a n d W ( x ) s e t s b y l e t t i n g
e a c h t r e e n o d e x h a v e f o u r a d d i t i o n a l p o i n t e r s : O n e t o
8/7/2019 Worst Case Efficient Priority Queues
3/7
W o r s t - C a s e E c i e n t P r i o r i t y Q u e u e s 3
t h e r s t n o d e i n V ( x ) , o n e t o t h e r s t n o d e i n W ( x )
a n d t w o t o t h e n e x t a n d p r e v i o u s n o d e i n t h e v i o l a t i o n
l i s t t h a t x b e l o n g s t o | p r o v i d e d x i s c o n t a i n e d i n a
v i o l a t i o n l i s t . E a c h t i m e w e a d d a n o d e t o a v i o l a t i o n s e t
w e a l w a y s r s t r e m o v e t h e n o d e f r o m t h e s e t i t p o s s i b l y
b e l o n g e d t o .
I n t u i t i v e l y V ( x ) ' s p u r p o s e i s t o c o n t a i n v i o l a t i n g
n o d e s o f l a r g e r a n k . W h e r e a s W ( x ) ' s p u r p o s e i s t o
c o n t a i n v i o l a t i n g n o d e s o f s m a l l r a n k . I f a n e w v i o l a t i n g
n o d e i s c r e a t e d w h i c h h a s l a r g e r a n k , i . e . r ( x ) r ( t
1
)
w e a d d t h e v i o l a t i o n t o V ( t
1
) , o t h e r w i s e w e a d d t h e
v i o l a t i o n t o W ( t
1
) . T o b e a b l e t o a d d a n o d e t o W ( t
1
)
a t t h e c o r r e c t p o s i t i o n w e n e e d t o k n o w i f a n o d e a l r e a d y
e x i s t s i n W ( t
1
) o f t h e s a m e r a n k . I n c a s e t h e r e i s w e
n e e d t o k n o w s u c h a n e l e m e n t . F o r t h i s p u r p o s e w e
m a i n t a i n a n e x t e n d i b l e a r r a y
1
o f s i z e r ( t
1
) o f p o i n t e r s
t o n o d e s i n W ( t
1
) o f e a c h p o s s i b l e r a n k . I f n o n o d e
e x i s t s o f a g i v e n r a n k i n W ( t
1
) t h e c o r r e s p o n d i n g e n t r y
i n t h e a r r a y i s n u l l
T h e s t r u c t u r e o n t h e V a n d W s e t s i s e n f o r c e d b y
t h e f o l l o w i n g i n v a r i a n t s O 1 { O 5 . W e l e t w
i
( x ) d e n o t e
t h e n u m b e r o f n o d e s i n W ( x ) o f r a n k i
O 1 t
1
= m i n T
1
T
2
O 2 : i f y 2 V ( x ) W ( x ) , t h e n y x
O 3 : i f y < p ( y ) , t h e n a n x 6= y e x i s t s s u c h t h a t
y 2 V ( x ) W ( x )
O 4 w
i
( x ) 6
O 5 : i f V ( x ) = ( y
V ( x )
; : : : ; y
2
y
1
) , t h e n
r ( y
i
) b ( i 1 ) = c f o r i = 1 2 ; : : : ; V ( x )
w h e r e i s a c o n s t a n t .
O 1 g u a r a n t e e s t h a t t h e m i n i m u m e l e m e n t c o n t a i n e d
i n a p r i o r i t y q u e u e a l w a y s i s t h e r o o t o f T
1
. O 2 s a y s
t h a t t h e e l e m e n t s a r e h e a p o r d e r e d w i t h r e s p e c t t o
m e m b e r s h i p o f t h e V a n d W s e t s . O 3 s a y s t h a t a l l
v i o l a t i n g n o d e s b e l o n g t o a V o r W s e t . B e c a u s e a l l
n o d e s h a v e r a n k O ( l o g n ) i n v a r i a n t s O 4 a n d O 5 i m p l y
t h a t t h e s i z e s o f a l l V a n d W s e t s a r e O ( l o g n ) . N o t i c e
t h a t i f w e r e m o v e a n e l e m e n t f r o m a V o r W s e t , t h e n
t h e i n v a r i a n t s O 4 a n d O 5 c a n n o t b e c o m e v i o l a t e d .
T h a t i n v a r i a n t s O 4 a n d O 5 a r e s t a t e d q u i t e d i e r -
e n t l y i s b e c a u s e t h e V a n d W s e t s h a v e v e r y d i e r e n t
r o l e s i n t h e c o n s t r u c t i o n . R e c a l l t h a t t h e V s e t s t a k e
c a r e o f l a r g e v i o l a t i o n s , i . e . v i o l a t i o n s t h a t h a v e r a n k
l a r g e r t h a n r ( t
1
) w h e n t h e y a r e c r e a t e d . T h e c o n s t a n t
1
A n e x t e n d i b l e a r r a y i s a n a r r a y o f w h i c h t h e l e n g t h c a n b e
i n c r e a s e d b y o n e i n w o r s t c a s e c o n s t a n t t i m e . I t i s f o l k l o r e t h a t
e x t e n d i b l e a r r a y s c a n b e o b t a i n e d f r o m o r d i n a r y a r r a y s b y a r r a y
d o u b l i n g a n d i n c r e m e n t a l c o p y i n g . I n t h e r e s t o f t h i s p a p e r a l l
a r r a y s a r e e x t e n d i b l e a r r a y s .
i s t h e n u m b e r o f l a r g e v i o l a t i o n s t h a t c a n b e c r e a t e d
b e t w e e n t w o i n c r e a s e s i n t h e r a n k o f t
1
F o r t h e r o o t s t
1
a n d t
2
w e s t r e n g t h e n t h e i n v a r i a n t s
s u c h t h a t R 1 { R 3 a l s o s h o u l d b e s a t i s e d .
R 1 n
i
( t
j
) 2 f 2 3 ; : : : ; 7 g f o r i = 0 1 ; : : : ; r ( t
j
) 1
R 2 V ( t
1
) r ( t
1
)
R 3 : i f y 2 W ( t
1
) t h e n r ( y ) < r ( t
1
)
I n v a r i a n t R 1 g u a r a n t e e s t h a t t h e r e a r e a t l e a s t t w o
s o n s o f e a c h r a n k a t b o t h r o o t s . T h i s p r o p e r t y i s
i m p o r t a n t f o r t h e t r a n s f o r m a t i o n s t o b e d e s c r i b e d i n
S e c t i o n 4 . 2 a n d S e c t i o n 4 . 3 . I n v a r i a n t R 2 t o g e t h e r w i t h
i n v a r i a n t O 5 g u a r a n t e e t h a t i f w e c a n i n c r e a s e t h e r a n k
o f t
1
b y o n e w e c a n c r e a t e n e w l a r g e v i o l a t i o n s a n d a d d
t h e m t o V ( t
1
) w i t h o u t v i o l a t i n g i n v a r i a n t O 5 . I n v a r i a n t
R 3 s a y s t h a t a l l v i o l a t i o n s i n W ( t
1
) h a v e t o b e s m a l l .
T h e m a i n t e n a n c e o f R 1 a n d O 4 t u r n s o u t t o b e
n o n t r i v i a l b u t t h e y c a n a l l b e m a i n t a i n e d b y a p p l y i n g
t h e s a m e i d e a . T o u n i f y t h i s i d e a w e i n t r o d u c e t h e
c o n c e p t o f a g u i d e t o b e d e s c r i b e d i n S e c t i o n 3 .
T h e m a i n i d e a b e h i n d t h e c o n s t r u c t i o n i s t h e f o l -
l o w i n g c a p t u r e d b y t h e D e c r e a s e K e y o p e r a t i o n . T h e
d e t a i l s f o l l o w i n S e c t i o n 4 . E a c h t i m e w e p e r f o r m a
D e c r e a s e K e y o p e r a t i o n w e j u s t a d d t h e n e w v i o l a t -
i n g n o d e t o o n e o f t h e s e t s V ( t
1
) o r W ( t
1
) . T o a v o i d
h a v i n g t o o m a n y v i o l a t i o n s s t o r e d a t t h e r o o t o f T
1
w e
i n c r e m e n t a l l y d o t w o d i e r e n t k i n d s o f t r a n s f o r m a t i o n s .
T h e r s t t r a n s f o r m a t i o n m o v e s t h e s o n s o f t
2
t o t
1
s u c h
t h a t t h e r a n k o f t
1
i n c r e a s e s . T h e s e c o n d t r a n s f o r m a t i o n
r e d u c e s t h e n u m b e r o f v i o l a t i o n s i n W ( t
1
) b y r e p l a c i n g
t w o v i o l a t i o n s o f r a n k k b y a t m o s t o n e v i o l a t i o n o f r a n k
k + 1 . T h e s e t r a n s f o r m a t i o n s a r e p e r f o r m e d t o r e e s t a b -
l i s h i n v a r i a n t s R 2 a n d O 4 .
3 G u i d e s
I n t h i s s e c t i o n w e d e s c r i b e t h e g u i d e d a t a s t r u c t u r e
t h a t h e l p s u s m a i n t a i n i n g t h e i n v a r i a n t s R 1 a n d O 4 o n
n
i
( t
1
) n
i
( t
2
) a n d w
i
( t
1
) . T h e r e l a t i o n s h i p b e t w e e n t h e
a b s t r a c t s e q u e n c e s o f v a r i a b l e s i n t h i s s e c t i o n a n d t h e
s o n s a n d t h e v i o l a t i o n s s t o r e d a t t h e r o o t s a r e e x p l a i n e d
i n S e c t i o n 4 .
T h e p r o b l e m c a n i n f o r m a l l y b e d e s c r i b e d a s f o l l o w s .
A s s u m e w e h a v e t o m a i n t a i n a s e q u e n c e o f i n t e g e r
v a r i a b l e s x
k
x
k 1
; : : : ; x
1
( a l l s e q u e n c e s i n t h i s s e c t i o n
g o e s f r o m r i g h t t o l e f t ) a n d w e w a n t t o s a t i s f y t h e
i n v a r i a n t t h a t a l l x
i
T f o r s o m e t h r e s h o l d T . O n t h e
s e q u e n c e w e c a n o n l y p e r f o r m R e d u c e ( i ) o p e r a t i o n s
w h i c h d e c r e a s e x
i
b y a t l e a s t t w o a n d i n c r e a s e x
i + 1
b y
a t m o s t o n e . T h e x
i
s c a n b e f o r c e d t o i n c r e a s e a n d
d e c r e a s e b y o n e , b u t f o r e a c h c h a n g e i n a n x
i
w e a r e
a l l o w e d t o d o O ( 1 ) R e d u c e o p e r a t i o n s t o p r e v e n t a n y
x
i
f r o m e x c e e d i n g T . T h e g u i d e ' s j o b i s t o t e l l u s w h i c h
o p e r a t i o n s t o p e r f o r m .
8/7/2019 Worst Case Efficient Priority Queues
4/7
4 G e r t h S t l t i n g B r o d a l
x
1
x
2
x
3
x
9
x
1 6
0120102021101121
r r r r r r r r r r r r r r r r
3
?
79
?
1 5
?
? ? ? ? ? ? ?
6
F i g u r e 1 : T h e g u i d e d a t a s t r u c t u r e .
T h i s p r o b l e m a l s o a r i s e s i m p l i c i t l y i n 1 , 2 , 8 , 9 ] .
B u t t h e s o l u t i o n p r e s e n t e d i n 8 ] r e q u i r e s t i m e ( k )
t o n d w h i c h R e d u c e o p e r a t i o n s t o p e r f o r m w h e r e a s
t h e p r o b l e m s i n t h e o t h e r p a p e r s a r e s i m p l e r b e c a u s e
o n l y x
1
c a n b e f o r c e d t o i n c r e a s e a n d d e c r e a s e . T h e
d a t a s t r u c t u r e w e p r e s e n t c a n n d w h i c h o p e r a t i o n s t o
p e r f o r m i n w o r s t c a s e t i m e O ( 1 ) f o r t h e g e n e r a l p r o b l e m .
T o m a k e t h e g u i d e ' s k n o w l e d g e a b o u t t h e x
i
s a s
s m a l l a s p o s s i b l e w e r e v e a l t o t h e g u i d e a n o t h e r s e q u e n c e
x
k
; : : : ; x
1
s u c h t h a t x
i
x
i
2 f T 2 T 1 T g ( t h i s
c h o i c e i s a c o n s e q u e n c e o f t h e c o n s t r u c t i o n w e d e s c r i b e
b e l o w ) . A s l o n g a s a l l x
i
x
i
w e d o n o t r e q u i r e h e l p
f r o m t h e g u i d e . F i r s t w h e n a n x
i
= x
i
i s f o r c e d t o
b e c o m e x
i
+ 1 w e r e q u i r e h e l p f r o m t h e g u i d e . I n t h e
f o l l o w i n g w e a s s u m e w . l . o . g . t h a t t h e t h r e s h o l d T i s t w o
s u c h t h a t x
i
2 f 0 1 2 g a n d t h a t R e d u c e ( i ) d e c r e a s e s
x
i
b y t w o a n d i n c r e a s e s x
i + 1
b y o n e .
T h e d a t a s t r u c t u r e m a i n t a i n e d b y t h e g u i d e p a r t i -
t i o n s t h e s e q u e n c e x
k
; : : : ; x
1
i n t o b l o c k s o f c o n s e c u t i v e
x
i
s o f t h e f o r m 2 1 1 ; : : : ; 1 0 w h e r e t h e n u m b e r o f o n e s
i s a l l o w e d t o b e z e r o . T h e g u i d e m a i n t a i n s t h e i n v a r i a n t
t h a t a l l x
i
s n o t b e l o n g i n g t o a b l o c k o f t h e a b o v e t y p e
h a v e v a l u e e i t h e r z e r o o r o n e . A n e x a m p l e o f a s e q u e n c e
s a t i s f y i n g t h i s i s t h e f o l l o w i n g w h e r e b l o c k s a r e s h o w n
b y u n d e r l i n i n g t h e s u b s e q u e n c e s .
1 2 1 1 0 1 1 2 0 2 0 1 0 2 1 0
T h e g u i d e s t o r e s t h e v a l u e s o f t h e v a r i a b l e s x
i
i n
o n e a r r a y a n d u s e s a n o t h e r a r r a y t o h a n d l e t h e b l o c k s .
T h e s e c o n d a r r a y c o n t a i n s p o i n t e r s t o m e m o r y c e l l s
w h i c h c o n t a i n t h e i n d e x o f a n x
i
o r t h e v a l u e ? . A l l
v a r i a b l e s i n t h e s a m e b l o c k p o i n t t o t h e s a m e c e l l a n d
t h i s c e l l c o n t a i n s t h e i n d e x o f t h e l e f t m o s t v a r i a b l e i n
t h e b l o c k . V a r i a b l e s n o t b e l o n g i n g t o a b l o c k p o i n t t o
a c e l l c o n t a i n i n g ? . A d a t a s t r u c t u r e f o r t h e p r e v i o u s
e x a m p l e i s i l l u s t r a t e d i n F i g u r e 1 . N o t i c e t h a t s e v e r a l
v a r i a b l e s c a n s h a r e a m e m o r y c e l l c o n t a i n i n g ? . T h i s
d a t a s t r u c t u r e h a s t w o v e r y i m p o r t a n t p r o p e r t i e s :
1 . G i v e n a v a r i a b l e w e c a n i n w o r s t c a s e t i m e O ( 1 )
n d t h e l e f t m o s t v a r i a b l e i n t h e b l o c k , a n d
2 . w e c a n i n w o r s t c a s e t i m e O ( 1 ) d e s t r o y a g i v e n
b l o c k , i . e . l e t a l l n o d e s i n t h e b l o c k b e l o n g t o n o
b l o c k , b y s i m p l y a s s i g n i n g ? t o t h e b l o c k ' s m e m o r y
c e l l .
W h e n a n x
i
i s f o r c e d t o i n c r e a s e t h e g u i d e c a n i n
w o r s t c a s e t i m e O ( 1 ) d e c i d e w h i c h R e d u c e o p e r a t i o n s
t o p e r f o r m . W e o n l y s h o w h o w t o h a n d l e o n e n o n t r i v i a l
c a s e , a l l o t h e r c a s e s a r e s i m i l a r . A s s u m e t h a t t h e r e a r e
t w o b l o c k s o f v a r i a b l e s a d j a c e n t t o e a c h o t h e r a n d t h a t
t h e l e f t m o s t x
i
= 1 i n t h e r i g h t m o s t b l o c k h a s t o b e
i n c r e a s e d . T h e n t h e f o l l o w i n g t r a n s f o r m a t i o n s h a v e t o
b e p e r f o r m e d :
2 1 1 0 2 1 1 1 0
> 2 1 1 0 2 2 1 1 0 i n c r e m e n t x
i
> 2 1 1 1 0 2 1 1 0 R e d u c e
> 2 1 1 1 1 0 1 1 0 R e d u c e
> 2 1 1 1 1 0 1 1 0 r e e s t a b l i s h b l o c k s
T o r e e s t a b l i s h t h e b l o c k s t h e t w o p o i n t e r s o f t h e n e w
v a r i a b l e s i n t h e l e f t m o s t b l o c k a r e s e t t o p o i n t t o t h e
l e f t m o s t b l o c k ' s m e m o r y c e l l a n d t h e r i g h t m o s t b l o c k ' s
m e m o r y c e l l i s a s s i g n e d t h e v a l u e ?
I n t h e c a s e d e s c r i b e d a b o v e o n l y t w o R e d u c e
o p e r a t i o n s w e r e r e q u i r e d a n d t h e s e w e r e p e r f o r m e d o n
x
j
s w h e r e j i . T h i s i s t r u e f o r a l l c a s e s .
W e c o n c l u d e t h i s s e c t i o n w i t h t w o r e m a r k s o n t h e
c o n s t r u c t i o n . B y u s i n g e x t e n d i b l e a r r a y s t h e s e q u e n c e
o f v a r i a b l e s c a n b e e x t e n d e d b y a n e w x
k + 1
e q u a l t o z e r o
o r o n e i n w o r s t c a s e t i m e O ( 1 ) . I f w e a d d a r e f e r e n c e
c o u n t e r t o e a c h m e m o r y c e l l w e c a n r e u s e t h e m e m o r y
c e l l s s u c h t h a t t h e t o t a l n u m b e r o f n e e d e d m e m o r y c e l l s
i s a t m o s t k
4 O p e r a t i o n s
I n t h i s s e c t i o n w e d e s c r i b e h o w t o i m p l e m e n t t h e d i e r -
e n t p r i o r i t y q u e u e o p e r a t i o n s . W e b e g i n b y d e s c r i b i n g
s o m e t r a n s f o r m a t i o n s o n t h e t r e e s w h i c h a r e e s s e n t i a l t o
t h e o p e r a t i o n s t o b e i m p l e m e n t e d .
4 . 1 L i n k i n g a n d d e l i n k i n g t r e e s . T h e f u n d a m e n t a l
o p e r a t i o n o n t h e t r e e s i s t h e l i n k i n g o f t r e e s . A s s u m e
t h a t w e h a v e t h r e e n o d e s x
1
x
2
a n d x
3
o f e q u a l r a n k a n d
n o n e o f t h e m i s a r o o t t
i
. B y d o i n g t w o c o m p a r i s o n s w e
c a n n d t h e m i n i m u m . A s s u m e w . l . o . g . t h a t x
1
i s t h e
m i n i m u m . W e c a n n o w m a k e x
2
a n d x
3
t h e l e f t m o s t
s o n s o f x
1
a n d i n c r e a s e t h e r a n k o f x
1
b y o n e . N e i t h e r
x
2
o r x
3
b e c o m e v i o l a t i n g n o d e s a n d x
1
s t i l l s a t i s e s a l l
t h e i n v a r i a n t s S 1 { S 5 a n d O 1 { O 5 .
T h e d e l i n k i n g o f a t r e e r o o t e d a t n o d e x i s a l i t t l e
b i t m o r e t r i c k y . I f x h a s e x a c t l y t w o o r t h r e e s o n s o f
r a n k r ( x ) 1 , t h e n t h e s e t w o o r t h r e e s o n s c a n b e c u t
o a n d x g e t s t h e r a n k o f t h e l a r g e s t r a n k e d s o n p l u s
o n e . F r o m S 4 i t f o l l o w s t h a t x s t i l l s a t i s e s S 3 a n d i t
8/7/2019 Worst Case Efficient Priority Queues
5/7
W o r s t - C a s e E c i e n t P r i o r i t y Q u e u e s 5
f o l l o w s t h a t S 1 { S 5 a n d O 1 { O 5 a r e s t i l l s a t i s e d . I n t h e
c a s e w h e r e x h a s a t l e a s t f o u r s o n s o f r a n k r ( x ) 1 t w o
o f t h e s e s o n s a r e s i m p l y c u t o . B e c a u s e x s t i l l h a s a t
l e a s t t w o s o n s o f r a n k r ( x ) 1 t h e i n v a r i a n t s a r e s t i l l
s a t i s e d .
I t f o l l o w s t h a t t h e d e l i n k i n g o f a t r e e o f r a n k k
a l w a y s r e s u l t s i n t w o o r t h r e e t r e e s o f r a n k k 1 a n d
o n e a d d i t i o n a l t r e e o f r a n k a t m o s t k ( t h e t r e e c a n b e
o f a n y r a n k b e t w e e n z e r o a n d k )
4 . 2 M a i n t a i n i n g t h e s o n s o f a r o o t . W e n o w
d e s c r i b e h o w t o a d d s o n s b e l o w a r o o t a n d h o w t o c u t
o s o n s a t a r o o t w h i l e k e e p i n g R 1 s a t i s e d . F o r t h i s
p u r p o s e w e r e q u i r e f o u r g u i d e s , t w o f o r e a c h o f t h e r o o t s
t
1
a n d t
2
. W e o n l y s k e t c h t h e s i t u a t i o n a t t
1
b e c a u s e
t h e c o n s t r u c t i o n f o r t
2
i s a n a l o g o u s .
T o h a v e c o n s t a n t t i m e a c c e s s t o t h e s o n s o f t
1
w e
m a i n t a i n a n e x t e n d i b l e a r r a y o f p o i n t e r s t h a t f o r e a c h
r a n k i = 0 ; : : : ; r ( t
1
) 1 h a s a p o i n t e r t o a s o n o f t
1
o f r a n k i . B e c a u s e o f R 1 s u c h s o n s a r e g u a r a n t e e d t o
e x i s t . T h i s e n a b l e s u s t o l i n k a n d d e l i n k s o n s o f r a n k i i n
w o r s t c a s e t i m e O ( 1 ) f o r a n a r b i t r a r y i . O n e g u i d e t a k e s
c a r e o f t h a t n
i
( t
1
) 7 a n d t h e o t h e r o f t h a t n
i
( t
1
) 2
f o r i = 0 ; : : : ; r ( t
1
) 3 ( t o m a i n t a i n a l o w e r b o u n d o n
a s e q u e n c e o f v a r i a b l e s i s e q u i v a l e n t t o m a i n t a i n i n g a n
u p p e r b o u n d o n t h e n e g a t e d s e q u e n c e ) . T h e s o n s o f t
1
o f r a n k r ( t
1
) 1 a n d r ( t
1
) 2 a r e t r e a t e d s e p a r a t e l y i n a
s t r a i g h t f o r w a r d w a y s u c h t h a t t h e r e a l w a y s a r e b e t w e e n
2 a n d 7 s o n s o f t h e s e r a n k s . T h i s i s n e c e s s a r y b e c a u s e
o f t h e d e p e n d e n c y b e t w e e n t h e g u i d e m a i n t a i n i n g t h e
u p p e r b o u n d o n n
i
( t
1
) a n d t h e g u i d e m a i n t a i n i n g t h e
l o w e r b o u n d o n n
i
( t
1
) . T h e \ m a r k e d " v a r i a b l e s t h a t
w e r e v e a l t o t h e g u i d e t h a t m a i n t a i n s t h e u p p e r b o u n d
o n n
i
( t
1
) h a v e v a l u e s f 5 6 7 g a n d t o t h e g u i d e t h a t
m a i n t a i n s t h e l o w e r b o u n d h a v e v a l u e s f 4 3 2 g
I f w e a d d a n e w s o n a t t
1
o f r a n k i w e t e l l t h e g u i d e
m a i n t a i n i n g t h e u p p e r b o u n d t h a t n
i
( t
1
) i s f o r c e d t o
i n c r e a s e b y o n e ( t h i s a s s u m e s i < r ( t
1
) 2 ) . T h e n t h e
g u i d e t h e n t e l l s u s w h e r e t o d o a t m o s t t w o R e d u c e
o p e r a t i o n s . T h e R e d u c e ( i ) o p e r a t i o n i n t h i s c o n t e x t
c o r r e s p o n d s t o t h e l i n k i n g o f t h r e e t r e e s o f r a n k i . T h i s
d e c r e a s e s n
i
( t
1
) b y t h r e e a n d i n c r e a s e s n
i + 1
( t
1
) b y o n e .
W e o n l y d o t h e l i n k i n g w h e n n
i
( t
1
) = 7 s o t h a t t h e g u i d e
m a i n t a i n i n g t h e l o w e r b o u n d o n n
i
( t
1
) w i l l b e u n a e c t e d
( t h i s i m p l i e s a m i n o r c h a n g e i n t h e g u i d e ) . I f t h i s r e s u l t s
i n t o o m a n y s o n s o f r a n k r ( t
1
) 2 o r r ( t
1
) 1 w e l i n k
s o m e o f t h e s e s o n s a n d p o s s i b l y i n c r e a s e t h e r a n k o f t
1
I f t h e r a n k o f t
1
i n c r e a s e s w e a l s o h a v e t o i n c r e a s e t h e
d o m a i n o f t h e t w o g u i d e s .
T o c u t o a s o n i s s i m i l a r , b u t n o w t h e R e d u c e
o p e r a t i o n c o r r e s p o n d s t o t h e d e l i n k i n g o f a t r e e . T h e
a d d i t i o n a l t r e e f r o m t h e d e l i n k i n g t r a n s f o r m a t i o n t h a t
c a n h a v e v a r i o u s r a n k s i s t r e a t e d s e p a r a t e l y a f t e r t h e
d e l i n k i n g . W e j u s t a d d i t b e l o w t
1
a s d e s c r i b e d a b o v e .
A t t
2
t h e s i t u a t i o n i s n e a r l y t h e s a m e . T h e m a j o r
d i e r e n c e i s t h a t b e c a u s e w e k n e w t h a t t
1
w a s t h e
s m a l l e s t e l e m e n t t h e l i n k i n g a n d d e l i n k i n g o f s o n s o f t
1
w o u l d n o t c r e a t e n e w v i o l a t i o n s . T h i s i s n o t t r u e a t t
2
T h e l i n k i n g o f s o n s n e v e r c r e a t e s n e w v i o l a t i o n s b u t t h e
d e l i n k i n g o f s o n s a t t
2
c a n c r e a t e t h r e e n e w v i o l a t i o n s .
W e w i l l s e e i n S e c t i o n 4 . 4 t h a t i t t u r n s o u t t h a t w e o n l y
c u t o s o n s o f t
2
w h i c h h a v e r a n k l a r g e r t h a n r ( t
1
) . T h e
t r e e \ l e f t o v e r " b y a d e l i n k i n g i s m a d e a s o n o f t
1
i f i t
h a s r a n k l e s s t h a n r ( t
1
) . O t h e r w i s e i t i s m a d e a s o n
o f t
2
. T h e n e w v i o l a t i o n s w h i c h h a v e r a n k l a r g e r t h a n
r ( t
1
) a r e a d d e d t o V ( t
1
) . T o s a t i s f y O 5 a n d R 2 w e j u s t
h a v e t o g u a r a n t e e t h a t t h e r a n k o f t
1
w i l l b e i n c r e a s e d
a n d t h a t i n R 2 a n d O 5 i s c h o s e n l a r g e e n o u g h .
4 . 3 V i o l a t i o n r e d u c i n g t r a n s f o r m a t i o n s . W e n o w
d e s c r i b e t h e m o s t e s s e n t i a l t r a n s f o r m a t i o n o n t h e t r e e s .
T h e t r a n s f o r m a t i o n r e d u c e s t h e n u m b e r o f p o t e n t i a l
v i o l a t i o n s
S
y 2 T
1
T
2
V ( y ) W ( y ) i n t h e t r e e b y a t l e a s t
o n e .
A s s u m e w e h a v e t w o p o t e n t i a l v i o l a t i o n s x
1
a n d x
2
o f e q u a l r a n k k < r ( t
1
) w h i c h a r e n o t r o o t s o r s o n s
o f a r o o t . F i r s t w e c h e c k t h a t b o t h x
1
a n d x
2
a r e
v i o l a t i n g n o d e s . I f o n e o f t h e n o d e s a l r e a d y i s a g o o d
n o d e w e r e m o v e i t f r o m t h e c o r r e s p o n d i n g v i o l a t i o n s e t .
O t h e r w i s e w e p r o c e e d a s d e s c r i b e d b e l o w .
B e c a u s e o f S 4 w e k n o w t h a t b o t h x
1
a n d x
2
h a v e a t
l e a s t o n e b r o t h e r . I f x
1
a n d x
2
a r e n o t b r o t h e r s a s s u m e
w . l . o . g . t h a t p ( x
1
) p ( x
2
) a n d s w a p t h e s u b t r e e s r o o t e d
a t x
1
a n d a t a b r o t h e r o f x
2
. T h e n u m b e r o f v i o l a t i o n s
c a n o n l y d e c r e a s e b y d o i n g t h i s s w a p . W e c a n n o w
w . l . o . g . a s s u m e t h a t x
1
a n d x
2
a r e b r o t h e r s a n d b o t h
s o n s o f n o d e y
I f x
1
h a s m o r e t h a n o n e b r o t h e r o f r a n k k w e j u s t
c u t o x
1
a n d m a k e i t a g o o d s o n o f t
1
a s d e s c r i b e d
i n S e c t i o n 4 . 2 . B e c a u s e x
1
h a d a t l e a s t t w o b r o t h e r s o f
r a n k k , S 4 i s s t i l l s a t i s e d a t y
I n c a s e x
1
a n d x
2
a r e t h e o n l y b r o t h e r s o f r a n k k
a n d r ( y ) > k + 1 w e j u s t c u t o b o t h x
1
a n d x
2
a n d m a k e
t h e m n e w g o o d s o n s o f t
1
a s d e s c r i b e d i n S e c t i o n 4 . 2 .
B e c a u s e o f i n v a r i a n t S 4 w e a r e f o r c e d t o c u t o b o t h
s o n s .
T h e o n l y c a s e t h a t r e m a i n s t o b e c o n s i d e r e d i s
w h e n x
1
a n d x
2
a r e t h e o n l y s o n s o f r a n k k a n d t h a t
r ( y ) = k + 1 . I n t h i s c a s e w e c u t o x
1
x
2
a n d y . T h e
n e w r a n k o f y i s u n i q u e l y g i v e n b y o n e p l u s t h e r a n k o f
i t s n e w l e f t m o s t s o n . W e r e p l a c e y b y a s o n o f t
1
o f r a n k
k + 1 w h i c h c a n b e c u t o a s d e s c r i b e d i n S e c t i o n 4 . 2 .
I f y w a s a s o n o f t
1
w e o n l y c u t o y . I f t h e r e p l a c e m e n t
f o r y b e c o m e s a v i o l a t i n g n o d e o f r a n k k + 1 w e a d d i t
t o W ( t
1
) . F i n a l l y , x
1
x
2
a n d y a r e m a d e g o o d s o n s o f
t
1
a s d e s c r i b e d i n S e c t i o n 4 . 2 .
8/7/2019 Worst Case Efficient Priority Queues
6/7
6 G e r t h S t l t i n g B r o d a l
A b o v e i t i s i m p o r t a n t t h a t t h e n o d e y i s r e p l a c e d
b y i s n o t a n a n c e s t o r o f y , b e c a u s e i f y w a s r e p l a c e d b y
s u c h a n o d e a c y c l e a m o n g t h e p a r e n t p o i n t e r s w o u l d
b e c r e a t e d . I n v a r i a n t S 2 g u a r a n t e e s t h a t t h i s c a n n o t
h a p p e n .
4 . 4 A v o i d i n g t o o m a n y v i o l a t i o n s . W e n o w d e -
s c r i b e h o w t o a v o i d t o o m a n y v i o l a t i o n s . T h e o n l y v i -
o l a t i o n s e t s w e a d d v i o l a t i o n s t o a r e V ( t
1
) a n d W ( t
1
)
V i o l a t i o n s o f r a n k l a r g e r t h a n r ( t
1
) a r e a d d e d t o V ( t
1
)
a n d o t h e r w i s e t h e y a r e a d d e d t o W ( t
1
) . T h e v i o l a t i o n s
i n W ( t
1
) a r e c o n t r o l l e d b y a g u i d e . T h i s g u i d e g u a r a n -
t e e s t h a t w
i
( t
1
) 6 . W e m a i n t a i n e d a s i n g l e a r r a y s o
w e c o u l d a c c e s s t h e v i o l a t i n g n o d e s i n W ( t
1
) b y t h e i r
r a n k .
I f w e a d d a v i o l a t i o n t o W ( t
1
) t h e g u i d e t e l l s
u s f o r w h i c h r a n k s w e s h o u l d d o v i o l a t i o n r e d u c i n g
t r a n s f o r m a t i o n s a s d e s c r i b e d i n t h e p r e v i o u s s e c t i o n .
W e o n l y d o t h e t r a n s f o r m a t i o n i f t h e r e a r e e x a c t l y s i x
v i o l a t i o n s o f t h e g i v e n r a n k a n d t h a t t h e r e i s a t l e a s t
t w o v i o l a t i n g n o d e s w h i c h a r e n o t s o n s o f t
2
. I f t h e r e
a r e m o r e t h a n f o u r v i o l a t i o n s t h a t a r e s o n s o f t
2
w e c u t
t h e a d d i t i o n a l v i o l a t i o n s o a n d l i n k s t h e m b e l o w t
1
T h i s m a k e s t h e s e n o d e s g o o d a n d d o e s n o t a e c t t h e
g u i d e s m a i n t a i n i n g t h e s o n s a t t
2
F o r e a c h p r i o r i t y q u e u e o p e r a t i o n t h a t i s p e r f o r m e d
w e i n c r e a s e t h e r a n k o f t
1
b y a t l e a s t o n e b y m o v i n g
a c o n s t a n t n u m b e r o f s o n s f r o m t
2
t o t
1
| p r o v i d e d
T
2
6= ; . B y i n c r e a s i n g t h e r a n k o f t
1
b y o n e w e c a n
a o r d c r e a t i n g n e w v i o l a t i o n s o f r a n k l a r g e r t h a n r ( t
1
)
b y i n v a r i a n t O 5 a n d w e c a n j u s t a d d t h e v i o l a t i o n s t o
t h e l i s t V ( t
1
) . I f T
2
6= ; a n d r ( t
2
) r ( t
1
) + 2 w e j u s t
c u t o f t h e l a r g e s t s o n s o f t
2
a n d l i n k t h e m b e l o w t
1
a n d
n a l l y a d d t
2
b e l o w t
1
. T h i s w i l l s a t i s f y t h e i n v a r i a n t s .
O t h e r w i s e w e c u t o a s o n o f t
2
o f r a n k r ( t
1
) + 2 a n d
d e l i n k t h i s s o n a n d a d d t h e r e s u l t i n g t r e e s b e l o w t
1
s u c h
t h a t t h e r a n k o f t
1
i n c r e a s e s b y a t l e a s t o n e . B y c h o o s i n g
l a r g e e n o u g h t h e i n v a r i a n t s w i l l b e c o m e r e e s t a b l i s h e d .
I f T
2
i s e m p t y w e c a n n o t i n c r e a s e t h e r a n k t
1
, b u t
t h i s a l s o i m p l i e s t h a t t
1
i s t h e n o d e o f m a x i m u m r a n k
s o n o l a r g e v i o l a t i o n s c a n b e c r e a t e d a n d R 2 c a n n o t
b e c o m e v i o l a t e d .
4 . 5 P r i o r i t y q u e u e o p e r a t i o n s . I n t h e f o l l o w i n g w e
d e s c r i b e h o w t o i m p l e m e n t t h e d i e r e n t p r i o r i t y q u e u e
o p e r a t i o n s s u c h t h a t t h e i n v a r i a n t s f r o m S e c t i o n 2 a r e
m a i n t a i n e d .
M a k e Q u e u e i s t r i v i a l . W e r e t u r n a p a i r o f e m p t y
t r e e s .
F i n d M i n ( Q ) r e t u r n s t
1
I n s e r t ( Q e ) i s a s p e c i a l c a s e o f M e l d w h e r e Q
2
i s
a p r i o r i t y q u e u e o n l y c o n t a i n i n g o n e e l e m e n t .
M e l d ( Q
1
Q
2
) i n v o l v e s a t m o s t f o u r t r e e s ; t w o f o r
e a c h q u e u e . T h e t r e e h a v i n g t h e n e w m i n i m u m
e l e m e n t a s r o o t b e c o m e s t h e n e w T
1
t r e e . T h i s
t r e e w a s e i t h e r t h e T
1
t r e e o f Q
1
o r o f Q
2
. I f t h i s
t r e e i s t h e t r e e o f m a x i m u m r a n k w e j u s t a d d t h e
o t h e r t r e e s b e l o w t h i s t r e e a s d e s c r i b e d p r e v i o u s l y .
I n t h i s c a s e n o v i o l a t i n g n o d e i s c r e a t e d s o n o
t r a n s f o r m a t i o n i s d o n e o n t h e v i o l a t i n g n o d e s .
O t h e r w i s e t h e t r e e o f m a x i m u m r a n k b e c o m e s t h e
n e w T
2
t r e e a n d t h e r e m a i n i n g t r e e s a r e a d d e d
b e l o w t h i s n o d e a s d e s c r i b e d i n S e c t i o n 4 . 2 , p o s s i b l y
d e l i n k i n g t h e n e w s o n s o n c e i f t h e y h a v e t h e s a m e
r a n k a s t
2
. T h e v i o l a t i o n s c r e a t e d b y t h i s a r e
t r e a t e d a s d e s c r i b e d i n S e c t i o n 4 . 4 . T h e g u i d e s a n d
a r r a y s u s e d a t t h e o l d r o o t s t h a t n o w a r e l i n k e d
b e l o w t h e n e w t
2
n o d e w e j u s t d i s c a r d .
D e c r e a s e K e y ( Q ; e ; e ) r e p l a c e s t h e e l e m e n t o f e
b y e ( e e ) . I f e i s l e s s t h a n t
1
w e s w a p
t h e e l e m e n t s i n t h e t w o n o d e s . I f e i s a g o o d
n o d e w e s t o p , o t h e r w i s e w e p r o c e e d a s d e s c r i b e d
i n S e c t i o n 4 . 4 t o a v o i d h a v i n g t o o m a n y v i o l a t i o n s
s t o r e d a t t
1
D e l e t e M i n ( Q ) i s a l l o w e d t o t a k e w o r s t c a s e t i m e
O ( l o g n ) . F i r s t T
2
i s m a d e e m p t y b y m o v i n g a l l
s o n s o f T
2
t o T
1
a n d m a k i n g t h e r o o t t
2
a r a n k
z e r o s o n o f t
1
. T h e n t
1
i s d e l e t e d . T h i s g i v e s u s a t
m o s t O ( l o g n ) i n d e p e n d e n t t r e e s . T h e m i n i m u m
e l e m e n t i s t h e n f o u n d b y l o o k i n g a t t h e s e t s V
a n d W o f t h e o l d r o o t o f T
1
a n d a l l t h e r o o t s o f
t h e i n d e p e n d e n t t r e e s . I f t h e m i n i m u m e l e m e n t i s
n o t a r o o t w e s w a p i t w i t h o n e o f t h e i n d e p e n d e n t
t r e e s o f e q u a l r a n k . T h i s a t m o s t c r e a t e s o n e n e w
v i o l a t i o n . B y m a k i n g t h e i n d e p e n d e n t t r e e s s o n s o f
t h e n e w m i n i m u m e l e m e n t a n d p e r f o r m i n g O ( l o g n )
l i n k i n g a n d d e l i n k i n g o p e r a t i o n s o n t h e s e s o n s w e
c a n r e e s t a b l i s h S 1 { S 5 a n d R 1 a n d R 3 . B y m e r g i n g
t h e V a n d W s e t s a t t h e r o o t t o o n e s e t a n d m e r g i n g
t h e o l d m i n i m u m e l e m e n t ' s V a n d W s e t s w i t h
t h e s e t w e g e t o n e n e w s e t o f v i o l a t i o n s o f s i z e
O ( l o g n ) . P o s s i b l y w e a l s o h a v e t o a d d t h e s i n g l e
v i o l a t i o n c r e a t e d b y t h e s w a p p i n g . B y d o i n g a t
m o s t O ( l o g n ) v i o l a t i o n r e d u c i n g t r a n s f o r m a t i o n s
a s d e s c r i b e d p r e v i o u s l y w e c a n r e d u c e t h e s e t t o
c o n t a i n a t m o s t o n e v i o l a t i o n o f e a c h r a n k . W e
m a k e t h e r e s u l t i n g s e t t h e n e w W s e t o f t h e
n e w r o o t a n d l e t t h e c o r r e s p o n d i n g V s e t b e
e m p t y . T h i s i m p l i e s t h a t O 1 { O 5 a n d R 2 a r e b e i n g
r e e s t a b l i s h e d . T h e g u i d e s i n v o l v e d a r e i n i t i a t e d
a c c o r d i n g t o t h e n e w s i t u a t i o n a t t h e r o o t o f T
1
D e l e t e ( Q e ) . I f w e l e t 1 d e n o t e t h e s m a l l -
e s t p o s s i b l e e l e m e n t , t h e n D e l e t e c a n b e i m p l e -
m e n t e d a s D e c r e a s e K e y ( Q ; e ; 1 ) f o l l o w e d b y
D e l e t e M i n ( Q )
8/7/2019 Worst Case Efficient Priority Queues
7/7
W o r s t - C a s e E c i e n t P r i o r i t y Q u e u e s 7
5 I m p l e m e n t a t i o n d e t a i l s
I n t h i s s e c t i o n w e s u m m a r i z e t h e r e q u i r e d d e t a i l s o f o u r
n e w d a t a s t r u c t u r e .
E a c h n o d e w e r e p r e s e n t b y a r e c o r d h a v i n g t h e
f o l l o w i n g e l d s .
T h e e l e m e n t a s s o c i a t e d w i t h t h e n o d e ,
t h e r a n k o f t h e n o d e ,
p o i n t e r s t o t h e n o d e ' s l e f t a n d r i g h t b r o t h e r s ,
a p o i n t e r t o t h e f a t h e r n o d e ,
a p o i n t e r t o t h e l e f t m o s t s o n ,
p o i n t e r s t o t h e r s t n o d e i n t h e n o d e ' s V a n d W
s e t s , a n d
p o i n t e r s t o t h e n e x t a n d t h e p r e v i o u s n o d e i n
t h e v i o l a t i o n l i s t t h a t t h e n o d e b e l o n g s t o . T h e
r s t n o d e i n a v i o l a t i o n l i s t V ( x ) o r W ( x ) h a s i t s
p r e v i o u s v i o l a t i o n p o i n t e r p o i n t i n g t o x
I n a d d i t i o n t o t h e a b o v e n o d e s w e m a i n t a i n t h e
f o l l o w i n g t h r e e e x t e n d i b l e a r r a y s :
A n a r r a y o f p o i n t e r s t o s o n s o f t
1
o f r a n k i =
0 ; : : : ; r ( t
1
) 1
a s i m i l a r a r r a y f o r t
2
, a n d
a n a r r a y o f p o i n t e r s t o n o d e s i n W ( t
1
) o f r a n k
i = 0 ; : : : ; r ( t
1
) 1 ( i f n o n o d e i n W ( t
1
) e x i s t o f
a g i v e n r a n k w e l e t t h e c o r r e s p o n d i n g p o i n t e r b e
n u l l )
F i n a l l y w e h a v e v e g u i d e s : T h r e e t o m a i n t a i n t h e
u p p e r b o u n d s o n n
i
( t
1
) n
i
( t
2
) a n d w
i
( t
1
) a n d t w o t o
m a i n t a i n t h e l o w e r b o u n d s o n n
i
( t
1
) a n d n
i
( t
2
)
6 C o n c l u s i o n
F r o m t h e c o n s t r u c t i o n p r e s e n t e d i n t h e p r e v i o u s s e c t i o n s
w e c o n c l u d e t h a t :
T h e o r e m 6 . 1 . A n i m p l e m e n t a t i o n o f p r i o r i t y q u e -
u e s e x i s t s t h a t s u p p o r t s t h e o p e r a t i o n s M a k e Q u e u e
F i n d M i n I n s e r t M e l d a n d D e c r e a s e K e y i n w o r s t
c a s e t i m e O ( 1 ) a n d D e l e t e M i n a n d D e l e t e i n w o r s t
c a s e t i m e O ( l o g n ) . T h e s p a c e r e q u i r e d i s l i n e a r i n t h e
n u m b e r o f e l e m e n t s c o n t a i n e d i n t h e p r i o r i t y q u e u e s .
T h e d a t a s t r u c t u r e p r e s e n t e d i s q u i t e c o m p l i c a t e d .
A n i m p o r t a n t i s s u e f o r f u r t h e r w o r k i s t o s i m p l i f y t h e
c o n s t r u c t i o n t o m a k e i t a p p l i c a b l e i n p r a c t i c e . I t w o u l d
a l s o b e i n t e r e s t i n g t o s e e i f i t i s p o s s i b l e t o r e m o v e t h e
r e q u i r e m e n t f o r a r r a y s f r o m t h e c o n s t r u c t i o n .
A c k n o w l e d g e m e n t
T h e a u t h o r t h a n k s R o l f F a g e r b e r g f o r t h e l o n g d i s c u s -
s i o n s t h a t l e a d t o t h e r e s u l t s p r e s e n t e d i n t h e p a p e r .
R e f e r e n c e s
1 ] G e r t h S t l t i n g B r o d a l . F a s t m e l d a b l e p r i o r i t y q u e u e s .
I n P r o c . 4 t h W o r k s h o p o n A l g o r i t h m s a n d D a t a S t r u c -
t u r e s ( W A D S ) , v o l u m e 9 5 5 o f L e c t u r e N o t e s i n C o m -
p u t e r S c i e n c e , p a g e s 2 8 2 { 2 9 0 . S p r i n g e r V e r l a g , B e r l i n ,
1 9 9 5 .
2 ] S v a n t e C a r l s s o n , P a t r i c i o V . P o b l e t e , a n d J . I a n
M u n r o . A n i m p l i c i t b i n o m i a l q u e u e w i t h c o n s t a n t
i n s e r t i o n t i m e . I n P r o c . 1 s t S c a n d i n a v i a n W o r k s h o p
o n A l g o r i t h m T h e o r y ( S W A T ) , v o l u m e 3 1 8 o f L e c t u r e
N o t e s i n C o m p u t e r S c i e n c e , p a g e s 1 { 1 3 . S p r i n g e r V e r -
l a g , B e r l i n , 1 9 8 8 .
3 ] J a m e s R . D r i s c o l l , H a r o l d N . G a b o w , R u t h S h r a i r m a n ,
a n d R o b e r t E . T a r j a n . R e l a x e d h e a p s : A n a l t e r n a t i v e
t o b o n a c c i h e a p s w i t h a p p l i c a t i o n s t o p a r a l l e l c o m -
p u t a t i o n . C o m m u n i c a t i o n s o f t h e A C M , 3 1 ( 1 1 ) : 1 3 4 3 {
1 3 5 4 , 1 9 8 8 .
4 ] R o l f F a g e r b e r g . A g e n e r a l i z a t i o n o f b i n o m i a l q u e u e s .
T e c h n i c a l R e p o r t I M A D A - 9 4 - 3 5 , O d e n s e U n i v e r s i t y ,
1 9 9 4 . T o a p p e a r i n I n f o r m a t i o n P r o c e s s i n g L e t t e r s .
5 ] R o b e r t W . F l o y d . A l g o r i t h m 2 4 5 : T r e e s o r t 3 . C o m m u -
n i c a t i o n s o f t h e A C M , 7 ( 1 2 ) : 7 0 1 , 1 9 6 4 .
6 ] M i c h a e l L . F r e d m a n , R o b e r t S e d g e w i c k , D a n i e l D .
S l e a t o r , a n d R o b e r t E . T a r j a n . T h e p a i r i n g h e a p : A
n e w f o r m o f s e l f { a d j u s t i n g h e a p . A l g o r i t h m i c a , 1 : 1 1 1 {
1 2 9 , 1 9 8 6 .
7 ] M i c h a e l L . F r e d m a n a n d R o b e r t E n d r e T a r j a n . F i -
b o n a c c i h e a p s a n d t h e i r u s e s i n i m p r o v e d n e t w o r k o p -
t i m i z a t i o n a l g o r i t h m s . I n P r o c . 2 5 r d A n n . S y m p . o n
F o u n d a t i o n s o f C o m p u t e r S c i e n c e ( F O C S ) , p a g e s 3 3 8 {
3 4 6 , 1 9 8 4 .
8 ] L e o J . G u i b a s , E d w a r d M . M c C r e i g h t , M i c h a e l F .
P l a s s , a n d J a n e t R . R o b e r t s . A n e w r e p r e s e n t a t i o n f o r
l i n e a r l i s t s . I n P r o c . 9 t h A n n . A C M S y m p . o n T h e o r y
o f C o m p u t i n g ( S T O C ) , p a g e s 4 9 { 6 0 , 1 9 7 7 .
9 ] H a i m K a p l a n a n d R o b e r t T a r j a n . P e r s i s t e n t l i s t s w i t h
c a t e n a t i o n v i a r e c u r s i v e s l o w - d o w n . I n P r o c . 2 7 t h A n n .
A C M S y m p . o n T h e o r y o f C o m p u t i n g ( S T O C ) , p a g e s
9 3 { 1 0 2 , 1 9 9 5 .
1 0 ] D o n a l d E . K n u t h . T h e A r t o f C o m p u t e r P r o g r a m m i n g ,
V o l u m e I I I : S o r t i n g a n d S e a r c h i n g . A d d i s o n - W e s l e y ,
R e a d i n g , M A , 1 9 7 3 .
1 1 ] K u r t M e h l h o r n a n d A t h a n a s i o s K . T s a k a l i d i s . D a t a
s t r u c t u r e s . I n J . v a n L e e u w e n , e d i t o r , H a n d b o o k o f
T h e o r e t i c a l C o m p u t e r S c i e n c e , V o l u m e A : A l g o r i t h m s
a n d C o m p l e x i t y . M I T P r e s s / E l s e v i e r , 1 9 9 0 .
1 2 ] J o r g - R . S a c k a n d T h o m a s S t r o t h o t t e . A n a l g o r i t h m f o r
m e r g i n g h e a p s . A C T A I n f o r m a t i c a , 2 2 : 1 7 1 { 1 8 6 , 1 9 8 5 .
1 3 ] R o b e r t E n d r e T a r j a n . D a t a S t r u c t u r e s a n d N e t -
w o r k A l g o r t i h m s . S o c i e t y f o r I n d u s t r i a l a n d A p p l i e d
M a t e h m a t i c s , P h i l a d e l p h i a , P e n n s y l v a n i a , 1 9 8 3 .
1 4 ] R o b e r t E n d r e T a r j a n . A m o r t i z e d c o m p u t a t i o n a l c o m -
p l e x i t y . S I A M J o u r n a l o n A l g e b r a i c a n d D i s c r e t e
M e t h o d s , 6 : 3 0 6 { 3 1 8 , 1 9 8 5 .
1 5 ] J a n v a n L e e u w e n . T h e c o m p o s i t i o n o f f a s t p r i o r i t y
q u e u e s . T e c h n i c a l R e p o r t R U U - C S - 7 8 - 5 , D e p a r t m e n t
o f C o m p u t e r S c i e n c e , U n i v e r s i t y o f U t r e c h t , 1 9 7 8 .
1 6 ] J e a n V u i l l e m i n . A d a t a s t r u c t u r e f o r m a n i p u l a t i n g p r i -
o r i t y q u e u e s . C o m m u n i c a t i o n s o f t h e A C M , 2 1 ( 4 ) : 3 0 9 {
3 1 5 , 1 9 7 8 .
1 7 ] J . W . J . W i l l i a m s . A l g o r i t h m 2 3 2 : H e a p s o r t . C o m m u -
n i c a t i o n s o f t h e A C M , 7 ( 6 ) : 3 4 7 { 3 4 8 , 1 9 6 4 .