I have n integers given; both positive and negative values are included. What is a good algorithm to find m integers from that list, such that that the absolute value of the sum of those m integers is the smallest possible?
我有n个整数;包括正值和负值。从这个列表中找到m个整数的好算法是什么,这样,这些m整数的和的绝对值是最小的?
2 个解决方案
#1
5
The problem is NP-hard, since solving it efficiently would solve the subset-sum decision problem efficiently.
这个问题是np困难的,因为有效地解决它可以有效地解决子集和决策问题。
Given that, you're not going to find an efficient algorithm to solve it unless you believe that P=NP.
如果你不相信P=NP,那么你就不会找到一个有效的算法来解决它。
You can always come up with some heuristics to direct your search but in the worst case you'll have to check every subset of m integers.
你总是可以想出一些启发法来指导你的搜索,但在最坏的情况下,你必须检查每一个m个整数的子集。
#2
2
If "good" means "correct", then just try every possibility. This will take you about n choose m
time. Very slow. Unfortunately, this is the best you can do in general, because for any set of integers you can always add one more that is the negative of a sum of m-1
other ones--and those others could all have the same sign, so you have no way to search.
如果“好”的意思是“正确的”,那么就尝试每一种可能。这将花费你大约n选择m时间。非常缓慢。不幸的是,这是你能做的最好的,因为对于任何一组整数,你总是可以再加一个,这是m-1的和的负的,其他的都有相同的符号,所以你没有办法搜索。
If "good" means "fast and usually works okay", then there are various ways to proceed. E.g.:
如果“好”的意思是“快而且通常工作正常”,那么就有各种各样的方法来进行。例如:
Suppose you can solve the problem for m=2
, and suppose further you can solve it for both the positive and the negative answer (and then take the smaller of the two). Now suppose you want to solve m=4
. Solve for m=2
, then throw those two numbers out and solve again...should be obvious what to do next! Now, what about m=6
?
假设你能解出m=2的问题,假设你能解出正的和负的答案(然后取较小的两个)。现在假设你想解m=4。解出m=2,然后把这两个数丢出来再解…应该很明显下一步该做什么!现在,m = 6呢?
Now suppose you can solve the problem for m=3
and m=2
. Think you can get a decent answer for m=5
?
现在假设你能解出m=3和m=2的问题。你觉得m=5能得到一个不错的答案吗?
Finally, note that if you sort the numbers, you can solve for m=2
in one pass, and for m=3
you have an annoying quadratic search to do, but at least you can do it on only about a quarter of the list twice (the small halves of the positive and negative numbers) and look for a number of opposite sign to cancel.
最后,请注意,如果你的数字,你可以解出在一个通过m = 2,和3 m =你有一个烦人的二次搜索,但至少你能做到只有约四分之一的名单上两次(积极的和消极的数字)的很小部分,寻找异号取消。
#1
5
The problem is NP-hard, since solving it efficiently would solve the subset-sum decision problem efficiently.
这个问题是np困难的,因为有效地解决它可以有效地解决子集和决策问题。
Given that, you're not going to find an efficient algorithm to solve it unless you believe that P=NP.
如果你不相信P=NP,那么你就不会找到一个有效的算法来解决它。
You can always come up with some heuristics to direct your search but in the worst case you'll have to check every subset of m integers.
你总是可以想出一些启发法来指导你的搜索,但在最坏的情况下,你必须检查每一个m个整数的子集。
#2
2
If "good" means "correct", then just try every possibility. This will take you about n choose m
time. Very slow. Unfortunately, this is the best you can do in general, because for any set of integers you can always add one more that is the negative of a sum of m-1
other ones--and those others could all have the same sign, so you have no way to search.
如果“好”的意思是“正确的”,那么就尝试每一种可能。这将花费你大约n选择m时间。非常缓慢。不幸的是,这是你能做的最好的,因为对于任何一组整数,你总是可以再加一个,这是m-1的和的负的,其他的都有相同的符号,所以你没有办法搜索。
If "good" means "fast and usually works okay", then there are various ways to proceed. E.g.:
如果“好”的意思是“快而且通常工作正常”,那么就有各种各样的方法来进行。例如:
Suppose you can solve the problem for m=2
, and suppose further you can solve it for both the positive and the negative answer (and then take the smaller of the two). Now suppose you want to solve m=4
. Solve for m=2
, then throw those two numbers out and solve again...should be obvious what to do next! Now, what about m=6
?
假设你能解出m=2的问题,假设你能解出正的和负的答案(然后取较小的两个)。现在假设你想解m=4。解出m=2,然后把这两个数丢出来再解…应该很明显下一步该做什么!现在,m = 6呢?
Now suppose you can solve the problem for m=3
and m=2
. Think you can get a decent answer for m=5
?
现在假设你能解出m=3和m=2的问题。你觉得m=5能得到一个不错的答案吗?
Finally, note that if you sort the numbers, you can solve for m=2
in one pass, and for m=3
you have an annoying quadratic search to do, but at least you can do it on only about a quarter of the list twice (the small halves of the positive and negative numbers) and look for a number of opposite sign to cancel.
最后,请注意,如果你的数字,你可以解出在一个通过m = 2,和3 m =你有一个烦人的二次搜索,但至少你能做到只有约四分之一的名单上两次(积极的和消极的数字)的很小部分,寻找异号取消。