diff --git a/C05-Probabilistic-Analysis-and-Randomized-Algorithms/5.1.md b/C05-Probabilistic-Analysis-and-Randomized-Algorithms/5.1.md new file mode 100644 index 00000000..1802a0c1 --- /dev/null +++ b/C05-Probabilistic-Analysis-and-Randomized-Algorithms/5.1.md @@ -0,0 +1,43 @@ +### Exercises 5.1-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. + +### `Answer` +**always**这个词表示对所有n!种组合,都能够确定,而这n!种组合已经囊括了所有的两两比较. + + +### Exercises 5.1-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? + +### `Answer` + + RANDOM(a,b): + if(a == b) + return a + mid = (a+b)/2 + r = RANDOM(0,1) + if(r == 0) + return RANDOM(a,mid) + else + return RANDOM(mid+1,b) + +running time O(lg(b-a)) + +### Exercises 5.1-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? + +### `Answer` + while true: + x = BIASED-RANDOM() + y = BIASED-RANDOM() + if x != y: + return x + +expected running time = 1/(2p(1-p)) + + +*** +Follow [@louis1992](https://github.com/gzc) on github to help finish this task. + diff --git a/C05-Probabilistic-Analysis-and-Randomized-Algorithms/5.2.md b/C05-Probabilistic-Analysis-and-Randomized-Algorithms/5.2.md new file mode 100644 index 00000000..89e348a6 --- /dev/null +++ b/C05-Probabilistic-Analysis-and-Randomized-Algorithms/5.2.md @@ -0,0 +1,52 @@ +### Exercises 5.2-1 +*** +In HIRE-ASSISTANT, assuming that the candidates are presented in a random order, what is the probability that you will hire exactly one time? What is the probability that you will hire exactly n times? + +### `Answer` +分别是1/n和1/n! + + +### Exercises 5.2-2 +*** +In HIRE-ASSISTANT, assuming that the candidates are presented in a random order, what is the probability that you will hire exactly twice? + +### `Answer` +如果第一个雇员的质量是k,那么质量高于k的雇员都必须在质量最高的雇员后面. + +假设有n个雇员,质量分别是1,2,...,n.当第一个质量为k时,只雇佣2次的概率p = 1/(n-k).因为有n-k个质量比k高的,而且必须要最高的那个在前.而第一个质量为k的概率是1/n.所以 + +![](http://latex.codecogs.com/gif.latex? p = \\sum_{k = 1}^{n-1}\\frac{1}{n}\\frac{1}{n-k} = \\frac{1}{n}\\sum_{k = 1}^{n-1}\\frac{1}{k}) + +### Exercises 5.2-3 +*** +Use indicator random variables to compute the expected value of the sum of n dice. + +### `Answer` +Expectation of a single die + +![](http://latex.codecogs.com/gif.latex? E\(X_i\) = \\frac{1+2+3+4+5+6}{6} = 3.5 ) + +Expectation of N dies + +![](http://latex.codecogs.com/gif.latex? E\(X\) = \\sum_{i = 1}^{n} E\(X_i\) = 3.5n ) + +### Exercises 5.2-4 +*** +Use indicator random variables to solve the following problem, which is known as the **hat- check problem**. Each of n 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 that get back their own hat? + +### `Answer` +每个人都是1/n的期望,所以总的是1 + +### Exercises 5.2-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 each element of A is chosen randomly, independently, and uniformly from the range 1 through n. Use indicator random variables to compute the expected number of inversions. + +### `Answer` +最简单的解法如下: + +因为概率是一样的,所以出现正序和逆序是等量的,总共有n(n-1)/2对,所以期望是n(n-1)/4对. + + +*** +Follow [@louis1992](https://github.com/gzc) on github to help finish this task. + diff --git a/C05-Probabilistic-Analysis-and-Randomized-Algorithms/5.3.md b/C05-Probabilistic-Analysis-and-Randomized-Algorithms/5.3.md new file mode 100644 index 00000000..134e688a --- /dev/null +++ b/C05-Probabilistic-Analysis-and-Randomized-Algorithms/5.3.md @@ -0,0 +1,61 @@ +### Exercises 5.3-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. His reasoning is that one 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. + +### `Answer` +感觉这题完全没意义呀... + + +### Exercises 5.3-2 +*** +Professor Kelp decides to write a procedure that will produce at random any permutation besides the identity permutation. He proposes the following procedure: + PERMUTE-WITHOUT-IDENTITY(A) 1 n ← length[A] 2 for i ← 1 to n 3 do swap A[i] ↔ A[RANDOM(i + 1, n)] + Does this code do what Professor Kelp intends? + +### `Answer` +没有,因为[1,3,2...]这样的序列虽然不是identity permutation但是却不会产生. + +### Exercises 5.3-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) 1 n ← length[A] 2 for i ← 1 to n 3 do swap A[i] ↔ A[RANDOM(1, n)] Does this code produce a uniform random permutation? Why or why not? + +### `Answer` +不可以,因为本来有n!种排列,但是这种做法每个迭代有n种选择,所以有n^n种选择.而n^n不能被n!整除,因此不是uniform的排列. + +### Exercises 5.3-4 +*** +Professor Armstrong suggests the following procedure for generating a uniform random permutation: + PERMUTE-BY-CYCLIC(A) 1 n ← length[A] 2 offset ← RANDOM(1, n) + 3 for i <- i to n + 4 do dest <- i + offset + 5 if dest > n + 6 then dest <- dest-n + 7 B[dest] -< A[i] + 8 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. + +### `Answer` +这个算法对于给定的offset,只会产生一种排列...也就是最后只有n种排列而不是n!种. 只会将原数组**平移**,谈不上**随机**. + +### Exercises 5.3-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. + +### `Answer` +![](http://latex.codecogs.com/gif.latex? P = 1\(1-\\frac{1}{n^3}\)\(1-\\frac{2}{n^3}\) \(1-\\frac{3}{n^3}\)\\ldots \\\\ ~ \\hspace{10 mm} +\\ge 1\(1-\\frac{n}{n^3}\)\(1-\\frac{n}{n^3}\) \(1-\\frac{n}{n^3}\)\\ldots \\\\ ~ \\hspace{10 mm} +\\ge \(1-\\frac{1}{n^2}\)^n \\\\ ~ \\hspace{10 mm} +\\ge 1-\\frac{1}{n} ) + +### Exercises 5.3-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. + +### `Answer` +既然冲突了,那就重新产生嘛... + + +*** +Follow [@louis1992](https://github.com/gzc) on github to help finish this task. + diff --git a/C05-Probabilistic-Analysis-and-Randomized-Algorithms/5.4.md b/C05-Probabilistic-Analysis-and-Randomized-Algorithms/5.4.md new file mode 100644 index 00000000..ae15b11e --- /dev/null +++ b/C05-Probabilistic-Analysis-and-Randomized-Algorithms/5.4.md @@ -0,0 +1,66 @@ +### Exercises 5.4-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? + +### `Answer` +![](http://latex.codecogs.com/gif.latex? 1 - \(\\frac{364}{365}\)^n \\ge \\frac{1}{2} \\rightarrow n \\ge 253 ) + +![](http://latex.codecogs.com/gif.latex? 1 - C^1_n\(\\frac{1}{365}\)\(\\frac{364}{365}\)^{n-1} - C^0_n\(\\frac{364}{365}\)^n \\ge \\frac{1}{2} \\rightarrow n \\ge 613 ) + + +### Exercises 5.4-2 +*** +Suppose that balls are tossed into b bins. Each toss is independent, and each ball is equally likely to end up in any bin. What is the expected number of ball tosses before at least one of the bins contains two balls? + +### `Answer` +跟生日悖论一样,一年有b天,期望的人数达到多少能让两个人生日一样. + +![](http://latex.codecogs.com/gif.latex? E\(B\) = 1 + \\sum_{k = 1}^{B} \\frac{B!}{\(B-k\)!B^k} ) + +check [Quora](http://www.quora.com/What-is-the-expected-number-of-ball-tosses-until-some-bin-contains-two-balls) and [wiki](https://en.wikipedia.org/wiki/Birthday_problem#Average_number_of_people) +### Exercises 5.4-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. + +### `Answer` +足够. + +### Exercises 5.4-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? + +### `Answer` +使用指示器随机变量方法.3个人生日一样的概率是1/n^2,其中n = 365 + +![](http://latex.codecogs.com/gif.latex? E = C^3_k \\frac{1}{n^2} = 1 \\rightarrow k \\approx 94) + + +### Exercises 5.4-5 +*** +What is the probability that a k-string over a set of size n is actually a k-permutation? How does this question relate to the birthday paradox? + +### `Answer` +![](http://latex.codecogs.com/gif.latex? P = 1\\cdot \\frac{n-1}{n} \\cdot \\frac{n-2}{n} \\ldots \\ \\cdot \\frac{n-k+1}{n}) + +是生日悖论的反,相当于问你大家生日都不相同的概率. +### Exercises 5.4-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? + +### `Answer` + +![](http://latex.codecogs.com/gif.latex?Pr\\{X_i\\} \\quad\\text{is the probability that ith bin is empty} \\\\ P_r\\{X_i\\} = \(\\frac{n-1}{n}\)^n = \(1-\\frac{1}{n}\)^n \\approx \\frac{1}{e} \\\\ +E[X] = \\sum_{1}^{n}E[X_i] = \\frac{n}{e} \\\\ +Pr\\{Y_i\\} \\quad\\text{is the probability that ith has one ball} \\\\ + P_r\\{Y_i\\} = n \\cdot \\frac{1}{n} \(\\frac{n-1}{n}\)^{n-1} \\approx \\frac{1}{e} \\\\E[Y] = \\sum_{1}^{n}E[Y_i] = \\frac{n}{e} \\\\) + + +### Exercises 5.4-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 lg n-2 lg lg n consecutive heads occurs. +### `Answer` +**UNSOLVED** + +*** +Follow [@louis1992](https://github.com/gzc) on github to help finish this task. + diff --git a/C05-Probabilistic-Analysis-and-Randomized-Algorithms/problem.md b/C05-Probabilistic-Analysis-and-Randomized-Algorithms/problem.md new file mode 100644 index 00000000..ac559502 --- /dev/null +++ b/C05-Probabilistic-Analysis-and-Randomized-Algorithms/problem.md @@ -0,0 +1,80 @@ +### Problems 1 : Probabilistic counting +*** +With a b-bit counter, we can ordinarily only count up to 2b - 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 a count of ni for i = 0, 1,..., 2b -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 n0 = 0. The INCREMENT operation works on a counter containing the value i in a probabilistic manner. If i = 2b - 1, then an overflow error is reported. Otherwise, the counter is increased by 1 with probability 1/(ni+1 - ni), and it remains unchanged with probability 1 - 1/(ni+1 - ni). + If we select ni = i for all i ≥ 0, then the counter is an ordinary one. More interesting situations arise if we select, say, ni = 2i-1 for i > 0 or ni = Fi (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. + a. Show that the expected value represented by the counter after n INCREMENT operations have been performed is exactly n. + b. 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. + +### `Answer` +**a.** +每一次递增操作增加的期望为 + +![](http://latex.codecogs.com/gif.latex? E = 0 \\cdot \(1-\\frac{1}{n_{i+1}-n_i}\) + 1\\cdot \(n_{i+1}-n_i\) \\cdot \\frac{1}{n_{i+1}-n_i} = 1 ) + +**b.** + +![](http://latex.codecogs.com/gif.latex? X_j \\quad\\text {stands for jth increment} \\\\ Var[X_j] = E[X_j^2] - E^2[X_j] = \(0^2 \\cdot \\frac{99}{100} + 100^2\\cdot\\frac{1}{100}\) - 1^2 = 99 \\\\ +Var[X] = \\sum_{i= 1}^{n}Var[X_i] = 99n) + + +### Problems 2 : Searching an unsorted array +*** +Thus problem examines three algorithms for searching for a value x in an unsorted array A consisting of 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. + **a.** 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. + **b.** Suppose that there is exactly one index i such that A[i] = x. What is the expected number of indices into A that must be picked before x is found and RANDOM- SEARCH terminates? + **c.** 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 must be picked before x is found and RANDOM-SEARCH terminates? Your answer should be a function of n and k. + **d.** Suppose that there are no indices i such that A[i] = x. What is the expected number of indices into A that must be picked before all elements of A have been checked 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 A[i] = x is found or the end of the array is reached. Assume that all possible permutations of the input array are equally likely. + **e.** Suppose that there is exactly one index i such that A[i] = x. What is the expected running time of DETERMINISTIC-SEARCH? What is the worst-case running time of DETERMINISTIC-SEARCH? + **f.** Generalizing your solution to part (e), suppose that there are k ≥ 1 indices i such that A[i] = x. What is the expected 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. + **g.** Suppose that there are no indices i such that A[i] = x. What is the expected 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 permuted array. + **h.** Letting k be the number of indices i such that A[i] = x, give the worst-case and expected running times of SCRAMBLE-SEARCH for the cases in which k = 0 and k = 1. Generalize your solution to handle the case in which k ≥ 1. + **i.** Which of the three searching algorithms would you use? Explain your answer. + ### `Answer` + **a.** + RANDOM-SEARCH(A, v): + B = new array[n] + count = 0 + while(count < n): + r = RANDOM(1, n) + if A[r] == v: + return r + if B[r] == false: + count += 1 + B[r] = true + return false + +**b.** +就是伯努力分布~n + +**c.** +还是伯努力分布~n/k + +**d.** +Section5.4.2,在每个盒子里至少有一个球前,要投多少个球. + +n(lnn+O(1)) + +**e.** +平均查找时间是n/2,最坏查找时间是n + +**f.** +最坏运行时间是n-k+1.平均运行时间是n/(k+1) + +**g.** +都是n + +**h.** +跟DETERMINISTIC-SEARCH是一样的,这时候的期望就是平均. + +**i.** +自然是**DETERMINISTIC-SEARCH** + + +*** +Follow [@louis1992](https://github.com/gzc) on github to help finish this task. + diff --git a/README.md b/README.md index b4d7cd84..9adb6875 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,16 @@ p + + V + 1 + 2 + 3 + 4 + p + + +