MATLAB Sparse Matrix Multiplication Time Complexity

12 views (last 30 days)
Does anybody know what is the time complexity of multiplying two sparse matrices in terms of Big O? I searched a lot about this and it seems that MATLAB is using SuiteSparse's SSMULT package for this purpose but nowhere has mentioned anything about the time complexity.

Answers (1)

John D'Errico
John D'Errico on 25 Jan 2017
Edited: John D'Errico on 25 Jan 2017
I don't think you can get a valid time order estimate for this, since the time will depend on the sparsity patterns.
For example, it is easy to construct two matrices, each only 50% sparse, yet a matrix multiply yields a result that is all zero, with NO elements multiplied.
A= rand(10);
A(:,1:2:end) = 0;
A = sparse(A);
B = rand(10);
B(2:2:end,:) = 0;
B = sparse(B);
A*B
ans =
All zero sparse: 10×10
The time required to multiply A and B should be very low, since there are no hits at all. No element multiplies will be required.
timeit(@() A*B)
ans =
0.0014069
Af = full(A);
Bf = full(B);
timeit(@() Af*Bf)
ans =
0.051652
So the time required for the sparse matrix multiplies is really only the time required to find out that no multiplies are needed. Whereas, if the matrices were actually random sparse, with the same density, the multiply will be hugely more costly.
A = sprand(1000,1000,0.5);
B = sprand(1000,1000,0.5);
timeit(@() A*B)
ans =
0.34568
The point is, you need to know more than just the size of the matrices. An order estimate will be useless here, since what really matters is how the matrices relate to each other.
Next, MATLAB tried to perform bigger operations using multiple threads, where the software sees it will be of some value. While I'm not sure if the sparse codes use this capability yet, this will totally screw up any order estimates.
I think these factors are why you don't see any time estimates, since they would be invalid for anyone to use. If your matrices have specific patterns, you would be best off doing tests yourself. Use timeit to see the time required as I did, NOT tic/toc.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!