Dynamic Programming and
Applications
Design & Analysis of Algorithm
D&A of Algorithm - Best Group of College Gujrat 1
Dynamic Programming
Dynamic Programming is a general algorithm design technique for solving problems
defined by recurrences with overlapping subproblems
Invented by American mathematician Richard Bellman in the 1950s to solve
optimization problems and later assimilated by CS
“Programming” here means “planning”
Main idea:
set up a recurrence relating a solution to a more significant instance to solutions of
some smaller instances
solve smaller instances once
record solutions in a table
extract the solution to the initial instance from that table
D&A of Algorithm - Best Group of College Gujrat 2
Divide-and-Conquer Method
Divide-and-conquer method for algorithm design
Divide: If the input size is too large to deal with in a straightforward manner,
divide the problem into two or more disjoint subproblems.
Conquer: conquer recursively to solve the subproblems.
Combine: Take the solutions to the subproblems and “merge” these solutions
into a solution for the original problem.
D&A of Algorithm - Best Group of College Gujrat 3
Divide-and-Conquer - Example
Merge Sort
Array for merge sort
Divide the array
into smaller subparts
D&A of Algorithm - Best Group of College Gujrat 4
Divide-and-Conquer - Example
Combine the subparts
D&A of Algorithm - Best Group of College Gujrat 5
Dynamic Programming
Dynamic programming is a way of improving inefficient divide-and-conquer
algorithms.
By “inefficient”, we mean that the same recursive call is made over and over.
If the same subproblem is solved several times, we can use a table to store
the result of a subproblem the first time it is computed and thus never have
to recompute it again.
Dynamic programming is applicable when the subproblems are dependent,
that is when subproblems share sub-subproblems.
“Programming” refers to a tabular method
D&A of Algorithm - Best Group of College Gujrat 6
Difference between DP and Divide-and-
Conquer
Using Divide-and-Conquer to solve these problems is inefficient because the
same common subproblems have to be solved many times.
DP will solve each of them once and their answers are stored in a table for
future use.
D&A of Algorithm - Best Group of College Gujrat 7
Dynamic Programming vs. Recursion
and Divide & Conquer
D&A of Algorithm - Best Group of College Gujrat 8
Elements of Dynamic Programming
(DP)
DP is used to solve problems with the following characteristics:
Simple subproblems
We should be able to break the original problem into smaller subproblems that
have the same structure
Optimal substructure of the problems
The optimal solution to the problem contains optimal solutions to its subproblems.
Overlapping sub-problems
there exist some places where we solve the same subproblem more than once.
D&A of Algorithm - Best Group of College Gujrat 9
Steps to Designing a Dynamic Programming
Algorithm
1. Characterize optimal substructure
2. Recursively define the value of an optimal solution
3. Compute the value bottom up
4. (if needed) Construct an optimal solution
D&A of Algorithm - Best Group of College Gujrat 10
Principle of Optimality
The dynamic Programming works on a principle of optimality.
Principle of optimality states that in an optimal sequence of decisions or
choices, each sub sequences must also be optimal.
D&A of Algorithm - Best Group of College Gujrat 11
Dynamic Programming Algorithm
Example
we’re taking the Fibonacci number problem, and we’ll discuss how it can
be solved using the dynamic programming approach. The Fibonacci function
can be expressed mathematically as follows:
F(n) = F(n-1) + F(n-2)
With F(0) = 0 & F(1) = 1
Here,n is the user input. To illustrate how the Fibonacci function can be solved using the
dynamic programming approach, we’re taking the input value n = 5. In the first phase,
we divide the original problem into sub-problems. Unlike divide and conquer, it computes
the sub-problems iteratively and stores the solutions for future use.
Finally, we combine the solutions of the sub-problems to achieve the solution of the main
problem:
D&A of Algorithm - Best Group of College Gujrat 12
D&A of Algorithm - Best Group of College Gujrat 13
Advantages of Divide and Conquer
Algorithm
• The complexity for the multiplication of two matrices using the naive method
is O(n3), whereas using the divide and conquer approach (i.e. Strassen's
matrix multiplication) is O(n2.8074). This approach also simplifies other
problems, such as the Tower of Hanoi.
• This approach is suitable for multiprocessing systems.
• It makes efficient use of memory caches.
D&A of Algorithm - Best Group of College Gujrat 14
Dynamic Programming Applications
Longest Common Subsequence
Finding Shortest Path
Finding Maximum Profit with other Fixed Constraints
Job Scheduling in Processor
Bioinformatics
Optimal search solutions
D&A of Algorithm - Best Group of College Gujrat 15
Summary
• Dynamic Programming solves optimization problems by dividing them into smaller but similar
subproblems whose results are stored and used again.
• Dynamic Programming algorithm works by computing the answer to all possible subproblems
and storing them to find an optimal answer. This process of storing values of subproblems is
called memorization.
• A problem can be solved by using the Dynamic Programming algorithm if it follows these two
characteristics:
• Optimal Substructure
• Overlapping Subproblems
• The major components in any Dynamic Programming solution are:
• Stages
• States and state variables
• State Transition
• Optimal Choice
D&A of Algorithm - Best Group of College Gujrat 16
Summary
• Three elements of the Dynamic Programming algorithm are :
• Substructure
• Table Structure
• Bottom-Up Computation
• here are two types of approaches that can be used to solve a problem by
Dynamic Programming :
• Memorization or Top-Down Dynamic Programming
• Tabulation or Bottom Up Dynamic Programming
• Dynamic Programming finds its applications in different areas like
Bioinformatics, finding the shortest path, and many more.
D&A of Algorithm - Best Group of College Gujrat 17