The basis of algorithm time complexity analysis

Source: Internet
Author: User

Summary
This paper discusses an important problem in the field of algorithm analysis--the basic content of time complexity analysis. This paper will first clarify the significance of time complexity, and then discuss its mathematical definition and related derivation in a formal way. To help you understand the concept in essence.

Objective
Usually, for a given algorithm, we need to do two analyses. The first is to prove the correctness of the algorithm mathematically, this step mainly uses the formal proof method and the related inference mode, such as the cyclic invariant, the mathematical induction method and so on. On the basis of proving that the algorithm is correct, the second part is the time complexity of the analysis algorithm. The time complexity of the algorithm reflects the increment of the program execution time with the increase of the input scale, and it can well reflect the merits or demerits of the algorithm to a great extent. Therefore, as a programmer, it is necessary to master the basic algorithm time complexity analysis method.
But many friends do not understand the concept clearly, the reason, mainly because not from the mathematical level of understanding its essence, but accustomed to from the intuitive understanding. Next, we step closer to the mathematical nature of the algorithm's time complexity.

Mathematical significance of algorithm time complexity
Mathematically defined, given the algorithm A, if there is a function f (n), when N=k, F (k) represents the time of the algorithm A in the case of input size k, it is said that f (N) is the time complexity of algorithm a.
Here we first need to explicitly enter the concept of scale. On the input scale, not very well defined, not strictly speaking, the input scale refers to the size of the natural independent body that the input is accepted by the algorithm A. For example, for the sorting algorithm, the input size is generally the number of elements to be sorted, and for the two of the same matrix of the product of the algorithm, the input scale can be regarded as a single square of the dimension. For the sake of simplicity, in the following discussion, we always assume that the input size of the algorithm is represented by an integer greater than 0, i.e. n=1,2,3,......, k,......
We also know that for the same algorithm, the time of each execution depends not only on the input size, but also on the characteristics of the input and the state of the specific hardware environment at the time of execution. So it is impossible to get a uniform and precise f (n). To solve this problem, let's do two instructions:
1. Ignoring hardware and environmental considerations, assume that the hardware and environmental conditions are fully consistent at each execution.
2. For the differences in the input characteristics, we will mathematically analyze it and bring it into the analytic form of the function.

Algorithm time Complexity Analysis example
To make it easier for my friends to understand, I will not use classic examples such as quick sorting, merge sorting, and so on in textbooks, but rather a very simple algorithm as an example. Let's start by defining the problem.
Problem definition:
Input--This problem is entered as an ordered sequence with a number of elements of n,n to an integer greater than 0. The elements in the sequence are n integers from 1 to n, but their order is completely random.
Output-the position where the element n is located. (first element position is 1)

The problem is very simple, and the following is a straightforward solution to one of its algorithms (pseudo code):

Locationn (A)
{
for (int i=1;i<=n;i++)-----------------------T1
{
if (a[i] = = N)----------------------------T2
{return i;} ------------------------T3
}
}

Let's take a look at this algorithm. where T1, T2, and T3 each represent the time it takes for this line of code to execute.
First, the input scale n is one of the factors that affect the execution time of the algorithm. In cases where n is fixed, different input sequences can also affect their execution time. In the best case, N is ranked in the first position of the sequence, then the run time is "T1+T2+T3". In the worst case, N is ranked last in the sequence, and the run time is "n*t1+n*t2+t3= (t1+t2) *n+t3". As you can see, the best time to run is a constant, and the worst-case run time is a linear function of the input scale. So what is the average situation?
The problem definition says that the input sequence is completely random, that is, n appears in the n position of 1...N, it is possible, that is, the probability is 1/n. The average number of executions is the mathematical expectation of the number of executions, and the solution is:

E
= P (n=1) *1+p (n=2) *2+...+p (n=n) *n
= (1/n) * (1+2+...+n)
= (1/n) * ((N/2) * (1+n))
= (n+1)/2

That is, on average the for loop is going to execute (n+1)/2 times, the average run time is "(t1+t2) * (n+1)/2+t3".
Thus we conclude that:
T1+t2+t3 <= f (n) <= (T1+T2) *n+t3, on average f (n) = (t1+t2) * (n+1)/2+t3

Asymptotic time complexity of the algorithm
In the above analysis, we analyze the time complexity f (n) of the algorithm accurately. However, many times, we do not need to perform such an accurate analysis for the following reasons:
1. In more complex algorithms, accurate analysis is very complex.
2. In fact, most of the time we don't care about the accuracy of f (n), but only about its magnitude.
Based on this, the concept of asymptotic time complexity is proposed. Before formally giving asymptotic time complexity, a few mathematical definitions are given:

definition One: Θ (g (n)) ={f (n) | If there is a normal number C1, c2, and positive integer n0, when N>=n0, 0<C1G (n) <=f (n) <=c2g (n) is established}
Definition 2:0 (g (n)) ={f (n) | If there is a normal number C and a positive integer n0, so that when N>=n0, 0<=f (n) <=CG (n) is established}
Definition Three: Ω (g (n)) ={f (n) | If there is a normal number C and a positive integer n0, so that when N>=n0, 0<=CG (n) <=f (n) is established}

As you can see, the three definitions actually define a set of functions, except that the functions in the collection need to meet different conditions. With the above definition, you can define the asymptotic complexity of the time.
But here's another question: F (n) is not deterministic, he is in a range of changes, then we care about which F (n)? In general, we use the worst-case f (n) to evaluate the efficiency of the algorithm when analyzing the algorithm, for the following two points:
1. If you know the worst case scenario, we can guarantee that the algorithm will not be any worse than the situation at any time.
2. In many cases, the probability of the worst-case scenario of an algorithm's operation is still large, such as finding the unknown origin element does not exist in the problem. And in many cases, the asymptotic time complexity of the average condition and the asymptotic time complexity of the worst case is a magnitude.

The following definition is given: set F (n) to algorithm A in the worst case f (n), if f (n) is θ (g (n)), then the asymptotic time complexity of algorithm A is g (n), and g (n) is the asymptotic certainty bounds of f (n)

As an example of the above example, the above definition is f (n) = (t1+t2) *n+t3. The asymptotic certainty of f (n) is n, which proves as follows:

Prove:
Set c1=t1+t2,c2=t1+t2+t3,n0=2
And because t1,t2,t3 are more than 0
Then, when the N>n0, 0<c1n<=f (n) <=c2n namely 0< (T1+T2) *n<= (t1+t2) *n+t3<= (T1+T2+T3) *n permanent establishment.
So F (n) belongs to θ (n)
So n is the asymptotic certainty boundary of F (N)
Certificate of Completion

In practical application, we usually use the asymptotic time complexity instead of the actual time complexity to analyze the efficiency of the algorithm. It is generally believed that an asymptotic complexity of n algorithm is better than the asymptotic complexity of n^2 algorithm. Note that this is not to say that the asymptotic complexity n algorithm is necessarily more efficient in any case, but that the worst case of the previous algorithm is always better than that of the latter when the input size is large enough (greater than the critical condition N0). It turns out that this analysis is reasonable and effective in practice.
Similarly, the upper and lower bounds of the algorithm's time complexity can be given:
Set f (n) to algorithm A in the worst case f (n), if f (n) is 0 (g (n)), then the asymptotic time complexity of algorithm A is limited to G (n), and g (n) is the asymptotic upper bound of f (n).
Set f (n) to algorithm A in the worst case f (n), if f (n) belongs to Ω (g (n)), then the asymptotic time complexity of algorithm A is lower than g (n), and g (n) is f (n) the asymptotic lower bound.

It is important to note that since we are analyzing the worst case of f (n), we can 100% guarantee that when the input scale exceeds the critical condition, the running time of the algorithm will not be higher than the asymptotic n0, but it is not 100% guaranteed that the algorithm will not run less than the asymptotic boundary, but only 100% Ensure that the worst running time of the algorithm is not lower than that of the asymptotic lower bounds.

Summarize
Algorithm time complexity analysis is a very important problem, any programmer should master its concept and basic method, and be good at the mathematical level to explore its essence, can accurately understand its connotation. In the above analysis, we have only discussed the "tight boundary", in fact, in reality, the boundary is also divided into "tight" and "non-compact", interested friends can access the relevant information.

Well, this article is here, I hope the content of this article can help you.

From:http://www.cnblogs.com/leoo2sk/archive/2008/11/14/1332381.html

Top
0
Step

The basis of algorithm time complexity analysis

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.