Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does deque have an amortized constant Time Complexity

Tags:

c++

c++03

deque

I read here from the accepted answer that a std::deque has the following characteristic

1- Random access - constant O(1)
2- Insertion or removal of elements at the end or beginning - amortized constant O(1)
3- Insertion or removal of elements - linear O(n)

My question is about point 2. How can a deque have an amortized constant insertion at the end or beginning?

I understand that a std::vector has an amortized constant time complexity for insertions at the end. This is because a vector is continguous and is a dynamic array. So when it runs out of memory for a push_back at the end, it would allocate a whole new block of memory, copy existing items from the old location to the new location and then delete items from the old location. This operation I understand is amortized constant. How does this apply to a deque ? How can insertions at the top and bottom of a deque be amortized constant. I was under the impression that it was supposed to be constant O(1). I know that a deque is composed of memory chunks.

like image 631
Rajeshwar Avatar asked Jan 20 '15 16:01

Rajeshwar


2 Answers

The usual implementation of a deque is basically a vector of pointers to fixed-sized nodes.

Allocating the fixed-size node clearly has constant complexity, so that's pretty easy to handle--you just amortize the cost of allocating a single node across the number of items in that node to get a constant complexity for each.

The vector of pointers part is what's (marginally) more interesting. When we allocate enough of the fixed-size nodes that the vector of pointers is full, we need to increase the size of the vector. Like std::vector, we need to copy its contents to the newly allocated vector, so its growth must follow a geometric (rather than arithmetic) progression. This means that we have more pointers to copy we do the copying less and less frequently, so the total time devoted to copying pointers remains constant.

As a side note: the "vector" part is normally treated as a circular buffer, so if you're using your deque as a queue, constantly adding to one end and removing from the other does not result in re-allocating the vector--it just means moving the head and tail pointers that keep track of which of the pointers are "active" at a given time.

like image 163
Jerry Coffin Avatar answered Oct 10 '22 00:10

Jerry Coffin


The (profane) answer lies in containers.requirements.general, 23.2.1/2:

All of the complexity requirements in this Clause are stated solely in terms of the number of operations on the contained objects.

Reallocating the array of pointers is hence not covered by the complexity guarantee of the standard and may take arbitrarily long. As mentioned before, it likely adds an amortized constant overhead to each push_front()/push_back() call (or an equivalent modifier) in any "sane" implementation. I would not recommend using deque in RT-critical code though. Typically, in an RT scenario, you don't want to have unbounded queues or stacks (which in C++ by default use deque as the underlying container) anyway, neither memory allocations that could fail, so you will be most likely using a preallocated ring buffer (e.g. Boost's circular_buffer) instead.

like image 38
Arne Vogel Avatar answered Oct 09 '22 23:10

Arne Vogel