Sub topic 9
Edit Distance and Matrix Chain Multiplication
Assume you know Big-O notation, arrays, recursion, and basic dynamic programming terms. Both problems use a table whose entries describe optimal answers to smaller subproblems.
Edit distance
The edit distance between two strings is the minimum number of single-character operations needed to change one string into the other. The allowed operations are insertion, deletion, and substitution. Assume each operation costs 1.
Let D[i][j] be the edit distance between the first i characters of a and the first j characters of b.
The base cases are:
D[i][0] = i, because allicharacters must be deleted.D[0][j] = j, because alljcharacters must be inserted.
If the last characters match, no new operation is needed:
D[i][j] = D[i-1][j-1]
Otherwise, choose the cheapest of:
- Delete from
a:D[i-1][j] + 1 - Insert into
a:D[i][j-1] + 1 - Substitute:
D[i-1][j-1] + 1
Thus, each cell depends on the cell above, the cell to the left, and the diagonal cell. Filling rows from top to bottom gives O(mn) time and O(mn) space for strings of lengths m and n.
def edit_distance(a, b):
previous = list(range(len(b) + 1))
for i, char_a in enumerate(a, start=1):
current = [i]
for j, char_b in enumerate(b, start=1):
substitution = previous[j - 1] + (char_a != char_b)
insertion = current[j - 1] + 1
deletion = previous[j] + 1
current.append(min(substitution, insertion, deletion))
previous = current
return previous[-1]
print(edit_distance("kitten", "sitting")) # 3The code stores only two rows, so its space usage is O(n), although reconstructing the actual sequence of edits requires additional information.
Matrix chain multiplication
Suppose matrices A1 A2 ... An must be multiplied in order. Matrix multiplication is associative, so different parenthesizations produce the same result. Their running times can differ greatly.
If matrix Ai has dimensions p[i-1] × p[i], let m[i][j] be the minimum scalar multiplications needed for Ai ... Aj.
The base case is m[i][i] = 0. For i < j:
m[i][j] = min(m[i][k] + m[k+1][j] + p[i-1]p[k]p[j])
Try every split k between i and j. Fill the table by increasing chain length. There are O(n²) cells, and each cell tries O(n) splits, so the time complexity is O(n³) and the space complexity is O(n²).
def matrix_chain_cost(dimensions):
n = len(dimensions) - 1
cost = [[0] * n for _ in range(n)]
for length in range(2, n + 1):
for i in range(n - length + 1):
j = i + length - 1
cost[i][j] = min(
cost[i][k] + cost[k + 1][j]
+ dimensions[i] * dimensions[k + 1] * dimensions[j + 1]
for k in range(i, j)
)
return cost[0][n - 1]
print(matrix_chain_cost([10, 30, 5, 60])) # 4500| Problem | Table state | Time | Space |
|---|---|---|---|
| Edit distance | Two string prefixes | O(mn) | O(mn), or O(n) for the value |
| Matrix chain multiplication | One matrix subchain | O(n³) | O(n²) |
Exercise
What are the edit distance between "abc" and "ac", and the minimum multiplication cost for matrices with dimensions [5, 10, 3]?
Show answer
The edit distance is 1 because "b" is deleted. The multiplication cost is 150 because a 5 × 10 matrix is multiplied by a 10 × 3 matrix.