Lecture 5.
Analysis of Running time of algorithms
(Best, Average and Worst cases)
1
Recap
RAM model is used to measure the run time of an algorithm by counting the number of steps.
Space complexity of an algorithm refer to the amount of memory required to run.
Time complexity of an algorithm refer to its running time, which depends on input size.
Primitive operations refer to the unit of operation that can be identified in the pseudo-code
2
Counting Primitive Operations
Algorithm ArrayMax(A,n)
{An array A storing N integers
and find the largest element in A.}
currentMax = A[0] 2 steps + 1 to initialize i
for (i=1;i<=n-1;i++) 2 step each time (compare i to n, inc i)
n-1 if (currentMax < A[i]) 2 steps
times How often done??
currentMax = A[i] 2 steps
return currentMax 1 step
Between 4(n-1) and 6(n-1) in the loop
3 It depends on the order the numbers appear in in A[]
Run Time Analysis
Pseudo code to find product of two numbers
int method()
{ Run Time Complexity will be =
int a,b,c; --------- c1 c1+c2+c2 + c3 + c4
a=2; --------- c2 = c1 + 2c2 + c3 + c4 (as all constant)
b=3; --------- c2
=C
c= a*b; --------- c3
printf(“Product of a and b is %d”, c);
return 0; ----------- c4
}
4
Run Time Analysis (Cont !!!)
int method()
{
int a,b,large; --------- c1
a=2; --------- c2 Run Time Complexity will be =
b=3; --------- c2
c1+c2+c2 + c3 +c2 + c2 + c4
if(a>b) -------- c3
large=a; -------- c2 = c1 + 4c2 + c3 +c4 (as all constants)
else =C
large = b; ------- c2 Note:- =You can say there should be
printf(“Large number is %d”, large);
3c2 instead of 4c2 (Coefficient have
Return 0; ---------- c4
}
no impact)
5
Run Time Analysis (Cont !!!)
int method()
{
int I, codd, ceven, a[5]={5,4,3,2,1}; --------- c1
ceven=0;codd=0; ----------- c2
for(i=0;i<=4;i++) ------ c3 Run Time Complexity will be = c1+c2
if(a[i]%2==0) -------- c4 + n* (c3 +c4 + c5)+ c6
ceven++;-------- c5 = c1 + c2 + n*c +c6
else
= n*c + c
codd++; ------- c5
= n
printf(“Total evens %d and Total Odd %d”, ceven, codd);
Return 0; ---------- c6
}
6
Run Time Analysis (Cont !!!)
int method()
{
int I, a[5]; --------- c1
N for(i=0;i<=4;i++) ------ c2
Scanf(“%d”, &a[i]);
Run Time Complexity will be = c1 + n
for(i=0;i<=4;i++) * c2 + n*(c2+c3) + n * c2 + c4
A[i]=a[i]+5; ----------- c3 = c1 + 2n*c2 + n * c
N
for(i=0;i<=4;i++) = c1 + n * c + n * c
N
print(“%d”, a[i]); = 2n*c +c1
return 0; ---------- c4 = 2n
} =n
7
Run Time Analysis (Cont !!!)
int method() Note:-
{ •C3 refer to array scripts use
int r,c, a[][]= { {1,2}, {1,3}}; --------- c1 •C4 refer to arithmetic op +
•C5 refer to assignment
for(c=0;c<=1;c++) ------ c2
for(r=0;r<=1;r++)
N*m=n2 a[r][c]=a[r][c]+5;----------- c3+c4+c5
for(c=0;c<=1;c++) Run Time Complexity will be = c1 + n * m *
N * m = n 2 for(r=0;r<=1;r++) (c2+c3+c4+c5) + n * m * c3 + c6
printf(“%d”, a[r][c]); = c1 +c6 + n*m*c + n*m*c +
return 0; ---------- c6 = 2*n*m*c + c
}
= n*m*c
8 = n*m = n 2
Run Time Analysis (Cont !!!)
int method() Note:-
•C4 refer to array scripts use
{ •C5 refer to arithmetic op +
int r,c,,s, a[][]= { {1,2}, {1,3}}; --------- c1 •C2 refer to assignment
S=0; ------------- c2
Run Time Complexity will be = c1 + n * m *
for(c=0;c<=1;c++) ------ c3
(c2+c3+c4+c5) + n (c2+c3+c4+c5 )+c6
{ for(r=0;r<=1;r++) = c1+c6 + n*m*c + n*c
N times a[r][c]=a[r][c]+5; ---- c4+c5 +c2= n*m + n + c
N * m = n 2 s=s+a[r][0]; ------------- c4+c5 +c2
= n2 + n
}
Printf(“Sum of element of 1st row %d”, s);
return 0; ---------- c6
}
9
Types of Algorithm Complexity
Worst Case Complexity:
– the function defined by the maximum number of steps taken on any
instance of size n
Best Case Complexity:
– the function defined by the minimum number of steps taken on any
instance of size n
Average Case Complexity:
– the function defined by the average number of steps taken on any
instance of size n
10
Types of Algorithm Complexity
(Example: Linear Search)
5 7 2 6 9 12 1
Worst Case Complexity:
– You want to search 1 in above array which is at location N
– You need N steps
Best Case Complexity:
– You want to search 5 in above array which is at location 1
– You need 1 steps
Average Case Complexity:
– You want to search 2 or 9 etc in above array
11 – You need 3 steps for 2 and 5 steps for 9
Best, Worst, and Average Case Complexity
Number of Worst Case
steps Complexity
Average Case
Complexity
Best Case
Complexity
N
(input size)
12
Relationship between complexity types and
running time of Algorithms
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
Lower Bound Running Time Upper Bound
Average case
– Provides a prediction about the running time
– 13
Assumes that the input is random
Running Time
Most algorithms transform input
objects into output objects. best case
average case
The running time of an algorithm worst case
120
typically grows with the input size.
100
Average case time is often difficult
Running Time
80
to determine.
60
We focus on the worst case
40
running time.
– 20
Easier to analyze
– 0
Crucial to applications such as 1000 2000 3000 4000
games, finance and robotics Input Size
14
Home Work
Exercise-1.
– Write a pseudo code which find the sum of two 3*3 matrics and then calculate its running time.
Exercise-2.
– Write a pseudo code which read a number N and print whether it is prime or not . After that, calculate the run time complexity
15
Summary
Number of steps you need to find the complexity of an algorithm
Run time complexity vary due to use fo different constructs such as simple, nested and consecutive loop, selection and assignment statement.
Types of algorithms complexity are best, average and worst which also depend on number of steps or comparisons
16
In Next Lecturer
In next lecture, we will talk about the asymptotic analysis fro comparisons of algorithms.
17