香港中文大学:《Design and Analysis of Algorithms》课程教学资源(PPT课件讲稿)Week 5 Dynamic Programming

CSC3160: Design and Analysis of Algorithms Week 5: Dynamic Programming Instructor: Shengyu Zhang 1
Instructor: Shengyu Zhang 1

About midterm Time:Mar 3,2:50pm -4:50pm. Place:This lecture room. Open book,open lecture notes. But no Internet allowed. Scope:First 6 lectures 2
About midterm ◼ Time: Mar 3, 2:50pm – 4:50pm. ◼ Place: This lecture room. ◼ Open book, open lecture notes. ❑ But no Internet allowed. ◼ Scope: First 6 lectures 2

Dynamic Programming A simple but non-trivial method for designing algorithms Achieve much better efficiency than naive ones. A couple of examples will be exhibited and analyzed. 3
Dynamic Programming ◼ A simple but non-trivial method for designing algorithms ◼ Achieve much better efficiency than naïve ones. ◼ A couple of examples will be exhibited and analyzed. 3

Problem 1:Chain matrix multiplication 4
Problem 1: Chain matrix multiplication 4

Suppose we want to multiply four matrices We want to multiply four matrices:A×B×C×D. Dimensions:A5ox20,B20x1,C1x10,D10x100 ■ Assume:cost (Xmxn X Ynxi)=mnl. The order matters! ▣A×(B×C)×D):20×1×10+20×10×100+50×20×100 120,200 A×(B×(C×D):1×10×100+20×1×100+50×20×100= 103,000 口(A×B)×(C×D):50×20×1+1×10×100+50×1×100=7,000 口(A×B)×C)×D:50×20×1+50×1×10+50×10×100=51,500 0 (A×(B×C)×D:20×1×10+50×20×10+50×10×100=60,200 Question:In what order should we multiply them? 5
Suppose we want to multiply four matrices ◼ We want to multiply four matrices: 𝐴 × 𝐵 × 𝐶 × 𝐷. ◼ Dimensions: 𝐴50×20, 𝐵20×1 , 𝐶1×10, 𝐷10×100 ◼ Assume: cost (𝑋𝑚×𝑛 × 𝑌𝑛×𝑙 ) = 𝑚𝑛𝑙. ❑ 𝐴 × 𝐵 × 𝐶 × 𝐷 : 20 × 1 × 10 + 20 × 10 × 100 + 50 × 20 × 100 = 120,200 ❑ 𝐴 × 𝐵 × 𝐶 × 𝐷 : 1 × 10 × 100 + 20 × 1 × 100 + 50 × 20 × 100 = 103,000 ❑ 𝐴 × 𝐵 × 𝐶 × 𝐷 : 50 × 20 × 1 + 1 × 10 × 100 + 50 × 1 × 100 = 7,000 ❑ 𝐴 × 𝐵 × 𝐶 × 𝐷: 50 × 20 × 1 + 50 × 1 × 10 + 50 × 10 × 100 = 51,500 ❑ 𝐴 × 𝐵 × 𝐶 × 𝐷: 20 × 1 × 10 + 50 × 20 × 10 + 50 × 10 × 100 = 60,200 ◼ Question: In what order should we multiply them? The order matters! 5

Key property General question:We have matrices A1,...An,we want to find the best order for A1×…×An o Dimension of Ai:mi-1x mi ■ One way to find the optimum:Consider the last step. Suppose:(A1×…×Ai)×(Ai+1×…×An)for s0mei∈{1,.,n-1}. cost(1,n)=cost(1,i)+cost(i+1,n)+ momimn 6
Key property ◼ General question: We have matrices 𝐴1, …, 𝐴𝑛, we want to find the best order for 𝐴1 × ⋯ × 𝐴𝑛 ❑ Dimension of 𝐴𝑖 : 𝑚𝑖−1 × 𝑚𝑖 ◼ One way to find the optimum: Consider the last step. ❑ Suppose: 𝐴1 × ⋯ × 𝐴𝑖 × 𝐴𝑖+1 × ⋯ × 𝐴𝑛 for some 𝑖 ∈ 1,… , 𝑛 − 1 . ◼ cost 1, 𝑛 = cost 1, 𝑖 + cost 𝑖 + 1, 𝑛 + 𝑚0𝑚𝑖𝑚𝑛 6

Algorithm But what is a best i? We don't know...Try all and take the min. bestcost(1,n) min bestcost(1,i)+bestcost(i+1,n)+momimn bestcost(i,j):the min cost of computing(A;×…×A;) ■ How to solve(A1×…XAi)and(Ai+1×…×An)? Attempt:Same way,i.e.a recursion Complexity: ▣T(1,n)=∑(T(1,i)+T(i+1,n)+0(1) Exponential!
Algorithm ◼ But what is a best 𝑖? ◼ We don’t know… Try all and take the min. bestcost(1,𝑛) = min 𝑖 bestcost(1,𝑖) + bestcost(𝑖 + 1, 𝑛) + 𝑚0𝑚𝑖𝑚𝑛 ❑ bestcost(𝑖,𝑗): the min cost of computing 𝐴𝑖 × ⋯ × 𝐴𝑗 ◼ How to solve 𝐴1 × ⋯ × 𝐴𝑖 and 𝐴𝑖+1 × ⋯ × 𝐴𝑛 ? ◼ Attempt: Same way, i.e. a recursion ◼ Complexity: ❑ 𝑇(1, 𝑛) = σ𝑖 (𝑇(1, 𝑖) + 𝑇(𝑖 + 1, 𝑛) + 𝑂(1)) ❑ Exponential! 7

A50×20,B20X1,C1x10,D10×100,E100×30 AXBXC×DXE min A×(BXC (AXB)×(C (A×B×C) (A×B×C x D X E) XD X E) ×(DXE) XD)XE BXCXD CXDXE CXD XE AXBXC AXBXC B X C X D Observation:small subproblems are calculated many times! 8
𝐴50×20, 𝐵20×1, 𝐶1×10,𝐷10×100 ,𝐸100×30 ◼ Observation: small subproblems are calculated many times! 𝐴 × (𝐵 × 𝐶 × 𝐷 × 𝐸) 𝐴 × 𝐵 × 𝐶 × 𝐷 × 𝐸 (𝐴 × 𝐵) × (𝐶 × 𝐷 × 𝐸) (𝐴 × 𝐵 × 𝐶) × (𝐷 × 𝐸) min 𝐵 × 𝐶 × 𝐷 𝐶 × 𝐷 × 𝐸 𝐶 × 𝐷 × 𝐸 𝐴 × 𝐵 × 𝐶 𝐴 × 𝐵 × 𝐶 𝐵 × 𝐶 × 𝐷 (𝐴 × 𝐵 × 𝐶 × 𝐷) × 𝐸 8

What did we observe? Why not just do it once and store the result for later reference? When needed later:simply look up the stored result. That's dynamic programming. First compute the small problems and store the answers Then compute the large problems using the stored results of smaller subproblems
What did we observe? ◼ Why not just do it once and store the result for later reference? ❑ When needed later: simply look up the stored result. ◼ That’s dynamic programming. ❑ First compute the small problems and store the answers ❑ Then compute the large problems using the stored results of smaller subproblems. 9

A50×20,B20×1,C1x10,D10×100,E100x30 AXBX C X D XE min A×(BXC (AXB)×(C (A×B×C) (AxBxC XDXE) X D X E) x (D X E) XD)XE AXBXC BXCXD CXDXE AXB BxC CXD DXE Now solve the problem this way. 10
𝐴50×20, 𝐵20×1, 𝐶1×10,𝐷10×100 ,𝐸100×30 𝐴 × (𝐵 × 𝐶 × 𝐷 × 𝐸) 𝐴 × 𝐵 × 𝐶 × 𝐷 × 𝐸 (𝐴 × 𝐵) × (𝐶 × 𝐷 × 𝐸) (𝐴 × 𝐵 × 𝐶) × (𝐷 × 𝐸) min 𝐴 × 𝐵 × 𝐶 𝐵 × 𝐶 × 𝐷 𝐶 × 𝐷 × 𝐸 (𝐴 × 𝐵 × 𝐶 × 𝐷) × 𝐸 𝐴 × 𝐵 𝐵 × 𝐶 𝐶 × 𝐷 𝐷 × 𝐸 ◼ Now solve the problem this way. 10
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 香港中文大学:《Design and Analysis of Algorithms》课程教学资源(PPT课件讲稿)Week 4 Randomized Algorithms.pptx
- 香港中文大学:《Design and Analysis of Algorithms》课程教学资源(PPT课件讲稿)Week 3 Greedy Algorithms.pptx
- 香港中文大学:《Design and Analysis of Algorithms》课程教学资源(PPT课件讲稿)Week 2 Single Source Shortest Paths.pptx
- 香港中文大学:《Design and Analysis of Algorithms》课程教学资源(PPT课件讲稿)Week 1 Introduction to the course.pptx
- 香港中文大学:《Quantum Computing》课程教学资源(课件讲稿)Lecture 12 Quantum information theory 4.pdf
- 香港中文大学:《Quantum Computing》课程教学资源(课件讲稿)Lecture 11 Quantum information theory 3.pdf
- 香港中文大学:《Quantum Computing》课程教学资源(课件讲稿)Lecture 10 Quantum information theory 2.docx
- 香港中文大学:《Quantum Computing》课程教学资源(课件讲稿)Lecture 9 Quantum information theory 1.docx
- 香港中文大学:《Quantum Computing》课程教学资源(课件讲稿)Lecture 8 Quantum communication complexity 2.docx
- 香港中文大学:《Quantum Computing》课程教学资源(课件讲稿)Lecture 7 Quantum communication complexity 1.docx
- 香港中文大学:《Quantum Computing》课程教学资源(课件讲稿)Lecture 6 Quantum query complexity.docx
- 香港中文大学:《Quantum Computing》课程教学资源(课件讲稿)Lecture 5 Searching algorithm and quantum adversary method.docx
- 香港中文大学:《Quantum Computing》课程教学资源(课件讲稿)Lecture 4 Hidden Subgroup Problems 2.docx
- 香港中文大学:《Quantum Computing》课程教学资源(课件讲稿)Lecture 3 Hidden Subgroup Problems 1.docx
- 香港中文大学:《Quantum Computing》课程教学资源(课件讲稿)Lecture 2 Shor's algorithm.docx
- 香港中文大学:《Quantum Computing》课程教学资源(课件讲稿)Lecture 1 Basics.docx
- 香港中文大学:《Topics in Theoretical Computer Science》课程教学资源(PPT课件讲稿)Lecture 12 Quantum computing. Quantum algorithms, BQP, quantum non-locality, quantum games.pptx
- 香港中文大学:《Topics in Theoretical Computer Science》课程教学资源(PPT课件讲稿)Lecture 11 Influence maximization on social network.pptx
- 香港中文大学:《Topics in Theoretical Computer Science》课程教学资源(PPT课件讲稿)Lecture 10 Online learning. Expert problem.pptx
- 香港中文大学:《Topics in Theoretical Computer Science》课程教学资源(PPT课件讲稿)Lecture 9 Online algorithm.pptx
- 香港中文大学:《Design and Analysis of Algorithms》课程教学资源(PPT课件讲稿)Week 10 NP-completeness.pptx
- 香港中文大学:《Design and Analysis of Algorithms》课程教学资源(PPT课件讲稿)Week 11 Approximation Algorithms.pptx
- 香港中文大学:《Design and Analysis of Algorithms》课程教学资源(PPT课件讲稿)Week 12 Online Algorithms.pptx
- 香港中文大学:《Design and Analysis of Algorithms》课程教学资源(PPT课件讲稿)Week 6 Linear Programming.pptx
- 香港中文大学:《Design and Analysis of Algorithms》课程教学资源(PPT课件讲稿)Week 7 Divide and Conquer.pptx
- 香港中文大学:《Design and Analysis of Algorithms》课程教学资源(PPT课件讲稿)Week 8 Maximum Network Flow.pptx
- 香港中文大学:《Design and Analysis of Algorithms》课程教学资源(PPT课件讲稿)Week 10 Stable Matching and Secretary Problem.pptx
- 香港中文大学:《Design and Analysis of Algorithms》课程教学资源(辅导课件)tutorial 01.pptx
- 香港中文大学:《Design and Analysis of Algorithms》课程教学资源(辅导课件)tutorial 10.pptx
- 香港中文大学:《Design and Analysis of Algorithms》课程教学资源(辅导课件)tutorial 11.pptx
- 香港中文大学:《Design and Analysis of Algorithms》课程教学资源(辅导课件)tutorial 12.pptx
- 香港中文大学:《Design and Analysis of Algorithms》课程教学资源(辅导课件)tutorial 02.pptx
- 香港中文大学:《Design and Analysis of Algorithms》课程教学资源(辅导课件)tutorial 03.pptx
- 香港中文大学:《Design and Analysis of Algorithms》课程教学资源(辅导课件)tutorial 04.pptx
- 香港中文大学:《Design and Analysis of Algorithms》课程教学资源(辅导课件)tutorial 05.pptx
- 香港中文大学:《Design and Analysis of Algorithms》课程教学资源(辅导课件)tutorial 06.pptx
- 香港中文大学:《Design and Analysis of Algorithms》课程教学资源(辅导课件)tutorial 08.pptx
- 香港中文大学:《Design and Analysis of Algorithms》课程教学资源(辅导课件)tutorial 09.pptx
- 香港中文大学:《Theory of Computational Complexity》课程教学资源(讲义)Lecture 1 Samples of possibility and impossibility results in algorithm designing.docx
- 香港中文大学:《Theory of Computational Complexity》课程教学资源(讲义)Lecture 2 More samples.docx