Table of Contents

Types of Analysis

  1. Aposteriori Analysis - By code execution
  2. Apriori Analysis - Before code execution

Aposteriori Analysis

Two methods of Aposteriori Analysis -

  1. Step Count
  2. Order of Magnitude

1. Step Count Method

Counting the exact number of fundamental operations done in the code.

Example -

p = q + r                        // 2 operations
s = t * u                        // 2 operations
 
for (i=1, i<=n, i++) {           // (1 + n * 4 + 1 = 4n + 2) operations
	a = b + c
}
 
for (j=1; j<=n, j++) {           // (1 + n * (m+2) + 1) operations
	for (k=1, k<=n, k++) {       // m = 1 + n * 4 + 1 = 4n + 2 operations
		a = b + c                // Total = 4n^2 + 4n + 2 operations
	}
}

Total number of operations in the above code is - .

2. Order of Magnitude Method

Instead of having the exact count, consider the order of magnitude of the fundamental statements executed.

Time Complexity

The amount of time required by any program to execute, expressed as a function of the input size is called the Time Complexity.

Cases for Time Complexity

Based upon the input there are 3 cases -

  1. Best Case - Minimum number of times.
  2. Worst Case - Maximum number of times.
  3. Average Case - Probability dependent.

Mathematical Series

Arithmetic Progression -

Geometric Progression -

Harmonic Progression -

Sum of Squares of first n Natural Numbers -

Sum of Cubes of first n Natural Numbers -

Time Complexity of Recursive functions

  1. Back Substitution Method - Gives value of recurrence and TC [Universal Method]
  2. Master’s Method - Works only in specific cases and only gives TC
  3. Recursive Tree Approach - Only TC

Back Substitution Method

Consider the code for calculating the Factorial -

f(n) {
	if (n == 1) {
		return 1
	}
	return n * f(n-1)
}

Assume that the time complexity of f(n) is . We can write as -

Using this information we can solve for -

This can be generalized as . To reach the base condition we need .

Time Complexity of Loops

The TC for any loop depends upon two factors -

  1. The number of times the loop is running/iterating/repeating.
  2. The complexity of all the individual statements within the loop body.

How to calculate -

  1. Identify the variable(s) determining the termination criteria.
  2. Identify the pattern of updation for these variables over each iteration and generalize for some iteration.
  3. Calculate the iteration in terms for for which the termination criteria doesn’t satisfy. Thus the loop would run for these iterations. Check Question 4 for example.

Space Complexity

The amount of Auxiliary memory space (excluding the space required by the input) required by the program, expressed as a function of the input size is called the Space Complexity. Check Question 5 for example.

Questions

1. TC of Recurrence

Q1) Find the time complexity and recurrence value for -

where and are constants and is the input size.

Sol - Since dominates the constant , we can simplify to be -

For what value of would be achieve the base case of ?

Putting and in ,


Q2) Find the worst case and best case time complexity for the below diagram

Sol - Best Case TC is achieved by Path A - Worst Case TC is achieved by Path A -


2. TC of loops

Q3) Find the TC of the below code -
i = 1, a = 0;
while (i = 2) {
	a += 1;
}

Sol - The condition i=2 is not a comparison but an assignment. Thus the loop’s condition is always true and this runs infinitely.


Q4) Find the TC of the below code -

Sol -

For the loop to terminate , so will be cause the last iteration.

Thus the loop would run for iterations. Thus TC =


3. Space Complexity

Q5) Find the SC of the below code -

Sol - The elements that would be added to the recursion stack would be - . The total number of elements would be -

Thus the Space Complexity is .