1 / 32

Greedy Algorithm

Greedy Algorithm. , n 個活動. Start time:S i. 對任一活動 i ,. finish time:f i. or. 任二活動 i , j 可相容表示此兩種活動不重疊。即. Activity-selection problem: Problem: 目的:安排最多的相容活動。. Greedy-Activity-Selector(S,f): Algorithm: Complexity: Thm: 上述演算法可產生最多的相容活動。. , 此可由 quicksort 達到 ~. 假設.

dior
Download Presentation

Greedy Algorithm

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Greedy Algorithm

  2. , n 個活動. Start time:Si 對任一活動 i , finish time:fi or 任二活動 i , j 可相容表示此兩種活動不重疊。即 • Activity-selection problem: • Problem: • 目的:安排最多的相容活動。

  3. Greedy-Activity-Selector(S,f): • Algorithm: • Complexity: • Thm:上述演算法可產生最多的相容活動。 , 此可由 quicksort 達到 ~ 假設 Greedy-Activity-Selector(S,f) { n = length(S) ; A = {1} ; j=1; for i=2 to n do if then return A }

  4. Greedy 方法之要素: • 1. Globally optimal solution 可由選擇 local optimal solution 獲得。 • 2. 一最佳解包含子問題之最佳解。 ( 此與 Dynamic Programming 類似 )

  5. worth dollars i-th item: weight units • 0-1 knapsack problem:( NP complete ) • Problem: • Goal:to carry as much value as possible. • Fractional knapsack problem: • Problem: n items n items A person can carry W units. 同上,每一件東西可只取部分 ( 如 1/4, …. ) 依序由大而小排列 Example:W=50 item1: 取 item1 , item2 及 item2: 2/3 的 item3. item3:

  6. a b c d e f Frequency (x1000) 9 45 13 12 16 5 Fixed-length codeword 000 001 010 011 100 101 variable-length codeword 0 101 100 111 1101 1100 • Huffman codes: 若用 fixed-length codeword: 總共有 若用 variable-length codeword: 總共有

  7. 100 a : 45 c : 12 d : 16 b : 13 No codeword is also a prefix of some other codes. • Prefix codes: 字首 T: 0 1 55 1 0 cost of the tree T. 30 25 0 0 1 1 14 0 1 f : 5 e : 9

  8. e : 9 e : 9 f : 5 f : 5 e : 9 f : 5 f : 5 e : 9 f : 5 e : 9 f : 5 c : 12 b : 13 d : 16 a : 45 c : 12 b : 13 d : 16 b : 13 c : 12 b : 13 a : 45 b : 13 a : 45 d : 16 d : 16 c : 12 c : 12 a : 45 c : 12 b : 13 d : 16 a : 45 d : 16 a : 45 14 14 14 14 14 1 1 1 1 1 0 0 0 0 0 25 25 1 1 0 0 30 30 30 55 1 1 1 0 0 0 1 0 25 1 0 (a) e : 9 (b) • Constructing a Huffman code: (c) (d) (f) (同前頁:T) 55 1 0 (e) 55 1 0 25 1 0

  9. 2 4 3 3 2 2 5 1 1 1 6 6 2 5 3 2 5 4 2 2 3 4 3 3 2 3 5 1 1 1 2 4 2 3 11 23 0 1 11 12 1 0 0 1 6 6 I N 1 0 1 0 21 3 1 0 O B A 1 0 11 10 1 0 F G 5 1 E 0 2 1 0 T C P Example:” A SIMPLE STRING TO BE ENCODED USING A MINIMAL NUMBER OF BITS.” source: A B C D E F G I L M N D P R S T U 60 0 1 37 0 1 16 1 0 8 8 1 1 0 0 4 4 1 0 M S 1 0 U L D R 0 1 1 0 1 1 1 1 0 0 1 0 0 1 1 0 1 0 1 1 0 1 0 1 1 0 0 0 1 1 1 0 0 1 1 1 1 0 0 1 1 1 0 1 1 1 0 1 1 1 0 0 1 0 0 0 0 1 1 1 1 1 1 1 1 1 0 1 1 0 1 0 0 1 1 1 0 1 0 1 1 1 0 0 1 1 1 1 1 1 0 0 0 0 1 1 0 1 0 0 0 1 0 0 1 0 1 1 0 1 1 0 0 1 0 1 1 0 1 1 1 1 0 0 0 0 1 0 0 1 0 0 1 0 0 0 0 1 1 1 1 1 1 1 0 1 1 0 1 1 1 1 0 1 0 0 0 1 0 0 0 0 0 1 1 0 1 0 0 1 1 0 1 0 0 0 1 1 1 1 0 0 0 1 0 0 0 0 1 0 1 0 0 1 0 1 1 1 0 0 1 0 1 1 1 1 1 1 0 1 0 0 0 1 1 1 0 1 1 1 0 1 0 1 0 0 1 1 1 0 1 1 1 0 0 1

  10. A Huffman tree is a binary tree of integers with these two properties: Each internal node is the sum of of its children Its weighted external path length is minimal

  11. Algorithm: Generating a Huffman code Input: a sequence of characters output: a bit code for the input characters Postconditions: the bit code has the unique prefix property and is optimal • Tally the frequencies of the input characters • Load the letter-frequency pairs into a min priority queue • Coalesce the pairs into a Huffman tree • Encode the character at each leaf with the bit sequence along root-to-leaf path

  12. 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) ; }

  13. C C C C C :字元 , :字母 , c:出現 次. 令 且 為最小的兩個. • Lemma 17.2: 存在一最佳的 prefix-free code, 使 x,y 之碼等長且只差最後一個 bit. Proof: T ( optimal ) T’ T’’ = = 同理

  14. C C T 是 C 之最佳之 prefix code. 令 T:full binary tree representing an optimal prefix code over . • Lemma 17.3: leaves T’ represents an optimal prefix code for C’. Proof: 對任一 若 T’ 不是 C’ 的最佳 prefix code. 則可找到 T’’ 使 B(T’’) < B(T’)

  15. Huffman 之演算法可產生最佳之 prefix code. • Thm: Proof: 由 lemma 17.2 , 17.3. Example: 若用 { 0 , 1 , 2 } 來編碼,試將上述方法一般化以求得最佳之編碼方法。

  16. Matroids: • 1. S:有限非空集合 • 2. X:為 S 的部分集合所形成之集合 ( 即 X 的元素為集合 ) 並滿足:若 且 則 • 3. 若 且 則存在 使得 • Graphic matroid: • 且 A:acyclic. independent subset ( hereditary property ) ( exchange property ) Example:Matric matroid if rows in I are linearly independent. 為一 graph. ( i.e. A forms a forest )

  17. Thm 17.5: 若 G 為一 undirected graph 則 為 一 matroid. Proof: 1. E:finite 2. is hereditary, acyclic graph 之部分仍為 acyclic. 3. 假設 A,B 為 G 中之 forests. 且 A:包含 個 trees. B:包含 個 trees. ( B 的樹較少 ) ( 但有較大顆的 ) 故在 B 中有一樹 T 包含 A 中的 2顆樹的vertices. 亦即 T 中存在一 edge (u,v) 使得 u 和 v 分佈在 A 中的兩顆樹. 故將 (u,v) 加到 A 中 不會產生 cycle. 故 由 1. 2. 3. 定理得證.

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

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

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

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

  22. Thm 17.8: 令 為任一 matroid. 若 且 x不為 之 extension, 則 x不為任一 independent set 之 extension. Proof: 假設 x為 A 之 extension 但不為 之 extension. independent. x為 A 之 extension. 由假設 x不是 之 extension. independent.

  23. Thm 17.9(Matroids optimal-substructure): • Thm 17.10: 令 x為 Greedy 演算法中第一個被選入的元素. 尋找 M 中包含 x 之 maximum-weight independent subset 可以 被轉化為尋找 matroid 之 maximum weight ind. Set. Proof: 若 且 且 A:maximum weighted 反之若 則 又 故由 M 中含 x 之 maximum-weight solution 可找到 M’ 之 maximum-weight solution. 反之亦然. 給定 , 則 Greedy(M,w) 可以找到 optimal solution. Proof:

  24. 1 2 3 4 5 6 7 4 2 4 3 1 4 6 70 60 50 40 30 20 10 • A task-scheduling problem: • S={1,2,…..,n} n unit-time tasks. • Deadlines: task i 需在 di前完成. • Penalties: 若 task i 不能在 di前完成,則罰 wi 若 task i 能在 di前完成著無 penalty. 目標:安排一執行順序使 penalty 最小. Example: Schedule:< 2 4 1 3 7 5 6 > penalty 20+30=50.

  25. k k+1 j i • 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 nondecreasing deadlines. • a set A of task is independent:若存在一 schedule 使得 A 中無 late schedule. • 故任一 schedule 中之 early tasks 形成一 independent set. 令X表所有 independent set 之集合. • 如何判定一 tasks 集合是否為 independent? k k+1 i j 若 i,j:early task t=1,2,…,n ,令 Nt(A) 表 A 中之 tasks 其 deadline t.

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

  27. Thm 17.12: S={ unit tasks with deadline } (S,X) is a matroid. X={ independent sets of tasks } Proof: • clearly. • 由上述 independent set 之定義知其滿足 matroid 之第 2 個條件。 • 且

  28. Solution by Greedy Algorithm • Sort w1,….., wn in decreasing order • Check A  {i}  X ? • Time complexity: O(n*n).

  29. 0 0 0 2 2 2 0 0 1 0 1 2 1 1 3 1 1 1 0 1 1 1 0 2 1 2 4 51 a 2 50 49 r 1 48 47 NYT d 45 46 1 2 3 51 51 51 NYT NYT a NYT a a 1 49 50 49 50 51 0 50 49 (a) (aa) r 47 48 (aar) 4 51 a 2 50 49 r 1 48 47 d 1 46 45 (aardv) (aard) v NYT 4 43 44 51 5 a 2 51 2 a 50 3 49 r 2 r 47 2 48 d 1 d 46 45 1 NYT v (aardv) (aardv) NYT 43 44 v

  30. start stop Node number max in block? Read in symbol Call update procedure Send code for NYT node followed by Index in the NYT list Code is the path from The root node to the corresponding node • Encoding: • Adaptive Huffman Coding:( dynamic ) Is this the first appearance of the symbol Yes No No Yes

  31. start stop Read bit and go to corresponding node Call update procedure Is the node the NYT node? Read e bits Read e bits Is the e-bit number p less than r? Read one more bit Is this the last bit? • Decoding: Go to root Of the tree • Adaptive Huffman Coding:( dynamic ) Is the node an external node? No Yes Yes No Yes Yes Decode the (P+1) element in NYT list

  32. start stop First appearance for symbol? Node number max in block? NYT gives birth to new NYT and External node Increment weight Of external node and old NYT node Go to symbol external node Go to symbol external node Switch node with highest numbered node in block Is this the root node? Go to parent node Yes • Updating: No Go to old NYT node No Yes Yes No

More Related