Question 1)
I have a large sparse vector of doubles in c++, I need to efficiently parse out the indices of the non zero elements from the vector. I can obviously loop over the length and do it, any better way to do it?
Question 1)
I have a large sparse vector of doubles in c++, I need to efficiently parse out the indices of the non zero elements from the vector. I can obviously loop over the length and do it, any better way to do it?
Unless you have some special knowledge of the makeup of the vector of doubles, (for example, it's sorted), a loop over its entirety is the most efficient you're gonna get.
Of course, a change in structure as suggested by eladidan is probably something you should consider.
I have a large sparse vector of doubles in c++, I need to efficiently parse out the indices of the non zero elements from the vector. I can obviously loop over the length and do it, any better way to do it?
If the vector is truly sparse (n = o(N)
where n
is the number of non-zero elements and N
is the size of the vector), then representing it in an std::map<int,double>
or std::unordered_map<int,double>
is probably best. With std::map
way you get to find an element in O(log(n))
. With std::unordered_map
a find operation takes amortized time of O(1)
. In both cases, the number of non-zero elements is simply the size of the container. Both approaches also take O(n)
space instead of O(N)
.
If you cannot change the representation of your data, you have to examine each element to filter out those that are almost equal to zero. However, this task is embarrassingly parallel, so maybe you can partition the workload to a bunch of threads to at least improve the runtime (although not the complexity).