返回所有可能的匹配配对的算法
我有一个 PlayerID 数组,
Players = [ 1, 2, 4, 12, 14, 16 ]
现在需要一个返回所有可能组合的算法,例如
[1,2], [4,12], [14,16]
[1,4], [2,12], [14,16]
[1,12], [2,4], [14,16]
[1,14], [2,4], [12,16]
...and so on, till all combinations have been listed
[1,2] 或 [2,1] 之间没有区别。 我会将 X
我发现了以下方法 [https://stackoverflow.com/a/16256122][1] 这非常接近我所寻找的,但该算法并不对配对进行分组(6 名玩家的数组 = 3 组可能的配对)。
有人可以改进这个算法来满足我的需要吗?
Java、C++、C# 或 PHP 中的示例都很好。
先感谢您!
I have an Array of PlayerIDs
Players = [ 1, 2, 4, 12, 14, 16 ]
I now need an algorithm that returns all possible combinations, like
[1,2], [4,12], [14,16]
[1,4], [2,12], [14,16]
[1,12], [2,4], [14,16]
[1,14], [2,4], [12,16]
...and so on, till all combinations have been listed
There is no difference between [1,2] or [2,1].
I would stay that X<Y at [X,Y]
I found the following approach [https://stackoverflow.com/a/16256122][1] which is really near to what I look for, but this algorithm doesn't group the pairings (Array of 6 Players = Group of 3 possible pairings).
Can somebody refine this algorithm that fits my needs?
Examples in Java, C++, C# or PHP are fine.
Thank you in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
急切评估
我们可以编写
choosek(t, k)
来从数组或列表中查找大小为k
的所有固定长度组合,t
。我们可以使用归纳推理来解决问题 -k
为零或负数,没有更多的选择,返回一个空选择的单例列表k
是正数,所以至少有一个选择。如果t
为空,则没有任何可供选择的内容,返回空的选择列表。k
为正,t
至少有一个选择。对于子问题choosek(t.slice(1), k - 1)
的每个comb
,将t
的第一个元素添加到由此产生的梳子
。将此结果连接到第二个子问题choosek(t.slice(1), k)
的结果。下面我选择了 JavaScript,因为我可以在答案中嵌入一个功能演示。运行下面的程序来查看
k = 2
和k = 3
的演示eager evaluation
We can write
choosek(t, k)
to find all fixed-length combinations of sizek
from an array or list,t
. We can approach the problem using inductive reasoning -k
is zero or negative, there are no more choices to make, return a singleton list of the empty choicek
is positive so there is at least one thing to choose. Ift
is empty, there is nothing to choose from, return the empty list of choices.k
is positive andt
has at least one choice to make. For eachcomb
of the sub-problemchoosek(t.slice(1), k - 1)
, prepend the first element oft
to the resultingcomb
. Concatenate this result to the result of the second sub-problem,choosek(t.slice(1), k)
.Below I chose JavaScript because I can embed a functioning demo right here in the answer. Run the program below to see demoes of
k = 2
andk = 3
All of the functions used here
.map
,.concat
, and.slice
have straightforward counterparts in PHP, Java, Python, and C#.Notice that
choosek
returns an array of combinations where each combination is itself an array. This allows the caller to do whatever she/he pleases with each combination. For example, you may wish to convert each to a string for logging in the console or perhaps you will leave them as arrays to build a tournament bracket.generators
PHP, Python, and C# all support generators, though I'm unsure of Java. Using JavaScript we can demonstrate
choosek
using generators. The advantage of this approach are numerous -Generators are lazy, meaning we can pause/resume them. The caller can collect as many combinations as needed, discard the rest, or exhaust the entire generator.
If the input is significantly large, the solution space may be gigantic. Forcing all of the combinations into an array before returning the result could cost a lot of memory. Generators allow you to process the result as each one is being generated.
A generator's results can easily be collected into an array using
Array.from
or equivalent, if desired, though this is not required. This choice is left for the caller to decide. As a result, the generator implementation reduces one level of nesting arrays, making it easier to read/write the program.The exact same inductive reasoning approach can be applied to solve the problem
To compute permutations, see this related Q&A.