Introduction to algorithms 5.3 Random Algorithms

Source: Internet
Author: User

In the case of employment issues, if the applicant appears in a random order, the expected number of times to hire a new office assistant is lnn. This algorithm changes with the input changes. For a specific input, it will always
Generate a fixed number of hires.

If the applicant is randomly arranged first, the random occurrence occurs in the algorithm instead of in the input distribution.
The execution of this algorithm depends on random selection instead of input.This is the difference between Random Algorithms and probability analysis.

RANDOMIZED-HIRE-ASSISTANT(n)1 randomly permute the list of candidates2 best ← 0       candidate 0 is a least-qualified dummy candidate3 for i ← 1 to n4       do interview candidate i5          if candidate i is better than candidate best6             then besti7                  hire candidate i

The random algorithm is actuallySort input in advance in the algorithm.

Here, we will introduce two algorithms for shuffling input. They are random shuffling. Both methods produce even random sorting. (Each sort may be generated ).

Pay special attention to the definition of uniform and random arrangement !!!! The two algorithms given below in this book rearrange the arrays, produce even and random sorting, and provide a strict mathematical proof that the theory is very high, here we will focus on the introduction and understanding of these two methods, without further proof, you can go to the book to see the proof.

Certificate ---------------------------------------------------------------------------------------------------------------------------------------------------

Randomly arrange Arrays:

1) permute-by-sorting (A)

Assign a random priority to each element of the array, and sort array A according to the priority.

PERMUTE-BY-SORTING(A)1 nlength[A]2 for i ← 1 to n3      do P[i] = RANDOM(1, n

3

)4 sort A, using P as sort keys5 return A

This obviously can disrupt the order of the array. The key is to prove that it is even and random. This is based on the definition of the even random arrangement, that is, the probability of generating each arrangement is 1/n!

Potential:

A) if an algorithm still produces less than n! Obviously, it is not an even random arrangement algorithm. Exercise after class

B) it may be said that to prove that an arrangement is evenly and randomly arranged, you only need to prove that the probability of each element appearing at each position is 1/N. This is a misunderstanding of the even random arrangement. Exercise after class

Permute-by-sorting is difficult. You cannot directly see how many sort can be generated.

2) randomize-in-place (A)

Loop over the array, and an Exchange (the following code) between each element a [I] And a [I] to the last element is easy to see that the total number of arrays is n! Items

RANDOMIZE-IN-PLACE(A)1 nlength[A]2 for ito n3      do swap A[i]  A[RANDOM(i, n)]

For example, 1) 2) the proof of uniform and random arrangement is not mentioned here, and it is in the book.

Certificate --------------------------------------------------------------------------------------------------------------------------------------------------

Exercise

(It turns out that the process of even and random sorting is very troublesome. We will focus on the bottom line A and B to determine whether an algorithm can generate even and random sorting)

5.3.2 Professor Kelp decided to use the following algorithms to randomly generate an arbitrary arrangement of non-same arrays. Did he implement his intention?

PERMUTE-WITHOUT-IDENTITY(A)1 nlength[A]2 for i ← 1 to n3      do swap A[i]  A[RANDOM(i + 1, n)]

No, this code can only be generated (n-1) in total )! Sort (self-calculation), to achieve the intention of the necessary conditions need to be able to generate n! -One sort.

A.

5.3.3 assume that a [I] And a [I .... n] a random number exchange (note that this is our correct algorithm), but each time it is exchanged with the number at any position on the array, can it generate a uniform random arrangement?

Permute-with-all (A) 1NBytesLength[A] 2For ILimit 1To N3DoSwapA[I]A[Random (1,N)]

No, it is also the) point. You can analyze it yourself.

For a special case, assume that the permutation length is n = 3. Then, we need to call the random three times. If one of the three values is returned each time, there will be 27 possible results after the execution of the permute-with-all process. Because the arrangement length is N, there are 3 in total! = 6 arrays. If permute-with-all generates a uniform random arrangement, the probability of each permutation is 1/6. That is to say, each arrangement should appear m times so that M/27 = 1/6. Since there is no integer m, M/27 = 1/6, permute-with-all does not necessarily produce a uniform random arrangement.


5.3.4 Armstrong we recommend that you use the following process to produce a uniform and random arrangement:

Permute-by-cyclic (A) 1NBytesLength[A] 2OffsetRandom random (1,N) 3For ILimit 1To N4Do DestBytesI+Offset5If Dest>N6Then DestBytesDest-N7B[Dest] BytesA[I] 8Return B
It is proved that the probability of any element appearing at any position in B is 1/N, and this algorithm cannot produce a uniform and random arrangement.

1) Pay attention to the 2nd rows. The offset value is obtained here. It is a fixed value when used in.

Offset can have n values, so the probability of any element appearing at any position in B is 1/N.

2) This code can only generate n Different arrays (the reason is still 2nd lines)


This shows part B of the line)




Certificate ------------------------------------------------------------------------------------------------------------------------------------------------


Summary:

1) Definition of uniform and random arrangement

2) Two evenly and randomly arranged

3) Even random sorting is difficult to prove, but how can we determine whether an algorithm can produce even random sorting?



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.