日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/10 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 算法导论第三版 第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 - abits = ceil(log(2, range))result = 0for i = 1 to bitsr = RANDOM(0, 1)result = 2*result + rif result > rangereturn RANDOM(a, b)else return a + result

Python Implementation:

import math import randomdef random_a_b(a , b):range_a_b = b - abits = math.ceil(math.log(range_a_b,2))result = 0for i in range(1, bits + 1):r = random.choice([0,1])result = 2 * result + rif result > range_a_b:return random_a_b(a,b)else:return a + resultrandom_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?.?RANDOM(0,1)?will be called?k?times in that procedure.The expected running time is?,?k?is?. Considering? 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)*p, p*(1-p), ?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-RANDOMwhile truex = BIASED-RANDOMy = BIASED-RANDOMif x != yreturn x

Python implementation:

import math import random import numpy as npdef biased_random():return random.choice([0,1])def unbiased_random():while True:x = biased_random()y = biased_random()if x != y:return xrandom_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 , 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 .

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 .

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 - 1swap 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 nswap 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 ndest = i + offsetif dest > ndest = dest - nB[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 == 0return ? elseS = RANDOM-SAMPLE(m-1, n-1)i = RANDOM(1,n)if i ∈ SS = S ∪ {n}elseS = S ∪ {i}return S

Each combination should have a??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?. 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,…,, 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?=0. The?INCREMENT?operation works on a counter containing the value?i?in a probabilistic manner. If?i=, then the operation reports an overflow error. Otherwise, the?INCREMENT?operation increases the counter by 1 with probability?, and it leaves the counter unchanged with probability?.

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

For this problem, assume that??is large enough that the probability of an overflow error is negligible.

  • Show that the expected value represented by the counter after?n?INCREMENT?operations have been performed is exactly?n.
  • 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.

  • 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.
  • 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?
  • 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.
  • 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.

  • 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?
  • 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.
  • 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.

  • 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.
  • Which of the three searching algorithms would you use? Explain your answer.
  • ?

    ?

    總結

    以上是生活随笔為你收集整理的算法导论第三版 第5章习题答案的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。