Skip List

Skip List | Set 1 (Introduction)

Can we search in a sorted linked list in better than O(n) time?
The worst case search time for a sorted linked list is O(n) as we can only linearly traverse the list and cannot skip nodes while searching. For a Balanced Binary Search Tree, we skip almost half of the nodes after one comparison with root. For a sorted array, we have random access and we can apply Binary Search on arrays.

Can we augment sorted linked lists to make the search faster? The answer is Skip List. The idea is simple, we create multiple layers so that we can skip some nodes. See the following example list with 16 nodes and two layers. The upper layer works as an “express lane” which connects only main outer stations, and the lower layer works as a “normal lane” which connects every station. Suppose we want to search for 50, we start from first node of “express lane” and keep moving on “express lane” till we find a node whose next is greater than 50. Once we find such a node (30 is the node in following example) on “express lane”, we move to “normal lane” using pointer from this node, and linearly search for 50 on “normal lane”. In following example, we start from 30 on “normal lane” and with linear search, we find 50.

What is the time complexity with two layers? The worst case time complexity is number of nodes on “express lane” plus number of nodes in a segment (A segment is number of “normal lane” nodes between two “express lane” nodes) of “normal lane”. So if we have n nodes on “normal lane”,  nodes on “express lane” and we equally divide the “normal lane”, then there will be  nodes in every segment of “normal lane” .  is actually optimal division with two layers. With this arrangement, the number of nodes traversed for a search will be . Therefore, with  extra space, we are able to reduce the time complexity to .

Can we do better?
The time complexity of skip lists can be reduced further by adding more layers. In fact, the time complexity of search, insert and delete can become O(Logn) in average case. We will soon be publishing more posts on Skip Lists.

References
MIT Video Lecture on Skip Lists
http://en.wikipedia.org/wiki/Skip_list

==========================================================

http://www.geeksforgeeks.org/skip-list/

时间: 2024-08-30 03:13:15

Skip List的相关文章

Skip the Class

BestCoder Round #92 Skip the Class Accepts: 678 Submissions: 1285 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem Description Finally term begins. luras loves school so much as she could skip the class happily

LINQ系列:LINQ to SQL Take/Skip

1. Take var expr = context.Products .Take(10); var expr = (from p in context.Products select p) .Take(10); SELECT TOP (10) [c].[ProductID] AS [ProductID], [c].[CategoryID] AS [CategoryID], [c].[ProductName] AS [ProductName], [c].[UnitPrice] AS [UnitP

git的提交突出--git rebase之abort、continue、skip

(1)应用实例描述 假设在github或者gitoschina上建立了一个项目,默认分支为master分支,远程master分支上c.sh文件内容: 开发者A.B分别将项目拷贝到自己本地进行开发 某一天,开发者B提交c.sh,并且提交成功, 之后,开发者A在本地代码并没有和远程master分支的代码同步的情况下,对本地的c.sh进行了修改,修改后c.sh内容如下: 修改后,开发者A准备将代码提交到远程master分支上. (2)引入问题 假设开发者A提交过程如下: $ git add c.sh

Linq中Take、TakeWhile、Skip、SkipWhile的比较(转)

参考文章:http://blog.csdn.net/lxfzgg/article/details/20534281 Take() int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var first3Numbers = numbers.Take(3); //从第一个元素开始,获取三个 return的是前面的数 Console.WriteLine("First 3 numbers:"); foreach (var n in first3N

Xcode升级误点Skip Bundle解决办法!!!

交代一下背景,我电脑上是从AppStore下载的Xcode7.2,后来从网上下载了Xcode7.3安装包,然后安装7.3,电脑上同时存在7.2版本和7.3版本,但是在安装7.3的时候误点了Skip Bundle,然后工程就有一点小问题,下面是解决办法: 第一步:“Finder”--->"应用程序"--->"鼠标右键点击7.3版本Xcode"----->“显示包内容”. 第二步:“Contents”---->"info.plist&q

Gradle Goodness: Skip Building Project Dependencies

If we use Gradle in a multi-module project we can define project dependencies between modules. Gradle uses the information from the project dependencies to determine which tasks need to be run. For example if module B depends on module A and we want

Oracle给Select结果集加锁,Skip Locked(跳过加锁行获得可以加锁的结果集)

1.通过select for update或select for update wait或select for update nowait给数据集加锁 具体实现参考select for update和select for update wait和select for update nowait的区别 2.Skip Locked(跳过加锁行获得可以加锁的结果集) Skip locked是oracle 11g引入的. 通过skip locked可以使select for update语句可以查询出(

Linq分区操作之Skip,SkipWhile,Take,TakeWhile源码分析

Linq分区操作之Skip,SkipWhile,Take,TakeWhile源码分析 二:linq的分区操作 常用的分区操作:Take,TakeWhile,Skip,SkipWhile 三:Take 1. 注释: 从序列的开头返回指定数量的连续元素 2. 实战: var nums = new int[] { 10, 20, 30, 40, 50, 60 }; var query = nums.Take(2).ToList(); // 10,20 3. 探究源码: 四:TakeWhile 1. 注

uwsgi部署到nginx出现invalid request block size: 4161 (max 4096)...skip问题

使用Flask制作一个网页平台之后,登陆使用openid登陆,然后使用uwsgi服务部署到nginx上,运行起来没有什么问题,但是偶尔在登陆的时候出现502的错误,一般登陆成功之后后面的任何操作都不会出错. 查看uwsgi的log之后,发现出现这样的一个错误: invalid request block size: 4161 (max 4096)...skip 之前一个没有去详细搜索过,也没有具体去看错误产生的原因,因为只是偶尔出现,并且有时候重试的时候是可以登陆的,所以没有太多的去关注,今天因

mongodb sort limit和skip用法

http://www.ttlsa.com/mongodb/mongodb-sort-limit-skip-usage/ 1.sort是排序用的.sort({"字段":1}) 2.skip跳过多少条结果 3.limit每次查询返回的最大结果数量: 项目中的用法比较好: 再次记录: 前台传过,page参数:根据page参数确定是第几页: 1 function(cb) { User.find(query).skip(page * 40).limit(40).exec(function(err