SlideShare a Scribd company logo
1 of 36
DESIGN AND ANALYSIS
OF ALGORITHMS
DYNAMIC PROGRAMMING TECHNIQUE
• Coin changing problem
• Computing a Binomial Coefficient
• Floyd‘s algorithm
• Multi stage graph
• Optimal Binary Search Trees
• Knapsack Problem and Memory functions.
Contents
Dynamic Programming is a general algorithm design technique
for solving problems defined by recurrences with overlapping
subproblems
• Invented by American mathematician Richard Bellman in the 1950s to
solve optimization problems and later assimilated by CS
• “Programming” here means “planning”
• Main idea:
-set up a recurrence relating a solution to a larger instance to
solutions of some smaller instances
- solve smaller instances once
-record solutions in a table
-extract solution to the initial instance from that table
Dynamic Programming
Computing the nth Fibonacci number using bottom-up iteration and recording
results:
F(0) = 0
F(1) = 1
F(2) = 1+0 = 1
…
F(n-2) =
F(n-1) =
F(n) = F(n-1) + F(n-2)
Efficiency:
- time
- space
Fibonacci numbers
• Input: A list of integers representing coin
denominations, plus another positive integer
representing an amount of money.
• Output: A minimal collection of coins of the given
denominations which sum to the given amount.
• Give change for amount n using the minimum
number of coins of denominations d1<d2 < . . .<dm.
Coin-changing problem
Example : amount n = 6 and coin
denominations 1, 3, and 4.
Coin-changing problem
.
Coin-changing problem
Binomial coefficient, denoted c(n,k), is the number of
combinations k elements from an n-element set (0 ≤ k ≤ n).
The binomial formula is:
(a+b)n = c(n,0)an + …. + c(n,i)a n-i b i + … + c(n,n)bn
c(n,k) = c(n-1,k-1) + c(n-1,k) for n>k>0
and
c(n,0)= c(n,n) = 1
COMPUTING A BINOMIAL COEFFICIENT
c(n,k) = c(n-1,k-1) + c(n-1,k) for n>k>0
and c(n,0)= c(n,n) = 1
COMPUTING A BINOMIAL COEFFICIENT
COMPUTING A BINOMIAL COEFFICIENT
Analysis:
Time efficiency: Θ(nk)
Space efficiency: Θ(nk)
Given n items of
integer weights: w1 w2 … wn
values: v1 v2 … vn
a knapsack of integer capacity W
find most valuable subset of the items that fit into the knapsack
Recursive solution?
What is smaller problem?
How to use solution to smaller in solution to larger
Table?
Order to solve?
Initial conditions?
Knapsack Problem by DP
Example: Knapsack of capacity W = 5
item weight value
1 2 $12
2 1 $10
3 3 $20
4 2 $15
Knapsack Problem by DP (example)
Given n items of
integer weights: w1 w2 … wn
values: v1 v2 … vn
a knapsack of integer capacity W
find most valuable subset of the items that fit into the knapsack
Consider instance defined by first i items and capacity j (j  W).
Let V[i,j] be optimal value of such instance. Then
max {V[i-1,j], vi + V[i-1,j- wi]} if j- wi  0
V[i,j] =
V[i-1,j] if j- wi < 0
Initial conditions: V[0,j] = 0 and V[i,0] = 0
Knapsack Problem by DP
Problem: Given n keys a1 < …< an and probabilities p1 ≤ … ≤
pn
searching for them, find a BST with a minimum
average number of comparisons in successful search.
Since total number of BSTs with n nodes is given by
C(2n,n)/(n+1), which grows exponentially, brute force is
hopeless.
Optimal Binary Search Trees
Example: What is an optimal BST for keys A, B, C, and D with
search probabilities 0.1, 0.2, 0.4, and 0.3,
respectively?
Optimal Binary Search Trees
Example: key A B C D
probability 0.1 0.2 0.4 0.3
B
A
C
D
Optimal BST
Expected number of comparisons for optimal BST:
1*0.4 + 2*(0.2+0.3) + 3*(0.1) = 0.4+1.0+0.3=1.7
Non-optimal BST – Swap A and B:
1*0.4 + 2*(0.1+0.3) + 3*(0.2) = 0.4+0.8+0.6=1.8
Optimal Binary Search Trees
B
A
C
D
Example: key A B C D
probability 0.1 0.2 0.4 0.3
After simplifications, we obtain the recurrence for C[i,j]:
C[i,j] = min {C[i,k-1] + C[k+1,j]} + ∑ ps for 1 ≤ i ≤ j ≤ n
C[i,i] = pi for 1 ≤ i ≤ j ≤ n
DP for Optimal BST Problem (cont.)
goal
0
0
C[i,j]
0
1
n+1
0 1 n
p 1
p2
n
p
i
j
The left table is filled using the recurrence
C[i,j] = min {C[i,k-1] + C[k+1,j]} + ∑ ps , C[i,i] = pi
i ≤ k ≤ j s = i
The right saves the tree roots, which are the k’s that give the
minima
Example: key A B C D
probability 0.1 0.2 0.4 0.3
j
0 1 2 3 4
1 0 .1 .4 1.1 1.7
2 0 .2 .8 1.4
3 0 .4 1.0
4 0 .3
5 0
0 1 2 3 4
1 1 2 3 3
2 2 3 3
3 3 3
4 4
5
B
A
C
D
Contents
Time efficiency: Θ(n3) but can be reduced to Θ(n2) by taking
advantage of monotonicity of entries in the
root table, i.e., R[i,j] is always in the range
between R[i,j-1] and R[i+1,j]
Space efficiency: Θ(n2)
Method can be expanded to include unsuccessful searches
Analysis DP for Optimal BST Problem
Problem: In a weighted (di)graph, find shortest paths between
every pair of vertices
Same idea: construct solution through series of matrices D(0), …,
D (n) using increasing subsets of the vertices allowed
as intermediate
Example:
Floyd’s Algorithm: All pairs shortest paths
3
4
2
1
4
1
6
1
5
3
On the k-th iteration, the algorithm determines shortest paths
between every pair of vertices i, j that use only vertices among
1,…,k as intermediate
D(k)[i,j] = min {D(k-1)[i,j], D(k-1)[i,k] + D(k-1)[k,j]}
Floyd’s Algorithm (matrix generation)
Floyd’s Algorithm (example)
0 ∞ 3 ∞
2 0 ∞ ∞
∞ 7 0 1
6 ∞ ∞ 0
D(0) =
0 ∞ 3 ∞
2 0 5 ∞
∞ 7 0 1
6 ∞ 9 0
D(1) =
0 ∞ 3 ∞
2 0 5 ∞
9 7 0 1
6 ∞ 9 0
D(2) =
0 10 3 4
2 0 5 6
9 7 0 1
6 16 9 0
D(3) =
0 10 3 4
2 0 5 6
7 7 0 1
6 16 9 0
D(4) =
3
1
3
2
6 7
4
1 2
3
1
3
2
6 7
4
1 2
0 ∞ 3 ∞
2 0 ∞ ∞
∞ 7 0 1
6 ∞ ∞ 0
D(0) =
1 2 4
3
1
2
3
4
D(1) =
1 2 4
3
1
2
3
4
D(2) =
1 2 4
3
1
2
3
4
0 10 3 4
2 0 5 6
9 7 0 1
6 16 9 0
D(3) =
Floyd’s Algorithm (pseudocode and analysis)
Time efficiency: Θ(n3)
Space efficiency: Matrices can be written over their predecessors
Note: Shortest paths themselves can be found, too
MULTISTAGE GRAPHS
• A multistage graph G = (V, E)is a directed graph in which
the vertices are partitioned into k ≥ 2 disjoint sets Vi, 1 ≤ i ≤
k.
• If <u,v> is an edge in E, then u∈ Vi and v∈ Vi+1 for some i,
1≤i ≤ k. The set V1 and Vk are such that |V1| = |Vk| = 1.
• Let s and t respectively, be the vertices in V1 and Vk
• The multistage graph problem is to find a minimum
costpath from s to t.
MULTISTAGE GRAPHS
MULTISTAGE GRAPHS ( Forward Approach)
MULTISTAGE GRAPHS (Backward approach)
MULTISTAGE GRAPHS( Forward Approach)
 Dynamic programming approach:
 Cost(1, S) = min{1+Cost(2, A),
2+Cost(2, B), 5+Cost(2, C)}
S T
2
B
A
C
1
5
d(C, T)
d(B, T)
d(A, T)
TRAVELLING SALESMAN PROBLEM
1 2 3 4
1 0 10 15 20
2 5 0 9 10
3 6 13 0 12
4 8 8 9 0
Cost( i, s)=min{Cost(j, s-(j))+d[ i, j]}
S = Φ
Cost(2,Φ,1)=d(2,1)=5
Cost(3,Φ,1)=d(3,1)=6
Cost(4,Φ,1)=d(4,1)=8
S = 1
Cost(2,{3},1)=d[2,3]+Cost(3,Φ,1)=9+6=15
Cost(2,{4},1)=d[2,4]+Cost(4,Φ,1)=10+8=18
Cost(3,{2},1)=d[3,2]+Cost(2,Φ,1)=13+5=18
Cost(3,{4},1)=d[3,4]+Cost(4,Φ,1)=12+8=20
Cost(4,{3},1)=d[4,3]+Cost(3,Φ,1)=9+6=15
Cost(4,{2},1)=d[4,2]+Cost(2,Φ,1)=8+5=13
S = 2
Cost(2,{3,4},1)=min{d[2,3]+Cost(3,{4},1),
d[2,4]+Cost(4,{3},1)}
= min {9+20,10+15} = min{29,25} = 25
Cost(3,{2,4},1)=min{d[3,2]+Cost(2,{4},1),
d[3,4]+Cost(4,{2},1)}
=min {13+18,12+13} = min {31, 25} = 25
Cost(4,{2,3},1)=min{d[4,2]+Cost(2,{3},1),
d[4,3]+Cost(3,{2},1)}
=min {8+15,9+18} = min {23,27} =23
S = 3
Cost(1,{2,3,4},1)=min{ d[1,2]+Cost(2,{3,4},1),
d[1,3]+Cost(3,{2,4},1), d[1,4]+cost(4,{2,3},1)}
=min{10+25, 15+25, 20+23} =
min{35,40,43}=35

More Related Content

Similar to Dynamic Programming.pptx

Sienna 10 dynamic
Sienna 10 dynamicSienna 10 dynamic
Sienna 10 dynamicchidabdu
 
Lp and ip programming cp 9
Lp and ip programming cp 9Lp and ip programming cp 9
Lp and ip programming cp 9M S Prasad
 
DAA - UNIT 4 - Engineering.pptx
DAA - UNIT 4 - Engineering.pptxDAA - UNIT 4 - Engineering.pptx
DAA - UNIT 4 - Engineering.pptxvaishnavi339314
 
parameterized complexity for graph Motif
parameterized complexity for graph Motifparameterized complexity for graph Motif
parameterized complexity for graph MotifAMR koura
 
9 - DynamicProgramming-plus latihan.ppt
9 - DynamicProgramming-plus latihan.ppt9 - DynamicProgramming-plus latihan.ppt
9 - DynamicProgramming-plus latihan.pptKerbauBakar
 
Dynamic1
Dynamic1Dynamic1
Dynamic1MyAlome
 
Divide-and-Conquer & Dynamic ProgrammingDivide-and-Conqu.docx
Divide-and-Conquer & Dynamic ProgrammingDivide-and-Conqu.docxDivide-and-Conquer & Dynamic ProgrammingDivide-and-Conqu.docx
Divide-and-Conquer & Dynamic ProgrammingDivide-and-Conqu.docxjacksnathalie
 
Dynamic Programming Matrix Chain Multiplication
Dynamic Programming Matrix Chain MultiplicationDynamic Programming Matrix Chain Multiplication
Dynamic Programming Matrix Chain MultiplicationKrishnakoumarC
 
Recursion - Computer Algorithms
Recursion - Computer AlgorithmsRecursion - Computer Algorithms
Recursion - Computer AlgorithmsAlaa Al-Makhzoomy
 
Integer_Functions .pdf
Integer_Functions .pdfInteger_Functions .pdf
Integer_Functions .pdfJainggaPotla
 
Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfUnit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfAmayJaiswal4
 
Binomial Theorem
Binomial TheoremBinomial Theorem
Binomial Theoremitutor
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03Krish_ver2
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03Krish_ver2
 
tutorial5.ppt
tutorial5.ppttutorial5.ppt
tutorial5.pptjvjfvvoa
 
20101017 program analysis_for_security_livshits_lecture02_compilers
20101017 program analysis_for_security_livshits_lecture02_compilers20101017 program analysis_for_security_livshits_lecture02_compilers
20101017 program analysis_for_security_livshits_lecture02_compilersComputer Science Club
 
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-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdf
Unit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdfUnit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdf
Unit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdfyashodamb
 

Similar to Dynamic Programming.pptx (20)

Sienna 10 dynamic
Sienna 10 dynamicSienna 10 dynamic
Sienna 10 dynamic
 
Lp and ip programming cp 9
Lp and ip programming cp 9Lp and ip programming cp 9
Lp and ip programming cp 9
 
DAA - UNIT 4 - Engineering.pptx
DAA - UNIT 4 - Engineering.pptxDAA - UNIT 4 - Engineering.pptx
DAA - UNIT 4 - Engineering.pptx
 
parameterized complexity for graph Motif
parameterized complexity for graph Motifparameterized complexity for graph Motif
parameterized complexity for graph Motif
 
Computer science-formulas
Computer science-formulasComputer science-formulas
Computer science-formulas
 
9 - DynamicProgramming-plus latihan.ppt
9 - DynamicProgramming-plus latihan.ppt9 - DynamicProgramming-plus latihan.ppt
9 - DynamicProgramming-plus latihan.ppt
 
Dynamic1
Dynamic1Dynamic1
Dynamic1
 
Divide-and-Conquer & Dynamic ProgrammingDivide-and-Conqu.docx
Divide-and-Conquer & Dynamic ProgrammingDivide-and-Conqu.docxDivide-and-Conquer & Dynamic ProgrammingDivide-and-Conqu.docx
Divide-and-Conquer & Dynamic ProgrammingDivide-and-Conqu.docx
 
Dynamic Programming Matrix Chain Multiplication
Dynamic Programming Matrix Chain MultiplicationDynamic Programming Matrix Chain Multiplication
Dynamic Programming Matrix Chain Multiplication
 
Recursion - Computer Algorithms
Recursion - Computer AlgorithmsRecursion - Computer Algorithms
Recursion - Computer Algorithms
 
dynamic-programming
dynamic-programmingdynamic-programming
dynamic-programming
 
Integer_Functions .pdf
Integer_Functions .pdfInteger_Functions .pdf
Integer_Functions .pdf
 
Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfUnit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdf
 
Binomial Theorem
Binomial TheoremBinomial Theorem
Binomial Theorem
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03
 
5.2 divede and conquer 03
5.2 divede and conquer 035.2 divede and conquer 03
5.2 divede and conquer 03
 
tutorial5.ppt
tutorial5.ppttutorial5.ppt
tutorial5.ppt
 
20101017 program analysis_for_security_livshits_lecture02_compilers
20101017 program analysis_for_security_livshits_lecture02_compilers20101017 program analysis_for_security_livshits_lecture02_compilers
20101017 program analysis_for_security_livshits_lecture02_compilers
 
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-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdf
Unit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdfUnit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdf
Unit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdf
 

Recently uploaded

REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxPooja Bhuva
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptxJoelynRubio1
 
Philosophy of china and it's charactistics
Philosophy of china and it's charactisticsPhilosophy of china and it's charactistics
Philosophy of china and it's charactisticshameyhk98
 
Basic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationBasic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationNeilDeclaro1
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxUmeshTimilsina1
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsNbelano25
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 

Recently uploaded (20)

REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
Philosophy of china and it's charactistics
Philosophy of china and it's charactisticsPhilosophy of china and it's charactistics
Philosophy of china and it's charactistics
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Basic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationBasic Intentional Injuries Health Education
Basic Intentional Injuries Health Education
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 

Dynamic Programming.pptx

  • 1. DESIGN AND ANALYSIS OF ALGORITHMS DYNAMIC PROGRAMMING TECHNIQUE
  • 2. • Coin changing problem • Computing a Binomial Coefficient • Floyd‘s algorithm • Multi stage graph • Optimal Binary Search Trees • Knapsack Problem and Memory functions. Contents
  • 3. Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems • Invented by American mathematician Richard Bellman in the 1950s to solve optimization problems and later assimilated by CS • “Programming” here means “planning” • Main idea: -set up a recurrence relating a solution to a larger instance to solutions of some smaller instances - solve smaller instances once -record solutions in a table -extract solution to the initial instance from that table Dynamic Programming
  • 4. Computing the nth Fibonacci number using bottom-up iteration and recording results: F(0) = 0 F(1) = 1 F(2) = 1+0 = 1 … F(n-2) = F(n-1) = F(n) = F(n-1) + F(n-2) Efficiency: - time - space Fibonacci numbers
  • 5. • Input: A list of integers representing coin denominations, plus another positive integer representing an amount of money. • Output: A minimal collection of coins of the given denominations which sum to the given amount. • Give change for amount n using the minimum number of coins of denominations d1<d2 < . . .<dm. Coin-changing problem
  • 6. Example : amount n = 6 and coin denominations 1, 3, and 4. Coin-changing problem
  • 8. Binomial coefficient, denoted c(n,k), is the number of combinations k elements from an n-element set (0 ≤ k ≤ n). The binomial formula is: (a+b)n = c(n,0)an + …. + c(n,i)a n-i b i + … + c(n,n)bn c(n,k) = c(n-1,k-1) + c(n-1,k) for n>k>0 and c(n,0)= c(n,n) = 1 COMPUTING A BINOMIAL COEFFICIENT
  • 9. c(n,k) = c(n-1,k-1) + c(n-1,k) for n>k>0 and c(n,0)= c(n,n) = 1 COMPUTING A BINOMIAL COEFFICIENT
  • 10. COMPUTING A BINOMIAL COEFFICIENT Analysis: Time efficiency: Θ(nk) Space efficiency: Θ(nk)
  • 11. Given n items of integer weights: w1 w2 … wn values: v1 v2 … vn a knapsack of integer capacity W find most valuable subset of the items that fit into the knapsack Recursive solution? What is smaller problem? How to use solution to smaller in solution to larger Table? Order to solve? Initial conditions? Knapsack Problem by DP
  • 12. Example: Knapsack of capacity W = 5 item weight value 1 2 $12 2 1 $10 3 3 $20 4 2 $15 Knapsack Problem by DP (example)
  • 13. Given n items of integer weights: w1 w2 … wn values: v1 v2 … vn a knapsack of integer capacity W find most valuable subset of the items that fit into the knapsack Consider instance defined by first i items and capacity j (j  W). Let V[i,j] be optimal value of such instance. Then max {V[i-1,j], vi + V[i-1,j- wi]} if j- wi  0 V[i,j] = V[i-1,j] if j- wi < 0 Initial conditions: V[0,j] = 0 and V[i,0] = 0 Knapsack Problem by DP
  • 14. Problem: Given n keys a1 < …< an and probabilities p1 ≤ … ≤ pn searching for them, find a BST with a minimum average number of comparisons in successful search. Since total number of BSTs with n nodes is given by C(2n,n)/(n+1), which grows exponentially, brute force is hopeless. Optimal Binary Search Trees
  • 15. Example: What is an optimal BST for keys A, B, C, and D with search probabilities 0.1, 0.2, 0.4, and 0.3, respectively? Optimal Binary Search Trees Example: key A B C D probability 0.1 0.2 0.4 0.3 B A C D Optimal BST
  • 16. Expected number of comparisons for optimal BST: 1*0.4 + 2*(0.2+0.3) + 3*(0.1) = 0.4+1.0+0.3=1.7 Non-optimal BST – Swap A and B: 1*0.4 + 2*(0.1+0.3) + 3*(0.2) = 0.4+0.8+0.6=1.8 Optimal Binary Search Trees B A C D Example: key A B C D probability 0.1 0.2 0.4 0.3
  • 17. After simplifications, we obtain the recurrence for C[i,j]: C[i,j] = min {C[i,k-1] + C[k+1,j]} + ∑ ps for 1 ≤ i ≤ j ≤ n C[i,i] = pi for 1 ≤ i ≤ j ≤ n DP for Optimal BST Problem (cont.) goal 0 0 C[i,j] 0 1 n+1 0 1 n p 1 p2 n p i j
  • 18. The left table is filled using the recurrence C[i,j] = min {C[i,k-1] + C[k+1,j]} + ∑ ps , C[i,i] = pi i ≤ k ≤ j s = i The right saves the tree roots, which are the k’s that give the minima Example: key A B C D probability 0.1 0.2 0.4 0.3 j 0 1 2 3 4 1 0 .1 .4 1.1 1.7 2 0 .2 .8 1.4 3 0 .4 1.0 4 0 .3 5 0 0 1 2 3 4 1 1 2 3 3 2 2 3 3 3 3 3 4 4 5 B A C D
  • 20. Time efficiency: Θ(n3) but can be reduced to Θ(n2) by taking advantage of monotonicity of entries in the root table, i.e., R[i,j] is always in the range between R[i,j-1] and R[i+1,j] Space efficiency: Θ(n2) Method can be expanded to include unsuccessful searches Analysis DP for Optimal BST Problem
  • 21. Problem: In a weighted (di)graph, find shortest paths between every pair of vertices Same idea: construct solution through series of matrices D(0), …, D (n) using increasing subsets of the vertices allowed as intermediate Example: Floyd’s Algorithm: All pairs shortest paths 3 4 2 1 4 1 6 1 5 3
  • 22.
  • 23. On the k-th iteration, the algorithm determines shortest paths between every pair of vertices i, j that use only vertices among 1,…,k as intermediate D(k)[i,j] = min {D(k-1)[i,j], D(k-1)[i,k] + D(k-1)[k,j]} Floyd’s Algorithm (matrix generation)
  • 24. Floyd’s Algorithm (example) 0 ∞ 3 ∞ 2 0 ∞ ∞ ∞ 7 0 1 6 ∞ ∞ 0 D(0) = 0 ∞ 3 ∞ 2 0 5 ∞ ∞ 7 0 1 6 ∞ 9 0 D(1) = 0 ∞ 3 ∞ 2 0 5 ∞ 9 7 0 1 6 ∞ 9 0 D(2) = 0 10 3 4 2 0 5 6 9 7 0 1 6 16 9 0 D(3) = 0 10 3 4 2 0 5 6 7 7 0 1 6 16 9 0 D(4) = 3 1 3 2 6 7 4 1 2
  • 25. 3 1 3 2 6 7 4 1 2 0 ∞ 3 ∞ 2 0 ∞ ∞ ∞ 7 0 1 6 ∞ ∞ 0 D(0) = 1 2 4 3 1 2 3 4 D(1) = 1 2 4 3 1 2 3 4 D(2) = 1 2 4 3 1 2 3 4 0 10 3 4 2 0 5 6 9 7 0 1 6 16 9 0 D(3) =
  • 26. Floyd’s Algorithm (pseudocode and analysis) Time efficiency: Θ(n3) Space efficiency: Matrices can be written over their predecessors Note: Shortest paths themselves can be found, too
  • 27. MULTISTAGE GRAPHS • A multistage graph G = (V, E)is a directed graph in which the vertices are partitioned into k ≥ 2 disjoint sets Vi, 1 ≤ i ≤ k. • If <u,v> is an edge in E, then u∈ Vi and v∈ Vi+1 for some i, 1≤i ≤ k. The set V1 and Vk are such that |V1| = |Vk| = 1. • Let s and t respectively, be the vertices in V1 and Vk • The multistage graph problem is to find a minimum costpath from s to t.
  • 29.
  • 30.
  • 31. MULTISTAGE GRAPHS ( Forward Approach)
  • 33. MULTISTAGE GRAPHS( Forward Approach)  Dynamic programming approach:  Cost(1, S) = min{1+Cost(2, A), 2+Cost(2, B), 5+Cost(2, C)} S T 2 B A C 1 5 d(C, T) d(B, T) d(A, T)
  • 34. TRAVELLING SALESMAN PROBLEM 1 2 3 4 1 0 10 15 20 2 5 0 9 10 3 6 13 0 12 4 8 8 9 0 Cost( i, s)=min{Cost(j, s-(j))+d[ i, j]}
  • 35. S = Φ Cost(2,Φ,1)=d(2,1)=5 Cost(3,Φ,1)=d(3,1)=6 Cost(4,Φ,1)=d(4,1)=8 S = 1 Cost(2,{3},1)=d[2,3]+Cost(3,Φ,1)=9+6=15 Cost(2,{4},1)=d[2,4]+Cost(4,Φ,1)=10+8=18 Cost(3,{2},1)=d[3,2]+Cost(2,Φ,1)=13+5=18 Cost(3,{4},1)=d[3,4]+Cost(4,Φ,1)=12+8=20 Cost(4,{3},1)=d[4,3]+Cost(3,Φ,1)=9+6=15 Cost(4,{2},1)=d[4,2]+Cost(2,Φ,1)=8+5=13 S = 2 Cost(2,{3,4},1)=min{d[2,3]+Cost(3,{4},1), d[2,4]+Cost(4,{3},1)} = min {9+20,10+15} = min{29,25} = 25
  • 36. Cost(3,{2,4},1)=min{d[3,2]+Cost(2,{4},1), d[3,4]+Cost(4,{2},1)} =min {13+18,12+13} = min {31, 25} = 25 Cost(4,{2,3},1)=min{d[4,2]+Cost(2,{3},1), d[4,3]+Cost(3,{2},1)} =min {8+15,9+18} = min {23,27} =23 S = 3 Cost(1,{2,3,4},1)=min{ d[1,2]+Cost(2,{3,4},1), d[1,3]+Cost(3,{2,4},1), d[1,4]+cost(4,{2,3},1)} =min{10+25, 15+25, 20+23} = min{35,40,43}=35