SUBSET_SUM The Subset Sum Problem

SUBSET_SUM is a C library which seeks solutions of the subset sum problem.

The task is to compute a target value as the sum of a selected subset of a given set of weights.

SUBSET_SUM_NEXT works by backtracking, returning all possible solutions one at a time, keeping track of the selected weights using a 0/1 mask vector of size N.

SUBSET_SUM_TABLE works by a kind of dynamic programming approach, constructing a table of all possible sums from 1 to S. The storage required is N * S, so for large S this can be an issue.

SUBSET_SUM_FIND works by brute force, trying every possible subset to see if it sums to the desired value. It uses the bits of a 32 bit integer to keep track of the possibilities, and hence cannot work with more N = 31 weights.

Licensing:

I don't care what you do with this code.

Languages:

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

Related Data and Programs:

CHANGE_MAKING, a C library which considers the change making problem, in which a given sum is to be formed using coins of various denominations.

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

COMBO, a C library which includes many combinatorial routines.

KNAPSACK_01, a C library which uses brute force to solve small versions of the 0/1 knapsack problem;

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

SATISFY, a C 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.

TSP_BRUTE, a C program which reads a file of city-to-city distances and solves the traveling salesperson problem, using brute force.

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.

List of Routines:

• BACKUP_ONE seeks the last 1 in the subarray U[0:TOLD-1].
• I4_MAX returns the maximum of two I4's.
• I4_MIN returns the smaller of two I4's.
• I4_POWER returns the value of I^J.
• I4_TO_DIGITS_BINARY produces the binary digits of an I4.
• I4VEC_COPY_NEW copies an I4VEC.
• I4VEC_DOT_PRODUCT computes the dot product of two I4VEC's.
• I4VEC_PRINT prints an I4VEC.
• SUBSET_SUM_COUNT counts solutions to the subset sum problem in a range.
• SUBSET_SUM_COUNT_TEST tests SUBSET_SUM_COUNT.
• SUBSET_SUM_COUNT_TESTS tests SUBSET_SUM_COUNT_TEST.
• SUBSET_SUM seeks a subset of a set that has a given sum.
• SUBSET_SUM_FIND_TEST tests SUBSET_SUM_FIND.
• SUBSET_SUM_FIND_TESTS tests SUBSET_SUM_FIND_TEST.
• SUBSET_SUM_NEXT seeks, one at a time, subsets of V that sum to S.
• SUBSET_SUM_NEXT_TEST tests the SUBSET_SUM_NEXT library.
• SUBSET_SUM_NEXT_TESTS calls SUBSET_SUM_NEXT_TEST with various values.
• SUBSET_SUM_TABLE sets a subset sum table.
• SUBSET_SUM_TABLE_TEST tests SUBSET_SUM_TABLE.
• SUBSET_SUM_TABLE_TESTS tests SUBSET_SUM_TABLE_TEST.
• SUBSET_SUM_TABLE_TO_LIST converts a subset sum table to a list.
• SUBSET_SUM_TABLE_TO_LIST_LENGTH returns the length of a list.
• TIMESTAMP prints the current YMDHMS date as a time stamp.

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

Last revised on 15 July 2017.