DSA Week3 Pasrt 1
DSA Week3 Pasrt 1
9/16/2024 1
Last time….
• Sorting: InsertionSort and MergeSort
• What does it mean to work and be fast?
• Worst-Case Analysis
• Big-Oh Notation
• Analyzing running time of recursive algorithms
• By writing out a tree and adding up all the work done.
9/16/2024 2
Today
• Recurrence Relations!
• How do we calculate the runtime a recursive algorithm?
• The Master Method
• A useful theorem so we don’t have to answer this
question from scratch each time.
• The Substitution Method
• A different way to solve recurrence relations, more
general than the Master Method.
9/16/2024 3
Running time of MergeSort
• Let T(n) be the running time of MergeSort on a
length n array.
• The challenge:
Given a recurrence relation for T(n), find a
closed form expression for T(n).
Note that
𝑛
• For example, T(n) = O(nlog(n)) 𝑇 𝑛 ≤2⋅𝑇
2
+ 11 ⋅ 𝑛
(with a ≤) is also a recurrence
relation! Does it matter for a
9/16/2024 conclusion like T(n) = O(n log(n))? 6
Technicalities I
Base Cases
• Formally, we should always have base cases with
recurrence relations.
𝑛
• 𝑇 𝑛 =2⋅𝑇 + 11 ⋅ 𝑛 with 𝑇 1 = 1
2
is not the same function as
𝑛
• 𝑇 𝑛 =2⋅𝑇 + 11 ⋅ 𝑛 with 𝑇 1 = 1000000000
2
• However, no matter what T is, T(1) is O(1), so sometimes
we’ll just omit it. Why is T(1) = O(1)?
9/16/2024 7
• You played around with these examples (when n is
a power of 2):
𝑛
1. 𝑇1 𝑛 = 𝑇1 2
+ 𝑛, 𝑇 1 =1
𝑛
2. 𝑇 𝑛 = 2 ⋅ 𝑇 2
+ 𝑛, 𝑇 1 =1
𝑛
3. 𝑇2 𝑛 = 4 ⋅ 𝑇2 2
+ 𝑛, 𝑇 1 =1
9/16/2024 8
One approach for all of these
Size n
• The “tree” approach
from last time. n/2 n/2
9/16/2024
(Size 1) 9
Contribution
𝑛 at this layer:
• 𝑇1 𝑛 = 𝑇1 + 𝑛, 𝑇1 1 = 1.
2 n
Size n
• Adding up over all layers:
n/2 n/2
log(𝑛)
𝑛
𝑖 = 2𝑛 − 1 n/4 n/4
2
𝑖=0
…
• So 𝑇1 𝑛 = 𝑂 𝑛 . n/2t n/2t
…
1
9/16/2024 (Size 1) 10
𝑛
• 𝑇2 𝑛 = 4𝑇2 + 𝑛, 𝑇2 1 = 1.
2 Contribution
• Adding up over all layers: at this layer:
• MergeSort
• T(n) = 2T(n/2) + O(n)
• T(n) = O( nlog(n) )
9/16/2024 What’s the pattern?!?!?!?! 12
The master theorem
A useful
• A formula for many formula it is.
recurrence relations. Know why it works
• You’ll come up with an you should.
example in next class when it
won’t work.
O 𝑛𝑑 log 𝑛 if 𝑎 = 𝑏 𝑑
𝑇 𝑛 = O 𝑛𝑑 if 𝑎 < 𝑏 𝑑
O 𝑛log𝑏 𝑎
if 𝑎 > 𝑏 𝑑
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
9/16/2024
and combine their solutions. 14
Technicalities II
Integer division
• If n is odd, I can’t break it up into two problems of
size n/2.
𝑛 𝑛
𝑇 𝑛 =𝑇 +𝑇 + 𝑂(𝑛)
2 2
• However one can show that the Master theorem
works fine if you pretend that what you have is:
𝑛
𝑇 𝑛 =2⋅𝑇 + 𝑂(𝑛)
2
• From now on we’ll mostly ignore floors and ceilings
in recurrence relations.
9/16/2024 15
𝑛
𝑇 𝑛 =𝑎⋅𝑇 + 𝑂 𝑛𝑑 .
Examples 𝑏
O 𝑛𝑑 log 𝑛 if 𝑎 = 𝑏𝑑
(details on board)
𝑇 𝑛 = O 𝑛𝑑 if 𝑎 < 𝑏𝑑
O 𝑛log𝑏 𝑎
if 𝑎 > 𝑏𝑑
✓
• 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
9/16/2024 16
Understanding the Master Theorem
O 𝑛𝑑 log 𝑛 if 𝑎 = 𝑏 𝑑
𝑇 𝑛 = O 𝑛𝑑 if 𝑎 < 𝑏 𝑑
O 𝑛log𝑏 𝑎
if 𝑎 > 𝑏 𝑑
9/16/2024 18
The eternal struggle
9/16/2024 20
First example: tall and skinny tree
𝑛
1. 𝑇 𝑛 = 𝑇 + 𝑛, 𝑎 < 𝑏𝑑 Size n
2
TIE!
1 1 1 1 1 1 1 1 1 1
9/16/2024 23
What have we learned?
• The “Master Method” makes our lives easier.
• But it’s basically just codifying a calculation we
could do from scratch if we wanted to.
9/16/2024 24
The Substitution Method
• Another way to solve recurrence relations.
• More general than the master method.
9/16/2024 25
The Substitution Method
first example
9/16/2024 26
𝑛
𝑇 𝑛 =2⋅𝑇 + 𝑛, with 𝑇 1 = 1.
2
9/16/2024 28
Next Time
• What happens if the sub-problems are different sizes?
• And when might that happen?
9/16/2024 29