循环赛的安排算法?
我最近做了一些研究,并与唐纳德·高德纳 (Donald Knuth) 会面。但我没有找到解决我的问题的正确算法。
问题 我们有一个由 n 名玩家组成的联盟。每周他们都会有一场比赛。在 n-1 周内,每个团队都互相对抗。每天有 n/2 场比赛。但一支球队一周只能比赛一次。如果我们生成一个 (n/k) 组合,我们会得到所有组合...(假设 k = 2),但我需要按正确的顺序排列它们。
我的第一个建议是......不是最好的。我只是做了一个数组,然后让计算机尝试是否找到了正确的方法。如果没有,回到开始,打乱数组并再次执行,好吧,我用 PHP 编写了它(n = 8)并且结果有效,但是需要很多时间,对于 n = 16 它给了我一个超时以及。
所以我想我们是否可以找到一种算法,或者有人知道一本涵盖这个问题的书。
这是我的代码: http://pastebin.com/Rfm4TquY
I recently did studying stuff and meet up with Donald Knuth. But i didn't found the right algorithm to my problem.
The Problem We have a league with n players. every week they have a match with one other. in n-1 weeks every team fought against each other. there are n/2 matches a day. but one team can only fight once in a week. if we generate an (n/k) combination we get all of the combinations... (assuming k = 2) but i need to bring them in the right order.
My first suggestion was... not the best one. i just made an array, and then let the computer try if he finds the right way. if not, go back to the start, shuffle the array and do it again, well, i programmed it in PHP (n=8) and what comes out works, but take many time, and for n=16 it gives me a timeout as well.
So i thought if maybe we find an algorithm, or anybody knows a book which covers this problem.
And here's my code:
http://pastebin.com/Rfm4TquY
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
经典算法的工作原理如下:
将团队编号为 1..n。 (这里我取n=8。)
将所有球队写成两行。
这些列显示了该轮比赛的球队(1 对 8、2 对 7,...)。
现在,保持 1 队固定,但轮换所有其他球队。在第 2 周,您得到
,在第 3 周,您得到
这将持续到第 n-1 周,在这种情况下,
如果 n 是奇数,则执行相同的操作,但添加一个虚拟团队。无论谁与虚拟球队比赛,那周都会再见。
The classic algorithm works like this:
Number the teams 1..n. (Here I'll take n=8.)
Write all the teams in two rows.
The columns show which teams will play in that round (1 vs 8, 2 vs 7, ...).
Now, keep 1 fixed, but rotate all the other teams. In week 2, you get
and in week 3, you get
This continues through week n-1, in this case,
If n is odd, do the same thing but add a dummy team. Whoever is matched against the dummy team gets a bye that week.
下面是它的 JavaScript 代码。
更新:
修复了评论中报告的错误
Here is the code for it in JavaScript.
UPDATED:
Fixed a bug reported in the comments
我制作了这段代码,关于 冈崎 解释
PS:我举了一个奇数的例子,所以有一个队伍不是每轮都打,与undefined决斗,你可以按照你想要的方式定制它
I made this code, regarding the Okasaki explanation
P.S.: I put an example with an odd amount, so there is a team doesn't play every round, dueling with undefined, you can customize it the way you want
我使用可重用函数为此创建了一个更新的解决方案(受到 varun 的启发):
I made an updated solution for this with reusable functions (inspired by varun):
这是Python中的代码,供感兴趣的人参考:
here is the code in python for those interested :