Computational method of time complexity of algorithm

Source: Internet
Author: User

The time complexity of the algorithm in the big liar data structure is defined as follows:

"When the algorithm is analyzed, the total number of executions of the statement t[n] is a function of the problem size n, which then analyzes the t[n] with the change of N and determines the order of t[n]. The time complexity of the algorithm, that is, the time measurement of the algorithm, remember to do T[n]=o (F[n]), indicating that with the increase of the size of the problem, the rate of execution time of the algorithm and F[n] growth rate is the same, called the progressive time complexity of the algorithm, referred to as time complexity, f[n] is a function of the problem scale n ”

By definition, for example, calculate 1+2+3+4+ +100 =?

int sum = 0, n=100;

for (int i=1;i<=n;i++) {

Sum+=i;

}

When the algorithm executes, the total number of executions of the statement t[n] = 1+n+1+n = 2n+2, with the increase of the problem size n, the rate of execution time of the algorithm and F[n] growth rate is the same, where f[n] = 2n, because with the increase of n, the constant 2 does not increase, in order to simplify the calculation, Take the main number of executions 2n, so t[n] = O (2n). That is, O (n) "below explains why 2 is removed"

The same problem, we use Gauss algorithm to solve.

int sum = 0,n = 100;

sum = (n (n+1))/2;

When the algorithm finishes executing, the total number of executions of the statement is 2,t[n]=o (2). That is, O (1) "explained below"

Through the above example, give the "Big O-order" of the demolition (? *?ω?)? Steps:

1. Calculation of total execution count of statements F[n]

2. Replace all the addition constants in this book with constant 1

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

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

Let me give you an example.

int n=100;

for (int i=0; i<n; i++) {

for (int j=0; j<n; J + +) {

n--;

}

}

printf ("n=%d", N);

The first sentence is clearly executed 1 times, the number of execution of the nested loop does not come out, which needs to be analyzed, first look at the inner loop, J self-increment once, n to subtract one time, it can be inferred that when the N self-reduction to N/2, the first cycle of the end, the second sentence executed once. Next look at the second inner loop, when n is reduced to N/4, the second loop ends and the second sentence executes a second time. Thus, when the i>=n/(2i), the outer loop is the end of the second sentence, that is, the outer loop runs at most n/(2i) times, when n equals 1 million, I equals 16, so the outer loop can be neglected, the inner loop n--; n-n/(2i) was executed, approximately equal to n times. So the algorithm time complexity of this example is O (n).

I made up the example above. At the beginning of the calculation of the outer loop is also confused, in the group and we discussed the only understand, this also tells us that when computing complexity, we must have a holistic view, do not die every sentence execution times. This morning when reading "Programming Zhu Ji Nanxiong", I learned about approximate estimates and found that they could be used in this place.

Finally, a variety of complexity of the image, drawing tools using Mathematica, the feeling is better than matlab mathematical tools.

  

Remove a few growth rates fast

  

Computational method of time complexity of algorithm

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.