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
  • Priority Queue Interface
  • Implementation
  • Binary Heap (以 Max Heap 为例)
  • Python heapq
  • 参考
  1. MIT - 6.006

Priority Queue Interface & Implementation

Priority Queue Interface

Priority Queue 在 Queue 的基础上,为 Queue 中的每个元素设置优先级,从而影响 dequeue 的顺序。我们知道 Queue 的 dequeue 是遵循 FIFO 策略,但现实中有很多场景的 dequeue 需要按照一定的优先级实施调度策略:

  • 飞机头等舱与经济舱的登机顺序

  • 操作系统的进程调度管理

  • ...

将这些场景抽象起来,就得到了 Priority Queue Interface:

方法

功能

将元素 x 插入到队列 Q 中

返回队列 Q 中优先级最高的元素

将队列 Q 中优先级最高的元素出列,并返回

将队列 Q 中的元素 x 的优先级变成 k (k 大于当前优先级)

Implementation

在 Priority Queue 中,元素处于一种 “半排序” 的状态,它需要高效地支持三种操作:

  • 定位优先级最高的元素

  • 优先级最高的元素出列后,定位下一个优先级最高的元素

  • 新的元素入列后处在合理的排序位置

这也是实现好的 Priority Queue 的关键所在。

Binary Heap (以 Max Heap 为例)

方法

时间复杂度

空间复杂度

Binary Heap 有两个不变特性(invariants):

  • Structure Property: 结构上它是一棵二叉完全数(binary complete tree)

  • Heap Order Property: 父节点的取值大于它的子节点的取值

实现示例:

binary_heap.py
# 参考
# http://interactivepython.org/courselib/static/pythonds/Trees/BinaryHeapImplementation.html
class MaxHeap:
    def __init__(self):
        # start from index 1, so that for node i
        # parent index: i//2
        # left child index: 2*i
        # right child index: 2*i+1
        self.h = [0]
        self.n = 0

    def insert(self, x):
        # time complexity: O(logn)
        self.h.append(x)
        self.n += 1
        self._bubbleUp(self.n)
    
    def max(self):
        # time complexity: O(1)
        self._ensureNotEmpty()
        return self.h[1]

    def heapify(self, A):
        # time complexity: O(n)
        self.h, self.n = [0] + A, len(A)
        for i in range(self.n//2, 0, -1):
            self._bubbleDown(i)
            
    def extractMax(self):
        # time complexity: O(logn)
        m = self.max()
        if self.n == 1:
            self.h, self.n = [0], 0
        else:
            self.h[1], self.h[self.n] = self.h[self.n], self.h[1]
            del self.h[self.n]
            self.n -= 1
            self._bubbleDown(1)
        return m
    
    def _ensureNotEmpty(self):
        if self.n <= 0:
            raise Exception("empty tree")
        
    def _bubbleUp(self, i):
        while i//2 > 0:
            if self.h[i] > self.h[i//2]:
                self.h[i//2], self.h[i] = self.h[i], self.h[i//2]
            i = i//2
    
    def _bubbleDown(self, i):
        while (i*2) <= self.n:
            mci = self._maxChild(i)
            if self.h[i] < self.h[mci]:
                self.h[i], self.h[mci] = self.h[mci], self.h[i]
            i = mci   
    
    def _maxChild(self, i):
        if i*2 + 1 > self.n:
            return i*2
        else:
            if self.h[i*2] > self.h[i*2+1]:
                return i*2
            else:
                return i*2+1

Python heapq

参考

PreviousString Matching, Karp-RabinNextDictionary Problem & Implementation

Last updated 6 years ago

insert(Q,x)insert(Q, x)insert(Q,x)
max(Q)max(Q)max(Q)
extractMax(Q)extractMax(Q)extractMax(Q)
increaseKey(Q,x,k)increaseKey(Q, x, k)increaseKey(Q,x,k)
insert(x)insert(x)insert(x)
O(log(n))O(log(n))O(log(n))
O(1)O(1)O(1)
max()max()max()
O(1)O(1)O(1)
O(1)O(1)O(1)
extractMax()extractMax()extractMax()
O(log(n))O(log(n))O(log(n))
O(1)O(1)O(1)
python heapq source code