Summary of Random Algorithms

Source: Internet
Author: User

A random algorithm involves a large amount of probability theory knowledge. Sometimes it is rare to carefully look at the derivation process. Of course, it is advantageous to fully understand the derivation process. If you do not know the derivation process, at least remember the conclusion. This article summarizes some of the most common questions about random algorithms, as well as the preparation for the interview.It should be noted that all the random functions used here assume that they can generate integers in the range [a, B] randomly, that is, the probability of each integer is equal. (Not necessarily in reality, but who cares? The world is so random)


I. Randomly arrange Arrays

Assume that an array A contains elements 1 to n. Our goal is to construct a random arrangement of the array.

A common method is to assign a random priority P [I] to each element of the array, and then sort the Array Based on the priority. For example, our array is a = {1, 2, 3, 4}. If the selected priority array is P = {36, 3, 97, 19 }, then we can get the series B = {2, 4, 1, 3}, because 3 has the highest priority (97), and 2 has the lowest priority (3 ). This algorithm needs to generate a priority array and sort the original Array Using the priority array. Here we will not detail it. There is also a better way to get a random array.

A better way to generate random arrays is to arrange the given arrays in the same place, which can be completed within the O (n) time. The pseudocode is as follows:

RANDOMIZE-IN-PLACE ( A , n )     for i ←1 to n          do swap A[i] A[RANDOM(i , n )]

As shown in the code, element a [I] is randomly selected from element a [I] To element a [n] During the I iteration. After the I iteration, we will never change a [I] again.

A [I] Where J is located, the probability is 1/N. This is easily deduced. For example, the probability that a [1] is at location 1 is 1/n. Obviously, because the probability that a [1] is not replaced by elements 1 to n is 1/N, then a [I] will not be changed. The probability that a [1] is located at location 2 is also 1/N, because a [1] wants to be located at location 2, it must be exchanged with a [k] for the first time (k = 2... n), and replace a [2] with a [k] for the second time. The probability of the first exchange with a [k] is (n-1)/n, the second replacement probability is 1/(n-1), so the total probability is (n-1)/n * 1/(n-1)
= 1/N. Likewise, other cases can be deduced.

Of course, this condition can only be a necessary condition for random array arrangement, that is, the probability that element a [I] is located at position J is 1/N does not necessarily indicate that this can generate a random array. Because it may generate less than N !, Although the probability is equal, but the number of permutation does not meet the requirements, there is an inverse example above in the introduction to algorithms.

The algorithm randomize-in-place can produce a uniform and random arrangement. The proof process is as follows:

First, the concept of K arrangement is given. The so-called K arrangement is to select k elements from n elements, so there is a total of n! /(N-k )! K.

Cycle unchanged: For loop before iteration I, for each possible I-1 arrangement, sub array a [1... i-1] the probability of containing the I-1 arrangement is (n-I + 1 )! /N !.

Initialization: Before the first iteration, if I = 1, the loop is not changed, indicating that the sub-array a [1... i-1] contains the probability of the 0 arrangement is (n-1 + 1 )! /N! = 1. A [1... 0] is an empty array, and 0 is arranged without any elements. Therefore, A contains the probability of all possible 0 orders as 1. It is not changed.

Maintenance: Assuming that the I-1 arrangement of the array appears in a [1... I-1] prior to iteration I, the probability is (n-I + 1 )! /N !, After iteration I, the probability that all I arrays appear in a [1... I] is (n-I )! /N !. The following conclusions are derived:

Consider a special I arrangement P = {x1, x2 ,... XI}, which is arranged by a I-1 p '= {x1, x2 ,..., xi−1} is followed by a Xi. Set two event variables E1 and E2:

E1 for this algorithm will arrange the p 'to a [1... I-1] event, the probability from the induction hypothesis is known as PR (E1) = (n-I + 1 )! /N !.

E2 is the event that puts Xi into a [I] During iteration I.

Therefore, we can see the I arrangement in a [1... i] the probability is PR {E2 1_e1} = Pr {E2 | E1} PR {E1 }. PR {E2 | E1} = 1/(n −i + 1), so

PR {E2 1_e1} = Pr {E2 | E1} PR {E1} = 1/(n −i + 1) * (N −i + 1 )! /N! = (N−i )! /N !.

End:At the end, I = n + 1. Therefore, we can obtain that a [1... n] is a probability of 1/n given to N !.

Extended question

If the above random sorting algorithm is written as below, can we generate even random sorting?

PERMUTE-WITH-ALL( A , n )     for i ←1 to n          do swap A[i] A[RANDOM(1 , n )]

Note: This algorithm cannot produce even random sorting.. Assuming n = 3, the algorithm can generate 3*3*3 = 27 outputs, and 3 elements have only 3! = 6 different arrays. To make these arrays have a probability of equal to 1/6, the number of occurrences of m in each arrangement must satisfy M/27 = 1/6. Obviously, no such integer meets the condition. In fact, the probability of occurrence of each permutation is as follows. For example, the probability of occurrence of {4/27, 3} is 1/6, not equal.


2. Randomly select a number

Question: How to randomly select an integer stream with an unknown length? (Random means that the probability of each number being selected is equal)

Solution 1:

If the data stream is not very long, it can exist in the array and then be randomly selected from the array. Of course, the question is about unknown length, so if the length is too large to be saved in the memory, it will be very troublesome. This solution has its own limitations.

Solution 2:

If the data stream ends after 1st digits, 1st digits are required.

If the data stream ends after 2nd digits, the probability of selecting 2nd digits is 1/2. we replace the preceding random number with 1/2 digits with a probability of 2nd to obtain a new random number.

.........

If the data flow ends after the nth digit, we select the nth digit with a probability of 1/N, that is, we replace the selected random number with the nth number with the probability of 1/N to obtain a new random number.

A simple method is to use the random function f (n) = bigrand () % n, where bigrand () returns a large random integer. When the data flow reaches the nth number, if F (n) = 0, replace the selected random number. This ensures that the probability of each number being selected is 1/N. For example, if n = 1, F (1) = 0, then select 1st numbers. If n = 2, the probability of 2nd numbers being selected is 1/2, and so on, when the number length is N, the probability of N digits being selected is 1/N.


3. Randomly select M numbers Question: The program input contains two integers m and n, where M <n, and the output is 0 ~ An ordered list of M random integers in the n-1 range. Duplicates are not allowed. From the perspective of probability, we hope to get an orderly selection without repetition, where each choice has the same probability.
Solution 1: Consider a simple example. When m = 2, n = 5, we need ~ 4. Select two ordered integers with a medium probability for these five integers, and they cannot be repeated. If the following conditions are selected: bigrand () % 5 <2, the probability of 0 is 2/5. However, we cannot select 1 with the same probability, because after 0 is selected, we should select 1 with a probability of 1/4. If 0 is not selected, we should select 1 with a probability of 2/4. The pseudo code is as follows:
select = mremaining = nfor i = [0, n)    if (bigrand() % remaining < select)         print i         select--    remaining--
As long as the condition m <= N is met, the program outputs M ordered integers, not many. Do not select multiple, because select -- is selected for each number, so that when select is reduced to 0, it will not be selected again. At the same time, there will be no fewer choices, because each time a remaining --, when select/remaining = 1, a number will be selected. The probability of each subset being selected is equal. For example, if you select 5 to 2, there are a total of C () = 10 subsets, such }... and so on. The probability of each subset being selected is 1/10. In a more general derivation, the number of subsets of n m is a total of C (n, m), considering a specific M sequence, such as 0... then, the probability is M/N.
* M-1)/(n-1) *... 1/(n-m + 1) = 1/C (n, m), we can see that the probability is equal. The implementation code of the C ++ language is as follows. The time complexity of this algorithm is O (n ).
Void genknuth (int m, int N) {for (INT I = 0; I <n; I ++) if (bigrand () % (n-I) <m) {// Add 1 to I in n-I, which is equivalent to reducing 1 cout for remaining each time <I <Endl; m --; // minus 1 }}

Solution 2:Insert a random integer into an empty set until the number reaches M. Since each insert operation requires O (logm) time, it takes O (m) Time to traverse the set. This algorithm requires O (mlogm) in total. Code:

void gensets(int m, int n){     set<int> S;     while (S.size() < m)           S.insert(bigrand() % n);     set<int>::iterator it;     for (it = S.begin(); it != S.end(); it++)          cout << *it << endl;}

Solution 3:Using the thought of the random array above, first sort the first M numbers randomly, then sort the M numbers and output them. Code omitted.

Iv. rand7 () generate rand10 () for issues see http://blog.csdn.net/ssjhust123/article/details/7753012

V. Interesting probability questions 1) Male and female: What is the ratio of men to women in a country where male is preferred? Every family wants to have boys in a country where boys and girls are important. If the children they give birth to are girls, they will be reborn until the children are born. What is the ratio of men to women in such a country?

Answer. Among all the First Children, the ratio of men to women is; among all the second children, the ratio of men to women is ;.... among all the children born with N, the ratio of men to women is. So the total proportion of men and women is.

 

2) Appointment Problems: The two met at a meeting between 5 and 6 at a certain place. The first person waited for 20 minutes before leaving, asking them the probability of meeting.

Answer: set the two to reach their destination at and respectively, the condition they can meet is | X-Y | <= 20, the whole range is S = {(x, y): 0 = <x <= 60, 0 = <Y <= 60}. Therefore, the Meeting condition is the area shown in the figure, the probability is (60 ^ 2-40 ^ 2)/60 ^ 2 = 5/9.

3) hats: There are N Customers. Each of them gives a waiter a hat and the waiter returns it to the customer in random order. What are the expectations of the customers who have their hats?

Answer: It is easier to use random variables to solve this problem. Defining a random variable X is equal to the number of customers who can get their hats. We need to calculate E [X]. For I = 1, 2... n, define xi = I {customer I get his hat}, then x = X1 + X2 +... XN. Since the return order of HATS is random, the probability of each customer getting their hats is 1/N, that is, Pr (xi = 1) = 1/N, and E (XI) = 1/N, so E (x) = E (X1 + X2 +... XN) = E (X1) + E (X2) +... E (Xn) = N * 1/n
= 1. That is, about one customer can get his hat.

4) birthday paradox:
How many people must have at least one room before two people can have their birthdays on the same day?
Answer: For each pair (I, j) in the room K, define the indicator variable Xij = {I and j birthday on the same day}, Then I and j birthday are the same, Xij = 1, otherwise, Xij = 0. The probability PR (Xij = 1) = 1/N of two people on the same day's birthday. If X is used to represent the number of pairs of two persons on the same natural day, then E (x) = E (Σ ki = 1 Σ kJ = I + 1Xij) = C (K, 2) * 1/N = K (k-1)/2n, so K (k-1)/2n> = 1, can get K> = 28, that is, at least 28 people are expected to have their birthdays on the same day.


5)If the probability of a car driving over the highway within 30 minutes is 0.95, what is the probability of a car driving over 10 minutes? (Assuming the condition of common probability)

Answer: If the probability of a car driving in 10 minutes is X, then the probability of not seeing a car driving is. The probability of not seeing a car driving in 30 minutes is) ^ 3, that is, 0.05. Therefore, the equation (0.05) ^ 3 = 0.63 is obtained, and the equation x is about.

 

References

Introduction to Algorithms

Programming Pearl 2

Http://blog.csdn.net/wxwtj/article/details/6621430





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.