1 / 4

Greedy strategy At each iteration, it adds a part of the solution.

Greedy strategy At each iteration, it adds a part of the solution. Which part of the solution to add next is determined by a greedy rule . A greedy algorithm may or may not be optimal . The Coin Changing Problem. Given: a large collection of change and a value x .

duscha
Download Presentation

Greedy strategy At each iteration, it adds a part of the solution.

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 strategy • At each iteration, it adds a part of the solution. • Which part of the solution to add next is determined by a greedy rule. • A greedy algorithm may or may not be optimal

  2. The Coin Changing Problem Given: a large collection of change and a value x. Goal: make change for x using the fewest number of coins Greedy strategy: • choose the most valuable coin, then • subtract its value from X and continue.

  3. e.g.) The Coin Changing Problem • You are given the collection of change (25) (10) (5) (1), and a value x=92 25*3 + 10*1 + 5*1 + 1*2 Using only 7 coins • If you are given the collection of change (12)(5)(1) instead of (25) (10) (5) (1), and a value x=15 12*1+1*3 using 4 coins but there is a better solution 15=5*3 using only 3 coins • The greedy algorithm doesn’t always give the best solution.

  4. Greedy Solution Coin-Changing (d[1]>d[2]>…d[n], x) { i = 1 while (x>0){ C[i]=x/d[i] x=x-c*d[i] i++ } return c[1]+c[2]+…+c[n] } Complexity: O(n)

More Related