Reputation: 123
For the algorithm above, I can only figure out the runtime for
if n==0:
is 1 and the runtime for
rec_opt(n-1)
will be T(n-1). But I can't figure out the runtime for
rec_opt(p[n])
and why the recurrence has an exponential closed-form, O(2^n )
And furthermore, why this algorithm will be O(n).
Upvotes: 0
Views: 422
Reputation: 4502
rec_opt(p[n])
For recursion call rec_opt(p[n]), there will be another recursion tree which will act like rec_opt(n-1). As p[n]
could be any value from 1 - n
then we can assume that it will act like a rec_opt(n)
.
And furthermore, why this algorithm will be O(n).
On the second part as algorithm doing memoization, it will not calculate same sub-problem again and again. That's why the complexity drastically reduced to O(n)
.
For more please chech dynamic programming.
Upvotes: 1