优化Damerau-Levenshtein算法的时间复杂度达到比O(n*m)更好的水平

4

这里是算法(用Ruby编写)

#http://en.wikipedia.org/wiki/Damerau%E2%80%93Levenshtein_distance
  def self.dameraulevenshtein(seq1, seq2)
      oneago = nil
      thisrow = (1..seq2.size).to_a + [0]
      seq1.size.times do |x|
          twoago, oneago, thisrow = oneago, thisrow, [0] * seq2.size + [x + 1]
          seq2.size.times do |y|
              delcost = oneago[y] + 1
              addcost = thisrow[y - 1] + 1
              subcost = oneago[y - 1] + ((seq1[x] != seq2[y]) ? 1 : 0)
              thisrow[y] = [delcost, addcost, subcost].min
              if (x > 0 and y > 0 and seq1[x] == seq2[y-1] and seq1[x-1] == seq2[y] and seq1[x] != seq2[y])
                  thisrow[y] = [thisrow[y], twoago[y-2] + 1].min
              end
          end
      end
      return thisrow[seq2.size - 1]
  end

我的问题是,对于长度为780的seq1和长度为7238的seq2,在i7笔记本电脑上运行大约需要25秒。理想情况下,我希望将其缩短到约1秒,因为它作为Web应用程序的一部分运行。
我发现有一种方法可以优化vanilla levenshtein距离,使得运行时间从O(n*m)降至O(n + d^2),其中n是较长字符串的长度,d是编辑距离。因此,我的问题是,我所拥有的Damerau版本是否可以应用同样的优化?

你看过Levenshtein Automata了吗? - dbenhur
你需要知道确切的距离,还是只需要知道距离是否在某个阈值以下?前者比后者要难得多。 - Nick Johnson
1个回答

0

是的,优化可以应用于Damereau版本。这里有一个Haskell代码来实现这一点(我不知道Ruby):

distd :: Eq a => [a] -> [a] -> Int
distd a b
    = last (if lab == 0 then mainDiag
            else if lab > 0 then lowers !! (lab - 1)
                 else{- < 0 -}   uppers !! (-1 - lab))
    where mainDiag = oneDiag a b (head uppers) (-1 : head lowers)
          uppers = eachDiag a b (mainDiag : uppers) -- upper diagonals
          lowers = eachDiag b a (mainDiag : lowers) -- lower diagonals
          eachDiag a [] diags = []
          eachDiag a (bch:bs) (lastDiag:diags) = oneDiag a bs nextDiag lastDiag : eachDiag a bs diags
              where nextDiag = head (tail diags)
          oneDiag a b diagAbove diagBelow = thisdiag
              where doDiag [_] b nw n w = []
                    doDiag a [_] nw n w = []
                    doDiag (apr:ach:as) (bpr:bch:bs) nw n w = me : (doDiag (ach:as) (bch:bs) me (tail n) (tail w))
                        where me = if ach == bch then nw else if ach == bpr && bch == apr then nw else 1 + min3 (head w) nw (head n)
                    firstelt = 1 + head diagBelow
                    thisdiag = firstelt : doDiag a b firstelt diagAbove (tail diagBelow)
          lab = length a - length b
          min3 x y z = if x < y then x else min y z

distance :: [Char] -> [Char] -> Int
distance a b = distd ('0':a) ('0':b)

上面的代码是对这段代码的改编。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接