Pergunta

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;
}
Foi útil?

Solução

Isn't your function equivalent to this:

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

with helper function

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

This should be linear runtime in timeTable.Exams.Sum(exam=>exam.Student.Count()) whereas yours looks quadratic to me.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top