Lec 4 - Big O Notation 17102022 092718am
Lec 4 - Big O Notation 17102022 092718am
ALGORITHMS
Big O Notation
2
Function Bounds
Lets understand with the help of example. Suppose we have a function 10N2
Can we say it is bounded by 11N2 and 9N2 for all N ≥ 1?
• i.e 10N2 cannot go above 11N2 and doesn’t come down below 9N2 for all values of N.
10N2 is sandwiched between 9N2 and 11N2
• Now if f(n) is 10N2 and g(n) is N2
• Then we say that f(n) is Θ (g(n))
3
Big Oh Notation
Sometimes we are only interested in proving one bound.
We use O-notation, when we have only an asymptotic upper bound.
4
Big Oh Notation
Simplified analysis of an algorithm’s efficiency.
• The letter O is used because the rate of growth of a function is also called its order
• Used in complexity theory, computer science and mathematics to describe the
behavior of functions.
• It determines how fast a function grows or declines.
For worst
cases
5
Big Oh Notation
• Let f (n) and g(n) be functions mapping non-negative numbers to non-negative
numbers.
• Big-Oh. f (n) is O(g(n)) if there is a constant c > 0 and a constant n0 ≥ 1 such that f
(n) ≤ c · g(n) for every number n ≥ n0.
6
Big Oh
7
Constant Time: O(1)
Run in constant time if it requires the
same amount of time regardless of the
input size.
Daily life
Example???
What will be
the worst
case???
Worst case
need ‘n’ steps
for ‘n’ items
best
Example: traversing an array
average
9
Quadratic Time: O(n2)
Worst case
• n= number of need ‘n*n’ steps for desired
output
items
What will be
the worst
case???
log 10 = ?
L0g 20 =?
Sorted
order Log 100 = ?
Consider buying
pair for your shirts
from store but not
going through every
item
13
Rules for analysis
Ignore multiplicative constants
14
Rules for analysis
15
Example
x= 5 + (15 * 30); // O(1)
(independent of input size)
x= 5 + (15 * 30); // O(1)
y= 6-4; // O(1)
print x+y; // O(1)
16
Example
for (int i = 0; i < n; i ++)
O(n)
sum = sum – i;
for (int i = 0; i < n * n ; i ++)
O(n2)
sum = sum + i;
sum = 0
for (int i = 0; i < n; i ++)
O(n2)
for (int j = 0; j < n; j ++)
sum + = i * j;
17
Practice
int sum( int n )
{
int partialSum;
partialSum = 0; Complexity??
for( int i = 1; i <= n; ++i )
partialSum += i * i * i;
return partialSum;
}
18
Practice
Fiblist(n)
create an array F [0 . . . n]
F [0] ← 0
F [1] ← 1
for i from 2 to n:
F [i ] ← F [i − 1] + F [i − 2] Complexity??
return F [n]
19