How to understand the progressive symbols in the algorithm?

Source: Internet
Author: User

When we analyze an algorithm, we often need to use mathematics to describe its performance. What's the most common? For example, in a program

for (I = 0,i < n; I + +)

for (j = 0; J < N; j + +);

for (cnt = 0; cnt < n; cnt + +);

We will say that the worst case is n^2+ N, when, in fact, we do not consider the machine factor, we take each instruction execution time as a unit to see, if we consider the machine factors, such as on a machine, each instruction execution speed is C1, then his worst case is (n^2 + N)/C1, And on machine B, the speed becomes C2, then the worst case becomes (n^2 + N)/c2, if the C1 is 10, and C2 is 20, then we say: The performance of the algorithm on machine A is worse than machine B? Obviously not, the machine is bad, but the algorithm itself is not bad, so say on different machines running the same algorithm, the worst case time is always not the same, we have to find ways to remove the impact of machine performance, but only to analyze the merits and demerits of the algorithm itself, In order to facilitate the specific quantification of the performance of the algorithm can also remove the impact of machine factors, because we talk about algorithmic analysis rather than computer system analysis. So someone invented the theta symbol. For this symbol, let's look at a couple of examples and see the definition

For example:

A * x ^2 + b * x + c = f (x); Then there is θ (x^2) = f (x).

A * x ^ 3 + b * x ^ 2 + c *x + D = f (x); Then there is θ (x ^ 3) = f (x).

In other words, it is equivalent to removing the highest item of f (x) and then removing its constant factor, and then putting it into θ (), and putting it in θ () what does it mean? To clarify the problem, let's look at its definition:

Note: the ":" Symbol in the collection representation means "satisfied with ..." Conditions

Θ (g (n)) = {f (n): There is a constant c1,c2, and n0, so that when n >= n0, there are 0 <= C1 * g (n) <=f (n) <= C2 * g (N)}

Starting from the latter half sentence, "presence n > N0,0<= c1 * g (n)" here guarantees that g (n) is an increment function. Next, there is C1 * g (n) <= f (n) <= C2 * g (n). In my head, I can imagine. function Image [C1 * g (n), C2 * g (n)] This interval is actually a swinging curve, if G (n) is considered as a positive proportional function of y = x, it is a A fan, huh? A curve swung, sweeping out a fan. and f (n) is one of the many curves that make up a fan! This means that, regardless of what g (n) is, there is always a constant factor that makes C1 * f (n) =g (n). Then return to the original topic to understand:

A * x ^2 + b * x + c = f (x); so there is θ (x^2) = f (x), here θ (x^2) means that, in the process of x tending to infinity, there is a constant C that makes C * n^2 = a * x ^2 + b * x +c = f (x), that is: the same! number ! (see high number); So, we use θ () to describe the performance of the first program n^2 + n into θ (n ^ 2), this shows that in the process of n tends to infinity, there is always a constant factor to make c * n^2 = n ^2 + N, this constant factor C in the above, is determined by the machine and other non-algorithmic factors That is, when we use Θ (n ^ 2) to denote the performance of an algorithm, we completely ignore the machine, only the algorithm itself, it seems to say: no matter what machine you run this algorithm, as long as the input size is large enough, your performance is n ^ 2 integer times, as to how many times, the machine decided, On the same machine anyway, this multiplier is equal.

You see, Theta (x^2) is not the machine-independent, pure algorithm performance? The original problem was solved. However, the θ (x^2) symbol also allows us to focus only on the highest power, not on the lower term, because when the n-> infinity, the lower term is meaningless, because as long as n changes a little bit, it is enough to offset the low-order changes (in fact, the same number). So we know that for a time complexity of θ (x^2) algorithm, its time performance is better than θ (x^3) algorithm, here we do not consider the constant factor, even if θ (x^3) The constant factor is 10000000000000 and θ (x^2) The constant factor is 1 does not matter, Because anyway, there is always a value n0 so that n over this point the θ (x^2) algorithm always defeats the θ (x^3) algorithm, that is, the use of the theta notation (progressive sign) to compare algorithm time performance, is machine-independent, even if you run the theta (x^3) algorithm on the supercomputer, As long as the input scale and time is enough, the x^2 algorithm of the old 386 machine always beats the supercomputer. Similar to the θ symbol we draw the O sign (read as Big O).

O (g (n)) = {f (n): There is a constant C, and n0, so that when n >= n0, there is 0 <= f (n) <= c *g (N)}

Here we are only concerned with the worst case, we always like to use the O symbol when we talk about the worst-case time complexity of an algorithm, and I illustrate the function of the O symbol by the previous example.

for (I = 0,i < n; I + +)

for (j = 0; J < N; j + +);

for (cnt = 0; cnt < n; cnt + +);

At this time we regard the speed of the machine as Unit 1, and then execute the N^2 + N, we will say, the above algorithm worst case time complexity is O (n^2), or the same purpose, we do not consider the impact of the machine itself, because the speed of the machine execution if it is C, then the time becomes (n^ 2 + N)/C, it's just a matter of constant factor. When the input size is large enough, we use O (n^ 2) to ignore the impact of the machine, only to consider the worst case of the algorithm itself, to tell others: Hey! No matter what machine you run on, your worst case can be worse than x times the value of N ^ 2. As for x times is how many times, anyway we just research algorithm, only do machine people know X is how many times, and we compare algorithm of time, not in different machine to test the same algorithm then compare, often on the same machine go to examine an algorithm, when constant factor x is equal, care it is also superfluous, This is like when we college entrance exam each person's score is multiplied by 10 points and then to apply, the result is not the same? Because the university is based on the rankings to accept, so for the algorithm is the same, we only focus: "That algorithm is the best?" Which algorithm should we choose? "This kind of comparative problem.

———————————————————————— Adults Kong Yiji 2014, 11, 23

How to understand the progressive symbols in 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.