algorithm - Find out which combinations of numbers in a set add up to a given total -


i've been tasked helping accountants solve common problem have - given list of transactions , total deposit, transactions part of deposit? example, have list of numbers:

1.00 2.50 3.75 8.00 

and know total deposit 10.50, can see it's made of 8.00 , 2.50 transaction. however, given hundred transactions , deposit in millions, becomes more difficult.

in testing brute force solution (which takes way long practical), had 2 questions:

  1. with list of 60 numbers, seems find dozen or more combinations total that's reasonable. i expecting single combination satisfy total, or maybe few possibilities, there seem ton of combinations. there math principle describes why is? seems given collection of random numbers of medium size, can find multiple combination adds total want.

  2. i built brute force solution problem, it's o(n!), , grows out of control. aside obvious shortcuts (exclude numbers larger total themselves), there way shorten time calculate this?

details on current (super-slow) solution:

the list of detail amounts sorted largest smallest, , following process runs recursively:

  • take next item in list , see if adding running total makes total match target. if does, set aside current chain match. if falls short of target, add running total, remove list of detail amounts, , call process again

this way excludes larger numbers quickly, cutting list down numbers needs consider. however, it's still n! , larger lists never seem finish, i'm interested in shortcuts might able take speed - suspect cutting 1 number out of list cut calculation time in half.

thanks help!

this special case of knapsack problem called subset sum.


Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -