Combination Generator

S

Scott Dudley

I need an algorithm that will, provided a set of decimal numbers,
identify any combination thereof that add up to N. The size of the set
(array, list, etc.) is unknown.

Seems I recall seeing code for a combination generator in one of my
algorithm books (Sedgwick?) that would fit the bill. Unfortunately, I'm
faced with a challenge at work and my books are at home.

Thanks.
 
R

Roedy Green

Seems I recall seeing code for a combination generator in one of my
algorithm books (Sedgwick?) that would fit the bill. Unfortunately, I'm
faced with a challenge at work and my books are at home.

see http://mindprod.com/jgloss/combination.html

--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
G

Gordon Beaton

I need an algorithm that will, provided a set of decimal numbers,
identify any combination thereof that add up to N. The size of the
set (array, list, etc.) is unknown.

This is a knapsack problem, a one-dimensional special case of bin
packing. You'll find lots of information, analysis and example code if
you google. AFAIK the problem is NP-complete.

Here's a good heuristic however, from memory:

Choose the largest of your remaining elements that fits in N, and
reduce N accordingly. Repeat until either N is 0, or there are no more
elements that are small enough.

/gordon
 
B

bugbear

Scott said:
I need an algorithm that will, provided a set of decimal numbers,
identify any combination thereof that add up to N. The size of the set
(array, list, etc.) is unknown.

Seems I recall seeing code for a combination generator in one of my
algorithm books (Sedgwick?) that would fit the bill. Unfortunately, I'm
faced with a challenge at work and my books are at home.

http://www.nuigalway.ie/mat/algorithms/knapsack.html

BugBear
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,526
Members
44,997
Latest member
mileyka

Latest Threads

Top