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
  • 回顾
  • 假设
  • 实现
  • 静态数组(Static Arrays)
  • 链表(Linked Lists)
  • 动态数组 (Dynamic Arrays)
  1. MIT - 6.006

Data Structure for Dynamic Sequence Interface

回顾

回顾一下 dynamic sequence interface 的定义:维护一个数据序列 x0,x1,x2,...,xn−1x_0, x_1, x_2, ..., x_{n-1}x0​,x1​,x2​,...,xn−1​

  • len():返回数据序列的长度 n

  • seq-iter():遍历 x0,x1,x2,...,xn−1x_0, x_1, x_2, ..., x_{n-1}x0​,x1​,x2​,...,xn−1​

  • at(i):返回序号为 i 的数据xixi​

    • left() => at(0)

    • right() => at(n-1)

  • set-at(i, x):将序号为 i 的元素替换为 x

  • insert-at(i, x):将 x 插入到序号为 i 的位置上,同时将所有原来序号 ≥ i 的元素向右移动一个位置

  • delete-at(i): 将所有序号 ≥ i+1 的元素向左移动一个位置

  • set-at(i, x):语义上等同于连续执行 delete-at(i) 和 insert-at(i, x)

假设

  • 内存分配模型:分配一个长度为 n 数组的时间复杂度为 θ(n)θ(n)θ(n)

实现

静态数组(Static Arrays)

静态数组的实现很容易,这里不讨论具体的实现,各个操作的计算复杂度如下表

操作

时间复杂度

空间复杂度

len

seq-iter

at

set-at

insert-at

delete-at

可以看出,在 insert/delete-at 的操作上,性能不够理想。

链表(Linked Lists)

链表是基于指针的数据结构,它将每个数据如糖葫芦一般串起来,并维护 left/head, right/tail 两个指针。这样 insert/delete left/right 的复杂度都减小到了 O(1)O(1)O(1)。这对于 stack,queue,deque 等特殊 ADT 都是非常合适的。但对于 dynamic sequence interface 来说,at 操作的复杂度变差了。整体分析如下表所示:

操作

时间复杂度

空间复杂度

len

seq-iter

at

set-at

insert-at

delete-at

insert/delete left/right

动态数组 (Dynamic Arrays)

dynamic arrays 内部则维护一个长度一定的数组,且在必要时将该数组缩小或者扩大,通过将操作成本均摊到每次操作中,使得 insert/delete right 的均摊复杂度达到 O(1)O(1)O(1) 。

Insert-right

当 dynamic arrays 内部的数组元素数量达到 n 时,扩容到 2n,因此连续插入的复杂度变成:

cost=θ(1+2+4+8+...+nn)=θ((1−2n)(1−2)n)=θ(1)cost = θ(\frac{1 + 2 + 4 + 8 + ... + n}{n}) = θ(\frac{(1 - 2^n)}{(1-2)n}) = θ(1)cost=θ(n1+2+4+8+...+n​)=θ((1−2)n(1−2n)​)=θ(1)

Delete-right

当 dynamic arrays 内部的数组元素数量小于 n4\frac{n}{4}4n​ 时,将其缩小到 n2\frac{n}{2}2n​ 。具体证明待补充

复杂度

操作

时间复杂度

空间复杂度

len

seq-iter

at

set-at

insert-at

delete-at

insert/delete left/right

PreviousSequence and Set InterfaceNextComputation Complexity

Last updated 6 years ago

O(1)O(1)O(1)
O(1)O(1)O(1)
O(n)O(n)O(n)
O(n)O(n)O(n)
O(1)O(1)O(1)
O(1)O(1)O(1)
O(1)O(1)O(1)
O(1)O(1)O(1)
O(n)O(n)O(n)
O(n)O(n)O(n)
O(n)O(n)O(n)
O(n)O(n)O(n)
O(1)O(1)O(1)
O(1)O(1)O(1)
O(n)O(n)O(n)
O(n)O(n)O(n)
O(n)O(n)O(n)
O(1)O(1)O(1)
O(n)O(n)O(n)
O(1)O(1)O(1)
O(n)O(n)O(n)
O(n)O(n)O(n)
O(n)O(n)O(n)
O(n)O(n)O(n)
O(1)O(1)O(1)
O(1)O(1)O(1)
O(1)O(1)O(1)
O(1)O(1)O(1)
O(n)O(n)O(n)
O(n)O(n)O(n)
O(1)O(1)O(1)
O(1)O(1)O(1)
O(1)O(1)O(1)
O(1)O(1)O(1)
O(n)O(n)O(n)
O(n)O(n)O(n)
O(n)O(n)O(n)
O(n)O(n)O(n)
O(1)O(1)O(1)
O(1)O(1)O(1)