Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serial insertion into sparse matrices should be done in O(1) #227

Closed
vkostyukov opened this issue Dec 7, 2014 · 8 comments
Closed

Serial insertion into sparse matrices should be done in O(1) #227

vkostyukov opened this issue Dec 7, 2014 · 8 comments

Comments

@vkostyukov
Copy link
Owner

If we insert values into CSR matrix row-by-row or into CCS matrix column-by-column, in a serial order, it should take O(1) time. Here is the check that does if for sparse vector at searchForIndex routine:

if (cardinality == 0 || i > indices[cardinality - 1]) {
  return cardinality;
}
@vkostyukov
Copy link
Owner Author

@DWiechert, this might be interesting for you.

@DWiechert
Copy link
Contributor

Would you rather me work on this one instead of #172? I currently have not started on that one so there would be no work lost at this point.

@vkostyukov
Copy link
Owner Author

Oh, sorry! I've forgot you start working on that issue. Please, go ahead with any issue you like!

@DWiechert
Copy link
Contributor

I'll continue with that one (#172) and try to get it done in the next day or so. Then, if this is still open I can come back to it.

@DWiechert
Copy link
Contributor

@vkostyukov, care to elaborate a little more about this issue? Or is it just adding a similar check to CSRMatrix and CCSMatrix?

@vkostyukov
Copy link
Owner Author

The idea is to not do binary search in the array[0...N-1] if we insert the value into the first empty spot in the array. The current version of Matrix.insert does binary search for every call. But we can do it in O(1) if we do serial insertions (indices are always growing).

Small example on sparse vector.

Initially everything is empty:

cardinality = 0 // non-zero elements
indices = [0, 0, 0, 0]
value = [0, 0, 0, 0]

If we write the first element a[5] = 42.0:

cardinality = 1
indices = [5, 0, 0, 0]
value = [42.0, 0, 0, 0]

In case of writing a[10] = 100. We don't need to binary search to find a proper place. Since both of the arrays are sorted between 0 and cardinality. We can check the right-most element's index, and if it's less the our index, we can just write our element right after the right-most element. No binary search here.

@DWiechert
Copy link
Contributor

@vkostyukov, I won't be able to focus on this issue until the new year. I just thought I'd let you know in case you wanted to pass it off to someone else in the mean time.

@vkostyukov
Copy link
Owner Author

No worries @DWiechert. I will handle this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants