Probabilistic analysis and stochastic algorithms--Introduction to Algorithms (5)

Source: Internet
Author: User

1. Employment issues

(1) Ask questions

Your boss has asked you to hire a programmer for the company, and now there are n individuals who have cast their resumes. You randomly pick one of these n resumes every day, and then let the corresponding candidates come to the interview, and each interview will cost C1. And your employment principle is: If the current programmer is better than the current programmer, then you quit the current programmer, and expensive C2 to hire the interview of the programmer. Your boss is willing to pay for the strategy, but want you to estimate how much it will cost.

Here is the pseudo-code for this strategy:

(2) unscientific algorithms

Because no matter where the best interviewer appears, we always have to interview all the people, so the cost of the interview is fixed, for c1*n; assuming that you need to hire m for this n interview, then you will spend C2 * m hiring fee. Therefore the total cost w is: W = c1*n + C2 * M.

You might think the best thing is: The best interviewers appear first, m=1, and worst of all: the best interviewers appear in the last, M=n. So the average situation is, M = (n+1)/2. This simple practice of averaging from the upper and lower limits is unscientific here, because the distribution of the total cost m is not uniform (as can be seen below).

2. Probability analysis

We consider the average number of people to be employed m. Set the random variable XI to indicate whether the person interviewed by the first interview is employed (if the employed XI is 1, otherwise 0).

So the expectation of M E (m) is therefore:

So the average will be spent: w = c1*n + C2 * E (m) = O (ln n), which is much better than the worst-case employment cost O (n).

3. Stochastic algorithms

Above, we analyze the average of the distribution of M, but in many cases we are unable to know the input distribution information. But we might be able to design a random algorithm.

For the above employment problem, we can arrange the candidates randomly before the algorithm is run, in order to strengthen all the permutations are likely to occur.

The following is a pseudo-code description of the random algorithm:

Now our focus is on how to generate a random arrangement. Without losing its generality, we assume that an array a, containing the n elements of 1~n, our goal is to construct a random array of group A. Two algorithms are described below:

Method ① : We first construct an array of length n, and the elements in the array p are random numbers from 1 to N3. We then take the elements in the array p as the priority of the corresponding position elements in the array A, and the elements in array A are arranged in the priority size (the arbitrarily arranged precedence). For example, A={1, 2, 3, 4}, p = {13, 1, 60, 28} (array P illustrates array A, 1 priority is 13,2 priority is 1, ...) ), then the a={2,1,4,3} (arranged by priority from small to large). The pseudo code is as follows:

As for the sorting algorithm, the number will be discussed later.

Prove a little (be interested to see the original book yourself).

method ②: relatively simple, directly post pseudo-code:

Proof slightly.

Here is the Java implementation code for the generation algorithm of these two random arrays:

 Public Static voidMain (string[] args) {PrintArray (permutebysort));p Rintarray (Randomizeinplace (10));}/** * Generate Random Array (Method 1) * * @param length * Array size * @return * *Private Static int[] Permutebysort (intLength) {int[] A =New int[Length]; for(inti = 0; i < length; i++) {A[i] = i + 1;} Random random =NewRandom (System.currenttimemillis ());int[] p =New int[Length]; for(inti = 0; i < p.length; i++) {//Note that forcing type conversions here is likely to lose dataP[i] = Random.nextint ((int) Math.pow (length, 3)) + 1;}//Bubble Sort Method for(inti = 0; i < p.length; i++) { for(intj = 0; J < p.length-1-I; J + +) {if(P[j] > p[j + 1]) {inttemp = P[j];p [j] = P[j + 1];p [j + 1] = Temp;temp = A[j];a[j] = a[j + 1];a[j + 1] = temp;}}returnA;}/** * Generate Random Array (Method 2) * * @param length * Array size * @return * *Private Static int[] Randomizeinplace (intLength) {int[] A =New int[Length]; for(inti = 0; i < length; i++) {A[i] = i + 1;} Random random =NewRandom (System.currenttimemillis ()); for(inti = 0; i < a.length; i++) {intSwapindex = Random.nextint (a.length-i) + i;inttemp = A[i];a[i] = A[swapindex];a[swapindex] = temp;}returnA;}/** * Print Array * * @param a * *Private Static voidPrintArray (int[] a) { for(intI:A) {System.out.print (i + ")  ");} System.out.println ();}

PS: all of these are excerpted from the Chinese translation of the introduction to algorithms. I just extracted the article in the personal opinion of the more important points, added some personal understanding, for reference only.

Probabilistic analysis and stochastic algorithms--Introduction to Algorithms (5)

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.