Home > Back-end >  Is there an algorithm that can pair in each execution all the different pairs without having dublica
Is there an algorithm that can pair in each execution all the different pairs without having dublica

Time:09-16

To be more specific i want to make a tournament algorithm that mathces all the participants each round with another pair and not having each round same opponent . I hope this graph helps you understand.enter image description here

enter image description here If anything isn't clear i would be more than happy to explain to you the problem .

Also it doesn't have to be with the order i drew the lines.But i thought thats the systematic approach i have in my mind.

CodePudding user response:

You want a round robin scheduling algorithm.

Long story short, put all the participants on two rows, and at the first round match those who are on the same column. Then keep one player fixed (usually it's the first in the first row), rotate all the others clockwise, and again match those on the same column, and so on until all the rounds have been played.

CodePudding user response:

You can use the circle method. In the Wikipedia article the example keeps participant #1 at a fixed position, but below I have numbered participants starting at 0 (like you did) and kept the last one fixed, while the others rotate:

function calcRound(n, round) {
    return Array.from({length: n >> 1}, (_, i) =>
        [(i   round) % (n - 1), i ? (n - i - 1   round) % (n - 1) : n - 1]
    )
}

const numParticipants = 8;
for (let round = 0; round < numParticipants - 1; round  ) {
    const pairs = calcRound(numParticipants, round);
    console.log(JSON.stringify(pairs));
}

  • Related