1 / 22

Greedy Algorithms

Greedy methods select the best immediate choice at each decision step, offering efficient yet sometimes suboptimal solutions for optimization problems. Learn about the elements, design, and examples of Greedy Algorithms in activity selection, knapsack problems, Huffman codes, and optimal coding tree concepts. Understand the approach, challenges, and benefits of this algorithmic strategy.

twarfield
Download Presentation

Greedy Algorithms

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 Algorithms

  2. Greedy Methods(描述1) • 解最佳化問題的演算法, 其解題過程可看成是由一連串的決策步驟所組成, 而每一步驟都有一組選擇要選定. • 一個 greedy method在每一決策步驟總是選定那目前看來最好 的選擇. • Greedy methods 並不保證總是得到最佳解, 但在有些問題卻可以得到最佳解. Greedy Algorithms

  3. Greedy Methods(描述2) • Greedy 演算法經常是非常有效率且簡單的演算; 但 但較難證明其正確性(與 DP 演算法比較). • 很多 heuristic algorithms 都採用 greedy methods 的策略. Greedy Algorithms

  4. 1 4 9 假設活動i, 其提出申請使用場地的時段為一半關半開的區間 [si,fi), 並以符號Ii代表. 2 7 11 3 8 5 10 6 時間軸 一個活動選擇問題 (定義) 假設有 n個 活動 提出申請要使用一個場地, 而這場地在同一時間點時最多只能讓一個活動使用. 問題是:從這 n個活動選一組數量最多, 且可以在這場地舉辦的活動集. Greedy Algorithms

  5. N[i]: i 一個活動選擇問題 (設計1) • Let P(A) denote the problem with A as the given set of proposed activities and S denote an optimal solution of P(A). For any activity i in A, we have 1. i S S is an optimal solution of P(A\{i}). 2. i S S \{i} is an optimal solution of P(A\N[i]) but not necessary an optima solution of P(A\{i}). N[i]={jA: IjIi} Greedy Algorithms

  6. 一個活動選擇問題 (設計2) • What kind of activity i in A will be contained in an optimal solution of P(A) : an activity with 1. minimum fisior 2.minimum |N[i]| or 3. minimum fi or 4.minimum si. Answer : . Proof : Let f1= min { fi } andSbe an optimal solution ofP(A). If 1S then there is one and only one activity in S, say j, such that IjI1. Then S \{j}{1}is also an optimal solution. Greedy Algorithms

  7. 一個活動選擇問題 (程式+例子) Input: isifi 1 1 4 2 3 5 3 0 6 4 5 7 5 3 8 6 5 9 7 6 10 8 8 11 9 8 12 10 9 13 11 12 14 Greedy-ASP(s, f, n) { /* f[1]  f[2]  …  f[n]*/ Ans = {1}; for(i=2, j=1; in; i++) if(s[i]f[j]){Ans = Ans {i}; j = i; } } 1 4 9 2 7 11 3 8 5 10 6 time Greedy Algorithms

  8. Greedy 演算法的要素 • Optimal substructure (a problem exhibits optimal substructure if an optimal solution to the problem contains within it optimal solutions to subproblems) • Greedy-choice property • Priority queue or sorting Greedy Algorithms

  9. Knapsack Problem(Greedy vs. DP) Given n items: weights: w1 w2 … wn values: v1 v2 … vn a knapsack of capacity W Find the most valuable load of the items that fit into the knapsack Example: item weight value Knapsack capacityW=16 1 2 $20 2 5 $30 3 10 $50 4 5 $10 Greedy Algorithms

  10. Knapsack Problem Given n items: weights: w1 w2 … wn values: v1 v2 … vn a knapsack of capacity W T[i, j]: the optimal solution using item 1,...,iwith weight at most j. If wi> j T[i, j] = T[i-1, j]; otherwise T[i, j] = max{ T[i-1, j], wi +T[i-1, j - wi]}. How good is this method? Greedy Algorithms

  11. 0-1 and Fractional Knapsack Problem • Constraints of 2 variants of the knapsack problem: • 0-1 knapsack problem: each item must either be taken or left behind. • Fractional knapsack problem: the thief can take fractions of items. • The greedy strategy of taking as mush as possible of the item with greatest vi / wi only works for the fractional knapsack problem. Greedy Algorithms

  12. Huffman Codes • A very effective technique for compressing data • Consider the problem of designing a binary character code • Fixed length code vs. variable-length code, e.g.: Alphabet: a b c d e f Frequency in a file 45 13 12 16 9 5 Fixed-length codeword000 001 010 011 100 101 Variable-length codeword 0 101 100 111 1101 1100file length 1 = 300; file length 2 = 224 Compression ratio = (300224)/300100%  25% Greedy Algorithms

  13. Prefix Codes & Coding Trees • We consider only codes in which no codeword is also a prefix of some other codeword. • The assumption is crucial for decoding variable-length code (using a binary tree). E.g.: 1 0 a:45 1 0 1 0 1 0 d:16 c:12 b:13 0 1 f: 5 e: 9 Greedy Algorithms

  14. Optimal Coding Trees • For a alphabet C, and its corresponding coding tree T, let f(c) denote the frequency of cC in a file, and let dT(c) denote the depth of c’s leaf in T. (dT(c) is also the length of the codeword for c.) • The size required to encode the file is thus:B(T) = cC f(c)dT(c) • We want to find a coding tree with minimum B(T). Greedy Algorithms

  15. Observation 1 • Any optimal coding tree for C, |C| > 1, must be a full binary tree, in which every nonleaf node has two children. E.g.: for the fixed-length code: b:13 c:12 d:16 a:45 e: 9 f: 5 Greedy Algorithms

  16. Observation 2 c2 c1 • Assume C = { c1, c2, … , cn}, and f(c1)  f(c2) …  f(cn). Then there exists an optimal coding tree T such that : T: anddT(c1) =dT(c2) = maxcC dT(c) Greedy Algorithms

  17. Observation 3 c2 c1 • If is T an optimal coding tree for C , then T' is an optimal coding tree for C \{c1, c2} {c'} with f(c') =f(c1) + f(c2). T: T': c' c2 c1 Greedy Algorithms

  18. Huffman’s Algorithm (例) 14 c:12 b:13 d:16 a:45 f: 5 e: 9 14 25 d:16 a:45 f: 5 e: 9 c:12 b:13 f: 5 e: 9 c:12 b:13 d:16 a:45 Greedy Algorithms

  19. Huffman’s Algorithm (例-續1) 25 30 a:45 14 c:12 b:13 d:16 f: 5 e: 9 14 25 d:16 a:45 f: 5 e: 9 c:12 b:13 Greedy Algorithms

  20. Huffman’s Algorithm (例-續2) 55 a:45 25 30 14 c:12 b:13 d:16 f: 5 e: 9 25 30 a:45 14 c:12 b:13 d:16 f: 5 e: 9 Greedy Algorithms

  21. Huffman’s Algorithm (例-續3) 100 55 1 a:45 0 55 a:45 25 1 30 0 25 30 14 c:12 b:13 d:16 1 0 1 0 14 d:16 c:12 b:13 f: 5 e: 9 0 1 f: 5 e: 9 Greedy Algorithms

  22. Huffman’s Algorithm (pseudo-code) Huffman(C) QC // Q :priority queue while(|Q| > 1) 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) Time efficiency:. Greedy Algorithms

More Related