Greedy Algorithms. p2. Activity-selection problem: Problem : Want to schedule as many compatible activities as possible., n activities. Activity i, start.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms
Advertisements

CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Algorithm Design Techniques: Greedy Algorithms. Introduction Algorithm Design Techniques –Design of algorithms –Algorithms commonly used to solve problems.
Comp 122, Spring 2004 Greedy Algorithms. greedy - 2 Lin / Devi Comp 122, Fall 2003 Overview  Like dynamic programming, used to solve optimization problems.
Greedy Algorithms Greed is good. (Some of the time)
Greed is good. (Some of the time)
Greedy Algorithms Be greedy! always make the choice that looks best at the moment. Local optimization. Not always yielding a globally optimal solution.
Greedy algorithm 叶德仕
T(n) = 4 T(n/3) +  (n). T(n) = 2 T(n/2) +  (n)
Greedy Algorithms for Matroids Andreas Klappenecker.
1 Huffman Codes. 2 Introduction Huffman codes are a very effective technique for compressing data; savings of 20% to 90% are typical, depending on the.
CPSC 411, Fall 2008: Set 4 1 CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Fall 2008.
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
Chapter 9 Greedy Technique Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Greedy Algorithms CIS 606 Spring Greedy Algorithms Similar to dynamic programming. Used for optimization problems. Idea – When we have a choice.
CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 5. Greedy Algorithms - 1 Greedy.
Data Structures – LECTURE 10 Huffman coding
Greedy Algorithms Huffman Coding
Lecture 10 Matroid. Independent System Consider a finite set S and a collection C of subsets of S. (S,C) is called an independent system if i.e., it is.
Lecture 7: Greedy Algorithms II
CPSC 411, Fall 2008: Set 4 1 CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Fall 2008.
CS420 lecture eight Greedy Algorithms. Going from A to G Starting with a full tank, we can drive 350 miles before we need to gas up, minimize the number.
16.Greedy algorithms Hsu, Lih-Hsing. Computer Theory Lab. Chapter 16P An activity-selection problem Suppose we have a set S = {a 1, a 2,..., a.
Lecture 1: The Greedy Method 主講人 : 虞台文. Content What is it? Activity Selection Problem Fractional Knapsack Problem Minimum Spanning Tree – Kruskal’s Algorithm.
Advanced Algorithm Design and Analysis (Lecture 5) SW5 fall 2004 Simonas Šaltenis E1-215b
1 Greedy algorithm 叶德仕 2 Greedy algorithm’s paradigm Algorithm is greedy if it builds up a solution in small steps it chooses a decision.
Data Structures and Algorithms A. G. Malamos
Greedy Algorithms and Matroids Andreas Klappenecker.
Introduction to Algorithms Chapter 16: Greedy Algorithms.
Divide-and-Conquer & Dynamic Programming Divide-and-Conquer: Divide a problem to independent subproblems, find the solutions of the subproblems, and then.
 Rooted tree and binary tree  Theorem 5.19: A full binary tree with t leaves contains i=t-1 internal vertices.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 9 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Dynamic programming vs Greedy algo – con’t Input: Output: Objective: a number W and a set of n items, the i-th item has a weight w i and a cost c i a subset.
1 Greedy Technique Constructs a solution to an optimization problem piece by piece through a sequence of choices that are: b feasible b locally optimal.
Greedy Algorithms Z. GuoUNC Chapel Hill CLRS CH. 16, 23, & 24.
COSC 3101A - Design and Analysis of Algorithms 9 Knapsack Problem Huffman Codes Introduction to Graphs Many of these slides are taken from Monica Nicolescu,
Greedy Algorithms.
Huffman Codes. Overview  Huffman codes: compressing data (savings of 20% to 90%)  Huffman’s greedy algorithm uses a table of the frequencies of occurrence.
Greedy Algorithms Chapter 16 Highlights
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2010 Lecture 2 Tuesday, 2/2/10 Design Patterns for Optimization.
1 Chapter 16: Greedy Algorithm. 2 About this lecture Introduce Greedy Algorithm Look at some problems solvable by Greedy Algorithm.
Greedy Algorithms Analysis of Algorithms.
Huffman encoding.
Greedy Algorithms Lecture 10 Asst. Prof. Dr. İlker Kocabaş 1.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 18.
6/13/20161 Greedy A Comparison. 6/13/20162 Greedy Solves an optimization problem: the solution is “best” in some sense. Greedy Strategy: –At each decision.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 17.
CS6045: Advanced Algorithms Greedy Algorithms. Main Concept –Divide the problem into multiple steps (sub-problems) –For each step take the best choice.
CSCI 58000, Algorithm Design, Analysis & Implementation Lecture 12 Greedy Algorithms (Chapter 16)
Greedy Algorithms General principle of greedy algorithm
CSCE 411 Design and Analysis of Algorithms
Greedy Technique.
Chapter 8 The Greedy Approach.
Introduction to Algorithms`
Greedy Algorithm.
Advanced Algorithms Analysis and Design
Chapter 16: Greedy Algorithm
CS6045: Advanced Algorithms
Advanced Algorithms Analysis and Design
Greedy Algorithms Many optimization problems can be solved more quickly using a greedy approach The basic principle is that local optimal decisions may.
Chapter 16: Greedy algorithms Ming-Te Chi
Lecture 8 Greedy Approach
Greedy Algorithms TOPICS Greedy Strategy Activity Selection
Data Structure and Algorithms
Chapter 16: Greedy algorithms Ming-Te Chi
Algorithm Design Techniques Greedy Approach vs Dynamic Programming
Lecture 2: Greedy Algorithms
Huffman Coding Greedy Algorithm
Huffman codes Binary character code: each character is represented by a unique binary string. A data file can be coded in two ways: a b c d e f frequency(%)
Analysis of Algorithms CS 477/677
Presentation transcript:

Greedy Algorithms

p2. Activity-selection problem: Problem : Want to schedule as many compatible activities as possible., n activities. Activity i, start time : S i finish time : f i Any two activities i, j are compatible if their intervals do not overlap, i.e. or

p3. Greedy-Activity-Selector(s,f) : Complexity : Thm: The above algorithm schedules optimally. Assume, by quicksort Greedy-Activity-Selector(s, f) { n = s.length; A = {a 1 } ; k=1; for m=2 to n do return A }

p4. Elements of the Greedy strategy : 1. Greedy-choice property: Assemble globally optimal solution by making locally optimal choice. 2. Optimal substructure: An optimal solution to the problem contains within it optimal solutions to subproblems.

p knapsack problem : ( NP complete ) Problem : Goal : to carry as much value as possible. Fractional knapsack problem : Problem : n items i-th item : worth dollars weight units A person can carry W units. Can take fractions of items ( eg. 1/4, …. ) in decreasing order Example : W=50 item1 : item2 : item3 : 取 item1, item2 及 2/3 的 item3.

p6. Huffman codes : a Frequency (x1000) Fixed-length codeword variable-length codeword b c d e f Total Use fixed-length codeword : Total Use variable-length codeword :

p7. Prefix codes : No codeword is also a prefix of some other codes. 字首 T:T: a : d : b : c : e : 9 1 f : 5 0 cost of the tree T.

Constructing a Huffman code : (a) f : 5 (b) (c) e : 9 c : 12b : 13d : 16a : 45c : 12b : f : 5e : 9 d : 16a : f : 5e : 9 d : c : 12b : 13 a : 45 (d) c : 12b : f : 5e : d : 16 a : 45 (e) a : f : 5e : d : c : 12b : (f) f : 5e : d : c : 12b : a : 45 ( 同前頁: T)

source : Example : ” A SIMPLE STRING TO BE ENCODED USING A MINIMAL NUMBER OF BITS.” ABCDEFGILMNDPRSTU N I FG OB 6 3 A UL 8 4 S DR 8 4 M CP 5 3 T E

p10. Huffman(C) : Algorithm : Complexity : Huffman(C) { // Q : priority queue for i=1 to n-1 do {z = allocate-Node() ; x = left(z) = Extract-min(Q) ; y = right(z) = Extract-min(Q) ; f(z) = f(x) + f(y) ; Insert(Q, z) ; } return Extract-min(Q) ; }

Lemma 2 : C : alphabet set, each c in C with frequency f(c). Let x, y in C with the lowest frequencies f(x) and f(y). Then there exists an optimal prefix code, where the code words for x and y have the same length and differ in the last bit. Proof : T ( optimal ) T’T’’ CC == 同理

Lemma 3 : T’ : full binary tree representing an optimal prefix code over C’ 若 T 不是 C 的最佳 prefix code. 則可找到 T’’ 使 B(T’’) < B(T) leaves T, obtained from T’ by replacing z with an internal node with children x and y, is an optimal prefix code. Proof : For any T 是 C 之最佳之 prefix code.

p13. Thm : Procedure Huffman produces an optimal prefix code. Proof: By Lemma 2 and 3.

p14. Matroids : 1. S : a finite set 2. I : a nonempty family of subsets of S ( 即 I 的元素為集合 ) 並滿足:若 且 則 3. 若 且 則存在 使得 Graphic matroid : and A : acyclic. independent subset ( hereditary property ) ( exchange property ) Example : Matric matroid if columns in A are linearly independent. is a graph. ( i.e. A forms a forest )

p15. Example: Matric Matroid : Example: Graphic Matroid:

p16. Thm 5 : If G is an undirected graph then is a matroid. Proof : 1. E : finite 2. is hereditary, acyclic graph 之部分仍為 acyclic. 3. 假設 A,B 為 G 中之 forests 且 A :包含 個 trees. B :包含 個 trees.( B 的樹較少棵 ) ( A single node is counted as a tree.) 故在 B 中有一樹 T 包含 A 中的 2 棵樹的 vertices. Why? 亦即 T 中存在一 edge (u,v) 使得 u 和 v 分佈在 A 中的兩棵樹. 故將 (u,v) 加到 A 中 不會產生 cycle. 故 由 定理得證.

p17. Thm 6 : All maximal independent subsets in a matroid have the same size. Proof :, A is maximal if it has no extension. 即不存在 使得 假設 A : maximal independent subset. B : maximal independent subset 且 存在 使得 A 為 maximal.

p18. Weighted Matroid : Definition : Greedy algorithms on a weighted matroid Problem :, weight function : w(x) for each Given, find has maximal possible weight. Example : Minimum Spanning Tree : weight function defined on E. Define, and Let A be a maximal independent set in I. Then A corresponds to a spanning tree in G.

p19. Algorithm : Greedy(M,w) { Sort M.S into monotonically decreasing order by weight w ; For each, taken in monotonically decreasing order by w(x) ; do if then Return A } 若 則上述需 步驟。

Lemma 7 : 令 為一加權 ( weight ) matroid. Proof : S 依加權函數 w, 排列由大到小. 令 x 為 S 中第一個使 之 independent 元素 ( 但此 x 並不一定存在 ). 若此 x 存在,則存在一最佳 ( 即 w(A) 最大 ) 之 A 若沒有上述 x 存在,則 為唯一的 independent set. Why ? 現假設 B 為一非空之 optimal subset. 若, 則取 A 等於 B 故得證. 若, 則 B 中之元素的 weight 不會比 x 之 weight 大. 假設 但已知 令 由 exchange property, 可將 A 擴充至 而且 A 仍保持 為 independent. 取 B 為 optimal A 為 optimal 且含 x.

p21. Lemma 8-9 : Proof : 假設 x 為 A 之 extension 但不為 之 extension. 令 為任一 matroid. 若 且 x 不為 之 extension, 則 x 不為任一 independent set 之 extension. independent. x 為 A 之 extension. independent. 由假設 x 不是 之 extension.

p22. Lemma 10(Matroids optimal-substructure) : Proof : 若 且 且 A : maximum weighted 令 x 為 Greedy 演算法中第一個被選入的元素. 尋找 M 中包含 x 之 maximum-weight independent subset 可以 被轉化為尋找 matroid 之 maximum weight ind. subset 又 故由 M 中含 x 之 maximum-weight solution 可找到 M’ 之 maximum-weight solution. 反之亦然.

p23. Thm 11 : 給定, 則 Greedy(M,w) 可以找到 optimal solution. Proof: By Lemma 16.9, pass over all elements that are not extensions of . By lemma 16.7, once the first x is selected, Greedy is safe to add x to A. Lemma 16.10, implies that the remaining problem is one of finding an Optimal subset in the matroid M’ which is the contraction of M by x.

p24. A task-scheduling problem : S={1,2,…..,n} n unit-time tasks. Deadlines : task i 需在 d i 前完成. Penalties : 若 task i 不能在 d i 前完成,則罰 w i 若 task i 能在 d i 前完成者無 penalty. 目標:安排一執行順序使 penalty 最小. Example : Schedule : penalty 20+30=50.

Def : 在一 schedule 中: late task : if it finishes after its deadline. early task : if it finishes before its deadline. Early-first form : early tasks precede the late tasks. Canonical form : same as early-first form and the early tasks are scheduled in order of monotonically increasing deadlines. a set A of task is independent :若存在一 schedule 使得 A 中無 late schedule. 故任一 schedule 中之 early tasks 形成一 independent set. 令 I 表所有 independent set 之集合. 如何判定一 task 集合是否為 independent ? 若 i j kk+1 i,j : early task j kk+1 i t=1,2,…,n ,令 N t (A) 表 A 中之 tasks 其 deadline t 之 task 個數.

p26. Lemma 12 : Proof : 若, 則 A 中存在 late task. 令 A : tasks 所形成之集合. 則下列敘述為等價 1. A : independent. 2. For t=1,2,…n , 3. 若 A 中之 tasks 依 deadlines ( nondecreasing ) 排序,則無 late task. 故 顯然.

p27. Thm 13 : Proof : (1)Clearly. (2) 由上述 independent set 之定義知其滿足 matroid 之第 2 個條件。 (3)Suppose A, B  I and |B| > |A|. Let k be the largest t s.t. Such t exist. It holds at least for t=0. Since N n (B)=|B| and N n (A)=|A|, but |B|>|A|, we must have k N j (A). Thus, B contains more tasks with deadline k+1 than A does. Let task i be in B-A with deadline k+1 and A’ = A  {i}. By property 2 of lemma 12, we show A’ is ind. For t in [0, k], N t (A’)=N t (A)  t, since A is ind. For t in (k, n], we have N t (A’)  N t (B)  t, since B is ind. Thus A’ is ind. And (S, I ) is a matroid. S={ unit tasks with deadline } I={ independent sets of tasks } (S,I ) is a matroid.

p28. Solution by Greedy Algorithm Sort w 1,….., w n in decreasing order Check A  {i}  I ? I.e. is A  {i} independent? I.e does N t (A  {i})  t hold for t=0,…,n? Time complexity: O(n 2 ).