Jump to content

Subset sum problem: Difference between revisions

→‎{{header|C}}: Call it a set not path
(→‎{{header|C}}: shorten the example, to show all the zero sum subsets, you can kill the process if you got all you need from it. and its easier to follow without the bit math which doesn't change the time complexity anyway.)
(→‎{{header|C}}: Call it a set not path)
Line 207:
 
int n = sizeof (items) / sizeof (item_t);
int *pathset;
 
void subsum (int i, int weight) {
Line 213:
if (i && !weight) {
for (j = 0; j < i; j++) {
item_t item = items[pathset[j]];
printf("%s%s", j ? " " : "", items[pathset[j]].word);
}
printf("\n");
}
for (j = i ? pathset[i - 1] + 1: 0; j < n; j++) {
pathset[i] = j;
subsum(i + 1, weight + items[j].weight);
}
Line 225:
 
int main () {
pathset = malloc(n * sizeof (int));
subsum(0, 0);
return 0;
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.