Lecture3 Compressed
Lecture3 Compressed
• The challenge:
Given a recurrence relation for T(n), find a
closed form expression for T(n).
(Size 1)
*
• 𝑇 𝑛 =2⋅𝑇 + 11 ⋅ 𝑛 Another
+
* 22⋅*
approach:
• 𝑇 𝑛 =2⋅ 2⋅𝑇 + + 11 ⋅ 𝑛 Recursively apply
1 +
* the relationship a
• 𝑇 𝑛 =4⋅𝑇 + 22 ⋅ 𝑛
1 bunch until you
see a pattern.
* 22⋅*
• 𝑇 𝑛 =4⋅ 2⋅𝑇 + + 22 ⋅ 𝑛 Formally, this should
3 1 be accompanied
*
• 𝑇 𝑛 =8⋅𝑇 + 33 ⋅ 𝑛 with a proof that
3 the pattern holds!
• MergeSort
• T(n) = 2T(n/2) + O(n)
• T(n) = O( nlog(n) )
What’s the pattern?!?!?!?!
The master theorem
A useful
• A formula that solves formula it is.
recurrences when all of the Know why it works
sub-problems are the same you should.
size.
• We’ll see an example
Wednesday when not all
problems are the same size.
*
• Suppose 𝑇 𝑛 = 𝑎 ⋅ 𝑇 + 𝑂 𝑛H . Then
F
Three parameters:
a : number of subproblems Many symbols
b : factor by which input size shrinks
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
(details on board)
✓
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
Proof of the master theorem
• We’ll do the same recursion tree thing we did for
MergeSort, but be more careful.
*
• Suppose that 𝑇 𝑛 = 𝑎 ⋅ 𝑇 + 𝑐 ⋅ 𝑛H .
F
Hang on! The hypothesis of the Master Theorem was
the the extra work at each level was O(nd). That’s NOT
the same as work <= cnd for some constant c.
Size n
0 1 n 𝑐 ⋅ 𝑛𝑑
𝑛 H
a n/b 𝑎𝑐
n/b n/b n/b 1 𝑏
n/b2 n/b2 𝑛 H
n/b2 n/b2 2 a2 𝑎+ 𝑐
n/b2 n/b2 n/b2 n/b2 𝑏+
…
𝑛 H
n/bt n/bt n/bt n/bt n/bt n/bt
t at n/bt 𝑎O 𝑐
𝑏O
… …
logb(n) 𝑎?@AL * 1 𝑎?@AL * 𝑐
(Size 1)
𝑛
Recursion tree 𝑇 𝑛 =𝑎⋅𝑇 + 𝑐 ⋅ 𝑛H
𝑏 Size of
Amount of
work at this
# each
level
Level problems problem
Size n
0 1 n 𝑐 ⋅ 𝑛𝑑
𝑛 H
a n/b 𝑎𝑐
n/b n/b n/b 1 𝑏
n/b2 Total
n/b work (derivation on board)
2 is at
n/bmost:
2 𝑛 H
a 𝑎+ 𝑐
2
n/b 2
n/b2 2 n/b2 𝑏+
n/b2 n/b2
… QRSL (*)
H
𝑎 O
𝑐 ⋅ 𝑛n/b ⋅ P
𝑏aHt 𝑛 H
n/bt n/bt n/bt n/bt n/bt t
t n/bt 𝑎O 𝑐
OTU 𝑏O
… …
logb(n) 𝑎?@AL * 1 𝑎?@AL * 𝑐
(Size 1)
Now let’s check all the cases
(on board)
Even more generally,
for T(n) = aT(n/b) + f(n)…
TIE!
1 1 1 1 1 1 1 1 1 1
Recap
• The ”Master Method” makes our lives easier.
• But it’s basically just codifying a calculation we
could do from scratch if we wanted to.
Next Time
• What if the sub-problems are different sizes?
• And when might that happen?