Dynamic Programming for Beginners. Part 1

Dynamic Programming has similarities with backtracking and divide-conquer in many respects.
Here is how we generally solve a problem using dynamic programming.Split the problem into overlapping sub-problems.Solve each sub-problem recursively.Combine the solutions to sub-problems into a solution for the given problem.Don’t compute the answer to the same problem more than once.Let us see some problems in which we can implement the above procedure.The Fibonacci Sequence (http://en.wikipedia.org/wiki/Fibonacci_number)Let us devise a program to calculate nth Fibonacci number.Let us solve this recursively.int fibo(int n)
return 1;

return fibo(n-2)+fibo(n-1);
}The complexity of the above program would be exponentialNow let are solve this problem dynamically.int memory[500]
memset(memory, -1 ,500)
int fibo(int n) {
return 1;

return memory[n];

int s=fibo(n-1)+fibo(n-2);
return s;
}We have n possible inputs to the function: 1, 2, …, n. Each input will either:–be computed, and the result savedbe returned from the memoryEach input will be computed at most once Time complexity is O(n × k), where k is the time complexity of computing an input if we assume that the recursive calls are returned directly from memory(O(1))Since we’re only doing constant amount of work tocompute the answer to an input, k = O(1)Total time complexity is O(n).

News Reporter

Leave a Reply

Your email address will not be published. Required fields are marked *