1 / 58

Greedy Algorithms

Greedy Algorithms. Amihood Amir Bar-Ilan University. Idea. Simplest type of strategy: 1. Take a step that makes the problem smaller. 2. iterate. Difficulty: Prove that this leads to an optimal solution. This is not always the case!. Example: Centerstring Problem.

fkim
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 Amihood Amir Bar-Ilan University

  2. Idea Simplest type of strategy: 1. Take a step that makes the problem smaller. 2. iterate. Difficulty: Prove that this leads to an optimal solution. This is not always the case!

  3. Example: Centerstring Problem Input: kstrings s1,…,sk of length ℓ over alphabet Σ, distance d. Find: string s* such that max(Ham(s*,si)), i=1,…k is ≤ d. 3

  4. Our Problem: lengthℓ kstrings Maximum distance is smallest 4

  5. Example: s1: 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 s2: 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 s3: 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 -------------------------------------------------- s*:0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 The Hamming distance of the consensus from any string:4 5

  6. Suggestion: greedy strategycolumn majority? 0 1 1 1 0 0 0 0 1 0 1 1 0 0 0 0 1 1 0 0 1 1 1 0 1 0 1 0 0 1 0 1 0 1 0 0 1 0 1 0 1 0 0 0 1 1 0 0 1 0 1 0 1 0 1 0 0 1 1 0 0 0 1 0 1 0 1 1 0 0 1 0 0 1 0 0 1 0 1 0 0 1 1 0 --------------------------------------- 0 1 1 1 0 0 0 0 1 0 1 1 0 0 0 0 1 1 0 0 1 0 1 0 1 0 1 0 Problem: Works if we want to minimize average Not if we want to minimize maximum! 6

  7. Why? 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ----------------------------------------- 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 (majority) Hamming distance from last string: 16 7

  8. But: 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ----------------------------------------- 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 Hamming distance from any string: 8 8

  9. Example (that works) –Huffman code Computer Data Encoding: How do we represent data in binary? Historical Solution: Fixed length codes. Encode every symbol by a unique binary string of a fixed length. Examples: ASCII (7 bit code), EBCDIC (8 bit code), …

  10. American Standard Code for Information Interchange

  11. ASCII Example: AABCAA AABC AA 1000001 1000001 1000010 1000011 1000001 1000001

  12. Total space usage in bits: Assume an ℓ bit fixed length code. For a file of n characters Need nℓ bits.

  13. Variable Length codes Idea: In order to save space, use less bits for frequent characters and more bits for rare characters. Example: suppose alphabet of 3 symbols: { A, B, C }. suppose in file: 1,000,000 characters. Need 2 bits for a fixed length code for a total of 2,000,000 bits.

  14. Variable Length codes - example Suppose the frequency distribution of the characters is: Encode: Note that the code of A is of length 1, and the codes for B and C are of length2

  15. Total space usage in bits: Fixed code: 1,000,000 x 2 = 2,000,000 Varable code: 999,000 x 1 + 500 x 2 500 x 2 1,001,000 A savings of almost 50%

  16. How do we decode? In the fixed length, we know where every character starts, since they all have the same number of bits. Example:A = 00 B = 01 C = 10 000000010110101001100100001010 A A A B B C C C B C B A A C C

  17. How do we decode? In the variable length code, we use an idea called Prefix code, where no code is a prefix of another. Example:A = 0 B = 10 C = 11 None of the above codes is a prefix of another.

  18. How do we decode? Example:A = 0 B = 10 C = 11 So, for the string: A A A B B C C C B C B A A C C the encoding: 0 0 01010111111101110 0 01111

  19. Prefix Code Example:A = 0 B = 10 C = 11 Decode the string 0 0 01010111111101110 0 01111 A A A B B C C C B C B A A C C

  20. Desiderata: • Construct a variable length code for a given file with the following properties: • Prefix code. • Using shortest possible codes. • Efficient. • As close to entropy as possible.

  21. Idea Consider a binary tree, with: 0 meaning a left turn 1 meaning a right turn. 0 1 A 0 1 B 0 1 C D

  22. Idea Consider the paths from the root to each of the leaves A, B, C, D: A : 0 B : 10 C : 110 D : 111 0 1 A 0 1 B 0 1 C D

  23. Observe: • Thisis a prefix code, since each of theleaves has a path ending in it, without continuation. • If the tree is full then we are not “wasting” bits. • If we make sure that • the more frequent • symbols are closer to • the root then they will • have a smaller code. 0 1 A 0 1 B 0 1 C D

  24. Greedy Algorithm: 1. Consider all pairs: <frequency, symbol>. 2. Choose the two lowest frequencies, and make them brothers, with the root having the combined frequency. 3. Iterate.

  25. Greedy Algorithm Example: Alphabet: A, B, C, D, E, F Frequency table: Total File Length: 210

  26. Algorithm Run: B20 C30 D40 E50 F60 A10

  27. Algorithm Run: X 30 C30 D40 E50 F60 B20 A10

  28. Algorithm Run: Y 60 D40 E50 F60 X 30 C30 B20 A10

  29. Algorithm Run: D40 E50 Y 60 F60 X 30 C30 B20 A10

  30. Algorithm Run: Z 90 Y 60 F60 E50 D40 X 30 C30 B20 A10

  31. Algorithm Run: Y 60 F60 Z 90 X 30 C30 E50 D40 B20 A10

  32. Algorithm Run: W 120 Z 90 Y 60 F60 E50 D40 X 30 C30 B20 A10

  33. Algorithm Run: Z 90 W 120 E50 D40 Y 60 F60 X 30 C30 B20 A10

  34. Algorithm Run: V 210 0 1 Z 90 W 120 1 0 1 0 E50 D40 Y 60 F60 1 0 X 30 C30 0 1 B20 A10

  35. The Huffman encoding: A: 1000 B: 1001 C: 101 D: 00 E: 01 F: 11 V 210 0 1 Z 90 W 120 1 0 1 0 E50 D40 Y 60 F60 1 0 X 30 C30 0 1 B20 A10 File Size: 10x4 + 20x4 + 30x3 + 40x2 + 50x2 + 60x2 = 40 + 80 + 90 + 80 + 100 + 120 = 510 bits

  36. Note the savings: The Huffman code: Required 510 bits for the file. Fixed length code: Need 3 bits for 6 characters. File has 210 characters. Total:630 bits for the file.

  37. Note also: For uniform character distribution: The Huffman encoding will be equal to the fixed length encoding. Why? Assignment.

  38. Formally, the algorithm: Initialize trees of a single node each. Keep the roots of all subtrees in a priority queue. Iterate until only one tree left: Merge the two smallest frequency subtrees into a single subtree with two children, and insert into priority queue.

  39. Algorithm time: Each priority queue operation (e.g. heap): O(log n) In each iteration:one less subtree. Initially: n subtrees. Total: O(n log n) time.

  40. Algorithm correctness: Need to prove two things for greedy algorithms: Greedy Choice Property: The choice of local optimum is indeed part of a global optimum. Optimal Substructure Property: When we recurse on the remaining and combine it with the local optimum of the greedy choice, we get a global optimum.

  41. Centerstring Agorithm correctness: Greedy Choice Property: The choice of majority at a column turns out not be necessarily a global optimum. Optimal Substructure Property: A global optimum means that the overall max distance including the first greedy choice is smallest.

  42. Example: 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ----------------------------------------- 1 For the optimum the second index needs to be 0, but if we ignore the first index, a global optimum may be 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 42

  43. Huffman Algorithm correctness: Need to prove two things: Greedy Choice Property: There exists a minimum cost prefix tree where the two smallest frequency characters are indeed siblings with the longest path from root. This means that the greedy choice does not hurt finding the optimum.

  44. Algorithm correctness: Optimal Substructure Property: An optimal solution to the problem once we choose the two least frequent elements and combine them to produce a smaller problem, is indeed a solution to the problem when the two elements are added.

  45. Algorithm correctness: There exists a minimum cost tree where the minimum frequency elements are longest path siblings: Assume that is not the situation. Then there are two elements in the longest path. Say a,b are the elements with smallest frequency and x,y the elements in the longest path.

  46. Algorithm correctness: CT We know about depth and frequency: da ≤ dy fa ≤ fy da dy a x y

  47. Algorithm correctness: We also know about code tree CT: ∑fσdσ σ is smallest possible. CT da dy a x y Now exchange a and y.

  48. Algorithm correctness: Cost(CT) = ∑fσdσ= σ ∑fσdσ+fada+fydy≥ σ≠a,y CT’ da dy (da ≤ dy, fa≤ fy Therefore fada ≥fydaand fydy ≥fady ) y ∑fσdσ+fyda+fady= σ≠a,y cost(CT’) x a

  49. Algorithm correctness: CT Now do the same thing for b and x db dx b x a

  50. Algorithm correctness: CT” And get an optimal code tree where a and b are sibling with the longest paths db dx x b a

More Related