Chapter 16: Greedy Algorithm

Slides:



Advertisements
Similar presentations
Introduction to Algorithms
Advertisements

CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Algorithm Design Techniques: Greedy Algorithms. Introduction Algorithm Design Techniques –Design of algorithms –Algorithms commonly used to solve problems.
CSCE 411H Design and Analysis of Algorithms Set 8: Greedy Algorithms Prof. Evdokia Nikolova* Spring 2013 CSCE 411H, Spring 2013: Set 8 1 * Slides adapted.
Greedy Algorithms Amihood Amir Bar-Ilan University.
Greedy Algorithms Greed is good. (Some of the time)
1 Huffman Codes. 2 Introduction Huffman codes are a very effective technique for compressing data; savings of 20% to 90% are typical, depending on the.
3 -1 Chapter 3 The Greedy Method 3 -2 The greedy method Suppose that a problem can be solved by a sequence of decisions. The greedy method has that each.
CPSC 411, Fall 2008: Set 4 1 CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Fall 2008.
Greedy Algorithms CIS 606 Spring Greedy Algorithms Similar to dynamic programming. Used for optimization problems. Idea – When we have a choice.
Data Structures – LECTURE 10 Huffman coding
Greedy Algorithms Huffman Coding
CPSC 411, Fall 2008: Set 4 1 CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Fall 2008.
16.Greedy algorithms Hsu, Lih-Hsing. Computer Theory Lab. Chapter 16P An activity-selection problem Suppose we have a set S = {a 1, a 2,..., a.
© The McGraw-Hill Companies, Inc., Chapter 3 The Greedy Method.
Advanced Algorithm Design and Analysis (Lecture 5) SW5 fall 2004 Simonas Šaltenis E1-215b
Introduction to Algorithms Chapter 16: Greedy Algorithms.
1 Algorithms CSCI 235, Fall 2015 Lecture 30 More Greedy Algorithms.
Huffman Codes. Overview  Huffman codes: compressing data (savings of 20% to 90%)  Huffman’s greedy algorithm uses a table of the frequencies of occurrence.
Greedy Algorithms Chapter 16 Highlights
1 Chapter 16: Greedy Algorithm. 2 About this lecture Introduce Greedy Algorithm Look at some problems solvable by Greedy Algorithm.
Greedy Algorithms Analysis of Algorithms.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 18.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 17.
Greedy Algorithms. p2. Activity-selection problem: Problem : Want to schedule as many compatible activities as possible., n activities. Activity i, start.
CS6045: Advanced Algorithms Greedy Algorithms. Main Concept –Divide the problem into multiple steps (sub-problems) –For each step take the best choice.
Greedy Algorithms Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Greedy Algorithms General principle of greedy algorithm
Greedy algorithms: CSC317
HUFFMAN CODES.
CSC317 Greedy algorithms; Two main properties:
CSCE 411 Design and Analysis of Algorithms
Lecture on Design and Analysis of Computer Algorithm
Greedy Method 6/22/2018 6:57 PM Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015.
Merge Sort 7/29/ :21 PM The Greedy Method The Greedy Method.
The Greedy Method and Text Compression
Greedy Algorithms (Chap. 16)
The Greedy Method and Text Compression
Introduction to Algorithms`
Greedy Algorithms / Interval Scheduling Yin Tat Lee
Chapter 9: Huffman Codes
Chapter 16: Greedy Algorithms
Algorithm Design Methods
Binhai Zhu Computer Science Department, Montana State University
Merge Sort 11/28/2018 2:18 AM The Greedy Method The Greedy Method.
Merge Sort 11/28/2018 2:21 AM The Greedy Method The Greedy Method.
CS6045: Advanced Algorithms
Merge Sort 11/28/2018 8:16 AM The Greedy Method The Greedy Method.
Algorithms (2IL15) – Lecture 2
Advanced Algorithms Analysis and Design
Greedy Algorithms Many optimization problems can be solved more quickly using a greedy approach The basic principle is that local optimal decisions may.
Chapter 16: Greedy algorithms Ming-Te Chi
Advanced Algorithms Analysis and Design
Merge Sort Dynamic Programming
Merge Sort 1/17/2019 3:11 AM The Greedy Method The Greedy Method.
Greedy Algorithms TOPICS Greedy Strategy Activity Selection
Greedy: Huffman Codes Yin Tat Lee
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Data Structure and Algorithms
Greedy Algorithms Alexandra Stefan.
Chapter 16: Greedy algorithms Ming-Te Chi
Greedy Algorithms Comp 122, Spring 2004.
Algorithm Design Techniques Greedy Approach vs Dynamic Programming
Merge Sort 5/2/2019 7:53 PM The Greedy Method The Greedy Method.
Lecture 2: Greedy Algorithms
Algorithms CSCI 235, Spring 2019 Lecture 30 More Greedy Algorithms
Huffman Coding Greedy Algorithm
Huffman codes Binary character code: each character is represented by a unique binary string. A data file can be coded in two ways: a b c d e f frequency(%)
Algorithms CSCI 235, Spring 2019 Lecture 31 Huffman Codes
Analysis of Algorithms CS 477/677
Presentation transcript:

Chapter 16: Greedy Algorithm

About this lecture Introduce Greedy Algorithm Look at some problems solvable by Greedy Algorithm

Suppose that in a certain country, the coin dominations consist of: Coin Changing Suppose that in a certain country, the coin dominations consist of: $1, $2, $5, $10 You want to design an algorithm such that you can make change of any x dollars using the fewest number of coins

Coin Changing An idea is as follows: 1. Create an empty bag 2. while (x  0) { Find the largest coin c at most x; Put c in the bag; Set x = x – c ; } 3. Return coins in the bag

Coin Changing It is easy to check that the algorithm always return coins whose sum is x At each step, the algorithm makes a greedy choice (by including the largest coin) which looks best to come up with an optimal solution (a change with fewest #coins) This is an example of Greedy Algorithm

Coin Changing Is Greedy Algorithm always working? No! Consider a new set of coin denominations: $1, $4, $5, $10 Suppose we want a change of $8 Greedy algorithm: 4 coins (5,1,1,1) Optimal solution: 2 coins (4,4)

Greedy Algorithm We will look at some non-trivial examples where greedy algorithm works correctly Usually, to show a greedy algorithm works: We show that some optimal solution includes the greedy choice  selecting greedy choice is correct We show optimal substructure property  solve the subproblem recursively

Activity Selection Suppose you are a freshman in a school, and there are many welcoming activities There are n activities A1, A2, …, An For each activity Ak , it has a start time sk , and a finish time fk Target: Join as many as possible!

Activity Selection To join the activity Ak, you must join at sk ; you must also stay until fk Since we want as many activities as possible, should we choose the one with (1) Shortest duration time? (2) Earliest start time? (3) Earliest finish time?

Activity Selection Shortest duration time may not be good: A2 : [3:00, 5:00), A3 : [5:05, 7:00), Though not optimal, #activities in this solution R (shortest duration first) is at least half #activities in an optimal solution O: One activity in R clashes with at most 2 in O If |O|  2|R|, R should have one more activity

Activity Selection Earliest start time may even be worse: A2 : [3:10, 3:20), A3 : [3:20, 3:30), A4 : [3:30, 3:40), A5 : [3:40, 3:50) … In the worst-case, the solution contains 1 activity, while optimal has n-1 activities

Greedy Choice Property To our surprise, earliest finish time works! We actually have the following lemma: Lemma: For the activity selection problem, some optimal solution includes an activity with earliest finish time How to prove?

Proof: (By “Cut-and-Paste” argument) Let OPT = an optimal solution Let Aj = activity with earliest finish time If OPT contains Aj, done! Else, let A’ = earliest activity in OPT Since Aj finishes no later than A’, we can replace A’ by Aj in OPT without conflicting other activities in OPT an optimal solution containing Aj (since it has same #activities as OPT)

Optimal Substructure L e t A j = a c t i v i t y w i t h e a r l i e s t f i n i s h t i m e L e t S = t h e s u b s e t o f o r i g i n a l a c t i v i t i e s t h a t d o n o t c o n f l i c t w i t h A j L e t O P T = o p t i m a l s o l u t i o n c o n t a i n i n g A j L e m m a : O P T – { A j } m u s t b e a n o p t i m a l s o l u t i o n f o r t h e s u b p r o b l e m w i t h i n p u t a c t i v i t i e s S

Proof: (By contradiction) First, OPT – { Aj } can contain only activities in S If it is not an optimal solution for input activities in S, let C be some optimal solution for input S  C has more activities than OPT – { Aj }  C ∪{Aj} has more activities than OPT  Contradiction occurs

If finish times are sorted in input, running time = O(n) Greedy Algorithm The previous two lemmas implies the following correct greedy algorithm: S = input set of activities ; while (S is not empty) { A = activity in S with earliest finish time; Select A and update S by removing activities having conflicts with A; } If finish times are sorted in input, running time = O(n)

Designing a greedy algorithm 1. Cast the optimization problem as one in which we make a choice and are left with one subproblem to solve. 2. Prove that there is always an optimal solution to the original problem that makes the greedy choice 3. Demonstrate optimal substructure by showing that, having made the greedy choice, if we combine an optimal solution to the subproblem with the greedy choice, we arrive at an optimal solution to the original problem.

Designing a greedy algorithm Greedy-choice property: A global optimal solution can be achieved by making a local optimal (optimal) choice. Optimal substructure: An optimal solution to the problem contains its optimal solution to subproblem.

0-1 Knapsack Problem Suppose you are a thief, and you are now in a jewelry shop (nobody is around !) You have a big knapsack that you have “borrowed” from some shop before Weight limit of knapsack: W There are n items, I1, I2, …, In Ik has value vk, weight wk Target: Get items with total value as large as possible without exceeding weight limit

0-1 Knapsack Problem We may think of some strategies like: (1) Take the most valuable item first (2) Take the densest item (with vk/wk is maximized) first Unfortunately, someone shows that this problem is very hard (NP-complete), so that it is unlikely to have a good strategy Let’s change the problem a bit…

Fractional Knapsack Problem In the previous problem, for each item, we either take it all, or leave it there Cannot take a fraction of an item Suppose we can allow taking fractions of the items; precisely, for a fraction c c part of Ik has value cvk, weight cwk Target: Get as valuable a load as possible, without exceeding weight limit

Fractional Knapsack Problem Suddenly, the following strategy works: Take as much of the densest item (with vk/wk is maximized) as possible The correctness of the above greedy- choice property can be shown by cut- and-paste argument Also, it is easy to see that this problem has optimal substructure property  implies a correct greedy algorithm

Fractional Knapsack Problem However, the previous greedy algorithm (pick densest) does not work for 0-1 knapsack To see why, consider W = 50 and: I1 : v1 = $60, w1 = 10 (density: 6) I2 : v2 = $100, w2 = 20 (density: 5) I3 : v3 = $120, w3 = 30 (density: 4) Greedy algorithm: $160 (I1, I2) Optimal solution: $220 (I2, I3)

Brainstorm Suppose there are n items. Let Solve this problem in (nW). S= {item1, item2, …, itemn} wi = weight of itemi pi = profit of itemi W = maximum weight the knapsack can hold, where wi, pi, W are positive integers. Determine a subset A of S such that is maximized subject to Solve this problem in (nW). Hint: Use Dynamic Programming Strategy

Encoding Characters In ASCII, each character is encoded using the same number of bits (8 bits) called fixed-length encoding However, in real-life English texts, not every character has the same frequency One way to encode the texts is: Encode frequent chars with few bits Encode infrequent chars with more bits  called variable-length encoding

Encoding Characters V a r i a b l e - l e n g t h e n c o d i n g m a y g a i n a l o t i n s t o r a g e r e q u i r e m e n t Example: Suppose we have a 100K-char file consisted of only chars a, b, c, d, e, f Suppose we know a occurs 45K times, and other chars each 11K times  Fixed-length encoding: 300K bits

Encoding Characters Example (cont): Suppose we encode the chars as follows: a  0, b  100, c  101, d  110, e  1110, f  1111 Storage with the above encoding: (45x1 + 33x3 + 22x4) x 1K = 232K bits (reduced by 25% !!)

Encoding Characters Thinking a step ahead, you may consider an even “better” encoding scheme: a  0, b  1, c  00, d  01, e  10, f  11 This encoding requires less storage since each char is encoded in fewer bits … What’s wrong with this encoding?

Prefix Code Suppose the encoded texts is: 0101 We cannot tell if the original text is abab, dd, abd, aeb, or … The problem comes from: one codeword is a prefix of another one

Prefix Code To avoid the problem, we generally want each codeword not a prefix of another called prefix code, or prefix-free code Let T = text encoded by prefix code We can easily decode T back to original: Scan T from the beginning Once we see a codeword, output the corresponding char Then, recursively decode remaining

Prefix Code Tree N a t u r a l l y , a p r e f i x c o d e s c h e m e c o r r e s p o n d s t o a p r e f i x c o d e t r e e Each char  a leaf Root-to-leaf path  codeword E . g . , a  0 , b  1 0 0 , c  1 0 1 , d  1 1 0 , e  1 1 1 0 , f  1 1 1 1 a 1 b c d e f 1

Optimal Prefix Code Question: Given frequencies of each char, how to find the optimal prefix code scheme (or optimal prefix code tree)? Precisely: Input: S = a set n chars, c1, c2, …, cn with ck occurs fck times Target: Find codeword wk for each ck such that k |wk| fck is minimized

Huffman Code I n 1 9 5 2 , D a v i d H u f f m a n ( t h e n a n M I T P h D s t u d e n t ) t h i n k s o f a g r e e d y a l g o r i t h m t o o b t a i n t h e o p t i m a l p r e f i x c o d e t r e e L e t c a n d c ’ b e c h a r s w i t h l e a s t f r e q u e n c i e s . H e o b s e r v e d t h a t : Lemma: There is some optimal prefix code tree with c and c’ sharing the same parent, and the two leaves are farthest from root

Proof: (By “Cut-and-Paste” argument) Let OPT = some optimal solution If c and c’ as required, done! Else, let a and b be two bottom-most leaves sharing same parent (such leaves must exist… why??) swap a with c, swap b with c’ an optimal solution as required (since it at most the same k |wk| fk as OPT … why??)

Graphically: 1 a b c If this is optimal Bottom-most leaves 1 c b a 1 a b c If this is optimal Bottom-most leaves 1 c b a then this is optimal

Optimal Substructure Let OPT be an optimal prefix code tree with c and c’ as required Let T’ be a tree formed by merging c, c’, and their parent into one node Consider S’ = set formed by removing c and c’ from S, but adding X with fX = fc + fc’ Lemma: If T’ is an optimal prefix code tree for S’, then T is an optimal prefix code tree for S.

X Graphically, the lemma says: If this is optimal for S then this is optimal for S’ 1 1 Tree T’ for S’ Tree T for S a a X Merged node Merging c, c’ and the parent c c’ Here, fX = fc + fc’

Huffman Code Questions: Based on the previous lemmas, can you obtain Huffman’s coding scheme? (Try to think about yourself before looking at next page…) What is the running time? O(n log n) time, using heap (how??)

Huffman(S) { // build Huffman code tree 1. Find least frequent chars c and c’ 2. S’ = remove c and c’ from S, but add char X with fX = fc + fc’ 3. T’ = Huffman(S’) 4. Make leaf X of T’ an internal node by connecting two leaves c and c’ to it 5. Return resulting tree }

Constructing a Huffman code Q  C /* initialize the min-priority queue with the character in C */ 3 for i  1 to n – 1 4 do allocate a new node z 5 left[z]  x  EXTRACT-MIN(Q) 6 right[z]  y  EXTRACT-MIN(Q) 7 f[z]  f[x] + f[y] 8 INSERT(Q,Z) 9 return EXTRACT-MIN(Q) Complexity: O(n log n)

The steps of Huffman’s algorithm

The steps of Huffman’s algorithm

Homework Practice at home: 16.1-2, 16.1-5, 16.2- 3, 16.2-4, 16.2-7, 16.3-6, 16.3-8 Problem 16-1 (a) (Due Nov. 25)