0
private  void CalculateFitness(TimeTable timeTable)
{
    int score = 0, DAYS_NUM = 5;

    score = timeTable.Exams.SelectMany(exam => exam.Students)
         .GroupBy(s => s)
         .Select(g => Connections(g.Count()))
         .Sum();
    timeTable.Fitness = score;
}

int Connections(int corners)
{
    // 0+1+2+...+(corners-1)
    return corners * (corners - 1) / 2;
}
4

1 回答 1

1

您的功能不等同于:

score = timeTable.Exams.SelectMany(exam=>exam.Students)
                 .GroupBy(s=>s)
                 .Select(g=>Connections(g.Count()))
                 .Sum();

带辅助功能

int Connections(int corners)
{
  //Formula for number of sides in a complete graph
  //http://en.wikipedia.org/wiki/Complete_graph
  // 0+1+2+...+(corners-1)
  return corners*(corners-1)/2;
}

这应该是线性运行时,timeTable.Exams.Sum(exam=>exam.Student.Count())而你的在我看来是二次的。

于 2011-01-12T15:00:42.787 回答