Algorithm complexity analysis method and its application

Source: Internet
Author: User

AlgorithmComplexity occurs in the first chapter of the course "Data Structure", because it involves a little mathematical problems, so many people feel very difficult. In addition, this concept is not so specific, this makes it difficult for many students to review the questions. Next we will analyze the questions for you.

First, let's take a look at several concepts. One is time complexity, and the other is time complexity. The former is the time consumption of an algorithm, and it is the function of solving the problem scale N. The latter is the order of magnitude of the time complexity of the algorithm when the problem scale tends to be infinite.

When evaluating the time performance of an algorithm, the main criterion is the approximate time complexity of the algorithm. Therefore, during algorithm analysis, the two are usually not distinguished, it is often referred to as the time complexity T (n) = O (f (n), where F (n) is generally the statement frequency with the largest intermediate frequency of the algorithm.

In addition, the frequency of statements in the algorithm is not only related to the problem scale, but also to the values of each element in the input instance. However, we always consider the time complexity in the worst case. To ensure that the algorithm does not run longer than it.

Common time complexity: constant order O (1), logarithm order o (log2n), linear order O (N), linear logarithm order o (nlog2n), square order O (N ^ 2 ),CubeLevel O (N ^ 3), K to the power of O (N ^ K), exponential order o (2 ^ N ).

The following example shows how to solve the problem.

1. Set the F, G, and H functions to F (n) = 100n ^ 3 + N ^ 2 + 1000, g (n) = 25n ^ 3 + 5000n ^ 2, H (n) = n ^ 1.5 + 5000 nlgn

Determine whether the following link is true:

(1) f (n) = O (G (n ))

(2) g (n) = O (f (n ))

(3) H (n) = O (N ^ 1.5)

(4) H (n) = O (nlgn)

Here we will review the representation of the approximate time complexity T (n) = O (f (N). Here "O" is a mathematical symbol, it is strictly defined as "If T (N) and F (n) are two functions defined on a positive integer set, T (n) = O (f (n )) represents the existence of positive constants c and N0, so that when n is greater than or equal to N0, all meet 0 ≤ T (n) ≤ C? F (n ). "It is easy to understand that when the integer independent variable n tends to be infinite, the ratio of the two functions is a constant not equal to 0. In this case, it's easy to calculate it.

◆ (1) was established. In the question, because the maximum times of the two functions are all N ^ 3, when n → ∞, the ratio of the two functions is a constant, so this relationship is true.

◆ (2) was established. Same as above.

◆ (3) was established. Same as above.

◆ (4) not true. When n → ∞, N ^ 1.5 is faster than nlgn, the ratio of H (n) To nlgn is not a constant, so it is not true.
2. Set N as a positive integer and use the large "O" markProgramThe execution time of the segment is represented as N.

(1) I = 1; k = 0

While (I <n)

{K = K + 10 * I; I ++;

}

Answer: T (n) = n-1, T (n) = O (N). This function increases linearly.

(2) x = N; // n> 1

While (x> = (Y + 1) * (Y + 1 ))

Y ++;

Answer: T (n) = N1/2, T (n) = O (N1/2). The worst case is Y = 0, the number of cycles is N1/2. This is a function that increments by the square root order.

(3) x = 91; y = 100;

While (Y> 0)

If (x> 100)

{X = X-10; y --;}

Else x ++;

Q: T (n) = O (1). This program looks a little scary. It runs 1000 times in a loop, but we see N does not? No. The running of this program has nothing to do with N. Even if it has been recycled for 10 thousand years, we don't care about it. It's just a constant order function.

 

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.