I have the following sparse matrix that contains O(N)
elements
boost::numeric::ublas::compressed_matrix<int> adjacency (N, N);
I could write a brute force double loop to go over all the entries in O(N^2)
time like below, but this is going to be too slow.
for(int i=0; i<N; ++i)
for(int j=0; j<N; ++j)
std::cout << adjacency(i,j) std::endl;
How can I loop over only the non-zero entries in O(N)
time? For each non-zero element I would like to have access to its value, and the indexes i,j
.
You can find the answer in this FAQ: How to iterate over all non zero elements?
In your case it would be: