1
$\begingroup$

I have a very long "list" of numbers ( maybe thousands ) which may be grouped, by sum into "n" groups. The number of groups and values are given. For example:

  • List of numbers: [1, 2, 5, 6, 7, 8.5, 10]
  • Groups:[3, 15, 21.5]

I need to find a summary of which numbers in the "list" will crate given "groups".

  • 3 = 1+2
  • 15 = 5+10
  • 21.5 = 6+7+8.5

Ideas?

$\endgroup$
6
  • $\begingroup$ Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. $\endgroup$
    – Community Bot
    Commented Jan 24, 2022 at 11:16
  • $\begingroup$ This is basically the subset-sum-problem , which is NP-complete. Maybe there are efficient algorithms working well in practical instances. $\endgroup$
    – Peter
    Commented Jan 24, 2022 at 11:26
  • $\begingroup$ subset-sum talking about finding a solution for one target only, not to list of targets $\endgroup$ Commented Jan 24, 2022 at 11:40
  • $\begingroup$ Are the numbers from the given list and the groups such that the solution exists and it's unique? $\endgroup$
    – jjagmath
    Commented Jan 24, 2022 at 12:41
  • $\begingroup$ @jjagmath Yes, it is $\endgroup$ Commented Jan 24, 2022 at 14:54

2 Answers 2

1
$\begingroup$

You can solve the problem via integer linear programming as follows. Let binary decision variable $x_{i,g}$ indicate whether item $i$ (with value $v_i$) appears in group $g$. Let $s_g$ be the target sum for group $g$. The problem is to find a feasible solution to the linear constraints \begin{align} \sum_g x_{i,g} &= 1 &&\text{for all $i$} \\ \sum_i v_i x_{i,g} &= s_g &&\text{for all $g$} \end{align}

$\endgroup$
1
$\begingroup$

This looks like a variation of The Subset Sum Problem Given a list of numbers, does some subset's sum equal 0? The complexity remains the same if the target isn't zero.

A brute force algorithm is loop through the desired targets generating subsets, then add each subset's terms. There are $2^N-1$ possible subsets if you ignore the empty set. You can generate the subsets by generating the first $2^N-1$ binary numbers, and have a routine that can convert the number into its corresponding sum. $1$ means include, $0$ means exclude.

You probably don't want to use this method for 1000 data points. That article has some alternatives.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .