算法导论第三版 第5章习题答案

2020/11/04 初稿,主要加了Python实现。

参考资料:

https://walkccc.github.io/CLRS/

https://blog.csdn.net/rx_wen/article/details/5702703

https://ita.skanev.com/

5.1和5.2仔细理解与核对过,剩余部分只是为了完整性拷贝过来。

5 Probabilistic Analysis and Randomized Algorithms

5.1 The hiring problem

1.Show that the assumption that we are always able to determine which candidate is best in line 4 of procedure 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 (∀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 A is better than B and B is better than C, then A is better than C.
  • Antisymmetric: If A is better than B, then B is not better than A.

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.

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

As (b−a) could be any number, we need at least ⌈lg(b−a)⌉ bits to represent the number. We set ⌈lg(b−a)⌉ as k. Basically, we need to call RANDOM(0,1) k times. If the number represented by binary is bigger than b - 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 = 1 to bits
        r = RANDOM(0, 1)
        result = 2*result + r
    if result > range
        return RANDOM(a, b)
    else return a + result

Python Implementation:

import math
import random

def random_a_b(a , b):
    range_a_b = b - a
    bits = math.ceil(math.log(range_a_b,2))
    result = 0

    for i in range(1, bits + 1):
        r = random.choice([0,1])
        result = 2 * result + r
    
    if result > range_a_b:
        return random_a_b(a,b)
    else:
        return a + result

random_numbers = []
for i in range(1,100):
    random_numbers.append(random_a_b(5,30))

import numpy as np
print(random_numbers)
print(np.unique(random_numbers,return_counts=True))

The expectation of times of calling procedure RANDOM(a,b) is \frac{2^k}{b - a}. RANDOM(0,1) will be called k times in that procedure.The expected running time is \Theta(\frac{2^k}{b - a} \cdot k), k is \lceil \lg(b - a) \rceil. Considering 2^k is less than 2⋅(b−a), so the running time is O(k).

3.Suppose that you want to output 0 with probability 1 / 2 and 1 with probability 1 / 2. At your disposal is a procedure BIASED-RANDOM, that outputs either 0 or 1. It outputs 1 with some probability p and 0 with probability 1 - p, where 0 < p < 1, but you do not know what p is. Give an algorithm that uses BIASED-RANDOM as a subroutine, and returns an unbiased answer, returning 0 with probability 1 / 2 and 1 with probability 1/2. What is the expected running time of your algorithm as a function of p?

If we run BIASED-RANDOM twice, we might get of of following result: 00, 01, 10, 11. And the probabilities for getting each of them is: (1-p)^2 , (1-p)*p, p*(1-p), p^2 respectively. We can see that the probabilities for getting 01 equals 10. So, if we can constraint the output to either 01 or 10, we have a UNBIASED-RANDOM that can return 01 or 10 with probability of 1/2 each. And we can simply replace 01, 10 with 0 and 1 to get desired function. How can we constraint the result in 01 and 10? We can use the similar idea used in Ex5.1-2, that's abandoning any result that doesn't belong to 01 and 10 till we get one of them.

And there's a risk in this algorithm. If p is very close to 1 or 0, we may need to try a looooot of times to get either 01 or 10 which makes a very poor performance. To get around this, we can invoke BIASED-RANDOM more times. As we know, the probability of getting a full 0 or 1 permutation is p power the number of times invoking BIASED-RANDOM. And because p is between 0 and 1, the more we invoke BIASED-RANDOM, the less will the probability be, consequently the quicker we don't get full 0 or 1.

The pseudo code is as follow:

UNBIASED-RANDOM
    while true
        x = BIASED-RANDOM
        y = BIASED-RANDOM
        if x != y
            return x

Python implementation:

import math
import random
import numpy as np

def biased_random():
    return random.choice([0,1])

def unbiased_random():
    while True:
        x = biased_random()
        y = biased_random()
        if x != y:
            return x

random_numbers = []
for i in range(1,100):
    random_numbers.append(unbiased_random())

print(np.unique(random_numbers,return_counts=True))

This algorithm actually uses the equivalence of the probability of occurrence of 01 and 10, and subtly eliminates the unequal 00 and 11 to 01 and 10, thus eliminating the probability that its probability is not equivalent. Each iteration is a Bernoulli trial, where "success" means that the iteration does return a value.

5.2 Indicator random variables

1.In \text{HIRE-ASSISTANT}, assuming that the candidates are presented in a random order, what is the probability that you hire exactly one time? What is the probability you hire exactly n times?

You will hire exactly one time if the best candidate is at first. There are (n − 1)! orderings with the best candidate being at first, so the probability that you hire exactly one time is \frac{(n - 1)!}{n!} = \frac{1}{n}.

You will hire exactly n times if the candidates are presented in increasing order. There is only an ordering for this situation, so the probability that you hire exactly n times is \frac{1}{n!}.

2.In HIRE-ASSISTANT, assuming that the candidates are presented in a random order, what is the probability that you hire exactly twice?

Note that

  • Candidate 1 is always hired
  • The best candidate (candidate whose rank is n) is always hired
  • If the best candidate is candidate 1, then that's the only candidate hired.

3.Use indicator random variables to compute the expected value of the sum of n dice.

4.Use indicator random variables to solve the following problem, which is known as the hat-check problem. Each of nn customers gives a hat to a hat-check person at a restaurant. The hat-check person gives the hats back to the customers in a random order. What is the expected number of customers who get back their hat?

5.Let A[1..n] be an array of n distinct numbers. If i < j and A[i] > A[j], then the pair (i, j) is called an inversion of A. (See Problem 2-4 for more on inversions.) Suppose that the elements of A form a uniform random permutation of 〈1,2,…,n〉. Use indicator random variables to compute the expected number of inversions.

 

5.3 Randomized algorithms

1.Professor Marceau objects to the loop invariant used in the proof of Lemma 5.5. He questions whether it is true prior to the first iteration. He reasons that we could just as easily declare that an empty subarray contains no 0-permutations. Therefore, the probability that an empty subarray contains a 0-permutation should be 0, thus invalidating the loop invariant prior to the first iteration. Rewrite the procedure RANDOMIZE-IN-PLACE so that its associated loop invariant applies to a nonempty subarray prior to the first iteration, and modify the proof of Lemma 5.5 for your procedure.

I'm not going to write any code since this is trivial.We can pick up an element at random before entering the loop and replace it with the first one. Now our invariant holds initially for a 1-permutation.


2.Professor Kelp decides to write a procedure that produces at random any permutation besides the identity permutation. He proposes the following procedure:

PERMUTE-WITHOUT-IDENTITY(A)
n = A.length
for i = 1 to n - 1
    swap A[i] with A[RANDOM(i + 1, n)]

Does this code do what Professor Kelp intends?

It does not. It always changes the position of each element. We cannot get the identity permutation, but we also can't get any permutation where an element is at the same place.

3.Suppose that instead of swapping element A[i] with a random element from the subarray A[i…n], we swapped it with a random element from anywhere in the array:

PERMUTE-WITH-ALL(A)
n = A.length
for i = 1 to n
    swap A[i] with A[RANDOM(1,n)]

Does this code produce a uniform random permutation? Why or why not?

It does not. Intuitivelly, this one can go in n different ways while there are n! combinations. Since n! does not divide nn, there is no way that this can be a uniform distribution. (Why doesn't it divide nn? That's the intuitive part. n! is divisable by n−1, but n can't be for n>2).

Of course, this is a popular problem and there are tons of posts and papers written on it. Here's one from Coding Horror

4.Professor Armstrong suggests the following procedure for generating a uniform random permutation:

n = A.length
let B[1..n] be a new array
offset = RANDOM(1, n)
for i = 1 to n
    dest = i + offset
    if dest > n
        dest = dest - n
    B[dest] = A[i]
return B

Show that each element A[i] has a 1/n probability of winding up in any particular position in B. Then show that Professor Armstrong is mistaken by showing that the resulting permutation is not uniformly random.

Both are trivial.

A[i] will go to B[j] if j≡offset+i(modn). There is 1/n probability of that happening.

It does not generate all permutations - it only generates permutations that can be obtained from the initial input by cycling.

BTW, "Armstrong" and "cycling". Nice pun.

5.⋆ Prove that in the array P in procedure PERMUTE-BY-SORTING, the probability that all elements are unique is at least 1−1/n.

6.Explain how to implement the algorithm PERMUTE-BY-SORTING to handle the case in which two or more priorities are identical. That is, your algorithm should produce a uniform random permutation, even if two or more priorities are identical.

This is a stupid algorithm and requires a stupid solution. Just generate new priorities and try again.

Suppose we want to create a random sample of the set {1,2,3,…,n}, that is, an m-element subset S, where 0≤m≤n, such that each m-subset is equally likely to be created. One way would be to set A[i]=i for i=1,2,3,…,n, call RANDOMIZE-IN-PLACE(A), and then take just the first m array elements. This method would make n calls to the RANDOM procedure. If n is much larger than m, we can create a random sample with fewer calls to RANDOM. Show that the following recursive procedure returns a random m-subset S of {1,2,…,n}, in which each m-subset is equally likely, while making only m calls to RANDOM:

RANDOM-SAMPLE(m,n)
if m == 0
    return ∅
else
    S = RANDOM-SAMPLE(m-1, n-1)
    i = RANDOM(1,n)
    if i ∈ S
        S = S ∪ {n}
    else
        S = S ∪ {i}
    return S

Each combination should have a 1/\binom{n}{m} chance of showing up. Let's establish an invariant for RANDOM-SAMPLE(m,n). We are going to go with:

RANDOM-SAMPLE(m,n) returns a uniformly distributed combination.

5.4 Probabilistic analysis and further uses of indicator random variables

1.How many people must there be in a room before the probability that someone has the same birthday as you do is at least 1/2? How many people must there be before the probability that at least two people have a birthday on July 4 is greater than 1/2?

2.Suppose that we toss balls into b bins until some bin contains two balls. Each toss is independent, and each ball is equally likely to end up in any bin. What is the expected number of ball tosses?

This is just a restatement of the birthday problem. I consider this all that needs to be said on this subject.

3.⋆ For the analysis of the birthday paradox, is it important that the birthdays be mutually independent, or is pairwise independence sufficient? Justify your answer.

Pairwise independence is enough. It's sufficient for the derivation after (5.6).

4.⋆ How many people should be invited to a party in order to make it likely that there are three people with the same birthday?

5.⋆ What is the probability that a k-string over a set of size n forms a k-permutation? How does this question relate to the birthday paradox?

6.⋆ Suppose that n balls are tossed into n bins, where each toss is independent and the ball is equally likely to end up in any bin. What is the expected number of empty bins? What is the expected number of bins with exactly one ball?

 

7.⋆ Sharpen the lower bound on streak length by showing that in n flips of a fair coin, the probability is less than 1/n that no streak longer than lgn−2lglgn consecutive heads occurs.

(UNSOLVED) Too much work, too little connection to reality.

 

Problems

1.With a b-bit counter, we can ordinarily only count up to 2^n - 1. With R. Morris's probabilistic counting, we can count up to a much larger value at the expense of some loss of precision.

We let a counter value of i represent that a count of ni for i=0,1,…,2^b - 1, where the ni form an increasing sequence of nonnegative values. We assume that the initial value of the counter is 0, representing a count of n_0=0. The INCREMENT operation works on a counter containing the value i in a probabilistic manner. If i=2^b - 1, then the operation reports an overflow error. Otherwise, the INCREMENT operation increases the counter by 1 with probability 1/(n_{i+1} - n_{i}), and it leaves the counter unchanged with probability 1-1/(n_{i+1} - n_{i}).

If we select n_i = i for all i≥0, then the counter is an ordinary one. More interesting situations arise if we select, say,n_i = 2^i -1 for i>0 or n_i = F_i (the ith Fibonacci number - see Section 3.2).

For this problem, assume that n_{2^b} - 1 is large enough that the probability of an overflow error is negligible.

  1. Show that the expected value represented by the counter after n INCREMENT operations have been performed is exactly n.
  2. The analysis of the variance of the count represented by the counter depends on the sequence of the ni. Let us consider a simple case: ni=100i for all i≥0. Estimate the variance in the value represented by the register after n INCREMENT operations have been performed.

 

 

2.Searching in unsorted array

The problem examines three algorithms for searching for a value x in an unsorted array A consisting for n elements.

Consider the following randomized strategy: pick a random index i into A. If A[i]=x, then we terminate; otherwise, we continue the search by picking a new random index into A. We continue picking random indices into A until we find an index j such that A[j]=x or until we have checked every element of A. Note that we pick from the whole set of indices each time, so that we may examine a given element more than once.

  1. Write pseudocode for a procedure RANDOM-SEARCH to implement the strategy above. Be sure that your algorithm terminates when all indices into A have been picked.
  2. Suppose that there is exactly one index i such that A[i]=x. What is the expected number of indices into A that we must pick before we find x and RANDOM-SEARCH terminates?
  3. Generalizing your solution to part (b), suppose that there are k≥1 indices i such that A[i]=x. What is the expected number of indices into A that we must pick before we find x and RANDOM-SEARCH terminates? Your answer should be a function of n and k.
  4. Suppose that there are no indices i such that A[i]=x. What is the expected number of indices into A that we must pick before we have checked all elements of A and RANDOM-SEARCH terminates?

Now consider a deterministic linear search algorithm, which we refer to as DETERMINISTIC-SEARCH. Specifically, the algorithm searches A for x in order, considering A[1],A[2],A[3],…,A[n] until either it finds A[i]=x or it reaches the end of the array. Assume that possible permutations of the input array are equally likely.

  1. Suppose that there is exactly one index i such that A[i]=x. What is the average-case running time of DETERMINISTIC-SEARCH? What is the worst-case running time of DETERMINISTIC-SEARCH?
  2. Generalizing your solution to part (e), suppose that there are k≥1 indices i such that A[i]=x. What is the average-case running time of DETERMINISTIC-SEARCH? What is the worst-case running time of DETERMINISTIC-SEARCH? Your answer should be a function of n and k.
  3. Suppose that there are no indices i such that A[i]=x. What is the average-case running time of DETERMINISTIC-SEARCH? What is the worst-case running time of DETERMINISTIC-SEARCH?

Finally, consider a randomized algorithm SCRAMBLE-SEARCH that works by first randomly permuting the input array and then running the deterministic linear search given above on the resulting permuting array.

  1. Letting k be the number of indices i such that A[i]=x, give the worst-case and expected running time of SCRAMBLE-SEARCH for the cases in which k=0 and k=1. Generalizing your solution to handle the case in which k≥1.
  2. Which of the three searching algorithms would you use? Explain your answer.

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值