Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 19

Asymptotic Analysis

What do we analyze in an algorithm?


Correctness
◦ Does the input/output relation match algorithm requirement?
Amount of work done (complexity)
◦ Basic operations to do task
Amount of space used
◦ Memory used
Optimality
◦ Is it possible to do better?

2/4/2020 PREPARED BY TASNEEA HOSSAIN 2


Purpose of Analysis
Given, two algorithms, how would you measure their performance?

Measure the time manually, but that will be prone to error

Analyze mathematically

2/4/2020 PREPARED BY TASNEEA HOSSAIN 3


Asymptotic Notation
The O Notation:
A function f(n)=O(g(n)) if there exists N and c such that f(n)<c.g(n) whenever
n>N.

 Read O as “Big-O” (you’ll also hear it as “order”).


 It’s a measure of the longest time it could take for the algorithm to complete.
 g(n) is an asymptotic upper bound for f(n).

2/4/2020 PREPARED BY TASNEEA HOSSAIN 4


Upper Bound
c.g(n)
time

f(n)

N n

2/4/2020 PREPARED BY TASNEEA HOSSAIN 5


Asymptotic Notation
Big-Omega Notation
A function f(n)= (g(n)) if there exists N and c such that f(n)> c.g(n)
whenever n>N.

Here, g(n) is a lower bound function.


g(n) is an asymptotic lower bound for f(n).
It describes the best performance or the least time taken for an algorithm.

2/4/2020 PREPARED BY TASNEEA HOSSAIN 6


Lower Bound
f(n)
time

c.g(n)

n
N

2/4/2020 PREPARED BY TASNEEA HOSSAIN 7


Asymptotic Notation
Theta Notation
A Function f(n) = (g(n)) if and only if c1.g(n)<f(n)<c2.g(n)
Where c1 and c2 are constants.

The function f(n) is bounded both from the top and the bottom.
g(n) is an asymptotic tight bound for f(n)

2/4/2020 PREPARED BY TASNEEA HOSSAIN 8


Asymptotic Tight Bound
c2.g(n)

time f(n)

c1.g(n)

N n

2/4/2020 PREPARED BY TASNEEA HOSSAIN 9


Theorem
For any two functions f(n) and g(n), we have f(n)= (g(n)) if and only if f(n)=O(g(n)) and f(n)=
(g(n)).

As an example of the application of this theorem, an2+bn2+c= (n2) for any constants a,b and c,
where a>0, immediately implies that an2+bn+c= (n2) and an2+bn+c= O(n2).

2/4/2020 PREPARED BY TASNEEA HOSSAIN 10


2/4/2020 PREPARED BY TASNEEA HOSSAIN 11
Operations
The time required for the following operations is (1):
Assignment operation
Relational operation
Logical operation
Mathematical operation

2/4/2020 PREPARED BY TASNEEA HOSSAIN 12


Control Statements
Control statements comprise of the if-else statements, switch-case statements as well as loops such as
for, while, do-while.
Given
if ( condition) {
// true body
} else {
// false body
}
The run time of a conditional statement is:
The run time of the condition (the test), plus the run time of the body which is run
In most cases, the run time of the condition is (1)

2/4/2020 PREPARED BY TASNEEA HOSSAIN 13


Condition Controlled loops
for( i=0; i<N; i++){
//statements
}
The initialization, condition and increment statements are usually (1).
Assuming there are no break or return statements in the loop, the run time is
(n).

2/4/2020 PREPARED BY TASNEEA HOSSAIN 14


Condition Controlled Loops
If the body does not depend on the variable (in this example, i), then the run
time of
for ( int i= 0; i< n; i++) {
// code which is Theta(f(n))
}
is (n f(n))
If the body is O(f(n)), then the run time of the loop is O(n f(n))

2/4/2020 PREPARED BY TASNEEA HOSSAIN 15


Conditional Statements
Switch case statements are similar to the nested If-else statements and both
would appear to run in O(n) time, where n is the number of cases.

2/4/2020 PREPARED BY TASNEEA HOSSAIN 16


Blocks of code
If one block of code is followed by another block, then the code is said to be run
serially.
If, in a single code, block 1 , block 2 and block 3 require (1) , (n) and (n2)
respectively, the total amount of time required would be (1+n+n2)= (n2).

2/4/2020 PREPARED BY TASNEEA HOSSAIN 17


Serial Statements
Consider the following two problems:
Search through a random list of size n to find the maximum entry, and
Search through a random list of size n to find if it contains a particular entry

What is the proper means of describing the run time of these two algorithms?

2/4/2020 PREPARED BY TASNEEA HOSSAIN 18


Example
Recall the linear search and binary search,
What is the time required by each algorithm?

2/4/2020 PREPARED BY TASNEEA HOSSAIN 19

You might also like