Lesson 2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

ANALYSING

ALGORITHMS
COMPILED BY JN MASI
What does it mean to analyse an algorithm?

• This can mean predicting the resources that the algorithm requires
• Theoretical study of a computer program performance and resource
usage
• These resources can be
• Memory
• Running time
• etc
Input size

• Input size is the number of items in the


input
• The input size depends on the problem
• The input can be numbers , an array, a
graph etc.
Memory

• This is the amount of memory the


algorithm will occupy while its running
Running time

• Running time is the number of primitive steps executed. It is


often affected by the size of inputs
• For a given size of n, time T is expressed to run the algorithm
as a function of n
• T(n)
• The rate at which cost of the algorithm grows as the size of
input grows is called growth rate
Growth rate Examples
• // Return position of largest value in "A"
static int largest(int[] A)
• {
int currlarge = 0; // Holds largest element position
• for (int i=1; i<A.length; i++)
if (A[currlarge] < A[i])
• currlarge = i;
return currlarge; // Return largest position
• }

• The growth rate is T(n)=cn


Growth rate Examples

• sum = 0;
for (i=1; i<=n; i++)
for (j=1; j<=n; j++)

sum++;

• The growth rate is T(n)=cn²


Growth rate Examples

• A growth rate of cn (c being a positive


constant) is said to be linear
• n² growth rates are said to be quadratic
• 2ⁿ growth rates are said to be exponential
Growth rate
Choosing an efficient algorithm

• How do you compare 2 algorithms that solve the same


problem? Which one will be more efficient?

• Implement both algorithms in programs and run them


on a suitable range of inputs
• Run them on the same computer, given equal time and
effort during the coding, same compiler etc
Choosing an efficient algorithm

• Drawbacks
• 1. A lot of effort putting the algorithm in code, yet in the
end only 1 will be choosen
• 2. There is a chance 1 of the algorithms was written better
than the other
• 3. The choice of test cases might favor one algorithm,
• 4. You might find out that none of the implemented
algorithms suite your budget
Choosing an efficient algorithm

• Growth rate
This is the rate at which cost of the algorithm
grows as the size of input grows
•a
Ways of analysing an algorithm

• Best, worst and average cases


• Empirical analysis
• Asymptotic analysis
Best, average and worst cases

• Let us consider the linear search algorithm


• The best case would be finding the number at position (0)
• Average case will be finding the number in the middle of
the array n/2
• And worst case (Maximum run time over all inputs of size
n) would be finding the number at the end of the array
Best, average and worst cases

• Normally best cases are not considered when analyzing


algorithms, they rarely happen
• The good thing about worst case is that u know for
certain that the algorithm will perform that well/badly
• Average cases come in handy if you know the contents of
your input and how the input is distributed
Asymptotic analysis

• This type of algorithm measures the efficiency of an algorithm or


program as the input increases
• It is an estimating technique
• In asymptotic analysis, we ignore all other constants and only focus on
the growth rate
• Defined as the study of an algorithm as the input gets big or reaches
its limit
• This analysis is usually used to compare 2 or more algorithms
Asymptotic analysis

• Terms used in asymptotic analysis


• Upper bound
• Lower bounds
• Big O notation
• Big omega notation(Ω notation)
• Big Theta notation (Θ Notation)
Upper bound

• Indicates the highest growth rate that an algorithm


can have
• We measure upper bound for best, average and
worst cases
• We use the Big O notation to describe the upper
bound of an algorithm
Upper bound-Example

• Assuming the algorithm has an input size of n


• And the average case growth rate is n²
• We can say the algorithm has an upper bound to its
growth rate in the average case of f(n)
• Using Big O notation - T(n)= O(f(n)) in average
case
Upper bound

• Big O notation describes the upper bound


• It states a claim about the highest amount of
resources used (Mostly time) by an algorithm for
some class of input size n
Lower bound

• Lower bound measures the least amount of resources


(usually time) used by an algorithm
• The lower bound is also calculated in the worst, average
and best cases
• Like Big O notation, a similar notation is used to denote
the lower bound of an algorithm
• The lower bound is denoted by the big omega Ω
Lower bound- Example

• Assuming the lower bound of an algorithm is


f(n), the lower bound expression can be written
as T(n)=Ω(f(n))
• For linear search, the Omega notation for
average and worst case is T(n)= Ω(f(n))
Big Theta notation Θ

• When the upper and lower bounds are the same within a
constant factor, the big theta notation is used to denote
this
• For example if the upper bound is T(n)= O(f(n)) and the
lower bound is T(n)=Ω(f(n))
• This means Ω(f(n))= O(f(n))
• Therefore T(n)= Θ(f(n))
Theta notation Θ

• When you have enough information about an


algorithm, use the Θ notation because you are
sure that the upper and lower bounds match
• If insufficient information is provided the upper
or lower bound notations may be used
Calculating the run time of an algorithm

• Ex 1
• Int Num = 2
• Ex 2
• sum = 0;
for (i=1; i<=n; i++)
sum += n;
Calculating the run time of an algorithm

• Ex 3
sum = 0;
for (j=1; j<=n; j++)
for (i=1; i<=j; i++)
sum++;
for (k=0; k<n; k++)
A[k] = k;
Calculating the run time of an algorithm

• Ex 4
sum1 = 0;
for (i=1; i<=n; i++)
for (j=1; j<=n; j++)
sum1++;
sum2 = 0;
for (i=1; i<=n; i++)
for (j=1; j<=i; j++)
sum2++;
QUESTIONS???

You might also like