Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is adding to or removing from the middle of a collections.deque slower than lookup there?

This wiki.python.org page on algorithmic complexity of some data structures says the following for a collections.deque object:

A deque (double-ended queue) is represented internally as a doubly linked list. (Well, a list of arrays rather than objects, for greater efficiency.) Both ends are accessible, but even looking at the middle is slow, and adding to or removing from the middle is slower still.

Two questions:

1) Is adding to the middle of a deque even possible? I don't see any method to do so in the API.

2) Why would removing (or adding) be slower than lookup in the middle of a deque? It's a doubly-linked list, so add/remove should be a constant time operation once you've found the object you want to add.

like image 218
Eli Rose Avatar asked Sep 11 '15 18:09

Eli Rose


People also ask

What is collections deque() in Python HackerRank solution?

Howdy readers, today we will solve Collections.deque () in Python HackerRank Solution. A deque is a double-ended queue. It can be used to add or remove elements from both ends. Deques support thread safe, memory efficient appends and pops from either side of the deque with approximately the same O(1) performance in either direction.

What is a deque in Java?

A deque is a double-ended queue. It can be used to add or remove elements from both ends. Deques support thread safe, memory efficient appends and pops from either side of the deque with approximately the same O(1) performance in either direction. Click on the link to learn more about deque () methods.

What is the difference between a list and a deque?

The most important difference between deque and list is that the former allows you to perform efficient append and pop operations on both ends of the sequence. The deque class implements dedicated .popleft () and .appendleft () methods that operate on the left end of the sequence directly:

How to add and remove values to the left end of deque?

The deque class implements dedicated .popleft () and .appendleft () methods that operate on the left end of the sequence directly: Here, you use .popleft () and .appendleft () to remove and add values, respectively, to the left end of numbers. These methods are specific to the design of deque, and you won’t find them in list.


1 Answers

  1. It's possible to delete items using the remove() method or the del keyword. It's not possible to insert items. (The only possible way to insert that wouldn't show up in the API documentation would be slice assignment, and that's invalid on a deque.)
  2. Because, as the description says, it's actually a doubly-linked list of arrays. So inserting or removing things could require elements to be moved from one array to another. (I haven't looked at the implementation, but I know deque uses a stride technique when looking for elements and I assume the size of the arrays used is the same as the stride length, which is 62.) You'd have to shift a lot of memory around in a regular list when deleting items, too, but at least it's all in one chunk and it can be moved efficiently.
like image 77
kindall Avatar answered Nov 15 '22 15:11

kindall