算法导论习题[Exercises 32.1-3 ]

本文通过数学推导证明了对于随机选择的模式串P和文本串T,朴素字符串匹配算法的期望字符比较次数与字符串长度及字符集大小相关。通过定义单次字符匹配的概率和失配的概率,利用级数求和的方法得出期望比较次数的公式,并进一步证明了该算法的有效性。

Suppose that pattern P and text T are randomly chosen strings of length m and n, respectively, from the d-ary alphabet Σd = {0, 1, . . . , d - 1}, where d 2. Show that the expected number of character-to-character comparisons made by the implicit loop in line 4 of the naive algorithm is

over all executions of this loop. (Assume that the naive algorithm stops comparing characters for a given shift once a mismatch is found or the entire pattern is matched.) Thus, for randomly chosen strings, the naive algorithm is quite efficient.
证明:
P为单个字符比较匹配的概率,1-P为失配的概率,P=d-1
比较次数 概率 比较次数 * 概率
1 1 - P 1 - P
2 P1 - P 2P - 2P2
3 P2 (1 - P) 3P - 3P3
m-1 Pm-2(1 - P) (m-1)Pm-2 - (m-1) Pm-1
m Pm-1(1 - P)+Pm-1P (m)Pm-1 - (m) Pm+ (m)Pm
E =∑(比较次数 * 概率) = 1+P+P2+…..Pm-1 = (1-Pm)/1-P = (1- d –m )/( 1- d-1)
f(x) =(1- x –m )/( 1- x-1),fx)求导可知,在m>1,x>=2时导数为负,则f(x)x>=2严格减函数,所以f(x)<=f(2)<=2.证毕。
由此可知,对于随机的字符串,朴素的字符串比较还是有效的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值