2

Consider the recurrence $$ T(n,m) = T(n,m-1) + T(n-1,m) + c, $$ with base cases $T(n,0) = T(0,m) = 1$.

This is the complexity of a recursive algorithm for the longest common subsequence, I know that the complexity is exponential and equals $\Theta(2^i)$, but I'm not sure if $i=\max(n, m)$ or $i=nm$. Also, how can I demonstrate it using induction?

Yuval Filmus
  • 280,205
  • 27
  • 317
  • 514
Sydney.Ka
  • 69
  • 5

2 Answers2

6

The function $f(n,m) = \binom{n+m}{n}$ satisfies the recurrence $$ f(n,m) = f(n-1,m) + f(n,m-1), $$ with base cases $f(n,0) = f(0,m) = 1$. This already gives you the solution when $c = 0$, assuming that the base cases are $f(n,0) = f(0,m) = 1$.

One function which satisfies the recurrence, though (in general) not the base case, is $g(n,m) = -c$: $$ g(n,m) = g(n,m-1) + g(n-1,m) + c. $$ This gives the solution when $c = -1$.

Finally, note that any function of the form $\alpha f(n,m) + g(n,m)$ is also a solution of the recurrence. Since $$ \alpha f(n,0) + g(n,0) = \alpha f(0,m) + g(0,m) = \alpha - c, $$ we see that $$ T(n,m) = (c + 1) f(n,m) + g(n,m) = (c+1) \binom{n+m}{n} - c. $$

Yuval Filmus
  • 280,205
  • 27
  • 317
  • 514
0

Here's my take on it.

The general recursive formula is
T(n, m) = c1 + T(n-1, m) + T(n, m-1)
T(0, m) = c2, T(n, 0) = c3
c1, c2, c3 > 0

  1. First approach (a more loose constraint)
    To simplify, let's consider the longest branch in the recursion tree. It's of length n + m.
    So the height of the tree is n + m.
    A full tree (our tree is not a full tree) has this many nodes: 2^(heigh + 1) - 1 = 2^(n + m + 1) - 1 = O(2^(n + m)).
    There is constant work done for each node in the algorithm. So worst case time complexity is O(2^(n + m)).

  2. Second approach (a tighter constraint)
    Let k = max(n, m)
    T(n, m) <= T(k, k) = c1 + T(k-1, k) + T(k, k-1)
    Function T needs to be symmetric regarding its two variables. This forces T(k-1, k) == T(k, k-1). And we can (probably, not sure) rewrite the inequality in one variable
    T(n, m) <= P(k) = c1 + 2*P(k-1) = O(2^k) = O(2^max(n, m))

agshe
  • 1
  • 1