Dynamic Arrays: Facts and figures

The one problem we face with arrays is that its size is fixed. We can run out of space once the array has been filled up. This is pretty bad in production as we cannot really estimate the size of the array beforehand in many situations. Of course, we could allocate a huge array but we’d be wasting resources at the end of the day if they’re not populated. Sounds like a chicken and an egg problem.. How would we want to solve it?

The answer is dynamic array allocation. The concept is pretty simple. While instantiating a dynamic array, initially allocate a considerable size N (for example, 10 as done in Java). Then once we run out of space we double its size from N to 2 \cdot N. To delve a bit deeper into it, we first allocate an array of size 2 \cdot N, then copy the N elements over to the newly allocated array. Then we give up the older array’s to the memory management system after making the array reference point to the new one. The figure below illustrates the state of the array before, during and after re-sizing the array.

Figure: Dynamic Array Re-sizing (click image to enlarge)

It can be noted that the same procedure holds good while we have too much space with us, once the array is only filled to N/2, it gets re-sized to this number, instead of holding up space for all the N elements.

Now let’s get into the interesting part, algorithmic complexity. For simplicity, let’s assume that the initial size of the array is 1.

The first question to be asked here is how many times do we have to double the array to reach a final capacity of N. Before thinking in abstract terms, let us talk some numbers. Suppose if the capacity of my dynamic array is 16 right now, how many times have I doubled the capacity of my array? We’ve doubled 4 times (1 to 2, 2 to 4, 4 to 8, 8 to 16). Thus we can conlude that to reach the capacity of N we must have doubled log_2 N times.

The question to be asked next is how many times have we re-copied elements into a new array the original array is currently at capacity of N and we’ve just re-sized to a capacity of 2 \cdot N? Half of the elements of the original array has moved once, the quarter of the original array have moved twice, and so on. When we formulate this as a summation S we get:

S = \sum_{i=1}^{\log N}{i \cdot n / 2^i} = n \cdot \sum_{i=1}^{\log N}{i / 2^i} < N \cdot \sum_{i=1}^{\log \infty}{i / 2^i} = 2 \cdot N

Notice that the we’re talking about the total number of movements (re-copies) of elements rather than how many times an element moves. This is useful because, when we look at N insertions as a whole, we’ve just spent 2 \cdot N amount of work. This is really cool because, creating and managing a dynamic array framework is still guarenteed to be O\left(N\right). Such a guarantee/cost analysis is formally known as an amortized guarantee/analysis depending on the context.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s