You are on page 1of 13

Algorithm and

complexity
GUIDELINES FOR ASYMPTOTIC ANALYSIS
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
Nested loop
Analyse from the insite out. Total running time is the product of the sizes of all the loops
Consecutive statements
Add the times complexities of each statements
If-then-else statement
Worst-case running time: the test, plus either the then part or the else part (which ever is the larger)
Logarithmic complexity
An algorithm is O(logn) if it takrd a constant time to cut the problem size by a fraction (usually by 1/2). As an
example let us consider the following program:

If we look carefully, the value of is doubling every time. Initially , in next step ,
and in subsequent steps and so on. Let us assume that the loop is executing
some times. At steps and we come out of loop. Taking logarithm on both
sides, gives.
Master theorem
The simplest form of the Master method is:

To solve these type of recurrence relations, we have to go through three different cases as

𝑎≥ 1 𝑎𝑛𝑑 𝑏>1
Master theorem solved example
Method of Guessing and confirming
This method for solving recurrences consists of two steps:
Guess the form of the solution.
Use mathematical induction to find constants in the form and show that the solution works.
The inductive hypothesis is applied to smaller values, similar to recursive calls bring us closer to
the base case.
The substitution method is powerful to establish lower or upper bounds on a recurrence.

You might also like