Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Finding all subsets which have nearly the desired sum

by halley (Prior)
on May 03, 2005 at 16:03 UTC ( [id://453659]=note: print w/replies, xml ) Need Help??


in reply to Finding the simplest combination of descrete thicknesses that sums nearest to the target thickness.

Someone else came to me with a 1D variant of this problem as a homework example. I wrote this recursive version in a few minutes. This finds all possible combinations for 1D; you could add a scoring mechanism to find the optimum choice. I'm sure it can be improved, but it's speedy enough and simple enough for homework.
#!/usr/bin/perl # First line of DATA is desired total and the tolerance. my $Finish = <DATA>; my ($Wanted, $Tolerance) = ($Finish =~ /(-?\d+)\s+(\d+)/); # Other lines of DATA are the available elements. my @Items = (<DATA>); chomp @Items; #--------------------------------------------------------------------- +------- my $Stack = (); sub build { my $index = shift; my $total = shift; while ($index < @Items) { my $num = $Items[$index]; push(@Stack, $num); if ($num + $total > ($Wanted+$Tolerance)) { next; } elsif ($num + $total >= ($Wanted-$Tolerance) and $num + $total <= ($Wanted+$Tolerance)) { print "( @Stack ) = ", $num+$total, "\n"; } else { build($index+1, $num + $total); } } continue { pop(@Stack); $index++; } } #--------------------------------------------------------------------- +------- build(0, 0); __DATA__ 30 5 -5 5 10 15 20 10 25 25

--
[ e d @ h a l l e y . c c ]

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://453659]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2024-04-23 13:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found