SlideShare a Scribd company logo
WHY DYNAMIC PROGRAMMING??
In Divide & Conqure ,
1) We divide the sub-problem into random instances.
2) Solve each instance randomly.
3) Combine all the solutions to get the final solution.
Now ,there may arise two issues
1) Sub-problems are not independent to solve them
separately. AND/OR
2) We may have to calculate some part of solution
again and again.
SO WHAT’S THERE IN DP?
1) Solution First issue (independence)
• DP allows us to have inter-dependant sub-problems.
• Rather its good to have inter-related sub-instances so
that we can reuse them.
2) Solution to second issue (re-computation)
• Previously computed sub-solutions are used to calculate
further sub-solutions.
• So re-computation is avoided and each time we
calculate next value, We have its required sub-values
calculated.
HOW ITS DONE ?
 Sequence of actions : : ->
1) Divide the input at each possible split point.
2) Compute the solution to smallest instance.
3) STORE THE RESULT. ********
4) Using previously stored result calculate the next
result. (AND STORE IT……!!!)
5) Keep doing this till you reach the final solution.
SO,
1) We never compute the already
computed solution.
2)We always have required result
with us.
EXAMPLES
1) Solving the fibonacci sequence for n=1,000,002.
• This will be a very long process, but what if I
give you the results for n=1,000,000 and
n=1,000,0001? Suddenly the problem just
becomes more manageable.
2)string problems like string edit problem.
You solve a subset(s) of the problem and then use that
information to solve the more difficult original
problem.
FIBONACCI WITHOUT DP
int fib(int n)
{
if (n <= 1)
return n;//Return 0 for n=0 and 1 for n=1
return fib(n-1) + fib(n-2);//Else return Fn-1 + Fn-2
}
• Time complexity : T(n-1) + T(n-2)
Which is exponential…..!
The worst possible Fibonacci solution:
FIBONACCI WITH DP
int fib(int n)
{
/* Declare an array to store fibonacci numbers. */
int f[n+1];
int i;
/* 0th and 1st number of the series are 0 and 1*/
f[0] = 0;
f[1] = 1;
for (i = 2; i <= n; i++)
{
/* Add the previous 2 numbers in the series
and store it */
f[i] = f[i-1] + f[i-2];
}
return f[n];
}
TIME AND SPACE WITH DP
• Time Complexity: O(n)
• Extra Space: O(n)
V I S H N U D A H A T O N D E
PRESENTED BY

More Related Content

PPTX
Hill climbing algorithm
PPTX
Problem solving agents
PDF
Regular expression in javascript
PDF
Dynamic programming
PDF
JavaScript - Chapter 13 - Browser Object Model(BOM)
PPT
Artificial Intelligence 1 Planning In The Real World
PPT
Greedy Algorihm
PPTX
Bruteforce algorithm
Hill climbing algorithm
Problem solving agents
Regular expression in javascript
Dynamic programming
JavaScript - Chapter 13 - Browser Object Model(BOM)
Artificial Intelligence 1 Planning In The Real World
Greedy Algorihm
Bruteforce algorithm

What's hot (20)

PPT
Dynamic pgmming
PDF
JavaScript Looping Statements
PDF
Python programming : Classes objects
PPTX
Greedy algorithm
PPTX
daa-unit-3-greedy method
PDF
JavaScript - Chapter 6 - Basic Functions
PPT
Perception
PPT
SQLITE Android
PPTX
AI: AI & Problem Solving
PPTX
Event In JavaScript
PDF
Callback Function
PPTX
Local search algorithms
PPTX
PPTX
Object Oriented Programming In JavaScript
PPTX
String matching algorithms
PDF
Basics of JavaScript
PPTX
Demand Paging in OS (Operating System): Example, Advantages, Working
PPTX
Local search algorithm
PPT
Class 5 - PHP Strings
PDF
Ai lecture 06(unit-02)
Dynamic pgmming
JavaScript Looping Statements
Python programming : Classes objects
Greedy algorithm
daa-unit-3-greedy method
JavaScript - Chapter 6 - Basic Functions
Perception
SQLITE Android
AI: AI & Problem Solving
Event In JavaScript
Callback Function
Local search algorithms
Object Oriented Programming In JavaScript
String matching algorithms
Basics of JavaScript
Demand Paging in OS (Operating System): Example, Advantages, Working
Local search algorithm
Class 5 - PHP Strings
Ai lecture 06(unit-02)
Ad

Similar to Dynamic programming Basics (20)

PDF
Dynamic Programming Algorithm CSI-504.pdf
PDF
Dynamic programming
PPTX
Computer science in Dynamic programming .pptx
PPTX
Dynamic Programing.pptx good for understanding
PPTX
Dynamic Programming: Optimizing Solutions
PPTX
Annotaed slides for dynamic programming algorithm
PDF
Dynamic programming
PPTX
W8L1 Introduction & Fibonacci Numbers part 1.pptx
PPTX
Dynamic programming
PPTX
dinosourrrrrrrrrrrrrrrrrrrrrr formula .pptx
PPTX
Algorithm in computer science
PPTX
Dynamic programming class 16
PPTX
dynamic programming complete by Mumtaz Ali (03154103173)
PPTX
Dynamic programming prasintation eaisy
PPTX
Dynamic_Programming_Presentation in Design and Analysis of Algorithm
PDF
algorithm_6dynamic_programming.pdf
PPTX
Module 2ppt.pptx divid and conquer method
PPTX
Data Structure and Algorithm - Divide and Conquer
PDF
PDF
Dynamic Programming: Memoization, Introduction to ALgorithms
Dynamic Programming Algorithm CSI-504.pdf
Dynamic programming
Computer science in Dynamic programming .pptx
Dynamic Programing.pptx good for understanding
Dynamic Programming: Optimizing Solutions
Annotaed slides for dynamic programming algorithm
Dynamic programming
W8L1 Introduction & Fibonacci Numbers part 1.pptx
Dynamic programming
dinosourrrrrrrrrrrrrrrrrrrrrr formula .pptx
Algorithm in computer science
Dynamic programming class 16
dynamic programming complete by Mumtaz Ali (03154103173)
Dynamic programming prasintation eaisy
Dynamic_Programming_Presentation in Design and Analysis of Algorithm
algorithm_6dynamic_programming.pdf
Module 2ppt.pptx divid and conquer method
Data Structure and Algorithm - Divide and Conquer
Dynamic Programming: Memoization, Introduction to ALgorithms
Ad

Recently uploaded (20)

PPTX
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
PDF
From loneliness to social connection charting
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
How to Manage Starshipit in Odoo 18 - Odoo Slides
PPTX
Open Quiz Monsoon Mind Game Prelims.pptx
PDF
Mga Unang Hakbang Tungo Sa Tao by Joe Vibar Nero.pdf
PPTX
Open Quiz Monsoon Mind Game Final Set.pptx
DOCX
UPPER GASTRO INTESTINAL DISORDER.docx
PPTX
Cell Structure & Organelles in detailed.
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
PSYCHOLOGY IN EDUCATION.pdf ( nice pdf ...)
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
NOI Hackathon - Summer Edition - GreenThumber.pptx
From loneliness to social connection charting
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
How to Manage Starshipit in Odoo 18 - Odoo Slides
Open Quiz Monsoon Mind Game Prelims.pptx
Mga Unang Hakbang Tungo Sa Tao by Joe Vibar Nero.pdf
Open Quiz Monsoon Mind Game Final Set.pptx
UPPER GASTRO INTESTINAL DISORDER.docx
Cell Structure & Organelles in detailed.
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PSYCHOLOGY IN EDUCATION.pdf ( nice pdf ...)
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Week 4 Term 3 Study Techniques revisited.pptx
human mycosis Human fungal infections are called human mycosis..pptx
102 student loan defaulters named and shamed – Is someone you know on the list?
O5-L3 Freight Transport Ops (International) V1.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...

Dynamic programming Basics

  • 1. WHY DYNAMIC PROGRAMMING?? In Divide & Conqure , 1) We divide the sub-problem into random instances. 2) Solve each instance randomly. 3) Combine all the solutions to get the final solution. Now ,there may arise two issues 1) Sub-problems are not independent to solve them separately. AND/OR 2) We may have to calculate some part of solution again and again.
  • 2. SO WHAT’S THERE IN DP? 1) Solution First issue (independence) • DP allows us to have inter-dependant sub-problems. • Rather its good to have inter-related sub-instances so that we can reuse them. 2) Solution to second issue (re-computation) • Previously computed sub-solutions are used to calculate further sub-solutions. • So re-computation is avoided and each time we calculate next value, We have its required sub-values calculated.
  • 3. HOW ITS DONE ?  Sequence of actions : : -> 1) Divide the input at each possible split point. 2) Compute the solution to smallest instance. 3) STORE THE RESULT. ******** 4) Using previously stored result calculate the next result. (AND STORE IT……!!!) 5) Keep doing this till you reach the final solution.
  • 4. SO, 1) We never compute the already computed solution. 2)We always have required result with us.
  • 5. EXAMPLES 1) Solving the fibonacci sequence for n=1,000,002. • This will be a very long process, but what if I give you the results for n=1,000,000 and n=1,000,0001? Suddenly the problem just becomes more manageable. 2)string problems like string edit problem. You solve a subset(s) of the problem and then use that information to solve the more difficult original problem.
  • 6. FIBONACCI WITHOUT DP int fib(int n) { if (n <= 1) return n;//Return 0 for n=0 and 1 for n=1 return fib(n-1) + fib(n-2);//Else return Fn-1 + Fn-2 } • Time complexity : T(n-1) + T(n-2) Which is exponential…..! The worst possible Fibonacci solution:
  • 7. FIBONACCI WITH DP int fib(int n) { /* Declare an array to store fibonacci numbers. */ int f[n+1]; int i; /* 0th and 1st number of the series are 0 and 1*/ f[0] = 0; f[1] = 1; for (i = 2; i <= n; i++) { /* Add the previous 2 numbers in the series and store it */ f[i] = f[i-1] + f[i-2]; } return f[n]; }
  • 8. TIME AND SPACE WITH DP • Time Complexity: O(n) • Extra Space: O(n)
  • 9. V I S H N U D A H A T O N D E PRESENTED BY