Basically I have list of players, and I want to pair them up so that each player will play everyone once. What\'s the quickest way to find this data?
A simple divide and conquer algorithm:
Otherwise:
Join the two lists.
E.g. [[(a,b)]]
and [[(c,d)]]
becomes [[(a,b),(c,d)]]
.
Find pairs across of the two groups, by rotating group two.
E.g. [[(a,c),(b,d)],[(a,d),(b,c)]]
(3)
+ (4)
This algorithm runs in O(n^2)
time, which is optimal, as it generates (n-1)
rounds of n/2
pairings.
For eight players you would get 7 rounds:
[(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)]