【发布时间】:2013-01-15 17:30:48
【问题描述】:
我整天都在研究Levenshtein Edit Distance 这个简单的python 实现。
def lev(a, b):
"""Recursively calculate the Levenshtein edit distance between two strings, a and b.
Returns the edit distance.
"""
if("" == a):
return len(b) # returns if a is an empty string
if("" == b):
return len(a) # returns if b is an empty string
return min(lev(a[:-1], b[:-1])+(a[-1] != b[-1]), lev(a[:-1], b)+1, lev(a, b[:-1])+1)
发件人:http://www.clear.rice.edu/comp130/12spring/editdist/
我知道它具有指数级复杂性,但我将如何从头开始计算该复杂性?
我一直在互联网上搜索,但没有找到任何解释,只有声明它是指数的。
谢谢。
【问题讨论】:
-
尝试绘制调用树。通常,这可以通过动态编程/记忆来解决。
-
我已经做到了,但我找不到节点数和指数增长之间的联系
-
这种幼稚的实现是指数型的,但算法并非必须如此。记忆化使这个多项式。
标签: python algorithm complexity-theory dynamic-programming levenshtein-distance