1 Basic and MatrixMultiplication
1 Basic and MatrixMultiplication
1 Basic and MatrixMultiplication
Running time?
• T(n) = T(n-1) + T(n-2) + O(1)
• T(n) ≥ T(n-1) + T(n-2) for n ≥ 2
• So T(n) grows at least as fast as
the Fibonacci numbers
themselves…
• This is EXPONENTIALLY QUICKLY!
IPython notebook
Why do the Fibonacci numbers grow exponentially
quickly?
• 𝑇 𝑛 =𝑇 𝑛−1 +𝑇 𝑛−2
• ≥ 2𝑇 𝑛 − 2
Trying solving this with the Back Substitution method
• 𝑇 𝑛 ≥ 2𝑇 𝑛 − 2
• ≥ 4𝑇 𝑛 − 4
• ≥ 8𝑇 𝑛 − 6
•… ≥ 2 k 𝑇 𝑛 − 2k for any k < 𝑛/2
•… ≥ 2𝑛/2𝑇 1 by plugging in k = 𝑛−1
2
• So 𝑻 𝒏 ≥ 𝟐𝒏/𝟐, which is REALLY BIG!!!
What’s going on? That’s a lot of
repeated
Consider Fib(8) computation!
6 7
4 5 5 6
2 3 3 4 3 4 4 5
0 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4
1 2 1 2 etc
0 1 0 1 0 1 1 2 0 1 0 1 0 1 1 2
0 1 0 1 0 1 1
Maybe this would be better:
8 def fasterFibonacci(n):
• F = [0, 1, None, None, …, None ]
• \\ F has length n + 1
• for i = 2, …, n:
7 • F[i] = F[i-1] + F[i-2]
• return F[n]
0
This was an example of…
What is dynamic programming?
• It is an algorithm design paradigm
• like divide-and-conquer is an algorithm design paradigm.
• Usually, it is for solving optimization problems
• eg, shortest path (Bellman Ford’s Algorithm)
• (Fibonacci numbers aren’t an optimization problem, but
they are a good example of DP anyway…)
Elements of dynamic programming
1. Optimal sub-structure:
• Big problems break up into sub-problems.
• Fibonacci: F(i) for i ≤ n
• Top down
• Bottom up
Bottom up approach
what we just saw.
• For Fibonacci:
• Solve the small problems first
• fill in F[0],F[1]
• Then bigger problems
• fill in F[2]
•…
• Then bigger problems
• fill in F[n-1]
• Then finally solve the real problem.
• fill in F[n]
Top down approach
• Think of it like a recursive algorithm.
• To solve the big problem:
• Recurse to solve smaller problems
• Those recurse to solve smaller problems
• etc..
6 7
4 5 5 6
2 3 3 4 3 4 4 5
0 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4
1 2 1 2 etc
0 1 0 1 0 1 1 2 0 1 0 1 0 1 1 2
0 1 0 1 0 1 1
30
9
Memo-ization Visualization 8
ctd
7
4
• define a global list F = [0,1,None, None, …, None]
3
• def Fibonacci(n):
• if F[n] != None: 2
• return F[n]
• else: 1
• F[n] = Fibonacci(n-1) + Fibonacci(n-2)
0
• return F[n] 40
What have we learned?
𝒄[𝒊, 𝒋] = 𝒂 𝒊, 𝒌 𝒃[𝒌, 𝒋]
𝒌=𝟏
For 1 ≤ 𝑖 ≤ 𝑝 and 1 ≤ 𝑗 ≤ 𝑟
Matrix Multiplication
• If 𝐴𝐵 is defined, 𝐵𝐴 may not be defined
𝑐[𝑖, 𝑗] = 𝑎 𝑖, 𝑘 𝑏[𝑘, 𝑗]
𝑘=1
For 1 ≤ 𝑖 ≤ 𝑝 and 1 ≤ 𝑗 ≤ 𝑟.
𝐴1 𝐴2 𝐴3 𝐴4 = 𝐴1 𝐴2 (𝐴3, 𝐴4)
= 𝐴1 (𝐴2(𝐴3𝐴4)) = 𝐴1((𝐴2 𝐴3)𝐴4)
= ((𝐴1 𝐴2)𝐴3)𝐴4 = (𝐴1 (𝐴2 𝐴3))𝐴4
Exhaustive search: Exponential
DP a better approach…
Steps for applying Dynamic Programming
• Step 1: Identify optimal substructure.
𝐴𝑖 … 𝑗 is a 𝑝𝑖 − 1 × 𝑝𝑗 matrix
Split into 2
substructures
𝑨𝒊, 𝑨 𝒊 + 𝟏, …, 𝑨𝒌 𝑨𝒌 + 𝟏, …, 𝑨𝒋
• Find the optimal solution for both substructures
• And then combine them to get the optimal solution of the
original structure
NOTE: Need to ensure the correct place (i.e., 𝒌) to split the product
Step 1: Optimal substructure
Split the original structure into substructures
Suppose 𝐴𝑖 , 𝐴 𝑖 + 1, …, 𝐴𝑗 is split between two substructures
around 𝑘
𝐴3 … 6 = (𝐴3(𝐴4 𝐴5))(𝐴6)
𝑘=5
𝑨𝟑 …𝟓
𝑨𝟔 … 𝟔
𝑘=3
𝑨𝟑 …𝟑
𝑨𝟒 … 𝟓
Step 1: Optimal substructure
Split the original structure into substructures
Suppose 𝐴𝑖 , 𝐴 𝑖 + 1, …, 𝐴𝑗 is split between two substructures
around 𝑘
Recursive Formulation:
𝟎 𝒊𝒇 𝒊 == 𝒋
𝒎 𝒊, 𝒋 = 𝒎𝒊𝒏 (𝒎 𝒊, 𝒌 + 𝒎 𝒌 + 𝟏, 𝒋 + 𝒑𝒊 − 𝟏 𝒑𝒌 𝒑𝒋)
𝒊≤𝒌<𝒋
𝒊𝒇 𝒊 < 𝒋
Steps for applying Dynamic Programming
• Step 1: Identify optimal substructure.
• Step 2: Find a recursive formulation for the matrix-
chain multiplication
CASE 2: 𝒊 < 𝒋
Steps for applying Dynamic Programming
• Step 1: Identify optimal substructure.
• Step 2: Find a recursive formulation for the
matrix-chain multiplication
• Step 3: Use dynamic programming to find the
minimum product in matrix-chain
multiplication.
𝐴2 𝐴3 = 4 × 6 × 2 = 48
𝐴1)( 𝐴2 𝐴3 = 5 × 4 × 2 = 40
𝐴1 𝐴2 𝐴3 𝐴4 = 5 × 2 × 7 = 70
Total Multiplication 𝐴1 𝐴2 𝐴3 𝐴4 = 48 + 40 + 70
= 158
Matrix-chain Multiplication
Example: Given a sequence of four matrices 𝐴1, 𝐴2, 𝐴3, 𝐴4
with 𝑝0 = 5, 𝑝1 = 4, 𝑝2 = 6, 𝑝3 = 2 and 𝑝4 = 7.
𝑖 𝑖
1 2 3 4 1 2 3
4 0 4
3 0 𝑗 3
𝑗
2 0 2
1 0
𝒎 𝒔
Matrix-chain Multiplication
𝑝0 = 5, 𝑝1 = 4, 𝑝2 = 6, 𝑝3 = 2 and 𝑝4 = 7
4 0 4
3 0 𝑗 3
𝑗
2 120 0 2 1
1 0
𝒎 𝒔
Matrix-chain Multiplication
𝑝0 = 5, 𝑝1 = 4, 𝑝2 = 6, 𝑝3 = 2 and 𝑝4 = 7.
4 0 4
3 48 0 𝑗 3 2
𝑗
2 120 0 2 1
1 0
𝒎 𝒔
Matrix-chain Multiplication
𝑝0 = 5, 𝑝1 = 4, 𝑝2 = 6, 𝑝3 = 2 and 𝑝4 = 7.
4 84 0 4 3
3 48 0 𝑗 3 2
𝑗
2 120 0 2 1
1 0
𝒎 𝒔
Matrix-chain Multiplication
𝑝 = 5, 𝑝 = 4, 𝑝 = 6, 𝑝 = 2 and 𝑝 = 7.
0 1 2 3 4
𝑚[1, 3] = 𝑚𝑖𝑛 (𝑚 1, 𝑘 + 𝑚 𝑘 + 1, 3 + 𝑝0 𝑝𝑘 𝑝3)
1≤𝑘<3
𝑚 1,1 + 𝑚 2, 3 + 𝑝0 𝑝1 𝑝3 = 88
= 𝑚𝑖𝑛 ቊ
𝑚 1,2 + 𝑚 3, 3 + 𝑝0 𝑝2 𝑝3 = 180
= 88
𝑖 𝑖
1 2 3 4 1 2 3
4 84 0 4 3
3 88 48 0 𝑗 3 1 2
𝑗
2 120 0 2 1
1 0
𝒎 𝒔
Matrix-chain Multiplication
𝑝 = 5, 𝑝 = 4, 𝑝 = 6, 𝑝 = 2 and 𝑝 = 7.
0 1 2 3 4
𝑚[2, 4] = 𝑚𝑖𝑛 (𝑚 2, 𝑘 + 𝑚 𝑘 + 1, 4 + 𝑝1 𝑝𝑘 𝑝4)
2≤𝑘<4
𝑚 2,2 + 𝑚 3 4 + 𝑝1 𝑝2 𝑝4 = 252
= 𝑚𝑖𝑛 ቊ
𝑚 2,3 + 𝑚 4, 4 + 𝑝1 𝑝3 𝑝4 = 104
= 104
𝑖 𝑖
1 2 3 4 1 2 3
4 104 84 0 4 3 3
3 88 48 0 𝑗 3 1 2
𝑗
2 120 0 2 1
1 0
𝒎 𝒔
Matrix-chain Multiplication
𝑝 = 5, 𝑝 = 4, 𝑝 = 6, 𝑝 = 2 and 𝑝 = 7.
0 1 2 3 4
𝑚[1, 4] = 𝑚𝑖𝑛 (𝑚 1, 𝑘 + 𝑚 𝑘 + 1, 4 + 𝑝0 𝑝𝑘 𝑝4)
1≤𝑘<4
𝑚 1,1 + 𝑚 2, 4 + 𝑝0 𝑝1 𝑝4 = 244
= 𝑚𝑖𝑛 ൞ 𝑚 1,2 + 𝑚 3, 4 + 𝑝0 𝑝2 𝑝4 = 294 = 158
𝑚 1,3 + 𝑚 4, 4 + 𝑝0 𝑝3 𝑝4 = 158
1 2 3 4 1 2 3
4 158 104 84 0 4 3 3 3
3 88 48 0 𝑗 3 1 2
𝑗
2 120 0 2 1
1 0
𝒎 𝒔
Steps for applying Dynamic Programming
Step 4: If needed, keep track of some additional info so that
the algorithm from Step 3 can find the actual parenthesis
multiplication from the auxiliary table 𝒔.
1 2 3
𝒔 𝟏, 𝟒 = 3 𝑨𝟏𝑨𝟐 𝑨𝟑 (𝑨𝟒) 4 3 3 3
𝑗 3 1 2
𝒔 𝟏, 𝟑 = 1 ( 𝑨𝟏)(𝑨𝟐 𝑨𝟑 )(𝑨𝟒)
2 1
Solution: 𝒔
Total Multiplication = 𝒎 𝟏, 𝟒 = 158
𝑨𝟏 𝑨𝟐 𝑨𝟑 𝑨𝟒
Steps for applying Dynamic Programming
Step 4: If needed, keep track of some additional info so that
the algorithm from Step 3 can find the actual parenthesis
multiplication from the auxiliary table 𝒔.
1 2 3
𝒔 𝟏, 𝟒 = 3 𝑨𝟏𝑨𝟐 𝑨𝟑 (𝑨𝟒) 4 3 2 3
𝑗 3 1 2
𝒔 𝟏, 𝟑 = 1 ( 𝑨𝟏)(𝑨𝟐 𝑨𝟑 )(𝑨𝟒)
2 1
𝑝0 = 5, 𝑝1 = 4, 𝑝2 = 6, 𝑝3 = 2 and 𝑝4 = 7 𝒔
𝑨𝟐 𝑨𝟑 = 𝟒 × 𝟔 × 𝟐 = 𝟒𝟖
𝑨𝟏)( 𝑨𝟐 𝑨𝟑 = 𝟓 × 𝟒 × 𝟐 = 𝟒𝟎
𝑨𝟏 𝑨𝟐 𝑨𝟑 𝑨𝟒 = 𝟓 × 𝟐 × 𝟕 = 𝟕𝟎
Total Multiplication = 48 + 40 + 70 = 158
Steps for applying Dynamic Programming
Step 4: If needed, keep track of some additional info so that
the algorithm from Step 3 can find the actual parenthesis
multiplication from the auxiliary table 𝒔.
PRINT-OPTIMAL-PARENS(𝑠, 𝑖, 𝑗)
1 if 𝑖 == j
2 print “𝐴” 𝑖
3 else print “(”
4 PRINT-OPTIMAL-PARENS(𝑠, 𝑖, 𝑠[𝑖, 𝑗])
5 PRINT-OPTIMAL-PARENS (𝑠, 𝑠 𝑖, 𝑗 + 1, 𝑗)
6 print “)”