Answer the question
In order to leave comments, you need to log in
How to implement a round robin schedule?
Is there an elegant way to implement the algorithm, the input of which is an array of participants and the number of the round, and the output is pairs of opponents formed in a certain order.
The principle of building a schedule is described here , but it looks like this:
Первый тур:
home: [t0 t1 t2 t3 t4 ]
guest: [t5 t6 t7 t8 t9 ]
Второй тур:
home: [t0 t5 t1 t2 t3]
guest: [t6 t7 t8 t9 t4]
третий тур:
home: [t0 t6 t5 t1 t2]
guest: [t7 t8 t9 t4 t3]
Answer the question
In order to leave comments, you need to log in
I just don't see any other methods. Search for "round-robin tournament algorithm". But, in my opinion, it is easier to turn not the ring, but the pattern of matches by placing one of the teams (preferably the last one) in the center of the circle.
https://commons.wikimedia.org/wiki/File:Round-robi...
Matches 2-13, 3-12 etc. we give fixed directions, and alternately: 2-13, 12-3, 11-4 ...
The direction of the match 1-14 is constantly changing.
If the teams are numbered from 0 to N−1, N is even, the algorithm is as follows.
trans := случайная перестановка чисел 0 … N−1
если N чётное
то M := N−1
иначе M := N
цикл 2half = false…true
цикл round = 0…M−1
цикл shift = 1…[M/2]
home := (round + shift) % M
away := (round + M − shift) % M
если (shift нечётное) xor 2half
обменять home, away
вывести trans[home], trans[away]
если N чётное
home := round
away := M
если (round нечётное) xor 2half
обменять home, away
вывести trans[home], trans[away]
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question