最佳匹配算法,以便在比赛中均匀地进行比赛

时间:2014-12-23 18:58:18

标签: c# .net algorithm graph-algorithm tournament

我需要一种能够进行任意数量匹配的算法,如下所示,并将它们均匀分组,每次参与其中一次,如果可能的话。我在下面的比赛中为8队和3队进行了比赛。我有问题填补我的回合,并留下了一个不能进入最后一轮的孤儿游戏。

现在轮次是随意的,但你可以告诉每一轮参赛者都可以在每轮比赛中找到(1,2,3,4,5,6,7,8)。现在这些比赛可以删除或添加,并在下面生成后随机排序,因此需要均匀分配它们并在以后查找轮次,我只是不能保存原始轮次,因为可以添加/更改游戏/删除。

此算法应该是通用的,并且每次每轮最佳匹配。如果每支球队的比赛数量参差不齐,那么还需要考虑与其他球队相比可能有额外轮次的比赛。这也需要高性能。

我正在寻找C#.NET中的一些伪代码或其他一些语言来解决这个问题。

8支队伍,每场10场比赛

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个小组,每个2场比赛

Round 1
1 vs 2

Round 2
2 vs 3

Round 3
1 vs 3

1 个答案:

答案 0 :(得分:1)

如果您需要更具体的内容,则必须自定义代码。

班组:

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;
    }
}

类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();

    }
}

用法:

        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();

输出:

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

编辑:

您为何以及为何将此解决方案投票?这是用户目标的第一个提示。 如果提问者想要更详细的答案,他必须要求更具体(或至少使用问号)。