改进Wagner-Fischer算法实现的技巧

时间:2017-01-31 16:11:20

标签: java algorithm performance optimization time-complexity

所以我正在为一个在线编程挑战网站开发一个Wagner-Fischer-Algorithm的实现,但我似乎无法把时间推到需要的地方。对于许多不同的“拼写错误”单词 w1,w2,...,wn 和字典 D 计算所有单词之间的编辑距离 > wi 和字典中的所有单词 di ,对于每个 wi ,输出字典中与无线即可。

目前这就是我实现算法的方式;

main():
  minDistance = 1000 // arbitrary large number
  Let D be the dictionary
  Let W be the set of words that needs ‘correcting’
  For every w in W:
    For every d in D:
      dist = distance(w, d)
      if dist < minDistance:
        minDistance = dist
        Make a linked list minList and add d to it
      if dist == minDistance:
        Add d to minList
    Output minDistance aswell as minList 

distance(w, d):
  Make a matrix M with dimensions (m,n)
  If the last d and this d has any p (start)-letters in common => use M(m, p) from last computation (no need to compute it again) 
  Fill the first row and the first column with their respective ‘index’ //Look at table on Wiki
  For col = 1 to m:
    For row = p to n:
      wagner-fischer(w, d, col, row)

wagner-fischer(w, d, col, row):
  res = M(col-1, row-1) + (1 if w have the same letter at index col-1 as d at row-1)
  addLetter = M(col-1, row) + 1
  deleteLetter = M(col, row-1) + 1
  if addLetter < res:
    res = addLetter
  if deleteLetter < res:
    res = deleteLetter
  return res

有没有人有关于如何进一步优化我的实施的任何提示?我现在真的很挣扎,我真的不知道如何进一步改进它。如果有任何重要的话,我已经用Java完成了。

0 个答案:

没有答案
相关问题