0% found this document useful (0 votes)
25 views

Session 3

The document discusses asymptotic analysis of algorithms. It defines asymptotic notations like Big-Oh, Omega and Theta that describe the growth rate of functions for large inputs. Big-Oh notation provides an upper bound, Omega provides a lower bound, and Theta provides a tight bound on the growth rate. Common functions like constant, logarithmic, linear, quadratic, cubic and exponential are classified based on their growth rate. The document also explains asymptotic analysis with an example of Towers of Hanoi recursive algorithm.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Session 3

The document discusses asymptotic analysis of algorithms. It defines asymptotic notations like Big-Oh, Omega and Theta that describe the growth rate of functions for large inputs. Big-Oh notation provides an upper bound, Omega provides a lower bound, and Theta provides a tight bound on the growth rate. Common functions like constant, logarithmic, linear, quadratic, cubic and exponential are classified based on their growth rate. The document also explains asymptotic analysis with an example of Towers of Hanoi recursive algorithm.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Algorithm Design and Analysis

Session -3
Asymptotic notations

1
Asymptotic efficiency

• Asymptotic efficiency means study of algorithms efficiency for large


inputs.

• To compare two algorithms with running times f(n) and g(n), we need a
rough measure that characterizes how fast each function grows as n
grows.

• Hint: use rate of growth


• Compare functions asymptotically!
(i.e., for large values of n)
Functions ordered by growth rate
Function Name
1 Growth is constant
logn Growth is logarithmic
n Growth is linear
nlogn Growth is n-log-n
n2 Growth is quadratic
n3 Growth is cubic
2n Growth is exponential
n! Growth is factorial

1 < logn < n < nlogn < n2 < n3 < 2n < n!


– To get a feel for how the various functions grow with n, you are advised to
study the following figs:
2
• The low order terms and constants in a function are relatively insignificant for large n
n2 + 100n + log10n + 1000 ~ n2

i.e., we say that n2 + 100n + log10n + 1000 and n2 have the same rate of growth

Some more examples


• n4 + 100n2 + 10n + 50 is ~ n4
• 10n3 + 2n2 is ~ n3
• n3 - n2 is ~ n3
• constants
 10 is ~ 1
 1273 is ~ 1
Asymptotic/order Notations

• Asymptotic/order notation describes the behavior of functions for


the large inputs.

• Big Oh(O) notation:


– The big oh notation describes an upper bound on the asymptotic growth
rate of the function f.

Definition: [Big “oh’’]


– f(n) = O(g(n)) (read as “f of n is big oh of g of n”) iff there exist
positive constants c and n0 such that
f(n)  cg(n) for all n, n  n0.
• The definition states that the function f(n) is at most c times the function g(n) except
when n is smaller than n0.
• In other words, f(n) grows slower than or same rate as” g(n).
• When providing an upper –bound function g for f, we normally use a single term in
n.
• Examples
– f(n) = 3n+2
• 3n + 2 <= 4n, for all n >= 2, 3n + 2 =  (n)

– f(n) = 10n2+4n+2
• 10n2+4n+2 <= 11n2, for all n >= 5,  10n2+4n+2 =  (n2)

– f(n)=6*2n+n2=O(2n) /* 6*2n+n2 7*2n for n4 */


• It also possible to write 10n2+4n+2 = O(n3) since 10n2+4n+2 <=7n3 for n>=2

• Although n3 is an upper bound for 10n2+4n+2, it is not a tight upper bound; we can
find a smaller function (n2) that satisfies big oh relation.

• But, we can not write 10n2+4n+2 =O(n), since it does not satisfy the big oh relation
for sufficiently large input.
• Omega () notation:
– The omega notation describes a lower bound on the asymptotic growth
rate of the function f.

Definition: [Omega]
– f(n) = (g(n)) (read as “f of n is omega of g of
n”) iff there exist positive constants c and n0
such that f(n)  cg(n) for all n,
n  n0.
• The definition states that the function f(n) is at least c times the function g(n) except when n is
smaller than n0.
• In other words,f(n) grows faster than or same rate as” g(n).
• Examples
– f(n) = 3n+2
• 3n + 2 >= 3n, for all n >= 1, 3n + 2 =  (n)

– f(n) = 10n2+4n+2
• 10n2+4n+2 >= n2, for all n >= 1,  10n2+4n+2 =  (n2)

• It also possible to write 10n2+4n+2 = (n) since 10n2+4n+2 >=n for n>=0

• Although n is a lower bound for 10n2+4n+2, it is not a tight lower bound; we can find a larger
function (n2) that satisfies omega relation.

• But, we can not write 10n2+4n+2 = (n3), since it does not satisfy the omega relation for sufficiently
large input.
• Theta () notation:
– The Theta notation describes a tight bound on the asymptotic
growth rate of the function f.

Definition: [Theta]
– f(n) = (g(n)) (read as “f of n is theta of g of n”) iff there
exist positive constants c1, c2, and n0 such that c1g(n)  f(n)
 c2g(n) for all n, n  n0.
• The definition states that the function f(n) lies between c1 times the function g(n) and c2 times the
function g(n) except when n is smaller than n0.
• In other words,f(n) grows same rate as” g(n).

• Examples:-
– f(n) = 3n+2
• 3n <= 3n + 2 <= 4n, for all n >= 2,  3n + 2 =  (n)

– f(n) = 10n2+4n+2
• n2<= 10n2+4n+2 <= 11n2, for all n >= 5,  10n2+4n+2 =  (n2)
• But, we can not write either 10n2+4n+2= (n) or 10n2+4n+2= (n3), since neither of these will satisfy
the theta relation.
Big-Oh, Theta, Omega

Tips :
• Think of O(g(n)) as “less than or equal to” g(n)
– Upper bound: “grows slower than or same rate as” g(n)

• Think of Ω(g(n)) as “greater than or equal to” g(n)


– Lower bound: “grows faster than or same rate as” g(n)

• Think of Θ(g(n)) as “equal to” g(n)


– “Tight” bound: same growth rate

• (True for large N)


Example: Towers of Hanoi problem
Algorithm TowersOfHanoi(n, x, y, z)
{
If (n ≥ 1) then
{
TowersOfHanoi(n-1, x, z, y);
Write(“move top disk from tower” , x, “to
top of tower”, y);
TowersOfHanoi(n-1, z, y, x);
}
}
17
18
1. For the functions  n^k and c^n, what is the asymptotic relationship
between these functions?
Assume that k >= 1 and c > 1 are constants.

2. Trace the Towers of Hanoi algorithm for number of disks=4

20

You might also like