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

CSC3160: Design and Analysis of Algorithms Week 3: Greedy Algorithms Instructor: Shengyu Zhang 1
Instructor: Shengyu Zhang 1

Content Two problems Minimum Spanning Tree Huffman encoding One approach:greedy algorithms 2
Content ◼ Two problems ❑ Minimum Spanning Tree ❑ Huffman encoding ◼ One approach: greedy algorithms 2

Example 1:Minimum Spanning Tree 3
Example 1: Minimum Spanning Tree 3

MST:Problem and Motivation Suppose we have n computers, connected by wires as given in the graph. 4 1 ■ Each wire has a renting cost. 4 6 3 ■ We want to select some wires, such that all computers are connected (i.e.every two can communicate). 3 Algorithmic question:How to select a subset of wires with the minimum renting cost? Answer to this graph?
MST: Problem and Motivation ◼ Suppose we have 𝑛 computers, connected by wires as given in the graph. ◼ Each wire has a renting cost. ◼ We want to select some wires, such that all computers are connected (i.e. every two can communicate). ◼ Algorithmic question: How to select a subset of wires with the minimum renting cost? ◼ Answer to this graph? 4 1 4 3 5 4 2 2 2 3 3 2 6 4

More precisely Given a weighted graph G,we want a subgraph G'=(V,E),E'C E,s.t. all vertices are connected on G'. 4 1 0 total weight (w(x,y)is minimized. 4 6 3 Observation:The answer is a tree. 2 Tree:connected graph without cycle Spanning tree:a tree containing all vertices in G. 3 3 Question:Find a spanning tree with 2 minimum weight. The problem is thus called Minimum Spanning Tree (MST). 5
More precisely ◼ Given a weighted graph 𝐺, we want a subgraph 𝐺 ′ = 𝑉,𝐸 ′ ,𝐸 ′ ⊆ 𝐸, s.t. ❑ all vertices are connected on G’. ❑ total weight σ 𝑥,𝑦 ∈𝐸′ 𝑤(𝑥, 𝑦) is minimized. ◼ Observation: The answer is a tree. ❑ Tree: connected graph without cycle ◼ Spanning tree: a tree containing all vertices in 𝐺. ◼ Question: Find a spanning tree with minimum weight. ❑ The problem is thus called Minimum Spanning Tree (MST). 4 1 4 3 5 4 2 2 2 3 3 2 6 5

MST:The abstract problem Input:A connected weighted graph 4 1 口G=(V,E),w:E→R. Output:A spanning tree 4 6 3 with min total weight. 2 2 ▣A spanning tree whose weight is the minimum of 2 3 that of all spanning trees. Any algorithm? 6
MST: The abstract problem ◼ Input: A connected weighted graph ❑ 𝐺 = (𝑉, 𝐸), 𝑤: 𝐸 → ℝ. ◼ Output: A spanning tree with min total weight. ❑ A spanning tree whose weight is the minimum of that of all spanning trees. ◼ Any algorithm? 4 1 4 3 5 4 2 2 2 3 3 2 6 6

Methodology 4:Starting from a naive solution 0 See whether it works well enough If not,try to improve it. A first attempt may not be correct ■ But that's fine.The key is that it'll give you a chance to understand the problem
◼ Methodology 4: Starting from a naïve solution ❑ See whether it works well enough ❑ If not, try to improve it. ◼ A first attempt may not be correct ◼ But that’s fine. The key is that it’ll give you a chance to understand the problem. 7

What if I'm really stingy? I'll first pick the cheapest edge. I'll then again pick the cheapest one in the remaining edges 6 1 I'll just keep doing like this .. 5 6 3 as long as no cycle caused ..until a cycle is unavoidable. 4 Then I've got a spanning tree! No cycle. Connected:Otherwise I can still pick something without causing a cycle. Concern:Is there a better spanning tree? 8
What if I’m really stingy? ◼ I’ll first pick the cheapest edge. ◼ I’ll then again pick the cheapest one in the remaining edges ◼ I’ll just keep doing like this … ❑ as long as no cycle caused ◼ … until a cycle is unavoidable. Then I’ve got a spanning tree! ❑ No cycle. ❑ Connected: Otherwise I can still pick something without causing a cycle. ◼ Concern: Is there a better spanning tree? 6 1 5 4 6 5 4 2 4 3 4 2 6 8

Kruskal's Algorithm What we did just now is Kruskal's algorithm. Repeatedly add the next lightest edge that doesn't produce a cycle... in case of a tie,break it arbitrarily ...until finally reaching a tree ---that's the answer!
Kruskal's Algorithm ◼ What we did just now is Kruskal’s algorithm. ❑ Repeatedly add the next lightest edge that doesn't produce a cycle… ◼ in case of a tie, break it arbitrarily. ❑ …until finally reaching a tree --- that’s the answer! 9

Illustrate an execution of the algorithm -At first all vertices are all separated. Little by little,they merge 5 into groups. Groups merge into larger groups. Finally,all groups merge into one. o That's the spanning tree outputted by the algorithm. 10
Illustrate an execution of the algorithm ◼ At first all vertices are all separated. ◼ Little by little, they merge into groups. ◼ Groups merge into larger groups. ◼ Finally, all groups merge into one. ◼ That’s the spanning tree outputted by the algorithm. 6 1 5 4 6 5 4 2 4 3 4 2 6 10
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 香港中文大学:《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
- 香港中文大学:《Topics in Theoretical Computer Science》课程教学资源(PPT课件讲稿)Lecture 8 Mechanism design.pptx
- 香港中文大学:《Topics in Theoretical Computer Science》课程教学资源(PPT课件讲稿)Lecture 6 Algorithms for resource allocation.pptx
- 香港中文大学:《Design and Analysis of Algorithms》课程教学资源(PPT课件讲稿)Week 4 Randomized Algorithms.pptx
- 香港中文大学:《Design and Analysis of Algorithms》课程教学资源(PPT课件讲稿)Week 5 Dynamic Programming.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