algorithmic modeling for Rhino
Hello everyone,
here I am again with another question :')
After some research I am most likely done with figuring out the time complexity of my algorithm. However there are a few points where I am not sure about my decision.
Let's take a look at the method <createPopulation>:
public static List<List<Point3d>> createPopulation(List<Point3d> cP, int populationCount)
{
List<List<Point3d>> Population = new List<List<Point3d>>(); // 1
for(int i = 0; i < populationCount; i++) //2
{
List<Point3d> individual = cP.ToList(); // 3
Population.Add(individual); // 4
System.Security.Cryptography.RNGCryptoServiceProvider provider = new System.Security.Cryptography.RNGCryptoServiceProvider();
int n = Population[i].Count;
while (n > 0)
{
byte[] box = new byte[1];
do provider.GetBytes(box); // 5
while (!(box[0] < n * (Byte.MaxValue / n)));
int k = (box[0] % n);
n--;
Point3d value = Population[i][k];
Population[i][k] = Population[i][n];
Population[i][n] = value;
}
}
Tags:
You need to forget about details of your code and one-off things like declarations (more on those later btw.). Big-O runtime is about how much longer your algorithm will take if you increase the amount of data it has to operate on. So only those aspects of the algorithm that differ in these cases are relevant.
For example if your algorithm always does exactly the same thing (let's say all it does is measure the size of an array and display it on screen) will be O(1), because it doesn't matter if you run it on an array containing 10 or 1000000 items. Measuring the size of an array is a constant-time operation:
Print(string.Format("Array contains: {0} element(s)", data.Length);
However if your algorithm works on not on arrays but on linked-lists, then it becomes an O(N) operation because counting all the elements in a linked list means you have to iterate over all of them. And the longer the list, the more iterations you need. In fact the number of iterations is exactly the same as the number of items. (ps. if you'd be using the System.Collections.Generic.LinkedList<T> class then it's still O(1), because apparently that particular implementation of linked lists caches the count and keeps it up to date.)
If you have a loop that runs for each item, and then inside that loop there is another loop that also runs for each item, then your complexity becomes O(N²). Or, in a similar case if your algorithm consumes two collections (N and M) and iterates over all items in N, and then inside that loop it iterates over all items in M, the complexity is O(N×M).
The case can be made that only the most severe complexity is relevant enough to report. For example if you have an algorithm that comprises of three steps, the first of which is O(log(N)), the second is O(N²) and the third is O(3ⁿ), then technically the total complexity would be O(log(N) + N² + 3ⁿ), however the first two parts are utterly insignificant compared to the third and therefore can be omitted entirely. Consider for example increasing the input size from 10 to 20 elements:
As you can see the increase of the complexity is almost entirely due to the O(3ⁿ) portion, so much so that there's almost no point in mentioning the other two.
Now, your specific questions:
There's no such thing as O(13). O(1) is a stand-in for constant time, no matter how long it takes. O(1) tells you that it always takes the same amount of time, but it doesn't tell you how much time. Similarly, O(2N) is not meaningful, because it merely linearly scales the actual measured runtime, which is not what big-O notation is about.
Initialising new empty collections is almost always an O(1) operation. It's only when you initialise a class and populate it through the constructor that complexity may be worse. For example new HashSet<int>(largeIntegerCollection) or new SortedList<string, int>(largeDictionary). Note that the comments for the latter constructor on MSDN specifica....
You mean I should do something like: List<Point3d> test = new List<Point3d>(N); if I know I am going to have N points in the list.
Yup.
So <calculateFitness> has runtime N^2 and <List.ToList()> runtime N. What is then the total runtime?
Either O(N+N²) or just O(N²) depending on opinion. I'd say the latter.
Welcome to
Grasshopper
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
© 2024 Created by Scott Davidson. Powered by