Worst-Case Analysis: - in This Class, We Will Focus On
Worst-Case Analysis: - in This Class, We Will Focus On
12345678
• In this class, we will focus on worst-case analysis
Here is my algorithm!
Here is an
Algorithm: input!
Do the thing
Do the stuff
Return the answer
Algorithm
designer
Pros: Cons:
• Abstracts away from • Only makes sense if n is
hardware- and language- large (compared to the
specific issues. constant factors).
• Makes algorithm analysis 2100000000000000 n
much more tractable. is “better” than n2 ?!?!
pronounced “big-oh of …” or sometimes “oh of …”
• Formally,
𝑇𝑇 𝑛𝑛 = 𝑂𝑂 𝑔𝑔 𝑛𝑛
⟺
∃𝑐𝑐, 𝑛𝑛0 > 0 𝑠𝑠. 𝑡𝑡. ∀𝑛𝑛 ≥ 𝑛𝑛0 ,
0 ≤ 𝑇𝑇 𝑛𝑛 ≤ 𝑐𝑐 ⋅ 𝑔𝑔(𝑛𝑛)
𝑇𝑇 𝑛𝑛 = 𝑂𝑂 𝑔𝑔 𝑛𝑛
Example ⟺
∃𝑐𝑐, 𝑛𝑛0 > 0 𝑠𝑠. 𝑡𝑡. ∀𝑛𝑛 ≥ 𝑛𝑛0 ,
2𝑛𝑛2 + 10 = 𝑂𝑂 𝑛𝑛2 0 ≤ 𝑇𝑇 𝑛𝑛 ≤ 𝑐𝑐 ⋅ 𝑔𝑔(𝑛𝑛)
3n2
2n2 + 10
n2
𝑇𝑇 𝑛𝑛 = 𝑂𝑂 𝑔𝑔 𝑛𝑛
Example ⟺
∃𝑐𝑐, 𝑛𝑛0 > 0 𝑠𝑠. 𝑡𝑡. ∀𝑛𝑛 ≥ 𝑛𝑛0 ,
2𝑛𝑛2 + 10 = 𝑂𝑂 𝑛𝑛2 0 ≤ 𝑇𝑇 𝑛𝑛 ≤ 𝑐𝑐 ⋅ 𝑔𝑔(𝑛𝑛)
Formally:
3n2 • Choose c = 3
• Choose n0 = 4
• Then:
∀𝑛𝑛 ≥ 4,
0 ≤ 2𝑛𝑛2 + 10 ≤ 3 ⋅ 𝑛𝑛2
n2
𝑇𝑇 𝑛𝑛 = 𝑂𝑂 𝑔𝑔 𝑛𝑛
same Example ⟺
∃𝑐𝑐, 𝑛𝑛0 > 0 𝑠𝑠. 𝑡𝑡. ∀𝑛𝑛 ≥ 𝑛𝑛0 ,
2𝑛𝑛2 + 10 = 𝑂𝑂 𝑛𝑛2 0 ≤ 𝑇𝑇 𝑛𝑛 ≤ 𝑐𝑐 ⋅ 𝑔𝑔(𝑛𝑛)
Formally:
7n2 • Choose c = 7
• Choose n0 = 2
• Then:
∀𝑛𝑛 ≥ 2,
0 ≤ 2𝑛𝑛2 + 10 ≤ 7 ⋅ 𝑛𝑛2
n2
𝑇𝑇 𝑛𝑛 = 𝑂𝑂 𝑔𝑔 𝑛𝑛
Another example: ⟺
∃𝑐𝑐, 𝑛𝑛0 > 0 𝑠𝑠. 𝑡𝑡. ∀𝑛𝑛 ≥ 𝑛𝑛0 ,
𝑛𝑛 = 𝑂𝑂(𝑛𝑛 )
2 0 ≤ 𝑇𝑇 𝑛𝑛 ≤ 𝑐𝑐 ⋅ 𝑔𝑔(𝑛𝑛)
g(n) = n2 • Choose c = 1
• Choose n0 = 1
• Then
∀𝑛𝑛 ≥ 1,
T(n) = n 0 ≤ 𝑛𝑛 ≤ 𝑛𝑛2
Ω(…) means a lower bound
• We say “T(n) is Ω(g(n))” if g(n) grows at most as fast
as T(n) as n gets large.
• Formally,
𝑇𝑇 𝑛𝑛 = Ω 𝑔𝑔 𝑛𝑛
⟺
∃𝑐𝑐, 𝑛𝑛0 > 0 𝑠𝑠. 𝑡𝑡. ∀𝑛𝑛 ≥ 𝑛𝑛0 ,
0 ≤ 𝑐𝑐 ⋅ 𝑔𝑔 𝑛𝑛 ≤ 𝑇𝑇 𝑛𝑛
Switched these!!
𝑇𝑇 𝑛𝑛 = Ω 𝑔𝑔 𝑛𝑛
Example ⟺
∃𝑐𝑐, 𝑛𝑛0 > 0 𝑠𝑠. 𝑡𝑡. ∀𝑛𝑛 ≥ 𝑛𝑛0 ,
𝑛𝑛 log 2 𝑛𝑛 = Ω 3𝑛𝑛 0 ≤ 𝑐𝑐 ⋅ 𝑔𝑔 𝑛𝑛 ≤ 𝑇𝑇 𝑛𝑛
• Choose c = 1/3
• Choose n0 = 3
• Then
∀𝑛𝑛 ≥ 3,
3𝑛𝑛
0≤ ≤ 𝑛𝑛 log 2 𝑛𝑛
3
Θ(…) means both!
• We say “T(n) is Θ(g(n))” if:
T(n) = O(g(n))
-AND-
T(n) = Ω(g(n))
Some more examples
• All degree k polynomials* are O(nk)
• For any k ≥ 1, nk is not O(nk-1)
Asymptotic Notation
• This makes both Plucky and Lucky happy.
• Plucky the Pedantic Penguin is happy because
there is a precise definition.
• Lucky the Lackadaisical Lemur is happy because we
don’t have to pay close attention to all those pesky
constant factors like “11”.
• The challenge:
Given a recurrence relation for T(n), find a
closed form expression for T(n).
𝑛𝑛
• Suppose 𝑇𝑇 𝑛𝑛 = 𝑎𝑎 ⋅ 𝑇𝑇 + 𝑂𝑂 𝑛𝑛𝑑𝑑 . Then
𝑏𝑏
Three parameters:
a : number of subproblems Many
b : factor by which input size shrinks symbols
d : need to do nd work to create all the those are….
subproblems and combine their solutions.
Technicalities II
Plucky the
Integer division Pedantic Penguin
𝑛𝑛
𝑇𝑇 𝑛𝑛 = 2 ⋅ 𝑇𝑇 + 𝑂𝑂(𝑛𝑛)
2
• From now on we’ll mostly ignore floors and ceilings
in recurrence relations.
𝑛𝑛
𝑇𝑇 𝑛𝑛 = 𝑎𝑎 ⋅ 𝑇𝑇 + 𝑂𝑂 𝑛𝑛𝑑𝑑 .
Examples 𝑏𝑏
✓
• Needlessly recursive integer mult.
a=4
• T(n) = 4 T(n/2) + O(n) b=2 a > bd
✓
• T(n) = O( n2 ) d=1
• Karatsuba integer multiplication
a=3
• T(n) = 3 T(n/2) + O(n) b=2 a > bd
✓
• T(n) = O( nlog_2(3) ≈ n1.6 ) d=1
• MergeSort
a=2
• T(n) = 2T(n/2) + O(n) b=2 a = bd
✓
• T(n) = O( nlog(n) ) d=1
• That other one
a=1
• T(n) = T(n/2) + O(n)
b=2 a < bd
• T(n) = O(n) d=1
Understanding the Master Theorem
𝑛𝑛
• Suppose 𝑇𝑇 𝑛𝑛 = 𝑎𝑎 ⋅ 𝑇𝑇 + 𝑂𝑂 𝑛𝑛𝑑𝑑 . Then
𝑏𝑏
TIE!
1 1 1 1 1 1 1 1 1 1