Professional Documents
Culture Documents
CS483-04 Non-Recursive and Recursive Algorithm Analysis: Lifei@cs - Gmu.edu
CS483-04 Non-Recursive and Recursive Algorithm Analysis: Lifei@cs - Gmu.edu
CS483-04 Non-Recursive and Recursive Algorithm Analysis: Lifei@cs - Gmu.edu
Analysis
Instructor: Fei Li
Room 443 ST II
f (n) ∈ O(g(n))
f (n) ∈ Ω(g(n))
f (n) ∈ Θ(g(n))
c2 g(n)
f (n)
c1 g(n)
n0 n
f (n) ∈ Θ(g(n))
cg(n)
f (n) f (n)
cg(n)
n0 n n0 n
f (n) ∈ O(g(n)) f (n) ∈ Ω(g(n))
f (n) f ′ (n)
lim = lim ′
n→∞ g(n) n→∞ g (n)
Stirling’s formula
√ n
n! ≈ 2πn( )n
e
where e is the base of natural logarithm, e ≈ 2.718. π ≈ 3.1415.
➣ All logarithmic functions loga n belong to the same class Θ(log n) no matter
what the logarithmic base a > 1 is.
➣ Transitivity
f (n) ∈ O(g(n)) and g(n) ∈ O(h(n)) ⇒ f (n) ∈ O(h(n))
f (n) ∈ Θ(g(n)) and g(n) ∈ Θ(h(n)) ⇒ f (n) ∈ Θ(h(n))
f (n) ∈ Ω(g(n)) and g(n) ∈ Ω(h(n)) ⇒ f (n) ∈ Ω(h(n))
➣ Reflexivity
f (n) ∈ O(f (n))
f (n) ∈ Θ(f (n))
f (n) ∈ Ω(f (n))
➣ Symmetry and Transpose Symmetry
f (n) ∈ Θ(g(n)) if and only if g(n) ∈ Θ(f (n))
f (n) ∈ O(g(n)) if and only if g(n) ∈ Ω(f (n))
➣ Review
➣ Analysis of Non-recursive Algorithms
➣ Analysis of Recursive Algorithms
➣ Examples
max = A[0]
for i = 1 to n − 1
if A[i] > max
do
then max = A[i]
return (max)
for i= 0 to n − 2
for j = i + 1 to n − 1
do if A[i] = A[j]
do
then return (f alse)
return (true)
for i= 0 to n − 1
for j = 0 to n − 1
C[i, j] = 0
do
do for k = 0 to n − 1
do C[i, j] = C[i, j] + A[i, k] · B[k, j]
return (C)
count = 1
while n>1
count = count + 1
do
n = ⌊n/2⌋
return (count)
➣ Review
➣ Analysis of Non-recursive Algorithms
➣ Analysis of Recursive Algorithms
➣ Examples
➣ Example: Find n!
➣ Example: Find n!
Non-recursive: 1 · 2 · 3···n
f actorial = 1
for i = 1 to n
do f actorial = f actorial · i
return (f actorial)
Recurrence: f (n) = n · f (n − 1)
if n =0
then return (1)
count = 1
while n>1
8
<count = count + 1
do
:n = ⌊n/2⌋
return (count)
if i =1
do return (1)
if n =0
return (0)
if n =1
return (1)
Move all the disks from peg a to peg c. Large disk cannot be on top of a smaller one.
http://en.wikipedia.org/wiki/Tower_of_Hanoi
if n =1
Move the disk from a to c
8
<HanoiTower(n − 1, a, b, c)
>
>
else Move the largest disk from a to c
>
>
HanoiTower(n − 1, b, c, a)
:
n!
T (n) = T (n − 1) + 1
Tower of Hanoi
T (n) = 2T (n − 1) + 1
n! (T (n) = T (n − 1) + 1)
T (n) = T (n − 1) + 1
= (T (n − 2) + 1) + 1
= T (n − 2) + 2
··· ···
= T (n − i) + i
··· ···
= T (0) + n = n
n! (T (n) = T (n − 1) + 1)
Tower of Hanoi (T (n) = 2T (n − 1) + 1)
T (n) = 2T (n − 1) + 1
= 2(2T (n − 2) + 1) + 1
= 22 T (n − 2) + 2 + 1
··· ···
= 2i T (n − i) + 2i−1 + · · · + 1
··· ···
= 2n−1 T (1) + 2n−1 + 2n−1 + · · · + 1
n−2
X
n−1
= 2 T (1) + 2i
i=0
= 2n−1 + 2 n−1
−1
= 2n − 1
➣ Problems
1. Prove or find a counter-example:
n n n n
( ) < n! < ( ) , if n ≥ 6.
3 2
2. p. 8, Exercises (1.1) 5, 6.