Algorithm Course Review (14.1)

Source: Internet
Author: User

Chapter 2: random algorithm, random algorithm.

Random Algorithms: When an algorithm faces a selection during execution, the selection of random rows is usually faster than the optimal selection. Therefore, the random algorithm can reduce the complexity of the algorithm to a certain extent.
Due to the existence of a random process, the same input for the problem to be solved may have completely different outputs.
Random Algorithms are generally divided into four types: Numerical Random Algorithms, Monte Carlo algorithms, Las Vegas algorithms and Sherwood algorithms.
The numerical probability algorithm is often used to solve numerical problems. This type of algorithms often obtain approximate solutions. In addition, the accuracy of the approximate solution increases with the increase of the computing time. In many cases, it is impossible or unnecessary to calculate the exact solution to a problem. Therefore, a numerical probability algorithm can be used to obtain a satisfactory solution.
The Monte Carlo algorithm is used to find an accurate solution to the problem. For many questions, the approximate solution is meaningless. For example, if a problem is solved as "yes" or "no", the second party must have one and there is no approximate solution. For example, the answer given when we require an integer factor must be accurate, and the approximate factor of an integer has no significance. A solution to the problem can be obtained using the Monte Carlo algorithm, but this solution is not necessarily correct. The probability of a correct solution depends on the time used by the algorithm. The more time the algorithm takes, the higher the probability of obtaining a correct solution. This is the main drawback of the Monte Carlo algorithm. Generally, it is impossible to effectively judge whether the obtained solution is correct.
The Las Vegas algorithm won't get an incorrect solution. Once the Las Vegas algorithm is used to find a solution, the solution must be correct. But sometimes the Las Vegas algorithm may not be able to find the solution. Similar to Monte Carlo algorithms. The probability that the Las Vegas algorithm returns a correct solution increases with the increase in computing time. For any instance that solves the problem, the same Las Vegas algorithm is used repeatedly to solve the instance multiple times, so that the probability of failure of the solution is small.
The Sherwood algorithm can always find a solution to the problem, and the obtained solution is always correct. When the computing complexity of a deterministic algorithm in the worst case is significantly different from that in the average case, we can introduce a random row into this deterministic algorithm to transform it into a schood algorithm to eliminate or reduce the difference between the quality and strength of the problem. The essence of the schedood algorithm does not avoid the worst condition of the algorithm, but tries to eliminate the associations between the worst condition and the specific instance.
For this section, refer to computer algorithm design and analysis (Third edition ).

Because random numbers need to be generated, first look at the generation of random numbers.
Real random numbers cannot be generated on real computers, but to some extent they are random, namely, pseudo-random ). The linear same-remainder method is the most common method for generating pseudo-random numbers. The random sequence a1, a2,..., an... produced by the linear same remainder method satisfies

A0 = d; an = (B * an-1 + c) mod m, n = 1, 2 ,...

D is called the seed of the random sequence. B, c, and m are related to the random performance of the random sequence. Generally, m is the machine's big number, and B is a prime number. In the reference books mentioned above, an example program is assigned,

randseed = time(0)unsigned short random(unsigned short n){    randseed = m * randseed + b;    return (unsigned short) ((randseed>>16)%n);}

The above is slightly different from the example program. In the example program, n is unsigned long, but it is to generate a random number of [0, n). The return type and n type should be consistent. Randseed is also used inside the function, so it is used as a member variable of the class, and the above function is used as a member function.

C/C ++ provides two functions related to random numbers. <stdlib>

Void srand (unsigned int seed); set the seed int rand (void) of the random number; generate a random number in the range of 0 ~ The integer of RAND_MAX. Value = rand () % p + B; a random number generated in the range of [B, B + p.

The sequence generated by rand is a pseudo-random number. When seed is determined, a random program runs twice and the generated random sequence is the same. Generally, srand is used to set the current time to seed.
The Code is as follows,

#include <iostream>#include <ctime>#include <cstdlib>using namespace std;int main(){    int N = 10;    int P = 100;    srand(time(NULL));    for(int i=0;i<N;i++)    {        cout << rand() % P << endl;    }    return 0;}

The Application of Random Algorithms, typical numeric random algorithms include pi calculation and definite points. Typical applications of the schedood algorithm, such as random and fast sorting and random selection algorithms. The Las Vegas algorithm can be used to solve the n queen problem and integer factor decomposition. Typical Monte Carlo problems include primary element and prime number testing. Next time, I will describe the applications in detail.

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.