CSE 41 Algorihms ad Daa Srucures 10/14/015 Skip Liss This hadou gives he skip lis mehods ha we discussed i class. A skip lis is a ordered, doublyliked lis wih some exra poiers ha allow us o jump over muliple odes a oce whe raversig he lis i order. For a complee descripio of skip liss wih some aalysis, see he paper o he subjec by heir iveor, Bill Pugh, i Commuicaios of he ACM 33(6):668 76, Jue 1990. Each ode i a skip lis has a key ad a pillar of some heigh (which varies amog odes). A pillar is a array coaiig pairs of ex ad prev poiers. Every pillar of heigh a leas l is hreaded io a ordered liked lis by he poiers a level l. A skip lis coais wo guard pillars, he head ad ail, which have key values ad + respecively. These pillars are a leas as all as ay ohers i he lis ad so form he edpois of he liked liss a every level. Pillar heighs are chose radomly a he ime of ode iserio from a geomeric disribuio wih parameer p = 1/. This disribuio is characerized by Pr[heigh = ] = ( ) 1 for all 1. You ca geerae geomerically disribued radom umbers by, e.g., repeaedly flippig a ubiased coi ad couig he umber of flips eeded o obai he firs head. Alhough pillars ca i heory be arbirarily all, he probabiliy of creaig a very all pillar is exremely small; for example, here is less ha oe chace i a millio ha a give pillar will have heigh above 0. I he followig code, he procedure RadomHeigh() reurs a heigh accordig o he above geomeric disribuio. The oaio x[l].ex refers o he ex poier for he liked lis a level l of x s pillar. 1 Trivial Operaios The mi, max, succ, ad pred operaios o skip liss are rivial (as hey are for ay ordered lis). Successor ad predecessor ca be obaied for ay ode by followig he ex ad prev liks a he boom (level 0) of is pillar. The mi ad max odes are respecively he successor of he head ad he predecessor of he ail. I is sraighforward o recogize ha, e.g., succ has reached he ed of he lis because i will ecouer he ail ode wih value +. Assumig we keep poiers o he lis head ad ail, all of hese operaios are Θ(1). Deleig a ode x i a skip lis requires ha we ulik is pillar from he liked liss a every level. The pseudocode for his operaio is: Remove(x) for l i 0... x.heigh 1 do splice x ou of liked lis a level l If he pillar o be deleed is of heigh, Remove requires ime Θ(). 1
Norivial Operaios The wo ieresig operaios o a skip lis are Fid ad Iser. I will ake some aalysis o show ha he expeced cos of hese operaios is O(log ); for ow, we jus give he pseudocode. The Fid operaio raverses he lis, as you migh expec, bu i ries o jump over as may odes as possible wih each sep. Liks a higher levels usually jump over more odes ha hose a lower levels (because aller pillars occur less ofe), so we jump as far as we ca a each level before descedig o he level below i. Fid(k) l head.heigh 1 x head while l 0 do y x[l].ex if y.key = k reur y else if y.key < k x y else l reur ull Iserio is oly slighly more ieresig. We eed o iser he ewly allocaed pillar a is correc place i he liked liss for every level. While we could raverse each liked lis separaely sarig from he head ode o fid he righ iserio poi, we ake advaage of he fac ha if a ew ode z should be isered afer a ode x a level l, he z mus also appear afer (hough perhaps o immediaely afer) x a every level below l. Noe ha we should sar he iserio, like he fid, from he op level of he skip lis, o from level 1; oherwise, whe = 1 (half he ime o average), we ed up raversig Θ() odes a level 0 o do he iserio. Iser(z) RadomHeigh allocae a pillar of heigh for z l head.heigh 1 x head while l 0 do y x[l].ex if y.key < z.key x y else if l < lik z io lis a level l bewee x ad y l Oe slighly ricky deail of iserio is ha we migh allocae a ew pillar larger ha he head ad ail pillars. Whe his happes, we ca simply icrease he heigh of he head ad ail o accommodae he ew ode. As for resizable hash ables, i ca be show ha if we double he head ad ail heigh each ime a
icrease is eeded, he cos of relikig he ew pillars io he liss a every level is amorized over a large umber of iserios. 3 Performace Aalysis The performace aalysis of skip liss depeds heavily o he fac ha heir pillar heighs are chose from a geomeric disribuio. There are wo higs o show. Firs, he alles pillar heigh, which upper-bouds he cos of deleio, is oo big; i paricular, i s very likely o be O(log ) for a skip lis of elemes. Secod, a fid operaio i a skip lis o average ispecs a cosa umber of keys per level of he lis; hece, he overall umber of key ispecios per search is also O(log ) o average. 3.1 Average Heigh of Talles Pillar The followig proof of logarihmic expeced heigh is much more precise (ad more complex) ha wha we did i class. I class, we merely showed ha, for some cosa c, pillars of heigh < c log were very likely, while hose of heigh > c log were very ulikely, suggesig ha he ippig poi bewee he wo regimes is he ypical behavior. Le H be he heigh of he alles pillar i a skip lis wih elemes (excludig he head ad ail). Firs, i is o hard o see ha ( ) 1 Pr(H > ). To see his, cosider ha he probabiliy ha H > is he probabiliy ha a leas oe pillar i he lis has heigh >. The laer is upper-bouded by imes he probabiliy ha ay sigle pillar has heigh >. We ow wish o argue ha E[H] = O(log ). By defiiio of expecaio, we have E[H] = Pr(H = ). We spli his sum io several pars, each of which ca be bouded idepedely: E[H] = log =1 For he firs par, we may wrie log =1 For he secod par, we have ha Pr(H = ) + = log +1 =1 = log +1 Pr(H = ) + log Pr(H = ) log log 1. Pr(H = ) = 1. 3 =1 = log +1 Pr(H = ) Pr(H = ) Pr(H > log ) ( ) ( log ) Pr(H = ). (1)
Here, we ve simply plugged i our origial boud o Pr(H > ). Fially, for he hird par, we use he followig sum formula for x < 1: x = x [ ] ( + 1)x x+1 1 x 1 x 1 () (You ca derive his formula yourself i he same way as he formula for x see Appedix A of your ex.) We have ha Pr(H = ) = Pr(H > 1) [ ) ( 1 ( ) ] 1. The hird sep uses our origial upper boud for Pr(H > ), while he fourh uses he fac ha x = x (1 x). (Agai, see Appedix A of your ex.) Pluggig x = 1 io Equaio (), we fid ha ( ) 1 = ( ) 1. Coclude ha [ ( ( ) 1 )] Pr(H = ) ) = ( 1 = o(1). Combiig our bouds for all hree erms of he sum i (1), we coclude ha implyig ha E[H] = O(log ). E[H] log + 1 + o(1), 3. Cosa Expeced Search Time Per Level Cosider a fid operaio i a skip lis. The fid fucio ca be broke dow io a series of raversals a differe levels l of he lis, sarig wih he firs key ispeced a level l ad edig whe a key ispecio causes us o go dow isead of forward. How may keys are ispeced o average a level l? To aalyze his siuaio, we sar by observig ha every key ispeced a level l, excep possibly he las, belogs o a pillar of heigh exacly l + 1 (ha is, a pillar whose maximum level is l). Suppose o, i.e. suppose ha some key k ha is o he las has a associaed pillar p of high > l + 1. If k is he argume o he fid fucio, we kow ha k < k, sice oherwise he level s raversal would ermiae a or before 4
k. Hece, he search will pass pillar p a level l. Bu his meas ha he search would have passed pillar p a a higher level (say, l + 1) as well, sice p is i higher-level chais ad we ever move backwards durig a raversal. Hece, we would o acually ispec p s key a level l. Coclude ha our supposiio is false, ad p mus have heigh exacly l + 1. We ca ow boud he expeced legh of he raversal a level l by askig: how may pillars of heigh exacly l + 1 do you expec o raverse before ecouerig a pillar of heigh > l + 1? Each pillar a level l is geeraed idepedely a radom from a geomeric disribuio wih parameer 1/, ad all have heigh a leas = l + 1. For each such pillar, he chace ha is heigh h exceeds is give by Pr(h > h ) = 1 Pr(h = h ) = 1 1/ = 1/. To see he 1/, cosider ha he decisio o wheher o sop he pillar a heigh or coiue o grow i correspods o a sigle, ubiased coi flip. Now he expeced raversal legh is oe plus he expeced umber E[ ] of pillars of heigh exacly prior o he firs pillar of heigh >. We have probabiliy 1/ ha = 0, i.e. ha we immediaely exceed heigh o he firs ew key. Similarly, = 1 wih probabiliy 1/4, wih probabiliy 1/8, ad so forh. Coclude ha ( ) 1 +1 E[ ] = = 1 ( ) 1 = 1 1/ (1 1/) = 1 1/ (1 1/) = 1. Hece, if we iclude he key ha ermiaes he raversal a level l, he average legh of his raversal is 1 + 1 =. Sice we expec he umber of levels raversed o be O(log ), he oal expeced umber of key ispecios is also O(log ). 5