dynamic programming practice
TRANSCRIPT
Dynamic programming practice
10/21/21
Recall: Matrix chain multiplication
• What is the optimal number of multiplications to combine a range of matrices?
(30, 35, 15, 5, 10, 20, 25)0 1 2 3 4 5 6
to
from1 2 3 4 5 6
1
2
3
4
5
6
Finding a subproblem
• What is the optimal number of multiplications to combine a range of matrices?
(30, 35, 15, 5, 10, 20, 25)0 1 2 3 4 5 6
to
from1 2 3 4 5 6
1
2
3
4
5
6
What goes in the colored cell?A. 0B. 10C. 200D. Cannot be determinedE. None of the above
Finding a subproblem
• What is the optimal number of multiplications to combine a range of matrices?
(30, 35, 15, 5, 10, 20, 25)0 1 2 3 4 5 6
to
from1 2 3 4 5 6
1
2
3
4
5
6
What goes in the colored cell?A. 0B. 10C. 200D. Cannot be determinedE. None of the above
Finding a subproblem
• What is the optimal number of multiplications to combine a range of matrices?
(30, 35, 15, 5, 10, 20, 25)0 1 2 3 4 5 6
0
0
0
0
0
0
to
from1 2 3 4 5 6
1
2
3
4
5
6
What goes in the colored cell?A. 0B. 50C. 750D. 1000E. None of the above
Finding a subproblem
• What is the optimal number of multiplications to combine a range of matrices?
(30, 35, 15, 5, 10, 20, 25)0 1 2 3 4 5 6
0
0
0
0
0
0
to
from1 2 3 4 5 6
1
2
3
4
5
6
What goes in the colored cell?A. 0B. 50C. 750D. 1000E. None of the above
Finding a subproblem
• What is the optimal number of multiplications to combine a range of matrices?
(30, 35, 15, 5, 10, 20, 25)0 1 2 3 4 5 6
05000
01000
750
2625 0
0
015750
0
to
from1 2 3 4 5 6
1
2
3
4
5
6
Finding a subproblem
• What is the optimal number of multiplications to combine a range of matrices?
(30, 35, 15, 5, 10, 20, 25)0 1 2 3 4 5 6
05000
01000
750
2625 0
0
015750
0
to
from1 2 3 4 5 6
1
2
3
4
5
6
What goes in the colored cell?A. 750B. 1000C. 2500D. 3750E. None of the above
Finding a subproblem
• What is the optimal number of multiplications to combine a range of matrices?
(30, 35, 15, 5, 10, 20, 25)0 1 2 3 4 5 6
05000
01000
750
2625 0
0
015750
0
to
from1 2 3 4 5 6
1
2
3
4
5
6
What goes in the colored cell?A. 750B. 1000C. 2500D. 3750E. None of the above
Matrix Chain multiplication
• What is the optimal number of multiplications to combine a range of matrices?
(30, 35, 15, 5, 10, 20, 25)0 1 2 3 4 5 61 2 3 4 5 6
15125 10500 5375 3500 05000
11875 7125
9375
2500 01000
4375
7875
750
2625 0
0
015750
0
to
from
1
2
3
4
5
6
Matrix Chain multiplication
• What is the optimal number of multiplications to combine a range of matrices?
(30, 35, 15, 5, 10, 20, 25)0 1 2 3 4 5 6
What is the running time of this algorithm?A. ⍬(n)B. ⍬(n2)C. ⍬(n2 log n)D. ⍬(n3)E. None of the above
1 2 3 4 5 615125 10500 5375 3500 05000
11875 7125
9375
2500 01000
4375
7875
750
2625 0
0
015750
0
to
from
1
2
3
4
5
6
Matrix Chain multiplication
• What is the optimal number of multiplications to combine a range of matrices?
(30, 35, 15, 5, 10, 20, 25)0 1 2 3 4 5 6
What is the running time of this algorithm?A. ⍬(n)B. ⍬(n2)C. ⍬(n2 log n)D. ⍬(n3)E. None of the above
1 2 3 4 5 615125 10500 5375 3500 05000
11875 7125
9375
2500 01000
4375
7875
750
2625 0
0
015750
0
to
from
1
2
3
4
5
6
Dynamic programming
• Identify subproblems– How do you use them to solve bigger problems?– How do you store their solutions?
Subtlety in optimal substructure
• Optimal substructure: An optimal solution contains the optimal solution to subproblems
Subtlety in optimal substructure
• Optimal substructure: An optimal solution contains the optimal solution to subproblems
• Unweighted shortest path: Given a graph, find a path from u to v with the fewest edges
u vw
Subtlety in optimal substructure
• Optimal substructure: An optimal solution contains the optimal solution to subproblems
• Unweighted longest simple path: Given a graph, find a path from u to v visits each vertex at most once each and has the most edges
uv
w
Longest common subsequence (LCS)
Given two sequences, what is the longest subsequence that they share? (Taking some values and preserving order)
B, D, C, A, B, A
A, B, C, B, D, A, B
What is the length of the LCS for the sequences below?
A, B, A, C, A, B, C, A, B, A, CC, A, B, C, C, B, C, B, A
A. 4B. 5C. 6D. 7E. None of the above
What is the length of the LCS for the sequences below?
A, B, A, C, A, B, C, A, B, A, CC, A, B, C, C, B, C, B, A
A. 4B. 5C. 6D. 7E. None of the above
Subproblem
A[i,j] = length of LCS using first i chars of 1st
sequence and first j chars of 2nd
𝜀 A B A C A B C A B A C𝜀CA
B
CC
B
CB
A
0 if i=0 or j=0A[i,j] = 1+A[i-1,j-1] if s[i]=t[j]
max{A[i-1,j], A[i,j-1]} o.w. {A[i,j] = length of LCS using i chars of s and j chars of t