Subset sum with special conditions

有些话、适合烂在心里 提交于 2019-12-04 14:44:39

Given the mentioned conditions, I believe a backtracking solution with branch & bound is your best shot to get the exact solution.

The idea is to check all subsets, but you can trim the calculation tree for some possible subsets during the run of the algorithm.

For example, assume you are looking for S = 10^8, and you already found a solution of sol=10^8 + 10^7, Now, you are checking all subsets that are supersets of some X, and you find out that sum(X) = 10^9. There is no need to keep checking any subsets that contain X, you can simply skip them - they will not get you optimal.

I'd also try to parallelize the solution, branch and bound is easily parallelized usually, just need to synchronize the new best solution once in a while.

As you said that the set wasn't very larget(around 40). I think the classic exponential time algorithm of complexity O(2^(n/2) n) will fit your need http://en.wikipedia.org/wiki/Subset_sum_problem#Exponential_time_algorithm.

I can briefly describe the approach here. Split the set into two equal size set, say A and B. And enumerate the subset sum for them to generate two set of size 2^(n/2), say PA and PB. Then you can sort PA and PB and use binary search to find the sum that exceeds T in time O(2^(n/2) n).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!