SlideShare a Scribd company logo
Dynamic programming
Dr. G.L. Saini
Dynamic Programming
Dynamic programming approach is similar to divide and conquer in breaking down the problem into
smaller and yet smaller possible sub-problems. But unlike, divide and conquer, these sub-problems are
not solved independently. Rather, results of these smaller sub-problems are remembered and used for
similar or overlapping sub-problems.
Dynamic programming is used where we have problems, which can be divided into similar sub-
problems, so that their results can be re-used. Mostly, these algorithms are used for optimization.
Before solving the in-hand sub-problem, dynamic algorithm will try to examine the results of the
previously solved sub-problems. The solutions of sub-problems are combined in order to achieve the
best solution.
So we can say that −
● The problem should be able to be divided into smaller overlapping sub-problem.
● An optimum solution can be achieved by using an optimum solution of smaller sub-problems.
● Dynamic algorithms use Memoization.
Comparison
In contrast to greedy algorithms, where local optimization is addressed, dynamic
algorithms are motivated for an overall optimization of the problem.
In contrast to divide and conquer algorithms, where solutions are combined to achieve an
overall solution, dynamic algorithms use the output of a smaller sub-problem and then try
to optimize a bigger sub-problem. Dynamic algorithms use Memoization to remember the
output of already solved sub-problems.
Example
The following computer problems can be solved using dynamic programming approach −
● Shortest path by Bellman ford
● Knapsack problem
● All pair shortest path by Floyd-Warshall
● Matrix chain multiplication
● Fibonacci number series etc.
How does the dynamic programming approach work?
The following are the steps that the dynamic programming follows:
● It breaks down the complex problem into simpler subproblems.
● It finds the optimal solution to these sub-problems.
● It stores the results of subproblems (memoization). The process of storing the results of subproblems is known as memorization.
● It reuses them so that same sub-problem is calculated more than once.
● Finally, calculate the result of the complex problem.
The above five steps are the basic steps for dynamic programming. The dynamic programming is applicable that are having properties
such as:
Those problems that are having overlapping subproblems and optimal substructures. Here, optimal substructure means that the solution of
optimization problems can be obtained by simply combining the optimal solution of all the subproblems.
In the case of dynamic programming, the space complexity would be increased as we are storing the intermediate results, but the time
complexity would be decreased.
There are two approaches to dynamic programming:
● Top-down approach
● Bottom-up approach
Top-down approach(Recursive)(Memoization)
The top-down approach follows the memorization technique, while bottom-up approach follows the tabulation
method. Here memorization is equal to the sum of recursion and caching. Recursion means calling the function itself,
while caching means storing the intermediate results.
Advantages
● It is very easy to understand and implement.
● It solves the subproblems only when it is required.
● It is easy to debug.
Disadvantages
It uses the recursion technique that occupies more memory in the call stack. Sometimes when the recursion is too
deep, the stack overflow condition will occur.
It occupies more memory that degrades the overall performance.
Bottom-Up approach(Iterative)(Tabulation)
The bottom-up approach is also one of the techniques which can be used to implement the dynamic programming. It uses
the tabulation technique to implement the dynamic programming approach. It solves the same kind of problems but it
removes the recursion. If we remove the recursion, there is no stack overflow issue and no overhead of the recursive
functions. In this tabulation technique, we solve the problems and store the results in a matrix.
The bottom-up is the approach used to avoid the recursion, thus saving the memory space. The bottom-up is an algorithm
that starts from the beginning, whereas the recursive algorithm starts from the end and works backward. In the bottom-up
approach, we start from the base case to find the answer for the end. As we know, the base cases in the Fibonacci series are
0 and 1. Since the bottom approach starts from the base cases, so we will start from 0 and 1.
Key points
● We solve all the smaller sub-problems that will be needed to solve the larger sub-problems then move to the larger
problems using smaller sub-problems.
● We use for loop to iterate over the sub-problems.
● The bottom-up approach is also known as the tabulation or table filling method.
Top-Down Vs Bottom Up
Top-Down Approach (Memoization):
In the top-down approach, also known as memoization, we start with the final
solution and recursively break it down into smaller subproblems. To avoid
redundant calculations, we store the results of solved subproblems in a
memoization table.
Bottom-Up Approach (Tabulation):
In the bottom-up approach, also known as tabulation, we start with the smallest
subproblems and gradually build up to the final solution. We store the results of
solved subproblems in a table to avoid redundant calculations.
Fibonacci Numbers using Dynamic Programming
The recursive solution is more elegant:
function fib(n){
if (n < 0) return undefined;
if (n < 2) return n;
return fib(n-1) + fib(n-2)
}
but its time complexity is exponential, or O(2^n), which is not
ideal at all.
Simple recursive solution
Top-Down (Memoization)
This is a bottom-up approach. We start from the bottom, finding fib(0) and fib(1), add them together to
get fib(2) and so on until we reach fib(5).
function tabulatedFib(n)
{
if (n === 1 || n === 2){
return 1;
}
const fibNums = [0, 1, 1];
for (let i = 3; i <= n; i++){
fibNums[i] = fibNums[i-1] + fibNums[i-2];
}
return fibNums[n];
}
The time complexity of both the memoization and tabulation solutions are O(n) — time grows linearly
with the size of n, because we are calculating fib(4), fib(3), etc each one time.
Fibonacci Numbers using Dynamic Programming (Bottom Up)
Complexity Analysis
• The time complexity of the recursive solution is exponential – O(2^N) to be exact. This is
due to solving the same subproblems multiple times.
• For the top-down approach, we only solve each subproblem one time. Since each
subproblem takes a constant amount of time to solve, this gives us a time complexity of
O(N). However, since we need to keep an array of size N + 1 to save our intermediate
results, the space complexity for this algorithm is also O(N).
• In the bottom-up approach, we also solve each subproblem only once. So the time
complexity of the algorithm is also O(N). Since we only use two variables to track our
intermediate results, our space complexity is constant, O(1).
Bellman-Ford algorithm
Bellman-Ford algorithm
Initialize all the
distances
Bellman-Ford algorithm
iterate over all
edges/vertices
and apply update
rule
Bellman-Ford algorithm
Bellman-Ford algorithm
check for negative
cycles
Bellman-Ford algorithm
Negative cycles
A
B
C E
D
1
1
-10
5
10
3
What is the shortest path
from a to e?
The End
Ad

Recommended

DYNAMIC_________________________PROGRAMMING.pptx
DYNAMIC_________________________PROGRAMMING.pptx
nazmusshakib335
 
L21_L27_Unit_5_Dynamic_Programming Computer Science
L21_L27_Unit_5_Dynamic_Programming Computer Science
priyanshukumarbt23cs
 
ADA Unit 2.pptx
ADA Unit 2.pptx
AmanKumar879992
 
Computer science in Dynamic programming .pptx
Computer science in Dynamic programming .pptx
shorabi2127061
 
Dynamic pgmming
Dynamic pgmming
Dr. C.V. Suresh Babu
 
Elements of Dynamic Programming
Elements of Dynamic Programming
Vishwajeet Shabadi
 
dinosourrrrrrrrrrrrrrrrrrrrrr formula .pptx
dinosourrrrrrrrrrrrrrrrrrrrrr formula .pptx
ShohidulIslamSovon
 
Algorithm_Dynamic Programming
Algorithm_Dynamic Programming
Im Rafid
 
Dynamic Programing.pptx good for understanding
Dynamic Programing.pptx good for understanding
HUSNAINAHMAD39
 
Dynamic Programming.pptx
Dynamic Programming.pptx
MuktarHossain13
 
Dynamic programming
Dynamic programming
Jay Nagar
 
Dynamic Programming: Memoization, Introduction to ALgorithms
Dynamic Programming: Memoization, Introduction to ALgorithms
ZuhairZafar3
 
dynamic-programming unit 3 power point presentation
dynamic-programming unit 3 power point presentation
Shrinivasa6
 
Algorithm Design Technique
Algorithm Design Technique
Bharat Bhushan
 
Dynamic Programming
Dynamic Programming
Bharat Bhushan
 
Dynamicpgmming
Dynamicpgmming
Muhammad Wasif
 
dynamic programming complete by Mumtaz Ali (03154103173)
dynamic programming complete by Mumtaz Ali (03154103173)
Mumtaz Ali
 
Annotaed slides for dynamic programming algorithm
Annotaed slides for dynamic programming algorithm
johnathangamal27
 
What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...
What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...
Simplilearn
 
Dynamic Programming Algorithm CSI-504.pdf
Dynamic Programming Algorithm CSI-504.pdf
dinemma1
 
Dynamic programming class 16
Dynamic programming class 16
Kumar
 
W8L1 Introduction & Fibonacci Numbers part 1.pptx
W8L1 Introduction & Fibonacci Numbers part 1.pptx
sakibahmed181234
 
week 9 lec 15.pptx
week 9 lec 15.pptx
SulamanHassan
 
Module 2ppt.pptx divid and conquer method
Module 2ppt.pptx divid and conquer method
JyoReddy9
 
DynamicProgramming.pptx
DynamicProgramming.pptx
SaimaShaheen14
 
5617723.pptx
5617723.pptx
MatthewMhlongo
 
Disign and Analysis for algorithm in computer science and technology
Disign and Analysis for algorithm in computer science and technology
ritikkumarchaudhury7
 
Introduction to dynamic programming
Introduction to dynamic programming
Amisha Narsingani
 
OCS Group SG - HPHT Well Design and Operation - SN.pdf
OCS Group SG - HPHT Well Design and Operation - SN.pdf
Muanisa Waras
 
Engineering Mechanics Introduction and its Application
Engineering Mechanics Introduction and its Application
Sakthivel M
 

More Related Content

Similar to Dynamic programming in Design Analysis and Algorithms (20)

Dynamic Programing.pptx good for understanding
Dynamic Programing.pptx good for understanding
HUSNAINAHMAD39
 
Dynamic Programming.pptx
Dynamic Programming.pptx
MuktarHossain13
 
Dynamic programming
Dynamic programming
Jay Nagar
 
Dynamic Programming: Memoization, Introduction to ALgorithms
Dynamic Programming: Memoization, Introduction to ALgorithms
ZuhairZafar3
 
dynamic-programming unit 3 power point presentation
dynamic-programming unit 3 power point presentation
Shrinivasa6
 
Algorithm Design Technique
Algorithm Design Technique
Bharat Bhushan
 
Dynamic Programming
Dynamic Programming
Bharat Bhushan
 
Dynamicpgmming
Dynamicpgmming
Muhammad Wasif
 
dynamic programming complete by Mumtaz Ali (03154103173)
dynamic programming complete by Mumtaz Ali (03154103173)
Mumtaz Ali
 
Annotaed slides for dynamic programming algorithm
Annotaed slides for dynamic programming algorithm
johnathangamal27
 
What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...
What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...
Simplilearn
 
Dynamic Programming Algorithm CSI-504.pdf
Dynamic Programming Algorithm CSI-504.pdf
dinemma1
 
Dynamic programming class 16
Dynamic programming class 16
Kumar
 
W8L1 Introduction & Fibonacci Numbers part 1.pptx
W8L1 Introduction & Fibonacci Numbers part 1.pptx
sakibahmed181234
 
week 9 lec 15.pptx
week 9 lec 15.pptx
SulamanHassan
 
Module 2ppt.pptx divid and conquer method
Module 2ppt.pptx divid and conquer method
JyoReddy9
 
DynamicProgramming.pptx
DynamicProgramming.pptx
SaimaShaheen14
 
5617723.pptx
5617723.pptx
MatthewMhlongo
 
Disign and Analysis for algorithm in computer science and technology
Disign and Analysis for algorithm in computer science and technology
ritikkumarchaudhury7
 
Introduction to dynamic programming
Introduction to dynamic programming
Amisha Narsingani
 
Dynamic Programing.pptx good for understanding
Dynamic Programing.pptx good for understanding
HUSNAINAHMAD39
 
Dynamic Programming.pptx
Dynamic Programming.pptx
MuktarHossain13
 
Dynamic programming
Dynamic programming
Jay Nagar
 
Dynamic Programming: Memoization, Introduction to ALgorithms
Dynamic Programming: Memoization, Introduction to ALgorithms
ZuhairZafar3
 
dynamic-programming unit 3 power point presentation
dynamic-programming unit 3 power point presentation
Shrinivasa6
 
Algorithm Design Technique
Algorithm Design Technique
Bharat Bhushan
 
dynamic programming complete by Mumtaz Ali (03154103173)
dynamic programming complete by Mumtaz Ali (03154103173)
Mumtaz Ali
 
Annotaed slides for dynamic programming algorithm
Annotaed slides for dynamic programming algorithm
johnathangamal27
 
What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...
What Is Dynamic Programming? | Dynamic Programming Explained | Programming Fo...
Simplilearn
 
Dynamic Programming Algorithm CSI-504.pdf
Dynamic Programming Algorithm CSI-504.pdf
dinemma1
 
Dynamic programming class 16
Dynamic programming class 16
Kumar
 
W8L1 Introduction & Fibonacci Numbers part 1.pptx
W8L1 Introduction & Fibonacci Numbers part 1.pptx
sakibahmed181234
 
Module 2ppt.pptx divid and conquer method
Module 2ppt.pptx divid and conquer method
JyoReddy9
 
DynamicProgramming.pptx
DynamicProgramming.pptx
SaimaShaheen14
 
Disign and Analysis for algorithm in computer science and technology
Disign and Analysis for algorithm in computer science and technology
ritikkumarchaudhury7
 
Introduction to dynamic programming
Introduction to dynamic programming
Amisha Narsingani
 

Recently uploaded (20)

OCS Group SG - HPHT Well Design and Operation - SN.pdf
OCS Group SG - HPHT Well Design and Operation - SN.pdf
Muanisa Waras
 
Engineering Mechanics Introduction and its Application
Engineering Mechanics Introduction and its Application
Sakthivel M
 
Machine Learning - Classification Algorithms
Machine Learning - Classification Algorithms
resming1
 
最新版美国圣莫尼卡学院毕业证(SMC毕业证书)原版定制
最新版美国圣莫尼卡学院毕业证(SMC毕业证书)原版定制
Taqyea
 
NALCO Green Anode Plant,Compositions of CPC,Pitch
NALCO Green Anode Plant,Compositions of CPC,Pitch
arpitprachi123
 
chemistry investigatory project for class 12
chemistry investigatory project for class 12
Susis10
 
Modern multi-proposer consensus implementations
Modern multi-proposer consensus implementations
François Garillot
 
3. What is the principles of Teamwork_Module_V1.0.ppt
3. What is the principles of Teamwork_Module_V1.0.ppt
engaash9
 
Industry 4.o the fourth revolutionWeek-2.pptx
Industry 4.o the fourth revolutionWeek-2.pptx
KNaveenKumarECE
 
Structural Design for Residential-to-Restaurant Conversion
Structural Design for Residential-to-Restaurant Conversion
DanielRoman285499
 
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
SharinAbGhani1
 
社内勉強会資料_Chain of Thought .
社内勉強会資料_Chain of Thought .
NABLAS株式会社
 
Water demand - Types , variations and WDS
Water demand - Types , variations and WDS
dhanashree78
 
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
aniket862935
 
Impurities of Water and their Significance.pptx
Impurities of Water and their Significance.pptx
dhanashree78
 
Cadastral Maps
Cadastral Maps
Google
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
IntroSlides-June-GDG-Cloud-Munich community [email protected]
IntroSlides-June-GDG-Cloud-Munich community [email protected]
Luiz Carneiro
 
Fundamentals of Digital Design_Class_21st May - Copy.pptx
Fundamentals of Digital Design_Class_21st May - Copy.pptx
drdebarshi1993
 
The basics of hydrogenation of co2 reaction
The basics of hydrogenation of co2 reaction
kumarrahul230759
 
OCS Group SG - HPHT Well Design and Operation - SN.pdf
OCS Group SG - HPHT Well Design and Operation - SN.pdf
Muanisa Waras
 
Engineering Mechanics Introduction and its Application
Engineering Mechanics Introduction and its Application
Sakthivel M
 
Machine Learning - Classification Algorithms
Machine Learning - Classification Algorithms
resming1
 
最新版美国圣莫尼卡学院毕业证(SMC毕业证书)原版定制
最新版美国圣莫尼卡学院毕业证(SMC毕业证书)原版定制
Taqyea
 
NALCO Green Anode Plant,Compositions of CPC,Pitch
NALCO Green Anode Plant,Compositions of CPC,Pitch
arpitprachi123
 
chemistry investigatory project for class 12
chemistry investigatory project for class 12
Susis10
 
Modern multi-proposer consensus implementations
Modern multi-proposer consensus implementations
François Garillot
 
3. What is the principles of Teamwork_Module_V1.0.ppt
3. What is the principles of Teamwork_Module_V1.0.ppt
engaash9
 
Industry 4.o the fourth revolutionWeek-2.pptx
Industry 4.o the fourth revolutionWeek-2.pptx
KNaveenKumarECE
 
Structural Design for Residential-to-Restaurant Conversion
Structural Design for Residential-to-Restaurant Conversion
DanielRoman285499
 
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
SharinAbGhani1
 
社内勉強会資料_Chain of Thought .
社内勉強会資料_Chain of Thought .
NABLAS株式会社
 
Water demand - Types , variations and WDS
Water demand - Types , variations and WDS
dhanashree78
 
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
aniket862935
 
Impurities of Water and their Significance.pptx
Impurities of Water and their Significance.pptx
dhanashree78
 
Cadastral Maps
Cadastral Maps
Google
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
Fundamentals of Digital Design_Class_21st May - Copy.pptx
Fundamentals of Digital Design_Class_21st May - Copy.pptx
drdebarshi1993
 
The basics of hydrogenation of co2 reaction
The basics of hydrogenation of co2 reaction
kumarrahul230759
 
Ad

Dynamic programming in Design Analysis and Algorithms

  • 2. Dynamic Programming Dynamic programming approach is similar to divide and conquer in breaking down the problem into smaller and yet smaller possible sub-problems. But unlike, divide and conquer, these sub-problems are not solved independently. Rather, results of these smaller sub-problems are remembered and used for similar or overlapping sub-problems. Dynamic programming is used where we have problems, which can be divided into similar sub- problems, so that their results can be re-used. Mostly, these algorithms are used for optimization. Before solving the in-hand sub-problem, dynamic algorithm will try to examine the results of the previously solved sub-problems. The solutions of sub-problems are combined in order to achieve the best solution. So we can say that − ● The problem should be able to be divided into smaller overlapping sub-problem. ● An optimum solution can be achieved by using an optimum solution of smaller sub-problems. ● Dynamic algorithms use Memoization.
  • 3. Comparison In contrast to greedy algorithms, where local optimization is addressed, dynamic algorithms are motivated for an overall optimization of the problem. In contrast to divide and conquer algorithms, where solutions are combined to achieve an overall solution, dynamic algorithms use the output of a smaller sub-problem and then try to optimize a bigger sub-problem. Dynamic algorithms use Memoization to remember the output of already solved sub-problems. Example The following computer problems can be solved using dynamic programming approach − ● Shortest path by Bellman ford ● Knapsack problem ● All pair shortest path by Floyd-Warshall ● Matrix chain multiplication ● Fibonacci number series etc.
  • 4. How does the dynamic programming approach work? The following are the steps that the dynamic programming follows: ● It breaks down the complex problem into simpler subproblems. ● It finds the optimal solution to these sub-problems. ● It stores the results of subproblems (memoization). The process of storing the results of subproblems is known as memorization. ● It reuses them so that same sub-problem is calculated more than once. ● Finally, calculate the result of the complex problem. The above five steps are the basic steps for dynamic programming. The dynamic programming is applicable that are having properties such as: Those problems that are having overlapping subproblems and optimal substructures. Here, optimal substructure means that the solution of optimization problems can be obtained by simply combining the optimal solution of all the subproblems. In the case of dynamic programming, the space complexity would be increased as we are storing the intermediate results, but the time complexity would be decreased. There are two approaches to dynamic programming: ● Top-down approach ● Bottom-up approach
  • 5. Top-down approach(Recursive)(Memoization) The top-down approach follows the memorization technique, while bottom-up approach follows the tabulation method. Here memorization is equal to the sum of recursion and caching. Recursion means calling the function itself, while caching means storing the intermediate results. Advantages ● It is very easy to understand and implement. ● It solves the subproblems only when it is required. ● It is easy to debug. Disadvantages It uses the recursion technique that occupies more memory in the call stack. Sometimes when the recursion is too deep, the stack overflow condition will occur. It occupies more memory that degrades the overall performance.
  • 6. Bottom-Up approach(Iterative)(Tabulation) The bottom-up approach is also one of the techniques which can be used to implement the dynamic programming. It uses the tabulation technique to implement the dynamic programming approach. It solves the same kind of problems but it removes the recursion. If we remove the recursion, there is no stack overflow issue and no overhead of the recursive functions. In this tabulation technique, we solve the problems and store the results in a matrix. The bottom-up is the approach used to avoid the recursion, thus saving the memory space. The bottom-up is an algorithm that starts from the beginning, whereas the recursive algorithm starts from the end and works backward. In the bottom-up approach, we start from the base case to find the answer for the end. As we know, the base cases in the Fibonacci series are 0 and 1. Since the bottom approach starts from the base cases, so we will start from 0 and 1. Key points ● We solve all the smaller sub-problems that will be needed to solve the larger sub-problems then move to the larger problems using smaller sub-problems. ● We use for loop to iterate over the sub-problems. ● The bottom-up approach is also known as the tabulation or table filling method.
  • 7. Top-Down Vs Bottom Up Top-Down Approach (Memoization): In the top-down approach, also known as memoization, we start with the final solution and recursively break it down into smaller subproblems. To avoid redundant calculations, we store the results of solved subproblems in a memoization table. Bottom-Up Approach (Tabulation): In the bottom-up approach, also known as tabulation, we start with the smallest subproblems and gradually build up to the final solution. We store the results of solved subproblems in a table to avoid redundant calculations.
  • 8. Fibonacci Numbers using Dynamic Programming The recursive solution is more elegant: function fib(n){ if (n < 0) return undefined; if (n < 2) return n; return fib(n-1) + fib(n-2) } but its time complexity is exponential, or O(2^n), which is not ideal at all.
  • 11. This is a bottom-up approach. We start from the bottom, finding fib(0) and fib(1), add them together to get fib(2) and so on until we reach fib(5). function tabulatedFib(n) { if (n === 1 || n === 2){ return 1; } const fibNums = [0, 1, 1]; for (let i = 3; i <= n; i++){ fibNums[i] = fibNums[i-1] + fibNums[i-2]; } return fibNums[n]; } The time complexity of both the memoization and tabulation solutions are O(n) — time grows linearly with the size of n, because we are calculating fib(4), fib(3), etc each one time. Fibonacci Numbers using Dynamic Programming (Bottom Up)
  • 12. Complexity Analysis • The time complexity of the recursive solution is exponential – O(2^N) to be exact. This is due to solving the same subproblems multiple times. • For the top-down approach, we only solve each subproblem one time. Since each subproblem takes a constant amount of time to solve, this gives us a time complexity of O(N). However, since we need to keep an array of size N + 1 to save our intermediate results, the space complexity for this algorithm is also O(N). • In the bottom-up approach, we also solve each subproblem only once. So the time complexity of the algorithm is also O(N). Since we only use two variables to track our intermediate results, our space complexity is constant, O(1).
  • 16. iterate over all edges/vertices and apply update rule Bellman-Ford algorithm
  • 19. Negative cycles A B C E D 1 1 -10 5 10 3 What is the shortest path from a to e?