0% found this document useful (0 votes)
67 views19 pages

Lec 4 - Big O Notation 17102022 092718am

The document discusses Big O notation and asymptotic analysis for describing how algorithms scale with increasing input size. It defines several common time complexities like constant O(1), linear O(n), quadratic O(n^2), logarithmic O(log n), and O(n log n) time. Examples are provided to illustrate constant, linear, quadratic, and logarithmic time complexities. Rules for analyzing complexity are outlined, such as ignoring constants and lower order terms. Examples are used to demonstrate calculating the time complexity of code snippets.

Uploaded by

doodhjaleybi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views19 pages

Lec 4 - Big O Notation 17102022 092718am

The document discusses Big O notation and asymptotic analysis for describing how algorithms scale with increasing input size. It defines several common time complexities like constant O(1), linear O(n), quadratic O(n^2), logarithmic O(log n), and O(n log n) time. Examples are provided to illustrate constant, linear, quadratic, and logarithmic time complexities. Rules for analyzing complexity are outlined, such as ignoring constants and lower order terms. Examples are used to demonstrate calculating the time complexity of code snippets.

Uploaded by

doodhjaleybi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

DATA STRUCTURES &

ALGORITHMS
Big O Notation

Instructor: Engr. Laraib Siddiqui


Asymptotic Analysis
▪ Asymptotic analysis deals with analyzing the properties of the running time when
the input size goes to infinity (this means a very large input size).
▪ The differences between orders of growths are more significant for larger input
size. Analyzing the running times on small inputs does not allow us to distinguish
between efficient and inefficient algorithms.
▪ The objective of asymptotic analysis is to describe the behavior of a function T(N)
as it goes to infinity.
▪ Asymptotic notations are used to describe the asymptotic analysis.

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???

• Example: accessing any element


in array
8
Linear Time: O(n)
n= number of items

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???

Example: bubble sort,


selection sort, insertion sort
10
Logarithmic Time: O(log n)

log 10 = ?
L0g 20 =?
Sorted
order Log 100 = ?

Example: binary search


11
O(nlog n)
• Growth rate is faster as compared to linear and log functions

Consider buying
pair for your shirts
from store but not
going through every
item

Example: merge sort


12
Complexity graph

13
Rules for analysis
Ignore multiplicative constants

‘n’ gets larger & constant no


6n –> O(N) longer matter

Certain terms dominate others


Ignore lower order terms
O(1) < O(logn) < O(n) < O(nlogn) < O(n2) < O(2n) < O (n!)

14
Rules for analysis

Loops Number of iterations

Nested loops Complexity of inner loop * outer loop

Consecutive statements Addition

If/else Block which take long time

Switch case Block which take long time

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)

Total Time = O(1) + O(1) + O(1) = > O(1)


(drop constant)

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy