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

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                                       P 1 - 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) P+ (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), f x )求导可知,在 m>1,x>=2 时导数为负,则 f(x) x>=2 严格减函数,所以 f(x)<=f(2)<=2. 证毕。
 
由此可知,对于随机的字符串,朴素的字符串比较还是有效的。
 
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值