general remarks
play

General remarks Algorithms Algorithms Oliver Oliver Week 8 - PowerPoint PPT Presentation

CS 270 CS 270 General remarks Algorithms Algorithms Oliver Oliver Week 8 Kullmann Kullmann Greedy Greedy algorithms algorithms Making Making Greedy Algorithms change change Minimum Minimum We learn about greedy algorithms .


  1. CS 270 CS 270 General remarks Algorithms Algorithms Oliver Oliver Week 8 Kullmann Kullmann Greedy Greedy algorithms algorithms Making Making Greedy Algorithms change change Minimum Minimum We learn about greedy algorithms . spanning spanning trees trees Greedy algorithms Huffman Huffman 1 Reading from CLRS for week 8 Codes Codes 1 Chapter 16.1-16.3 Making change 2 2 Chapter 23 Minimum spanning trees 3 Huffman Codes 4 CS 270 CS 270 Greedy Algorithms The problem Algorithms Algorithms Oliver Oliver Kullmann Kullmann Greedy Greedy From [Anany Levitin, Introduction to the Design and Analysis of algorithms algorithms This week and next week we will discuss two general methods Algorithms], Chapter 9, “Greedy Technique”, with some Making Making change change which may be used to solve optimisation problems. comments in square brackets: Minimum Minimum spanning spanning The topic this week will be Greedy Algorithms. trees trees Let us start with the change-making problem faced Huffman Huffman by millions of cashiers all over the world (at least Next week we will discuss Dynamic Programming. Codes Codes subconsciously): give change for a specific amount n Idea of Greedy Algorithms with the least number of coins of the denominations d 1 > d 2 > · · · > d m used in that locale. For example, When we have a choice to make, make the one that looks best the widely [?] used coin denomination in the United right now . Make a locally optimal choice in hope of getting a States [of America] are d 1 = 25 (quarter), d 2 = 10 globally optimal solution . (dime), d 3 = 5 (nickel), and d 4 = 1 (penny).

  2. CS 270 CS 270 The problem (cont.) Reflection Algorithms Algorithms Oliver Oliver How would you give change with the coins of these Kullmann Kullmann denominations of, say, 48 cents? If you came up with Greedy Greedy the answer 1 quarter, 2 dimes, and 3 pennies algorithms algorithms Is this solution to the instance of the change-making [ 48 = 25 + 2 · 10 + 3 ], you followed [likely] — Making Making problem optimal? Yes, it is. In fact, it is possible to change change consciously or not — a logical [better “reasonable”] prove that the greedy algorithm yields an optimal Minimum Minimum strategy of making a sequence of best choices among spanning spanning solution for every positive integer amount with these trees trees the currently available alternatives. Indeed, in the first [!] coin denominations. At the same time, it is easy to Huffman Huffman step, you could have given one coin of any of the four Codes Codes give an example of “weird” coin denominations — denominations. “Greedy” thinking leads to giving one e.g., d 1 = 7 , d 2 = 5 , d 3 = 1 — which may not yield an quarter because it reduces the remaining about the optimal solution for some amounts. [The smallest most, namely, to 23 cents. In the second step, you had amount for which the greedy strategy is not working is the same coins at your disposal, but you could not give n = 10 . The general problem is called the subset-sum a quarter because it would have violated the problem’s problem , and is NP-complete.] constraints. So your best selection in this step was one dime, reducing the remaining amount to 13 cents. Giving one more dime left you with 3 cents to be given with three pennies. CS 270 CS 270 Reflection (cont.) Reflection (cont.) Algorithms Algorithms Oliver Oliver Kullmann Kullmann The approach applied in the opening paragraph to the Greedy Greedy change-making problem is called greedy . [...] The algorithms algorithms greedy approach suggests constructing a solution Making Making through a sequence of steps, each expanding a partially change change Minimum Minimum constructed solution obtained so far, until a complete These requirements explain the technique’s name: on spanning spanning trees trees solution to the problem is reached. On each step — each step, it suggests a “greedy” grab of the the best Huffman Huffman and this is the central point of this technique — the alternative available in the hope that a sequence of Codes Codes locally optimal choices will yield a (globally) optimal choice made must be solution to the entire problem. feasible, i.e., it has to satisfy the problem’s constraints. locally optimal, i.e., it has to be the [or one] best local choice among all feasible choices available on that step. irrevocable, i.e., once made, it cannot be changed on subsequent steps of the algorithm.

  3. CS 270 CS 270 Making change (again) Applicability of greedy algorithms Algorithms Algorithms Oliver Oliver Greedy algorithms don’t always work. Kullmann Kullmann For example, in Making Change, if the denominations were Greedy Greedy algorithms algorithms (25,11,5,1), then Input: A list of integers representing coin denominations, plus Making Making change change another positive integer representing an amount of money. 15 = 11 + 1 + 1 + 1 + 1 = 5 + 5 + 5 . Minimum Minimum spanning spanning Output: A minimal collection of coins of the given trees trees denominations which sum to the given amount. However, quite often they do, or they come close enough to Huffman Huffman Codes Codes the optimal solution to make the outcome acceptable. Greedy Strategy: Repeatedly include in the solution the largest coin whose value doesn’t exceed the remaining amount. This, and the fact that they are quite easy to implement, make them an attractive alternative for many hard E.g.: If the denominations are (25,10,5,1) and the amount is problems. 87, then 87 = 25 + 25 + 25 + 10 + 1 + 1 . Well-known instances of the use of Greedy Algorithms are the following problems: Minimum Spanning Trees (Kruskal’s Algorithm) Data Compression (Huffman Codes.) . CS 270 CS 270 Remark on coin denominations Minimum Spanning Trees (MST) Algorithms Algorithms Oliver Oliver As far as I am aware, for all coin denominations in place today Kullmann Kullmann Input: An undirected connected graph G = ( V , E ), and a the greedy strategy works. (positive) weight w ( e ) ∈ R ≥ 0 on each edge e ∈ E . Greedy Greedy algorithms algorithms Output: A subset F ⊆ E of edges which connects all of the The only coin denomination in history I am aware of for Making Making change change vertices V of G , has no cycles, and minimises the total edge which the greedy strategy doesn’t work was the British Minimum Minimum weight: system before decimalisation (1971). spanning spanning trees trees w ( F ) = � e ∈ F w ( e ) . That system had Huffman Huffman Codes Codes half crown – 30 pence Example: two shillings – 24 pence shilling – 12 pence 20 b 15 sixpence – 6 pence c 12 threepence – 3 pence a 7 6 penny – 1 pence 11 d halfpenny – 1/2 pence. 3 e 1 7 4 Do you see an example where it doesn’t work? 10 g f 48 = 30 + 12 + 6 = 2 · 24 .

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend