0 ratings 0% found this document useful (0 votes) 14 views 7 pages Dynamic Programming
Dynamic Programming (DP) is an algorithm design technique that optimizes recursive problems by breaking them into smaller overlapping subproblems, solving each once, and storing results to avoid redundancy. It is particularly effective for problems with optimal substructure and overlapping subproblems, such as the Fibonacci sequence and the Knapsack problem. Key advantages of DP include reduced time complexity, efficient handling of large-scale problems, and guaranteed optimal solutions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here .
Available Formats
Download as PDF or read online on Scribd
Carousel Previous Carousel Next
Save Dynamic Programming For Later Prof: Mahmood Alam _- Dynamic Programming Algorithm Design Technique
Dynamic Programming Algorithm Design Technique
Dynamic Programming (DP) is an algorithm design technique used to solve
optimization problems by breaking them into smaller overlapping subproblems,
solving each subproblem once, and storing its result to avoid redundant
computations.
It is particularly useful for problems with optimal substructure and overlapping
subproblems.
Example Algorithms using Dynamic Programming
Fibonacci Sequence
Longest Common Subsequence (LCS)
Matrix Chain Multiplication
Coin Change Problem
Rod Cutting Problem
Bellman-Ford Algorithm for Shortest Path
Knapsack Problem
NAA YN
Advantages of Dynamic Programming (DP)
Dynamic Programming (DP) is a powerful algorithmic technique that optimizes recursive
problems by storing previously computed results. Here are its key advantages
1, Avoids Repeated Computations (Reduces Time Complexity)
+ DP stores previously computed results, eliminating redundant calculations.
+ Significantly reduces the time complexity of recursive solutions.
2. Efficiently Solves Overlapping Subproblems
+ DP is effective for problems where the same subprobiem is solved multiple times,
«+ Instead of recomputing, DP stores results in an array or table.
3. Ensures Optimal Solutions
DP guarantees an optimal solution if the problem has optimal substructure
4. Useful for Large-Scale Problems
BSCS 6th -2021-25 Page |1Prof: Mahmood Alam _- Dynamic Programming Algorithm Design Technique
+ Unlike brute-force approaches, DP is efficient for problems with large constraints.
+ Example: Matrix Chain Multiplication
© Brute Force: 0(2*n)
© Dynamic Programming: O(n*3)
Fibonacci Sequence Example Using Dynamic Programming
The Fibonacci sequence is defined as:
F(n)}=F(n=1)+F(n-2)
With base cases:
F(0)=0, F(I)=1
The Fibonacci sequence starts with:
+ Fib@)=0
i 1
Fib(1) + Fib(0) =1+0=1
+ Fib(@)= Fib(2) + Fib(1) =1+1=2
So, the values are:
+ Fib@)=2
BSCS 6th -2021-25 Page | 2Prof: Mahmood Alam_- Dynamic Programming Algorithm Design Technique
hy
ib(4) Fib(0)
Fib(2) Fib(O)
Recursive Approach (Without DP)
#include
using name: std;
int fib(int n) {
if (n <= 1) return n; // Base ca
return fib(n - 1) + fib(n - 2)7
int main() (
cout << fib(6); // Output: &
return 0;
)
Time Complexity: 0(2'n) (Exponential due to redundant calculations)
Optimized DP Approach (Memoization - Top-Down)
#include
using namespace stds
int
fib(int n)
int dp[n + 1];
dp [0] dpt1] = 1;
(int i = 2; i <= on; itt)
dpli] = dp(i - 1] + dp[i - 21;
return dp(a];
o4
cout << fib(6); // Output: 8
return 0;
Time Complexity: O(n)
Space Complexity: O(n)
ain Properties of Dyna Programming
1, Optimal Substructure Property:
© A problem has an optimal substructure if an optimal solution to the
problem can be constructed from optimal solutions of its
subproblems.
e Example: Shortest Path problems (Dijkstra’s, Floyd-Warshall)
2. Overlapping Subproblems Property:
© Aproblem has overlapping subproblems if the same smaller
subproblems are solved multiple times
Example: Fibonacci sequence (without DP, fi» (5) calls
fib (3), but fib (4) also calls fib (3), leading to redundant
calculations).
3. Memoization or Tabulation:
BSCS 6th -2021-25 Page |4Prof: Mahmood Alam _- Dynamic Programming Algorithm Design Technique
© Memoization (Top-Down): Memoization is used for recursively
solving problems while storing computed results.
© Tabulation (Bottom-Up): Tabulation is used for iteratively solving
problems from base cases to the final solution.
lements of Dynamic Programming (DP)
Dynamic Programming consists of key elements that help structure and optimize
problem-solving.
The main elements of Dynamic Programming are:
. Optimal Substructure
. Overlapping Subproblems
Weepe
State Definition
Recurrence Relation
Base Cases
These elements ensure an efficient problem-solving approach by avoiding
redundant computations and structuring problems in a stepwise manner.
1. Optimal Substructure
A problem exhibits optimal substructure if an optimal solution can be built using
optimal solutions to its sub problems.
Example: Fibonacci Sequence
The Fibonacci sequence follows optimal substructure because the solution to £ib(n) is
constructed from solutions to its smaller subproblems, ie., £ib(n-1) and £ib (n-2)
2. Overlapping Subproblems
A problem has overlapping subproblems if the same subproblem is solved
multiple times.
Instead of solving the same subproblem repeatedly, DP stores the results in a table.
+ Example (Fibonacci Numbers)
© Without DP: F(n)=F(n-1)+F(n-2)
« Causes repeated calculations of F (n-2),F(n-3), etc.
o With DP:
+ Store results in an array to avoid redundant calculations.
BSCS 6th -2021-25 Page |5Prof: Mahmood Alam _- Dynamic Programming Algorithm Design Technique
3. State Definition
The state represents the solution of'a subproblem. Defining the correct state is
crucial in DP formulation.
+ Example Example: In Fibonacci, api] stores F (1), the Fibonacci number
at index i
4, Recurrence Relation
A recurrence relation defines how a DP state depends on previous states. It helps
transition from smaller subproblems to larger ones.
+ Example (Fibonacci Sequence):
dplil-dpli-1] + dpli-2]
5. Base Cases
Base cases provide initial condi
states
ins for the DP approach, preventing undefined
+ Example (Fibonacci Numbers):
© F(0) =0
° F()
Difference Between Dynamic Programming (DP) and Divide and Conquer
+ DPis used when subproblems overlap and need to be solved multiple times —> Store
resuits to avoid recomputation.
+ Divide and Conquer is used when subproblems are independent — No need to store
previous results
BSCS 6th -2021-25 Page |6Prof: Mahmood Alam _- Dynamic Programming Algorithm Design Technique
Feature | __Dynamic Programming (DP) Divide and Conquer
ISches problems by breaking them into oe oe ‘y aiid
. ‘overlapping subproblems and storing Pel
Iconeept subproblems, solving each
resulls to avoid recomputation Peach col omtane
(mempization or tabulation). SEI, iB
[results.
[Overlapping Yes, DP’ wsed when subproblems repeat No, subproblems are independent
[Subproblems __|(¢.g.. Fibonacci, Knapsack, LCS) e.g. Merge Sort, Quick Sort).
loptimat Yes, DP ensures the solution to a problem Yes, Divide and Conquer abo
Ce atcture {8 built fiom optimal solutions of luses optimal substructure, but
isubproblenss. [without storing results.
7 Uses bottom-up (iterative) or top-down [Uses top-down recursive
[Approael (recursive with mempization) approach. approach.
frime (Generally Of) or O(w) when using [Ofien O(n log m) or OG")
[Complexity __|memoization or tabulation ldepending on recursion depth.
Space Uses extra memory (DP i earn be aiablen
|Complexity table/mempization) to store results. io eaindiaai ”
Examples Fibonacei, Matrix Chain Mukiplication, [Merge Sort, Quick Sort, Binary
Longest Common Subsequence (LCS).
[Seareh,
BSCS 6th -2021-25
Page |7