Skip to content
Related Articles

Related Articles

Improve Article

Guidelines for asymptotic analysis

  • Difficulty Level : Basic
  • Last Updated : 09 Jul, 2021

In this article, the focus is on learning some rules that can help to determine the running time of an algorithm.

Asymptotic analysis refers to computing the running time of any operation in mathematical units of computation. In Asymptotic Analysis, the performance of an algorithm in terms of input size (we don’t measure the actual running time) is evaluated. How the time (or space) taken by an algorithm increases with the input size is also calculated.

(g(n)) = {f(n)   such that g(n) is a curve which approximates f(n) at higher values of input size, n}

This curve is called asymptotic curve and the algorithm analysis for such a curve is called Asymptotic analysis.

Loops: The running time of a loop is, at most, the running time of the statements inside the loop, including tests) multiplied number of iterations.



Below is the python program that demonstrates the above concept:

Python3




# Python program to implement
# the above concept
# execute n times for in
# range(0.00);
 
for i in range(0, n):
print ('Current Number:', i, sep = "")
#constant time 
Total time a constant cx n = cn = O(n).

Nested loops: Analyze from the inside out. The total running time is the product of the sizes of all the loops.

Below is a python program that demonstrates the above concept:

Python3




# Python program to implement
# the above concept
# outer loop executed n times
for i in range(0, n):
 
    # inner loop executes n times
    for j in range(0, n):
      print("i value % d  and j value % d" % (i, j))
# constant time 
Total time = C x n x n = cn^2 =0(n²).

Consecutive statements: Add the time complexity of each statement.

Below is a python program that demonstrates the above concept:

Python3






# Python program that implements
# the above concept
n = 100
 
# executes n times
for i in range (0, n):
    print (Current Number: i, sep = "")
     
    # outer loop executed n times
    for i in range (0, n):
       
      # inner loop executes n times
      for j in range(0, n):
        print(" i value % d and j value % d"%(i, j))
 
X

 
 

Total time = co + c1n + c2n^2 = 0(n^2).

 

If-then-else statements: Worst-case running time: the test, plus either the then part of the else part whichever is the largest. 

 

Below is a python program that demonstrates the above concept:

 

Python3




# Python program that implements
# the above concept
if n == I:
  print ("Incorrect Value")
  print (n)
 
else:
  for i in range(0, n):
     
    # constant time
    print (CurrNumber:, i, sep = "")
Total time = co + c1*n = 0(n).

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :