Topic 1 - Time Complexity Analysis
Topic 1 - Time Complexity Analysis
Algorithms
1
Asymptotic analysis
⚫ Asymptotic analysis of an algorithm refers to
defining the mathematical boundation/framing of
its run-time performance. Using asymptotic
analysis, we can very well conclude the best case,
⚫ average case, and worst case scenario of an
algorithm.
⚫ Best Case − Minimum time required for
program execution.
⚫ Average Case − Average time required for
program execution.
⚫ Worst Case − Maximum time required for
program execution.
2
3
4
Spring Semester 2020 5
Big O Notation
⚫ Describes the limiting behaviour of the
function when the argument tends
towards a particular value or infinity ,
usually in terms of a simpler function.
⚫ Big O notation gives the upper bound
which will be determined by the most
dominant term.
6
Examples of Big O Notations
1. T(n)=2n³+4n²logn is __O(n³)______
2. T(n)= 2¹ºº is _______
3. T(n)= 5n+nlogn is _______
4. T(n)= 5nlognˆn is ____
7
General rules
⚫ Rule#1 (For loop):
The running time of ‘for’ loop is equal to the
sum of running time of individual statements
along with the running time of the ‘for’ loop
Algo:
1. Sum=0
2. for (i=1, i<=n, i++)
3. sum+=I
4. return sum
big O notation is O(n)
8
General rules
⚫ Rule#2 (Nested ‘For’ loops):
The total running time of a statement inside a
group of nested ‘for’ loop is equal to the
running time of statement multiplied by the
product of sizes of all ‘for’ loops
Algo:
1- sum=0
2- for i=1 to i=n # of iteration=n
3- for j=1 to j=n # of iteration=n
4- sum++
big O notation is O(n²)
9
General rules
⚫ Rule#3 (Consecutive Statements):
The running time of individual consecutive
statements are added to calculate the running
time of algorithm.
Algo:
1- sum=0 # consecutive statement
2- for i=1 to i=n # of iteration=n
3- sum++
big O notation is O(n)
10
General rules
⚫ Rule#4 (Conditional Statements):
The running time of an if/else statement is
never more than the running time of test
plus the larger of running time of S1 and S2.
Algo:
1. if (condition)
2. S1
3. else
4. S2
11
General rules
⚫ Rule#5 :
An algorithm is O(log n) if it takes constant
time to divide the problem size/data set by
a fraction (which is usually half or ½).
12
Example for rule#5
int divide_sum (int n)
{ Iteration n=8
int sum=0
while (n>1) 1 8
{ 2 4
sum+=n
n/=2 3 2
}
return sum O(logn)
} // Calculation done in class
13
Practice problems T(n) =? O(?)
14