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
Handling noisy data
Vivek Gandhi
 
PPT
Pipeline parallelism
Dr. C.V. Suresh Babu
 
PPTX
Dynamic Programming Matrix Chain Multiplication
KrishnakoumarC
 
PDF
Introduction to algorithms
subhashchandra197
 
PPT
Dynamic programming
Gopi Saiteja
 
PPT
Genetic algorithms
guest9938738
 
PDF
Dynamic programming
Amit Kumar Rathi
 
PPTX
Huffman Coding Algorithm Presentation
Akm Monir
 
Handling noisy data
Vivek Gandhi
 
Pipeline parallelism
Dr. C.V. Suresh Babu
 
Dynamic Programming Matrix Chain Multiplication
KrishnakoumarC
 
Introduction to algorithms
subhashchandra197
 
Dynamic programming
Gopi Saiteja
 
Genetic algorithms
guest9938738
 
Dynamic programming
Amit Kumar Rathi
 
Huffman Coding Algorithm Presentation
Akm Monir
 

What's hot (20)

PPTX
Divide and Conquer - Part 1
Amrinder Arora
 
PDF
Dynamic Programming
Bharat Bhushan
 
PPTX
Tsp branch and-bound
Saravanan Natarajan
 
PPTX
String matching algorithms
Ashikapokiya12345
 
PPTX
Turing Machine
AniketKandara1
 
PPT
Recursion tree method
Rajendran
 
PPTX
ContextFreeGrammars.pptx
PEzhumalai
 
PDF
The Back Propagation Learning Algorithm
ESCOM
 
PDF
Optimal binary search tree dynamic programming
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
 
PPTX
Performance analysis(Time & Space Complexity)
swapnac12
 
PPTX
Petri Nets: Properties, Analysis and Applications
Dr. Mohamed Torky
 
PDF
String matching algorithms
Mahdi Esmailoghli
 
PPTX
7-NFA to Minimized DFA.pptx
SLekshmiNair
 
PPTX
Turing Machine
arwa wshyar
 
PPTX
Divide and conquer
ramya marichamy
 
PDF
P, NP, NP-Complete, and NP-Hard
Animesh Chaturvedi
 
PPTX
First and follow set
Dawood Faheem Abbasi
 
PPTX
01 Knapsack using Dynamic Programming
Fenil Shah
 
PDF
Python matplotlib cheat_sheet
Nishant Upadhyay
 
PPTX
Chess engine presentation
TanushreeSharma34
 
Divide and Conquer - Part 1
Amrinder Arora
 
Dynamic Programming
Bharat Bhushan
 
Tsp branch and-bound
Saravanan Natarajan
 
String matching algorithms
Ashikapokiya12345
 
Turing Machine
AniketKandara1
 
Recursion tree method
Rajendran
 
ContextFreeGrammars.pptx
PEzhumalai
 
The Back Propagation Learning Algorithm
ESCOM
 
Performance analysis(Time & Space Complexity)
swapnac12
 
Petri Nets: Properties, Analysis and Applications
Dr. Mohamed Torky
 
String matching algorithms
Mahdi Esmailoghli
 
7-NFA to Minimized DFA.pptx
SLekshmiNair
 
Turing Machine
arwa wshyar
 
Divide and conquer
ramya marichamy
 
P, NP, NP-Complete, and NP-Hard
Animesh Chaturvedi
 
First and follow set
Dawood Faheem Abbasi
 
01 Knapsack using Dynamic Programming
Fenil Shah
 
Python matplotlib cheat_sheet
Nishant Upadhyay
 
Chess engine presentation
TanushreeSharma34
 
Ad

Similar to Dynamic programming Basics (20)

PPT
Recursion C programming exercises_ Recursion - w3resource.ppt
Carlos701746
 
PPT
dynamic programming Rod cutting class
giridaroori
 
PDF
Effective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
Professor Lili Saghafi
 
PDF
NMT with Attention-1.pdfhhhhhhhhhhhhhhhh
Kowser Tusher
 
PDF
Recursion.pdf
Flavia Tembo Kambale
 
PPTX
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
15AnasKhan
 
PPT
number system: Floating Point representation.ppt
NARENDRAKUMARCHAURAS1
 
PPTX
Recursion
SAGARDAVE29
 
PPTX
Daa unit 2
snehajiyani
 
PPTX
Daa unit 2
jinalgoti
 
PPTX
Algorithm Using Divide And Conquer
UrviBhalani2
 
PDF
C users_mpk7_app_data_local_temp_plugtmp_plugin-week3recursive
rokiah64
 
PPT
Dynamicpgmming
Muhammad Wasif
 
PPTX
W8L1 Introduction & Fibonacci Numbers part 1.pptx
sakibahmed181234
 
PDF
Recursion examples
HafsaZahran
 
PPTX
Lesson4.1 u4 l1 binary representation
Lexume1
 
PPT
Dynamic pgmming
Dr. C.V. Suresh Babu
 
PDF
02 Notes Divide and Conquer
Andres Mendez-Vazquez
 
PPTX
Analysis of Algorithm II Unit version .pptx
rajesshs31r
 
PPTX
dynamic programming complete by Mumtaz Ali (03154103173)
Mumtaz Ali
 
Recursion C programming exercises_ Recursion - w3resource.ppt
Carlos701746
 
dynamic programming Rod cutting class
giridaroori
 
Effective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
Professor Lili Saghafi
 
NMT with Attention-1.pdfhhhhhhhhhhhhhhhh
Kowser Tusher
 
Recursion.pdf
Flavia Tembo Kambale
 
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
15AnasKhan
 
number system: Floating Point representation.ppt
NARENDRAKUMARCHAURAS1
 
Recursion
SAGARDAVE29
 
Daa unit 2
snehajiyani
 
Daa unit 2
jinalgoti
 
Algorithm Using Divide And Conquer
UrviBhalani2
 
C users_mpk7_app_data_local_temp_plugtmp_plugin-week3recursive
rokiah64
 
Dynamicpgmming
Muhammad Wasif
 
W8L1 Introduction & Fibonacci Numbers part 1.pptx
sakibahmed181234
 
Recursion examples
HafsaZahran
 
Lesson4.1 u4 l1 binary representation
Lexume1
 
Dynamic pgmming
Dr. C.V. Suresh Babu
 
02 Notes Divide and Conquer
Andres Mendez-Vazquez
 
Analysis of Algorithm II Unit version .pptx
rajesshs31r
 
dynamic programming complete by Mumtaz Ali (03154103173)
Mumtaz Ali
 
Ad

Recently uploaded (20)

DOCX
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
PPTX
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
PPTX
Care of patients with elImination deviation.pptx
AneetaSharma15
 
PDF
Study Material and notes for Women Empowerment
ComputerScienceSACWC
 
PDF
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 
PPTX
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 
PPTX
PREVENTIVE PEDIATRIC. pptx
AneetaSharma15
 
DOCX
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
DOCX
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
PDF
Sunset Boulevard Student Revision Booklet
jpinnuck
 
PPTX
Trends in pediatric nursing .pptx
AneetaSharma15
 
PDF
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
PPTX
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
PDF
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
PPTX
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
PPTX
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
PPTX
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
PPTX
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
PDF
RA 12028_ARAL_Orientation_Day-2-Sessions_v2.pdf
Seven De Los Reyes
 
PDF
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
Care of patients with elImination deviation.pptx
AneetaSharma15
 
Study Material and notes for Women Empowerment
ComputerScienceSACWC
 
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 
PREVENTIVE PEDIATRIC. pptx
AneetaSharma15
 
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
Sunset Boulevard Student Revision Booklet
jpinnuck
 
Trends in pediatric nursing .pptx
AneetaSharma15
 
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
RA 12028_ARAL_Orientation_Day-2-Sessions_v2.pdf
Seven De Los Reyes
 
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 

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