【发布时间】:2015-11-07 16:38:58
【问题描述】:
我是算法编程的新手。我知道当涉及到动态规划的序列比对时,它应该遵循以下算法:
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?谢谢!
【问题讨论】:
标签: algorithm dynamic-programming