Time and space complexity of the code

How to assess the complexity of the code

The code has two directions complexity measure, a time complexity, the spatial complexity is a

First, the time complexity of
definition: if n is the size of a problem, an algorithm to solve the problem of the time required for the T (n), which is a function of n T (n) algorithm called the "time complexity."

Properties:
1, the asymptotic complexity of time: When the input n gradually increased, the limit where the time complexity.

T(n)=O(f(n))

T (0) is expressed as the time complexity

Big O notation indicates that the function has an upper limit

f (n) represents a function of time spent on the problem itself is caused by the size of n

2, a large O-order derivation method

All run-time constants substituted in addition by a constant 1.

In function of the number of runs after the modification, retaining only the highest order term.

If the highest order term exists and is not 1, then remove multiplied by a constant to this project. The result is the big O-order.

3, the specific circumstances:

Constant order: no sequential execution cycle

Linear order: a cycle, and cycle times of time complexity related
int i;

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

/ * Time complexity is O (1) the sequence of program steps * /

}
// loop n times, the overall time complexity is O (n)

Of the order: Because after each count is multiplied by 2, on a closer distance n points. In other words, the number multiplied greater than 2 n, it will exit the loop. The resulting 2 ^ x = n x = logn. Therefore, the cycle time complexity is O (logn).

int count = 1;

while (count < n){

count = count * 2;

/ * Time complexity is O (1) the sequence of program steps * /

}

Order of the square:
For the outer loop, but this time the internal complexity of O (n) statement, n times recycle. Therefore, the time complexity of this code is O (n ^ 2). If the number of cycles to the outer loop m, the time complexity becomes O (mXn) it was concluded that we, the cycle time is equal to the complexity of the complexity of the loop multiplied by the number of times of the cycle operation.

例1:
you i, j;

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

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

/ * Time complexity is O (1) the sequence of program steps * /

  }

} // independent external circulation, inside and outside the time complexity is multiplied


Example 2:
int I, J;

for (I = 0; I <n-; I ++) {

  for (j = i; J <n-; J ++) {/ * Note that j = i not 0 * /

  / * time complexity sequence of steps for the program O (1) is * /

  }

} // inner and outer circular relationship requires adding one by one, derived relationship, and large O derivation, the complexity of the final time:

Since when i = 0, the loop is executed n times, when i = 1, n-1 times is performed, ...... when i = n-1 when executed once. So the total number of executions is:

O by the method we derive a large order, first, no additive constant disregarded; second, retaining only the highest order term, thus retention (n ^ 2) / 2; third, removing the item by multiplying constant, i.e. removal of 1/2, the final code of the time complexity is O (n2).

Cubic order:

int I, J;

for (I =. 1; I <n-; I ++)

for (J =. 1; J <n-; J ++)

for (J =. 1; J <n-; J ++) {

/ * time complexity of O ( 1) the sequence of program steps * /

} // time complexity is O (n ^ 3)

4, common time complexity

When asked complexity of Common Table:

Common time complexity time taken from small to large are:

5, worst-case and average case

Worst-case running time is a guarantee that the running time will not be broken. In the application, which is one of the most important needs, typically, unless otherwise specified, the running time we mentioned are the worst-case running time.

The average running time is the most significant in all cases, because it is the desired operating time.

Note: In the general case there are no special instructions, refer to the worst time complexity.


Second, the space complexity

Space complexity of the calculations required by the implemented algorithm storage space, calculated spatial complexity algorithm denoted: S (n) = O (f (n)), where, n-scale of the problem is, f (n) as a function of n statement occupied storage space.

In general, when a program is executed on the machine, in addition to instructions stored in the program itself, constants, variables and input data, but also need the data stored in the storage unit of the operation, if the input data is only dependent on the space occupied by the problem itself, and algorithm independent, so that only auxiliary units to the analysis of the algorithm is required when implemented. If desired algorithm executed with respect to the ancillary space in terms of amount of input data is constant, this algorithm called the work place, the space complexity is O (1). // i.e. auxiliary space algorithm has been determined that the allocated space is fixed.

Third, the time and space complexity of calculation rules

1, the addition rule

T (n, m) = T1 (n) + T2 (m) = O (max {f (n), g (m)}) // two time adding complexity, and whichever is the greatest of

2, multiplication rule

T(n,m) = T1(n) * T2(m) = O(max{f(n)*g(m)})//

3, an experience

Relationship between complexity and time efficient:
! C (constant) <logN <n-<n-logN * <^ n-2 <n-^. 3 <n-2 ^ <^. 3 n-<n-
L ---------- -------------------- l -------------------------- l-- ------------ l
preferably generally poor


Fourth, the complexity of time and space commonly used algorithm complexity

 

 

参考文章:https://www.cnblogs.com/silence-x/p/10544072.html

Guess you like

Origin www.cnblogs.com/yu-tang/p/12084618.html