SlideShare a Scribd company logo
CIST212-Data Structures and
Algorithms
Lecture 9
Dynamic Programming I
Prof. Boxiang Dong
https://msuweb.montclair.edu/~dongb/
Office: CCIS-227E
Email: dongb@montclair.edu
Review
• Stack, Queue, Linked List
• traverse: O(n)
• search: O(n)
• insertion: (1)
• deletion: O(1)
• Binary Search Tree
• traverse: inorder, preorder, postorder O(n)
• search: O(h)
• minimum/maximum: O(h)
• successor: O(h)
• insertion: O(h)
• deletion: O(h)
Dynamic Programming
• Dynamic programming is a general strategy to solve computational
problems, like divide-and-conquer.
Dynamic Programming
• Divide-and-conquer paradigm:
• Break the problem into several
disjoint subproblems that are
similar to the original one but
smaller in size.
• Solve the subproblems
recursively.
• Combine these solutions to
create a solution to the original
problem.
• Dynamic programming:
• Break the problem into several
overlapping subproblems that
are similar to the original one
but smaller in size.
• Solve the subproblems
recursively only once.
• Combine these solutions to
create a solution to the original
problem.
Dynamic Programming
Dynamic Programming
• Lets look at the rod-cutting problem.
• Given a rod of length n inches and a table of prices pi for i=1,2,…,n, determine
the maximum revenue rn obtainable by cutting up the rod and selling the
prices.
Dynamic Programming
Dynamic Programming
pn: the revenue from no cut at all
ri+rn-i: the revenue from cutting at the i-th place
This is called the optimal substructure property:
Optimal solutions to a problem incorporate optimal solutions to
related subproblems, which we may solve independently.
Dynamic Programming
• To make it simpler, we have
Only the right-hand side of a cut at the i–th place will be cut further.
Dynamic Programming
• Based on the previous formula, we derive the following recursive
algorithm:
Dynamic Programming
Time complexity: O(2n)
Dynamic Programming
• We do not want to repeatedly calculate r2, r3, …, rn-1.
• In dynamic programming, we only need to calculate them once.
• The first time we calculate ri, we store it in a place.
• The next time we need to use ri, we just retrieve it directly.
Dynamic Programming
Dynamic Programming
Key idea:
1. We use r[0,…,n] to store the maximum revenue.
2. In line 1 of MEMOIZED-CUT-ROD-AUX(p, n, r),
If we find that rn is calculated before, we just
return it directly;
Otherwise, calculate and store it.
Dynamic Programming
• MEMOIZED-CUT-ROD-AUX(p, n, r) calculates rn in a top-down fashion
by recursion.
• Most often, we want a bottom-up solution for two reasons:
• Logically, it is easy to follow.
• Faster, as it avoids recursion.
Dynamic Programming
Time complexity: O(n2)
Space complexity: O(n)
Dynamic programming is an approach
to trading space for efficiency.
Dynamic Programming
Your Task
• Read: Ch 15.1 (Must-be)

More Related Content

PPTX
Dynamic programming
PPTX
Module 2ppt.pptx divid and conquer method
PPTX
Dynamic Programming - Part 1
PDF
Unit 4 of design and analysis of algorithms
PPT
Dynamic programming
PDF
L21_L27_Unit_5_Dynamic_Programming Computer Science
PDF
06DynamicProgramming in algorithms CSE.pdf
PDF
algorithm_6dynamic_programming.pdf
Dynamic programming
Module 2ppt.pptx divid and conquer method
Dynamic Programming - Part 1
Unit 4 of design and analysis of algorithms
Dynamic programming
L21_L27_Unit_5_Dynamic_Programming Computer Science
06DynamicProgramming in algorithms CSE.pdf
algorithm_6dynamic_programming.pdf

Similar to Lecture+9+-+Dynamic+Programming+I.pdf (20)

PPT
Dynamic pgmming
PPTX
Introduction to dynamic programming
PPTX
Dynamic Programing.pptx good for understanding
PPTX
Computer science in Dynamic programming .pptx
PPTX
ADA Unit 2.pptx
PPTX
Dynamic programmng2
PDF
Unit 4 DSA along with DATA Science And it's logical reasoning.
PPT
Dynamicpgmming
PDF
Dynamic programming
PPTX
The Volcano/Cascades Optimizer
PPTX
W8L1 Introduction & Fibonacci Numbers part 1.pptx
PPTX
Dynamic Programming: Optimizing Solutions
PPTX
Dynamic programming class 16
PPTX
dinosourrrrrrrrrrrrrrrrrrrrrr formula .pptx
PPTX
dynamic-programming
PDF
Daa chapter 3
PPT
Pintu ram
PPTX
Dynamic programming prasintation eaisy
PPTX
5617723.pptx
PDF
Dynamic Programming: Memoization, Introduction to ALgorithms
Dynamic pgmming
Introduction to dynamic programming
Dynamic Programing.pptx good for understanding
Computer science in Dynamic programming .pptx
ADA Unit 2.pptx
Dynamic programmng2
Unit 4 DSA along with DATA Science And it's logical reasoning.
Dynamicpgmming
Dynamic programming
The Volcano/Cascades Optimizer
W8L1 Introduction & Fibonacci Numbers part 1.pptx
Dynamic Programming: Optimizing Solutions
Dynamic programming class 16
dinosourrrrrrrrrrrrrrrrrrrrrr formula .pptx
dynamic-programming
Daa chapter 3
Pintu ram
Dynamic programming prasintation eaisy
5617723.pptx
Dynamic Programming: Memoization, Introduction to ALgorithms
Ad

More from ShaistaRiaz4 (20)

PDF
Lecture3(b).pdf
PDF
Algorithms Analysis.pdf
PDF
Case Study(Analysis of Algorithm.pdf
PPT
02_Computer-Evolution(1).ppt
PPT
01_Introduction.ppt
PPTX
Algo_Lecture01.pptx
PPT
02_Computer-Evolution(1).ppt
PPT
01_Introduction.ppt
PDF
Bisma Zahid (1)-1.pdf
PPTX
MNS Lecture 1.pptx
PPTX
Plan (2).pptx
PDF
Lecture 3(a) Asymptotic-analysis.pdf
PPTX
oppositional-defiant-disorder495.pptx
PPTX
Development Education.pptx
PPT
WISC-IV Introduction Handout.ppt
PPTX
Summary and Evaluation of the Book.pptx
PPT
MH&PSS for L&NFBED 7-8 April 2020.ppt
PPT
Intro_to_Literature_2012-2013-1.ppt
PPT
Coping strategies-Farzana Razi.ppt
PPT
Intellectual_development.ppt
Lecture3(b).pdf
Algorithms Analysis.pdf
Case Study(Analysis of Algorithm.pdf
02_Computer-Evolution(1).ppt
01_Introduction.ppt
Algo_Lecture01.pptx
02_Computer-Evolution(1).ppt
01_Introduction.ppt
Bisma Zahid (1)-1.pdf
MNS Lecture 1.pptx
Plan (2).pptx
Lecture 3(a) Asymptotic-analysis.pdf
oppositional-defiant-disorder495.pptx
Development Education.pptx
WISC-IV Introduction Handout.ppt
Summary and Evaluation of the Book.pptx
MH&PSS for L&NFBED 7-8 April 2020.ppt
Intro_to_Literature_2012-2013-1.ppt
Coping strategies-Farzana Razi.ppt
Intellectual_development.ppt
Ad

Recently uploaded (20)

PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Complications of Minimal Access Surgery at WLH
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
01-Introduction-to-Information-Management.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Complications of Minimal Access Surgery at WLH
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Microbial disease of the cardiovascular and lymphatic systems
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Anesthesia in Laparoscopic Surgery in India
Final Presentation General Medicine 03-08-2024.pptx
01-Introduction-to-Information-Management.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
VCE English Exam - Section C Student Revision Booklet
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Microbial diseases, their pathogenesis and prophylaxis
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
human mycosis Human fungal infections are called human mycosis..pptx

Lecture+9+-+Dynamic+Programming+I.pdf

  • 1. CIST212-Data Structures and Algorithms Lecture 9 Dynamic Programming I Prof. Boxiang Dong https://msuweb.montclair.edu/~dongb/ Office: CCIS-227E Email: dongb@montclair.edu
  • 2. Review • Stack, Queue, Linked List • traverse: O(n) • search: O(n) • insertion: (1) • deletion: O(1) • Binary Search Tree • traverse: inorder, preorder, postorder O(n) • search: O(h) • minimum/maximum: O(h) • successor: O(h) • insertion: O(h) • deletion: O(h)
  • 3. Dynamic Programming • Dynamic programming is a general strategy to solve computational problems, like divide-and-conquer.
  • 4. Dynamic Programming • Divide-and-conquer paradigm: • Break the problem into several disjoint subproblems that are similar to the original one but smaller in size. • Solve the subproblems recursively. • Combine these solutions to create a solution to the original problem. • Dynamic programming: • Break the problem into several overlapping subproblems that are similar to the original one but smaller in size. • Solve the subproblems recursively only once. • Combine these solutions to create a solution to the original problem.
  • 6. Dynamic Programming • Lets look at the rod-cutting problem. • Given a rod of length n inches and a table of prices pi for i=1,2,…,n, determine the maximum revenue rn obtainable by cutting up the rod and selling the prices.
  • 8. Dynamic Programming pn: the revenue from no cut at all ri+rn-i: the revenue from cutting at the i-th place This is called the optimal substructure property: Optimal solutions to a problem incorporate optimal solutions to related subproblems, which we may solve independently.
  • 9. Dynamic Programming • To make it simpler, we have Only the right-hand side of a cut at the i–th place will be cut further.
  • 10. Dynamic Programming • Based on the previous formula, we derive the following recursive algorithm:
  • 12. Dynamic Programming • We do not want to repeatedly calculate r2, r3, …, rn-1. • In dynamic programming, we only need to calculate them once. • The first time we calculate ri, we store it in a place. • The next time we need to use ri, we just retrieve it directly.
  • 14. Dynamic Programming Key idea: 1. We use r[0,…,n] to store the maximum revenue. 2. In line 1 of MEMOIZED-CUT-ROD-AUX(p, n, r), If we find that rn is calculated before, we just return it directly; Otherwise, calculate and store it.
  • 15. Dynamic Programming • MEMOIZED-CUT-ROD-AUX(p, n, r) calculates rn in a top-down fashion by recursion. • Most often, we want a bottom-up solution for two reasons: • Logically, it is easy to follow. • Faster, as it avoids recursion.
  • 16. Dynamic Programming Time complexity: O(n2) Space complexity: O(n) Dynamic programming is an approach to trading space for efficiency.
  • 18. Your Task • Read: Ch 15.1 (Must-be)