Week 9N
Week 9N
Week 9 - Revision
Dynamic programming
Dynamic programming Example
Grid paths
longest common sub word
longest common sub sequence
Edit distance
Matrix multiplication
Dynamic programming
Solution to original problem can be derived by combining solutions to subproblems
Memoization
Implementation
1 def LCW(s1,s2):
2 import numpy as np
3 (m,n) = (len(s1),len(s2))
4 lcw = np.zeros((m+1,n+1))
5 maxw = 0
6 for c in range(n-1,-1,-1):
7 for r in range(m-1,-1,-1):
8 if s1[r] == s2[c]:
9 lcw[r,c] = 1 + lcw[r+1,c+1]
10 else:
11 lcw[r,c] = 0
12 if lcw[r,c] > maxw:
13 maxw = lcw[r,c]
14 return maxw
15 s1 = input()
16 s2 = input()
17 print(LCW(s1,s2))
longest common sub sequence
Subsequence – can drop some letters in between
Subproblems are LCS(i, j), for 0 ≤ 𝑖 ≤ 𝑚, 0 ≤ 𝑗 ≤ 𝑛
Table of 𝑚 + 1 𝑛 + 1 values
Inductive structure
Complexity:
Implementation
1 def LCS(s1,s2):
2 import numpy as np
3 (m,n) = (len(s1),len(s2))
4 lcs = np.zeros((m+1,n+1))
5 for c in range(n-1,-1,-1):
6 for r in range(m-1,-1,-1):
7 if s1[r] == s2[c]:
8 lcs[r,c] = 1 + lcs[r+1,c+1]
9 else:
10 lcs[r,c] = max(lcs[r+1,c], lcs[r,c+1])
11 return lcs[0,0]
12 s1 = input()
13 s2 = input()
14 print(LCS(s1,s2))
Edit distance
Minimum number of editing operations needed to transform one document to the other
Subproblems are ED(i, j), for 0 ≤ 𝑖 ≤ 𝑚, 0 ≤ 𝑗 ≤ 𝑛
Table of 𝑚 + 1 𝑛 + 1 values ▪
Inductive structure
Complexity: 𝑂(𝑚𝑛)
Implementation
1 def ED(u,v):
2 import numpy as np
3 (m,n) = (len(u),len(v))
4 ed = np.zeros((m+1,n+1))
5 for i in range(m-1,-1,-1):
6 ed[i,n] = m-i
7 for j in range(n-1,-1,-1):
8 ed[m,j] = n-j
9 for j in range(n-1,-1,-1):
10 for i in range(m-1,-1,-1):
11 if u[i] == v[j]:
12 ed[i,j] = ed[i+1,j+1]
13 else:
14 ed[i,j] = 1 + min(ed[i+1,j+1], ed[i,j+1], ed[i+1,j])
15 return(ed[0,0])
Matrix multiplication
Matrix multiplication is associative
Bracketing does not change answer but can affect the complexity
Find an optimal order to compute the product
Compute C ( i, j), 0 ≤ 𝑖, 𝑗 < 𝑛, only for 𝑖 ≤ 𝑗
C ( i, j), depends on C ( i, k – 1) , C( i, k) for every 𝑖 < 𝑘 ≤ 𝑗
Diagonal entries are base case, fill matrix from main diagonal
Complexity:
Implementation
1 def C(dim):
2 n = dim.shape[0]
3 C = np.zeros((n,n))
4 for i in range(n):
5 C[i,i] = 0
6 for diff in range(1,n):
7 for i in range(0,n-diff):
8 j = i + diff
9 C[i,j] = C[i,i] + C[i+1,j] + dim[i][0] * dim[i+1][0] * dim[j][1]
10 for k in range(i+1, j+1):
11 C[i,j] = min(C[i,j],C[i,k-1] + C[k,j] + dim[i][0] * dim[k]
[0] * dim[j][1])
12 return(C[0,n-1])
13 import numpy as np
14 a = np.array([[2,3],[3,4],[4,5]])
15 print(C(a))