The Wayback Machine - https://web.archive.org/web/20240626150442/https://www.geeksforgeeks.org/guidelines-for-asymptotic-analysis/
Open In App

Guidelines for asymptotic analysis

Last Updated : 15 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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:

C++




// C++ program to implement
// the above concept
// outer loop executed n times
#include <iostream>
using namespace std;
 
int main()
{
    for(int i = 0; i < n; i++)
    {
 
    // inner loop executes n times
    for(int j = 0; j < n; j++)
        cout<<"i value " << i << " and j value " << j;
    }
}
 
// This code is contributed by Utkarsh


Java




// Java program to implement
// the above concept
// outer loop executed n times
import java.io.*;
 
class GFG {
  public static void main (String[] args)
  {
    for(int i = 0; i < n; i++)
    {
 
      // inner loop executes n times
      for(int j = 0; j < n; j++)
        System.out.print("i value " + i + "  and j value " + j);
    }
  }
}
 
// This code is contributed by Aman Kumar


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))


C#




// C# program to implement
// the above concept
// outer loop executed n times
using System;
 
class GFG {
  public static void Main ()
  {
    for(int i = 0; i < n; i++)
    {
 
      // inner loop executes n times
      for(int j = 0; j < n; j++)
        Console.Write("i value " + i + " and j value " + j);
    }
  }
}
 
// This code is contributed by Pushpesh Raj


Javascript




// JavaScript program to implement
// the above concept
// outer loop executed n times
let n = 10;
for(let i = 0; i < n; i++)
{
    // inner loop executes n times
    for(let j = 0; j < n; j++)
        console.log("i value " + i + " and j value " + 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).


Next Article

Similar Reads

Asymptotic Notation and Analysis (Based on input size) in Complexity Analysis of Algorithms
Asymptotic Analysis is defined as the big idea that handles the above issues in analyzing algorithms. In Asymptotic Analysis, we evaluate the performance of an algorithm in terms of input size (we don't measure the actual running time). We calculate, how the time (or space) taken by an algorithm increases with the input size. Asymptotic notation is
8 min read
Types of Asymptotic Notations in Complexity Analysis of Algorithms
We have discussed Asymptotic Analysis, and Worst, Average, and Best Cases of Algorithms. The main idea of asymptotic analysis is to have a measure of the efficiency of algorithms that don't depend on machine-specific constants and don't require algorithms to be implemented and time taken by programs to be compared. Asymptotic notations are mathemat
8 min read
Asymptotic Analysis and comparison of sorting algorithms
It is a well established fact that merge sort runs faster than insertion sort. Using asymptotic analysis. we can prove that merge sort runs in O(nlogn) time and insertion sort takes O(n^2). It is obvious because merge sort uses a divide-and-conquer approach by recursively solving the problems where as insertion sort follows an incremental approach.
15+ min read
Properties of Asymptotic Notations
Prerequisite: Asymptotic Notations Assuming f(n), g(n) and h(n) be asymptotic functions the mathematical definitions are: If f(n) = Θ(g(n)), then there exists positive constants c1, c2, n0 such that 0 ≤ c1.g(n) ≤ f(n) ≤ c2.g(n), for all n ≥ n0If f(n) = O(g(n)), then there exists positive constants c, n0 such that 0 ≤ f(n) ≤ c.g(n), for all n ≥ n0If
6 min read
Asymptotic Notations and how to calculate them
In mathematics, asymptotic analysis, also known as asymptotics, is a method of describing the limiting behavior of a function. In computing, asymptotic analysis of an algorithm refers to defining the mathematical boundation of its run-time performance based on the input size. For example, the running time of one operation is computed as f(n), and m
5 min read
RFM Analysis Analysis Using Python
In this article, we are going to see Recency, Frequency, Monetary value analysis using Python. But first, let us understand the RFM analysis briefly. What is RFM analysis? RFM stands for recency, frequency, monetary value. In business analytics, we often use this concept to divide customers into different segments, like high-value customers, medium
3 min read
Factor Analysis | Data Analysis
Factor analysis is a statistical method used to analyze the relationships among a set of observed variables by explaining the correlations or covariances between them in terms of a smaller number of unobserved variables called factors. Table of Content What is Factor Analysis?What does Factor mean in Factor Analysis?How to do Factor Analysis (Facto
13 min read
Performance analysis of Row major and Column major order of iterating arrays in C
In computing, row-major order and column-major order are methods for storing multidimensional arrays in linear storage such as random access memory.The two mentioned ways differ from each other with respect to the order in which elements are stored contiguously in the memory. The elements in row-major order are arranged consecutively along the row
2 min read
Analysis and applications Karger’s algorithm for Minimum Cut
We have introduced and discussed below Karger's algorithm in set 1. 1) Initialize contracted graph CG as copy of original graph 2) While there are more than 2 vertices. a) Pick a random edge (u, v) in the contracted graph. b) Merge (or contract) u and v into a single vertex (update the contracted graph). c) Remove self-loops 3) Return cut represent
4 min read
Analysis of test data using K-Means Clustering in Python
This article demonstrates an illustration of K-means clustering on a sample random data using open-cv library. Pre-requisites: Numpy, OpenCV, matplot-lib Let's first visualize test data with Multiple Features using matplot-lib tool. # importing required tools import numpy as np from matplotlib import pyplot as plt # creating two test data X = np.ra
2 min read
three90RightbarBannerImg