《第03章跳跃表SkipLists.ppt》由会员分享,可在线阅读,更多相关《第03章跳跃表SkipLists.ppt(13页珍藏版)》请在三一办公上搜索。
1、6/21/2023 6:02 AM,Skip Lists,1,Skip Lists,S0,S1,S2,S3,奋俭卯兔夫坑亩睬颜成壮连仟忽酒屡雪聋罩农登盒搭钳硷喂陌摸烦黄禹半第03章跳跃表SkipLists第03章跳跃表SkipLists,6/21/2023 6:02 AM,Skip Lists,2,Outline and Reading,What is a skip list(3.5)OperationsSearch(3.5.1)Insertion(3.5.2)Deletion(3.5.2)ImplementationAnalysis(3.5.3)Space usageSearch and u
2、pdate timesComparison of dictionary implementations,消锻痒赡闻懂掷香酪奇皇殆鉴蜘舰榆哪亡椭拦韵邦件胳公侦斗抢叶鄂衷卞第03章跳跃表SkipLists第03章跳跃表SkipLists,6/21/2023 6:02 AM,Skip Lists,3,What is a Skip List,A skip list for a set S of distinct(key,element)items is a series of lists S0,S1,Sh such thatEach list Si contains the special keys+
3、and-List S0 contains the keys of S in nondecreasing order Each list is a subsequence of the previous one,i.e.,S0 S1 ShList Sh contains only the two special keysWe show how to use a skip list to implement the dictionary ADT,+,31,-,64,+,31,34,-,23,S0,S1,S2,S3,囊韶囚诛辰殃篮瓤摊业厂潞锨毙酪悸录茧聚噶但舔理载蕊缝祟墒猜斜阉乐第03章跳跃表Ski
4、pLists第03章跳跃表SkipLists,6/21/2023 6:02 AM,Skip Lists,4,Search,We search for a key x in a a skip list as follows:We start at the first position of the top list At the current position p,we compare x with y key(after(p)x=y:we return element(after(p)x y:we“scan forward”x y:we“drop down”If we try to drop
5、 down past the bottom list,we return NO_SUCH_KEYExample:search for 78,S0,S1,S2,S3,+,31,-,64,+,31,34,-,23,56,64,78,+,31,34,44,-,12,23,26,芭欢往缅协坦赏欲霞摹精雕阅酬辉琐喧仿筐淘践迫蝶崖幸浑梁鹏荷悸唁梁第03章跳跃表SkipLists第03章跳跃表SkipLists,6/21/2023 6:02 AM,Skip Lists,5,Randomized Algorithms,A randomized algorithm performs coin tosses(i.
6、e.,uses random bits)to control its executionIt contains statements of the typeb random()if b=0do A else b=1do B Its running time depends on the outcomes of the coin tosses,We analyze the expected running time of a randomized algorithm under the following assumptionsthe coins are unbiased,and the coi
7、n tosses are independentThe worst-case running time of a randomized algorithm is often large but has very low probability(e.g.,it occurs when all the coin tosses give“heads”)We use a randomized algorithm to insert items into a skip list,滋江诛蜘招滁举引乃宰叫受审汀肖尼谁吟瞄砷舀也袜呈妙逝骑赶砚猫锄忽第03章跳跃表SkipLists第03章跳跃表SkipList
8、s,6/21/2023 6:02 AM,Skip Lists,6,To insert an item(x,o)into a skip list,we use a randomized algorithm:We repeatedly toss a coin until we get tails,and we denote with i the number of times the coin came up headsIf i h,we add to the skip list new lists Sh+1,Si+1,each containing only the two special ke
9、ysWe search for x in the skip list and find the positions p0,p1,pi of the items with largest key less than x in each list S0,S1,SiFor j 0,i,we insert item(x,o)into list Sj after position pjExample:insert key 15,with i=2,Insertion,+,-,10,36,+,-,23,23,+,-,S0,S1,S2,S0,S1,S2,S3,p0,p1,p2,码骡灰曲躯巳配旺沛骤呼掠兰贷怕腾
10、眉挚寞布绽赶乾揭搪蝎歪爸仅箕磊赖第03章跳跃表SkipLists第03章跳跃表SkipLists,6/21/2023 6:02 AM,Skip Lists,7,Deletion,To remove an item with key x from a skip list,we proceed as follows:We search for x in the skip list and find the positions p0,p1,pi of the items with key x,where position pj is in list SjWe remove positions p0,
11、p1,pi from the lists S0,S1,SiWe remove all but one list containing only the two special keysExample:remove key 34,-,+,45,12,-,+,23,23,-,+,S0,S1,S2,-,+,S0,S1,S2,S3,-,+,45,12,23,34,-,+,34,-,+,23,34,p0,p1,p2,骇怨纹僚梳续庶栈兆罕咆翼慌字舀依政挂俯谓陨力莹勇帧憋蕉肃斧琢鲤淖第03章跳跃表SkipLists第03章跳跃表SkipLists,6/21/2023 6:02 AM,Skip Lists,8
12、,Implementation,We can implement a skip list with quad-nodesA quad-node stores:itemlink to the node beforelink to the node afterlink to the node belowlink to the node afterAlso,we define special keys PLUS_INF and MINUS_INF,and we modify the key comparator to handle them,x,quad-node,型班腿瞅宣含奠粘幂神堡邹颊缉辞驭则
13、呵车仍毡迸终牌蛾诣高砰媒静除纹第03章跳跃表SkipLists第03章跳跃表SkipLists,6/21/2023 6:02 AM,Skip Lists,9,Space Usage,The space used by a skip list depends on the random bits used by each invocation of the insertion algorithmWe use the following two basic probabilistic facts:Fact 1:The probability of getting i consecutive hea
14、ds when flipping a coin is 1/2iFact 2:If each of n items is present in a set with probability p,the expected size of the set is np,Consider a skip list with n itemsBy Fact 1,we insert an item in list Si with probability 1/2iBy Fact 2,the expected size of list Si is n/2i The expected number of nodes
15、used by the skip list is,Thus,the expected space usage of a skip list with n items is O(n),渤负乓春报镇字丸摈钥女讯轿贞疑六凛借焦逸守彤袜象勤侗踩吟君乍壤蕾第03章跳跃表SkipLists第03章跳跃表SkipLists,6/21/2023 6:02 AM,Skip Lists,10,Height,The running time of the search an insertion algorithms is affected by the height h of the skip listWe sho
16、w that with high probability,a skip list with n items has height O(log n)We use the following additional probabilistic fact:Fact 3:If each of n events has probability p,the probability that at least one event occurs is at most np,Consider a skip list with n itemsBy Fact 1,we insert an item in list S
17、i with probability 1/2iBy Fact 3,the probability that list Si has at least one item is at most n/2iBy picking i=3log n,we have that the probability that S3log n has at least one item isat most n/23log n=n/n3=1/n2Thus a skip list with n items has height at most 3log n with probability at least 1-1/n2
18、,绳谅蝴黎不戍棱棉胎阜汉印锨缆疥崩冬溺谷融放搓析霓没菊挡矫词乞僻粱第03章跳跃表SkipLists第03章跳跃表SkipLists,6/21/2023 6:02 AM,Skip Lists,11,Search and Update Times,The search time in a skip list is proportional tothe number of drop-down steps,plusthe number of scan-forward stepsThe drop-down steps are bounded by the height of the skip list
19、and thus are O(log n)with high probabilityTo analyze the scan-forward steps,we use yet another probabilistic fact:Fact 4:The expected number of coin tosses required in order to get tails is 2,When we scan forward in a list,the destination key does not belong to a higher listA scan-forward step is as
20、sociated with a former coin toss that gave tailsBy Fact 4,in each list the expected number of scan-forward steps is 2Thus,the expected number of scan-forward steps is O(log n)We conclude that a search in a skip list takes O(log n)expected timeThe analysis of insertion and deletion gives similar resu
21、lts,箍渭风阑霖搀订贵早谣牢庚袭呻焰铂诸剖荡骆舀丛邓福糕猪概溢搓膜篡菱第03章跳跃表SkipLists第03章跳跃表SkipLists,6/21/2023 6:02 AM,Skip Lists,12,Implementing a Dictionary,Comparison of efficient dictionary implementations,名所蛇夕挡费甜救莉烈江妨逆繁溺兆横欠咽泄郡撅雌瓢柞假痔拿瘟瘤泌排第03章跳跃表SkipLists第03章跳跃表SkipLists,6/21/2023 6:02 AM,Skip Lists,13,Summary,A skip list is a
22、data structure for dictionaries that uses a randomized insertion algorithmIn a skip list with n items The expected space used is O(n)The expected search,insertion and deletion time is O(log n),Using a more complex probabilistic analysis,one can show that these performance bounds also hold with high probabilitySkip lists are fast and simple to implement in practice,砒钥烹外滑防具依遮钱细灾朱铰躺啥扇俺朋们止浓酚克员诚奔曰奥炬屡嘴第03章跳跃表SkipLists第03章跳跃表SkipLists,