-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubsetsumsolver.plx
148 lines (128 loc) · 4.55 KB
/
subsetsumsolver.plx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/perl
# Copyright (C) 2013, Bradley M. Kuhn
#
# This program gives you software freedom; you can copy, modify, convey,
# and/or redistribute it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program in a file called 'GPLv3'. If not, write to the:
# Free Software Foundation, Inc., 51 Franklin St, Fifth Floor
# Boston, MA 02110-1301, USA.
use strict;
use warnings;
my $ZERO= 0;
sub SubSetSumSolver ($$$) {
my($numberList, $totalSought, $extractNumber) = @_;
my($P, $N) = (0, 0);
foreach my $ii (@{$numberList}) {
if ($ii < $ZERO) {
$N += $ii;
} else {
$P += $ii;
}
}
print "P = $P, N = $N\n";
my $size = scalar(@{$numberList});
my %Q;
my(@L) =
map { { val => &$extractNumber($_), obj => $_ } } @{$numberList};
for (my $ii = 0 ; $ii <= $size ; $ii++ ) {
$Q{$ii}{0}{value} = 1;
$Q{$ii}{0}{list} = [];
}
for (my $jj = $N; $jj <= $P ; $jj++) {
$Q{0}{$jj}{value} = ($L[0]{val} == $jj);
$Q{0}{$jj}{list} = $Q{0}{$jj}{value} ? [ $L[0]{obj} ] : [];
}
for (my $ii = 1; $ii <= $size ; $ii++ ) {
for (my $jj = $N; $jj <= $P ; $jj++) {
if ($Q{$ii-1}{$jj}{value}) {
$Q{$ii}{$jj}{value} = 1;
$Q{$ii}{$jj}{list} = [] unless defined $Q{$ii}{$jj}{list};
push(@{$Q{$ii}{$jj}{list}}, @{$Q{$ii-1}{$jj}{list}});
} elsif ($L[$ii]{val} == $jj) {
$Q{$ii}{$jj}{value} = 1;
$Q{$ii}{$jj}{list} = [] unless defined $Q{$ii}{$jj}{list};
push(@{$Q{$ii}{$jj}{list}}, $jj);
} elsif ($Q{$ii-1}{$jj - $L[$ii]{val}}{value}) {
$Q{$ii}{$jj}{value} = 1;
$Q{$ii}{$jj}{list} = [] unless defined $Q{$ii}{$jj}{list};
push(@{$Q{$ii}{$jj}{list}}, $L[$ii]{obj}, @{$Q{$ii-1}{$jj - $L[$ii]{val}}{list}});
} else {
$Q{$ii}{$jj}{value} = 0;
$Q{$ii}{$jj}{list} = [];
}
}
}
foreach (my $ii = 0; $ii <= $size; $ii++) {
foreach (my $jj = $N; $jj <= $P; $jj++) {
print "Q($ii, $jj) == $Q{$ii}{$jj}{value} with List of ", join(", ", @{$Q{$ii}{$jj}{list}}), "\n";
}
}
if (not $Q{$size}{$totalSought}{value}) {
print "No solution\n";
} else {
print "Solution\n";
print "List: ", join(", ", @{$Q{$size}{$totalSought}{list}}), "\n";
}
}
sub NonNegativeSubSetSumSolver ($$$) {
# First arg is list ref that is the whole list, and second arg is the
# total sought, and third arg is a subref that will extract the number
# from items in the list (so that the list can be of more complex
# objects)
my($numberList, $totalSought, $extractNumber) = @_;
my(@L) =
map { { val => &$extractNumber($_), obj => $_ } } @{$numberList};
my %Q;
my $size = scalar(@{$numberList});
for (my $ii = 0 ; $ii <= $size ; $ii++ ) {
$Q{$ii}{0}{value} = 1;
$Q{$ii}{0}{list} = [];
}
for (my $jj = 1; $jj <= $totalSought ; $jj++) {
$Q{0}{$jj}{value} = 0;
$Q{0}{$jj}{list} = [];
}
for (my $ii = 1; $ii <= $size ; $ii++ ) {
for (my $jj = 1; $jj <= $totalSought ; $jj++) {
if ($Q{$ii-1}{$jj}{value}) {
$Q{$ii}{$jj}{value} = 1;
$Q{$ii}{$jj}{list} = [] unless defined $Q{$ii}{$jj}{list};
push(@{$Q{$ii}{$jj}{list}}, @{$Q{$ii-1}{$jj}{list}});
} elsif ( ($L[$ii-1]{val} <= $jj) and ($Q{$ii-1}{$jj - $L[$ii-1]{val}}{value}) ) {
$Q{$ii}{$jj}{value} = 1;
$Q{$ii}{$jj}{list} = [] unless defined $Q{$ii}{$jj}{list};
push(@{$Q{$ii}{$jj}{list}}, $L[$ii-1]{obj}, @{$Q{$ii-1}{$jj - $L[$ii-1]{val}}{list}});
} else {
$Q{$ii}{$jj}{value} = 0;
$Q{$ii}{$jj}{list} = [];
}
}
}
if (not $Q{$size}{$totalSought}{value}) {
print "No solution\n";
} else {
print "Solution\n";
print "List: ", join(", ", @{$Q{$size}{$totalSought}{list}}), "\n";
}
}
if (@ARGV < 1) {
print STDERR "usage: $0 <SUM_DESIRED> <SET_NUMBERS> ...\n";
exit 1;
}
my $sum = shift @ARGV;
my $x = SubSetSumSolver( \@ARGV, $sum, sub { return $_[0]; } );
$x = NonNegativeSubSetSumSolver( \@ARGV, $sum, sub { return $_[0]; } );
###############################################################################
#
# Local variables:
# compile-command: "perl -c subsetsumsolver.plx"
# End: