约 389 个字 47 行代码 5 张图片 预计阅读时间 3 分钟
Chap 13 | “Randomized Algorithms”
章节启示录
摆烂了。……
1.概述¶
2.[Example] The Hiring Problem¶
- 从猎头公司聘请办公室助理
- 每天面试不同的申请人,持续 N 天
- 面试成本 = << 招聘成本 =
- 分析面试和招聘成本,而不是运行时间
假设雇用了 人。 总成本:
- 朴素的解决方案
int Hiring ( EventType C[ ], int N )
{ /* candidate 0 is a least-qualified dummy candidate */
int Best = 0;
int BestQ = the quality of candidate 0;
for ( i=1; i<=N; i++ ) {
Qi = interview( i ); /* Ci */
if ( Qi > BestQ ) {
BestQ = Qi;
Best = i;
hire( i ); /* Ch */
}
}
return Best;
}
- 假设候选人以随机顺序到达
随机性假设:到目前为止,前 名候选人中的任何一个都同样可能最有资格
int RandomizedHiring ( EventType C[ ], int N )
{ /* candidate 0 is a least-qualified dummy candidate */
int Best = 0;
int BestQ = the quality of candidate 0;
randomly permute the list of candidates;
for ( i=1; i<=N; i++ ) {
Qi = interview( i ); /* Ci */
if ( Qi > BestQ ) {
BestQ = Qi;
Best = i;
hire( i ); /* Ch */
}
}
}
Online Hiring Algorithm – hire only once¶
int OnlineHiring ( EventType C[ ], int N, int k )
{
int Best = N;
int BestQ = - ;
for ( i=1; i<=k; i++ ) {
Qi = interview( i );
if ( Qi > BestQ ) BestQ = Qi;
}
for ( i=k+1; i<=N; i++ ) {
Qi = interview( i );
if ( Qi > BestQ ) {
Best = i;
break;
}
}
return Best;
}
当且仅当:{ A:= the best one is at position i } { B:= no one at positions k+1 ~ i–1 are hired }
其中,
3.[Example] Quicksort¶
-
Central splitter := the pivot that divides the set so that each side contains at least n/4
小的部分要超过四分之一(大的部分不超过四分之三) -
Modified Quicksort := always select a central splitter before recursions
每次需要先找到Central splitter再继续,若找的不是Central splitter,则需重新寻找。 -
结论1:在我们找到Central splitter之前,所需的预期迭代次数最多为 2 次。
证明
其实用抽屉原理就可以证明。最坏情况时,选的不满足Central splitter的数量最多是 n/2 个。
-
Type j : the subproblem S is of type j if
-
结论2:声明:最多存在 个 类型的子问题。