# 2.6: RootishArrayStack - A Space-Efficient Array Stack


One of the drawbacks of all previous data structures in this chapter is that, because they store their data in one or two arrays and they avoid resizing these arrays too often, the arrays frequently are not very full. For example, immediately after a $$\mathtt{resize()}$$ operation on an ArrayStack, the backing array $$\mathtt{a}$$ is only half full. Even worse, there are times when only one third of $$\mathtt{a}$$ contains data.

In this section, we discuss the RootishArrayStack data structure, that addresses the problem of wasted space. The RootishArrayStack stores $$\mathtt{n}$$ elements using $$O(\sqrt{\mathtt{n}})$$ arrays. In these arrays, at most $$O(\sqrt{\mathtt{n}})$$ array locations are unused at any time. All remaining array locations are used to store data. Therefore, these data structures waste at most $$O(\sqrt{\mathtt{n}})$$ space when storing $$\mathtt{n}$$ elements.

A RootishArrayStack stores its elements in a list of $$\mathtt{r}$$ arrays called blocks that are numbered $$0,1,\ldots,\mathtt{r}-1$$. See Figure $$\PageIndex{1}$$. Block $$b$$ contains $$b+1$$ elements. Therefore, all $$\mathtt{r}$$ blocks contain a total of

$1+ 2+ 3+\cdots +\mathtt{r} = \mathtt{r}(\mathtt{r}+1)/2 \nonumber$

elements. The above formula can be obtained as shown in Figure $$\PageIndex{2}$$.

    List<T[]> blocks;
int n;


As we might expect, the elements of the list are laid out in order within the blocks. The list element with index 0 is stored in block 0, elements with list indices 1 and 2 are stored in block 1, elements with list indices 3, 4, and 5 are stored in block 2, and so on. The main problem we have to address is that of determining, given an index $$\mathtt{i}$$, which block contains $$\mathtt{i}$$ as well as the index corresponding to $$\mathtt{i}$$ within that block.

Determining the index of $$\mathtt{i}$$ within its block turns out to be easy. If index $$\mathtt{i}$$ is in block $$\mathtt{b}$$, then the number of elements in blocks $$0,\ldots,\mathtt{b}-1$$ is $$\mathtt{b}(\mathtt{b}+1)/2$$. Therefore, $$\mathtt{i}$$ is stored at location

$\mathtt{j} = \mathtt{i} - \mathtt{b}(\mathtt{b}+1)/2 \nonumber$

within block $$\mathtt{b}$$. Somewhat more challenging is the problem of determining the value of $$\mathtt{b}$$. The number of elements that have indices less than or equal to $$\mathtt{i}$$ is $$\mathtt{i}+1$$. On the other hand, the number of elements in blocks 0,...,b is $$(\mathtt{b}+1)(\mathtt{b}+2)/2$$. Therefore, $$\mathtt{b}$$ is the smallest integer such that

$(\mathtt{b}+1)(\mathtt{b}+2)/2 \ge \mathtt{i}+1 \enspace . \nonumber$

We can rewrite this equation as

$\mathtt{b}^2 + 3\mathtt{b} - 2\mathtt{i} \ge 0 \enspace . \nonumber$

The corresponding quadratic equation $$\mathtt{b}^2 + 3\mathtt{b} - 2\mathtt{i} = 0$$ has two solutions: $$\mathtt{b}=(-3 + \sqrt{9+8\mathtt{i}}) / 2$$ and $$\mathtt{b}=(-3 - \sqrt{9+8\mathtt{i}}) / 2$$. The second solution makes no sense in our application since it always gives a negative value. Therefore, we obtain the solution $$\mathtt{b} = (-3 + \sqrt{9+8i}) / 2$$. In general, this solution is not an integer, but going back to our inequality, we want the smallest integer $$\mathtt{b}$$ such that $$\mathtt{b} \ge (-3 + \sqrt{9+8i}) / 2$$. This is simply

$\mathtt{b} = \left\lceil(-3 + \sqrt{9+8i}) / 2\right\rceil \enspace . \nonumber$

     int i2b(int i) {
double db = (-3.0 + Math.sqrt(9 + 8*i)) / 2.0;
int b = (int)Math.ceil(db);
return b;
}


With this out of the way, the $$\mathtt{get(i)}$$ and $$\mathtt{set(i,x)}$$ methods are straightforward. We first compute the appropriate block $$\mathtt{b}$$ and the appropriate index $$\mathtt{j}$$ within the block and then perform the appropriate operation:

    T get(int i) {
if (i < 0 || i > n - 1) throw new IndexOutOfBoundsException();
int b = i2b(i);
int j = i - b*(b+1)/2;
return blocks.get(b)[j];
}
T set(int i, T x) {
if (i < 0 || i > n - 1) throw new IndexOutOfBoundsException();
int b = i2b(i);
int j = i - b*(b+1)/2;
T y = blocks.get(b)[j];
blocks.get(b)[j] = x;
return y;
}


If we use any of the data structures in this chapter for representing the $$\mathtt{blocks}$$ list, then $$\mathtt{get(i)}$$ and $$\mathtt{set(i,x)}$$ will each run in constant time.

The $$\mathtt{add(i,x)}$$ method will, by now, look familiar. We first check to see if our data structure is full, by checking if the number of blocks, $$\mathtt{r}$$, is such that $$\mathtt{r}(\mathtt{r}+1)/2 = \mathtt{n}$$. If so, we call $$\mathtt{grow()}$$ to add another block. With this done, we shift elements with indices $$\mathtt{i},\ldots,\mathtt{n}-1$$ to the right by one position to make room for the new element with index $$\mathtt{i}$$:

    void add(int i, T x) {
if (i < 0 || i > n) throw new IndexOutOfBoundsException();
int r = blocks.size();
if (r*(r+1)/2 < n + 1) grow();
n++;
for (int j = n-1; j > i; j--)
set(j, get(j-1));
set(i, x);
}


The $$\mathtt{grow()}$$ method does what we expect. It adds a new block:

    void grow() {
}


Ignoring the cost of the $$\mathtt{grow()}$$ operation, the cost of an $$\mathtt{add(i,x)}$$ operation is dominated by the cost of shifting and is therefore $$O(1+\mathtt{n}-\mathtt{i})$$, just like an ArrayStack.

The $$\mathtt{remove(i)}$$ operation is similar to $$\mathtt{add(i,x)}$$. It shifts the elements with indices $$\mathtt{i}+1,\ldots,\mathtt{n}$$ left by one position and then, if there is more than one empty block, it calls the $$\mathtt{shrink()}$$ method to remove all but one of the unused blocks:

    T remove(int i) {
if (i < 0 || i > n - 1) throw new IndexOutOfBoundsException();
T x = get(i);
for (int j = i; j < n-1; j++)
set(j, get(j+1));
n--;
int r = blocks.size();
if ((r-2)*(r-1)/2 >= n)    shrink();
return x;
}

    void shrink() {
int r = blocks.size();
while (r > 0 && (r-2)*(r-1)/2 >= n) {
blocks.remove(blocks.size()-1);
r--;
}
}


Once again, ignoring the cost of the $$\mathtt{shrink()}$$ operation, the cost of a $$\mathtt{remove(i)}$$ operation is dominated by the cost of shifting and is therefore $$O(\mathtt{n}-\mathtt{i})$$.

## $$\PageIndex{1}$$ Analysis of Growing and Shrinking

The above analysis of $$\mathtt{add(i,x)}$$ and $$\mathtt{remove(i)}$$ does not account for the cost of $$\mathtt{grow()}$$ and $$\mathtt{shrink()}$$. Note that, unlike the $$\texttt{ArrayStack.resize()}$$ operation, $$\mathtt{grow()}$$ and $$\mathtt{shrink()}$$ do not copy any data. They only allocate or free an array of size $$\mathtt{r}$$. In some environments, this takes only constant time, while in others, it may require time proportional to $$\mathtt{r}$$.

We note that, immediately after a call to $$\mathtt{grow()}$$ or $$\mathtt{shrink()}$$, the situation is clear. The final block is completely empty, and all other blocks are completely full. Another call to $$\mathtt{grow()}$$ or $$\mathtt{shrink()}$$ will not happen until at least $$\mathtt{r}-1$$ elements have been added or removed. Therefore, even if $$\mathtt{grow()}$$ and $$\mathtt{shrink()}$$ take $$O(\mathtt{r})$$ time, this cost can be amortized over at least $$\mathtt{r}-1$$ $$\mathtt{add(i,x)}$$ or $$\mathtt{remove(i)}$$ operations, so that the amortized cost of $$\mathtt{grow()}$$ and $$\mathtt{shrink()}$$ is $$O(1)$$ per operation.

## $$\PageIndex{2}$$ Space Usage

Next, we analyze the amount of extra space used by a RootishArrayStack. In particular, we want to count any space used by a RootishArrayStack that is not an array element currently used to hold a list element. We call all such space wasted space.

The $$\mathtt{remove(i)}$$ operation ensures that a RootishArrayStack never has more than two blocks that are not completely full. The number of blocks, $$\mathtt{r}$$, used by a RootishArrayStack that stores $$\mathtt{n}$$ elements therefore satisfies

$(\mathtt{r}-2)(\mathtt{r}-1) \le \mathtt{n} \enspace . \nonumber$

Again, using the quadratic equation on this gives

$\mathtt{r} \le (3+\sqrt{1+4\mathtt{n}})/2 = O(\sqrt{\mathtt{n}}) \enspace . \nonumber$

The last two blocks have sizes $$\mathtt{r}$$ and $$\mathtt{r-1}$$, so the space wasted by these two blocks is at most $$2\mathtt{r}-1 = O(\sqrt{\mathtt{n}})$$. If we store the blocks in (for example) an ArrayStack, then the amount of space wasted by the List that stores those $$\mathtt{r}$$ blocks is also $$O(\mathtt{r})=O(\sqrt{\mathtt{n}})$$. The other space needed for storing $$\mathtt{n}$$ and other accounting information is $$O(1)$$. Therefore, the total amount of wasted space in a RootishArrayStack is $$O(\sqrt{\mathtt{n}})$$.

Next, we argue that this space usage is optimal for any data structure that starts out empty and can support the addition of one item at a time. More precisely, we will show that, at some point during the addition of $$\mathtt{n}$$ items, the data structure is wasting an amount of space at least in $$\sqrt{\mathtt{n}}$$ (though it may be only wasted for a moment).

Suppose we start with an empty data structure and we add $$\mathtt{n}$$ items one at a time. At the end of this process, all $$\mathtt{n}$$ items are stored in the structure and distributed among a collection of $$\mathtt{r}$$ memory blocks. If $$\mathtt{r}\ge \sqrt{\mathtt{n}}$$, then the data structure must be using $$\mathtt{r}$$ pointers (or references) to keep track of these $$\mathtt{r}$$ blocks, and these pointers are wasted space. On the other hand, if $$\mathtt{r} < \sqrt{\mathtt{n}}$$ then, by the pigeonhole principle, some block must have a size of at least $$\mathtt{n}/\mathtt{r} > \sqrt{\mathtt{n}}$$. Consider the moment at which this block was first allocated. Immediately after it was allocated, this block was empty, and was therefore wasting $$\sqrt{\mathtt{n}}$$ space. Therefore, at some point in time during the insertion of $$\mathtt{n}$$ elements, the data structure was wasting $$\sqrt{\mathtt{n}}$$ space.

## $$\PageIndex{3}$$ Summary

The following theorem summarizes our discussion of the RootishArrayStack data structure:

Theorem $$\PageIndex{1}$$.

A RootishArrayStack implements the List interface. Ignoring the cost of calls to $$\mathtt{grow()}$$ and $$\mathtt{shrink()}$$, a RootishArrayStack supports the operations

• $$\mathtt{get(i)}$$ and $$\mathtt{set(i,x)}$$ in $$O(1)$$ time per operation; and
• $$\mathtt{add(i,x)}$$ and $$\mathtt{remove(i)}$$ in $$O(1+\mathtt{n}-\mathtt{i})$$ time per operation.

Furthermore, beginning with an empty RootishArrayStack, any sequence of $$m$$ $$\mathtt{add(i,x)}$$ and $$\mathtt{remove(i)}$$ operations results in a total of $$O(m)$$ time spent during all calls to $$\mathtt{grow()}$$ and $$\mathtt{shrink()}$$.

The space (measured in words)3 used by a RootishArrayStack that stores $$\mathtt{n}$$ elements is $$\mathtt{n} +O(\sqrt{\mathtt{n}})$$.

## $$\PageIndex{4}$$ Computing Square Roots

A reader who has had some exposure to models of computation may notice that the RootishArrayStack, as described above, does not fit into the usual word-RAM model of computation (Section 1.4) because it requires taking square roots. The square root operation is generally not considered a basic operation and is therefore not usually part of the word-RAM model.

In this section, we show that the square root operation can be implemented efficiently. In particular, we show that for any integer $$\mathtt{x}\in\{0,\ldots,\mathtt{n}\}$$, $$\lfloor\sqrt{\mathtt{x}}\rfloor$$ can be computed in constant-time, after $$O(\sqrt{\mathtt{n}})$$ preprocessing that creates two arrays of length $$O(\sqrt{\mathtt{n}})$$. The following lemma shows that we can reduce the problem of computing the square root of $$\mathtt{x}$$ to the square root of a related value $$\mathtt{x'}$$.

Lemma $$\PageIndex{1}$$.

Let $$\mathtt{x}\ge 1$$ and let $$\mathtt{x'}=\mathtt{x}-a$$, where $$0\le a\le\sqrt{\mathtt{x}}$$. Then $$\sqrt{x'} \ge \sqrt{\mathtt{x}}-1$$.

Proof. It suffices to show that

$\sqrt{\mathtt{x}-\sqrt{\mathtt{x}}} \ge \sqrt{\mathtt{x}}-1 \enspace . \nonumber$

Square both sides of this inequality to get

$\mathtt{x}-\sqrt{\mathtt{x}} \ge \mathtt{x}-2\sqrt{\mathtt{x}}+1 \nonumber$

and gather terms to get

$\sqrt{\mathtt{x}} \ge 1 \nonumber$

which is clearly true for any $$\mathtt{x}\ge 1$$.

Start by restricting the problem a little, and assume that $$2^{\mathtt{r}} \le \mathtt{x} < 2^{\mathtt{r}+1}$$, so that $$\lfloor\log \mathtt{x}\rfloor=\mathtt{r}$$, i.e., $$\mathtt{x}$$ is an integer having $$\mathtt{r}+1$$ bits in its binary representation. We can take $$\mathtt{x'}=\mathtt{x} - (\mathtt{x}\bmod 2^{\lfloor r/2\rfloor})$$. Now, $$\mathtt{x'}$$ satisfies the conditions of Lemma $$\PageIndex{1}$$, so $$\sqrt{\mathtt{x}}-\sqrt{\mathtt{x'}} \le 1$$. Furthermore, $$\mathtt{x'}$$ has all of its lower-order $$\lfloor \mathtt{r}/2\rfloor$$ bits equal to 0, so there are only

$2^{\mathtt{r}+1-\lfloor \mathtt{r}/2\rfloor} \le 4\cdot2^{\mathtt{r}/2} \le 4\sqrt{\mathtt{x}} \nonumber$

possible values of $$\mathtt{x'}$$. This means that we can use an array, $$\mathtt{sqrttab}$$, that stores the value of $$\lfloor\sqrt{\mathtt{x'}}\rfloor$$ for each possible value of $$\mathtt{x'}$$. A little more precisely, we have

$\mathtt{sqrttab}[i] = \left\lfloor \sqrt{i 2^{\lfloor \mathtt{r}/2\rfloor}} \right\rfloor \enspace . \nonumber$

In this way, $$\mathtt{sqrttab}[i]$$ is within 2 of $$\sqrt{\mathtt{x}}$$ for all $$\mathtt{x}\in\{i2^{\lfloor r/2\rfloor},\ldots,(i+1)2^{\lfloor r/2\rfloor}-1\}$$. Stated another way, the array entry $$\mathtt{s}=\mathtt{sqrttab}[\mathtt{x}\mathtt{\text{>>}}\lfloor \mathtt{r}/2\rfloor]$$ is either equal to $$\lfloor\sqrt{\mathtt{x}}\rfloor$$, $$\lfloor\sqrt{\mathtt{x}}\rfloor-1$$, or $$\lfloor\sqrt{\mathtt{x}}\rfloor-2$$. From $$\mathtt{s}$$ we can determine the value of $$\lfloor\sqrt{\mathtt{x}}\rfloor$$ by incrementing $$\mathtt{s}$$ until $$(\mathtt{s}+1)^2 > \mathtt{x}$$.

    int sqrt(int x, int r) {
int s = sqrtab[x>>r/2];
while ((s+1)*(s+1) <= x) s++; // executes at most twice
return s;
}


Now, this only works for $$\mathtt{x}\in\{2^{\mathtt{r}},\ldots,2^{\mathtt{r}+1}-1\}$$ and $$\mathtt{sqrttab}$$ is a special table that only works for a particular value of $$\mathtt{r}=\lfloor\log \mathtt{x}\rfloor$$. To overcome this, we could compute $$\lfloor\log \mathtt{n}\rfloor$$ different $$\mathtt{sqrttab}$$ arrays, one for each possible value of $$\lfloor\log \mathtt{x}\rfloor$$. The sizes of these tables form an exponential sequence whose largest value is at most $$4\sqrt{\mathtt{n}}$$, so the total size of all tables is $$O(\sqrt{\mathtt{n}})$$.

However, it turns out that more than one $$\mathtt{sqrttab}$$ array is unnecessary; we only need one $$\mathtt{sqrttab}$$ array for the value $$\mathtt{r}=\lfloor\log\mathtt{n}\rfloor$$. Any value $$\mathtt{x}$$ with $$\log\mathtt{x}=\mathtt{r'}<\mathtt{r}$$ can be upgraded by multiplying $$\mathtt{x}$$ by $$2^{\mathtt{r}-\mathtt{r'}}$$ and using the equation

$\sqrt{2^{\mathtt{r}-\mathtt{r'}}x} = 2^{(\mathtt{r}-\mathtt{r}')/2}\sqrt{\mathtt{x}} \enspace . \nonumber$

The quantity $$2^{\mathtt{r}-\mathtt{r}'}x$$ is in the range $$\{2^{\mathtt{r}},\ldots,2^{\mathtt{r}+1}-1\}$$ so we can look up its square root in $$\mathtt{sqrttab}$$. The following code implements this idea to compute $$\lfloor\sqrt{\mathtt{x}}\rfloor$$ for all non-negative integers $$\mathtt{x}$$ in the range $$\{0,\ldots,2^{30}-1\}$$ using an array, $$\mathtt{sqrttab}$$, of size $$2^{16}$$.

    int sqrt(int x) {
int rp = log(x);
int upgrade = ((r-rp)/2) * 2;
int xp = x << upgrade;  // xp has r or r-1 bits
int s = sqrtab[xp>>(r/2)] >> (upgrade/2);
while ((s+1)*(s+1) <= x) s++;  // executes at most twice
return s;
}


Something we have taken for granted thus far is the question of how to compute $$\mathtt{r}'=\lfloor\log\mathtt{x}\rfloor$$. Again, this is a problem that can be solved with an array, $$\mathtt{logtab}$$, of size $$2^{\mathtt{r}/2}$$. In this case, the code is particularly simple, since $$\lfloor\log \mathtt{x}\rfloor$$ is just the index of the most significant 1 bit in the binary representation of $$\mathtt{x}$$. This means that, for $$\mathtt{x}>2^{\mathtt{r}/2}$$, we can right-shift the bits of $$\mathtt{x}$$ by $$\mathtt{r}/2$$ positions before using it as an index into $$\mathtt{logtab}$$. The following code does this using an array $$\mathtt{logtab}$$ of size $$2^{16}$$ to compute $$\lfloor\log \mathtt{x}\rfloor$$ for all $$\mathtt{x}$$ in the range $$\{1,\ldots,2^{32}-1\}$$.

    int log(int x) {
if (x >= halfint)
return 16 + logtab[x>>>16];
return logtab[x];
}


Finally, for completeness, we include the following code that initializes $$\mathtt{logtab}$$ and $$\mathtt{sqrttab}$$:

    void inittabs() {
sqrtab = new int[1<<(r/2)];
logtab = new int[1<<(r/2)];
for (int d = 0; d < r/2; d++)
Arrays.fill(logtab, 1<<d, 2<<d, d);
int s = 1<<(r/4);                    // sqrt(2^(r/2))
for (int i = 0; i < 1<<(r/2); i++) {
if ((s+1)*(s+1) <= i << (r/2)) s++; // sqrt increases
sqrtab[i] = s;
}
}


To summarize, the computations done by the $$\mathtt{i2b(i)}$$ method can be implemented in constant time on the word-RAM using $$O(\sqrt{n})$$ extra memory to store the $$\mathtt{sqrttab}$$ and $$\mathtt{logtab}$$ arrays. These arrays can be rebuilt when $$\mathtt{n}$$ increases or decreases by a factor of two, and the cost of this rebuilding can be amortized over the number of $$\mathtt{add(i,x)}$$ and $$\mathtt{remove(i)}$$ operations that caused the change in $$\mathtt{n}$$ in the same way that the cost of $$\mathtt{resize()}$$ is analyzed in the ArrayStack implementation.

#### Footnotes

3Recall Section 1.4 for a discussion of how memory is measured.

This page titled 2.6: RootishArrayStack - A Space-Efficient Array Stack is shared under a CC BY license and was authored, remixed, and/or curated by Pat Morin (Athabasca University Press) .