I was reading an article on solving the problem of Longest Common Subsequence at geekforgeeks, where there are two solutions, one recursive, and another through DP by a 2-D array. The DP solution does it in O(NM) time, while the recursive one does it in O(2^N) time.
The main problem with the recursive solution is the occurrence of overlapping of subsequences, as given there. however, if I store each pair in a hash, so that the next time that value is required by a recursion of the function, it can directly fetch the value from the hash instead of recursing further. So how much will this addition improve the efficiency? Will it come to O(NM)?
And secondly, how come the recursive solution yields O(2^N) time? How to find out the complexity of recursive functions like this one, or the one to find Fibonacci sequence, etc?
Yes, using a hash will make it
O(NM). The process, in this case, is called memoization (yes, without ther). Just make sure you don’t use an actual hashmap container as provided by your language of choice, make it a simple matrix: if the value for the current pair is-1, compute it recursively, otherwise assume it is already computed and return it.As for your second question, you can either do it mathematically to get the best bound, or get "good enough" by drawing it on paper like your link does:
This should be enough to inductively suggest that it will be
O(2^n): the tree has heightn, and at each node, you have two recursive calls that will reduce the problem from sizento sizen - 1(which will beO(2^(n - 1)). So the sizenoriginal problem will beO(2^n).Note that it is not incorrect to say that fibonacci is
O(2^n), but you can get a tighter bound with other mathematical methods.