Professional Documents
Culture Documents
• Asymptotic analysis
• Big O notation
• Example
• Simplified asymptotic analysis
• A comparison of some common functions
You have seen that the time complexity of an algorithm can be expressed
as polynomial. To compare two algorithms, you can compare the
respective polynomials. However, the analysis performed in the previous
lessons is cumbersome and would become intractable for bigger
algorithms that are normally encountered in practice.
Asymptotic analysis#
One observation that may help is that you want to worry about large input
sizes only. If the input size is really small, how bad can a poorly designed
algorithm get, right? Mathematicians have a tool for this sort of analysis
called asymptotic notation. The asymptotic notation compares two
functions; let’s say, f (n) and g(n) for large values of n. This fits in nicely
with your need for comparing algorithms for very large input sizes.
Big O notation#
https://www.educative.io/courses/data-structures-interviews-cs/qVQq0WLjO3p 1/7
06/11/2021, 10:53 Introduction to Asymptotic Analysis and Big O - Data Structures for Coding Interviews in C#
f (n) ≤ cg(n)
The following graph shows a plot of a function f (n) and cg(n) that
demonstrates this inequality.
c*
(g(n))
f(n)
growth
no
input size
Note that the above inequality does not have to hold for all n. For n < n0 ,
f (n) is allowed to exceed cg(n) but for all values of n beyond some value
n0 , f (n) is not allowed to exceed cg(n).
What good is this? It tells you that for large values of n, f (n) will be
within a constant factor of g(n) at most. In other words, f (n) will not
grow faster than a constant multiple of g(n). Put yet another way, the rate
of the growth of f (n) is within constant factors of that of g(n).
https://www.educative.io/courses/data-structures-interviews-cs/qVQq0WLjO3p 2/7
06/11/2021, 10:53 Introduction to Asymptotic Analysis and Big O - Data Structures for Coding Interviews in C#
Example#
Consider an algorithm whose running time is given by
f (n) = 3n3 + 4n + 2. Try to verify that this algorithm’s time complexity
is in O(n3 ). To do this, you need to find a positive constant c and an
integer n0 > 0 for all n ≥ n0 :
3n3 + 4n + 2 ≤ cn3
The above inequality would still be true if you rewrote it while replacing
cn3 with 3n3 + 4n3 + 2n3 . What you have just done is the replacement of
the variable part in all terms with n3 , which is the variable-part of the
highest order term. This gives us:
This does not violate the inequality because each term on the right-hand
side is greater than the corresponding term on the left-hand side. Now,
comparing it with the definition of Big-O, you can pick c = 9.
That leaves n0 . For what values of n is the inequality 9n3 ≤ cn3 satisfied?
All of them actually! You can pick n0 = 1.
The above solution (c = 9, n0 = 1) is not unique. You could have picked
any value for c that exceeds the coefficient of the highest power of n in
f (n). Suppose you decided to pick c = 4. The reader can verify that the
inequality 3n3 + 4n + 2 ≤ cn3 still holds for n0 = 3 or higher.
Note that it is not possible to find a constant c and n0 to show that
f (n) = 3n3 + 4n + 2 is O(n2 ) or O(n). It is possible to show that f (n) is
https://www.educative.io/courses/data-structures-interviews-cs/qVQq0WLjO3p 3/7
f( )
06/11/2021, 10:53 Introduction ( ) Analysis
to Asymptotic ( and
) Big O - Data
p Structures for Coding Interviews in C#f ( )
O(n4 ) or O(n5 ) or any higher power of n. Mathematically, it is correct to
Notice how the constant coefficients have become insignificant in the Big
O notation. Recall that these constants represent the number of primitive
https://www.educative.io/courses/data-structures-interviews-cs/qVQq0WLjO3p 4/7
06/11/2021, 10:53 Introduction to Asymptotic Analysis and Big O - Data Structures for Coding Interviews in C#
operations on a given line of code. This means that while analyzing code,
Moving forward, you will simplify the analysis by counting the number of
executions of each line of code instead of counting the number of
operations.
√n Root-n 2n Exponential
n Linear en Exponential
The following graph shows some of the functions from the above table.
O(n!)
O(2^n)
O(n^2)
O(nlogn)
O(n)
O(logn)
1 e3n is in O(en ).
A) True
https://www.educative.io/courses/data-structures-interviews-cs/qVQq0WLjO3p 6/7
06/11/2021, 10:53 Introduction to Asymptotic Analysis and Big O - Data Structures for Coding Interviews in C#
B) False
Submit Answer
Question 1 of 2
0 attempted
Reset Quiz
Back Next
Mark as Completed
Report an Issue
https://www.educative.io/courses/data-structures-interviews-cs/qVQq0WLjO3p 7/7