分组算法
我试图帮助某人编写一个我认为很容易的程序,但当然它从来都不是:)
我试图采取班级名册(通常在 10-20 名学生之间)并有效地将每个同学与另一个同学有效地唯一配对建立独特的群体。 因此,一个10人的班级,可以分成9组。
它也需要能够处理奇数数量的学生,这增加了我的困惑。
我正在考虑用 Java 来做这件事,但这很灵活。 关于算法方式的任何想法都可以保证a)不是无限循环(以已经成为合作伙伴的2个人结束)和b)我的目标是时间比空间更有效,因为班级规模会很小!
谢谢!
I am trying to help someone write a program that I thought would be easy, but of course it never is :)
I am trying to take a class roster (usually between 10-20 students) and effectivly uniquely pair off each classmate to another to make unique groups. Therefore in a class of 10 people, you can have 9 groups.
It needs to be able to handle odd number of students too, adding to my confusion.
I was looking at doing this in Java, but that is flexible. Any ideas on an algorithmic way to guarantee a)not infinite looping (ending with 2 people who have already been partners) and b) I am aiming for more efficent time than space, since class size will be small!
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我不知道这是否正是您所要求的,但这里是我用简单的 python 来实现的。
它会给出(在我的示例中)10 名学生的每个独特分组。
我猜这不是最快的事情,但它很容易实现和遵循。
元组代表一行中的所有组:
(0, 9, 1, 8, 2, 7, 3, 4, 5, 6) 表示 0 与 9 分组,1 与 8 分组,依此类推。
I don't know if it's exactly what you asked for, but here my take on it in simple python.
It spits out each unique grouping you can have for (in my example) 10 students.
It is not the fastest thing there is i guess, but its very easy to implement and to follow.
a tuple represents all groups in a row:
(0, 9, 1, 8, 2, 7, 3, 4, 5, 6) means 0 is grouped with 9, 1 is grouped with 8 and so on.
您想要创建一个以每个学生为节点的完整图,然后随机选择边并将它们插入到唯一的集合中。
在下一次“拉动”时,您想要执行相同的操作,但现在如果边缘已被选择,则丢弃并重新选择。
You want to create a complete graph with each student as a node, then randomly select edges and insert them into a unique set.
On the next "pull", you want to do the same thing, except now if the edge has already been selected, discard and reselect.
这是解决问题的 C# 代码。
我假设您真正关心的是最大化学生配对的独特性,而不是一组可能的独特学生配对组。
Here is C# code which solves the question.
I've assumed that you really care about maximizing the uniqueness in student pairing, not the set of possible unique groups of student pairings.
这对我来说是一个不寻常的答案 - 说“下载应用程序” - 但你就可以了:
你所描述的可能与国际象棋锦标赛配对足够相似。
看看这个:http://home.swipnet.se/rullchef/chessp/
这里是对 Monrad 系统的解释可能就是您所追求的:
This is an unusual answer for me - to say "download an application" - but here you go:
What you describe may be similar enough to Chess Tournament Pairing.
Check this out: http://home.swipnet.se/rullchef/chessp/
Here's an explanation of the Monrad system which may be what you're after:
这是上面 Vlion 答案的伪代码。 这不是最快的方法,但它是概念的说明(感谢 Vlion!)
Here's the pseudocode for Vlion's answer above. This isn't the fastest way to do it but it's an illustration of the concept (thanks Vlion!)
您要求的算法似乎与准备循环赛赛程的算法或多或少相同。 详细信息可以在这篇维基百科文章中找到。 您还可以使用网上的生成器进行快速试用。 其中之一可以在此处找到。
The algorithm you are asking for seems more or less the same as the algorithm for preparing schedules for round-robin tournaments. The details can be found in this Wikipedia article. You can also use generators lying around on the web for a quick tryout. One of them can be found here.