Measurement of Complexity

Source: Internet
Author: User
Measurement of Complexity

AlgorithmComplexity is the amount of computer resources required for algorithm operation. The amount of time resources required is called time complexity, and the amount of space (I .e. memory) resources required is called space complexity. This quantity should reflect the efficiency of the methods used in the algorithm, and abstract them from the actual computer that runs the algorithm. In other words, this amount depends only on the size of the problem to be solved by the algorithm, the input of the algorithm, and the function of the algorithm itself. If you useN, IAndAThe scale of the algorithm to solve the problem, the input of the algorithm, and the algorithm itself.CIt indicates the complexity of the algorithm, so there should be:

C=F(N, I,)

WhereF (n, I,)YesN, I,Is a definite ternary function. If the time complexity and space complexity are separatedTAndSTo indicate, there should be:

T=T(N, I,) (2.1)

AndS=S(N, I,) (2.2)

Generally, we make a implicit in the name of a complex function, so we can abbreviated (2.1) and (2.2)

T=T(N, I)

AndS=S(N, I)

Because the concept of time complexity is the same as that of space complexity, the calculation method is similar, and the spatial complexity analysis is relatively simple, so the following describes the time complexity.

The following usesT(N, I.

AccordingT(N, I), It should be the time required for an algorithm to run on an abstract computer. Set the meta operations provided by this abstract computerKRespectively.O1, O2,..., OK; then set the time required for each execution of these meta operationsT1, T2,.., TK. For a given algorithmA, Used for Meta calculation.OThe number of I times isEI,I= 1, 2,..., K, Obviously, for each I, 1 <=I<= K, EI yesNAndIFunction, that isEI =EI (N, I). There are:

(2.3)

WhereTI, I = 1, 2,..., K, is consistentN, IIndependent constant.

Obviously, we cannotNEach valid inputIAll go statisticsEI (N, I), I = 1, 2 ,..., K. ThereforeT(N, I), Or we can onlyNOf some or some types of representative legal input to calculate the correspondingEI ,I= 1, 2 ,..., KTime complexity.

The following takes into account the complexity of three situations: the worst case, the best case, and the average case, and is recordedTMax(N), TMin (N) AndTAVG (N). In mathematics, there are:

(2.4)

(2.5)

(2.6)

Where,DNYes, the scale isNThe set of valid inputs;I* YesDNOne enableT(N, I*) ReachesTMax(N)The valid input isDNOne enableT (n,)TMin(N)AndP(IIs input in the application of the algorithm.I.

In the above three cases, the time complexity reflects the efficiency of algorithms from a certain perspective, each of which has its own usefulness and limitations. However, practice shows that the time complexity in the worst case is the best operability and the most practical value. Next we will focus on the analysis of time complexity.

In general, the time complexity of the best and worst cases is difficult to measure, because the number of times N has reachedTMax(N)The valid input is difficult to determine, and the probability of each input of scale N is difficult to predict or determine. We sometimes measure time complexity on average, but at that timeP (I)Some artificial assumptions (such as probability) were made. There is always a lack of justification for the assumptions. Therefore, in the best case and average case, the time complexity analysis is just stuck in theory.

Now, question 1 in the above chapter uses the search algorithm as an example to describe how to use (2.4)-(2.6)TMax, TMin andTAVG. The scale of the problem is calculated in m, and the meta operations reused by the algorithm include assignment, test, and addition. The time constants required for each execution areA, T, AndS. For this example, if C is in a, it is easy to see that the worst case input appears in C.=[M:

TMax (M) =A+ 2Mt+ (M-1)S+ (M-1)A+T+A= (M+ 1)A+ (2M+ 1)T+ (M-1)S(2.7)

The input in the best case is c = A [1. At this time:

(2.8)

AsTAVG (M), As described earlierDThe probability distribution on m can be measured only when assumptions are made. For simplicity, let's make the simplest assumption:DThe probability distribution on M is equal, that isP(A[I] = C) =1/m. NoteTI= T(M, II), whereIIA[I] = C valid input, then:

(2.9)

According to the derivation similar to (2.7), there are:

If it is substituted into (2.9), then:

This happens to be:

TAVG (M) = (TMax (M) +Tmin(M)/2

However, it must be noted that the above formula is not general.

Similarly, for the algorithm B _search, the corresponding values can be calculated by (2.4)-(2.6 ).TMax (M), TMin (M) AndTAVG (M). However, here we only calculateTMax (M). For comparison with search, it is still assumed that C is in a, that is, the worst case input is still in C = A [M. At this time, the loop body of the while loop is executedM+ 1:K+ 1 time. Because the data size during the first execution is m, the size of the second execution is m/2, and the size of the last execution is 1. In addition, in addition to the assignment, test, and addition operations, there are also two meta operations, subtraction and division. After the two meta operations are completed, the time required for each operation is B and D. You can perform the following operations:

(2.10)

Compared with (2.7) and (2.10), we can see that when m is sufficiently large, the time complexity of B _search is much less than that of search in the worst case.


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.