基本上我有一个玩家列表,我想把它们配对,这样每个玩家就可以和每个人玩一次。查找此数据的最快方法是什么?
发布于 2011-02-19 02:41:19
一个简单的分而治之的算法:
:
1. Split the group in two groups of equal size.
2. Find all pairings in each group using this algorithm recursively.
3. Join the two lists.例如,[[(a,b)]]和[[(c,d)]]变成了[[(a,b),(c,d)]]。
4.通过轮换第二组,找出两组之间的配对。
例如,[[(a,c),(b,d)],[(a,d),(b,c)]]
5.返回(3) + (4)
该算法在O(n^2)时间内运行,这是最优的,因为它生成(n-1)轮的n/2配对。
对于8个玩家,你将得到7个回合:
[(a,b), (c,d), (e,f), (g,h)]
[(a,c), (b,d), (e,g), (f,h)]
[(a,d), (b,c), (e,h), (f,g)]
[(a,e), (b,f), (c,g), (e,h)]
[(a,f), (b,g), (c,h), (e,e)]
[(a,g), (b,h), (c,e), (e,f)]
[(a,h), (b,e), (c,f), (e,g)]https://stackoverflow.com/questions/5045137
复制相似问题