CS3383 Lecture 1.2: The Master
Theorem with applications
David Bremner
September 28, 2016
Outline
Divide and Conquer Continued
The Master Theorem
Matrix Multiplication
Contents
Divide and Conquer Continued
The Master Theorem
Matrix Multiplication
Generic divide and conquer algorithm
function Solve(P)
if |π | is small then
SolveDirectly(π )
else
π1 β¦ πuοΏ½ = π―πΊπππππππ(π )
for π = 1 β¦ π do
πuοΏ½ = π²ππ
ππΎ(πuοΏ½ )
end for
Combine(π1 β¦ πuοΏ½ )
end if
end function
βΆ How many times do we recurse?
Generic divide and conquer algorithm
function Solve(P)
if |π | is small then
SolveDirectly(π )
else
π1 β¦ πuοΏ½ = π―πΊπππππππ(π )
for π = 1 β¦ π do
πuοΏ½ = π²ππ
ππΎ(πuοΏ½ )
end for
Combine(π1 β¦ πuοΏ½ )
end if
end function
βΆ How many times do we recurse?
βΆ what fraction of input in each subproblem?
Generic divide and conquer algorithm
function Solve(P)
if |π | is small then
SolveDirectly(π )
else
π1 β¦ πuοΏ½ = π―πΊπππππππ(π )
for π = 1 β¦ π do
πuοΏ½ = π²ππ
ππΎ(πuοΏ½ )
end for
Combine(π1 β¦ πuοΏ½ )
end if
end function
βΆ How many times do we recurse?
βΆ what fraction of input in each subproblem?
βΆ How much time to combine results?
Common recursive structure
A typical Divide and Conquer algorithm has
Common recursive structure
A typical Divide and Conquer algorithm has
b : the branch factor, number of recursive calls
per instantiation
Common recursive structure
A typical Divide and Conquer algorithm has
b : the branch factor, number of recursive calls
per instantiation
s : the split, the inverse of the input size
reduction (so recursing on π/2 would be π = 2)
Common recursive structure
A typical Divide and Conquer algorithm has
b : the branch factor, number of recursive calls
per instantiation
s : the split, the inverse of the input size
reduction (so recursing on π/2 would be π = 2)
d : the degree of the polynomial of the running
time of the ο¬nd+combine steps
Common recursive structure
A typical Divide and Conquer algorithm has
b : the branch factor, number of recursive calls
per instantiation
s : the split, the inverse of the input size
reduction (so recursing on π/2 would be π = 2)
d : the degree of the polynomial of the running
time of the ο¬nd+combine steps
Variations
Common recursive structure
A typical Divide and Conquer algorithm has
b : the branch factor, number of recursive calls
per instantiation
s : the split, the inverse of the input size
reduction (so recursing on π/2 would be π = 2)
d : the degree of the polynomial of the running
time of the ο¬nd+combine steps
Variations
βΆ e.g. one call of
1
3
and one call of 23 ,
Common recursive structure
A typical Divide and Conquer algorithm has
b : the branch factor, number of recursive calls
per instantiation
s : the split, the inverse of the input size
reduction (so recursing on π/2 would be π = 2)
d : the degree of the polynomial of the running
time of the ο¬nd+combine steps
Variations
1
2
βΆ e.g. one call of
3 and one call of 3 ,
βΆ partition+combine step Ξ(π log π).
The Master Theorem
If β constants π > 0, π > 1 and π β₯ 0 such that
π (π) = π β
π (β uοΏ½uοΏ½ β) + Ξ(πuοΏ½ ), then
The Master Theorem
If β constants π > 0, π > 1 and π β₯ 0 such that
π (π) = π β
π (β uοΏ½uοΏ½ β) + Ξ(πuοΏ½ ), then
Ξ(πuοΏ½ )
if π > πππuοΏ½ π (equiv. to π < π uοΏ½ )
β§
{
π (π) = β¨ Ξ(πuοΏ½ log π) if π = πππuοΏ½ π (equiv. to π = π uοΏ½ )
log uοΏ½
uοΏ½
{
β© Ξ(π uοΏ½ ) if π < πππuοΏ½ π (equiv. to π > π )
The Master Theorem
If β constants π > 0, π > 1 and π β₯ 0 such that
π (π) = π β
π (β uοΏ½uοΏ½ β) + Ξ(πuοΏ½ ), then
Ξ(πuοΏ½ )
if π > πππuοΏ½ π (equiv. to π < π uοΏ½ )
β§
{
π (π) = β¨ Ξ(πuοΏ½ log π) if π = πππuοΏ½ π (equiv. to π = π uοΏ½ )
log uοΏ½
uοΏ½
{
β© Ξ(π uοΏ½ ) if π < πππuοΏ½ π (equiv. to π > π )
A proof of this follows.
Proof of Master theorem, in pictures
Size n
branching factor b
Size ns
n
Size s2
....................
Depth
Size 1
logs n
........
Width b
logs n
=
nlogsb
Proof of Master Theorem
We assume w.l.o.g. π is an integer power of π . (If
not, then what do we do?)
Proof of Master Theorem
We assume w.l.o.g. π is an integer power of π . (If
not, then what do we do?)
The height of our recursion tree is loguοΏ½ π.
Proof of Master Theorem
We assume w.l.o.g. π is an integer power of π . (If
not, then what do we do?)
The height of our recursion tree is loguοΏ½ π. At level
π of the recursion tree (counting from 0) we have:
Proof of Master Theorem
We assume w.l.o.g. π is an integer power of π . (If
not, then what do we do?)
The height of our recursion tree is loguοΏ½ π. At level
π of the recursion tree (counting from 0) we have:
uοΏ½
βΆ the size of the data =
uοΏ½uοΏ½
Proof of Master Theorem
We assume w.l.o.g. π is an integer power of π . (If
not, then what do we do?)
The height of our recursion tree is loguοΏ½ π. At level
π of the recursion tree (counting from 0) we have:
uοΏ½
βΆ the size of the data =
uοΏ½uοΏ½
βΆ
uοΏ½
the time for the combine step = π β
( uοΏ½uοΏ½uοΏ½ )
Proof of Master Theorem
We assume w.l.o.g. π is an integer power of π . (If
not, then what do we do?)
The height of our recursion tree is loguοΏ½ π. At level
π of the recursion tree (counting from 0) we have:
uοΏ½
βΆ the size of the data =
uοΏ½uοΏ½
βΆ
βΆ
uοΏ½
the time for the combine step = π β
( uοΏ½uοΏ½uοΏ½ )
the number of recursive instantiations = πuοΏ½
Proof of Master Theorem
We assume w.l.o.g. π is an integer power of π . (If
not, then what do we do?)
The height of our recursion tree is loguοΏ½ π. At level
π of the recursion tree (counting from 0) we have:
uοΏ½
βΆ the size of the data =
uοΏ½uοΏ½
uοΏ½
the time for the combine step = π β
( uοΏ½uοΏ½uοΏ½ )
βΆ the number of recursive instantiations =
πuοΏ½
And so
loguοΏ½ uοΏ½
π uοΏ½
π (π) = β π β
( uοΏ½ ) β
πuοΏ½
π
uοΏ½=0
βΆ
Proof of Master theorem, π = π π
loguοΏ½ uοΏ½
loguοΏ½ uοΏ½
π uοΏ½ β
β
π (π) = β πβ
(
)β
π = πβ
π β
β β ( uοΏ½ ) β
uοΏ½ )uοΏ½
π
(π
uοΏ½=0
β uοΏ½=0
β
πuοΏ½
uοΏ½
uοΏ½
Proof of Master theorem, π = π π
loguοΏ½ uοΏ½
loguοΏ½ uοΏ½
π uοΏ½ β
β
π (π) = β πβ
(
)β
π = πβ
π β
β β ( uοΏ½ ) β
uοΏ½ )uοΏ½
π
(π
uοΏ½=0
β uοΏ½=0
β
πuοΏ½
uοΏ½
uοΏ½
If π = π uοΏ½ , then
loguοΏ½ uοΏ½
β β 1β
β = π β
πuοΏ½ loguοΏ½ π
π (π) = π β
πuοΏ½ β
β
β uοΏ½=0 β
Proof of Master theorem, π = π π
loguοΏ½ uοΏ½
loguοΏ½ uοΏ½
π uοΏ½ β
β
π (π) = β πβ
(
)β
π = πβ
π β
β β ( uοΏ½ ) β
uοΏ½ )uοΏ½
π
(π
uοΏ½=0
β uοΏ½=0
β
πuοΏ½
uοΏ½
uοΏ½
If π = π uοΏ½ , then
loguοΏ½ uοΏ½
β β 1β
β = π β
πuοΏ½ loguοΏ½ π
π (π) = π β
πuοΏ½ β
β
β uοΏ½=0 β
so π (π) is Ξ(πuοΏ½ log π).
Proof of Master Theorem π β π π (1 of 2)
Otherwise (π β π uοΏ½ ), we have a geometric series,
uοΏ½
π (π) = π β
π β
(
( uοΏ½uοΏ½uοΏ½ )
loguοΏ½ uοΏ½+1
uοΏ½
uοΏ½uοΏ½
β1
β1
)
Proof of Master Theorem π β π π (1 of 2)
Otherwise (π β π uοΏ½ ), we have a geometric series,
uοΏ½
π (π) = π β
π β
(
( uοΏ½uοΏ½uοΏ½ )
loguοΏ½ uοΏ½+1
uοΏ½
uοΏ½uοΏ½
β1
Applying the identity
1
π
=
,
π/π β 1
πβπ
β1
)
Proof of Master Theorem π β π π (1 of 2)
Otherwise (π β π uοΏ½ ), we have a geometric series,
uοΏ½
π (π) = π β
π β
(
( uοΏ½uοΏ½uοΏ½ )
loguοΏ½ uοΏ½+1
uοΏ½
uοΏ½uοΏ½
β1
β1
)
Applying the identity
1
π
=
,
π/π β 1
πβπ
π uοΏ½
π loguοΏ½ uοΏ½+1
uοΏ½
π (π) =
β
π β
π β
(( uοΏ½ )
β 1)
π β π uοΏ½
π
Proof of Master Theorem π β π π (1 of 2)
Otherwise (π β π uοΏ½ ), we have a geometric series,
uοΏ½
π (π) = π β
π β
(
( uοΏ½uοΏ½uοΏ½ )
loguοΏ½ uοΏ½+1
uοΏ½
uοΏ½uοΏ½
β1
β1
)
Applying the identity
1
π
=
,
π/π β 1
πβπ
π uοΏ½
π loguοΏ½ uοΏ½+1
uοΏ½
π (π) =
β
π β
π β
(( uοΏ½ )
β 1)
π β π uοΏ½
π
π uοΏ½
π loguοΏ½ uοΏ½+1
π uοΏ½
uοΏ½
=
β
π
β
π
β
(
)
β
β
π β
πuοΏ½
uοΏ½
uοΏ½
uοΏ½
πβπ
π
πβπ
Proof of Master Theorem π β π π (2 of 2)
From rules of powers and logarithms:
π loguοΏ½ uοΏ½+1
π
π loguοΏ½ uοΏ½
π
πloguοΏ½ uοΏ½
( uοΏ½ )
= uοΏ½ β
( uοΏ½ )
= uοΏ½ β
π
π
π
π (π uοΏ½ )loguοΏ½ uοΏ½
Proof of Master Theorem π β π π (2 of 2)
From rules of powers and logarithms:
π loguοΏ½ uοΏ½+1
π
π loguοΏ½ uοΏ½
π
πloguοΏ½ uοΏ½
( uοΏ½ )
= uοΏ½ β
( uοΏ½ )
= uοΏ½ β
π
π
π
π (π uοΏ½ )loguοΏ½ uοΏ½
π πloguοΏ½ uοΏ½
πloguοΏ½ uοΏ½
= uοΏ½ β
= π β
uοΏ½ uοΏ½
π
πuοΏ½
π π
Proof of Master Theorem π β π π (2 of 2)
From rules of powers and logarithms:
π loguοΏ½ uοΏ½+1
π
π loguοΏ½ uοΏ½
π
πloguοΏ½ uοΏ½
( uοΏ½ )
= uοΏ½ β
( uοΏ½ )
= uοΏ½ β
π
π
π
π (π uοΏ½ )loguοΏ½ uοΏ½
π πloguοΏ½ uοΏ½
πloguοΏ½ uοΏ½
= uοΏ½ β
= π β
uοΏ½ uοΏ½
π
πuοΏ½
π π
Substituting in
π uοΏ½ πuοΏ½
π loguοΏ½ uοΏ½+1
π uοΏ½
π (π) =
β
π β
( uοΏ½ )
β
β
π β
πuοΏ½
π β π uοΏ½
π
π β π uοΏ½
Proof of Master Theorem π β π π (2 of 2)
From rules of powers and logarithms:
π loguοΏ½ uοΏ½+1
π
π loguοΏ½ uοΏ½
π
πloguοΏ½ uοΏ½
( uοΏ½ )
= uοΏ½ β
( uοΏ½ )
= uοΏ½ β
π
π
π
π (π uοΏ½ )loguοΏ½ uοΏ½
π πloguοΏ½ uοΏ½
πloguοΏ½ uοΏ½
= uοΏ½ β
= π β
uοΏ½ uοΏ½
π
πuοΏ½
π π
Substituting in
π uοΏ½ πuοΏ½
π loguοΏ½ uοΏ½+1
π uοΏ½
π (π) =
β
π β
( uοΏ½ )
β
β
π β
πuοΏ½
π β π uοΏ½
π
π β π uοΏ½
π
π uοΏ½
loguοΏ½ uοΏ½
=
β
πβ
π
β
β
π β
πuοΏ½
uοΏ½
uοΏ½
πβπ
πβπ
Branching versus subproblem size
π (π) =
π
π uοΏ½
loguοΏ½ uοΏ½
β
π
β
π
β
β
π β
πuοΏ½
uοΏ½
uοΏ½
πβπ
πβπ
Now we need to test π versus π uοΏ½ .
Branching versus subproblem size
π (π) =
π
π uοΏ½
loguοΏ½ uοΏ½
β
π
β
π
β
β
π β
πuοΏ½
uοΏ½
uοΏ½
πβπ
πβπ
Now we need to test π versus π uοΏ½ .
If π > π uοΏ½ (loguοΏ½ π > π), ο¬rst term dominates:
Ξ(πloguοΏ½ uοΏ½ ).
Branching versus subproblem size
π (π) =
π
π uοΏ½
loguοΏ½ uοΏ½
β
π
β
π
β
β
π β
πuοΏ½
uοΏ½
uοΏ½
πβπ
πβπ
Now we need to test π versus π uοΏ½ .
If π > π uοΏ½ (loguοΏ½ π > π), ο¬rst term dominates:
Ξ(πloguοΏ½ uοΏ½ ).
If π < π uοΏ½ (loguοΏ½ π < π), then
π uοΏ½
π
π (π) = uοΏ½
β
π β
πuοΏ½ β uοΏ½
β
π β
πloguοΏ½ uοΏ½
π βπ
π βπ
Branching versus subproblem size
π (π) =
π
π uοΏ½
loguοΏ½ uοΏ½
β
π
β
π
β
β
π β
πuοΏ½
uοΏ½
uοΏ½
πβπ
πβπ
Now we need to test π versus π uοΏ½ .
If π > π uοΏ½ (loguοΏ½ π > π), ο¬rst term dominates:
Ξ(πloguοΏ½ uοΏ½ ).
If π < π uοΏ½ (loguοΏ½ π < π), then
π uοΏ½
π
π (π) = uοΏ½
β
π β
πuοΏ½ β uοΏ½
β
π β
πloguοΏ½ uοΏ½
π βπ
π βπ
new ο¬rst term dominates: Ξ(πuοΏ½ ).
Sanity check: Merge sort
Master Theorem
Ξ(πuοΏ½ )
if π > πππuοΏ½ π
β§
{
uοΏ½
π (π) = β¨ Ξ(π log π) if π = πππuοΏ½ π
log uοΏ½
{
β© Ξ(π uοΏ½ ) if π < πππuοΏ½ π
Sanity check: Merge sort
Master Theorem
Ξ(πuοΏ½ )
if π > πππuοΏ½ π
β§
{
uοΏ½
π (π) = β¨ Ξ(π log π) if π = πππuοΏ½ π
log uοΏ½
{
β© Ξ(π uοΏ½ ) if π < πππuοΏ½ π
Merge Sort
βΆ
βΆ
βΆ
βΆ
π (π) = ππ (π/π ) + π(πuοΏ½ )
π how many recursive calls?
π what is the the split (denominator of size)
π degree
Contents
Divide and Conquer Continued
The Master Theorem
Matrix Multiplication
Matrix Multiplication
The product of two π × π matrices π₯ and π¦ is a
third π × π matrix π = ππ , with
uοΏ½
πuοΏ½uοΏ½ = β πuοΏ½uοΏ½ πuοΏ½uοΏ½
uοΏ½=1
where πuοΏ½uοΏ½ is the entry in row π and column π of
matrix π.
j
i
×
X
=
Y
(i, j )
Z
Calculating π directly using this formula takes
Ξ(π3 ) time.
Matrix Multiplication: Blocks
decompose the input matrices into four blocks each
(cutting the dimension π in half):
Matrix Multiplication: Blocks
decompose the input matrices into four blocks each
(cutting the dimension π in half):
π=[
π΄ π΅
],
πΆ π·
π =[
πΈ πΉ
]
πΊ π»
Matrix Multiplication: Blocks
decompose the input matrices into four blocks each
(cutting the dimension π in half):
π=[
π΄ π΅
],
πΆ π·
π =[
πΈ πΉ
]
πΊ π»
π΄ π΅
πΈ πΉ
][
]
πΆ π·
πΊ π»
π΄πΈ + π΅πΊ π΄πΉ + π΅π»
= [
]
πΆπΈ + π·πΊ πΆπΉ + π·π»
ππ = [
Matrix Multiplication: Blocks
decompose the input matrices into four blocks each
(cutting the dimension π in half):
π=[
π΄ π΅
],
πΆ π·
π =[
πΈ πΉ
]
πΊ π»
π΄ π΅
πΈ πΉ
][
]
πΆ π·
πΊ π»
π΄πΈ + π΅πΊ π΄πΉ + π΅π»
= [
]
πΆπΈ + π·πΊ πΆπΉ + π·π»
ππ = [
Eight subinstances
π΄πΈ, π΅πΊ, π΄πΉ , π΅π», πΆπΈ, π·πΊ, πΆπΉ , π·π»
Matrix Multiplication: Blocks
Recursing 8 times on subinstances of dimension uοΏ½2 ,
and taking ππ2 time to add the results, gives the
time recurrence:
π
π (π) = 8 β
π ( ) + ππ2
2
Matrix Multiplication: Blocks
Recursing 8 times on subinstances of dimension uοΏ½2 ,
and taking ππ2 time to add the results, gives the
time recurrence:
π
π (π) = 8 β
π ( ) + ππ2
2
Using the Master Theorem (and observing that
log2 8 = 3 > 2) shows that this is a
Ξ(πlog2 8 ) = Ξ(π3 ) algorithm.
Matrix Multiplication: Blocks
Recursing 8 times on subinstances of dimension uοΏ½2 ,
and taking ππ2 time to add the results, gives the
time recurrence:
π
π (π) = 8 β
π ( ) + ππ2
2
Using the Master Theorem (and observing that
log2 8 = 3 > 2) shows that this is a
Ξ(πlog2 8 ) = Ξ(π3 ) algorithm.
So, just as with integer multiplication, the most
direct way to split the instance does not produce an
improvement in the running time.
Matrix Multiplication: Blocks
Recursing 8 times on subinstances of dimension uοΏ½2 ,
and taking ππ2 time to add the results, gives the
time recurrence:
π
π (π) = 8 β
π ( ) + ππ2
2
Using the Master Theorem (and observing that
log2 8 = 3 > 2) shows that this is a
Ξ(πlog2 8 ) = Ξ(π3 ) algorithm.
So, just as with integer multiplication, the most
direct way to split the instance does not produce an
improvement in the running time.
(this is not technically βcubic algorithmβ, input size uοΏ½2 .)
Matrix Multiplication: Strassen
Decomposition
As with Integer Multiplication, we ο¬nd we need a
decomposition that reuses results.
Matrix Multiplication: Strassen
Decomposition
As with Integer Multiplication, we ο¬nd we need a
decomposition that reuses results. Strassen found
such a decomposition:
ππ = [
π5 + π4 β π2 + π6
π1 + π2
]
π3 + π4
π1 + π5 β π3 β π7
Matrix Multiplication: Strassen
Decomposition
As with Integer Multiplication, we ο¬nd we need a
decomposition that reuses results. Strassen found
such a decomposition:
ππ = [
π5 + π4 β π2 + π6
π1 + π2
]
π3 + π4
π1 + π5 β π3 β π7
where
π1
π2
π3
π4
= π΄(πΉ β π»)
= (π΄ + π΅)π»
= (πΆ + π·)πΈ
= π·(πΊ β πΈ)
π5 = (π΄ + π·)(πΈ + π»)
π6 = (π΅ β π·)(πΊ + π»)
π7 = (π΄ β πΆ)(πΈ + πΉ )
Matrix Multiplication: Strassen
Decomposition
This may not look like it would be an improvement
since the decomposition is complicated, but in
saving one recursive call, we get a time recurrence of
π
π (π) = 7 β
π ( ) + ππ2
2
Matrix Multiplication: Strassen
Decomposition
This may not look like it would be an improvement
since the decomposition is complicated, but in
saving one recursive call, we get a time recurrence of
π
π (π) = 7 β
π ( ) + ππ2
2
Using the Master Theorem (with
log2 7 > log2 4 = 2) shows that this is a Ξ(πlog2 7 )
algorithm, approximately Ξ(π2.81 ).
Matrix Multiplication: Strassen
Decomposition
This may not look like it would be an improvement
since the decomposition is complicated, but in
saving one recursive call, we get a time recurrence of
π
π (π) = 7 β
π ( ) + ππ2
2
Using the Master Theorem (with
log2 7 > log2 4 = 2) shows that this is a Ξ(πlog2 7 )
algorithm, approximately Ξ(π2.81 ).
Since the input size is π = π2 , the algorithm runs
in approximately Ξ(π1.404 ) time (versus the
Ξ(π1.5 ) of the original).
© Copyright 2026 Paperzz