《算法导论》

5.1-1

Show that the assumption that we are always able to determine which candidate is best in line 4 of procedure \text{HIRE-ASSISTANT}HIRE-ASSISTANT implies that we know a total order on the ranks of the candidates.

A total order is a partial order that is a total relation (\forall a, b \in A:aRb \text{ or } bRa)(∀a,bA:aRb or bRa). A relation is a partial order if it is reflexive, antisymmetric and transitive.

Assume that the relation is good or better.

  • Reflexive: This is a bit trivial, but everybody is as good or better as themselves.
  • Transitive: If AA is better than BB and BB is better than CC, then AA is better than CC.
  • Antisymmetric: If AA is better than BB, then BB is not better than AA.

So far we have a partial order.

Since we assume we can compare any two candidates, then comparison must be a total relation and thus we have a total order.

5.1-2 \star⋆

Describe an implementation of the procedure \text{RANDOM}(a, b)RANDOM(a,b) that only makes calls to \text{RANDOM}(0, 1)RANDOM(0,1). What is the expected running time of your procedure, as a function of aa and bb?

As (b - a)(ba) could be any number, we need at least \lceil \lg(b - a) \rceil⌈lg(ba)⌉ bits to represent the number. We set \lceil \lg(b - a) \rceil⌈lg(ba)⌉ as kk. Basically, we need to call \text{RANDOM}(0, 1)RANDOM(0,1) kk times. If the number represented by binary is bigger than b - aba, it's not valid number and we give it another try, otherwise we return that number.

RANDOM(a, b)
    range = b - a
    bits = ceil(log(2, range))
    result = 0
    for i = 0 to bits - 1
        r = RANDOM(0, 1)
        result = result + r << i
    if result > range
        return RANDOM(a, b)
    else return a + result

The expectation of times of calling procedure \text{RANDOM}(a, b)RANDOM(a,b) is \frac{2^k}{b - a}ba2k​. \text{RANDOM}(0, 1)RANDOM(0,1) will be called kk times in that procedure.

The expected running time is \Theta(\frac{2^k}{b - a} \cdot k)Θ(ba2k​⋅k), kk is \lceil \lg(b - a) \rceil⌈lg(ba)⌉. Considering 2^k2k is less than 2 \cdot (b - a)2⋅(ba), so the running time is O(k)O(k).

此处期望运行时间怎么算?

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

薄了代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值