You are on page 1of 33

Tugas DAA

- INDIVIDU
jawab exercise (1) dan (2) di slide ke-31 dan 32 ini ppt
nya
kirim ke email bu astuti yg ini lg.astuti@gmail.com
deadline sebelum UAS
-KELOMPOK
tambahkan pertanyaan ama pembahasaan dr yg
temen2/ibunya tanyain pas presentasi, isi nim nama
penanya trus
buat komplekitas algoritma yg lengkap dr presentasi
yg kalian bawain, kirim juga ke email deadline
sebelum UAS

Asymptotic Notations for


Time Efficiency Analysis
Design and Analysis of Algorithms

Contents
Asymptotic Notations:
O (big oh)
(big omega)
(big theta)

Basic Efficiency Classes

In the following
discussion
t(n) & g(n): any nonnegative functions
defined on the set of natural numbers
t(n) analgorithmsrunningtime
Usually indicated by its basic operation count
C(n)

g(n) some simple function to compare


the count with

O(g(n)): Informally
O(g(n)) is a set of all functions with a
smaller or same order of growth as g(n)
Examples:
n O(n2); 100n + 5 O(n2)
n (n-1) O(n2)
n3 O(n2); 0.0001 n3 O(n2); n4+n+1 O(n2)

(g(n)): Informally
(g(n)) is a set of all functions with a
larger or same order of growth as g(n)
Examples:
n3 (n2)
n (n-1) (n2)
100n + 5 (n2)

(g(n)): Informally
(g(n)) is a set of all functions with a
same order of growth as g(n)
Examples:
an2+bn+c; a>0 (n2); n2+sin n (n2)
n (n-1) (n2); n2+log n (n2)
100n + 5 (n2); n3 (n2)

O-notation: Formally
DEF1: A function t(n) is said to be in
O(g(n)), denoted t(n) O(g(n)), if t(n) is
bounded above by some constant multiple
of g(n) for all large n

i.e. there exist some positive constant c


and some nonnegative integer n0, such
that
t(n) cg(n)forallnn0

t(n) O(g(n)): Illustration


cg(n)

doesn't
matter

t(n)

n0

Proving Example: 100n + 5 O(n2)


Remember DEF1: find c and n0, such that
t(n) cg(n)forallnn0
100n + 5 100n+n(foralln5)=101n
101n2 c=101, n0=5
100n + 5 100n+5n(foralln1)=105n
105n2 c=105, n0=1

-notation: Formally
DEF2: A function t(n) is said to be in
(g(n)), denoted t(n) (g(n)), if t(n) is
bounded below by some constant multiple
of g(n) for all large n

i.e. there exist some positive constant c


and some nonnegative integer n0, such
that
t(n) cg(n)forallnn0

t(n) (g(n)): Illustration


t(n)

doesn't
matter

cg(n)

n0

Proving Example: n3 (n2)


Remember DEF2: find c and n0, such that
t(n) cg(n)forallnn0
n3 n2 (foralln0) c=1, n0=0

-notation: Formally
DEF3: A function t(n) is said to be in
(g(n)), denoted t(n) (g(n)), if t(n) is
bounded both above and below by some
constant multiple of g(n) for all large n

i.e there exist some positive constant c1


and c2 and some nonnegative integer n0,
such that
c2g(n) t(n) c1g(n)forallnn0

t(n) (g(n)): Illustration


c1g(n)

doesn't
matter

t(n)

n0

c2g(n)

Proving Example: n(n-1) (n2)


Remember DEF3: find c1 and c2 and some
nonnegative integer n0, such that
c2g(n) t(n) c1g(n)forallnn0
The upper bound: n(n-1) = n2 n
n2 (foralln0)
The lower bound: n(n-1) = n2 n
n2 - nn(foralln2) = n2
c1 = , c2 = , n0 = 2

Basic Efficiency Classes


The time efficiency of a large number of
algorithms fall into only few classes
1, log n, n, n log n, n2, n3, 2n, n!

Multiplicative constants are ignored it is


possible that an algorithm in worse
efficiency class running faster than
algorithm in better class
Exp: Alg A: n3, alg B: 106n2; unless n > 106,
alg B runs faster than alg A

Class: 1
Name: constant
Comment:
Short of best-case efficiency
Very few reasonable examples can be given
algsrunningtimetypicallygoestoinfinity
when its input size grows infinitely large

Class: log n
Name: logarithmic
Comment:
Typically,aresultofcuttingaproblemssize
by a constant factor on each iteration of the
algorithm
Logarithmic alg cannot take into account all its
input or even a fixed fraction of it: any
algorithm that does so will have at least linear
running time

Class: n
Name: linear
Comment:
Algorithms that scan a list of size n (e.g.
sequential search) belong to this class

Class: n log n
Name: n-log-n
Comment:
Many divide-and-conquer algorithms,
including merge sort and quick sort in the
average case, fall into this class

Class: n2
Name: quadratic
Comment:
Typically, characterizes efficiency of
algorithms with two embedded loops
Standard examples: elementary sorting
algorithms and certain operations on n-by-n
matrices

Class: n3
Name: cubic
Comment:
Typically, characterizes efficiency of
algorithms with three embedded loops
Several nontrivial algorithms from linear
algebra fall into this class

Class: 2n
Name: exponential
Comment:
Typical for algorithms that generate all
subsets of an n-element set
Thetermexponentialisoftenusedtoinclude
this and faster orders of growth as well

Class: n!
Name: factorial
Comment:
Typical for algorithms that generate all
permutations of an n-element set

Useful Property
Theorem:
If t1(n) O(g1(n)) and t2(n) O(g2(n)),
then t1(n) + t2(n) O(max{g1(n), g2(n)})
The analogous assertions are true for the
and notations as well

Example
Alg to check whether an array has
identical elements:
Sort the array
Scan the sorted array to check its
consecutive elements for equality

(1) = n(n-1) comparison O(n2)


(2) = n-1 comparison O(n)
The efficiency of (1)+(2) = O(max{n2,n})
= O(n2)

Using Limits for Comparing


OoG
Aconvenientmethodforcomparingorderof
growth of two specific functions
Three principal cases:

0implies
that
has
a
smalle
OoG
t(n)
g(n

t
(
n
)

lim
cimplies
that
has
the
same
OoG
t(n)
as
g(n

g
(
n
)

implies
that
has
a
larger
OoG
t(n)
g(n

The first two cases t(n) O(g(n)); the last two


cases t(n) (g(n)); the second case alone
t(n) (g(n))

Limit-based: why
convenient?
It can take advantage of the powerful
calculus techniques developed for
computing limits, such as
LHopitalsrule

t(
n
)
t'(
n
)
lim
lim
n

g
(
n
) n
g
'(
n
)

Stirlingsformula

for
n
!

2
n
large
valu
of
n
n
n
e

Example (1)
Compare OoG of n(n-1) and n2.
2
n
(
n

1
)1
n

n
1 11

lim

lim

lim
1

n
2
2
n

n 2 n2
2
1
2

The limit = c n(n-1) (n2 )

Compare OoG of log2n and n


1
(log
e
)
log
n
(log
n
)'
n
2
n
2
2
lim

lim

lim

2
log
e
lim

0
2
1
n

n
n (
n
)' 2
n

The limit = 0 log2n has smaller order of n

Example (2)
Compare OoG of n! and 2n.

n
n
! 2
n
n

lim

lim

lim
2
n

lim
2
n

2 2
2
e
2
e

n
n
n
e
n
n n
n
n

n
n

The limit = n! (2n )

Exercises (1)
True or false:
n(n+1)/2 O(n3)
n(n+1)/2 O(n2)
n(n+1)/2 (n3)
n(n+1)/2 (n)

Indicate the class (g(n)):


(n2+1)10
(10n2+7n+3)
2n log (n+2)2+(n+2)2 log (n/2)

Exercises (2)
1. Prove that every polynomial
p(n) = aknk + ak-1nk-1 ++a0 with ak > 0
belongs to (nk)
Prove that exponential functions an have
different orders of growth for different
values of base a > 0

You might also like