2 Analysis II (Normal & Recursive Code I) (ANN1)
2 Analysis II (Normal & Recursive Code I) (ANN1)
Analysis II
(Normal Code: Asymptotic Analysis
Recursive Code: Steps & Master Method)
LOGISTICS
Learning Management System (@trysakai)
– Contains ALL course-related stuff (Assignments, Quizzes, Lec’s, Labs…)
Analysis Design
• Explain the use of big O, big omega & big theta notation to
describe the amount of work done by an algorithm.
• Use recurrence relations to determine the time complexity of
recursive algorithms.
• Solve elementary recurrence relations.
References
• Chapter 3
• Chapter 4
– Section 4.5 (Master Method)
Asymptotic Notations
• Asymptotic Analysis: study the time complexity at large input size
• Can be expressed by:
– Upper bound: O()
– Lower bound: Ω()
– Exact bound: ()
• Always look for
• When it’s difficult to obtain look for O, Ω
PRE-TEST [Revisited]
1. Analyze:
fun(N)
{ sum = 0;
if (N == 1) return 0
for(i = 1 ; i*i < N; i++)
sum += i;
return fun(N/2) × fun(N/2)
}
Since loop from i=1 to N, one can say that # iterations is O(N)
However, this is an Upper bound since the condition is checked on i * i.
Exact bound… stay tuned
Asymptotic Notations
• O-notation
11
Example
10
2 +
2 n
) =
T( n
g(n) = n2
Example
3g(n) = 3n2
10
2 +
2 n
) =
T( n
g(n) = n2
Example
n0=4
Formally:
• Choose c = 3
3g(n) = 3n 2
• Choose n0 = 4
• Then:
10
2 +
2 n
) =
T( n
g(n) = n2
Same example
Formally:
• Choose c = 7
• Choose n0 = 2
• Then:
7g(n) = 7n2
10
2 +
= 2n
)
T(n
re is not a
g(n) = n2 The o ic e
t ” c h
“correc n0
o f c a n d
n0=2
Another example:
g(n) = n2 • Choose c = 1
• Choose n0 = 1
• Then
T(n) = n
Take-away from examples
20
Asymptotic Notations
• -notation
O&↔Θ
21
Examples
1. Show that:
1. 0.5n2 - 3n = (n2)
2. 5n3 + 100n ≠ (n2)
1. Iteration method
2. Master method
3. Recursive Tree method
2. Solve T(N) to Get (or O)
2) Master Method:
Direct apply to the master theory
Suitable ONLY for recurrences of the following form
• The top node effort is f(n) , the next level has work af(n/b) associated with each node,
the next level a2f(n/b2), and so on.
• The total time taken at all levels is the sum of the terms aif(n/bi).
• What this sum looks like depends on how the growth of f(n) compares to the growth
of the number of leaves.
f(N) vs
[Prove: last three videos here] 40
The Master Theorem
C 1: f(n)
ASE 1:
CASE f(n) isis O(),
O(), ε>0
ε>0.. Since
Since the
the leaves
leaves grow
grow faster
fasterthan
than f,f, asymptotically
asymptotically all
all
of
ofthe
thework
workisisdone
doneat atthe
theleaves,
leaves,and
andso T(n)isisΘ()
soT(n) Θ()..
41
The Master Theorem
C
CASE 2: f(n)
ASE 2: f(n) isis Θ()
Θ()..The
Theleaves
leavesgrow
growat
atthe
thesame
samerate
rateas
asf,f,so
sothe
thesame
sameorder
orderof
of
work
work isis done
done at
at every
every level
level of
of the
the tree.
tree. The
The tree has O(lg
tree has O(lg n)
n) levels,
levels, times
times the
the work
work
done
doneon
ononeonelevel,
level,yielding T(n)isisΘ(
yieldingT(n) Θ( lglg n)
n)..
42
The Master Theorem
C 3: f(n)
ASE 3:
CASE f(n) isis Ω()
Ω() ,, ε>0 AND aa f(n/b)
ε>0.. AND f(n/b) ≤≤ cc f(n)
f(n) ,, 00 << cc << 1.1. Since
Since height
height
grows
grows faster
faster than
than the
the number
number ofof leaves,
leaves, asymptotically,
asymptotically, the
the work
work isis done
done at
at the
the root
root
node
nodeand
andinternal
internallevels, T(n) isisΘ(f(n))
levels,T(n) Θ(f(n))..
43
The Master Theorem
where a ³ 1, b > 1, and f is
• If T(n) is a recurrence of the form asymptotically positive.
if (N == 1) return 0
sum += i;
{ sum = 0;
if (N == 1) return 0
sum += i;
ANALYSIS OF RECURSION
• Test yourself @this online quiz#2.
– Master Method Part
– Answers are available