0% found this document useful (0 votes)
11 views9 pages

Week 9N

The document covers dynamic programming concepts, including examples such as grid paths, longest common subword, longest common subsequence, edit distance, and matrix multiplication. It explains the structure of subproblems, the use of memoization, and provides implementations for each example. The complexity of each algorithm is also discussed, emphasizing the efficiency gained through dynamic programming techniques.

Uploaded by

Harshdeep Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views9 pages

Week 9N

The document covers dynamic programming concepts, including examples such as grid paths, longest common subword, longest common subsequence, edit distance, and matrix multiplication. It explains the structure of subproblems, the use of memoization, and provides implementations for each example. The complexity of each algorithm is also discussed, emphasizing the efficiency gained through dynamic programming techniques.

Uploaded by

Harshdeep Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Week 9 - Revision

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

Examples: Factorial, Insertion sort, Fibonacci series

Anticipate the structure of subproblems

Derive from inductive definition

Solve subproblems in topological order

Memoization

Inductive solution generates same subproblem at different stages


Naïve recursive implementation evaluates each instance of subproblem from scratch
Build a table of values already computed – Memory table
Store each newly computed value in a table
Look up the table before making a recursive call

Dynamic programming Example


Grid paths
Rectangular grid of one-way roads

Can only go up and right


How many paths from (0, 0) to ?

Every path has segments


What if an intersection is blocked?

Need to discard paths passing through blocked intersection


Inductive structure

Fill the grid by row, column or diagonal


Complexity is using dynamic programming, using memorization

longest common sub word


Given two strings, find the (length of the) longest common sub word
Subproblems are LCW(i, j), for 0 ≤ 𝑖 ≤ 𝑚, 0 ≤ 𝑗 ≤ 𝑛
Table of 𝑚 + 1 𝑛 + 1 values
Inductive structure

Start at bottom right and fill row by row or column by column


Complexity:

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

Start at bottom right and fill row by row, column or diagonal

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

Start at bottom right and fill row, column or diagonal

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))

You might also like