How to calculate the complexity of an algorithm by the numbers of addition and multiplication?
23 views (last 30 days)
Show older comments
Du Quoc Thanh
on 23 Oct 2022
Commented: Walter Roberson
on 24 Oct 2022
I have the following code of conducting an algorithm, but how can we calculate its time complexity basing on the number of addition or multiplication?
for k = 1:K % resourses
ind = find(F(k,:)==1); % non-zero elements, paths
for m1 = 1:M
for m2 = 1:M
for m3 = 1:M
d = y(k,jj)-(CB(k,m1,ind(1))*h(k,ind(1),jj)+CB(k,m2,ind(2))*h(k,ind(2),jj)+CB(k,m3,ind(3))*h(k,ind(3),jj));
f(m1,m2,m3,k) = -Noise*sum(real(d)^2+imag(d)^2);
end
end
end
end
The result I want to achieve may look something like this:

0 Comments
Accepted Answer
Walter Roberson
on 23 Oct 2022
It is not possible to calculate the time complexity of an algorithm by measuring the number of multiplications and additions that were done. You could do any finite set of measurements on different problem sizes and different inputs, and you could fit a polynomial to the measurements, but Lagrange interpolation tells you that there are an infinite number of different polynomials that exactly fit any finite set of measurements, so you cannot be certain that you have found the right model. Especially if the true model would involve log or exponential processes, as those cannot be fit with a polynomial.
Therefore to find the Complexity of an algorithm you must do a mathematical analysis of the algorithm. And you will need to decide whether you are measuring average case or worst case. For more advanced algorithms you may need to prove theorems to get to the proper complexity.
9 Comments
Bruno Luong
on 24 Oct 2022
@Du Quoc Thanh I don't understand Walter's objection to your question.
Your code is simple so as to be not very complicated to count the number of additions/multiplication needed to carried out. The count gives the formula that you can further simplify to O() polynomial notation (I guess M is the size here, and d is more or less fix). So I don't see a problem.
Walter Roberson
on 24 Oct 2022
Suppose that you have the user create an anonymous function, and suppose you had code that was like
N = 20;
add_count = zeros(N,1);
mult_count = zeros(N,1);
for K = 1 : N
this_matrix = randi([-9 9], K, K);
initialize_counting();
User_Function(this_matrix);
[this_add, this_mult] = operation_counts();
add_count(K) = this_add;
mult_count(I) = this_mult;
end
K = (1:N).^2;
plot(K, [add_count, mult_count]);
Given those counts, can you deduce the complexity order of the user function without examining the function body ??? The answer is NO, not reliably.
You would have measured the counts, and that would not be enough.
Asking whether there is any "initialize_counting()" and "operation_counts()" equivalents in MATLAB is a proper question about MATLAB. The answer to that question is:
- in sufficiently old versions of MATLAB, ending somewhere roughly MATLAB 6.0-ish at least 18 years ago, there was a flops() call that could be used to measure the combined total of additions and multiplications.. or maybe it was an instruction counter that included other items, I no longer remembered. And even if you had it, you would have to ask whether it would count the multiplications and additions needed to form indices of an array or if the code managed to convert those into pointer operations that did not need multiplications -- you need to know what exactly was being measured.
- As MATLAB improved its internals and as CPUs got more complicated, the flops() measurement was removed by Mathworks as no longer being sufficiently accurate
- The File Exchange contribution https://www.mathworks.com/matlabcentral/fileexchange/50608-counting-the-floating-point-operations-flops can be used to mark-up your MATLAB code to make it easier to count the number of additions and multiplications performed during actual execution of a function. It trusts that you use correct mark-up -- it does not count the number of operations actually done by the CPU, it increments according to the rules you give
- On Windows at least, on Intel machines (maybe AMD as well) you can get CPU cycle counts; see https://stackoverflow.com/questions/138932/how-do-i-obtain-cpu-cycle-count-in-win32 and in particular the reference to System.Diagnostics.Stopwatch.GetTimestamp() which you should be able to call. But notice the cautions in that thread about the counts overflowing often. And you need to know, if the operation is multi-threaded, will the count be over all threads? The cycle counter does NOT refine down to multiplications and additions, it counts all instructions. And it is notoriously variable.
- The MATLAB internal routines to do additions and multiplications or more complicated linear algebra, do not count operations, and Mathworks does not provide any equivalent routines that do count operations.
The above list dealing with (attempts) to measure actual operation counts over an execution are all valid MATLAB questions. But, as I demonstrated above, even if you had accurate operation counts for a series of runs of a function, you would not be able to (reliably) figure out what the computational complexity of the unknown function is.
Your code is simple so as to be not very complicated to count the number of additions/multiplication needed to carried out.
If you read code such as
for m1 = 1:M
for m2 = 1:M
for m3 = 1:M
d = y(k,jj)-(CB(k,m1,ind(1))*h(k,ind(1),jj)+CB(k,m2,ind(2))*h(k,ind(2),jj)+CB(k,m3,ind(3))*h(k,ind(3),jj));
and you say "okay, d involves 3 additions and 3 multiplications each time, and we will ignore any cost of generating indices, and we calculate d for each of M m1, each of M m2, each of M m3, so we are calculating d M*M*M = M^3 times, giving us 3*M^3 additions and 3*M^3 multiplications ---
If you do that, then you are not measuring the complexity, you are analyzing the complexity. And analyzing the complexity of an algorithm is outside the scope of MATLAB and so is not a proper question for here. At least not beyond the level of saying "Ah yes, A:B involves (B-A+1) different values so 1:M involves (M-1+1) = M different values.
Is it possible, even in theory, for there to be a program that could take a function handle to a "black box" function, and the program would carefully invoke the function with different inputs, measure the addition and multiplication counts, and work out what the algorithmic complexity of the black box function is? No, in order to be able to calculate that, any such program would have to be able to figure out whether the black box function terminates on all (finite-size) inputs, and Turing famously proved that that is something that cannot reliably be done, even with access to the source code.
If you want to have a go at analyzing computational complexity by measurements of number of actual additions and multiplications over a number of different runs, then I suggest you give a try to analyzing the algorithmic complexity by making measurements of the execution counts of the Collatz Conjecture https://en.wikipedia.org/wiki/Collatz_conjecture
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
