Best Fit Algorithm To Evenly Place Matchups In Rounds

六月ゝ 毕业季﹏ 提交于 2020-01-03 04:33:05

问题


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

回答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).



来源:https://stackoverflow.com/questions/27626392/best-fit-algorithm-to-evenly-place-matchups-in-rounds

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!