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
  • String Matching Problem
  • Naive/Brute Force Algorithm
  • Karp-Rabin Algorithm
  • Rolling Hash ADT
  • Karp-Rabin
  1. MIT - 6.006

String Matching, Karp-Rabin

String Matching Problem

给定两个字符串 s 和 t,判断 s 是否是 t 的子串?如果是,出现在哪?出现了多少次?

Naive/Brute Force Algorithm

def match(s, t):
    return any(s == t[i:i+len(s)] for i in range(len(t)-len(s))

子串比较的复杂度:O(∣s∣)O(|s|)O(∣s∣) ,因此算法时间复杂度为 O(∣s∣∗(∣t∣−∣s∣))O(|s|*(|t|-|s|))O(∣s∣∗(∣t∣−∣s∣)) ,近似为 O(∣s∣∗∣t∣)O(|s|*|t|)O(∣s∣∗∣t∣)

Karp-Rabin Algorithm

Rolling Hash ADT

在 Naive Algorithm 中,我们每次比较子串的复杂度都是 O(∣s∣)O(|s|)O(∣s∣) ,但实际上,前后两次比较中,即 s==t[i:i+len(s)]s == t[i:i+len(s)]s==t[i:i+len(s)] 与 s==t[i+1:i+len(s)+1]s == t[i+1:i+len(s)+1]s==t[i+1:i+len(s)+1] ,两个 t 的子串有 len(s)−1len(s)-1len(s)−1 个字母是重复的,如果能够不用重复比较这些比较过的字母,就有可能将比较子串的复杂度将到 O(1)O(1)O(1) 。

于是 Rolling Hash ADT 诞生了:它维护一个字符串 x,同时提供以下三个方法

方法

功能

r()

r 就是 hash function,r() 返回当前子串的哈希值 h(x)

r.append(c)

将字母 c 放到 x 的末尾

r.skip(c)

将 x 的第一个字母移除

Karp-Rabin

def karp_r(s, t):
    for c in s:
        rs.append(c)
    for c in t[:len(s)]:
        rt.append(c)
    if rs() == rt():
        # compare char by char
        pass
    
    for i in range(len(s), len(t)):
        rt.skip(t[i-len(s)])
        rt.append(t[i])
        if rs() == rt():
            # compare char by char
            pass

分析:

如果 Rolling Hash ADT 能够做到:

两个不同的子串的哈希值相等的概率 < 1∣s∣\frac{1}{|s|}∣s∣1​

Karp-Rabin Algorithm 的算法复杂度就是:

O(∣s∣+∣t∣∗∣s∣∗1∣s∣)=O(∣s∣+∣t∣)O(|s| + |t|*|s|*\frac{1}{|s|}) = O(|s| + |t|)O(∣s∣+∣t∣∗∣s∣∗∣s∣1​)=O(∣s∣+∣t∣)

An Rolling Hash Data Structure

想象 string x 是一个基数(base)为 a 的多位数(multi-digit number)u :

方法

基本实现

优化实现

r()

r.append(c)

r.skip(c)

在实际问题中 a 就是所有可能的字符数量。

PreviousWeighted Shortest PathsNextPriority Queue Interface & Implementation

Last updated 6 years ago

,其中 p 是接近 |s| 的随机质数,r 中保存着 u 和 x

,其中 p 是接近 |s| 的随机质数, r 中保存着 和 ,而并非 u

即

即

u mod pu \bmod pumodp
u mod pu \bmod pumodp
u mod pu \bmod pumodp
∣x∣|x|∣x∣
u=u⋅a+ord(c)u = u·a + ord(c)u=u⋅a+ord(c)
(u⋅a+ord(c)) mod p(u · a + ord(c))\bmod p(u⋅a+ord(c))modp
[(u mod p)⋅a+ord(c)] mod p[(u \bmod p)·a + ord(c)] \bmod p[(umodp)⋅a+ord(c)]modp
u=u−c⋅a∣x∣−1u = u - c·a^{|x|-1}u=u−c⋅a∣x∣−1
[u−ord(c)⋅(a∣x∣−1 mod p)] mod p[u - ord(c)·(a^{|x|-1} \bmod p)] \bmod p[u−ord(c)⋅(a∣x∣−1modp)]modp
[(u mod p)−ord(c)⋅(a∣x∣−1 mod p)] mod p[(u \bmod p) - ord(c)·(a^{|x|-1} \bmod p)] \bmod p[(umodp)−ord(c)⋅(a∣x∣−1modp)]modp