Time complexity and spatial complexity of the algorithm

Source: Internet
Author: User

The time complexity and spatial complexity of the < algorithm are called the complexity of the algorithm >

Time complexity for---> Algorithms

(1) Time frequency an algorithm implementation of the time spent, from the theoretical can not be calculated, must be on the machine to run the test to know. But we can not and do not need to test each algorithm, just know which algorithm spends more time, which algorithm spends less time on it. And the time that an algorithm spends is proportional to the number of executions of the statement in the algorithm, which algorithm takes more time than the number of statements executed. The number of times a statement is executed in an algorithm is called a statement frequency or time frequency. Note as T (N). (2) Time complexity in the time frequency mentioned earlier, n is called the scale of the problem, and when N is constantly changing, the time frequency t (n) will change constantly. But sometimes we want to know what the pattern is when it changes. To do this, we introduce the concept of time complexity. Under normal circumstances, the number of iterations of the basic operation of the algorithm is a function of the problem size n, denoted by T (n), if there is an auxiliary function f (n), so that when n approaches infinity, the limit value of T (n)/f (n) is not equal to zero constant, then f (n) is the same order of magnitude function of t     As T (n) =o (f (n)), called O (f (n)) is the progressive time complexity of the algorithm, which is referred to as the complexity of time. The time frequency is different, but the time complexity may be the same. such as: T (n) =n2+3n+4 and T (n) =4n2+2n+1 their frequency is different, but the time complexity is the same, all O (N2). In order of magnitude increment, the common time complexity is: Constant order O (1), Logarithmic order O (log2n), linear order O (n), linear logarithmic order O (nlog2n), square order O (n2), Cubic O (n3),..., K-order O (NK), exponent-order (2n). With the increasing of the problem scale N, the complexity of the time is increasing and the efficiency of the algorithm is less. (3) The worst time complexity and the average time complexity are worst-case time complexity called the worst time complexity. In general, it is not particularly stated that the time complexity of the discussion is the worst-case time complexity.     The reason for this is that the worst-case time complexity is the upper bound of the algorithm's run time on any input instance, which guarantees that the algorithm will not run longer than any other. In the worst case, the time complexity is T (n) =0 (n), which indicates that the algorithm will not run longer than 0 (n) for any input instance.    The average time complexity is the expected run time of the algorithm when all possible input instances are present with equal probabilities. Exponential order 0 (2n), obviously, the time complexity of the exponential order 0 (2n) algorithm is very inefficient, when the value of n is slightly larger can not be applied. (4) Time complexity "1" If the execution time of the algorithm does not grow with the increase of the problem size n, even if there are thousands of statements in the algorithm, the execution time is only a large constant. The time complexity of such an algorithm is O (1). x=91; y=100;
while (y>0) if (x>100) {x=x-10;y--;} else x + +;
Answer: T (n) =o (1),
This app looks a little scary and runs 1000 times in total, but did we see n?
Didn't. The operation of this program is independent of N,
Even if it circulates for another 10,000 years, we don't care about him, just a constant order function "2" when there are several loop statements, the time complexity of the algorithm is determined by the frequency f (n) of the most inner statement in the loop statement with the highest number of nested layers. X=1;   for (i=1;i<=n;i++) for (j=1;j<=i;j++) for (k=1;k<=j;k++) x + +; The most frequent statement in the program segment is (5), the number of executions in the inner loop is not directly related to the size of the problem, but it is associated with the variable value of the outer loop, and the number of outermost loops is directly related to n, so the number of executions of the statement (5) can be analyzed from the inner loop to the outer layer: ,..., M-1, so here the most inner loop is 0+1+...+m-1= (m-1) m/2 times So, I take from 0 to N, then the loop is carried out: 0+ (1-1) *1/2+...+ (n-1) n/2=n (n+1) (n-1)/6 times, Therefore, the time complexity of the program segment is t (n) =o (n3/6+ Low) =o (N3).

Time complexity and spatial complexity of the 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.