SlideShare a Scribd company logo
Dynamic Programming
What is Dynamic Programming
 A method for solving complex problems by breaking them down into simpler sub
problems. It is applicable to problems exhibiting the properties of overlapping
subproblems which are only slightly smaller
o The key idea behind dynamic programming is quite simple. In general, to solve a
given problem, we need to solve different parts of the problem (subproblems),
then combine the solutions of the subproblems to reach an overall solution.
Two types of Dynamic Programming
 Bottom-up algorithm
 In order to solve a given problem, a series of subproblems is solved.
 Top-Down algorithm (often called Memoization.)
 a technique that is associated with Dynamic Programming
 The concept is to cache the result of a function given its parameter so that the
calculation will not be repeated; it is simply retrieved
Fibonacci Sequence with Dynamic Programming
Pseudo-code for a simple recursive function will be :
fib(int n)
{
if (n==0) return 0;
if (n==1) return 1;
return fib(n-1)+fib(n-2);
}
Fibonacci Sequence with Dynamic Programming
Example:
 Consider the Fibonacci Series : 0,1,1,2,3,5,8,13,21...
 F(0)=0 ; F(1) = 1; F(N)=F(N-1)+F(N-2)
Calculating 14th fibonacci no., i.e., f14
Using Dynamic Programming
The 0-1 Knapsack
Problem
0-1 Knapsack
 the 0-1 Knapsack problem and its algorithm as well as its derivation from its
recursive formulation
 to enhance the development of understanding the use of dynamic programming to
solve discrete optimization problems
The complete recursive formulation of the solution
 Knap(k, y) = Knap(k-1, y) if y < a[k]
 Knap(k, y) = max { Knap(k-1, y), Knap(k-1, y-a[k])+ c[k] } if y > a[k]
 Knap(k, y) = max { Knap(k-1, y), c[k] } if y = a[k]
 Knap(0, y) = 0
 Suppose a[] = [4, 3, 2, 1], c[] = [7, 5, 3, 1] and b = 6.
Given: Suppose a[] = [4, 3, 2, 1], c[] = [7, 5, 3, 1] and b = 6.
 The ci represents the value of selecting item i for inclusion in the knapsack;
 The ai represents the weight of item i - the weights
 The constant b represents the maximum weight that the knapsack is permitted to
hold.
Dynamic Programming Matrix with the initialization
The matrix labels are colored orange and the initialized cells
Dynamic Programming Matrix with the initialization
has a weight of 4
Suppose a[] = [4, 3, 2, 1], c[] = [7, 5, 3, 1]
Weights = [4, 3, 2, 1]
Values = [7, 5, 3, 1]
Dynamic Programming Matrix with the initialization
has a weight of 3
Suppose a[] = [4, 3, 2, 1], c[] = [7, 5, 3, 1]
Weights = [4, 3, 2, 1]
Values = [7, 5, 3, 1]
Dynamic Programming Matrix with the initialization
has a weight of 2
Suppose a[] = [4, 3, 2, 1], c[] = [7, 5, 3, 1]
Weights = [4, 3, 2, 1]
Values = [7, 5, 3, 1]
Dynamic Programming Matrix with the initialization
has a weight of 1
The maximum value for this knapsack problem is in the bottom leftmost entry in the matrix, knap[4][5].
Weights = [4, 3, 2, 1]
Values = [7, 5, 3, 1]
Using Dynamic Programming
Coin Change
A dynamic programming solution (Coin Change )
 Idea: Solve first for one cent, then two cents, then three cents, etc., up
to the desired amount
 Save each answer in an array !
 For each new amount N, compute all the possible pairs of previous
answers which sum to N
 For example, to find the solution for 13¢,
 First, solve for all of 1¢, 2¢, 3¢, ..., 12¢
 Next, choose the best solution among:
 Solution for 1¢ + solution for 12¢
 Solution for 2¢ + solution for 11¢
 Solution for 3¢ + solution for 10¢
 Solution for 4¢ + solution for 9¢
 Solution for 5¢ + solution for 8¢
 Solution for 6¢ + solution for 7¢
Example
To count total number solutions, we can divide all set solutions in two sets.
 Suppose coins are 1¢, 3¢, and 4¢
 There’s only one way to make 1¢ (one coin)
 To make 2¢, try 1¢+1¢ (one coin + one coin = 2 coins)
 To make 3¢, just use the 3¢ coin (one coin)
 To make 4¢, just use the 4¢ coin (one coin)
 To make 5¢, try
 1¢ + 4¢ (1 coin + 1 coin = 2 coins)
 2¢ + 3¢ (2 coins + 1 coin = 3 coins)
 The first solution is better, so best solution is 2 coins
 To make 6¢, try
 1¢ + 5¢ (1 coin + 2 coins = 3 coins)
 2¢ + 4¢ (2 coins + 1 coin = 3 coins)
 3¢ + 3¢ (1 coin + 1 coin = 2 coins) – best solution
 Etc.
Time Complexity: O(mn)
Coin Change – Source Code
Sample Source Code
Dynamic programming example--typesetting a
paragraph.
Overall running time: O(n3)
THE END.

More Related Content

Similar to 5617723.pptx

Daa:Dynamic Programing
Daa:Dynamic ProgramingDaa:Dynamic Programing
Daa:Dynamic Programing
rupali_2bonde
 
Matrix chain multiplication
Matrix chain multiplicationMatrix chain multiplication
Matrix chain multiplication
Respa Peter
 
Matrix chain multiplication in design analysis of algorithm
Matrix chain multiplication in design analysis of algorithmMatrix chain multiplication in design analysis of algorithm
Matrix chain multiplication in design analysis of algorithm
RajKumar323561
 
Dynamic programming (dp) in Algorithm
Dynamic programming (dp) in AlgorithmDynamic programming (dp) in Algorithm
Dynamic programming (dp) in Algorithm
RaihanIslamSonet
 
Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)
Pramit Kumar
 
Daa notes 2
Daa notes 2Daa notes 2
Daa notes 2
smruti sarangi
 
Dynamic programming1
Dynamic programming1Dynamic programming1
Dynamic programming1
debolina13
 
dynamic-programming
dynamic-programmingdynamic-programming
dynamic-programming
MuhammadSheraz836877
 
Chapter 16
Chapter 16Chapter 16
Chapter 16
ashish bansal
 
Course project solutions 2019
Course project solutions 2019Course project solutions 2019
Course project solutions 2019
Robert Geofroy
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
Gopi Saiteja
 
Support Vector Machines Simply
Support Vector Machines SimplySupport Vector Machines Simply
Support Vector Machines Simply
Emad Nabil
 
Introduction to Dynamic Programming.pptx
Introduction to Dynamic Programming.pptxIntroduction to Dynamic Programming.pptx
Introduction to Dynamic Programming.pptx
PochupouOwo
 
Module 3_DAA (2).pptx
Module 3_DAA (2).pptxModule 3_DAA (2).pptx
Module 3_DAA (2).pptx
AnkitaVerma776806
 
Chapter 5.pptx
Chapter 5.pptxChapter 5.pptx
Chapter 5.pptx
Tekle12
 
Dynamic pgmming
Dynamic pgmmingDynamic pgmming
Dynamic pgmming
Dr. C.V. Suresh Babu
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
CRYPTOGRAPHY AND NUMBER THEORY, he ha huli
CRYPTOGRAPHY AND NUMBER THEORY, he ha huliCRYPTOGRAPHY AND NUMBER THEORY, he ha huli
CRYPTOGRAPHY AND NUMBER THEORY, he ha huli
harshmacduacin
 
Applied Algorithms and Structures week999
Applied Algorithms and Structures week999Applied Algorithms and Structures week999
Applied Algorithms and Structures week999
fashiontrendzz20
 
5.3 dynamic programming 03
5.3 dynamic programming 035.3 dynamic programming 03
5.3 dynamic programming 03
Krish_ver2
 

Similar to 5617723.pptx (20)

Daa:Dynamic Programing
Daa:Dynamic ProgramingDaa:Dynamic Programing
Daa:Dynamic Programing
 
Matrix chain multiplication
Matrix chain multiplicationMatrix chain multiplication
Matrix chain multiplication
 
Matrix chain multiplication in design analysis of algorithm
Matrix chain multiplication in design analysis of algorithmMatrix chain multiplication in design analysis of algorithm
Matrix chain multiplication in design analysis of algorithm
 
Dynamic programming (dp) in Algorithm
Dynamic programming (dp) in AlgorithmDynamic programming (dp) in Algorithm
Dynamic programming (dp) in Algorithm
 
Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)
 
Daa notes 2
Daa notes 2Daa notes 2
Daa notes 2
 
Dynamic programming1
Dynamic programming1Dynamic programming1
Dynamic programming1
 
dynamic-programming
dynamic-programmingdynamic-programming
dynamic-programming
 
Chapter 16
Chapter 16Chapter 16
Chapter 16
 
Course project solutions 2019
Course project solutions 2019Course project solutions 2019
Course project solutions 2019
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Support Vector Machines Simply
Support Vector Machines SimplySupport Vector Machines Simply
Support Vector Machines Simply
 
Introduction to Dynamic Programming.pptx
Introduction to Dynamic Programming.pptxIntroduction to Dynamic Programming.pptx
Introduction to Dynamic Programming.pptx
 
Module 3_DAA (2).pptx
Module 3_DAA (2).pptxModule 3_DAA (2).pptx
Module 3_DAA (2).pptx
 
Chapter 5.pptx
Chapter 5.pptxChapter 5.pptx
Chapter 5.pptx
 
Dynamic pgmming
Dynamic pgmmingDynamic pgmming
Dynamic pgmming
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
CRYPTOGRAPHY AND NUMBER THEORY, he ha huli
CRYPTOGRAPHY AND NUMBER THEORY, he ha huliCRYPTOGRAPHY AND NUMBER THEORY, he ha huli
CRYPTOGRAPHY AND NUMBER THEORY, he ha huli
 
Applied Algorithms and Structures week999
Applied Algorithms and Structures week999Applied Algorithms and Structures week999
Applied Algorithms and Structures week999
 
5.3 dynamic programming 03
5.3 dynamic programming 035.3 dynamic programming 03
5.3 dynamic programming 03
 

Recently uploaded

2023 Ukraine Crisis Media Center Annual Report
2023 Ukraine Crisis Media Center Annual Report2023 Ukraine Crisis Media Center Annual Report
2023 Ukraine Crisis Media Center Annual Report
UkraineCrisisMediaCenter
 
怎么办理(lincoln学位证书)英国林肯大学毕业证文凭学位证书原版一模一样
怎么办理(lincoln学位证书)英国林肯大学毕业证文凭学位证书原版一模一样怎么办理(lincoln学位证书)英国林肯大学毕业证文凭学位证书原版一模一样
怎么办理(lincoln学位证书)英国林肯大学毕业证文凭学位证书原版一模一样
kekzed
 
2023 Ukraine Crisis Media Center Financial Report
2023 Ukraine Crisis Media Center Financial Report2023 Ukraine Crisis Media Center Financial Report
2023 Ukraine Crisis Media Center Financial Report
UkraineCrisisMediaCenter
 
一比一原版(unc毕业证书)美国北卡罗来纳大学教堂山分校毕业证如何办理
一比一原版(unc毕业证书)美国北卡罗来纳大学教堂山分校毕业证如何办理一比一原版(unc毕业证书)美国北卡罗来纳大学教堂山分校毕业证如何办理
一比一原版(unc毕业证书)美国北卡罗来纳大学教堂山分校毕业证如何办理
gfysze
 
The Intersection between Competition and Data Privacy – CAPEL – June 2024 OEC...
The Intersection between Competition and Data Privacy – CAPEL – June 2024 OEC...The Intersection between Competition and Data Privacy – CAPEL – June 2024 OEC...
The Intersection between Competition and Data Privacy – CAPEL – June 2024 OEC...
OECD Directorate for Financial and Enterprise Affairs
 
Data Processing in PHP - PHPers 2024 Poznań
Data Processing in PHP - PHPers 2024 PoznańData Processing in PHP - PHPers 2024 Poznań
Data Processing in PHP - PHPers 2024 Poznań
Norbert Orzechowicz
 
ServiceNow CIS-ITSM Exam Dumps & Questions [2024]
ServiceNow CIS-ITSM Exam Dumps & Questions [2024]ServiceNow CIS-ITSM Exam Dumps & Questions [2024]
ServiceNow CIS-ITSM Exam Dumps & Questions [2024]
SkillCertProExams
 
Proposal: The Ark Project and The BEEP Inc
Proposal: The Ark Project and The BEEP IncProposal: The Ark Project and The BEEP Inc
Proposal: The Ark Project and The BEEP Inc
Raheem Muhammad
 
Using-Presentation-Software-to-the-Fullf.pptx
Using-Presentation-Software-to-the-Fullf.pptxUsing-Presentation-Software-to-the-Fullf.pptx
Using-Presentation-Software-to-the-Fullf.pptx
kainatfatyma9
 
2 December UAE National Day - United Arab Emirates
2 December UAE National Day - United Arab Emirates2 December UAE National Day - United Arab Emirates
2 December UAE National Day - United Arab Emirates
UAE Ppt
 
SASi-SPi Science Policy Lab Pre-engagement
SASi-SPi Science Policy Lab Pre-engagementSASi-SPi Science Policy Lab Pre-engagement
SASi-SPi Science Policy Lab Pre-engagement
Francois Stepman
 
IEEE CIS Webinar Sustainable futures.pdf
IEEE CIS Webinar Sustainable futures.pdfIEEE CIS Webinar Sustainable futures.pdf
IEEE CIS Webinar Sustainable futures.pdf
Claudio Gallicchio
 
Prsentation for VIVA Welike project 1semester.pptx
Prsentation for VIVA Welike project 1semester.pptxPrsentation for VIVA Welike project 1semester.pptx
Prsentation for VIVA Welike project 1semester.pptx
prafulpawar29
 
AWS User Group Torino 2024 #3 - 18/06/2024
AWS User Group Torino 2024 #3 - 18/06/2024AWS User Group Torino 2024 #3 - 18/06/2024
AWS User Group Torino 2024 #3 - 18/06/2024
Guido Maria Nebiolo
 
Bridging the visual gap between cultural heritage and digital scholarship
Bridging the visual gap between cultural heritage and digital scholarshipBridging the visual gap between cultural heritage and digital scholarship
Bridging the visual gap between cultural heritage and digital scholarship
Inesm9
 
The Intersection between Competition and Data Privacy – KEMP – June 2024 OECD...
The Intersection between Competition and Data Privacy – KEMP – June 2024 OECD...The Intersection between Competition and Data Privacy – KEMP – June 2024 OECD...
The Intersection between Competition and Data Privacy – KEMP – June 2024 OECD...
OECD Directorate for Financial and Enterprise Affairs
 
The Intersection between Competition and Data Privacy – OECD – June 2024 OECD...
The Intersection between Competition and Data Privacy – OECD – June 2024 OECD...The Intersection between Competition and Data Privacy – OECD – June 2024 OECD...
The Intersection between Competition and Data Privacy – OECD – June 2024 OECD...
OECD Directorate for Financial and Enterprise Affairs
 
Genesis chapter 3 Isaiah Scudder.pptx
Genesis    chapter 3 Isaiah Scudder.pptxGenesis    chapter 3 Isaiah Scudder.pptx
Genesis chapter 3 Isaiah Scudder.pptx
FamilyWorshipCenterD
 
2023 Ukraine Crisis Media Center Finance Balance
2023 Ukraine Crisis Media Center Finance Balance2023 Ukraine Crisis Media Center Finance Balance
2023 Ukraine Crisis Media Center Finance Balance
UkraineCrisisMediaCenter
 
ACTIVE IMPLANTABLE MEDICAL DEVICE IN EUROPE
ACTIVE IMPLANTABLE MEDICAL DEVICE IN EUROPEACTIVE IMPLANTABLE MEDICAL DEVICE IN EUROPE
ACTIVE IMPLANTABLE MEDICAL DEVICE IN EUROPE
Charmi13
 

Recently uploaded (20)

2023 Ukraine Crisis Media Center Annual Report
2023 Ukraine Crisis Media Center Annual Report2023 Ukraine Crisis Media Center Annual Report
2023 Ukraine Crisis Media Center Annual Report
 
怎么办理(lincoln学位证书)英国林肯大学毕业证文凭学位证书原版一模一样
怎么办理(lincoln学位证书)英国林肯大学毕业证文凭学位证书原版一模一样怎么办理(lincoln学位证书)英国林肯大学毕业证文凭学位证书原版一模一样
怎么办理(lincoln学位证书)英国林肯大学毕业证文凭学位证书原版一模一样
 
2023 Ukraine Crisis Media Center Financial Report
2023 Ukraine Crisis Media Center Financial Report2023 Ukraine Crisis Media Center Financial Report
2023 Ukraine Crisis Media Center Financial Report
 
一比一原版(unc毕业证书)美国北卡罗来纳大学教堂山分校毕业证如何办理
一比一原版(unc毕业证书)美国北卡罗来纳大学教堂山分校毕业证如何办理一比一原版(unc毕业证书)美国北卡罗来纳大学教堂山分校毕业证如何办理
一比一原版(unc毕业证书)美国北卡罗来纳大学教堂山分校毕业证如何办理
 
The Intersection between Competition and Data Privacy – CAPEL – June 2024 OEC...
The Intersection between Competition and Data Privacy – CAPEL – June 2024 OEC...The Intersection between Competition and Data Privacy – CAPEL – June 2024 OEC...
The Intersection between Competition and Data Privacy – CAPEL – June 2024 OEC...
 
Data Processing in PHP - PHPers 2024 Poznań
Data Processing in PHP - PHPers 2024 PoznańData Processing in PHP - PHPers 2024 Poznań
Data Processing in PHP - PHPers 2024 Poznań
 
ServiceNow CIS-ITSM Exam Dumps & Questions [2024]
ServiceNow CIS-ITSM Exam Dumps & Questions [2024]ServiceNow CIS-ITSM Exam Dumps & Questions [2024]
ServiceNow CIS-ITSM Exam Dumps & Questions [2024]
 
Proposal: The Ark Project and The BEEP Inc
Proposal: The Ark Project and The BEEP IncProposal: The Ark Project and The BEEP Inc
Proposal: The Ark Project and The BEEP Inc
 
Using-Presentation-Software-to-the-Fullf.pptx
Using-Presentation-Software-to-the-Fullf.pptxUsing-Presentation-Software-to-the-Fullf.pptx
Using-Presentation-Software-to-the-Fullf.pptx
 
2 December UAE National Day - United Arab Emirates
2 December UAE National Day - United Arab Emirates2 December UAE National Day - United Arab Emirates
2 December UAE National Day - United Arab Emirates
 
SASi-SPi Science Policy Lab Pre-engagement
SASi-SPi Science Policy Lab Pre-engagementSASi-SPi Science Policy Lab Pre-engagement
SASi-SPi Science Policy Lab Pre-engagement
 
IEEE CIS Webinar Sustainable futures.pdf
IEEE CIS Webinar Sustainable futures.pdfIEEE CIS Webinar Sustainable futures.pdf
IEEE CIS Webinar Sustainable futures.pdf
 
Prsentation for VIVA Welike project 1semester.pptx
Prsentation for VIVA Welike project 1semester.pptxPrsentation for VIVA Welike project 1semester.pptx
Prsentation for VIVA Welike project 1semester.pptx
 
AWS User Group Torino 2024 #3 - 18/06/2024
AWS User Group Torino 2024 #3 - 18/06/2024AWS User Group Torino 2024 #3 - 18/06/2024
AWS User Group Torino 2024 #3 - 18/06/2024
 
Bridging the visual gap between cultural heritage and digital scholarship
Bridging the visual gap between cultural heritage and digital scholarshipBridging the visual gap between cultural heritage and digital scholarship
Bridging the visual gap between cultural heritage and digital scholarship
 
The Intersection between Competition and Data Privacy – KEMP – June 2024 OECD...
The Intersection between Competition and Data Privacy – KEMP – June 2024 OECD...The Intersection between Competition and Data Privacy – KEMP – June 2024 OECD...
The Intersection between Competition and Data Privacy – KEMP – June 2024 OECD...
 
The Intersection between Competition and Data Privacy – OECD – June 2024 OECD...
The Intersection between Competition and Data Privacy – OECD – June 2024 OECD...The Intersection between Competition and Data Privacy – OECD – June 2024 OECD...
The Intersection between Competition and Data Privacy – OECD – June 2024 OECD...
 
Genesis chapter 3 Isaiah Scudder.pptx
Genesis    chapter 3 Isaiah Scudder.pptxGenesis    chapter 3 Isaiah Scudder.pptx
Genesis chapter 3 Isaiah Scudder.pptx
 
2023 Ukraine Crisis Media Center Finance Balance
2023 Ukraine Crisis Media Center Finance Balance2023 Ukraine Crisis Media Center Finance Balance
2023 Ukraine Crisis Media Center Finance Balance
 
ACTIVE IMPLANTABLE MEDICAL DEVICE IN EUROPE
ACTIVE IMPLANTABLE MEDICAL DEVICE IN EUROPEACTIVE IMPLANTABLE MEDICAL DEVICE IN EUROPE
ACTIVE IMPLANTABLE MEDICAL DEVICE IN EUROPE
 

5617723.pptx

  • 2. What is Dynamic Programming  A method for solving complex problems by breaking them down into simpler sub problems. It is applicable to problems exhibiting the properties of overlapping subproblems which are only slightly smaller o The key idea behind dynamic programming is quite simple. In general, to solve a given problem, we need to solve different parts of the problem (subproblems), then combine the solutions of the subproblems to reach an overall solution.
  • 3. Two types of Dynamic Programming  Bottom-up algorithm  In order to solve a given problem, a series of subproblems is solved.  Top-Down algorithm (often called Memoization.)  a technique that is associated with Dynamic Programming  The concept is to cache the result of a function given its parameter so that the calculation will not be repeated; it is simply retrieved
  • 4. Fibonacci Sequence with Dynamic Programming Pseudo-code for a simple recursive function will be : fib(int n) { if (n==0) return 0; if (n==1) return 1; return fib(n-1)+fib(n-2); }
  • 5. Fibonacci Sequence with Dynamic Programming Example:  Consider the Fibonacci Series : 0,1,1,2,3,5,8,13,21...  F(0)=0 ; F(1) = 1; F(N)=F(N-1)+F(N-2) Calculating 14th fibonacci no., i.e., f14
  • 6. Using Dynamic Programming The 0-1 Knapsack Problem
  • 7. 0-1 Knapsack  the 0-1 Knapsack problem and its algorithm as well as its derivation from its recursive formulation  to enhance the development of understanding the use of dynamic programming to solve discrete optimization problems
  • 8. The complete recursive formulation of the solution  Knap(k, y) = Knap(k-1, y) if y < a[k]  Knap(k, y) = max { Knap(k-1, y), Knap(k-1, y-a[k])+ c[k] } if y > a[k]  Knap(k, y) = max { Knap(k-1, y), c[k] } if y = a[k]  Knap(0, y) = 0  Suppose a[] = [4, 3, 2, 1], c[] = [7, 5, 3, 1] and b = 6.
  • 9. Given: Suppose a[] = [4, 3, 2, 1], c[] = [7, 5, 3, 1] and b = 6.  The ci represents the value of selecting item i for inclusion in the knapsack;  The ai represents the weight of item i - the weights  The constant b represents the maximum weight that the knapsack is permitted to hold.
  • 10. Dynamic Programming Matrix with the initialization The matrix labels are colored orange and the initialized cells
  • 11. Dynamic Programming Matrix with the initialization has a weight of 4 Suppose a[] = [4, 3, 2, 1], c[] = [7, 5, 3, 1] Weights = [4, 3, 2, 1] Values = [7, 5, 3, 1]
  • 12. Dynamic Programming Matrix with the initialization has a weight of 3 Suppose a[] = [4, 3, 2, 1], c[] = [7, 5, 3, 1] Weights = [4, 3, 2, 1] Values = [7, 5, 3, 1]
  • 13. Dynamic Programming Matrix with the initialization has a weight of 2 Suppose a[] = [4, 3, 2, 1], c[] = [7, 5, 3, 1] Weights = [4, 3, 2, 1] Values = [7, 5, 3, 1]
  • 14. Dynamic Programming Matrix with the initialization has a weight of 1 The maximum value for this knapsack problem is in the bottom leftmost entry in the matrix, knap[4][5]. Weights = [4, 3, 2, 1] Values = [7, 5, 3, 1]
  • 16. A dynamic programming solution (Coin Change )  Idea: Solve first for one cent, then two cents, then three cents, etc., up to the desired amount  Save each answer in an array !  For each new amount N, compute all the possible pairs of previous answers which sum to N  For example, to find the solution for 13¢,  First, solve for all of 1¢, 2¢, 3¢, ..., 12¢  Next, choose the best solution among:  Solution for 1¢ + solution for 12¢  Solution for 2¢ + solution for 11¢  Solution for 3¢ + solution for 10¢  Solution for 4¢ + solution for 9¢  Solution for 5¢ + solution for 8¢  Solution for 6¢ + solution for 7¢
  • 17. Example To count total number solutions, we can divide all set solutions in two sets.  Suppose coins are 1¢, 3¢, and 4¢  There’s only one way to make 1¢ (one coin)  To make 2¢, try 1¢+1¢ (one coin + one coin = 2 coins)  To make 3¢, just use the 3¢ coin (one coin)  To make 4¢, just use the 4¢ coin (one coin)  To make 5¢, try  1¢ + 4¢ (1 coin + 1 coin = 2 coins)  2¢ + 3¢ (2 coins + 1 coin = 3 coins)  The first solution is better, so best solution is 2 coins  To make 6¢, try  1¢ + 5¢ (1 coin + 2 coins = 3 coins)  2¢ + 4¢ (2 coins + 1 coin = 3 coins)  3¢ + 3¢ (1 coin + 1 coin = 2 coins) – best solution  Etc.
  • 18. Time Complexity: O(mn) Coin Change – Source Code
  • 19. Sample Source Code Dynamic programming example--typesetting a paragraph. Overall running time: O(n3)