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
  • 276,994
  • 27
  • 311
  • 503
Sydney.Ka
  • 69
  • 5

1 Answers1

4

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
  • 276,994
  • 27
  • 311
  • 503