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,b∈A: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)(b−a) could be any number, we need at least \lceil \lg(b - a) \rceil⌈lg(b−a)⌉ bits to represent the number. We set \lceil \lg(b - a) \rceil⌈lg(b−a)⌉ 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 - ab−a, 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}b−a2k. \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)Θ(b−a2k⋅k), kk is \lceil \lg(b - a) \rceil⌈lg(b−a)⌉. Considering 2^k2k is less than 2 \cdot (b - a)2⋅(b−a), so the running time is O(k)O(k).
此处期望运行时间怎么算?