SlideShare a Scribd company logo
1 of 29
DESIGN & ANALYSIS OF ALGORITHM
13 – DYNAMIC PROGRAMMING
Informatics Department
Parahyangan Catholic University
INTRODUCTION
 We have seen some algorithm design principles,
such as divide-and-conquer, brute force, and
greedy
 Brute force is widely applicable, but inefficient
 Divide-and-conquer and greedy are fast, but only
applicable on very specific problems.
 Dynamic Programming is somewhere in between
them, while still providing polynomial time
complexity, it is widely applicable.
DYNAMIC PROGRAMMING (DP)
 Similar to divide-and-conquer, DP solves problem
by combining the solutions of its sub-problems.
 The term “programming” here refers to a tabular
method
 DP solves a sub-problem, then saves its solution
in a table
FINDING N-TH FIBONACCI NUMBER
0 n=0
F(n) = 1 n=1
F(n-1) + F(n-2) n>1
FIBONACCI (n)
if (n==0)
return 0
else if (n==1)
return 1
else
return FIBONACCI(n-1) + FIBONACCI(n-2)
Recursive solution :
FINDING N-TH FIBONACCI NUMBER
F(3) F(2)
F(1)
F(2) F(0)
F(1)
F(0)
F(1)
F(1)
F(2)
F(0)
F(1)
F(5)
F(4) F(3)
Recursive solution :
FINDING N-TH FIBONACCI NUMBER
Memoization :
Maintain a table to store sub-problem’s solution
// Initially : Arr[0] = 0
// Arr[1] = 1
// Arr[2..n] = -1
FIBONACCI (n)
if (Arr[n] != -1)
return Arr[n]
else
Arr[n]=FIBONACCI(n-1) + FIBONACCI(n-2)
return Arr[n]
solution with memoization:
FINDING N-TH FIBONACCI NUMBER
F(3) F(2)
F(1)
F(2)
F(0)
F(1)
F(5)
F(4) F(3)
solution with memoization:
n 0 1 2 3 4 5
F(n) 0 1 -1 -1 -1 -1
1 2 3 5
=1
=2
FINDING N-TH FIBONACCI NUMBER
Bottom-up solution :
Use the natural ordering of sub-problems, solve them
one-by-one starting from the “smallest” one
FIBONACCI (n)
Arr[0] = 0
Arr[1] = 1
if(n>1)
for i=2 to n do
Arr[i] = Arr[i-1] + Arr[i-2]
return Arr[n]
Bottom-up solution:
TIME COMPLEXITY
FIBONACCI (n)
if (n==0)
return 0
else if (n==1)
return 1
else
return FIBONACCI(n-1) + FIBONACCI(n-2)
Recursive solution :
Every instance has ≤ 2
recursive calls
F(n-2) F(n-3) F(n-4)
F(n-3)
F(n)
F(n-1) F(n-2)
Height
=
n
Therefore, time
complexity is
O(2n)
TIME COMPLEXITY
FIBONACCI (n)
Arr[0] = 0
Arr[1] = 1
if(n>1)
for i=2 to n do
Arr[i] = Arr[i-1] + Arr[i-2]
return Arr[n]
Bottom-up solution:
There is a loop that iterates ≤ n times, each doing
a constant amount of work.
So the time complexity is O(n)
ROD CUTTING PROBLEM
Serling Enterprises buys long steel rods and cuts them into
shorter rods, which it then sells. Each cut is free, however
different rod length sells for different price. The management
of Sterling Enterprises wants to know the best way to cut up
the rods.
We assume that we know :
length 1 2 3 4 5 6 7 8 9 10
price 1 5 8 9 10 17 17 20 24 30
Example : n=4
(n-1) possible cut locations  2n-1 ways of cutting
ROD CUTTING PROBLEM
Example : n=4
length 1 2 3 4 5 6 7 8 9 10
price 1 5 8 9 10 17 17 20 24 30
9
=9
1 8
=9
5 5
=10
1
8
=9
1 1 5
=7
1 1
5
=7
1 1
5
=7
1 1 1 1
=4
BEST
ROD CUTTING PROBLEM
 Consider a rod of length n, and we cut a rod of
length i
 Then we left with a rod of length n-i
 Naturally, we want to optimize the selling price
of the remaining rod
n
i
1
ROD CUTTING PROBLEM
ROD-CUTTING (n)
if (n==0)
return 0
else
best = -∞
for i=1 to n do
best = MAX(best, price[i]+ROD-CUTTING(n-i))
return best
Recursive solution :
ROD CUTTING PROBLEM
Recursive solution :
RC(4)
RC(3)
RC(2) RC(1) RC(0) RC(1) RC(0) RC(0)
RC(1) RC(0)
RC(0)
RC(0)
RC(2) RC(1)
RC(0)
RC(0)
Same problem as recursive Fibonacci
ROD CUTTING PROBLEM
// Initially : Arr[0] = 0
// Arr[1..n] = -∞
ROD-CUTTING (n)
if Arr[n] ≥ 0
return Arr[n]
else
best = -∞
for i=1 to n do
best = MAX(best, price[i]+ROD-CUTTING(n-i))
Arr[n] = best
return best
Solution with memoization:
Exercise
Write a bottom-up solution for Rod Cutting problem !
TIME COMPLEXITY
ROD-CUTTING (n)
if (n==0)
return 0
else
best = -∞
for i=1 to n do
best = MAX(best, price[i]+ROD-CUTTING(n-i))
return best
Recursive solution :
Every instance has ≤ n recursive calls, and the
depth of the recursive tree is O(n).
So the time complexity is O(nn)
What is the time complexity for
the bottom-up solution ?
SHORTEST PATH IN DAG
 DAG = Directed Acyclic Graph
 Example :
S
A
C
B
D
E
2
1
1
1 2
4
6
3
SHORTEST PATH IN DAG
S A
C B D E
S
A
C
B
D
E
2
1
1
1 2
4
6
3
2 1 1
1 2
4 6
3
Sort using topological sort
SHORTEST PATH IN DAG
S A
C B D E
2 1 1
1 2
4 6
3
 Starting from S, suppose we want to reach node D
 The only way to reach D is either through B or C
dist(D) = MIN(dist(B)+1 , dist(C)+3)
SHORTEST PATH IN DAG
 A similar relation can be written for every node.
 As we have seen before, it is best to use bottom-up
way of computing dist, that is from “left most”
node to “right most” node
DAG-SHORTEST-PATH ()
initialize all dist[.] to ∞
dist[S] = 0
for each vertex v except S in left-to-right order do
for each edge (u,v) do
dist[v] = MIN(dist[v] , dist[u] + weight(u,v))
DYNAMIC PROGRAMMING
 DAG’s shortest path is a very general technique. We model
many other problems into DAG problem.
 Example #1: Fibonacci
 Example #2 : Rod Cutting
0 2
1 3 4
0
0 2
1 3 4 5
0 1
PROPERTIES OF DYNAMIC PROGRAMMING
 Problem can be divided into smaller
sub-problems
 Optimal substructure:
an optimal solution to the problem contains within
its optimal solutions to sub-problems
 Overlapping sub-problems:
the space of sub-problems is “small”, in the sense
that a recursive algorithm for the problem solves
the same sub-problems over and over, rather than
always generating new sub-problems
DYNAMIC PROGRAMMING
V.S. DIVIDE-AND-CONQUER
 Divide-and-Conquer
 Dynamic Programming
And so on…
LONGEST INCREASING SUBSEQUENCE
Given a sequence of numbers a1, a2, a3, … , an. A
subsequence is any subset of these numbers taken in
order of the form ai1, ai2, ai3…, aik, where 1 ≤ i1 < i2 <
… < ik ≤ n, and an increasing subsequence is one
which the numbers are getting strictly larger. The
task is to find the increasing subsequence of greatest
length.
Example: 5, 2, 8, 6, 3, 6, 9, 7
LONGEST INCREASING SUBSEQUENCE
How do we model this problem into a DAG ?
any number ai can precede aj iff ai < aj
5 8
2 6 3 6 9 7
Consider this number,
LIS that ends here must be either:
• subsequence consist of “6” alone
• LIS that ends at “5” + “6”
• LIS that ends at “2” + “6”
• LIS that ends at “”3” + “6”
LONGEST INCREASING SUBSEQUENCE
 How do we find the LIS of sequence
5, 2, 8, 6, 3, 6, 9, 7 ?
 Does the optimal solution always ends at “7” ?
// Initially L[1..n] = 1
LONGEST-INCREASING-SUBSEQUENCE ()
for j=2 to n do
for each i<j such that ai < aj do
if(L[j] < 1 + L[i]) then
L[j] = 1 + L[i]
return maximum of L[1..n]
This algorithm only gives the sequence’s length.
How do we find the actual sequence ?
RECONSTRUCTING A SOLUTION
 We can extend the dynamic programming
approach to record not only the optimal value for
each sub-problem, but also the choice that led to
that value
// Initially L[1..n] = 1
// Initially prev[1..n] = 0
LONGEST-INCREASING-SUBSEQUENCE ()
for j=2 to n do
for each i<j such that ai < aj do
if(L[j] < 1 + L[i]) then
L[j] = 1 + L[i]
prev[j] = i
return maximum of L[1..n] and array prev
EXERCISE : YUCKDONALD’S
Yuckdonald’s is considering opening a series of restaurants
along Quaint Valley Highway (QVH). The n possible locations
are along a straight line, and the distance of these locations
from the start of QVH are, in miles and in increasing order, m1,
m2, …, mn. The constraints are as follows:
 At each location, Yuckdonald’s may open at most one restaurant. The
expected profit from opening a restaurant at location i is pi > 0
 Any two restaurants should be at least k miles apart, where k is a
positive integer

More Related Content

Similar to dynamic-programming

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
 
Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfUnit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfAmayJaiswal4
 
Dynamic programming - fundamentals review
Dynamic programming - fundamentals reviewDynamic programming - fundamentals review
Dynamic programming - fundamentals reviewElifTech
 
01 - DAA - PPT.pptx
01 - DAA - PPT.pptx01 - DAA - PPT.pptx
01 - DAA - PPT.pptxKokilaK25
 
dynamic programming complete by Mumtaz Ali (03154103173)
dynamic programming complete by Mumtaz Ali (03154103173)dynamic programming complete by Mumtaz Ali (03154103173)
dynamic programming complete by Mumtaz Ali (03154103173)Mumtaz Ali
 
Ch3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhh
Ch3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhhCh3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhh
Ch3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhhdanielgetachew0922
 
270-102-divide-and-conquer_handout.pdfCS 270Algorithm.docx
270-102-divide-and-conquer_handout.pdfCS 270Algorithm.docx270-102-divide-and-conquer_handout.pdfCS 270Algorithm.docx
270-102-divide-and-conquer_handout.pdfCS 270Algorithm.docxeugeniadean34240
 
UNIT-II.pptx
UNIT-II.pptxUNIT-II.pptx
UNIT-II.pptxJyoReddy9
 
Design and Implementation of Parallel and Randomized Approximation Algorithms
Design and Implementation of Parallel and Randomized Approximation AlgorithmsDesign and Implementation of Parallel and Randomized Approximation Algorithms
Design and Implementation of Parallel and Randomized Approximation AlgorithmsAjay Bidyarthy
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1Amrinder Arora
 
5.3 dynamic programming 03
5.3 dynamic programming 035.3 dynamic programming 03
5.3 dynamic programming 03Krish_ver2
 
Dynamic1
Dynamic1Dynamic1
Dynamic1MyAlome
 
DAA - UNIT 4 - Engineering.pptx
DAA - UNIT 4 - Engineering.pptxDAA - UNIT 4 - Engineering.pptx
DAA - UNIT 4 - Engineering.pptxvaishnavi339314
 
ALGORITHMS - SHORT NOTES
ALGORITHMS - SHORT NOTESALGORITHMS - SHORT NOTES
ALGORITHMS - SHORT NOTESsuthi
 

Similar to dynamic-programming (20)

Time complexity.ppt
Time complexity.pptTime complexity.ppt
Time complexity.ppt
 
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
 
Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfUnit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdf
 
Dynamic programming - fundamentals review
Dynamic programming - fundamentals reviewDynamic programming - fundamentals review
Dynamic programming - fundamentals review
 
01 - DAA - PPT.pptx
01 - DAA - PPT.pptx01 - DAA - PPT.pptx
01 - DAA - PPT.pptx
 
dynamic programming complete by Mumtaz Ali (03154103173)
dynamic programming complete by Mumtaz Ali (03154103173)dynamic programming complete by Mumtaz Ali (03154103173)
dynamic programming complete by Mumtaz Ali (03154103173)
 
Ch3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhh
Ch3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhhCh3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhh
Ch3(1).pptxbbbbbbbbbbbbbbbbbbbhhhhhhhhhh
 
270-102-divide-and-conquer_handout.pdfCS 270Algorithm.docx
270-102-divide-and-conquer_handout.pdfCS 270Algorithm.docx270-102-divide-and-conquer_handout.pdfCS 270Algorithm.docx
270-102-divide-and-conquer_handout.pdfCS 270Algorithm.docx
 
Unit 3
Unit 3Unit 3
Unit 3
 
Unit 3
Unit 3Unit 3
Unit 3
 
Recursion in Java
Recursion in JavaRecursion in Java
Recursion in Java
 
UNIT-II.pptx
UNIT-II.pptxUNIT-II.pptx
UNIT-II.pptx
 
Design and Implementation of Parallel and Randomized Approximation Algorithms
Design and Implementation of Parallel and Randomized Approximation AlgorithmsDesign and Implementation of Parallel and Randomized Approximation Algorithms
Design and Implementation of Parallel and Randomized Approximation Algorithms
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1
 
Daa chapter 2
Daa chapter 2Daa chapter 2
Daa chapter 2
 
Daa chapter 3
Daa chapter 3Daa chapter 3
Daa chapter 3
 
5.3 dynamic programming 03
5.3 dynamic programming 035.3 dynamic programming 03
5.3 dynamic programming 03
 
Dynamic1
Dynamic1Dynamic1
Dynamic1
 
DAA - UNIT 4 - Engineering.pptx
DAA - UNIT 4 - Engineering.pptxDAA - UNIT 4 - Engineering.pptx
DAA - UNIT 4 - Engineering.pptx
 
ALGORITHMS - SHORT NOTES
ALGORITHMS - SHORT NOTESALGORITHMS - SHORT NOTES
ALGORITHMS - SHORT NOTES
 

More from MuhammadSheraz836877 (20)

Quick & Merge Sort.ppt
Quick & Merge Sort.pptQuick & Merge Sort.ppt
Quick & Merge Sort.ppt
 
Bubble Sort.ppt
Bubble Sort.pptBubble Sort.ppt
Bubble Sort.ppt
 
CHAPTER 1 BASIC sql STATEMENTS.pptx
CHAPTER 1 BASIC sql STATEMENTS.pptxCHAPTER 1 BASIC sql STATEMENTS.pptx
CHAPTER 1 BASIC sql STATEMENTS.pptx
 
Sortings .pptx
Sortings .pptxSortings .pptx
Sortings .pptx
 
Lecture9_StackQueue.ppt
Lecture9_StackQueue.pptLecture9_StackQueue.ppt
Lecture9_StackQueue.ppt
 
Articles Eng.ppt
Articles Eng.pptArticles Eng.ppt
Articles Eng.ppt
 
FORMS OF MATTER.pptx
FORMS OF MATTER.pptxFORMS OF MATTER.pptx
FORMS OF MATTER.pptx
 
Bonds of solids.pptx
Bonds of solids.pptxBonds of solids.pptx
Bonds of solids.pptx
 
Infinitives_by_M._Sheraz.PPT
Infinitives_by_M._Sheraz.PPTInfinitives_by_M._Sheraz.PPT
Infinitives_by_M._Sheraz.PPT
 
articles.ppt
articles.pptarticles.ppt
articles.ppt
 
Speakingskills. Ppt
Speakingskills. PptSpeakingskills. Ppt
Speakingskills. Ppt
 
speakingskills. Ppt
speakingskills. Pptspeakingskills. Ppt
speakingskills. Ppt
 
voice-techniques. Ppt
voice-techniques. Pptvoice-techniques. Ppt
voice-techniques. Ppt
 
body language. Ppt
body language. Pptbody language. Ppt
body language. Ppt
 
latchesandflipflops.ppt
latchesandflipflops.pptlatchesandflipflops.ppt
latchesandflipflops.ppt
 
listeningskills.ppt
listeningskills.pptlisteningskills.ppt
listeningskills.ppt
 
listeningskills.ppt
listeningskills.pptlisteningskills.ppt
listeningskills.ppt
 
listeningskills.ppt
listeningskills.pptlisteningskills.ppt
listeningskills.ppt
 
Business Econimics.ppt
Business Econimics.pptBusiness Econimics.ppt
Business Econimics.ppt
 
latchesflip-flop DLD
latchesflip-flop DLDlatchesflip-flop DLD
latchesflip-flop DLD
 

Recently uploaded

Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
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
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
MICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxMICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxabhijeetpadhi001
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 
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
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
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
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
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
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
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
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 

Recently uploaded (20)

Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
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
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
MICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxMICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptx
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 
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
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
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
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
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
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 

dynamic-programming

  • 1. DESIGN & ANALYSIS OF ALGORITHM 13 – DYNAMIC PROGRAMMING Informatics Department Parahyangan Catholic University
  • 2. INTRODUCTION  We have seen some algorithm design principles, such as divide-and-conquer, brute force, and greedy  Brute force is widely applicable, but inefficient  Divide-and-conquer and greedy are fast, but only applicable on very specific problems.  Dynamic Programming is somewhere in between them, while still providing polynomial time complexity, it is widely applicable.
  • 3. DYNAMIC PROGRAMMING (DP)  Similar to divide-and-conquer, DP solves problem by combining the solutions of its sub-problems.  The term “programming” here refers to a tabular method  DP solves a sub-problem, then saves its solution in a table
  • 4. FINDING N-TH FIBONACCI NUMBER 0 n=0 F(n) = 1 n=1 F(n-1) + F(n-2) n>1 FIBONACCI (n) if (n==0) return 0 else if (n==1) return 1 else return FIBONACCI(n-1) + FIBONACCI(n-2) Recursive solution :
  • 5. FINDING N-TH FIBONACCI NUMBER F(3) F(2) F(1) F(2) F(0) F(1) F(0) F(1) F(1) F(2) F(0) F(1) F(5) F(4) F(3) Recursive solution :
  • 6. FINDING N-TH FIBONACCI NUMBER Memoization : Maintain a table to store sub-problem’s solution // Initially : Arr[0] = 0 // Arr[1] = 1 // Arr[2..n] = -1 FIBONACCI (n) if (Arr[n] != -1) return Arr[n] else Arr[n]=FIBONACCI(n-1) + FIBONACCI(n-2) return Arr[n] solution with memoization:
  • 7. FINDING N-TH FIBONACCI NUMBER F(3) F(2) F(1) F(2) F(0) F(1) F(5) F(4) F(3) solution with memoization: n 0 1 2 3 4 5 F(n) 0 1 -1 -1 -1 -1 1 2 3 5 =1 =2
  • 8. FINDING N-TH FIBONACCI NUMBER Bottom-up solution : Use the natural ordering of sub-problems, solve them one-by-one starting from the “smallest” one FIBONACCI (n) Arr[0] = 0 Arr[1] = 1 if(n>1) for i=2 to n do Arr[i] = Arr[i-1] + Arr[i-2] return Arr[n] Bottom-up solution:
  • 9. TIME COMPLEXITY FIBONACCI (n) if (n==0) return 0 else if (n==1) return 1 else return FIBONACCI(n-1) + FIBONACCI(n-2) Recursive solution : Every instance has ≤ 2 recursive calls F(n-2) F(n-3) F(n-4) F(n-3) F(n) F(n-1) F(n-2) Height = n Therefore, time complexity is O(2n)
  • 10. TIME COMPLEXITY FIBONACCI (n) Arr[0] = 0 Arr[1] = 1 if(n>1) for i=2 to n do Arr[i] = Arr[i-1] + Arr[i-2] return Arr[n] Bottom-up solution: There is a loop that iterates ≤ n times, each doing a constant amount of work. So the time complexity is O(n)
  • 11. ROD CUTTING PROBLEM Serling Enterprises buys long steel rods and cuts them into shorter rods, which it then sells. Each cut is free, however different rod length sells for different price. The management of Sterling Enterprises wants to know the best way to cut up the rods. We assume that we know : length 1 2 3 4 5 6 7 8 9 10 price 1 5 8 9 10 17 17 20 24 30 Example : n=4 (n-1) possible cut locations  2n-1 ways of cutting
  • 12. ROD CUTTING PROBLEM Example : n=4 length 1 2 3 4 5 6 7 8 9 10 price 1 5 8 9 10 17 17 20 24 30 9 =9 1 8 =9 5 5 =10 1 8 =9 1 1 5 =7 1 1 5 =7 1 1 5 =7 1 1 1 1 =4 BEST
  • 13. ROD CUTTING PROBLEM  Consider a rod of length n, and we cut a rod of length i  Then we left with a rod of length n-i  Naturally, we want to optimize the selling price of the remaining rod n i 1
  • 14. ROD CUTTING PROBLEM ROD-CUTTING (n) if (n==0) return 0 else best = -∞ for i=1 to n do best = MAX(best, price[i]+ROD-CUTTING(n-i)) return best Recursive solution :
  • 15. ROD CUTTING PROBLEM Recursive solution : RC(4) RC(3) RC(2) RC(1) RC(0) RC(1) RC(0) RC(0) RC(1) RC(0) RC(0) RC(0) RC(2) RC(1) RC(0) RC(0) Same problem as recursive Fibonacci
  • 16. ROD CUTTING PROBLEM // Initially : Arr[0] = 0 // Arr[1..n] = -∞ ROD-CUTTING (n) if Arr[n] ≥ 0 return Arr[n] else best = -∞ for i=1 to n do best = MAX(best, price[i]+ROD-CUTTING(n-i)) Arr[n] = best return best Solution with memoization: Exercise Write a bottom-up solution for Rod Cutting problem !
  • 17. TIME COMPLEXITY ROD-CUTTING (n) if (n==0) return 0 else best = -∞ for i=1 to n do best = MAX(best, price[i]+ROD-CUTTING(n-i)) return best Recursive solution : Every instance has ≤ n recursive calls, and the depth of the recursive tree is O(n). So the time complexity is O(nn) What is the time complexity for the bottom-up solution ?
  • 18. SHORTEST PATH IN DAG  DAG = Directed Acyclic Graph  Example : S A C B D E 2 1 1 1 2 4 6 3
  • 19. SHORTEST PATH IN DAG S A C B D E S A C B D E 2 1 1 1 2 4 6 3 2 1 1 1 2 4 6 3 Sort using topological sort
  • 20. SHORTEST PATH IN DAG S A C B D E 2 1 1 1 2 4 6 3  Starting from S, suppose we want to reach node D  The only way to reach D is either through B or C dist(D) = MIN(dist(B)+1 , dist(C)+3)
  • 21. SHORTEST PATH IN DAG  A similar relation can be written for every node.  As we have seen before, it is best to use bottom-up way of computing dist, that is from “left most” node to “right most” node DAG-SHORTEST-PATH () initialize all dist[.] to ∞ dist[S] = 0 for each vertex v except S in left-to-right order do for each edge (u,v) do dist[v] = MIN(dist[v] , dist[u] + weight(u,v))
  • 22. DYNAMIC PROGRAMMING  DAG’s shortest path is a very general technique. We model many other problems into DAG problem.  Example #1: Fibonacci  Example #2 : Rod Cutting 0 2 1 3 4 0 0 2 1 3 4 5 0 1
  • 23. PROPERTIES OF DYNAMIC PROGRAMMING  Problem can be divided into smaller sub-problems  Optimal substructure: an optimal solution to the problem contains within its optimal solutions to sub-problems  Overlapping sub-problems: the space of sub-problems is “small”, in the sense that a recursive algorithm for the problem solves the same sub-problems over and over, rather than always generating new sub-problems
  • 24. DYNAMIC PROGRAMMING V.S. DIVIDE-AND-CONQUER  Divide-and-Conquer  Dynamic Programming And so on…
  • 25. LONGEST INCREASING SUBSEQUENCE Given a sequence of numbers a1, a2, a3, … , an. A subsequence is any subset of these numbers taken in order of the form ai1, ai2, ai3…, aik, where 1 ≤ i1 < i2 < … < ik ≤ n, and an increasing subsequence is one which the numbers are getting strictly larger. The task is to find the increasing subsequence of greatest length. Example: 5, 2, 8, 6, 3, 6, 9, 7
  • 26. LONGEST INCREASING SUBSEQUENCE How do we model this problem into a DAG ? any number ai can precede aj iff ai < aj 5 8 2 6 3 6 9 7 Consider this number, LIS that ends here must be either: • subsequence consist of “6” alone • LIS that ends at “5” + “6” • LIS that ends at “2” + “6” • LIS that ends at “”3” + “6”
  • 27. LONGEST INCREASING SUBSEQUENCE  How do we find the LIS of sequence 5, 2, 8, 6, 3, 6, 9, 7 ?  Does the optimal solution always ends at “7” ? // Initially L[1..n] = 1 LONGEST-INCREASING-SUBSEQUENCE () for j=2 to n do for each i<j such that ai < aj do if(L[j] < 1 + L[i]) then L[j] = 1 + L[i] return maximum of L[1..n] This algorithm only gives the sequence’s length. How do we find the actual sequence ?
  • 28. RECONSTRUCTING A SOLUTION  We can extend the dynamic programming approach to record not only the optimal value for each sub-problem, but also the choice that led to that value // Initially L[1..n] = 1 // Initially prev[1..n] = 0 LONGEST-INCREASING-SUBSEQUENCE () for j=2 to n do for each i<j such that ai < aj do if(L[j] < 1 + L[i]) then L[j] = 1 + L[i] prev[j] = i return maximum of L[1..n] and array prev
  • 29. EXERCISE : YUCKDONALD’S Yuckdonald’s is considering opening a series of restaurants along Quaint Valley Highway (QVH). The n possible locations are along a straight line, and the distance of these locations from the start of QVH are, in miles and in increasing order, m1, m2, …, mn. The constraints are as follows:  At each location, Yuckdonald’s may open at most one restaurant. The expected profit from opening a restaurant at location i is pi > 0  Any two restaurants should be at least k miles apart, where k is a positive integer