Saturday, 6 May 2017

Dynamic Programming

Difference between Dynamic Programming and Divide and Conquer

The main difference between dynamic programming and divide and conquer is that in the case of the latter, sub problems are independent, whereas in DP there can be an overlap of sub problems.

GEEKSFORGEEKS

Dynamic Programming Strategy

By uing memoization [maintaining a table of sub problems already solved], dynamic programming reduces the exponential complexity to polynomial complexity (O( n2), O( n3), etc.) for many problems.

The major components of DP are:

  • Recursion: Solves sub problems recursively. 
  • Memoization: Stores already computed values in table (Memoization means caching). 

Dynamic Programming = Recursion + Memoization


Problems:

Books Problem

Problem1:  Convert the following recurrence to code.
See RecursiveSolution.cpp - Recursive hence time  complexity is more
DynamicSolution.cpp - Time Complexity O(n), Space Compexity O(n)


 

  •  Longest Common SubSequence - See Program













































No comments:

Post a Comment

Array

Program for Array Rotation 1st Method - Using temp array  - Time complexity - O(n), Space - O(d) See algo and code 2nd Method - rotate...