SlideShare a Scribd company logo
Dynamic Programming
1
2
Dynamic Programming (DP)
• Seperti Divide and Conquer, memecahkan
masalah dengan mengkombinasikan solusi-solusi
dari subproblem
• Perbedaan antara divide-and-conquer dan DP:
– Pada DC sub-problems independen, penyelesaian
masalah secara independen dan secara rekursif,
(sehingga beberapa sub(sub)problems diselesaikan
berulang kali)
– Pada DP sub-problem saling bergantung, i.e., sub-
problems share sub-sub-problems, tiap
sub(sub)problem hanya diselesaikan sekali, solusi
sub(sub)problems disimpan di sebuah table dan
digunakan untuk menyelesaikan sub problem yang
levelnya lebih tinggi
Example: Fibonacci numbers
• Definisi dari bilangan Fibonacci:
F(n) = F(n-1) + F(n-2)
F(0) = 0
F(1) = 1
• Menghitung bilangan Fibonaci ke-n secara rekursif (top-
down):
F(n)
F(n-1) + F(n-2)
F(n-2) + F(n-3) F(n-3) + F(n-4)
...
Top down approach + memoization
• Always remember the past…
4
5
•fibb(0) = 0, fibb(1) = 1; fibb(n) = fibb(n-1) + fibb(n-2)
•Obvious algorithm:
•Code (precondition: n ≥ 0):
fibb(n)
if n < 2
return n
else
return fibb(n-1) + fibb(n-2)
•Performance:
•Recurrence: T(n)=...T(n)=...
•T(n)T(n) is exponential
•Actually, T(n)=Θ(fibb(n)T(n)=Θ(fibb(n)
•fibb(n)≃1.6n
Memoized Solutions
• Memoization adalah sebuah teknik untuk memperbaiki
kinerja algoritme rekursif
• Ini melibatkan penulisan kembali algoritme rekursif
sehingga setiap jawaban untuk suatu problem ditemukan
disimpan dalam suatu array
• Fungsi rekursif dapat melihat hasil di array daripada
menghitung kembali
• Memoization memperbaiki kinerja karena hasil parsial
tidak pernah dihitung dua kali
6
Bottom up approach
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
0 1 1 . . . F(n-2) F(n-1) F(n)
n
n
8
Domain aplikasi DP
• Problem optimisasi : Temukan suatu solusi
dengan nilai optimal (maximum or
minimum) value.
• Suatu suatu optimal, bisa jadi bukan solusi
optimal yang dicari, karena mungkin lebih
dari satu solusi optimal, yang manapun OK
9
Tahapan umum DP
• Karakterisasikan struktur dari sebuah solusi
optimal
• Secara rekursif definisikan nilai dari solusi
optimal
• Hitung suatu nilai optimal secara bottom uo
• Hitung suatu solusi optimal dari informasi
yang dihitung atau disimpan
10
Elemen-elemen dari DP
• Struktur Optimal
– Sebuah solusi optimal dari suatu problem mengandung
solusisolusi subproblemnya
• Sub-problem yang saling overlap
– Ruang dari subproblem-subproblem adalah “small”
pada kasus seperti ini sebuah algoritme rekursif untuk
suatu problem memecahkan sub-problem yang sama
terus menerus. Jumlah total subproblem yang berbeda
umunya polynomial pada ukuran inputnya.
11
Matrix-chain multiplication (MCM)
• Problem: given A1, A2, …,An, compute the
product: A1A2…An , find the fastest way (i.e.,
minimum number of multiplications) to compute
it.
• Suppose two matrices A(p,q) and B(q,r), compute
their product C(p,r) in p  q  r multiplications
– for a=1 to p
• for b=1 to r
– for c=1 to q
» C[a,b] = C[a,b]+ A[a,c]B[c,b]
12
Matrix-chain multiplication
• Different parenthesizations will have different
number of multiplications for product of multiple
matrices
• Example: A(10,100), B(100,5), C(5,50)
– If ((A B) C): 10 100 5 +10 5 50 =7500
– If (A (B C)): 10 100 50+100 5 50=75000
• The first way is ten times faster than the second !!!
• Denote A1, A2, …,An by < p0,p1,p2,…,pn>
– i.e, A1(p0,p1), A2(p1,p2), …, Ai(pi-1,pi),… An(pn-1,pn)
13
Matrix-chain multiplication –MCM
• Intuitive brute-force solution: Counting the number
of parenthesizations by exhaustively checking all
possible parenthesizations.
• Let P(n) denote the number of alternative
parenthesizations of a sequence of n matrices:
– P(n) = 1 if n=1
k=1
n-1 P(k)P(n-k) if n2
• The solution to the recursion is (2n).
• So brute-force will not work.
14
MCP Steps
• Step 1: structure of an optimal parenthesization
– Let Ai..j (ij) denote the matrix resulting from AiAi+1…Aj
– Any parenthesization of AiAi+1…Aj must split the product
between Ak and Ak+1 for some k, (ik<j). The cost =
#computing Ai..k + #computing Ak+1..j + # Ai..k  Ak+1..j.
– AiAi+1…Ak  Ak+1…Aj
15
MCM Steps
• Step 2: a recursive relation
– Let m[i,j] be the minimum number of multiplications
for AiAi+1…Aj
– m[1,n] will be the answer
– m[i,j] = 0 if i = j
min {m[i,k] + m[k+1,j] +pi-1pkpj } if i<j
ik<j
16
m[i,j] = 0 if i = j
min {m[i,k] + m[k+1,j] +pi-1pkpj } if i<j
17
A Recursive Algorithm for Matrix-Chain Multiplication
RECURSIVE-MATRIX-CHAIN(p,i,j) (called with(p,1,n))
1. if i=j then return 0
2. m[i,j]
3. for ki to j-1
4. do q RECURSIVE-MATRIX-CHAIN(p,i,k)+
RECURSIVE-MATRIX-CHAIN(p,k+1,j)+pi-1pkpj
5. if q< m[i,j] then m[i,j] q
6. return m[i,j]
The running time of the algorithm is O(2n)
18
3..3
1..3
3..4
1..2
2..4
1..1 4..4
2..3
3..4
2..2 4..4 2..2
1..1 4..4
3..3 1..1 2..3 1..2 3..3
1..4
2..2
4..4
3..3 2..2 3..3 1..1 2..2
This divide-and-conquer recursive algorithm solves the overlapping problems over and over.
In contrast, DP solves the same (overlapping) subproblems only once (at the first time),
then store the result in a table, when the same subproblem is encountered later, just look up
the table to get the result.
The computations in green color are replaced by table look up in MEMOIZED-MATRIX-CHAIN(p,1,4)
The divide-and-conquer is better for the problem which generates brand-new problems at
each step of recursion.
Recursion tree for the computation of RECURSIVE-MATRIX-CHAIN(p,1,4)
19
MCM Steps
• Step 3, Computing the optimal cost
– If by recursive algorithm, exponential time (2n) (ref.
to P.346 for the proof.), no better than brute-force.
– Total number of subproblems: +n = (n2)
– Recursive algorithm will encounter the same
subproblem many times.
– If tabling the answers for subproblems, each
subproblem is only solved once.
– The second hallmark of DP: overlapping subproblems
and solve every subproblem just once.
( )
n
2
20
MCM DP Steps
• Step 3, Algorithm,
– array m[1..n,1..n], with m[i,j] records the optimal
cost for AiAi+1…Aj .
– array s[1..n,1..n], s[i,j] records index k which
achieved the optimal cost when computing m[i,j].
– Suppose the input to the algorithm is p=< p0 , p1
,…, pn >.
21
MCM DP Steps
22
MCM DP—order of matrix computations
m(1,1) m(1,2) m(1,3) m(1,4) m(1,5) m(1,6)
m(2,2) m(2,3) m(2,4) m(2,5) m(2,6)
m(3,3) m(3,4) m(3,5) m(3,6)
m(4,4) m(4,5) m(4,6)
m(5,5) m(5,6)
m(6,6)
23
MCM DP Example
24
MCM DP Steps
• Step 4, constructing a parenthesization
order for the optimal solution.
– Since s[1..n,1..n] is computed, and s[i,j] is the
split position for AiAi+1…Aj , i.e, Ai…As[i,j] and
As[i,j] +1…Aj , thus, the parenthesization order
can be obtained from s[1..n,1..n] recursively,
beginning from s[1,n].
25
MCM DP Steps
• Step 4, algorithm
26
Finding Optimal substructures
• Show a solution to the problem consists of making a
choice, which results in one or more subproblems to
be solved.
• Suppose you are given a choice leading to an
optimal solution.
– Determine which subproblems follows and how to
characterize the resulting space of subproblems.
• Show the solution to the subproblems used within
the optimal solution to the problem must themselves
be optimal by cut-and-paste technique.
27
Optimal Substructure Varies in Two Ways
• How many subproblems
– In matrix-chain multiplication: two subproblems
• How many choices
– In matrix-chain multiplication: j-i choices
• DP solve the problem in bottom-up manner.
28
Running Time for DP Programs
• #overall subproblems  #choices.
– In matrix-chain multiplication, O(n2)  O(n) = O(n3)
• The cost =costs of solving subproblems + cost
of making choice.
– In matrix-chain multiplication, choice cost is pi-1pkpj.
29
Reconstructing an Optimal Solution
• An auxiliary table:
– Store the choice of the subproblem in each step
– Reconstructing the optimal steps from the table.
30
Memoization
• A variation of DP
• Keep the same efficiency as DP
• But in a top-down manner.
• Idea:
– Each entry in table initially contains a value indicating
the entry has yet to be filled in.
– When a subproblem is first encountered, its solution
needs to be solved and then is stored in the
corresponding entry of the table.
– If the subproblem is encountered again in the future,
just look up the table to take the value.
31
Memoized Matrix Chain
LOOKUP-CHAIN(p,i,j)
1. if m[i,j]< then return m[i,j]
2. if i=j then m[i,j] 0
3. else for ki to j-1
4. do q LOOKUP-CHAIN(p,i,k)+
5. LOOKUP-CHAIN(p,k+1,j)+pi-1pkpj
6. if q< m[i,j] then m[i,j] q
7. return m[i,j]
32
DP VS. Memoization
• MCM can be solved by DP or Memoized
algorithm, both in O(n3).
– Total (n2) subproblems, with O(n) for each.
• If all subproblems must be solved at least once,
DP is better by a constant factor due to no
recursive involvement as in Memoized algorithm.
• If some subproblems may not need to be solved,
Memoized algorithm may be more efficient, since
it only solve these subproblems which are
definitely required.
33
Summary
• DP two important properties
• Four steps of DP.
• Differences among divide-and-conquer
algorithms, DP algorithms, and Memoized
algorithm.
• Writing DP programs and analyze their
running time and space requirement.
• Modify the discussed DP algorithms.

More Related Content

Similar to 9 - DynamicProgramming-plus latihan.ppt

Chapter 16
Chapter 16Chapter 16
Chapter 16
ashish bansal
 
Matrix mult class-17
Matrix mult class-17Matrix mult class-17
Matrix mult class-17
Kumar
 
Sienna 10 dynamic
Sienna 10 dynamicSienna 10 dynamic
Sienna 10 dynamic
chidabdu
 
DynamicProgramming.ppt
DynamicProgramming.pptDynamicProgramming.ppt
DynamicProgramming.ppt
DavidMaina47
 
Dynamic Programming.pptx
Dynamic Programming.pptxDynamic Programming.pptx
Dynamic Programming.pptx
Thanga Ramya S
 
DynamicProgramming.pdf
DynamicProgramming.pdfDynamicProgramming.pdf
DynamicProgramming.pdf
ssuser3a8f33
 
DAA - UNIT 4 - Engineering.pptx
DAA - UNIT 4 - Engineering.pptxDAA - UNIT 4 - Engineering.pptx
DAA - UNIT 4 - Engineering.pptx
vaishnavi339314
 
Dynamic pgmming
Dynamic pgmmingDynamic pgmming
Dynamic pgmming
Dr. C.V. Suresh Babu
 
Lecture11
Lecture11Lecture11
Lecture11
Nv Thejaswini
 
5.3 dynamic programming 03
5.3 dynamic programming 035.3 dynamic programming 03
5.3 dynamic programming 03
Krish_ver2
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
Amit Kumar Rathi
 
unit-4-dynamic programming
unit-4-dynamic programmingunit-4-dynamic programming
unit-4-dynamic programming
hodcsencet
 
Daa:Dynamic Programing
Daa:Dynamic ProgramingDaa:Dynamic Programing
Daa:Dynamic Programing
rupali_2bonde
 
Matrix multiplicationdesign
Matrix multiplicationdesignMatrix multiplicationdesign
Matrix multiplicationdesign
Respa Peter
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
Yıldırım Tam
 
Least Square Optimization and Sparse-Linear Solver
Least Square Optimization and Sparse-Linear SolverLeast Square Optimization and Sparse-Linear Solver
Least Square Optimization and Sparse-Linear Solver
Ji-yong Kwon
 
Lp and ip programming cp 9
Lp and ip programming cp 9Lp and ip programming cp 9
Lp and ip programming cp 9
M S Prasad
 
Daa chapter 3
Daa chapter 3Daa chapter 3
Daa chapter 3
B.Kirron Reddi
 
dynamic-programming
dynamic-programmingdynamic-programming
dynamic-programming
MuhammadSheraz836877
 
Fundamental Problems in Number Theory.pptx
Fundamental Problems in Number Theory.pptxFundamental Problems in Number Theory.pptx
Fundamental Problems in Number Theory.pptx
Maths Assignment Help
 

Similar to 9 - DynamicProgramming-plus latihan.ppt (20)

Chapter 16
Chapter 16Chapter 16
Chapter 16
 
Matrix mult class-17
Matrix mult class-17Matrix mult class-17
Matrix mult class-17
 
Sienna 10 dynamic
Sienna 10 dynamicSienna 10 dynamic
Sienna 10 dynamic
 
DynamicProgramming.ppt
DynamicProgramming.pptDynamicProgramming.ppt
DynamicProgramming.ppt
 
Dynamic Programming.pptx
Dynamic Programming.pptxDynamic Programming.pptx
Dynamic Programming.pptx
 
DynamicProgramming.pdf
DynamicProgramming.pdfDynamicProgramming.pdf
DynamicProgramming.pdf
 
DAA - UNIT 4 - Engineering.pptx
DAA - UNIT 4 - Engineering.pptxDAA - UNIT 4 - Engineering.pptx
DAA - UNIT 4 - Engineering.pptx
 
Dynamic pgmming
Dynamic pgmmingDynamic pgmming
Dynamic pgmming
 
Lecture11
Lecture11Lecture11
Lecture11
 
5.3 dynamic programming 03
5.3 dynamic programming 035.3 dynamic programming 03
5.3 dynamic programming 03
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
unit-4-dynamic programming
unit-4-dynamic programmingunit-4-dynamic programming
unit-4-dynamic programming
 
Daa:Dynamic Programing
Daa:Dynamic ProgramingDaa:Dynamic Programing
Daa:Dynamic Programing
 
Matrix multiplicationdesign
Matrix multiplicationdesignMatrix multiplicationdesign
Matrix multiplicationdesign
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Least Square Optimization and Sparse-Linear Solver
Least Square Optimization and Sparse-Linear SolverLeast Square Optimization and Sparse-Linear Solver
Least Square Optimization and Sparse-Linear Solver
 
Lp and ip programming cp 9
Lp and ip programming cp 9Lp and ip programming cp 9
Lp and ip programming cp 9
 
Daa chapter 3
Daa chapter 3Daa chapter 3
Daa chapter 3
 
dynamic-programming
dynamic-programmingdynamic-programming
dynamic-programming
 
Fundamental Problems in Number Theory.pptx
Fundamental Problems in Number Theory.pptxFundamental Problems in Number Theory.pptx
Fundamental Problems in Number Theory.pptx
 

Recently uploaded

Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
Zilliz
 
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
Jason Yip
 
Principle of conventional tomography-Bibash Shahi ppt..pptx
Principle of conventional tomography-Bibash Shahi ppt..pptxPrinciple of conventional tomography-Bibash Shahi ppt..pptx
Principle of conventional tomography-Bibash Shahi ppt..pptx
BibashShahi
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
Tatiana Kojar
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
akankshawande
 
Essentials of Automations: Exploring Attributes & Automation Parameters
Essentials of Automations: Exploring Attributes & Automation ParametersEssentials of Automations: Exploring Attributes & Automation Parameters
Essentials of Automations: Exploring Attributes & Automation Parameters
Safe Software
 
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
Edge AI and Vision Alliance
 
Digital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
Digital Banking in the Cloud: How Citizens Bank Unlocked Their MainframeDigital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
Digital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
Precisely
 
What is an RPA CoE? Session 1 – CoE Vision
What is an RPA CoE?  Session 1 – CoE VisionWhat is an RPA CoE?  Session 1 – CoE Vision
What is an RPA CoE? Session 1 – CoE Vision
DianaGray10
 
“How Axelera AI Uses Digital Compute-in-memory to Deliver Fast and Energy-eff...
“How Axelera AI Uses Digital Compute-in-memory to Deliver Fast and Energy-eff...“How Axelera AI Uses Digital Compute-in-memory to Deliver Fast and Energy-eff...
“How Axelera AI Uses Digital Compute-in-memory to Deliver Fast and Energy-eff...
Edge AI and Vision Alliance
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
Jakub Marek
 
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
saastr
 
5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides
DanBrown980551
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
Hiroshi SHIBATA
 
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
Alex Pruden
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
Brandon Minnick, MBA
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
Jason Packer
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
Zilliz
 
AppSec PNW: Android and iOS Application Security with MobSF
AppSec PNW: Android and iOS Application Security with MobSFAppSec PNW: Android and iOS Application Security with MobSF
AppSec PNW: Android and iOS Application Security with MobSF
Ajin Abraham
 

Recently uploaded (20)

Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
 
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
[OReilly Superstream] Occupy the Space: A grassroots guide to engineering (an...
 
Principle of conventional tomography-Bibash Shahi ppt..pptx
Principle of conventional tomography-Bibash Shahi ppt..pptxPrinciple of conventional tomography-Bibash Shahi ppt..pptx
Principle of conventional tomography-Bibash Shahi ppt..pptx
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
 
Essentials of Automations: Exploring Attributes & Automation Parameters
Essentials of Automations: Exploring Attributes & Automation ParametersEssentials of Automations: Exploring Attributes & Automation Parameters
Essentials of Automations: Exploring Attributes & Automation Parameters
 
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
 
Digital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
Digital Banking in the Cloud: How Citizens Bank Unlocked Their MainframeDigital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
Digital Banking in the Cloud: How Citizens Bank Unlocked Their Mainframe
 
What is an RPA CoE? Session 1 – CoE Vision
What is an RPA CoE?  Session 1 – CoE VisionWhat is an RPA CoE?  Session 1 – CoE Vision
What is an RPA CoE? Session 1 – CoE Vision
 
“How Axelera AI Uses Digital Compute-in-memory to Deliver Fast and Energy-eff...
“How Axelera AI Uses Digital Compute-in-memory to Deliver Fast and Energy-eff...“How Axelera AI Uses Digital Compute-in-memory to Deliver Fast and Energy-eff...
“How Axelera AI Uses Digital Compute-in-memory to Deliver Fast and Energy-eff...
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
 
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
 
5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
 
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
zkStudyClub - LatticeFold: A Lattice-based Folding Scheme and its Application...
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
 
AppSec PNW: Android and iOS Application Security with MobSF
AppSec PNW: Android and iOS Application Security with MobSFAppSec PNW: Android and iOS Application Security with MobSF
AppSec PNW: Android and iOS Application Security with MobSF
 

9 - DynamicProgramming-plus latihan.ppt

  • 2. 2 Dynamic Programming (DP) • Seperti Divide and Conquer, memecahkan masalah dengan mengkombinasikan solusi-solusi dari subproblem • Perbedaan antara divide-and-conquer dan DP: – Pada DC sub-problems independen, penyelesaian masalah secara independen dan secara rekursif, (sehingga beberapa sub(sub)problems diselesaikan berulang kali) – Pada DP sub-problem saling bergantung, i.e., sub- problems share sub-sub-problems, tiap sub(sub)problem hanya diselesaikan sekali, solusi sub(sub)problems disimpan di sebuah table dan digunakan untuk menyelesaikan sub problem yang levelnya lebih tinggi
  • 3. Example: Fibonacci numbers • Definisi dari bilangan Fibonacci: F(n) = F(n-1) + F(n-2) F(0) = 0 F(1) = 1 • Menghitung bilangan Fibonaci ke-n secara rekursif (top- down): F(n) F(n-1) + F(n-2) F(n-2) + F(n-3) F(n-3) + F(n-4) ...
  • 4. Top down approach + memoization • Always remember the past… 4
  • 5. 5 •fibb(0) = 0, fibb(1) = 1; fibb(n) = fibb(n-1) + fibb(n-2) •Obvious algorithm: •Code (precondition: n ≥ 0): fibb(n) if n < 2 return n else return fibb(n-1) + fibb(n-2) •Performance: •Recurrence: T(n)=...T(n)=... •T(n)T(n) is exponential •Actually, T(n)=Θ(fibb(n)T(n)=Θ(fibb(n) •fibb(n)≃1.6n
  • 6. Memoized Solutions • Memoization adalah sebuah teknik untuk memperbaiki kinerja algoritme rekursif • Ini melibatkan penulisan kembali algoritme rekursif sehingga setiap jawaban untuk suatu problem ditemukan disimpan dalam suatu array • Fungsi rekursif dapat melihat hasil di array daripada menghitung kembali • Memoization memperbaiki kinerja karena hasil parsial tidak pernah dihitung dua kali 6
  • 7. Bottom up approach 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 0 1 1 . . . F(n-2) F(n-1) F(n) n n
  • 8. 8 Domain aplikasi DP • Problem optimisasi : Temukan suatu solusi dengan nilai optimal (maximum or minimum) value. • Suatu suatu optimal, bisa jadi bukan solusi optimal yang dicari, karena mungkin lebih dari satu solusi optimal, yang manapun OK
  • 9. 9 Tahapan umum DP • Karakterisasikan struktur dari sebuah solusi optimal • Secara rekursif definisikan nilai dari solusi optimal • Hitung suatu nilai optimal secara bottom uo • Hitung suatu solusi optimal dari informasi yang dihitung atau disimpan
  • 10. 10 Elemen-elemen dari DP • Struktur Optimal – Sebuah solusi optimal dari suatu problem mengandung solusisolusi subproblemnya • Sub-problem yang saling overlap – Ruang dari subproblem-subproblem adalah “small” pada kasus seperti ini sebuah algoritme rekursif untuk suatu problem memecahkan sub-problem yang sama terus menerus. Jumlah total subproblem yang berbeda umunya polynomial pada ukuran inputnya.
  • 11. 11 Matrix-chain multiplication (MCM) • Problem: given A1, A2, …,An, compute the product: A1A2…An , find the fastest way (i.e., minimum number of multiplications) to compute it. • Suppose two matrices A(p,q) and B(q,r), compute their product C(p,r) in p  q  r multiplications – for a=1 to p • for b=1 to r – for c=1 to q » C[a,b] = C[a,b]+ A[a,c]B[c,b]
  • 12. 12 Matrix-chain multiplication • Different parenthesizations will have different number of multiplications for product of multiple matrices • Example: A(10,100), B(100,5), C(5,50) – If ((A B) C): 10 100 5 +10 5 50 =7500 – If (A (B C)): 10 100 50+100 5 50=75000 • The first way is ten times faster than the second !!! • Denote A1, A2, …,An by < p0,p1,p2,…,pn> – i.e, A1(p0,p1), A2(p1,p2), …, Ai(pi-1,pi),… An(pn-1,pn)
  • 13. 13 Matrix-chain multiplication –MCM • Intuitive brute-force solution: Counting the number of parenthesizations by exhaustively checking all possible parenthesizations. • Let P(n) denote the number of alternative parenthesizations of a sequence of n matrices: – P(n) = 1 if n=1 k=1 n-1 P(k)P(n-k) if n2 • The solution to the recursion is (2n). • So brute-force will not work.
  • 14. 14 MCP Steps • Step 1: structure of an optimal parenthesization – Let Ai..j (ij) denote the matrix resulting from AiAi+1…Aj – Any parenthesization of AiAi+1…Aj must split the product between Ak and Ak+1 for some k, (ik<j). The cost = #computing Ai..k + #computing Ak+1..j + # Ai..k  Ak+1..j. – AiAi+1…Ak  Ak+1…Aj
  • 15. 15 MCM Steps • Step 2: a recursive relation – Let m[i,j] be the minimum number of multiplications for AiAi+1…Aj – m[1,n] will be the answer – m[i,j] = 0 if i = j min {m[i,k] + m[k+1,j] +pi-1pkpj } if i<j ik<j
  • 16. 16 m[i,j] = 0 if i = j min {m[i,k] + m[k+1,j] +pi-1pkpj } if i<j
  • 17. 17 A Recursive Algorithm for Matrix-Chain Multiplication RECURSIVE-MATRIX-CHAIN(p,i,j) (called with(p,1,n)) 1. if i=j then return 0 2. m[i,j] 3. for ki to j-1 4. do q RECURSIVE-MATRIX-CHAIN(p,i,k)+ RECURSIVE-MATRIX-CHAIN(p,k+1,j)+pi-1pkpj 5. if q< m[i,j] then m[i,j] q 6. return m[i,j] The running time of the algorithm is O(2n)
  • 18. 18 3..3 1..3 3..4 1..2 2..4 1..1 4..4 2..3 3..4 2..2 4..4 2..2 1..1 4..4 3..3 1..1 2..3 1..2 3..3 1..4 2..2 4..4 3..3 2..2 3..3 1..1 2..2 This divide-and-conquer recursive algorithm solves the overlapping problems over and over. In contrast, DP solves the same (overlapping) subproblems only once (at the first time), then store the result in a table, when the same subproblem is encountered later, just look up the table to get the result. The computations in green color are replaced by table look up in MEMOIZED-MATRIX-CHAIN(p,1,4) The divide-and-conquer is better for the problem which generates brand-new problems at each step of recursion. Recursion tree for the computation of RECURSIVE-MATRIX-CHAIN(p,1,4)
  • 19. 19 MCM Steps • Step 3, Computing the optimal cost – If by recursive algorithm, exponential time (2n) (ref. to P.346 for the proof.), no better than brute-force. – Total number of subproblems: +n = (n2) – Recursive algorithm will encounter the same subproblem many times. – If tabling the answers for subproblems, each subproblem is only solved once. – The second hallmark of DP: overlapping subproblems and solve every subproblem just once. ( ) n 2
  • 20. 20 MCM DP Steps • Step 3, Algorithm, – array m[1..n,1..n], with m[i,j] records the optimal cost for AiAi+1…Aj . – array s[1..n,1..n], s[i,j] records index k which achieved the optimal cost when computing m[i,j]. – Suppose the input to the algorithm is p=< p0 , p1 ,…, pn >.
  • 22. 22 MCM DP—order of matrix computations m(1,1) m(1,2) m(1,3) m(1,4) m(1,5) m(1,6) m(2,2) m(2,3) m(2,4) m(2,5) m(2,6) m(3,3) m(3,4) m(3,5) m(3,6) m(4,4) m(4,5) m(4,6) m(5,5) m(5,6) m(6,6)
  • 24. 24 MCM DP Steps • Step 4, constructing a parenthesization order for the optimal solution. – Since s[1..n,1..n] is computed, and s[i,j] is the split position for AiAi+1…Aj , i.e, Ai…As[i,j] and As[i,j] +1…Aj , thus, the parenthesization order can be obtained from s[1..n,1..n] recursively, beginning from s[1,n].
  • 25. 25 MCM DP Steps • Step 4, algorithm
  • 26. 26 Finding Optimal substructures • Show a solution to the problem consists of making a choice, which results in one or more subproblems to be solved. • Suppose you are given a choice leading to an optimal solution. – Determine which subproblems follows and how to characterize the resulting space of subproblems. • Show the solution to the subproblems used within the optimal solution to the problem must themselves be optimal by cut-and-paste technique.
  • 27. 27 Optimal Substructure Varies in Two Ways • How many subproblems – In matrix-chain multiplication: two subproblems • How many choices – In matrix-chain multiplication: j-i choices • DP solve the problem in bottom-up manner.
  • 28. 28 Running Time for DP Programs • #overall subproblems  #choices. – In matrix-chain multiplication, O(n2)  O(n) = O(n3) • The cost =costs of solving subproblems + cost of making choice. – In matrix-chain multiplication, choice cost is pi-1pkpj.
  • 29. 29 Reconstructing an Optimal Solution • An auxiliary table: – Store the choice of the subproblem in each step – Reconstructing the optimal steps from the table.
  • 30. 30 Memoization • A variation of DP • Keep the same efficiency as DP • But in a top-down manner. • Idea: – Each entry in table initially contains a value indicating the entry has yet to be filled in. – When a subproblem is first encountered, its solution needs to be solved and then is stored in the corresponding entry of the table. – If the subproblem is encountered again in the future, just look up the table to take the value.
  • 31. 31 Memoized Matrix Chain LOOKUP-CHAIN(p,i,j) 1. if m[i,j]< then return m[i,j] 2. if i=j then m[i,j] 0 3. else for ki to j-1 4. do q LOOKUP-CHAIN(p,i,k)+ 5. LOOKUP-CHAIN(p,k+1,j)+pi-1pkpj 6. if q< m[i,j] then m[i,j] q 7. return m[i,j]
  • 32. 32 DP VS. Memoization • MCM can be solved by DP or Memoized algorithm, both in O(n3). – Total (n2) subproblems, with O(n) for each. • If all subproblems must be solved at least once, DP is better by a constant factor due to no recursive involvement as in Memoized algorithm. • If some subproblems may not need to be solved, Memoized algorithm may be more efficient, since it only solve these subproblems which are definitely required.
  • 33. 33 Summary • DP two important properties • Four steps of DP. • Differences among divide-and-conquer algorithms, DP algorithms, and Memoized algorithm. • Writing DP programs and analyze their running time and space requirement. • Modify the discussed DP algorithms.