cs3230 Lec01b Beamer
cs3230 Lec01b Beamer
cs3230 Lec01b Beamer
School of Computing
National University of Singapore
Introduction
Asymptotic Analysis
Big O (upper bound)
Ω (lower bound)
New notation Θ (tight bound)
Little-o and ω
Taking Limits
Algorithm
Algorithm
“A sequence of unambiguous and executable instructions for
solving a problem (given a valid input, obtain a valid output)”
Let’s elaborate:
▶ What are the valid inputs?
▶ What is the meaning of unambiguous instructions?
▶ What is the meaning of executable instructions?
▶ Are all algorithms deterministic?
▶ Are all algorithms terminate?
Details
We assume that the algorithm does not need to concern itself with
invalid input, e.g., for Fib(n) later, we will assume that n will
always be a non-negative Integer
1
Unless we do not understand that language
An Example
Solve https://open.kattis.com/problems/rijeci
▶ Fib(0) = 0
▶ Fib(1) = 1
▶ For n > 1, Fib(n) = Fib(n − 1) + Fib(n − 2)
▶ First 10 terms: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, . . .
define Fib(n)
if n <= 1
return n
else
return Fib(n-1)+Fib(n-2)
Solve https://open.kattis.com/problems/rijeci
define IFib(n)
if n <= 1
return n
else
prev2 = 0
prev1 = 1
for i = 2 to n
temp = prev1
prev1 = prev1+prev2
prev2 = temp
return prev1
Live Problem Solving - with iterative IFib(n)
Solve https://open.kattis.com/problems/rijeci
We have seen that the recursive Fib(n) (in fast C++) is too slow
Sometimes, we do trade-offs:
▶ If space is not an issue, most of the time, we sacrifice
(or use more) space to gain faster time
▶ For some applications (e.g., Big Data), we may have to
sacrifice time so that we are able to process the data
T (0) = T (1) = 2
define Fib(n) (if+return)
if n <= 1
return n For n ≥ 2, T (n) =
else T (n − 1) + T (n − 2) + 6
return Fib(n-1)+Fib(n-2) (if+else+two function
calls+add+return)
See the recursion tree@VisuAlgo
(which is a big tree for large n) So T (n) ≥ Fib(n)
T (n) is exponential in n
n−2
How to show that Fib(n) ≥ 2 2 ?
Let Fib(1) = 1
Notice that for n ≥ 2, (including n ≥ 3)
Fib(n) = Fib(n − 1) + Fib(n − 2),
Fib(n) ≥ Fib(n − 2) + Fib(n − 2),
Fib(n) ≥ 2 · Fib(n − 2),
i.e., after two terms, the value of Fib(n) will double (or more), i.e.,
Fib(1), Fib(3), Fib(5), Fib(7), Fib(9), . . . = 1, 2, 5, 13, 34, . . .
define IFib(n)
if n <= 1
return n For n ≥ 2,
else T (n) ≈ 4 + (n − 1) ∗ 5 + 1
prev2 = 0 (if+else+two assignments
prev1 = 1 + (n − 1) iterations,
for i = 2 to n each takes ≈ 5 steps
temp = prev1 +return)
prev1 = prev1+prev2
prev2 = temp So T (n) ≈ 5n, linear in n
return prev1 This is much faster than
the recursive version that
See the recursion DAG@VisuAlgo
runs exponential in n
(small and proportional to n)
f ∈ O(g ) if there exists constant c > 0 and n0 > 0 such that for
all n ≥ n0 : 0 ≤ f (n) ≤ c · g (n)
O(g ) = {f : there exists constant c > 0 and n0 > 0 such that for
all n ≥ n0 , 0 ≤ f (n) ≤ c · g (n)}
f ∈ Ω(g ) if there exists constant c > 0 and n0 > 0 such that for
all n ≥ n0 : 0 ≤ c · g (n) ≤ f (n)
PS: We usually have f (n) as the more complex function and g (n)
to be the simpler one, i.e., 7n2 + 5n + 77 ∈ Ω(n2 )
Pictorial interpretation of Ω-notation
New notation Θ (tight bound)
Example: n ∈ o(n2 )
1
For any constant c > 0, let n0 = 1 + c (setting n0 = 2 is also ok)
Then, for n ≥ n0 , n < c · n2
But n2 − n ∈ / o(n2 )
Let’s say we pick c = 21 (just need to show one counterexample),
for any n0 and large enough n, we have:
n2 − n > 12 n2
1 2
2n > n
n2 > 2n
ω (strict lower bound)
f ∈ ω(g ) if for any constant c > 0, there exists n0 > 0 such that
for all n ≥ n0 : 0 ≤ c · g (n) < f (n)
Example: n2 − 36 ∈ ω(n) √
For any constant c > 0, let n0 > 36 + c,
0 ≤ c · n < n2 − 36
Asymptotic Notation: Taking Limits
Proof:
Hence, for any constant c > 0 (i.e., we can set c = ϵ), ∃n0 > 0,
such that ∀n ≥ n0 ,
f (n) < ϵ · g (n), i.e.,
f (n) < c · g (n),
f (n) ∈ o(g (n))
The slides are modified from previous editions of this course and
similar course elsewhere