Master Theorem and Examples

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 find+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 find+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 find+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 find+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οΏ½ 𝑏 > 𝑑), first term dominates:
Θ(𝑛loguοΏ½ uοΏ½ ).
Branching versus subproblem size
𝑇 (𝑛) =
𝑏
𝑠uοΏ½
loguοΏ½ uοΏ½
β‹…
𝑐
β‹…
𝑛
βˆ’
β‹… 𝑐 β‹… 𝑛uοΏ½
uοΏ½
uοΏ½
π‘βˆ’π‘ 
π‘βˆ’π‘ 
Now we need to test 𝑏 versus 𝑠uοΏ½ .
If 𝑏 > 𝑠uοΏ½ (loguοΏ½ 𝑏 > 𝑑), first 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οΏ½ 𝑏 > 𝑑), first term dominates:
Θ(𝑛loguοΏ½ uοΏ½ ).
If 𝑏 < 𝑠uοΏ½ (loguοΏ½ 𝑏 < 𝑑), then
𝑠uοΏ½
𝑏
𝑇 (𝑛) = uοΏ½
β‹… 𝑐 β‹… 𝑛uοΏ½ βˆ’ uοΏ½
β‹… 𝑐 β‹… 𝑛loguοΏ½ uοΏ½
𝑠 βˆ’π‘
𝑠 βˆ’π‘
new first 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 find we need a
decomposition that reuses results.
Matrix Multiplication: Strassen
Decomposition
As with Integer Multiplication, we find 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 find 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).