Dynamic
Programming
Group 24
What is dynamic programming
❏ Is a computer programming technique where an algorithmic problem is first broken down
into sub-problems.
❏ Solves subproblems recursively and stores their solutions to avoid repeated calculations.
❏ It is used where we have problems, which can be divided into similar sub-problems, so
that their results can be reused.
❏ The key is to store the solutions of sub-problems to be used in the future
❏ The solutions of sub-problems are combined in order to achieve the best solution.
❏ Mostly, these algorithms are used for optimization.
❏ Dynamic algorithms use Memoization to remember the output of already solved sub-
problems.
What are sub-problems ?
Sub-problems are smaller versions of the original problem. Let’s see an example. With the
equation below:
Original problem: 1+2+3+4
We can break this down to:
Sub-problem 1: 1+2
Sub-problem 2: 3+4
● Once we solve these two smaller problems, we can add the solutions to these sub-
problems to find the solution to the overall problem.
● Notice how these sub-problems break down the original problem into components that
build up the solution.
Recursion in Dynamic Programming
F(0) = 0, F(1) = 1 AND
F(n) = F(n-1) + F(n -2)
Applications
The following computer problems can be solved using dynamic programming approach
● Fibonacci number series
● Knapsack problem
● Tower of Hanoi
● All pair shortest path by Floyd-Warshall
● Shortest path by Dijkstra
● Project scheduling
Comparison of Algorithmic Design
approaches
Greedy Divide and Conquer Backtracking Dynamic Programming
Used to solve Used to solve decision Used to solve
Decision problem used to
optimization problem problem optimization problem
find a feasible solution of
the problem.
Optimisation problem
used to find the best
solution that can be
applied.
Enumeration problem
used to find the set of all
feasible solutions of the
Comparison of Algorithmic Design
approaches
Greedy Divide and Conquer Backtracking Dynamic Programming
Examples: Examples: Merge sort, Examples: 0/1 Knapsack
Fractional Knapsack pro Quick sort, ,
blem Strassen’s matrix multip All pair shortest path,
, lication Matrix-chain multiplicati
. on
Activity selection proble
m .
It
, may or may not It is used to obtain a It always generates
generate an optimal
Job sequencing problem solution to the given optimal solution.
solution.
. problem, it does not aim
for the optimal solution
Comparison of Algorithmic Design
approaches
Greedy Divide and Conquer Backtracking Dynamic Programming
Follows Top-down Follows Top-down Follows bottom-up or
approach approach bottom-up approach
Iterative in nature. Recursive in nature. Recursive in nature.
Comparison of Algorithmic Design
approaches
Greedy Divide and Conquer Backtracking Dynamic Programming