我是算法编程的新手。我知道当涉及动态编程的序列对齐时,它应该遵循以下算法:
Alg: Compute C[i, j]: min-cost to align (the first i symbols of X) and (the j symbols of Y)(C1:cost for mismatch,C2:cost for gap alignment)
and def d[i, j] = { C1 if X[i] ≠ Y[j],0 otherwise}
Compute C[i, j]:
case 1: align X[i] with Y[j]
C[i, j] = C[i-1, j-1] + d[i, j]
case 2: either X[i] or Y[j] is aligned to a gap
C[i, j] = min{ (C[i-1, j] + C2), (C[i, j-1] + C2) }
(C[i, j] = C[i-1, j] + C2 is case 2-1)
(C[i, j] = C[i-1, j] + C2 is case 2-2)
Alg: C[i, 0] = iC2, ∀i
C[0, j] = jC2, ∀j
for i = 1 to m
for j = 1 to n
C[i, j] = min{ (case 1), (case 2-1), (case 2-2) }
return C[m, n]
然而,最后一部分:
for i = 1 to m
for j = 1 to n
C[i, j] = min{ (case 1), (case 2-1), (case 2-2) }
return C[m, n]
我有点困惑,因为从前一部分它只是一个问题。为什么要一次又一次地在多个i中迭代j?谢谢!
答案 0 :(得分:0)
在这种情况下,C[i,j]
是memo
,它存储输入i
和j
的每个组合的计算值。您提到的嵌套循环只是遍历2-D矩阵中的每个单元格。实际上,相同的单元格从未计算过两次。