Greedy Algorithms n Activityselection problem n Problem n
Greedy Algorithms
n Activity-selection problem: n Problem: , n activities. Activity i, start time:Si finish time:fi Any two activities i , j are compatible if their intervals do not overlap, i. e. or n Want to schedule as many compatible activities as possible. p 2.
n Greedy-Activity-Selector(s, f): Assume , by quicksort Greedy-Activity-Selector(s, f) { n = s. length; A = {a 1} ; k=1; for m=2 to n do return A } n Complexity: n Thm: The above algorithm schedules optimally. p 3.
n 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 4.
n 0 -1 knapsack problem:( NP complete ) n Problem: n items i-th item: worth dollars weight units A person carry W units. n n Goal:to carry as much value as possible. Fractional knapsack problem: n Problem: Can take fractions of items ( eg. 1/4, …. ) in decreasing order Example:W=50 item 1: item 2: item 3: 取 item 1 , item 2 及 2/3 的 item 3. p 5.
n Huffman codes: a b c d e f Frequency (x 1000) 45 13 12 16 9 5 Fixed-length codeword 000 001 010 011 100 101 variable-length codeword 0 101 100 111 1100 Use fixed-length codeword: Total Use variable-length codeword: Total p 6.
n Prefix codes: No codeword is also a prefix of some other codes. 字首 T: 0 100 1 a : 45 55 1 0 cost of the tree T. 30 25 0 1 0 c : 12 b : 13 14 1 d : 16 0 1 f: 5 e: 9 p 7.
n Constructing a Huffman code: (a) f: 5 e: 9 (b) c : 12 b : 13 c : 12 0 f: 5 (c) 0 f: 5 (d) 0 c : 12 14 25 1 e: 9 0 14 a : 45 0 0 c : 12 25 55 1 b : 13 30 25 1 d : 16 1 e: 9 a : 45 14 a : 45 1 b : 13 a : 45 (f) (同前頁:T) 0 55 1 a : 45 1 0 0 f: 5 a : 45 1 e: 9 0 c : 12 1 b : 13 d : 16 14 d : 16 0 f: 5 (e) b : 13 0 30 1 d : 16 1 e: 9 0 c : 12 25 55 1 b : 13 1 0 0 f: 5 14 30 1 d : 16 1 e: 9
Example:” A SIMPLE STRING TO BE ENCODED USING A MINIMAL NUMBER OF BITS. ” source: 11 3 3 1 2 5 1 2 6 2 4 5 3 1 2 4 3 2 A B C D E F G I L M N D P R S T U 60 0 23 0 0 5 N 11 1 6 I 1 0 0 3 O 6 1 37 0 12 1 16 0 6 8 1 21 1 8 0 1 10 11 1 0 1 0 1 3 B 3 A 0 3 4 4 S 4 M 0 4 5 E 5 1 F 1 2 G 0 2 U 1 2 L 2 D 1 2 R 0 0 1 2 3 T 1 1 1 C P 011011110010011010110001111001110111001000011111011010011101011100111111 00001101001011011001011011110000100100100 00111111101101111010000011010001111 00010100101111110100011101010 0111001
n Huffman(C): n Algorithm: 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) ; } n Complexity: p 10.
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). n 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’’ C C = = 同理
n Lemma 3: T’:full binary tree representing an optimal prefix code over C’ 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. 則可找到 T’’ 使 B(T’’) < B(T) T 是 C 之最佳之 prefix code.
n Thm: Procedure Huffman produces an optimal prefix code. Proof: By Lemma 2 and 3. p 13.
n Matroids: 1. 2. S:a finite set I:a nonempty family of subsets of S ( 即 I 的元素為集合 ) independent subset 並滿足:若 且 則 ( hereditary property ) 3. 若 且 則存在 使得 ( exchange property ) Example:Matric matroid if columns in A are linearly independent. n Graphic matroid: is a graph. n n ( i. e. A forms a forest ) and A:acyclic. p 14.
Example: Matric Matroid: Example: Graphic Matroid: p 15.
n 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. 故 由 1. 2. 3. 定理得證. p 16.
n Thm 6: Proof: All maximal independent subsets in a matroid have the same size. , A is maximal if it has no extension. 即不存在 使得 假設 A:maximal independent subset. B:maximal independent subset 且 存在 使得 A 為 maximal. p 17.
n Weighted Matroid: n Definition: , weight function:w(x) for each n Greedy algorithms on a weighted matroid n Problem: 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. p 18.
n 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 } 若 則上述需 步驟。 p 19.
n Lemma 8 -9: 為任一 matroid. 令 若 且 x 不為 extension. 之 extension, 則 x 不為任一 independent set 之 Proof: 假設 x 為 A 之 extension 但不為 之 extension. independent. x 為 A 之 extension. independent. 由假設 x 不是 之 extension. p 21.
n Lemma 10(Matroids optimal-substructure): 令 x 為 Greedy 演算法中第一個被選入的元素. 尋找 M 中包含 x 之 maximum-weight independent subset 可以 被轉化為尋找 matroid 之 maximum weight ind. subset Proof: 若 且 A:maximum weighted 又 故由 M 中含 x 之 maximum-weight solution 可找到 M’ 之 maximum-weight solution. 反之亦然. p 22.
n 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. p 23.
n A task-scheduling problem: n S={1, 2, …. . , n} n unit-time tasks. n Deadlines: task i 需在 di 前完成. Penalties: 若 task i 不能在 di 前完成,則罰 wi 若 task i 能在 di 前完成者無 penalty. n 目標:安排一執行順序使 penalty 最小. Example: 1 2 3 4 5 6 7 4 2 4 3 1 4 6 70 60 50 40 30 20 10 Schedule:< 2 4 1 3 7 5 6 > penalty 20+30=50. p 24.
n Def: n 在一 schedule 中: n n 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. n Canonical form:same as early-first form and the early tasks are scheduled in order of monotonically increasing deadlines. n k 若 k+1 k i j i, j:early task j k+1 i a set A of task is independent:若存在一 schedule 使得 A 中無 late schedule. n n n 故任一 schedule 中之 early tasks 形成一 independent set. 令 I 表所有 independent set 之集合. 如何判定一 task 集合是否為 independent? t=1, 2, …, n ,令 Nt(A) 表 A 中之 tasks 其 deadline t 之 task 個數.
n Lemma 12: 令 A:tasks 所形成之集合. 則下列敘述為等價 1. A:independent. 2. For t=1, 2, …n, 3. 若 A 中之 tasks 依 deadlines ( nondecreasing ) 排序,則無 late task. Proof: 若 , 則 A 中存在 late task. 故 顯然. p 26.
Thm 13: n S={ unit tasks with deadline } I={ independent sets of tasks } (S, I ) is a matroid. Proof: (1) Clearly. (2) 由上述 independent set 之定義知其滿足 matroid 之第 2 個條件。 (3) Suppose A, B I and |B| > |A|. l Let k be the largest t s. t. Such t exist. It holds at least for t=0. Since Nn(B)=|B| and Nn(A)=|A|, but |B|>|A|, we must have k < n and for all k+1 j n, Nj(B) > Nj(A). l Thus, B contains more tasks with deadline k+1 than A does. l Let task i be in B-A with deadline k+1 and A’ = A {i}. l By property 2 of lemma 12, we show A’ is ind. For t in [0, k], Nt(A’)=Nt(A) t, since A is ind. For t in (k, n], we have Nt(A’) Nt(B) t, since B is ind. Thus A’ is ind. And (S, I ) is a matroid. p 27.
Solution by Greedy Algorithm n Sort w 1, …. . , wn in decreasing order Check A {i} I ? I. e. is A {i} independent? I. e does Nt(A {i}) t hold for t=0, …, n? n n Time complexity: O(n 2). p 28.
- Slides: 28