Introduction to Algorithms (Table Doubling, Karp-Rabin)
How Large should Table be?
- want m = Θ(n) at all times
Idea
Start small (constant) and grow (or shrink) at necessary
Rehashing
To grow or shrink table hash function must change
- must rebuild hash table from scratch
- Θ(n + m) time = Θ(n), if m = Θ(n)
How fast to grow
When n reaches m, say
- m += 1, rebuild every step, n inserts cost Θ(n^2)
- m *= 2, rebuild at insertion 2^i, n inserts cost Θ(n)
- a few inserts cost linear time, but Θ(1) “on average”
Amortized Analysis
This is a common technique in data structures
- an operation has amortized cost T(n) if k operations cost ≤ k · T(n)
- “T(n) amortized” roughly means T(n) “on average”, but averaged over all ops.
- e.g. inserting into a hash table takes O(1) amortized time.
Back to hashing
Maintain m = Θ(n) =⇒ α = Θ(1) =⇒ support search in O(1) expected time (assuming simple uniform or universal hashing)
Deletion
Also, O(1) expected as is.
- space can get big with respect to n e.g. n× insert, n× delete
- solution: when n decreases to m/4, shrink to half the size =⇒ O(1) amortized cost for both insert and delete
Resizable Arrays
list.append and list.pop in O(1) amortized
String Matching
Given two strings s & t: does s occur as a substring of t
Simple Algorithm:
any(s == t[i : i + len(s)] for i in range(len(t) − len(s)))
O(|s|) time for each substring comparison
O(|s| · (|t| − |s|)) time = O(|s| · |t|) potentially quadratic
Karp-Rabin Algorithm
Rolling Hash ADT:
Maintain string x subject to
- r(): reasonable hash function h(x) on string x
- r.append(c): add letter c to end of string x
- r.skip(c): remove the front letter from string x, assuming it is c
Karp-Rabin Application:
for c in s: rs.append(c)
for c in t[:len(s)]:rt.append(c)
if rs() == rt(): ...O(|s|)
for i in range(len(s), len(t)):rt.skip(t[i-len(s)])rt.append(t[i])if rs() == rt(): ...O(|t|) + O(#matches*|s|)
Data Structure:
Treat string x as a multi-digit number u in base a where a denotes the alphabet size, e.g., 256
- r() = u mod p for (ideally random) prime p ≈ |s| or |t| (division method)
- r stores u mod p and |x| (really
), not u ⇒ smaller and faster to work with (u mod p fits in one machine word)
- r.append(c): (u·a + ord(c)) mod p = [(u mod p) · a + ord(c)] mod p
- r.skip(c): [u − ord(c) · (
mod p)] mod p = [(u mod p) − ord(c) · (
mod p)] mod p
Introduction to Algorithms (Table Doubling, Karp-Rabin)相关推荐
- ebooks/算法导论官方配套课件下载(英文版)(Introduction to Algorithms,v3)中英部分内容对比/电子版教材下载/阅读/高中数学下载(zlib/微信读书)
文章目录 官网资源 所在目录 核心内容 关于算法导论的中文翻译版 Chinese segment English segment 其他教材资源下载 下载说明 数学 考研复习可以参考的初等知识 其他教材 ...
- Hashing哈希函数(Introduction to Algorithms, 算法导论,CLRS)学习笔记
Hashing Hashing fundamentals Application: Unordered sets Hash table with chaining Practical universa ...
- van Emde Boas Trees(vEB树)(Introduction to Algorithms, 算法导论,CLRS)学习笔记
van Emde Boas Trees 1. Predecessor search/ordered sets predecessor: return the nearest left neighbor ...
- 麻省理工学院《算法导论》(MIT - Introduction to Algorithms)
关于课本的介绍如下: 本书自第一版出版以来,已经成为世界范围内广泛使用的大学教材和专业人员的标准参考手册.本书全面论述了算法的内容,从一定深度上涵盖了算法的诸多方面,同时其讲授和分析方法又兼顾了各个层 ...
- Linear Programming线性规划(Introduction to Algorithms, 算法导论,CLRS)学习笔记
Linear Programming 1. Fundamentals objective function and constraints: min/max3x1+24x2+13x3+9x4...s. ...
- 大O符号、小o符号和逐点有界、一致有界(Introduction to Algorithms)
逐点有界函数{fn(x)}是定义在集合E上的函数序列. 如果对于每一个x属于E,序列{fn(x)}是有界的,即存在定义在E上的有限值函数g(x),使得: |fn(x)| < g(x) (x 属于 ...
- gossip algorithms
1.河内之塔.. 2.Algorithm Gossip: 费式数列. 3. 巴斯卡三角形 4.Algorithm Gossip: 三色棋 5.Algorithm Gossip: 老鼠走迷官(一) 6. ...
- 【Paper】A Comparative Evaluation of Unsupervised Anomaly Detection Algorithms for Multivariate Data
论文原文:HTML 论文年份:2020 论文被引:396(2020/10/03) 696(2022/03/26) 文章目录 Abstract Introduction Categorization o ...
- [转载]Maximum Flow: Augmenting Path Algorithms Comparison
https://www.topcoder.com/community/data-science/data-science-tutorials/maximum-flow-augmenting-path- ...
最新文章
- oracle insert忽略重复数据,Oracle’INSERT ALL’忽略重复项
- 技术 | Bengio终结Theano不是偶然,其性能早在Keras支持的四大框架中垫底
- 深度学习的数学 (6)误差反向传播法必需的链式法则
- ubuntu开启ssh
- 如果简历上真写了“会多线程”,那面试一般会被怎么问?
- php序列化和反序列化
- powerbi视觉对象_Power BI 视觉对象词云WordCloud
- Confluence 6 配置 workbox 通知
- Flutter框架基础
- su室外渲染参数设置_紫天资源星球下载:多层公寓楼室外Lumion预渲染场景
- 有时间属性,有过程的是软件,有空间属性,有结构的是硬件
- 这个是我学习的所有sql语句的写法
- 2016年CIO要掌握五大新思维
- 无法打开配置文件vmx_如何解决Windows照片查看器无法显示此图片
- ue4设置组合键_UE4快捷键
- 回波损耗和电压驻波比
- Java多线程的简介
- 惠普服务器不进系统,HP服务器无法启动
- Data Analysis - Day7 - Pandas
- 洛谷P3604 美好的每一天(莫队)
热门文章
- 【JAVA程序设计】(C00095)基于SSM框架的学生选课管理系统
- 22. Unity - 3D游戏开发小计01 --- 修复动画BUG、更改环境光照、导航网格、相机跟随、场景渲染后期处理
- 2.1虚拟机及相关知识。
- python用函数绘制椭圆_详解opencv中画圆circle函数和椭圆ellipse函数
- 青岛旅行网页毕设HTML 带报告讲解5个充实超链接页面 大学生网页 旅游网页
- 价值8000元的Excel教程
- ibm tivoli_使用Tivoli Access Manager进行电子商务的加密硬件,使用AIX上的IBM 4960进行WebSEAL的SSL加速
- 用C++编写的2048小游戏
- C/C++ 向上向下取整函数及例题
- 大学选修课实用计算机技术,【原创】写给大一学弟学妹:大一应该选修哪些实用有趣的选修课?...