算法设计与分析课程复习笔记5——随机化算法

算法设计与分析课程复习笔记5——随机化算法

随机化算法

雇佣问题

问题描述:

  • 通过中介招聘新职员
  • 一天面试一个中介介绍的候选人
  • 优胜劣汰

Hire-Assistant(A)
 current ← an infinitely useless dummy assistant(初值)
 for i = 1…n
  do if A[i] is better qualified than current
   then current ← A[i]
 return current

代价模型
面试代价:中介费用 c 1 c_1 c1,候选人路费 c 2 c_2 c2等等
雇用代价:因雇佣付给中介的额外费用 c 3 c_3 c3,雇佣导致的内部管理费用 c 4 c_4 c4

总费用:面试n个人的费用+面试中m个人获得雇佣的费用
即: c i ∗ n + c h ∗ m c_i*n+c_h*m cin+chm

最好情况:第一个人最优
c i ∗ n + c h c_i*n+c_h cin+ch

最坏情况:面试者越来越好
c i ∗ n + c h ∗ n c_i*n+c_h*n cin+chn

我们期望的m是什么?
概率分析
假设两两候选人互异
于是我们可以进行排序:1,2,……,n
假设每种排列的可能性相等
(即一致性随机排列)

指示变量
I ( E ) = { 1 ,   i f   e v e n t E   o c c u r s 0 ,   i f   e v e n t E   d o e s   n o t   o c c u r I(E)=\left\{ \begin{aligned} 1, if event E occurs\\ 0, if event E does not occur \end{aligned} \right. I(E)={1, if eventE occurs0, if eventE does not occur
指示变量用于概率和期望值之间的转换
Lemma: For an event E and its indicator variable X E X_E XE = I(E), Pr(E) = E( X E X_E XE).

雇佣问题平均情况分析
事件: E 1 E_1 E1 E 2 E_2 E2、……、 E n E_n En
E i E_i Ei即第i个受聘用

指示变量: X 1 X_1 X1 X 2 X_2 X2、……、 X n X_n Xn
X i X_i Xi= I ( E i ) I(E_i) I(Ei)

m = ∑ i = 1 n X i m=\sum^n_{i=1}X_i m=i=1nXi
E ( X i ) = 1 / i E(X_i)=1/i E(Xi)=1/i
E ( m ) = E ( ∑ i = 1 n X i ) = O ( l g n ) E(m)=E(\sum^n_{i=1}X_i)=O(lgn) E(m)=E(i=1nXi)=O(lgn)

期望雇佣代价:Θ( c i ∗ n + c h ∗ l g n c_i*n+c_h*lgn cin+chlgn

平均情况分析的缺陷

  1. 算法已确定
  2. 每一个确定的算法都有其最坏的输入
  3. 不良中介可能一直提供最坏的输入

解决方法:随机化
改进的雇佣算法:

  1. 从中介获得所有候选人名单
  2. 产生输入的一致性随机排列
  3. 再面试

期望代价:Θ( c i ∗ n + c h ∗ l g n c_i*n+c_h*lgn cin+chlgn

不同之处:

  • 没有争议性假设
  • 不存在明确的导致最坏情况的输入
  • 削弱了对手的操控能力

一致随机排列生成
如何得到n个元素的一致性随机排列?
方法1:排序法排列
Permute-By-Sorting(A)
 Allocate an array B of size n (数组)
 for i = 1…n
  do B[i] ← random(1, n 3 n^3 n3)
 Sort arrays A, using B as sort keys(排序,以B为参考)
一致性随机排列
方法2:换位法排列
Permute-In-Place(A)
 for i = 1…n – 1
 do j ← random(i, n)
  swap A[i] ↔ A[j] (交换)

Lemma: Procedure Permute-In-Place takes linear time O(n).
开销O(n).
Lemma: Procedure Permute-In-Place produces a uniformly
random permutation
.(一致性随机排列)

快速排序

为了排序A[p……r]

  • 分割,根据选定的某个中心,将A分成两个部分
  • 治理,每一部分用QuickSort嵌套处理
  • 组合

算法如下:
QuickSort(A,p,r)
 if p<r
  then q ← \leftarrow Partition(A,p,r)
   QuickSort(A,p,q-1)
   QuickSort(A,q+1,r)

其中Partition(A,p,r)分割,返回分离点的位置q
Partition(A,p,r)
 x ← \leftarrow A[r]
 i ← \leftarrow p-1
 for j ← \leftarrow p to r-1
  do if A[j]≤x
   then i ← \leftarrow i+1
    exchange A[i] ↔ \leftrightarrow A[j]
 exchange A[i+1] ↔ \leftrightarrow A[r]
 return i+1

解释:选择最后一个元素作为分割中心
分割过程中出现4个区域:

  1. All entries in A[p … i ] ≤ pivot.
  2. All entries in A[i+1 … j-1] > pivot.
  3. A[ j . . r-1]
  4. A[r] = pivot.
    快速排序

快速排序分析:
最坏情况:分割中心选择了最大或最小
T ( n ) = T ( n − 1 ) + Θ ( n ) = Θ ( n 2 ) T(n) = T(n-1) + Θ(n) = Θ(n^2) T(n)=T(n1)+Θ(n)=Θ(n2)
最好情况:分割中心每次都选择在中值
T ( n ) = 2 ∗ T ( n / 2 ) + Θ ( n ) = Θ ( n l g n ) T(n) = 2*T(n/2) + Θ(n) = Θ(n lg n) T(n)=2T(n/2)+Θ(n)=Θ(nlgn)
平均情况接近最好情况

随机化快速排序

QuickSort(A,p,r)
 if p<r
  then swap A[r] ↔ \leftrightarrow A[random(p,r)]
   q ← \leftarrow Partition(A,p,r)
   QuickSort(A,p,q-1)
   QuickSort(A,q+1,r)

快速排序运行时间
如果X为QuickSort作比较的总次数,则时间开销为 O ( X + n ) O(X+n) O(X+n)

E(X)=O(nlgn)
指示变量 X i j X_{ij} Xij
X i j = { 1 ,   i f   z i   a n d   z j   a r e   c o m p a r e d 0 ,   i f   z i   a n d   z j   a r e   n o t   c o m p a r e d X_{ij}=\left\{ \begin{aligned} 1, if z_i and z_j are compared\\ 0, if z_i and z_j are not compared\\ \end{aligned} \right. Xij={1, if zi and zj are compared0, if zi and zj are not compared

X = ∑ i = 1 n − 1 ∑ j = i + 1 n X i j X=\sum^{n-1}_{i=1}\sum^{n}_{j=i+1}X_{ij} X=i=1n1j=i+1nXij

E(X)
=E( ∑ i = 1 n − 1 ∑ j = i + 1 n X i j \sum^{n-1}_{i=1}\sum^{n}_{j=i+1}X_{ij} i=1n1j=i+1nXij)
= ∑ i = 1 n − 1 ∑ j = i + 1 n \sum^{n-1}_{i=1}\sum^{n}_{j=i+1} i=1n1j=i+1nE( X i j X_{ij} Xij)
= ∑ i = 1 n − 1 ∑ j = i + 1 n \sum^{n-1}_{i=1}\sum^{n}_{j=i+1} i=1n1j=i+1nPr( z i z_i zi and z j z_j zj are compared)
= ∑ i = 1 n − 1 ∑ j = i + 1 n \sum^{n-1}_{i=1}\sum^{n}_{j=i+1} i=1n1j=i+1n( 2 j − i + 1 \frac2{j-i+1} ji+12)
= ∑ i = 1 n − 1 O ( l g n ) \sum^{n-1}_{i=1}O(lgn) i=1n1O(lgn)
= O ( n l g n ) O(nlgn) O(nlgn)

随机快速排序期望的时间开销: O ( n l g n ) O(nlgn) O(nlgn)

随机算法的分类

  1. Las Vegas算法:随机算法总是或者给出正确的解,或者无解。
  2. Monte Carlo算法:居多能够给出正确的解,偶尔产生错误的解。可以采取措施使产生错误解的概率降低到任意的程度。

测试串的相等性
A:长串x
B:长串y
x=y?
A将x发送给B,或B将y发送给A,然后判断x=y?
缺点:浪费资源

A从x中取出一个短得多的串作为x的“指纹”发送给B,B用同样的方法获得y的“指纹”,如果两者的“指纹”相同,则认为x=y,否则x=y 不成立。
特点:节约了资源,但理论上不能100%正确。

“指纹”生成
对于一个串w,设I(w)是比特串w表示的一个整数,一种产生“指纹”的方法是选择一个素数p,通过“指纹”函数产生。
I p ( x ) = I ( x ) ( m o d   p ) I_p(x)=I(x)(mod p) Ipx=Ixmod p

I p ( x ) I_p(x) Ipx I p ( y ) I_p(y) Ipy则 x ≠ y
I p ( x ) = I p ( y ) 则 x = y ? I_p(x)= I_p(y)则 x = y? Ipx=Ipyx=y?

算法:
1、A从小于M的素数集中随机选择p;
2、A将p和 I p ( x ) I_p(x) Ipx发送给B;
3、B检查是否 I p ( x ) = I p ( y ) I_p(x)= I_p(y) Ipx=Ipy,确定x和y是否相等。

失败概率分析
失败概率
n 是x的二进制的表示形式的位数
π(n)是小于n 的不同素数的个数
π(n) → \rightarrow n / l n ( n ) n/ln(n) n/ln(n)

参考:任课教师邱德红教授的课件

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值