open-courses
  • 公开课笔记
  • CMU 15-445/645 Database Systems
    • Relational Data Model
    • Advanced SQL
    • Database Storage
    • Buffer Pools
    • Hash Tables
    • Tree Indexes
    • Index Concurrency Control
    • Query Processing
    • Sorting&Aggregations
    • Join Algorithms
    • Query Optimization
    • Parallel Execution
    • Embedded Database Logic
    • Concurrency Control Theory
    • Two Phase Locking
    • Timestamp Ordering Concurrency Control
    • Multi-Version Concurrency Control
    • Logging Schemes
    • Database Recovery
    • Introduction to Distributed Databases
    • Distributed OLTP Databases
    • Distributed OLAP Databases
  • UCB - CS162
    • OS intro
    • Introduction to the Process
    • Processes, Fork, I/O, Files
    • I/O Continued, Sockets, Networking
    • Concurrency: Processes & Threads
    • Cooperating Threads, Synchronization
    • Semaphores, Condition Variables, Readers/Writers
    • Scheduling
    • Resource Contention & Deadlock
    • Address Translation, Caching
    • File System (18,19,20)
    • Distributed Systems, Networking, TCP/IP, RPC (21,22)
    • Distributed Storage, Key-Value Stores, Security (23)
    • Security & Cloud Computing (24)
    • Topic: Ensuring Data Reaches Disk
  • MIT - 6.006
    • Sequence and Set Interface
    • Data Structure for Dynamic Sequence Interface
    • Computation Complexity
    • Algorithms and Computation
    • Structure Of Computation
    • Graph & Search
    • Tree & Search
    • Weighted Shortest Paths
    • String Matching, Karp-Rabin
    • Priority Queue Interface & Implementation
    • Dictionary Problem & Implementation
    • Sorting
    • Dynamic Programming
    • Backtracking
    • Self-Balancing Tree
  • MIT - 6.824
    • 2PC & 3PC
    • Introduction and MapReduce
    • RPC and Threads
    • Primary/Backup Replication
    • Lab: Primary/Backup Key/Value Service
    • Google File System (GFS)
    • Raft
    • Lab: Raft - Leader Election
    • Lab: Raft - Log Replication
  • Stanford-CS107
    • 原始数据类型及相互转化
    • 指鹿为马
    • 泛型函数
    • 泛型栈
    • 运行时内存结构
    • 从 C 到汇编
    • 函数的活动记录
    • C 与 C++ 代码生成
    • 编译的预处理过程
    • 编译的链接过程
    • 函数的活动记录续、并发
    • 从顺序到并发和并行
    • 信号量与多线程 1
    • 信号量与多线程 2
    • 复杂多线程问题
    • 函数式编程 - Scheme 1
    • 函数式编程 - Scheme 2
    • 函数式编程 - Scheme 3
    • 函数式编程 - Scheme 4
    • 函数式编程 - Scheme 5
    • Python 基础
  • MIT - 6.001 - SICP
    • 什么是程序
    • 程序抽象
    • 替代模型
    • 时间/空间复杂度
    • 数据抽象
    • 高阶函数
    • Symbol
    • 数据驱动编程与防御式编程
    • 数据抽象中的效率与可读性
    • 数据修改
    • 环境模型
    • 面向对象-消息传递
    • 面向对象 - Scheme 实现
    • 构建 Scheme 解释器
    • Eval-Apply Loop
    • Normal Order (Lazy) Evaluation
    • 通用机
    • 寄存器机器
    • 子程序、栈与递归
    • 在寄存器机器中执行
    • 内存管理
  • MIT - 6.046
    • Randomized Algorithms
    • Skip Lists
  • System Design
    • Twitter
    • Cache Consistency & Coherence
  • DDIA 笔记
    • Replication
    • Transactions
    • The Trouble with Distributed Systems
    • Consistency & Consensus
  • Papers We Love
    • Consistent Hashing and Random Trees (1997)
    • Dynamic Hash Tables (1988)
    • LFU Implementation With O(1) Complexity (2010)
    • Time, Clocks, and the Ordering of Events in a Distributed System (1978)
    • Dapper, a Large-Scale Distributed Systems Tracing Infrastructure (2010)
    • Gorilla: A Fast, Scalable, In-Memory Time Series Database (2015)
  • Release It 笔记
    • Anti-patterns & Patterns in Microservice Architecture
  • Database Design
    • Log Structured Merge (LSM) Tree & Usages in KV Stores
    • Prometheus
Powered by GitBook
On this page
  • 简介
  • 从 Find/Search 谈起
  • Skip Lists
  • 参考
  1. MIT - 6.046

Skip Lists

PreviousRandomized AlgorithmsNextTwitter

Last updated 6 years ago

简介

Skip Lists 是一种随机数据结构(Randomized Data Structure),它是 Set (Ordered Set) 的一种实现。它的各操作复杂度如下表所示:

Operation Name

Time Complexity

Space Complexity

find-key(k)

iter()

insert(x)

delete-key(k)

delete-min/max()

find-next/prev(k)

find-min/max()

order-iter()

从 Find/Search 谈起

One Linked List

Two Linked Lists

快速公交系统(BRT)与普通公交系统的区别在于:普通公交系统与其它私人交通工具共享车道且每站必停,而快速公交系统独享车道,且只经停部分公交站。那么假如我要从 A 地区 B 地(快速公交不可直达),就可以先从 A 开始乘坐快速公交到达离 B 最近的公交站,再乘坐普通公交到达 B。如果我们把 Search 操作比喻成这样的乘车过程,可以考虑使用两个 Linked Lists:

如图 2 所示,举例如下:

14 -> 59:14 -> 34 -> 42 -> 50 -> 59 14 -> 79:14 -> 34 -> 42 -> 72 -> 79

怎么设计快速公交系统的停靠站能使得公交系统的性能达到最大?直觉告诉我们,将快速公交的停靠站平均分布在普通公交停靠站上。那么这时候 Search 的成本为:

求其最小值,可以得到:

如此一来,Search 的成本就是:

More Linked Lists

Number Of Linked Lists

Search Cost

2

3

4

...

...

Skip Lists

Insert(x)

从空的 Skp Lists 开始不断 Insert 元素,就构成了 Skip Lists 的创建过程。如何保证这个过程能够建立出接近完美的 Skip Lists 其实就是 Insert 的实现需要解决的问题。

方案:先用 search/find 找到元素在最底层的位置,将元素插入到最底层中,然后以 1/2 的概率决定是否将该元素插入到上面一层,递归重复。根据概率理论,平均来看 ---

  • 1/2 的元素会被插入到上面 0 层

  • 1/4 的元素会被插入到上面 1 层

  • 1/8 的元素会被插入到上面 2 层

  • ...(以此类推)

Delete(x)

具体证明请查阅参考资料。

参考

当我们有一个 Linked List 时,Search 操作在最差情况下的复杂度为 θ(n)θ(n)θ(n) ,我们有什么方式能够提高它的速度呢?

searchcost=∣L1∣+∣L2∣∣L1∣searchcost = |L_1| + \frac{|L_2|}{|L_1|}searchcost=∣L1​∣+∣L1​∣∣L2​∣​
∣L1∣2=∣L2∣=n=>∣L1∣=n|L1|^2 = |L2| = n => |L1| = \sqrt{n}∣L1∣2=∣L2∣=n=>∣L1∣=n​
∣L1∣+∣L2∣∣L1∣=n+nn=2n |L_1| + \frac{|L_2|}{|L_1|} = \sqrt{n} + \frac{n}{\sqrt{n}} = 2\sqrt{n}∣L1​∣+∣L1​∣∣L2​∣​=n​+n​n​=2n​

使用 lgnlgnlgn 个 linked lists 时,已经很像一棵树,如 B 树。

完美的 Skip Lists 就是由 lgnlgnlgn 个 linked lists 构成的数据结构。

search 的时间复杂度为 O(lgn)O(lgn)O(lgn) ,递归插入的时间复杂度同样为 O(lgn)O(lgn)O(lgn) ,因此 insert 的总时间复杂度也为O(lgn)O(lgn)O(lgn)。

Delete 需要先用 search/find 找到元素的位置,然后从所有可能存在该元素的链表中删除该元素,因此总时间复杂度为O(lgn)O(lgn)O(lgn)。

,

O(lgn)O(lgn)O(lgn)
O(1)O(1)O(1)
O(n)O(n)O(n)
O(n)O(n)O(n)
O(lgn)O(lgn)O(lgn)
O(1)O(1)O(1)
O(lgn)O(lgn)O(lgn)
O(1)O(1)O(1)
O(lgn)O(lgn)O(lgn)
O(1)O(1)O(1)
O(lgn)O(lgn)O(lgn)
O(1)O(1)O(1)
O(lgn)O(lgn)O(lgn)
O(1)O(1)O(1)
O(n)O(n)O(n)
O(1)O(1)O(1)
2n2\sqrt{n}2n​
3n33\sqrt[3]{n}33n​
4n44\sqrt[4]{n}44n​
lgnlgnlgn
lgnnlgn=2lgnlgn\sqrt[lgn]{n} = 2lgnlgnlgnn​=2lgn
lecture note
video
图 1 - One Linked List
图 2 - Two Linked Lists