What is Asymptotic Analysis and how to calculate it?
💻 coding

What is Asymptotic Analysis and how to calculate it?

2 min read 307 words
2 min read
ShareWhatsAppPost on X
  • 1Asymptotic analysis describes the limiting behavior of functions as their input size approaches infinity.
  • 2The running time of loops can be calculated by multiplying the time of statements by the number of iterations.
  • 3Logarithmic complexity indicates that an algorithm reduces problem size by a constant fraction, typically resulting in O(logn) time.

AI-generated summary · May not capture all nuances

Key Insight
AskGif

"Asymptotic analysis describes the limiting behavior of functions as their input size approaches infinity."

What is Asymptotic Analysis and how to calculate it?

In mathematical analysis, asymptotic analysis, also known as asymptotics, is a method of describing limiting behavior.

As an illustration, suppose that we are interested in the properties of a function f(n) as n becomes very large. If f(n) = n^2 + 3n, then as n becomes very large, the term 3n becomes insignificant compared to n^2. The function f(n) is said to be "asymptotically equivalent to n^2, as n → ∞". This is often written symbolically as f(n) ~ n^2, which is read as "f(n) is asymptotic to n^2".

Guidelines for Asymptotic Analysis

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

//n times execution
for(int i=0;i<=n;i++) {
	n = n + 5; // constant time c
}
	
Total time = c x n = cn = O(n)

2) Nested loops: Analyze from the inside out. Total running time is the product of the sizes of all the loops.

//outer loop executes n times
	for(int i=0;i<n;i++) {
		//inner loop executes n times
		for(int j=0;j<n;j++) {
			n = n + 5; // constant time c
		}
	}
	
	Total time = c x n x n = cn^2 = O(n^2)

3) Logarithmic complexity: An algorithm is O(logn) if it takes a constant time to cut the problem size by a fraction (usually by 1/2). As an example let us consider the following example:

for(int i=0;i<n;) {
		i = i*2;
	}

On careful observation, the value of i is doubling every time. Initially i = 1, in next step i = 2, and in subsequent steps i =4, 8 and so on.

Now we have

2^k = n

taking log on both sides

log(2^k) = logn
klog2 = logn
k = logn // assuming base is 2

Total time = O(logn)

Enjoyed this article?

Share it with someone who'd find it useful.

ShareWhatsAppPost on X

AskGif

Published on 16 July 2018 · 2 min read · 307 words

Part of AskGif Blog · coding

You might also like

What is Asymptotic Analysis and how to calculate it? | AskGif Blog