Presetatio for use with the textbook, Algorithm Desig ad Applicatios, by M. T. Goodrich ad R. Tamassia, Wiley, 2015 Skip Lists S 3 15 15 23 10 15 23 36 Skip Lists 1
What is a Skip List A skip list for a set S of distict (key, elemet) items is a series of lists,,, S h such that Each list S i cotais the special keys ad List cotais the keys of S i odecreasig order Each list is a subseuece of the previous oe, i.e., S h List S h cotais oly the two special keys We show how to use a skip list to implemet the map ADT S 3 31 23 31 34 64 12 23 26 31 34 44 56 64 78 Skip Lists 2
Search We search for a key x i a a skip list as follows: We start at the first positio of the top list At the curret positio p, we compare x with y key(ext(p)) x = y: we retur elemet(ext(p)) x > y: we sca forward x < y: we drop dow If we try to drop dow past the bottom list, we retur ull Example: search for 78 S 3 sca forward 31 drop dow 23 31 34 64 12 23 26 31 34 44 56 64 78 Skip Lists 3
Radomized Algorithms A radomized algorithm performs coi tosses (i.e., uses radom bits) to cotrol its executio It cotais statemets of the type b radom() if b = 0 do A else { b = 1} do B Its ruig time depeds o the outcomes of the coi tosses We aalyze the expected ruig time of a radomized algorithm uder the followig assumptios the cois are ubiased, ad the coi tosses are idepedet The worst-case ruig time of a radomized algorithm is ofte large but has very low probability (e.g., it occurs whe all the coi tosses give heads ) We use a radomized algorithm to isert items ito a skip list Skip Lists 4
Isertio To isert a etry (x, o) ito a skip list, we use a radomized algorithm: We repeatedly toss a coi util we get tails, ad we deote with i the umber of times the coi came up heads If i h, we add to the skip list ew lists S h+1,, S i +1, each cotaiig oly the two special keys We search for x i the skip list ad fid the positios p 0, p 1,, p i of the items with largest key less tha x i each list,,, S i For j 0,, i, we isert item (x, o) ito list S j after positio p j Example: isert key 15, with i = 2 p 2 p 1 p 0 10 23 36 23 S 3 15 15 23 10 15 23 36 Skip Lists 5
Deletio To remove a etry with key x from a skip list, we proceed as follows: We search for x i the skip list ad fid the positios p 0, p 1,, p i of the items with key x, where positio p j is i list S j We remove positios p 0, p 1,, p i from the lists,,, S i We remove all but oe list cotaiig oly the two special keys Example: remove key 34 S 3 p 2 34 p 1 23 34 p0 23 12 23 34 45 12 23 45 Skip Lists 6
Implemetatio We ca implemet a skip list with uad-odes A uad-ode stores: etry lik to the ode prev lik to the ode ext lik to the ode below lik to the ode above Also, we defie special keys PLUS_INF ad MINUS_INF, ad we modify the key comparator to hadle them uad-ode x Skip Lists 7
Space Usage The space used by a skip list depeds o the radom bits used by each ivocatio of the isertio algorithm We use the followig two basic probabilistic facts: Fact 1: The probability of gettig i cosecutive heads whe flippig a coi is 1/2 i Fact 2: If each of etries is preset i a set with probability p, the expected size of the set is p Cosider a skip list with etries By Fact 1, we isert a etry i list S i with probability 1/2 i By Fact 2, the expected size of list S i is /2 i The expected umber of odes used by the skip list is h i= 0 h 1 = 2 i < i 2 2 i= 0 Thus, the expected space usage of a skip list with items is O() Skip Lists 8
Height The ruig time of the search a isertio algorithms is affected by the height h of the skip list We show that with high probability, a skip list with items has height O(log ) We use the followig additioal probabilistic fact: Fact 3: If each of evets has probability p, the probability that at least oe evet occurs is at most p Cosider a skip list with etires By Fact 1, we isert a etry i list S i with probability 1/2 i By Fact 3, the probability that list S i has at least oe item is at most /2 i By pickig i = 3log, we have that the probability that S 3log has at least oe etry is at most /2 3log = / 3 = 1/ 2 Thus a skip list with etries has height at most 3log with probability at least 1-1/ 2 Skip Lists 9
Search ad Update Times The search time i a skip list is proportioal to the umber of drop-dow steps, plus the umber of sca-forward steps The drop-dow steps are bouded by the height of the skip list ad thus are O(log ) with high probability To aalyze the sca-forward steps, we use yet aother probabilistic fact: Fact 4: The expected umber of coi tosses reuired i order to get tails is 2 Whe we sca forward i a list, the destiatio key does ot belog to a higher list A sca-forward step is associated with a former coi toss that gave tails By Fact 4, i each list the expected umber of scaforward steps is 2 Thus, the expected umber of sca-forward steps is O(log ) We coclude that a search i a skip list takes O(log ) expected time The aalysis of isertio ad deletio gives similar results Skip Lists 10
Summary A skip list is a data structure for maps that uses a radomized isertio algorithm I a skip list with etries The expected space used is O() The expected search, isertio ad deletio time is O(log ) Usig a more complex probabilistic aalysis, oe ca show that these performace bouds also hold with high probability Skip lists are fast ad simple to implemet i practice Skip Lists 11