Introduction to algorithms-Chapter 5-probability analysis and Random Algorithms

Source: Internet
Author: User

Summary: This chapter introduces probability analysis and Random Algorithms Based on employment issues. Probability Analysis is generally used to determine the running time of some algorithms. The randomization algorithm is used to force the input of the algorithm to conform to a certain probability distribution. The behavior of the randomization algorithm is determined not only by the input, but also by the value generated by a random number generator.

 

1.Probability Analysis

You can use random indicator variables for probability analysis to obtain the expected values of events.

 

 

Theorem: given event a in Sample Space S and S, if XA = I {A}, E [xa] = Pr {}

 

2.Random Algorithm

Probability Analysis assumes input. In a random algorithm, random occurs on the algorithm rather than on the input distribution, that is, for the same input, due to the introduction of random algorithms, the final arrangement also changes. This makes "even your worst enemy cannot generate the worst input series, because random sorting makes the input order irrelevant, the random algorithm runs poorly only when the random number generator generates an unlucky replacement ".

 

Method for randomly arranging Arrays

1) assign a random priority P [I] to each element a [I] of the array, and sort the elements in array A according to the priority. Sorting here will take O (nlgn) time.

Pseudocode

Permute-by-sorting

N <-length [A]

For I <-1 to n

Do P [I] = random (1, n ^ 3)

Sort a, using P as sort keys

Return

 

 

Theorem: Assuming that all priorities are unique, the permute-by-sorting process can generate an even and random arrangement of input.

 

2) arrange the given sequence in the same place. Completed in O (n) time.

Pseudocode

Randomize-in-place ()

N <-length [A]

For I <-1 to n

Do swap a [I] <-> A [random (I, n)]

 

The process randomize-in-place can calculate an even random arrangement.

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.