# SUBSET_SUM The Subset Sum Problem

SUBSET_SUM is a MATLAB program which seeks solutions of the subset sum problem.

This function tries to compute a target value as the sum of a selected subset of a given set of weights.

This function works by brute force, that is, it tries every possible subset to see if it sums to the desired value.

Given N weights, every possible selection can be described by one of the N-digit binary numbers from 0 to 2^N-1.

This function includes a range, which allows the user to control which subsets are to be checked. Thus, if there are N weights, specifying a range of [ 0, 2^N-1] indicates that all subsets should be checked. On the other hand, this full range could be broken down into smaller subranges, each of which could be checked independently.

It is possible that, in the given range, there may be multiple solutions of the problem. This function will only return one such solution, if found. However, the function may be called again, with an appropriate restriction of the range, to continue the search for other solutions.

### Usage:

[ c, index ] = subset_sum ( w, t, r )
where
• w, a set of N weights. N must be no more than 31.
• t, the target value.
• r, a vector of length 2, containing the lower and upper limits to be searched. To search the entire range, use [ 0, 2^N-1]. This argument is optional, and if omitted, the full range is searched.
• c, a vector containing the indices of the weights used to make the combination. This array is empty if no solution was found. Otherwise, the list of weights used to form the sum is w(c).
• index, the index, between r(1) and r(2), of the solution that was found, or an empty value if no solution was found. If a solution was found, you might call again for further solutions in the unchecked portion of the range, using the range [ i+1, r(2) ].

### Example:

```        w = [ 1, 2, 4, 8, 16, 32 ];
t = 22;
r = [ 0, 2^6 - 1 ];

[ c, index ] = subset_sum ( w, t, r )

c = [ 2, 3, 5 ]
index = 22
( the binary representation of 22 is 010110 )
```

### Languages:

SUBSET_SUM is available in a C version and a C++ version and a FORTRAN90 version and a FORTRAN77 version and a MATLAB version.

### Related Data and Programs:

COMBINATION_LOCK, a MATLAB program which simulates the process of determining the secret combination of a lock.

COMBO, a MATLAB library which includes many combinatorial routines.

PARTITION_PROBLEM, a MATLAB library which seeks solutions of the partition problem, splitting a set of integers into two subsets with equal sum.

SATISFY, a MATLAB program which demonstrates, for a particular circuit, an exhaustive search for solutions of the circuit satisfiability problem.

SUBSET_SUM, a dataset directory which contains examples of the subset sum problem, in which a set of numbers is given, and is desired to find at least one subset that sums to a given target value.

SUBSET_SUM_TASKS, a MATLAB program which runs in parallel, seeking solutions of the "subset sum" version of the knapsack problem. Instructions are available on how to run the job, via MATLAB's BATCH facility, on a remote system such as Virginia Tech's ITHACA cluster.

### Reference:

1. Donald Kreher, Douglas Simpson,
Combinatorial Algorithms,
CRC Press, 1998,
ISBN: 0-8493-3988-X,
LC: QA164.K73.
2. Silvano Martello, Paolo Toth,
Knapsack Problems: Algorithms and Computer Implementations,
Wiley, 1990,
ISBN: 0-471-92420-2,
LC: QA267.7.M37.

### Examples and Tests:

You can go up one level to the MATLAB source codes.