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 a^{|x|} ), 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) · (a^{|u|-1}mod p)] mod p = [(u mod p) − ord(c) · (a^{|x|-1} mod p)] mod p
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
《算法导论》Hardcover版的引言(Introduction to Algorithms - Hardcover Edition)是一本经典的计算机科学教材。该版本不仅在内容上与平装版相同,还具有精美的硬皮封面,能够更好地保护书籍,并增添一份高质感和专业感。 首先,这本书是由Thomas H. Cormen等四位作者共同编写。他们是计算机科学领域的权威人物,在算法研究和教育方面具有丰富的经验。这本书的目的是为计算机科学专业的学生和从业人员提供系统而全面的算法知识,帮助他们深入理解和应用算法。 《算法导论》Hardcover版首先介绍了算法设计和分析的基础知识,包括分治法、贪婪算法、动态规划和回溯法等。接着,书中详细阐述了各种经典算法,如排序、图算法、字符串匹配、高级数据结构等。此外,书中还介绍了算法的优化技巧和应用领域,例如算法的并行化和近似算法。 与平装版相比,Hardcover版的封面更加美观,书页由高品质纸张制成,更加耐用。这使得读者在长时间研究和使用这本书时,能够更好地保存它的完整性和精美外观。此外,Hardcover版也更加适合作为礼品或收藏品,体现了读者对该书的重视和对算法学习的热爱。 总之,《算法导论》Hardcover版是一本内容丰富、思想深刻的算法教材,通过系统化的介绍和实例,帮助读者深入理解和应用各种算法。同时,Hardcover版的精美外观和耐用性也增强了读者在日常使用和收藏方面的满意度。无论是学习算法的新手还是资深专家,都能从这本书中获得极大的收益。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值