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

CSC3160: Design and Analysis of Algorithms Week 7: Divide and Conquer Instructor: Shengyu Zhang 1
Instructor: Shengyu Zhang 1

Example 1:Merge sort 2
Example 1: Merge sort 2

Starting example Sorting: We have a list of numbers x1,...,xn. We want to sort them in the increasing order. 3
Starting example ◼ Sorting: ❑ We have a list of numbers 𝑥1, … , 𝑥𝑛. ❑ We want to sort them in the increasing order. 3

An algorithm:merge sort Merge sort: Cut it into two halves with equal size. Suppose 2 divides n for simplicity. Suppose the two halves are sorted:Merge them. Use two pointers,one for each half,to scan them, during which course do the appropriate merge. How to sort each of the two halves?Recursively. 4
An algorithm: merge sort ◼ Merge sort: ❑ Cut it into two halves with equal size. ◼ Suppose 2 divides 𝑛 for simplicity. ❑ Suppose the two halves are sorted: Merge them. ◼ Use two pointers, one for each half, to scan them, during which course do the appropriate merge. ❑ How to sort each of the two halves? Recursively. 4

Complexity? Suppose this algorithm takes T(n)time for an input with n numbers. Thus each of the two halves takes T(n/2) time. The merging?0(n) Scanning n elements,an 0(1)time operation needed for each. Total amount of time:T(n)≤2T(n/2)+c·n. 5
Complexity? ◼ Suppose this algorithm takes 𝑇(𝑛) time for an input with 𝑛 numbers. ◼ Thus each of the two halves takes 𝑇(𝑛/2) time. ◼ The merging? 𝑂(𝑛) ❑ Scanning 𝑛 elements, an 𝑂(1) time operation needed for each. ◼ Total amount of time: 𝑇(𝑛) ≤ 2𝑇(𝑛/2) + 𝑐 ∙ 𝑛. 5

How to solve/bound this recurrence relation? ■T(n)≤2T(n/2)+c·n ≤2T(m/4)+c·n/2 ≤4T(n/4)+2c·n ≤2T(n/8)+c·n/4 ≤8T(m/8)+3c·n ≤ ≤nT(n/m)+(ogn)c·n ≤O(n logn): 6
How to solve/bound this recurrence relation? ◼ 𝑇 𝑛 ≤ 2𝑇 𝑛/2 + 𝑐 ⋅ 𝑛 ~~~~~~~~ ≤ 2𝑇(𝑛/4) + 𝑐 ∙ 𝑛/2 ≤ 4𝑇(𝑛/4) + 2𝑐 ∙ 𝑛 ~~~~~~~ ≤ 2𝑇(𝑛/8) + 𝑐 ∙ 𝑛/4 ≤ 8𝑇(𝑛/8) + 3𝑐 ∙ 𝑛 ≤ … ≤ 𝑛𝑇(𝑛/𝑛) + (log 𝑛)𝑐 ∙ 𝑛 ≤ 𝑂(𝑛 log 𝑛). 6

A general method for designing algorithm: Divide and conquer o f Breaking the problem into subproblems that are themselves smaller instances of the same type of problem Recursively solving these subproblems Appropriately combining their answers
A general method for designing algorithm: Divide and conquer ◼ Breaking the problem into subproblems ❑ that are themselves smaller instances of the same type of problem ◼ Recursively solving these subproblems ◼ Appropriately combining their answers 7

Complexity Running time on an input of size n:T(n) Break problem into a subproblems,each of the same size n/b. In general,a is not necessarily equal to b. Time to recursively solve each subproblem: T(n/b) Time for breaking problem (into subproblems) and combining the answers:0(n) 8
Complexity ◼ Running time on an input of size 𝑛: 𝑇(𝑛) ◼ Break problem into 𝑎 subproblems, each of the same size 𝑛/𝑏. ❑ In general, 𝑎 is not necessarily equal to 𝑏. ◼ Time to recursively solve each subproblem: 𝑇(𝑛/𝑏) ◼ Time for breaking problem (into subproblems) and combining the answers: 𝑂(𝑛 𝑑 ) 8

Master theorem ·T(n)=aT(n/b)+O(na) a 0,b>1,and d >0 are all constants. Then O(n m了O3 ifd logb a O(ndlogn) if d logb a ifd<logb a· Proof in textbook.Not required. 口 But you need to know how to apply it. 9
Master theorem ◼ 𝑇(𝑛) = 𝑎𝑇(𝑛/𝑏) + 𝑂(𝑛 𝑑 ) ❑ 𝑎 > 0, 𝑏 > 1, and 𝑑 ≥ 0 are all constants. ◼ Then ❑ Proof in textbook. Not required. ❑ But you need to know how to apply it. 9

■T(n)=aT(n/b)+O(nd) rm二z ifd logb a O(ndlogn) if d logb a if'd logb a. Merge sort:T(n)<2T(n/2)+0(n). a=b=2,d=1.So d=logb a. By the master theorem:T(n)=0(n logn). 10
◼ 𝑇(𝑛) = 𝑎𝑇(𝑛/𝑏) + 𝑂(𝑛 𝑑 ) ◼ Merge sort: 𝑇(𝑛) ≤ 2𝑇(𝑛/2) + 𝑂(𝑛). ◼ 𝑎 = 𝑏 = 2, 𝑑 = 1. So 𝑑 = log𝑏 𝑎. ◼ By the master theorem: 𝑇(𝑛) = 𝑂(𝑛 log 𝑛). 10
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 香港中文大学:《Design and Analysis of Algorithms》课程教学资源(PPT课件讲稿)Week 6 Linear Programming.pptx
- 香港中文大学:《Design and Analysis of Algorithms》课程教学资源(PPT课件讲稿)Week 12 Online Algorithms.pptx
- 香港中文大学:《Design and Analysis of Algorithms》课程教学资源(PPT课件讲稿)Week 11 Approximation Algorithms.pptx
- 香港中文大学:《Design and Analysis of Algorithms》课程教学资源(PPT课件讲稿)Week 10 NP-completeness.pptx
- 香港中文大学:《Design and Analysis of Algorithms》课程教学资源(PPT课件讲稿)Week 5 Dynamic Programming.pptx
- 香港中文大学:《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
- 香港中文大学:《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
- 香港中文大学:《Theory of Computational Complexity》课程教学资源(讲义)Lecture 3 Communication complexity.docx
- 香港中文大学:《Theory of Computational Complexity》课程教学资源(讲义)Lecture 4 Multiparty Communication Complexity.docx
- 香港中文大学:《Theory of Computational Complexity》课程教学资源(讲义)Lecture 5 Formula complexity I.docx
- 香港中文大学:《Theory of Computational Complexity》课程教学资源(讲义)Lecture 6 Formula complexity II.docx
- 香港中文大学:《Theory of Computational Complexity》课程教学资源(讲义)Lecture 7 Decision Tree Complexity and Fourier analysis.docx