Modelling Algorithm Growth Functions
Modelling Algorithm Growth Functions
Functions
By
W.G. Gondwe
Lecturer, Computer Networks
CS&IT Department
Malawi University of Science & Technology
Mathematical Modelling & Fn Growth
rates
• Recall: Complexity (time), worst-case assumption
• Analysis of algorithms – predicting how much resources an
algorithm will consume
• Analysis of algorithms – rate of increase in complexity as a
function of input
• i.e. Need to model a function of the algorithm in terms of input
size (n)
Mathematical Modelling cont..
• Example 1:
Algorithm to search for item p in a list of n items:
Analysis:
Assuming step i takes ci time to execute,
for counter := 1 to n 1 then the total execution time is given by:
if list[counter] = p then 2
nc1 + nc2 + nc5 + k , where k is a constant
return counter 3
else 4 (ignoring the trivial steps)
counter := counter + 1 5
end if 6 = n(c1 + c2 + c5) + k
= c0n + k (where c0 = c1 + c2 + c5)
end for 7
i.e. the growth function for the algorithm
is given by c0n + k (linear function of n)
Mathematical Modelling cont..
• Example 2:
Counting number of inversions:
Given a list a of size n, the number of inversions is a measure
of how unsorted it is.
It is measured by counting all a[i] > a[j] where i < j
inv_count := 0 1
for i := 1 to n 2
for j := i+1 to n 3
if a[i] < a[j] then 4
inv_count := inv_count + 1 5
end if 6
end for 7
end for 8
Mathematical modeling – Growth rates
• Types of growth rates (functions)
Constant – f(n) = c
Linear - f(n) = cn
Log – f(n) = logb n
Log-linear - f(n) = n log n Increasing
Quadratic – f(n) = n2 + c complexity
Cubic – f(n) = n3 + c
Exponential – f(n) = cn
Factorial – f(n) = n!
Math modelling – growth rages
Graphical visualization of
algorithm complexities (growth
rates)