0% found this document useful (0 votes)
20 views14 pages

Topic 1 - Time Complexity Analysis

This document discusses complexity analysis and Big O notation. It defines asymptotic analysis and describes how it can determine the best, average, and worst case running times of an algorithm. It introduces Big O notation and examples of different Big O notations. It then provides general rules for determining the time complexity of loops, nested loops, consecutive statements, conditional statements, and algorithms that use logarithms. Examples are given to demonstrate applying these rules. The overall topic is analyzing the time complexity of algorithms using asymptotic analysis and Big O notation.

Uploaded by

razaaziz9191
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views14 pages

Topic 1 - Time Complexity Analysis

This document discusses complexity analysis and Big O notation. It defines asymptotic analysis and describes how it can determine the best, average, and worst case running times of an algorithm. It introduces Big O notation and examples of different Big O notations. It then provides general rules for determining the time complexity of loops, nested loops, consecutive statements, conditional statements, and algorithms that use logarithms. Examples are given to demonstrate applying these rules. The overall topic is analyzing the time complexity of algorithms using asymptotic analysis and Big O notation.

Uploaded by

razaaziz9191
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

CS-216 : Data Structures and

Algorithms

Topic1: Complexity Analysis

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 ____

Solve above examples

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

The base of log is basically the number with


which division of data set is performed.

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

You might also like