[题集]ADS13 Randomized Algorithms

1.Let a=(a1,a​2,…,a​i,…,aj,…,an) denote the list of elements we want to sort. In the quicksort algorithm, if the pivot is selected uniformly at random. Then any two elements get compared at most once and the probability of ai and a​j being compared is 2/(j−i+1) for j>i, given that ai or aj is selected as the pivot.
F, 假如有四个元素,第3个被选中。如果a1>a3, 如果a4<a3, 那么a1和a4发生交换,然后算法就停止了,不会比较a2和a3,此时而a1>a3概率为1/2,a4<a3的概率为1/2, 因此概率为1/4

具体要算的话,应该是N-j-1个元素中大于j的元素个数比i-1个中要多的概率。算起来好像比较麻烦。

yds后来解释了一下,如果a1,到aN是有序的,那么是正确的,因为只需要考虑i和j被分到不同的组,因此是i和j之间的这些元素中选择了i或者j的概率。

2.Reviewing the randomized QuickSort in our course, we always select a central splitter as a pivot before recursions, make sure that each side contains at least n/4 elements. Hence, differing from the deterministic QuickSort, the worst case expected running time of the randomized QuickSort is Θ(NlogN).

T

如果用随机算法,最差情况从O(NlogN)变成了这个,对。

3.Given a linked list containg N nodes. Our task is to remove all the nodes. At each step, we randomly choose one node in the current list, then delete the selected node together with all the nodes after it. Here we assume that each time we choose one node uniformly among all the remaining nodes. What is the expected number of steps to remove all the nodes?

A.Θ(logN)

B.N/e

C.N/2

D.√​N

删去当前节点需要1步,选择的节点可能是N个中任意一个,如果选择第i个,那么需要删除N-i个。i的范围是1到N-1

因此复杂度为 T ( N ) = 1 N ( T ( 1 ) + T ( 2 ) + . . . + T ( N − 1 ) ) + 1 T(N) = \frac 1N(T(1)+T(2)+...+T(N-1))+1 T(N)=N1(T(1)+T(2)+...+T(N1))+1

如果是N的话,1无法消掉。而logN刚好可以消掉1

4.The Online Hiring Algorithm ( hire only once ) is described as the following:

int OnlineHiring ( EventType C[ ], int N, int k )
{
    int Best = N;
    int BestQ = -INFINITY ;
    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;
}

Assume that the quality input C[ ] is uniformly random. When N = 271 and k = 90, the probability of hiring the Nth candidate is__.

A.1/e

B.1/N

C.1/3

D.1/k

要求雇佣第271个,因此前270个中,最好的必须落在前k个,就是前90个中,概率是90/270=1/3

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值