【发布时间】:2017-09-26 15:26:13
【问题描述】:
我正在尝试计算我编写的遗传算法的适应度函数的时间复杂度。
我做了什么:我已经阅读了一些文章和示例
- 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 。
然而,这些都不是真正令人满意的,我可以说:现在我知道如何在我的代码中应用它了。
让我向您展示我的适应度函数,我猜了几个执行时间。
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
}
您知道任何有示例的链接,以便我可以学习如何计算面向使用的时间复杂度。
或者也许有人可以在这里解释。例如,对于这段代码,我完全不确定:double totalDistance = startpoint.DistanceTo(cF[i][0]); --> (1+1)N ?
或者这样:actual = cF[i][j]; --> (1+1)NN ?
所以一般来说,时间复杂度是: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)
【问题讨论】:
-
我发现这本书非常擅长解释如何计算算法的时间复杂度。 amazon.com/Introduction-Algorithms-3rd-MIT-Press/dp/0262033844
-
这本书好像在我们大学的图书馆里。我会看看它。谢谢你:)
-
拿一本高级数学书,你可能需要它。
标签: c# time-complexity genetic-algorithm