Dynamic Programming
Dynamic Programming
1.INTRODUCTION
Simple Explanation
Dynamic Programming is a problem-solving technique that helps you solve complex problems
by breaking them down into smaller, simpler subproblems. It's like solving a big puzzle by
solving smaller pieces of it first.
Imagine you have a big task to complete, like climbing a tall mountain. Dynamic Programming
tells you to take it step by step. Instead of trying to jump to the top in one giant leap, you start at
the bottom and climb up one small step at a time.
The key idea in Dynamic Programming is to solve each subproblem only once, thus
reducing the number of computations: once the solution to a given subproblem has been
computed, it is stored, the next time the same solution is needed, it is simply looked up.
In computer science, Dynamic Programming is often used to optimize the efficiency of
algorithms by avoiding redundant work. It's particularly useful for solving problems that have
overlapping subproblems, where the same smaller problems are solved multiple times. By
storing and reusing the solutions, Dynamic Programming makes these problems much faster to
solve.
Principle of Optimality
It states that the optimal solution for a larger subproblem contains an optimal solution for a
smaller subproblem.
Optimal substructure means that the solution to a given optimization problem can be obtained by
the combination of optimal solutions to its sub problems.
Overlapping subproblems means that the space of subproblems must be small, that is, any
recursive algorithm solving the problem should solve the same subproblems over and over,
rather than generating new subproblems.
ALGORITHM FibonacciTabulation(n):
{
if n <=1:
return n;
else
F[0]=0 ; F[1]=1 ;
For ( int i=2 ; i<=n ; i++) {
F[I]= F[I-1]+F[I-2]
}
Return F[n]
}
Differences between Tabulation and Memoization
2.MATRIX CHAIN MULTIPLICATION
2.1. What's the Problem?
Imagine you have a bunch of matrices, and you want to multiply them together to get a final
result. In the world of computer graphics or scientific simulations, this happens a lot. But here's
the catch: The order in which you multiply these matrices can significantly affect the time it
takes to get the result.
Question:
If X = <A, B, C, B, D, A, B>
Y = <B, D, C, A, B, A>
Solution: