No. 414, Python common algorithm learning

Source: Internet
Author: User

The content of this section

    1. Algorithm definition
    2. Complexity of Time
    3. Complexity of space
    4. Examples of common algorithms
1. Algorithm definition

The algorithm (algorithm) is an accurate and complete description of the solution, a series of clear instructions to solve the problem, and the algorithm represents a systematic method to describe the strategy of solving the problem. In other words, the input of a certain specification can be obtained in a limited time with the required output. If an algorithm is defective, or is not suitable for a problem, executing the algorithm will not solve the problem. Different algorithms may use different time, space, or efficiency to accomplish the same task. The merits and demerits of an algorithm can be measured by the complexity of space and time.

An algorithm should have the following seven important characteristics:

① (finiteness): The poor nature of the algorithm means that the algorithm must be able to terminate after a finite number of steps have been performed;

② Certainty (definiteness): Each step of the algorithm must have a definite definition;

③ input: An algorithm has 0 or more inputs to characterize the initial situation of the operands, so-called 0 input refers to the algorithm itself set the initial conditions;

④ output: An algorithm has one or more outputs to reflect the results of processing the input data. The algorithm without output is meaningless;

⑤ feasibility (Effectiveness): Any calculation steps performed in the algorithm can be decomposed into basic executable steps, that is, each calculation step can be completed within a finite time (also known as validity);

⑥ efficiency (High efficiency): The execution speed is fast, consumes less resources;

⑦ Robustness (robustness): responds correctly to data.

Peddling a recording network
Recording website

2. Complexity of Time

In computer science, the time complexity of the algorithm is a function, it quantitatively describes the algorithm's running time, time complexity commonly used large o symbol (Big O notation) is used to describe the function of progressive behavior of mathematical symbols. Rather, it is using another (usually simpler) function to describe the asymptotic upper bounds of the order of magnitude of a function. In mathematics, it is generally used to characterize the remainder of the truncated infinite series, especially the asymptotic series; in computer science, it is very useful in analyzing the complexity of the algorithm. ), when using this approach, the time complexity can be called asymptotic, and it examines when the input value is approaching infinity.

Big O, in short, can be thought of as the meaning of "order of" (approximately).

Infinity Asymptotic
The large o symbol is useful when analyzing the efficiency of an algorithm. For example, the time taken to solve a problem of size n (or the number of steps required) can be evaluated: T (n) = 4n^2-2n + 2.
When n increases, n^2; Items will start to dominate, while others can be ignored-for example: when n = 500,4n^2; The item is 1000 times times larger than the 2n item, so in most cases, omitting the effect of the latter on the value of the expression will be negligible.

Constant order O (1)

constant , also known as a constant, refers to an invariant value, in contrast to a variable

Why the time complexity of the algorithm below is not O (3), but O (1).

123 int sum = 0,n = 100; /*执行一次*/ sum = (1+n)*n/2; /*执行一次*/ printf("%d", sum); /*行次*/

The run-times function of this algorithm is f (n) = 3. According to our method of deriving the large O-order, the first step is to change the constant term 3 to 1. When preserving the highest order, it is found that there is no highest order at all, so the time complexity of the algorithm is O (1).

In addition, let's imagine if the statement in this algorithm sum= (1+n) *N/2 has 10 sentences, namely:

123456789101112 int sum = 0, n = 100;/* executes 1 times */  sum = (1+n) *N/2;/* Executes the 1th time */  sum = (1+n) *N/2;/* 2nd */  sum = (1+n) *N/2; /* executes 3rd time */  sum = (1+n) *N/2;/* 4th */  sum = (1+n) *N/2;/* 6th */  sum = (1+n) *N/2;/* 8th */  sum = (1+n) *n/2;/* 10th */ 

In fact, regardless of the number of N, the above two code is the difference between 3 and 12 executions. This is independent of the size of the problem (how many n), the execution time constant algorithm, we call it with O (1) time complexity, also known as the constant order.

Note: no matter how much this constant is, we all remember O (1), not O (3), O (12) and any other number , which is a common mistake for beginners.

derivation of the large O-order method

1. Replace all addition constants in run time with constant 1

2. In the modified run Count function, only the highest order is retained

3. If the highest order exists and is not 1, the constant multiplied by the item is removed

  

Logarithmic order O (log2n)

Logarithmic

If the x of a is equal to N (a>0, and A is not equal to 1), then the number x is called the logarithm of the base n of a (logarithm), which is recorded as X=logan. Where a is called the base of the logarithm, n is called the true number.
5^2 = 25, recorded as 2= Log5 25
The logarithm is an operation, and the exponent is an inverse operation. For example

①3^2=9 <==> 2=log<3>9;

②4^ (3/2) =8 <==> 3/2=log<4>8;

③10^n=35 <==> N=lg35. For ease of use, people gradually common logarithm the bottom of the 10 as LGN

Logarithmic order

123456789 int count = 1;while (count < n) {    count = count * 2; /* 时间复杂度为O(1)的程序步骤序列 */}


Since each count is multiplied by 2, it is a point closer to N.

That is, the number of 2 multiplied by greater than N will exit the loop.

Get x=log2n by 2^x=n. So the time complexity of this cycle is O (logn).

Linear order O (n)

Execution time increases proportionally with problem size growth

12345 data = [ 8,3,67,77,78,22,6,3,88,21,2]find_num = 22for i in data:    if i == 22:        print("find",find_num,i )

Linear logarithmic order O (nlog2n)

Square Order O (n^2)
1234 for i in range(100):    for k in range(100):        print(i,k)

  

Cubic Order O (n^3)
K Order O (n^k),
Exponential order O (2^n).
With the increasing of the problem scale N, the complexity of the time is increasing and the efficiency of the algorithm is less.

No. 414, Python common algorithm learning

Related Article

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.