0

This is my first question. I tried to find an answer for 2 days but I couldn't find what I was looking for.

Question: How can I minimize the amount of matches between students from the same school

I have a very practical case, I need to arrange a competition (tournament bracket) but some of the participants might come from the same school. Those from the same school should be put as far as possible from each other

for example: {A A A B B C} => {A B}, {A C}, {A B}

if there are more than half participants from one school, then there would be no other way but to pair up 2 guys from the same school.

for example: {A A A A B C} => {A B}, {A C}, {A A}

I don't expect to get code, just some keywords or some pseudo code on what you think would be a way of making this would be of great help!

I tried digging into constraint resolution algorithms and tournament bracket algorithms, but they don't consider minimising the amount of matches between students from same school.

Well, thank you so much in advance!

Probie
  • 1,371
  • 7
  • 15
  • One more way: Sort the participants by school (like you already did). Then, every second will go in the top bracket, the others will go in the bottom bracket. E.g. for participants `(1, 2, 3, 4, 5, 6`), `(1, 3, 5)` go to the top and `(2, 4, 6)` go to the bottom. Then, within the brackets, do the same recursively, i.e. `(1, 5)` will go to the first bracket, `(3)` to the second, `(2, 6)` to the third, and `(4)` to the fourth. – Nico Schertler Apr 26 '18 at 06:19
  • @Nico Schertler Thanks for the suggestion, I think this is they way to go. I will try it out and tell you how it went. thanks! – Chilli Lucas Apr 26 '18 at 07:00

3 Answers3

0

A simple algorithm (EDIT 2)

From the comments below: you have a single elimination tournament. You must choose the places of the players in the tournament bracket. If you look at your bracket, you see: players, but also pairs of players (players that play the match 1 against each other), pairs of pairs of players (winner of pair 1 against winner of pair 2 for the match 2), and so on.

The idea

  • Sort the students by school, the schools with the more students before the ones with the less students. e.g A B B B B C C -> B B B B C C A.
  • Distribute the students in two groups A and B as in a war card game: 1st student in A, 2nd student in B, 3rd student in A, 4th student in B, ...
  • Continue with groups A and B.

You have a recursion: the position of a player in the level k-1 (k=n-1 to 0) is ((pos at level k) % 2) * 2^k + (pos at level k) // 2 (every even goes to the left, every odd goes to the right)

Python code

Sort array by number of schools:

assert 2**math.log2(len(players)) == len(players) # n is the number of rounds
c = collections.Counter([p.school for p in players])
players_sorted_by_school_count = sorted(players, key=lambda p:-c[p.school])

Find the final position of every player:

players_sorted_for_tournament = [-1] * 2**n
for j, player in enumerate(players_sorted_by_school_count):
    pos = 0
    for e in range(n-1,-1,-1):
        if j % 2 == 1:
            pos += 2**e # to the right
        j = j // 2
    players_sorted_for_tournament[pos] = player

This should give groups that are diverse enough, but I'm not sure whether it's optimal or not. Waiting for comments.

First version: how to make pairs from students of different schools

Just put the students from a same school into a stack. You have as many stack as schools. Now, sort your stacks by number of students. In your first example {A A A B B C}, you get:

A
A B
A B C

Now, take the two top elements from the two first stacks. The stack sizes have changed: if needed, reorder the stacks and continue. When you have only one stack, make pairs from this stack.

The idea is to keep as many "schools-stacks" as possible as long as possible: you spare the students of small stacks until you have no choice but to take them.

Steps with your second example, {A A A A B C}:

A
A
A
A B C => output A, B

A
A
A C => output A, C

A
A => output A A

It's a matching problem (EDIT 1)

I elaborate on the comments below. You have a single elimination tournament. You must choose the places of the players in the tournament bracket. If you look at your bracket, you see: players, but also pairs of players (players that play the match 1 against each other), pairs of pairs of players (winner of pair 1 against winner of pair 2 for the match 2), and so on.

Your solution is to start with the set of all players and split it into two sets that are as diverse a possible. "Diverse" means here: the maximum number of different schools. To do so, you check all possible combinations of elements that split the set into two subsets of equals size. Then you perform recursively the same operation on those sets, until you arrive to the player level.

Another idea is to start with players and try to make pairs with other players from other school. Let's define a distance: 1 if two players are in the same school, 0 if they are in a different school. You want to make pairs with the minimum global distance.

This distance may be generalized for the pairs of players: take the number of common schools. That is: A B A B -> 2 (A & B), A B A C -> 1 (A), A B C D -> 0. You can imagine the distance between two sets (players, pairs, pairs of pairs, ...): the number of common schools. Now you can see this as a graph whose vertices are the sets (players, pairs, pairs of pairs, ...) and whose edges connect every pair of vertices with a weight that is the distance defined above. You are looking for a perfect matching (all vertices are matched) with a minimum weight.

The blossom algorithm or some of its variants seems to fit your needs, but it's probably overkill if the number of players is limited.

jferard
  • 7,835
  • 2
  • 22
  • 35
  • Hey, Thank you so much for your help! This is awesome, but I think it still wont put the players as far from each other as possible. for example A A A B C D E F G H I would get the following pairs {A B}, {A C}, {A D}, {E F}, {G H} In this case, if two students from the same school win on the first round, then they will meet immediately on their second match. How would you sort those pairs so that A's are as far as possible such as: {A B}, {E F}, {A C}, {G H}, {A D} – Chilli Lucas Apr 26 '18 at 06:52
  • 1
    Define "far away as possible" in this extra criteria, if two A players win their matches, their *next* match will be against each other, even if there's a ton of matches between, can you please outline **all** the criteria in your question? Otherwise this turns out to be a moving target with "Yes, that looks good, but what about ...?". – Lasse V. Karlsen Apr 26 '18 at 08:04
  • I'm so sorry about that. The whole idea is to distribute a list with same elements as separate as possible. For example, the list {A A A B B C D E F} needs to be sorted as {A B C D A E F B A} thank you and I am sorry for not being clear enough – Chilli Lucas Apr 26 '18 at 08:21
  • @Chili Lucas If I understand, you have a tournament. You have to make pairs of pairs, then pairs of pairs of pairs, then.... And A B C D is better than A B A C which is better than A B A B. This is a matching problem: https://en.wikipedia.org/wiki/Matching_(graph_theory) – jferard Apr 26 '18 at 10:32
  • @jferard Yes! This is what did the trick! So I separated the group into 2 groups with all possible combinations (not caring of the order) of participants in group1 and group2. Then each combination gets a score based on how many repeated elements it contains. Then repeat recursively for each subgroup until count of participants per group is 2 or 1. Thank you! – Chilli Lucas Apr 26 '18 at 16:50
0

Create a two-dimensional array, where the first dimension will be for each school and the second dimension will be for each participant in this take-off. Load them and you'll have everything you need linearly. For example:

School 1 ------- Schol 2 -------- School 3

A ------------ B ------------- C

A ------------ B ------------- C

A ------------ B ------------- C

A ------------ B

A ------------ B

A

A

In the example above, we will have 3 schools (first dimension), with school 1 having 7 participants (second dimension), school 2 having 5 participants and school 3 having 3 participants. You can also create a second array containing the resulting combinations and, for each chosen pair, delete this pair from the initial array in a loop until it is completely empty and the result array is completely full.

Rogério Dec
  • 801
  • 8
  • 31
0

I think the algorithm in this answer could help.

Basically: group the students by school, and use the error tracking idea behind Bresenham's Algorithm to distribute the schools as far apart as possible. Then you pull out pairs from the list.

AShelly
  • 34,686
  • 15
  • 91
  • 152