11-Best Case, Worst Case, Average Case Analysis-08-08-2022
11-Best Case, Worst Case, Average Case Analysis-08-08-2022
– # of elements in a matrix
– # of bits in the binary representation of the input
– vertices and edges in a graph
4
Asymptotic Notations
• Asymptotic Notations are the expressions that
are used to represent the complexity of an
algorithm.
• There are three types of analysis that we
perform on a particular algorithm.
5
Types of Analysis
• Best Case: In which we analyse the performance
of an algorithm for the input, for which the
algorithm takes less time or space.
• Worst Case: In which we analyse the
performance of an algorithm for the input, for
which the algorithm takes long time or space.
• Average Case: In which we analyse the
performance of an algorithm for the input, for
which the algorithm takes time or space that lies
between best and worst case.
6
Types of Analysis
• Worst case
– Provides an upper bound on running time
– An absolute guarantee that the algorithm would not run longer, no
matter what the inputs are
• Best case
– Provides a lower bound on running time
– Input is the one for which the algorithm runs the fastest
• Average case
– Provides a prediction about the running time
– Assumes that the input is random
7
Types of Data Structure Asymptotic Notation
8
Big-O Notation (Ο)
Big O notation specifically describes worst case scenario. It
represents the upper bound running time complexity of an
algorithm.
Lets take few examples to understand how we represent the time
and space complexity using Big O notation.
O(1)
Big O notation O(1) represents the complexity of an algorithm that
always execute in same time or space regardless of the input data.
O(1) example
The following step will always execute in same time(or space)
regardless of the size of input data.
Accessing array index(int num = arr[5])
9
• O(n)
• Big O notation O(N) represents the complexity of an
algorithm, whose performance will grow linearly (in
direct proportion) to the size of the input data.
• O(n) example
The execution time will depend on the size of array.
When the size of the array increases, the execution
time will also increase in the same proportion
(linearly) Traversing an array
10
O(n^2)
Big O notation O(n^2) represents the complexity of
an algorithm, whose performance is directly
proportional to the square of the size of the input
data.
• O(n^2) example
Traversing a 2D array
• Other examples: Bubble sort, insertion sort and
selection sort algorithms
11
Similarly there are other Big O notations such as:
logarithmic growth O(log n), log-linear growth
O(n log n), exponential growth O(2^n) and factorial
growth O(n!).
The following diagram shows the comparative
performance of algorithms denoted by these
notations
12
Algorithmic Examples of Runtime Analysis:
Some of the examples of all those types of algorithms (in worst-case scenarios) are
mentioned below:
16
BIG Oh
Big O Notation
Omega Notation ( - notation)
Asymptotic notations (cont.)
• - notation
22
Θ Notation
Asymptotic notations (cont.)
• -notation
27
Basic asymptotic efficiency classes
1 constant
log n logarithmic
n linear
n log n n-log-n
n2 quadratic
n3 cubic
2n exponential
n! factorial
constant