1 / 16

Greedy Algorithms

Greedy Algorithms. Greedy Algorithms. For some problems, dynamic programming is overkill Greedy algorithms make the choice that’s best at that moment makes locally optimal choice in hopes for globally optimal solution will be useful when discussing MSTs, Dijkstra’s, etc.

ganya
Télécharger la présentation

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 Algorithms • For some problems, dynamic programming is overkill • Greedy algorithms make the choice that’s best at that moment • makes locally optimal choice in hopes for globally optimal solution • will be useful when discussing MSTs, Dijkstra’s, etc...

  3. The Activity-Selection Problem • Problem: schedule several competing activities that require exclusive use of a common resource • Could develop a solution similar to that of matrix-chain multiplication • suppose the activity is included • assume that times to the left/right are optimal

  4. Greedy Solution • Obviously, there are 2n combinations • Order activities by increasing finishing time • Pick an activity if it’s non-conflicting • Runs in linear time

  5. a0 1 1 4 a1 a0 2 3 5 a2 a1 3 0 6 a3 a1 4 5 7 a4 a1 5 3 8 a5 a1 a4 6 5 9 a6 a1 a4 7 6 10 a7 a1 a4 8 8 11 a8 9 8 12 a9 a1 a4 a8 10 2 13 a10 a1 a4 a8 11 12 14 a11 12  - a1 a4 a8 a11 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

  6. Elements of a Greedy Algorithm • Determine the optimal substructure of the problem • Develop a recursive solution • Prove that at any stage of the recursion, the greedy choice is optimal • Show that all but one of the subproblems induced by having made the greedy choice arae empty

  7. The Knapsack Problems • Assume a thief is robbing a store with various valued and weighted objects. Her bag can hold only W pounds. • The 0-1 knapsack problem: must take all or nothing • The fractional knapsack problem: can take all or part of the item

  8. Where Greedy Fails 50 50 50 50 50 20 20 --- 30 30 30 30 20 20 knapsack 20 10 10 10 10 $60 $120 =$160 $100 =$220 =$180 =$240

  9. Huffman Codes • A widely used compression algorithm • Savings of 20% - 90% • Uses the frequency of the characters • Fixed codes versus variable-length codes: Letter freq fixed code variable a 45 000 0 b 13 001 101 c 12 010 100 d 16 011 111 e 9 100 1101 f 5 101 1101

  10. Constructing a Huffman Tree f:5 e:9 c:12 b:13 d:16 a:45

  11. Constructing a Huffman Tree 14 c:12 b:13 d:16 a:45 0 1 f:5 e:9

  12. Constructing a Huffman Tree 14 25 d:16 a:45 0 1 0 1 f:5 e:9 c:12 b:13

  13. Constructing a Huffman Tree 30 25 a:45 1 0 0 1 14 d:16 c:12 b:13 0 1 f:5 e:9

  14. Constructing a Huffman Tree 55 a:45 1 0 25 30 1 0 0 1 14 c:12 b:13 d:16 0 1 f:5 e:9

  15. Constructing a Huffman Tree 100 1 0 55 a:45 1 0 25 30 1 0 0 1 14 c:12 b:13 d:16 0 1 f:5 e:9

  16. Summary • For every greedy algorithm, there is a dynamic algorithm • Greedy Algorithms make quick (local) decisions • After a decision is made, problems of size n-1 are left • Greedy does not always yield an optimal solution

More Related