Introduction to Algorithms: Time Complexity + Asymptotic Notation

2.1 Computational Complexity

 

 

  

2.2 Asymptotic analysis

Attributes:

The order of common functions is arranged from low to high:

insert image description here


my notes: 

 

other people's notes

1) O (Big-Oh, big O notation) - represents the upper limit, asymptotic upper bound

Construct an inequalityf(n) = c * g(n) of the form such that, under the condition of n ≥ n0, satisfies . (n0 is a number taken by oneself, requiring n0 ≥ 0)0≤f(n)≤c*g(n)

insert image description hereAs shown in the figure, when the formula to be analyzed is f(n)=2n+3
, the formula 2n+3n can be constructed, which can ensure that when n≥1, f(n)= 2n+3 ≤ 2n+3n = 5n is established at
this time , c*g(n)=5n (c is a coefficient of 5, g(n)=n)
because g(n)=n, so f(n)=O(n)

ps: Big O notation can be represented by a function with a higher order than the closest function.

For the above case of f(n) = 2n+3 ≤ 2n+3n = 5n: using O(n) is the most appropriate case, but you can also use O(n^2), O(nlogn) and other orders It can be expressed in a higher form, but it is meaningless when the order is too large.

2) Omega (Ω) - represents the lower limit, asymptotically lower bound

Construct f(n) = c * g(n)an inequality of the form such that, under the condition of n ≥ n0, satisfiesf(n)≥c*g(n)≥0 . (n0 is a number taken by myself)

insert image description here

As shown in the figure, when the formula to be analyzed is f(n)=2n+3
, the formula 1 * logn can be constructed, which can ensure that when n≥1, f(n)= 2n+3 ≥ 1*logn is established.
At this time, c *g(n)=1*logn (c is coefficient 1, g(n)=logn)
because g(n)=logn, so f(n)=O(logn)

ps: Omega (Ω) notation can be represented by a function with a higher order than the closest function.

In the same way, for the above f(n) = 2n+3 ≥ 1 * logn situation: O(logn) is the most appropriate situation, but it can also be expressed in a lower order form such as O(1) , but when the order is too low, it is meaningless.

3) Theta (Θ) - average limit, asymptotically tight constraint tight bound

insert image description here

Compared with big O and Ω, the selection of the average limit Θ is often stricter, and there will only be one. 

insert image description here

As shown in the figure, the formula to be analyzed is f(n)=2n+3
. At this time, the formula c1 * g(n) = 1 * n and the formula c2 * g(n) = 5 * n can be constructed, which can guarantee that when n≥ When 1, n ≤ f(n) = 2n+3 ≤ 5n holds.

At this time, g(n) on both sides of the inequality is n, and g(n) = n is the mean limit θ.

ps:
The average limit requires that g(n) on both sides of the inequality must be the same, as in the above example, the g(n) on the left cannot be n, and the g(n) on the right cannot be n2.

ps:
Big O (less than or equal to) does not only represent the worst case, big Ω (greater than or equal to) does not only represent the best case, in fact, any symbol can represent the best/worst case.
Please don't get confused.


1. Linear time O(n)

Linear time: The running time is at most a constant times the size of the input.

●Calculate the maximum value of n numbers a1, a2,...an

 ● Merge. Merge two sorted lists A = a1,a2,..., an with B = b1,b2,...,bn into one sorted list.

Merging two lists of size n takes O(n) time.

Pf. After each comparison, the length of the output list is increased by 1

2、O(n logn)

O(n logn) time, occurs in divide and conquer algorithms. Also known as linear time.

● Sort. Merge sort and heap sort are sorting algorithms that perform O(n logn) comparisons.

● Find the maximum time interval. Given n timestamps x1,...,xn when a copy of a file arrives at the server, what is the largest time interval when no copy of the file arrives at the server?

The solution is O(n log n). Sort n timestamps, scanning the sorted list in order, determining the maximum interval between consecutive timestamps.

3、O(n^{2})

secondary time. Enumerates all pairs of elements.

● Find the nearest pair of points. Given a list of n points on a plane (x1, y1), ..., (xn, yn), find the closest pair.

O( n^{2}) solution. Try all pairs.

O(n^{2}) seems inevitable, but this is just an illusion.  →see chapter 5

4、O(n^{3})

cube. A triplet that enumerates all elements.

● Set stripping. Given n sets S1,. , each set of Sn is 1,2,. , a subset of n, is there a disjoint pair of sets?

Solution O( n^{3}). Determine for each pair of sets in turn whether they are disjoint.

Guess you like

Origin blog.csdn.net/m0_65207522/article/details/127321018