Design and Analysis
of Algorithms
Lecture 3
Instructor: Yusra Mansoor
Email:
[email protected]Classes of Time Functions
O(1) ------------------ Constant
O(logn) ------------------ Logarithmic
O(n) ------------------ Linear
O(n^2) ------------------ Quadratic
O(n^3) ------------------ Cubic
O(2^n) ------------------ Exponential
1<logn<n<nlogn<n^2<n^3<2^n……<n^n
Classes of Function Comparison
Asymptotic Dominance in Action
Different running times
Exact Analysis is hard
Asymptotic Efficiency
We only look at input sizes large enough to
make the order of growth of the running time
relevant.
Asymptotic Efficiency
An asymptote of a real-valued function y = f(x) is a
curve which describes the behavior of f as either x or y
tends to infinity.
Asymptote Why?
Comparison of Two Functions
n3 + 2n2 vs. 100n2 + 1000
Comparison of Two Functions
5n5 vs. n!
Comparison of Two Functions
82log(n) vs. 3n7 + 7n
Growth Rates
Growth rates of functions: 1E+29
◦ Linear n 1E+27
1E+25
Cubic
Quadratic
◦ Quadratic n2 1E+23
1E+21 Linear
◦ Cubic n3 1E+19
1E+17
T(n)
1E+15
1E+13
1E+11
1E+9
1E+7
1E+5
1E+3
1E+1
1E-1
1E-1 1E+2 1E+5 1E+8
n
Growth Rates
The growth rate is not 1E+25 Quadratic
affected by 1E+23
Quadratic
1E+21
◦ constant factors or 1E+19
Linear
Linear
◦ lower-order terms 1E+17
1E+15
T(n)
Examples 1E+13
1E+11
◦ 102n + 105 is a linear function 1E+9
1E+7
◦ 105n2 + 108n is a quadratic 1E+5
function 1E+3
1E+1
1E-1
1E-1 1E+1 1E+3 1E+5 1E+7 1E+9
n
Big-O Notation
Givenfunctions f(n) and g(n), we say that f(n) is
O(g(n)) if there are positive constants
c>0 and n0>=1 such that
f(n) cg(n) for n n0
1<logn<n<nlogn<n^2<n^3…..<2^n……<n^
n
lower bound upper
bound
tightly bound
F(n)=2n^2+3n+2 f(n)=O(n)
false
2n^2+3n+2<=O(n^3) f(n)=O(n^2)
F(n)=O(2^n) f(n)=O(logn)
Big-O Notation
Given functions f(n) and g(n), we
say that f(n) is O(g(n)) if there are
positive constants
c and n0 such that
f(n) cg(n) for n n0
Example: 2n + 10 is O(n)
◦ 2n + 10 cn
◦ (c 2) n 10
◦ n 10/(c 2)
◦ Pick c = 3 and n0 = 10
Big-O Notation
Example: the function n2 is
not O(n)
n2 cn
n c
The above inequality cannot be
satisfied since c must be a
constant
Example (O)
Big-O and Growth Rate
Omega- Notation
Given functions f(n) and g(n), we say that f(n) is
(g(n)) if there are positive constants
c and n0 such that
f(n) cg(n) for n n0
Theta- Notation
Given functions f(n) and g(n), we say that f(n) is
(g(n)) if there are positive constants
c and n0 such that
c’•g(n) f(n) c’’•g(n) for n n0
Asymptotically tight bound
Relatives of Big-Oh
Intuition for Asymptotic Notation
Big-Oh
f(n) is O(g(n)) if f(n) is asymptotically less than or equal
to g(n)
big-Omega
f(n) is (g(n)) if f(n) is asymptotically greater than or
equal to g(n)
big-Theta
f(n) is (g(n)) if f(n) is asymptotically equal to g(n)
little-oh
f(n) is o(g(n)) if f(n) is asymptotically strictly less than
g(n)
little-omega
Example Uses of the Relatives of
Big-O