递归Levinshtein算法的下界

时间:2015-02-08 14:54:12

标签: algorithm recursion

Levinshtein距离算法的下限(Omega)在时间复杂度方面是什么?算法如下所述:

// len_s and len_t are the number of characters in string s and t respectively
int LevenshteinDistance(string s, int len_s, string t, int len_t)
{
  /* base case: empty strings */
  if (len_s == 0) return len_t;
  if (len_t == 0) return len_s;

  /* test if last characters of the strings match */
  if (s[len_s-1] == t[len_t-1])
      cost = 0;
  else
      cost = 1;

  /* return minimum of delete char from s, delete char from t, and delete char from both */
  return minimum(LevenshteinDistance(s, len_s - 1, t, len_t    ) + 1,
                 LevenshteinDistance(s, len_s    , t, len_t - 1) + 1,
   `             LevenshteinDistance(s, len_s - 1, t, len_t - 1) + cost));
}

我知道这已在这里得到解答:Complexity of edit distance (Levenshtein distance) recursion top down implementation。但我不明白Omega(2 ^(max(m,n)))是如何得出的?我通过某种规则,例子或数学推导来寻求推导。

1 个答案:

答案 0 :(得分:0)

对于两个第一次调用,递归深度限制在max(m,n)以下,因为它们每次减少一个字符串的长度;对于三次调用,递归深度以min(m,n)为界限,它们减少了两个字符串的长度(在最好的情况下)。这解释了2或3的权力。