1 / 42

Greedy Algorithms

Greedy Algorithms. Greedy Algorithm. Greedy Algorithm - Makes locally optimal choice at each stage. - For optimization problems. If the local optimum is a part of the global optimum, we get the global optimum. Greedy Algorithm vs Dynamic Programming. Dynamic Programming. Greedy

rowena
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 Algorithm • Greedy Algorithm - Makes locally optimal choice at each stage. - For optimization problems. • If the local optimum is a part of the global optimum, we get the global optimum.

  3. Greedy Algorithm vsDynamic Programming Dynamic Programming Greedy Algorithm

  4. Knapsack Problem a thief’s knapsack of size W n items

  5. Knapsack Problem • 0-1 knapsack problem - Each item must be either taken or left behind. • Fractional knapsack problem - The thief can take fractions of items.

  6. Knapsack Problem 50 n = 4, W = 50 45 30 20 10 $135 $100 $120 $60 knapsack ($6/unit) ($5/unit) ($4/unit) ($3/unit)

  7. Fractional Knapsack Problem $240 Greedy algorithm: greatest value per unit 50 20 30 45 30 20 50 20 10 $120 10 $135 $100 ($4/unit) $60 ($3/unit) ($5/unit) knapsack ($6/unit)

  8. 0-1 Knapsack Problem $220 $135 50 50 50 10 30 45 30 45 $60 $160 ($6/unit) 20 50 20 20 $120 ($4/unit) 10 $100 $135 value per unit value optimal ($5/unit) ($3/unit)

  9. 0-1 Knapsack Problem $220 $135 50 50 50 Difficult to get the optimal solution with a greedy strategy. 30 45 $160 20 50 Dynamic Programming : 20 10 value per unit value optimal

  10. Optimal Substructure vsSubproblem Solution T1,5 T1,6 Ti,j : the solution of a subproblem 8 4 3 4 A subproblem solution? A local optimum? 1 2 5 7 T2,5 T2,6

  11. Greedy Algorithm vsDynamic Programming

  12. Optimal Substructure vsSubproblem Solution • Subproblem solution - From all subproblem solutions • Optimal substructure - With only constant number of parameters - Without subproblems or future choices Usually Bottom-up Usually top-down

  13. Huffman Codes • A lossless data compression algorithm. • It uses variable-length code

  14. Variable-Length Code Six characters : a, b, c, d, e, f How can we represent them with binary strings?

  15. Variable-Length Code Six characters : a, b, c, d, e, f How can we represent them with binary strings? 0 0 1 0 = a a b a What 0010 menas? 0 01 0 = a d a 00 10 = c e …

  16. Prefix Code Six characters : a, b, c, d, e, f How can we represent them with binary strings? No codeword is a prefix of another codeword.

  17. Prefix Code Six characters : a, b, c, d, e, f How can we represent them with binary strings? No codeword is a prefix of another codeword.

  18. Variable-Length Code Is it the optimal way?

  19. Variable-Length Code

  20. Huffman Tree Fixed-length code 100 0 1 86 14 0 1 0 58 28 14 0 1 0 1 0 1 a: 45 c: 12 e: 9 b: 13 d: 16 f: 5

  21. Huffman Tree Variable-length code 100 0 1 55 a: 45 0 1 25 30 0 1 0 1 d: 16 14 c: 12 b: 13 1 0 f: 5 e: 9

  22. Huffman’s Algorithm Observation 1 The longest code : at least 2 characters 0 0 A Every non-leaf node has two children. B 0 B

  23. Huffman’s Algorithm Observation 1 The longest code : at least 2 characters Observation 2 The longest 2 codes : the least frequent two characters A F 1 0 1 0 B G C H 1 1 0 0 D E I J

  24. Huffman’s Algorithm Observation 1 The longest code : at least 2 characters Observation 2 The longest 2 codes : the least frequent two characters Observation 3 A non-leaf node : handled like a leaf node T 30 T ’ x: 30 14 d: 16

  25. Huffman’s Algorithm Observation 3 A non-leaf node : handled like a leaf node T 30 T ’ x: 30 1 0 14 d: 16 Total length in T = Total length in T ‘ + 16 * 1 + (9 + 5) * 2 1 0 e: 9 f: 5

  26. Huffman’s Algorithm Observation 1 The longest code : at least 2 characters Observation 2 The longest 2 codes : the least frequent two characters Observation 3 A non-leaf node : handled like a leaf node T 30 T ’ x: 30 14 d: 16

  27. Huffman’s Algorithm Merging two least frequent nodes. a: 45 b: 13 c: 12 d: 16 e: 9 f: 5

  28. Huffman’s Algorithm Merging two least frequent nodes. 14 a: 45 b: 13 c: 12 d: 16 1 0 e: 9 f: 5

  29. Huffman’s Algorithm Merging two least frequent nodes. 25 14 a: 45 d: 16 1 0 1 0 c: 12 b: 13 e: 9 f: 5

  30. Huffman’s Algorithm Merging two least frequent nodes. 25 30 a: 45 1 0 1 0 14 c: 12 b: 13 d: 16 1 0 e: 9 f: 5

  31. Huffman’s Algorithm Merging two least frequent nodes. 55 a: 45 0 1 25 30 1 0 1 0 14 c: 12 b: 13 d: 16 1 0 e: 9 f: 5

  32. Huffman’s Algorithm Merging two least frequent nodes. 100 0 1 55 a: 45 0 1 25 30 1 0 1 0 14 c: 12 b: 13 d: 16 1 0 e: 9 f: 5

  33. Greedy Algorithm • Optimization Algorithms : finds a proper local optimum. • Not as powerful as Dynamic Programming, but simpler. • Greedy algorithms - Knapsack Problem - Huffman Code

  34. Graphs

  35. What is a graph? G = ( V, E ) 1 2 V = { 1, 2, 3, 4, 5 } E = { {1,2}, {1,3}, {2,3}, {2,4}, {2,5}, {3,4} } 5 3 4

  36. Directed and Undirected Directed graph Undirected graph 1 2 1 2 5 5 3 4 3 4

  37. Representations of Graphs Directed graph Adjacency-list 1 2 1 2 / 3 4 / 2 5 1 3 / 3 3 4 2 3 / 4 2 / 5

  38. Representations of Graphs Directed graph Adjacency-Matrix 1 2 3 4 5 1 2 1 2 5 3 3 4 4 5

  39. Representations of Graphs Adjacency-list Adjacency-Matrix

  40. Adjacency Matrix of Graphs Directed graph Adjacency-Matrix 1 2 3 4 5 1 2 1 2 5 3 3 4 4 5

  41. Adjacency Matrix of Graphs A = AT =

  42. Weighted Graphs 1.7 1 2 -0.2 2.0 5 0.4 -2.1 3.6 3 4 -0.3 3.1

More Related