3
$\begingroup$

I have a table that contains only bits. I would like to be able to do the following two queries:

  • SET any bit to 0 or 1;
  • GET the number of bits that are set to 1 from the beginning of the table up to a specifique index i.

Is there a data structure that is more efficient than a binary indexed tree, i.e. a Fenwick tree? A binary indexed tree performs both operations in $O(\log n)$ time, where $n$ is the number of elements.

For practical info, my tables are really large, containing up to millions of elements.

Juho
22.9k7 gold badges63 silver badges118 bronze badges
asked Sep 18, 2014 at 8:57
$\endgroup$
6
  • 1
    $\begingroup$ For a practical implementation, one could experiment with a bitvector, or maybe a (dynamic) array of say 64-bit words. There are very fast bit counting methods available, see e.g. SSE4 popcnt. Of course theoretically, the GET operation requires linear time. Real performance will however depend on size of $n$. $\endgroup$ Commented Sep 18, 2014 at 9:17
  • $\begingroup$ @Juho, I am not sure you can play with the index i using popcnt. I will add a precision in the Question a precision about n $\endgroup$ Commented Sep 18, 2014 at 9:20
  • 1
    $\begingroup$ Not directly. Depending on what $i$ is, you can first popcount enough words, and then start counting bits one-by-one when you hit the right word, if you see what I mean. And also, if you want, you can increase the bit level parallelism by counting in parallel with AVX2 vector registers. $\endgroup$ Commented Sep 18, 2014 at 9:21
  • $\begingroup$ Yes I understood what you mean. But this won't be good for a small $n$ as the number of words will be small. And it won't be efficient of large $n$ since it will only add a factor of 1/64 in front of the linear complexity. However it may be a solution for medium range tables... $\endgroup$ Commented Sep 18, 2014 at 9:24
  • 2
    $\begingroup$ What is your space complexity constraint? $\endgroup$ Commented Sep 18, 2014 at 9:25

1 Answer 1

2
$\begingroup$

If one of the operations is much more common, you could use an array of bits resp. of counts, so the common operation can be done in $O(1)$ at the cost of the other operation taking $O(n)$.

If you can't make such an assumption, then I don't think there is anything better than the $O(\log n)$ of the BIT.

answered Sep 18, 2014 at 9:31
$\endgroup$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.