I am trying to calculate the time complexity of my fitness function for the genetic algorithm I wrote.
What I did do: I already read a few articles and examples
- How to calculate Time Complexity for a given algorithm
- Big-O Complexity Chart
- Determining The Complexity Of Algorithm (The Basic Part)
- How to find time complexity of an algorithm
- Time Complexity of Evolutionary Algorithms for Combinatorial Optimization: A Decade of Results .
However non of these were really satisfying, where I could say: Now I know how to apply this on my code.
Let me show you my fitness function, where I guessed a few execution times.
public static List<double> calculateFitness(List<List<Point3d>> cF, Point3d startpoint)
{
List<double> Fitness = new List<double>(); // 1+1
for (int i = 0; i < cF.Count; i++) // 1 ; N+1 ; N
{
Point3d actual; // N
Point3d next; // N
double distance; // N
double totalDistance = startpoint.DistanceTo(cF[i][0]); // (1+1+1+1)*N
for (int j = 0; j < cF[i].Count - 1; j++) // { 1 ; N ; N-1 }*N
{
actual = cF[i][j]; // (1+1)*(N-1)
next = cF[i][j + 1]; // (1+1)*(N-1)
distance = actual.DistanceTo(next); // (1+1+1+1)*(N-1)
totalDistance += distance; // (1+1)*(N-1)
}
totalDistance += cF[i][cF[i].Count - 1].DistanceTo(startpoint); // (1+1+1+1)*N
Fitness.Add(totalDistance); // N
}
return Fitness; // 1
}
Do you know any links where there are examples, so that I could learn how to calculate the time complexity use-oriented.
Or maybe someone can explain it here. For example for this code piece I'm not sure at all: double totalDistance = startpoint.DistanceTo(cF[i][0]);
--> (1+1)N ?
Or this: actual = cF[i][j];
--> (1+1)NN ?
So in general, the time complexity would be: 1+1+ (1+N+1+N+N+N+N+4N+ N*{ 1+N+N-1+2*(N-1)+2*(N-1)+4*(N-1)+2*(N-1) } +4N+N) = 2 + (2+14N+ N*{12N-10}) = 12N^2 + 4N + 4 = O(N^2)