O
O
Oleg2015-09-25 22:59:15
JavaScript
Oleg, 2015-09-25 22:59:15

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]

Perhaps there are ready-made solutions for a different algorithm, but I am not able to make a relevant query in Google.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mercury13, 2015-09-26
@ptrvch

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]

For an even N, one team constantly changes the field, for the rest, they go astray once on a circle. For odd N, nothing goes astray.

T
trurl123, 2018-12-08
@trurl123

It is very clear how to implement it is described here chess.sainfo.ru/tabl/tablei.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question