<p>Yes, this can be done through <em>bucketing</em>. The representation in Briggs and Torczon can be used to store not just bits, but values associated with any set bits, by storing them next to the values in the dense array. For $k$-bit values, this leads to a total storage cost of $(2n+1)\lceil \lg n \rceil + kn$.</p>

<p>Now pick $k=\lceil \lg n \rceil$. Each $k$-bit value will represent a set of $\lceil \lg n \rceil$ contiguous bits in the bit array. We thus only need to track $\lceil n/\lceil \lg n \rceil \rceil$ of them with the dense and sparse arrays. This leads to a total cost of $\Theta(n)$ bits of storage, with $\Theta(\lg n)$ initialized bits.</p>

<p>Like the basic solution, this requires $O(1)$ operations on words of length $\Theta(\lg n)$ to set a single bit.</p>

CC BY-SA 3.0
2013-05-31T13:21:28.410Z
2013-05-31T14:42:14.980Z
2013-05-31T14:42:14.980Z
jbapple
<p><a href="http://research.swtch.com/sparse" rel="nofollow">A well-known trick for storing bit vectors using uninitialized memory</a> can allocate a bit vector of size $n$ in which all of the bits are set to $0$ by allocating $(2 n + 1)\lceil \lg n \rceil$ bits of memory and initializing only $\lceil \lg n \rceil$ of them. This representation supports setting and unsetting any bit in constant time.</p>

<p>This dates back to "Alfred Aho, John Hopcroft, and Jeffrey Ullman's 1974 book The Design and Analysis of Computer Algorithms . . . Chapter 2, exercise 2.12", "Jon Bentley's 1986 book Programming Pearls . . .Column 1, exercise 8; exercise 9 in the Second Edition", and <a href="http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.30.7319" rel="nofollow">"Preston Briggs and Linda Torczon's 1993 paper, 'An Efficient Representation for Sparse Sets'"</a>.</p>

<p><a href="https://cs.nyu.edu/~dodis/ps/prefix.pdf" rel="nofollow">Dodis et al.'s "Changing Base without Losing Space"</a> brings the space requirement down slightly to $\lceil (2 n + 1) \lg n \rceil + 1$ bits, though this algorithm requires the precomputation of $\Theta(\lg n)$ constants with $\Theta(\lg n)$ bits each.</p>

<p>How much space can be saved? Is there a representation of bit vectors in which</p>

<ul>
<li>Bits can be set or unset in $O(1)$ time</li>
<li>Initializing a new bit vector of $0$s uses $o(n \lg n)$ bits of uninitialized memory and $O(\lg n)$ initialized memory</li>
</ul>

<ds.data-structures><space-bounded><space-time-tradeoff><space-complexity>
Storing a bit vector in uninitialized memory and minimal space
jbapple
# Storing a bit vector in uninitialized memory and minimal space

(Asked by: username_0 on 2013-05-30)

[A well-known trick for storing bit vectors using uninitialized memory](http://research.swtch.com/sparse) can allocate a bit vector of size $n$ in which all of the bits are set to $0$ by allocating $(2 n + 1)\lceil \lg n \rceil$ bits of memory and initializing only $\lceil \lg n \rceil$ of them. This representation supports setting and unsetting any bit in constant time.

This dates back to "Alfred Aho, John Hopcroft, and Jeffrey Ullman's 1974 book The Design and Analysis of Computer Algorithms . . . Chapter 2, exercise 2.12", "Jon Bentley's 1986 book Programming Pearls . . .Column 1, exercise 8; exercise 9 in the Second Edition", and ["Preston Briggs and Linda Torczon's 1993 paper, 'An Efficient Representation for Sparse Sets'"](http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.30.7319).

[Dodis et al.'s "Changing Base without Losing Space"](https://cs.nyu.edu/~dodis/ps/prefix.pdf) brings the space requirement down slightly to $\lceil (2 n + 1) \lg n \rceil + 1$ bits, though this algorithm requires the precomputation of $\Theta(\lg n)$ constants with $\Theta(\lg n)$ bits each.

How much space can be saved? Is there a representation of bit vectors in which

* Bits can be set or unset in $O(1)$ time
* Initializing a new bit vector of $0$s uses $o(n \lg n)$ bits of uninitialized memory and $O(\lg n)$ initialized memory

**Tags:** ds.data-structures, space-bounded, space-time-tradeoff, space-complexity

---

## 1 answers

### Answer by username_0 on 2013-05-31. Score: 2 | Accepted answer

Yes, this can be done through *bucketing*. The representation in Briggs and Torczon can be used to store not just bits, but values associated with any set bits, by storing them next to the values in the dense array. For $k$-bit values, this leads to a total storage cost of $(2n+1)\lceil \lg n \rceil + kn$.

Now pick $k=\lceil \lg n \rceil$. Each $k$-bit value will represent a set of $\lceil \lg n \rceil$ contiguous bits in the bit array. We thus only need to track $\lceil n/\lceil \lg n \rceil \rceil$ of them with the dense and sparse arrays. This leads to a total cost of $\Theta(n)$ bits of storage, with $\Theta(\lg n)$ initialized bits.

Like the basic solution, this requires $O(1)$ operations on words of length $\Theta(\lg n)$ to set a single bit.


---

