从一系列对象中找到所有组合的算法

发布于 2025-01-31 04:07:37 字数 344 浏览 4 评论 0原文

最近,我的一个朋友建议解决一个简单的问题,我很难找到解决问题的最佳算法。

有一个运动员的列表,每个运动员都有一个名字,一个重量,并且可以具有多个角色,至少有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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼角的笑意。 2025-02-07 04:07:37

对于此问题,找到所有可能组合的最佳算法是什么?


在最坏的情况下,您拥有n> = 6运动员,每个人的权重少,以至于极限无关紧要,每个人都能扮演所有角色,所以团队的数量成长非常非常,很快,即使您不想将具有相同球员的球员分配给不同角色的团队。

在这种情况下,确切的数字是“ n选择6”或:

n * (n - 1) * (n - 2) * (n - 3) * (n - 4) * (n - 5) / 720

这是O(n 6 )问题。如果n大于30。 123 ,该数量不再适合未签名的32位整数。

如果团队的大小可能有所不同,则问题是O(n k ),其中k是团队的大小。这不再是多项式;实际上,它比np-complete更难。它将能够枚举解决方案 knapsack问题 IS [1]

因此,这是一个工程问题,而不是算法问题。

现在,我发现所有组合都通过运动员名单,所有角色并删除了限制限制的所有组合。

这几乎是我要做的,只有您可以尽早修剪部分团队来使其更有效。我有一些想法:

  1. 按重量排序运动员名单,以便当您超出体重限制时,您可以停止查看名单中的后来的运动员。
  2. 鉴于党派团队,请跟踪有多少角色可以实现他们。当您到达第五成员时,您知道运动员必须在其中一个短角色中扮演角色。
    1. 例如,如果您到目前为止有四个运动员的团队,没有中位数球员,则必须仅考虑接下来的两个球员。
    2. 为了使这一点更容易,为每个角色的角色创建(排序)辅助列表,这些角色包含指向可以具有该角色的玩家的指针。

What is the best algorithm to find all possible combination for this problem?

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 choose 6" or:

n * (n - 1) * (n - 2) * (n - 3) * (n - 4) * (n - 5) / 720

This is an O(n6) problem. This is going to be slow no matter what if n is larger than, like, 30. Once n > 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.

Right now I find all combination iterating through the list of athletes, all of its roles and removing all combinations that overpass a limit.

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:

  1. Sort the list of athletes by weight so that when you go over the weight limit, you can stop looking at the later athletes in the list.
  2. Keep track of how many roles have a possible athlete to fulfill them, given the partial team. When you reach the fifth member, you know the athlete must have a role in one of the shorthanded roles.
    1. For example, if you have a team of four athletes so far with no median players, you must only consider median players for the next two.
    2. To make this easier, create (sorted) auxiliary lists for each role that contain pointers to the players that can have that role.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文