从一系列对象中找到所有组合的算法
最近,我的一个朋友建议解决一个简单的问题,我很难找到解决问题的最佳算法。
有一个运动员的列表,每个运动员都有一个名字,一个重量,并且可以具有多个角色,至少有1个最多1个(内部,中位数和外部)。 一支球队由6 运动员组成(2个内部分子,2个中位数和2个外部)。 还有一个重量限制为540公斤。
找到解决此问题的所有可能组合的最佳算法是什么?
现在,我发现所有组合都在运动员名单,所有角色以及删除所有限制的组合中都进行了迭代。
您有更好的算法可以解决此问题吗? 解决此类问题以解决它的最佳方法是什么?
谢谢
Recently a friend of mine proposed to solve a simple problem and I'm struggle to find the best algorithm to solve it.
There's a list of athletes, every athlete has a name, a weight and can have multiple roles, at least 1 maximum 3 ( Internal, Median and External ).
A team is composed by 6 athletes ( 2 internals, 2 medians and 2 externals ).
There is also a weight limit which is at 540kg.
What is the best algorithm to find all possible combination for this problem?
Right now I find all combination iterating through the list of athletes, all of its roles and removing all combinations that overpass a limit.
Do you have any better algorithm to solve this problem?
What is the best method to approach a problem like this in order to solve it?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在最坏的情况下,您拥有
n> = 6
运动员,每个人的权重少,以至于极限无关紧要,每个人都能扮演所有角色,所以团队的数量成长非常非常,很快,即使您不想将具有相同球员的球员分配给不同角色的团队。在这种情况下,确切的数字是“
n
选择6
”或:这是O(n 6 )问题。如果
n
大于30。 123 ,该数量不再适合未签名的32位整数。如果团队的大小可能有所不同,则问题是O(n k ),其中
k
是团队的大小。这不再是多项式;实际上,它比np-complete更难。它将能够枚举解决方案 knapsack问题 IS [1]因此,这是一个工程问题,而不是算法问题。
这几乎是我要做的,只有您可以尽早修剪部分团队来使其更有效。我有一些想法:
In the worst case, where you have
n >= 6
athletes, each weighing so little that the limit doesn't matter, and each able to play all roles, the number of teams grows very, very quickly, even if you don't want to multiply-count the teams that have the same set of players assigned to different roles.The exact number in this case is "
n
choose6
" or:This is an O(n6) problem. This is going to be slow no matter what if
n
is larger than, like, 30. Oncen > 123
, the quantity won't fit in an unsigned 32-bit integer anymore.If the team size can vary, then the problem is O(nk), where
k
is the team size. This is no longer polynomial; in fact, it's harder than NP-Complete. It would be able to enumerate solutions to the Knapsack Problem, which is ♯P complete.[1]Thus, this is an engineering problem more than it is an algorithms problem.
This is pretty much what I would do, only you can make it somewhat more efficient by pruning partial teams as early as possible. Here are some ideas I had: