0

I need an algorithm that can take any number of match-ups like you can see below and group them by round evenly with each participate in it once if possible. I generated the match-ups below by round for 8 and 3 teams. I am having issues filling up my rounds and being left with an orphan game that cant go in the last round.

Now the rounds are arbitrary but as you can tell each participate can be found in each round (1,2,3,4,5,6,7,8). Now these matchups can be deleted or added, and sorted randomly after they are generated below, hence the need to evenly distribute them and find the rounds at a later date, I just cant save the original rounds since the games could be added/changed/deleted.

This algorithm should be generic and do a best fit per round everytime. If there are an uneven number of matchups per team, then that also needs to be taken into account where they may have extra rounds compared to the others. This needs to be high performance too.

I am looking for some psuedocode in C# .NET or some other language on how to complete this.

8 Teams, 10 games each

Round 1
1 vs 2
3 vs 4
5 vs 6
7 vs 8

Round 2
1 vs 3
2 vs 4
5 vs 7
6 vs 8

Round 3
1 vs 4
2 vs 3
5 vs 8
6 vs 7

Round 4
1 vs 5
2 vs 6
3 vs 7
4 vs 8

Round 5
1 vs 6
2 vs 5
3 vs 8
4 vs 7

Round 6
1 vs 7
2 vs 8
3 vs 5
4 vs 6

Round 7
1 vs 8
2 vs 7
3 vs 6
4 vs 5

Round 8
1 vs 2
3 vs 4
5 vs 6
7 vs 8
Round 9
1 vs 3
2 vs 4
5 vs 7
6 vs 8

Round 10
1 vs 4
2 vs 3
5 vs 8
6 vs 7

3 Teams, 2 Games Each

Round 1
1 vs 2

Round 2
2 vs 3

Round 3
1 vs 3
Mike Flynn
  • 22,342
  • 54
  • 182
  • 341

1 Answers1

1

If you need it more specific, you have to customize the code.

Class Team:

class Team
{
    string name = "";
    List<Team> playedAgainst = new List<Team>();

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    public Team(string name)
    {
        this.name = name;
    }
    public void AddOpponent(Team opponent)
    {
        this.playedAgainst.Add(opponent);
    }
    public bool hasPlayedAgainst(Team opponent)
    {
        return playedAgainst.Contains(opponent);
    }
    public void Reset()
    {
        playedAgainst.Clear();
    }
    public override bool Equals(object obj)
    {
        if(!(obj is Team))
        return base.Equals(obj);
        Team t = (Team)obj;
        return t.name == name;
    }
    public override string ToString()
    {
        return name;
    }
}

class TeamMatchup:

class TeamMatchup
{
    List<Team> involvedTeams = new List<Team>();
    List<List<Team[]>> rounds = new List<List<Team[]>>();
    public void AddTeam(Team team)
    {
        involvedTeams.Add(team);
    }
    public void GenerateBattleRounds()
    {
        rounds = new List<List<Team[]>>();
        while(true)
        {
            List<Team[]> round = new List<Team[]>();
            foreach (Team team in involvedTeams)
            {
                if (!round.TrueForAll(battle => !battle.Contains(team)))
                    continue;
                Team team2 = involvedTeams.FirstOrDefault(t => t != team && !t.hasPlayedAgainst(team) && round.TrueForAll(battle => !battle.Contains(t)));
                if (team2 == null) //even count of teams
                    continue;
                team.AddOpponent(team2);
                team2.AddOpponent(team);
                round.Add(new Team[] { team, team2 });
            }
            if (round.Count == 0)
                break;
            rounds.Add(round);
        }
    }
    public override string ToString()
    {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < rounds.Count; i++)
        {
            sb.AppendLine("Round " + (i + 1));
            foreach (Team[] battle in rounds[i])
            {
                sb.AppendLine(battle[0] + " - " + battle[1]);
            }
        }

        return sb.ToString();

    }
}

Usage:

        TeamMatchup matchup = new TeamMatchup();
        matchup.AddTeam(new Team("Team 1"));
        matchup.AddTeam(new Team("Team 2"));
        matchup.AddTeam(new Team("Team 3"));
        matchup.AddTeam(new Team("Team 4"));
        matchup.AddTeam(new Team("Team 5"));
        matchup.AddTeam(new Team("Team 6"));
        matchup.AddTeam(new Team("Team 7"));
        matchup.AddTeam(new Team("Team 8"));
        matchup.GenerateBattleRounds();
        textBox1.Text = matchup.ToString();

Output:

Round 1
Team 1 - Team 2
Team 3 - Team 4
Team 5 - Team 6
Team 7 - Team 8
Round 2
Team 1 - Team 3
Team 2 - Team 4
Team 5 - Team 7
Team 6 - Team 8
Round 3
Team 1 - Team 4
Team 2 - Team 3
Team 5 - Team 8
Team 6 - Team 7
Round 4
Team 1 - Team 5
Team 2 - Team 6
Team 3 - Team 7
Team 4 - Team 8
Round 5
Team 1 - Team 6
Team 2 - Team 5
Team 3 - Team 8
Team 4 - Team 7
Round 6
Team 1 - Team 7
Team 2 - Team 8
Team 3 - Team 5
Team 4 - Team 6
Round 7
Team 1 - Team 8
Team 2 - Team 7
Team 3 - Team 6
Team 4 - Team 5

edit:

Who and why you vote this solution down? It's the first hint to the users target. If a Questioner wants a more detailed answer, he has to ask more specific (or use at least a question mark).

Mitja
  • 863
  • 5
  • 22
  • Agree with the answerer. He really did his best. Please indicate why this is voted down. If people have to learn from downvoting, please indicate what is missing or wrong. Everybody can learn from it in that case. – Sjips Dec 23 '14 at 20:11
  • I agree, the reviewers of Stackoverflow have become sticklers and I believe hinder good answers like this, kind of like big government getting in the way. – Mike Flynn Dec 23 '14 at 20:42
  • Good answer, but the teams play 10 rounds not 7. – Mike Flynn Dec 23 '14 at 20:47
  • You wanted pseudocode. So I focus on that each player competes against every other player once. The last 3 rounds are a repetition of the first 3. I assumed that this part is the lesser problem. – Mitja Dec 23 '14 at 20:51
  • Well you are taking teams and generating matchups, then rounds. I want to take a list of matchups already created and find the rounds for them. I can already take a list of participants and find the matchups, then rounds, that is pretty easy. – Mike Flynn Dec 23 '14 at 20:57
  • I posted a full example. Please look GenerateBattleRounds (This method will make the rounds for the added teams) to understand the algorithm. Care is taken that every team fights only once per turn and each team only once fighting against another. If my answer not helping, please ignore it and update your initial post, where it sounds, whether you needs an algorithm to create the rounds. – Mitja Dec 23 '14 at 21:05
  • `I generated the match-ups below by round for 8 and 3 teams. I am having issues filling up my rounds and being left with an orphan game that cant go in the last round.` – Mike Flynn Dec 23 '14 at 21:32