Introduction to Dynamic
Programming
•- A method for solving complex problems by
breaking them into simpler subproblems
• - Solves each subproblem only once and stores
the result
• - Avoids redundant calculations (uses
memoization or tabulation)
Steps to Solvea Problem Using DP
• 1. Identify if the problem has overlapping
subproblems
• 2. Define the DP state
• 3. Establish the recurrence relation
• 4. Decide memoization or tabulation
• 5. Implement the solution
5.
Memoization vs Tabulation
•Feature | Memoization |
Tabulation
• ----------------|----------------------------|--------------
---------------
• Approach | Top-down | Bottom-
up
• Recursive | Yes | No
• Storage | HashMap or Array |
Table/Array
• Call Stack | Uses function call stack |
6.
Classic Problems Solvedby DP
• - Fibonacci Numbers
• - 0/1 Knapsack Problem
• - Longest Common Subsequence (LCS)
• - Matrix Chain Multiplication
• - Coin Change Problem
• - Edit Distance
• - Rod Cutting Problem
7.
Example - Fibonacci(Memoization)
• fib(n):
• if n in memo:
• return memo[n]
• if n <= 1:
• return n
• memo[n] = fib(n-1) + fib(n-2)
• return memo[n]
8.
Example - 0/1Knapsack
(Tabulation)
• Problem: Maximize value in a knapsack of
capacity W
• DP Table: dp[i][w] = max value using first i
items with weight limit w
• Build table bottom-up
9.
Applications of Dynamic
Programming
•- Optimization problems
• - Game theory
• - Sequence alignment (bioinformatics)
• - Resource allocation
• - AI and machine learning (e.g., reinforcement
learning)
10.
Advantages and Disadvantages
•**Advantages:**
• - Efficient for problems with overlapping
subproblems
• - Guarantees optimal solutions
• **Disadvantages:**
• - Requires extra space for memoization/table
• - Not suitable for problems without optimal
substructure
11.
Tips for MasteringDP
• - Practice standard problems
• - Draw recursion trees
• - Understand base cases and transitions
• - Convert recursion to iteration
12.
Summary
• - DynamicProgramming = Divide + Memoize
• - Used for problems with optimal substructure
& overlapping subproblems
• - Two approaches: Memoization and
Tabulation