SlideShare a Scribd company logo
1 of 18
Download to read offline
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

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

Algorithms Design Patterns
Algorithms Design PatternsAlgorithms Design Patterns
Algorithms Design PatternsAshwin Shiv
 
Support Vector Machines is the the the the the the the the the
Support Vector Machines is the the the the the the the the theSupport Vector Machines is the the the the the the the the the
Support Vector Machines is the the the the the the the the thesanjaibalajeessn
 
Computer algorithm(Dynamic Programming).pdf
Computer algorithm(Dynamic Programming).pdfComputer algorithm(Dynamic Programming).pdf
Computer algorithm(Dynamic Programming).pdfjannatulferdousmaish
 
Algo_lecture1-3.pptx
Algo_lecture1-3.pptxAlgo_lecture1-3.pptx
Algo_lecture1-3.pptxssuserb14922
 
AAC ch 3 Advance strategies (Dynamic Programming).pptx
AAC ch 3 Advance strategies (Dynamic Programming).pptxAAC ch 3 Advance strategies (Dynamic Programming).pptx
AAC ch 3 Advance strategies (Dynamic Programming).pptxHarshitSingh334328
 
dynamic programming Rod cutting class
dynamic programming Rod cutting classdynamic programming Rod cutting class
dynamic programming Rod cutting classgiridaroori
 
Analysis and Design of Algorithms
Analysis and Design of AlgorithmsAnalysis and Design of Algorithms
Analysis and Design of AlgorithmsBulbul Agrawal
 
815.07 machine learning using python.pdf
815.07 machine learning using python.pdf815.07 machine learning using python.pdf
815.07 machine learning using python.pdfSairaAtta5
 
CH-1.1 Introduction (1).pptx
CH-1.1 Introduction (1).pptxCH-1.1 Introduction (1).pptx
CH-1.1 Introduction (1).pptxsatvikkushwaha1
 
Chapter 5.pptx
Chapter 5.pptxChapter 5.pptx
Chapter 5.pptxTekle12
 
Module 2ppt.pptx divid and conquer method
Module 2ppt.pptx divid and conquer methodModule 2ppt.pptx divid and conquer method
Module 2ppt.pptx divid and conquer methodJyoReddy9
 
Presentation merge sort.pptx
Presentation merge sort.pptxPresentation merge sort.pptx
Presentation merge sort.pptxkkumaraditya301
 
Dynamic programming 2
Dynamic programming 2Dynamic programming 2
Dynamic programming 2Roy Thomas
 
CS8461 - Design and Analysis of Algorithms
CS8461 - Design and Analysis of AlgorithmsCS8461 - Design and Analysis of Algorithms
CS8461 - Design and Analysis of AlgorithmsKrishnan MuthuManickam
 

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

Algorithms Design Patterns
Algorithms Design PatternsAlgorithms Design Patterns
Algorithms Design Patterns
 
Analysis.ppt
Analysis.pptAnalysis.ppt
Analysis.ppt
 
Algorithms
Algorithms Algorithms
Algorithms
 
Support Vector Machines is the the the the the the the the the
Support Vector Machines is the the the the the the the the theSupport Vector Machines is the the the the the the the the the
Support Vector Machines is the the the the the the the the the
 
Computer algorithm(Dynamic Programming).pdf
Computer algorithm(Dynamic Programming).pdfComputer algorithm(Dynamic Programming).pdf
Computer algorithm(Dynamic Programming).pdf
 
Algo_lecture1-3.pptx
Algo_lecture1-3.pptxAlgo_lecture1-3.pptx
Algo_lecture1-3.pptx
 
AAC ch 3 Advance strategies (Dynamic Programming).pptx
AAC ch 3 Advance strategies (Dynamic Programming).pptxAAC ch 3 Advance strategies (Dynamic Programming).pptx
AAC ch 3 Advance strategies (Dynamic Programming).pptx
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
dynamic programming Rod cutting class
dynamic programming Rod cutting classdynamic programming Rod cutting class
dynamic programming Rod cutting class
 
Analysis and Design of Algorithms
Analysis and Design of AlgorithmsAnalysis and Design of Algorithms
Analysis and Design of Algorithms
 
815.07 machine learning using python.pdf
815.07 machine learning using python.pdf815.07 machine learning using python.pdf
815.07 machine learning using python.pdf
 
Lec1.pptx
Lec1.pptxLec1.pptx
Lec1.pptx
 
CH-1.1 Introduction (1).pptx
CH-1.1 Introduction (1).pptxCH-1.1 Introduction (1).pptx
CH-1.1 Introduction (1).pptx
 
Chapter 5.pptx
Chapter 5.pptxChapter 5.pptx
Chapter 5.pptx
 
Module 2ppt.pptx divid and conquer method
Module 2ppt.pptx divid and conquer methodModule 2ppt.pptx divid and conquer method
Module 2ppt.pptx divid and conquer method
 
LP.ppt
LP.pptLP.ppt
LP.ppt
 
Presentation merge sort.pptx
Presentation merge sort.pptxPresentation merge sort.pptx
Presentation merge sort.pptx
 
dynamic-programming
dynamic-programmingdynamic-programming
dynamic-programming
 
Dynamic programming 2
Dynamic programming 2Dynamic programming 2
Dynamic programming 2
 
CS8461 - Design and Analysis of Algorithms
CS8461 - Design and Analysis of AlgorithmsCS8461 - Design and Analysis of Algorithms
CS8461 - Design and Analysis of Algorithms
 

More from ShaistaRiaz4

Algorithms Analysis.pdf
Algorithms Analysis.pdfAlgorithms Analysis.pdf
Algorithms Analysis.pdfShaistaRiaz4
 
Case Study(Analysis of Algorithm.pdf
Case Study(Analysis of Algorithm.pdfCase Study(Analysis of Algorithm.pdf
Case Study(Analysis of Algorithm.pdfShaistaRiaz4
 
02_Computer-Evolution(1).ppt
02_Computer-Evolution(1).ppt02_Computer-Evolution(1).ppt
02_Computer-Evolution(1).pptShaistaRiaz4
 
01_Introduction.ppt
01_Introduction.ppt01_Introduction.ppt
01_Introduction.pptShaistaRiaz4
 
Algo_Lecture01.pptx
Algo_Lecture01.pptxAlgo_Lecture01.pptx
Algo_Lecture01.pptxShaistaRiaz4
 
02_Computer-Evolution(1).ppt
02_Computer-Evolution(1).ppt02_Computer-Evolution(1).ppt
02_Computer-Evolution(1).pptShaistaRiaz4
 
01_Introduction.ppt
01_Introduction.ppt01_Introduction.ppt
01_Introduction.pptShaistaRiaz4
 
Bisma Zahid (1)-1.pdf
Bisma Zahid (1)-1.pdfBisma Zahid (1)-1.pdf
Bisma Zahid (1)-1.pdfShaistaRiaz4
 
MNS Lecture 1.pptx
MNS Lecture 1.pptxMNS Lecture 1.pptx
MNS Lecture 1.pptxShaistaRiaz4
 
Lecture 3(a) Asymptotic-analysis.pdf
Lecture 3(a) Asymptotic-analysis.pdfLecture 3(a) Asymptotic-analysis.pdf
Lecture 3(a) Asymptotic-analysis.pdfShaistaRiaz4
 
oppositional-defiant-disorder495.pptx
oppositional-defiant-disorder495.pptxoppositional-defiant-disorder495.pptx
oppositional-defiant-disorder495.pptxShaistaRiaz4
 
Development Education.pptx
Development Education.pptxDevelopment Education.pptx
Development Education.pptxShaistaRiaz4
 
WISC-IV Introduction Handout.ppt
WISC-IV Introduction Handout.pptWISC-IV Introduction Handout.ppt
WISC-IV Introduction Handout.pptShaistaRiaz4
 
Summary and Evaluation of the Book.pptx
Summary and Evaluation of the Book.pptxSummary and Evaluation of the Book.pptx
Summary and Evaluation of the Book.pptxShaistaRiaz4
 
MH&PSS for L&NFBED 7-8 April 2020.ppt
MH&PSS for L&NFBED 7-8 April 2020.pptMH&PSS for L&NFBED 7-8 April 2020.ppt
MH&PSS for L&NFBED 7-8 April 2020.pptShaistaRiaz4
 
Intro_to_Literature_2012-2013-1.ppt
Intro_to_Literature_2012-2013-1.pptIntro_to_Literature_2012-2013-1.ppt
Intro_to_Literature_2012-2013-1.pptShaistaRiaz4
 
Coping strategies-Farzana Razi.ppt
Coping strategies-Farzana Razi.pptCoping strategies-Farzana Razi.ppt
Coping strategies-Farzana Razi.pptShaistaRiaz4
 
Intellectual_development.ppt
Intellectual_development.pptIntellectual_development.ppt
Intellectual_development.pptShaistaRiaz4
 

More from ShaistaRiaz4 (20)

Lecture3(b).pdf
Lecture3(b).pdfLecture3(b).pdf
Lecture3(b).pdf
 
Algorithms Analysis.pdf
Algorithms Analysis.pdfAlgorithms Analysis.pdf
Algorithms Analysis.pdf
 
Case Study(Analysis of Algorithm.pdf
Case Study(Analysis of Algorithm.pdfCase Study(Analysis of Algorithm.pdf
Case Study(Analysis of Algorithm.pdf
 
02_Computer-Evolution(1).ppt
02_Computer-Evolution(1).ppt02_Computer-Evolution(1).ppt
02_Computer-Evolution(1).ppt
 
01_Introduction.ppt
01_Introduction.ppt01_Introduction.ppt
01_Introduction.ppt
 
Algo_Lecture01.pptx
Algo_Lecture01.pptxAlgo_Lecture01.pptx
Algo_Lecture01.pptx
 
02_Computer-Evolution(1).ppt
02_Computer-Evolution(1).ppt02_Computer-Evolution(1).ppt
02_Computer-Evolution(1).ppt
 
01_Introduction.ppt
01_Introduction.ppt01_Introduction.ppt
01_Introduction.ppt
 
Bisma Zahid (1)-1.pdf
Bisma Zahid (1)-1.pdfBisma Zahid (1)-1.pdf
Bisma Zahid (1)-1.pdf
 
MNS Lecture 1.pptx
MNS Lecture 1.pptxMNS Lecture 1.pptx
MNS Lecture 1.pptx
 
Plan (2).pptx
Plan (2).pptxPlan (2).pptx
Plan (2).pptx
 
Lecture 3(a) Asymptotic-analysis.pdf
Lecture 3(a) Asymptotic-analysis.pdfLecture 3(a) Asymptotic-analysis.pdf
Lecture 3(a) Asymptotic-analysis.pdf
 
oppositional-defiant-disorder495.pptx
oppositional-defiant-disorder495.pptxoppositional-defiant-disorder495.pptx
oppositional-defiant-disorder495.pptx
 
Development Education.pptx
Development Education.pptxDevelopment Education.pptx
Development Education.pptx
 
WISC-IV Introduction Handout.ppt
WISC-IV Introduction Handout.pptWISC-IV Introduction Handout.ppt
WISC-IV Introduction Handout.ppt
 
Summary and Evaluation of the Book.pptx
Summary and Evaluation of the Book.pptxSummary and Evaluation of the Book.pptx
Summary and Evaluation of the Book.pptx
 
MH&PSS for L&NFBED 7-8 April 2020.ppt
MH&PSS for L&NFBED 7-8 April 2020.pptMH&PSS for L&NFBED 7-8 April 2020.ppt
MH&PSS for L&NFBED 7-8 April 2020.ppt
 
Intro_to_Literature_2012-2013-1.ppt
Intro_to_Literature_2012-2013-1.pptIntro_to_Literature_2012-2013-1.ppt
Intro_to_Literature_2012-2013-1.ppt
 
Coping strategies-Farzana Razi.ppt
Coping strategies-Farzana Razi.pptCoping strategies-Farzana Razi.ppt
Coping strategies-Farzana Razi.ppt
 
Intellectual_development.ppt
Intellectual_development.pptIntellectual_development.ppt
Intellectual_development.ppt
 

Recently uploaded

How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxAnaBeatriceAblay2
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 

Recently uploaded (20)

How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.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)