Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
What about implementing David Pearson's algorithm instead? It will find the smallest counterexample in polynomial time O(N^3) when N is the number of coins.

Determining whether or not a given coin system can be done by the greedy algorithm is not NP-complete. Finding optimum change in a given coin system that does not allow the greedy algorithm is NP-complete.

It looks quite easy to implement. I will try it when I get a chance.

Update: Code added below. Update2: Rewrote greedy_change with a simple loop instead of the regular expression one I borrowed from AnonyMonk.

use strict; use warnings; sub greedy_change { my $change = shift; my @coins = @_; my @result; for my $i (0..$#coins) { my $val = int($change/$coins[$i]); $change -= $val*$coins[$i]; push @result,$val; } return @result; } + + + sub pearson_counterexample { my $i = shift; my $j = shift; my @coins = @_; + # Let position $i be the highest coin used, # Let positon $j be the lowest. Then the # potential counterexample will be: # 1) Compute the greedy change for the # next highest coins value - 1. # # 2) Copy all coin counts from 0 to $j-1 # # 3) Add one to the coin count at position $j. # # If this combination is more efficient than # the greedy value, we have a counterexample. + my $value = $coins[$i-1] - 1; my @greedy = greedy_change($value, @coins); my @example = (0) x @coins; $example[$_] = $greedy[$_] for (0..$j-1); $example[$j] = $greedy[$j]+1; + # Compute the numeric value and see if it # represents the amount more efficiently # than the greedy way. my $example_value = 0; $example_value += $coins[$_]*$example[$_] for (0..$#example); @greedy = greedy_change($example_value, @coins); my ($example_count, $greedy_count); $example_count += $example[$_] for (0..$#example); $greedy_count += $greedy[$_] for (0..$#greedy); + # This candidate case did not work. return if $example_count >= $greedy_count; return @example; } + # Read the coin system from the command line, in # decreasing order of size. my @coins = @ARGV; if (@coins < 3) { print "Must have a least three coins\n"; exit(1); } my ($i,$j); my $found = 0; + EXAMPLES: for ($i = 1; $i < @coins; ++$i) { for ($j = $i; $j < @coins; ++$j) { my @example = pearson_counterexample($i, $j, @coins); if (@example) { print "Counterexample: ",join(" ",@example),"\n"; $found = 1; last EXAMPLES; } } } + print "No counterexamples found. Good coin system\n" unless $found;

In reply to Re: The greedy change-making problem using regexes by tall_man
in thread The greedy change-making problem using regexes by blokhead

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-25 10:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found