Answer the question
In order to leave comments, you need to log in
How to find out the approximate complexity of an algorithm given its execution time?
There is a function f(x)=y, where x is the number of elements that the algorithm accepts, y is the execution time of the algorithm.
Having several such points (x, y), you need to approximately determine the complexity of the algorithm.
In other words, having these points on the graph, it is necessary to determine among such functions as {n,n^2,nlogn,n!,...} the one that will be closest to these points.
Answer the question
In order to leave comments, you need to log in
The modulus difference of the ratios of Y to the known list of complexities {n,n^2,nlogn,n!,...}.
The closest fraction tending to one, And one of their mutual differences (distances) - tending to zero, and will be the desired value.
Abstracting from the algorithms and their complexity in Big-O notation, the problem is reduced to calculating the differences between the values of a given function and candidate functions on some set of control points.
Take some set of values [x0, x1, ... xN]
.
Get "correct" y_true = [f(x0), f(x1), ... f(xN)]
For each candidate function from the same x
get their "predictions" y_pred = [Fn(x0), Fn(x1), ... Fn(xN)]
Determine which of the functions is the least "mistaken". To do this, add the squares of the differences produced y_true - y_pred
by each of the functions on the set of input values x
. The smallest sum will indicate the most likely candidate.
For example,
x = [1, 2, 4, 8, 16, 32, 64]
f(x) = [y0, y1, y2, y3, y4, y5, y6]
// n
[1, 2, 4, 8, 16, 32, 64]
sum = (y0 - 1)^2 + (y1 - 2)^2 + (y2 - 4)^2 + ... + (y6 - 64)^2 = sum0
// n^2
[1, 4, 16, 64, 256, 1024, 4096]
sum = (y0 - 1)^2 + (y1 - 4)^2 + (y2 - 16)^2 + ... + (y6 - 4096)^2 = sum1
// так же посчитать суммы ошибок для остальных функций:
// n * log(n), n!
sumN
will indicate the function closest to the desired one.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question