170 likes | 449 Views
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.
E N D
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...
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
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
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
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
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
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
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
Constructing a Huffman Tree f:5 e:9 c:12 b:13 d:16 a:45
Constructing a Huffman Tree 14 c:12 b:13 d:16 a:45 0 1 f:5 e:9
Constructing a Huffman Tree 14 25 d:16 a:45 0 1 0 1 f:5 e:9 c:12 b: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
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
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
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