[go: nahoru, domu]

Jump to content

Double-ended queue: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
m changing appendleft to insert for Python (see Raymond's answer - https://stackoverflow.com/questions/8537916/whats-the-idiomatic-syntax-for-prepending-to-a-short-python-list)
Use IPA and respell to convey pronunciation
 
(44 intermediate revisions by 36 users not shown)
Line 1: Line 1:
{{Short description|Abstract data type}}
{{redirect-distinguish2|Deque|dequeueing, a [[queue (abstract data type)|queue]] operation}}
{{redirect-distinguish2|Deque|dequeueing, a [[queue (abstract data type)|queue]] operation}}
{{Distinguish|Double-ended priority queue}}
{{Distinguish|Double-ended priority queue}}
{{tooshort|date=April 2022}}
In [[computer science]], a '''double-ended queue''' (abbreviated to '''deque''', pronounced ''deck''<ref>Jesse Liberty; Siddhartha Rao; Bradley Jones. ''C++ in One Hour a Day, Sams Teach Yourself'', Sixth Edition. Sams Publishing, 2009. {{ISBN|0-672-32941-7}}. Lesson 18: STL Dynamic Array Classes, pp. 486.</ref>) is an [[abstract data type]] that generalizes a [[queue (data structure)|queue]], for which elements can be added to or removed from either the front (head) or back (tail).<ref>[[Donald Knuth]]. ''[[The Art of Computer Programming]]'', Volume 1: ''Fundamental Algorithms'', Third Edition. Addison-Wesley, 1997. {{ISBN|0-201-89683-4}}. Section 2.2.1: Stacks, Queues, and Deques, pp. 238&ndash;243.</ref> It is also often called a '''head-tail linked list''', though properly this refers to a specific [[data structure]] ''[[#Implementations|implementation]]'' of a deque (see below).
{{technical|date=April 2022}}

In [[computer science]], a '''double-ended queue''' (abbreviated to '''deque''', {{IPAc-en|d|ɛ|k}} {{Respell|DEK}}<ref>Jesse Liberty; Siddhartha Rao; Bradley Jones. ''C++ in One Hour a Day, Sams Teach Yourself'', Sixth Edition. Sams Publishing, 2009. {{ISBN|0-672-32941-7}}. Lesson 18: STL Dynamic Array Classes, pp. 486.</ref>) is an [[abstract data type]] that generalizes a [[queue (data structure)|queue]], for which elements can be added to or removed from either the front (head) or back (tail).<ref>[[Donald Knuth]]. ''[[The Art of Computer Programming]]'', Volume 1: ''Fundamental Algorithms'', Third Edition. Addison-Wesley, 1997. {{ISBN|0-201-89683-4}}. Section 2.2.1: Stacks, Queues, and Deques, pp. 238&ndash;243.</ref> It is also often called a '''head-tail linked list''', though properly this refers to a specific [[data structure]] ''[[#Implementations|implementation]]'' of a deque (see below).


==Naming conventions==
==Naming conventions==
Line 15: Line 19:


== Operations ==
== Operations ==
[[File:UML deque.svg|right|UML class diagram of a double-ended queue]]
The basic operations on a deque are ''enqueue'' and ''dequeue'' on either end. Also generally implemented are ''[[Peek (data type operation)|peek]]'' operations, which return the value at that end without dequeuing it.
The basic operations on a deque are ''enqueue'' and ''dequeue'' on either end. Also generally implemented are ''[[Peek (data type operation)|peek]]'' operations, which return the value at that end without dequeuing it.


Line 26: Line 31:
|<code>push_back</code>||<code>push</code>
|<code>push_back</code>||<code>push</code>
|-
|-
| insert element at front || push, cons || <code>Prepend</code> || <code>push_front</code> || <code>offerFirst</code> || <code>unshift</code> || <code>array_unshift</code> || <code>insert</code> || <code>unshift</code>
| insert element at front || push, cons || <code>Prepend</code> || <code>push_front</code> || <code>offerFirst</code> || <code>unshift</code> || <code>array_unshift</code> || <code>appendleft</code> || <code>unshift</code>
|<code>push_front</code>||<code>unshift</code>
|<code>push_front</code>||<code>unshift</code>
|-
|-
Line 37: Line 42:
| examine last element || peek
| examine last element || peek
| <code>Last_Element</code> || <code>back</code> || <code>peekLast</code> || <code>$array[-1]</code> || <code>end</code> || <code><obj>[-1]</code> || <code>last</code>
| <code>Last_Element</code> || <code>back</code> || <code>peekLast</code> || <code>$array[-1]</code> || <code>end</code> || <code><obj>[-1]</code> || <code>last</code>
|<code>back</code>||<code><obj>[<obj>.length - 1]</code>
|<code>back</code>||<code><obj>.at(-1)</code>
|-
|-
| examine first element || || <code>First_Element</code> || <code>front</code> || <code>peekFirst</code> || <code>$array[0]</code> || <code>reset</code> || <code><obj>[0]</code> || <code>first</code>
| examine first element || || <code>First_Element</code> || <code>front</code> || <code>peekFirst</code> || <code>$array[0]</code> || <code>reset</code> || <code><obj>[0]</code> || <code>first</code>
Line 52: Line 57:


===Purely functional implementation===
===Purely functional implementation===
Double-ended queues can also be implemented as a [[purely functional data structure]].<ref name="functional">https://www.cs.cmu.edu/~rwh/theses/okasaki.pdf C. Okasaki, "Purely Functional Data Structures", September 1996</ref> Two versions of the implementation exist. The first one, called '''real-time deque'', is presented below. It allows the queue to be [[persistent data structure|persistent]] with operations in <math>O(1)</math> worst-case time, but requires [[lazy evaluation|lazy]] lists with [[memoization]]. The second one, with no lazy lists nor memoization is presented at the end of the sections. Its [[amortized analysis|amortized]] time is <math>O(1)</math> if the persistency is not used; but the worst-time complexity of an operation is <math>O(n)</math> where <math>n</math> is the number of elements in the double-ended queue.
Double-ended queues can also be implemented as a [[purely functional data structure]].<ref name="functional">{{cite thesis |url=https://www.cs.cmu.edu/~rwh/students/okasaki.pdf |first=Chris |last=Okasaki |title=Purely Functional Data Structures |type=Ph.D. thesis |date=September 1996 |publisher=Carnegie Mellon University |id=CMU-CS-96-177}}</ref>{{rp|115}} Two versions of the implementation exist. The first one, called '''real-time deque'', is presented below. It allows the queue to be [[persistent data structure|persistent]] with operations in {{math|''O''(1)}} worst-case time, but requires [[lazy evaluation|lazy]] lists with [[memoization]]. The second one, with no lazy lists nor memoization is presented at the end of the sections. Its [[amortized analysis|amortized]] time is {{math|''O''(1)}} if the persistency is not used; but the worst-time complexity of an operation is {{math|''O''(''n'')}} where {{mvar|n}} is the number of elements in the double-ended queue.


Let us recall that, for a list <code>l</code>, <code>|l|</code> denotes its length, that <code>NIL</code> represents an empty list and <code>CONS(h, t)</code> represents the list whose head is <code>h</code> and whose tail is <code>t</code>. The functions <code>drop(i, l)</code> and <code>take(i, l)</code> return the list <code>l</code> without its first <code>i</code> elements, and the first <code>i</code> elements of <code>l</code>, respectively. Or, if <code>|l| < i</code>, they return the empty list and <code>l</code> respectively.
Let us recall that, for a list <code>l</code>, <code>|l|</code> denotes its length, that <code>NIL</code> represents an empty list and <code>CONS(h, t)</code> represents the list whose head is <code>h</code> and whose tail is <code>t</code>. The functions <code>drop(i, l)</code> and <code>take(i, l)</code> return the list <code>l</code> without its first <code>i</code> elements, and the first <code>i</code> elements of <code>l</code>, respectively. Or, if <code>|l| < i</code>, they return the empty list and <code>l</code> respectively.


==== Real-time deques via lazy rebuilding and scheduling ====
A double-ended queue is represented as a sextuple <code>lenf, f, sf, lenr, r, sr</code> where <code>f</code> is a [[linked list]] which contains the front of the queue of length <code>lenf</code>. Similarly, <code>r</code> is a linked list which represents the reverse of the rear of the queue, of length <code>lenr</code>. Furthermore, it is assured that <code>|f| <= 2|r|+1</code> and <code>|r| <= 2|f|+1</code> - intuitively, it means that neither the front nor the rear contains more than a third of the list plus one element. Finally, <code>sf</code> and <code>sr</code> are tails of <code>f</code> and of <code>r</code>, they allow to schedule the moment where some lazy operations are forced. Note that, when a double-ended queue contains <code>n</code> elements in the front list and <code>n</code> elements in the rear list, then the inequality invariant remains satisfied after <code>i</code> insertions and <code>d</code> deletions when <code>(i+d)/2 <= n</code>. That is, at most <code>n/2</code> operations can happen between each rebalancing.
A double-ended queue is represented as a sextuple <code>(len_front, front, tail_front, len_rear, rear, tail_rear)</code> where <code>front</code> is a [[linked list]] which contains the front of the queue of length <code>len_front</code>. Similarly, <code>rear</code> is a linked list which represents the reverse of the rear of the queue, of length <code>len_rear</code>. Furthermore, it is assured that <code>|front| 2|rear|+1</code> and <code>|rear| 2|front|+1</code> - intuitively, it means that both the front and the rear contains between a third minus one and two thirds plus one of the elements. Finally, <code>tail_front</code> and <code>tail_rear</code> are tails of <code>front</code> and of <code>rear</code>, they allow scheduling the moment where some lazy operations are forced. Note that, when a double-ended queue contains <code>n</code> elements in the front list and <code>n</code> elements in the rear list, then the inequality invariant remains satisfied after <code>i</code> insertions and <code>d</code> deletions when <code>(i+d) &leq; n/2</code>. That is, at most <code>n/2</code> operations can happen between each rebalancing.


Let us first give an implementation of the various operations that affect the front of the deque - cons, head and tail. Those implementations do not necessarily respect the invariant. In a second time we'll explain how to modify a deque which does not satisfy the invariant into one which satisfies it. However, they use the invariant, in that if the front is empty then the rear has at most one element. The operations affecting the rear of the list are defined similarly by symmetry.
Intuitively, inserting an element <code>x</code> in front of the double-ended queue <code>lenf, f, sf, lenr, sr</code> leads almost to the double-ended queue <code>lenf+1, CONS(x, f), drop(2, sf), lenr, r, drop(2, sr)</code>, the head and the tail of the double-ended queue <code>lenf, CONS(x, f), sf, lenr, r, sr</code> are <code>x</code> and almost <code>lenf-1, f, drop(2, sf), lenr, r, drop(2, sr)</code> respectively, and the head and the tail of <code>lenf, NIL, NIL, lenr, CONS(x, NIL), drop(2, sr)</code> are <code>x</code> and <code>0, NIL, NIL, 0, NIL, NIL</code> respectively. The function to insert an element in the rear, or to drop the last element of the double-ended queue, are similar to the above function which deal with the front of the double-ended queue. It is said ''almost'' because, after insertion and after an application of ''tail'', the invariant <code>|r| <= 2|f|+1</code> may not be satisfied anymore. In this case it is required to rebalance the double-ended queue.


<syntaxhighlight lang="sml">
In order to avoid an operation with an <code>O(n)</code> costs, the algorithm uses laziness with memoization, and force the rebalancing to be partly done during the following <code>(|l| + |r|)/2</code> operations, that is, before the following rebalancing. In order to create the scheduling, some auxiliary lazy functions are required. The function <code>rotateRev(f, r, a)</code> returns the list <code>f</code>, followed by the list <code>r</code>, and followed by the list <code>a</code>. It is required in this function that <code>|r|-2|f|</code> is 2 or 3. This function is defined by induction as <code>rotateRev(NIL, r, a)=reverse(r++a)</code> where ++ is the concatenation operation, and by <code>rotateRev(CONS(x, f), r, a)=CONS(x, rotateRev(f, drop(2, r), reverse (take(2, r))++a))</code>. <code>rotateRev(f, r, NIL)</code> returns the list <code>f</code> followed by the list <code>r</code> reversed. The function <code>rotateDrop(f, j, r)</code> which returns <code>f</code> followed by (<code>r</code> without <code>j</code>'s first element) reversed is also required, for <code>j < |f|</code>. It is defined by <code>rotateDrop(f, 0, r) == rotateRev(f, r, NIL)</code>, <code>rotateDrop(f, 1, r) == rotateRev(f, drop(1, r), NIL)</code> and <code>rotateDrop(CONS(x, f), j, r) == CONS(x, rotateDrop(f, j-2), drop(2, r))</code>.
empty = (0, NIL, NIL, 0, NIL, NIL)
fun insert'(x, (len_front, front, tail_front, len_rear, rear, tail_rear)) =
(len_front+1, CONS(x, front), drop(2, tail_front), len_rear, rear, drop(2, tail_rear))
fun head((_, CONS(h, _), _, _, _, _)) = h
fun head((_, NIL, _, _, CONS(h, NIL), _)) = h
fun tail'((len_front, CONS(head_front, front), tail_front, len_rear, rear, tail_rear)) =
(len_front - 1, front, drop(2, tail_front), len_rear, rear, drop(2, tail_rear))
fun tail'((_, NIL, _, _, CONS(h, NIL), _)) = empty
</syntaxhighlight>

It remains to explain how to define a method <code>balance</code> that rebalance the deque if <code>insert'</code> or <code>tail</code> broke the invariant. The method <code>insert</code> and <code>tail</code> can be defined by first applying <code>insert'</code> and <code>tail'</code> and then applying <code>balance</code>.


The balancing function can now be defined with
<syntaxhighlight lang="sml">
<syntaxhighlight lang="sml">
fun balance(q as (lenf, f, sf, lenr, r, sr))=
fun balance(q as (len_front, front, tail_front, len_rear, rear, tail_rear)) =
let floor_half_len = (len_front + len_rear) / 2 in
if lenf > 2*lenr+1 then
let ceil_half_len = len_front + len_rear - floor_half_len in
let val i = (left+lenr)div 2
if len_front > 2*len_rear+1 then
val j = lenf + lenr -i
val f' = take(i, f)
let val front' = take(ceil_half_len, front)
val r' = rotateDrop(r, i, f)
val rear' = rotateDrop(rear, floor_half_len, front)
in (i, f', f', j, r', r')
in (ceil_half_len, front', front', floor_half_len, rear', rear')
else if lenf > 2*lenr+1 then
else if len_front > 2*len_rear+1 then
let val j = (left+lenr)div 2
let val rear' = take(floor_half_len, rear)
val i = lenf + lenr -j
val front' = rotateDrop(front, ceil_half_len, rear)
in (ceil_half_len, front', front', floor_half_len, rear', rear')
val r' = take(i, r)
val f' = rotateDrop(f, i, r)
in (i, f', f', j, r', r')
else q
else q
</syntaxhighlight>
</syntaxhighlight>
where <code>rotateDrop(front, i, rear))</code> return the concatenation of <code>front</code> and of <code>drop(i, rear)</code>. That is<code>front' = rotateDrop(front, ceil_half_len, rear)</code> put into <code>front'</code> the content of <code>front</code> and the content of <code>rear</code> that is not already in <code>rear'</code>. Since dropping <code>n</code> elements takes <math>O(n)</math> time, we use laziness to ensure that elements are dropped two by two, with two drops being done during each <code>tail'</code> and each <code>insert'</code> operation.


<syntaxhighlight lang="sml">
Note that, without the lazy part of the implementation, this would be a non-persistent implementation of queue in <code>O(1)</code> [[amortized analysis|amortized time]]. In this case, the lists <code>sf</code> and <code>sr</code> can be removed from the representation of the double-ended queue.
fun rotateDrop(front, i, rear) =
if i < 2 then rotateRev(front, drop(i, rear), NIL)
else let CONS(x, front') = front in
CONS(x, rotateDrop(front', j-2, drop(2, rear)))
</syntaxhighlight>
where <code>rotateRev(front, middle, rear)</code> is a function that returns the front, followed by the middle reversed, followed by the rear. This function is also defined using laziness to ensure that it can be computed step by step, with one step executed during each <code>insert'</code> and <code>tail'</code> and taking a constant time. This function uses the invariant that <code>|rear|-2|front|</code> is 2 or 3.

<syntaxhighlight lang="sml">
fun rotateRev(NIL, rear, a) =
reverse(rear)++a
fun rotateRev(CONS(x, front), rear, a) =
CONS(x, rotateRev(front, drop(2, rear), reverse(take(2, rear))++a))
</syntaxhighlight>
where <code>++</code> is the function concatenating two lists.

==== Implementation without laziness ====
Note that, without the lazy part of the implementation, this would be a non-persistent implementation of queue in {{math|''O''(1)}} [[amortized analysis|amortized time]]. In this case, the lists <code>tail_front</code> and <code>tail_rear</code> could be removed from the representation of the double-ended queue.


== Language support ==
== Language support ==
[[Ada (programming language)|Ada]]'s containers provides the generic packages <tt>Ada.Containers.Vectors</tt> and <tt>Ada.Containers.Doubly_Linked_Lists</tt>, for the dynamic array and linked list implementations, respectively.
[[Ada (programming language)|Ada]]'s containers provides the generic packages <code>Ada.Containers.Vectors</code> and <code>Ada.Containers.Doubly_Linked_Lists</code>, for the dynamic array and linked list implementations, respectively.


C++'s [[Standard Template Library]] provides the class templates <tt>std::deque</tt> and <tt>std::list</tt>, for the multiple array and linked list implementations, respectively.
C++'s [[Standard Template Library]] provides the class templates <code>std::deque</code> and <code>std::list</code>, for the multiple array and linked list implementations, respectively.


As of Java 6, Java's Collections Framework provides a new {{Javadoc:SE|java/util|Deque}} interface that provides the functionality of insertion and removal at both ends. It is implemented by classes such as {{Javadoc:SE|java/util|ArrayDeque}} (also new in Java 6) and {{Javadoc:SE|java/util|LinkedList}}, providing the dynamic array and linked list implementations, respectively. However, the <tt>ArrayDeque</tt>, contrary to its name, does not support random access.
As of Java 6, Java's Collections Framework provides a new {{Javadoc:SE|java/util|Deque}} interface that provides the functionality of insertion and removal at both ends. It is implemented by classes such as {{Javadoc:SE|java/util|ArrayDeque}} (also new in Java 6) and {{Javadoc:SE|java/util|LinkedList}}, providing the dynamic array and linked list implementations, respectively. However, the <code>ArrayDeque</code>, contrary to its name, does not support random access.


Javascript's [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array Array prototype] & [[Perl]]'s arrays have native support for both removing ([http://perldoc.perl.org/functions/shift.html shift] and [http://perldoc.perl.org/functions/pop.html pop]) and adding ([http://perldoc.perl.org/functions/unshift.html unshift] and [http://perldoc.perl.org/functions/push.html push]) elements on both ends.
Javascript's [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array Array prototype] & [[Perl]]'s arrays have native support for both removing ([http://perldoc.perl.org/functions/shift.html shift] and [http://perldoc.perl.org/functions/pop.html pop]) and adding ([http://perldoc.perl.org/functions/unshift.html unshift] and [http://perldoc.perl.org/functions/push.html push]) elements on both ends.


Python 2.4 introduced the <code>collections</code> module with support for [https://docs.python.org/2.7/library/collections.html#deque-objects deque objects]. It is implemented using a doubly linked list of fixed-length subarrays.
Python 2.4 introduced the <code>collections</code> module with support for [https://docs.python.org/3/library/collections.html#deque-objects deque objects]. It is implemented using a doubly linked list of fixed-length subarrays.


As of PHP 5.3, PHP's SPL extension contains the 'SplDoublyLinkedList' class that can be used to implement Deque datastructures. Previously to make a Deque structure the array functions array_shift/unshift/pop/push had to be used instead.
As of PHP 5.3, PHP's SPL extension contains the 'SplDoublyLinkedList' class that can be used to implement Deque datastructures. Previously to make a Deque structure the array functions array_shift/unshift/pop/push had to be used instead.


[[Glasgow Haskell Compiler|GHC]]'s [http://hackage.haskell.org/package/containers-0.6.0.1/docs/Data-Sequence.html Data.Sequence] module implements an efficient, functional deque structure in [[Haskell (programming language)|Haskell]]. The implementation uses [[2–3 tree|2–3 finger trees]] annotated with sizes. There are other (fast) possibilities to implement purely functional (thus also [[persistent data structure|persistent]]) double queues (most using heavily [[lazy evaluation]]).<ref name="functional">https://www.cs.cmu.edu/~rwh/theses/okasaki.pdf C. Okasaki, "Purely Functional Data Structures", September 1996</ref><ref>Adam L. Buchsbaum and Robert E. Tarjan. Confluently persistent deques via data structural bootstrapping. Journal of Algorithms, 18(3):513–547, May 1995. (pp. 58, 101, 125)</ref> Kaplan and Tarjan were the first to implement optimal confluently persistent catenable deques.<ref>Haim Kaplan and Robert E. Tarjan. Purely functional representations of catenable sorted lists. In ACM Symposium on Theory of Computing, pages 202–211, May 1996. (pp. 4, 82, 84, 124)</ref> Their implementation was strictly purely functional in the sense that it did not use lazy evaluation. Okasaki simplified the data structure by using lazy evaluation with a bootstrapped data structure and degrading the performance bounds from worst-case to amortized. Kaplan, Okasaki, and Tarjan produced a simpler, non-bootstrapped, amortized version that can be implemented either using lazy evaluation or more efficiently using mutation in a broader but still restricted fashion. Mihaesau and Tarjan created a simpler (but still highly complex) strictly purely functional implementation of catenable deques, and also a much simpler implementation of strictly purely functional non-catenable deques, both of which have optimal worst-case bounds.
[[Glasgow Haskell Compiler|GHC]]'s [http://hackage.haskell.org/package/containers-0.6.0.1/docs/Data-Sequence.html Data.Sequence] module implements an efficient, functional deque structure in [[Haskell (programming language)|Haskell]]. The implementation uses [[2–3 tree|2–3 finger trees]] annotated with sizes. There are other (fast) possibilities to implement purely functional (thus also [[persistent data structure|persistent]]) double queues (most using heavily [[lazy evaluation]]).<ref name="functional"/><ref>Adam L. Buchsbaum and Robert E. Tarjan. Confluently persistent deques via data structural bootstrapping. Journal of Algorithms, 18(3):513–547, May 1995. (pp. 58, 101, 125)</ref> Kaplan and Tarjan were the first to implement optimal confluently persistent catenable deques.<ref>Haim Kaplan and Robert E. Tarjan. Purely functional representations of catenable sorted lists. In ACM Symposium on Theory of Computing, pages 202–211, May 1996. (pp. 4, 82, 84, 124)</ref> Their implementation was strictly purely functional in the sense that it did not use lazy evaluation. Okasaki simplified the data structure by using lazy evaluation with a bootstrapped data structure and degrading the performance bounds from worst-case to amortized.<ref>Chris Okasaki (Aug. 1997), [https://dl.acm.org/doi/10.1145/258949.258956 Catenable double-ended queues], ACM SIGPLAN Notices Volume 32 Issue 8</ref> Kaplan, Okasaki, and Tarjan produced a simpler, non-bootstrapped, amortized version that can be implemented either using lazy evaluation or more efficiently using mutation in a broader but still restricted fashion.<ref> Haim Kaplan, Chris Okasaki, and Robert E. Tarjan (2000), [https://epubs.siam.org/doi/10.1137/S0097539798339430 Simple Confluently Persistent Catenable Lists], SIAM Journal on Computing Vol. 30, Iss. 3</ref> Mihaescu and Tarjan created a simpler (but still highly complex) strictly purely functional implementation of catenable deques, and also a much simpler implementation of strictly purely functional non-catenable deques, both of which have optimal worst-case bounds.<ref>Radu Mihaescu and Robert Tarjan (Aug. 2003), [https://www.cs.princeton.edu/courses/archive/fall03/cs528/handouts/Notes%20on%20Catenable%20Deques.doc Notes on Catenable Deques in Pure Lisp], Princetown University, COS 528, Fall 03</ref>


Rust's <code>std::collections</code> includes [https://doc.rust-lang.org/std/collections/struct.VecDeque.html VecDeque] which implements a double-ended queue using a growable ring buffer.
Rust's <code>std::collections</code> includes [https://doc.rust-lang.org/std/collections/struct.VecDeque.html VecDeque] which implements a double-ended queue using a growable ring buffer.
Line 102: Line 134:
* In a doubly-linked list implementation and assuming no allocation/deallocation overhead, the [[Computational complexity theory|time complexity]] of all deque operations is [[Big O notation|O(1)]]. Additionally, the time complexity of insertion or deletion in the middle, given an iterator, is O(1); however, the time complexity of random access by index is O(n).
* In a doubly-linked list implementation and assuming no allocation/deallocation overhead, the [[Computational complexity theory|time complexity]] of all deque operations is [[Big O notation|O(1)]]. Additionally, the time complexity of insertion or deletion in the middle, given an iterator, is O(1); however, the time complexity of random access by index is O(n).
* In a growing array, the [[Amortized analysis|amortized]] time complexity of all deque operations is [[Big O notation|O(1)]]. Additionally, the time complexity of random access by index is O(1); but the time complexity of insertion or deletion in the middle is [[Big O notation|O(n)]].
* In a growing array, the [[Amortized analysis|amortized]] time complexity of all deque operations is [[Big O notation|O(1)]]. Additionally, the time complexity of random access by index is O(1); but the time complexity of insertion or deletion in the middle is [[Big O notation|O(n)]].

== Applications ==
== Applications ==
[[File:Back button history (Ecosia browser - Google Pixel 4a) - Google Android 12 (cropped).png|thumb|A double-ended queue can be used to store the [[Web browsing history|browsing history]]: new websites are added to the end of the queue, while the oldest entries will be deleted when the history is too large. When a user asks to clear the browsing history for the past hour, the most recently added entries are removed.]]
One example where a deque can be used is the ''A-Steal'' [[Scheduling algorithm|job scheduling algorithm]].<ref>{{cite book | author=Eitan Frachtenberg, Uwe Schwiegelshohn | title=Job Scheduling Strategies for Parallel Processing: 12th International Workshop, JSSPP 2006
|publisher=Springer| year=2007| isbn=3-540-71034-5}} See p.22.</ref> This algorithm implements task scheduling for several processors. A separate deque with threads to be executed is maintained for each processor. To execute the next thread, the processor gets the first element from the deque (using the "remove first element" deque operation). If the current thread [[Fork (operating system)|forks]], it is put back to the front of the deque ("insert element at front") and a new thread is executed. When one of the processors finishes execution of its own threads (i.e. its deque is empty), it can "steal" a thread from another processor: it gets the last element from the deque of another processor ("remove last element") and executes it. The steal-job scheduling algorithm is used by Intel's Threading Building Blocks (TBB) library for parallel programming.
One example where a deque can be used is the [[Work stealing|work stealing algorithm]].<ref name="jacm">{{cite journal |last1=Blumofe |first1=Robert D. |first2=Charles E. |last2=Leiserson |author-link2=Charles E. Leiserson |title=Scheduling multithreaded computations by work stealing |journal=J ACM |volume=46 |issue=5 |year=1999 |pages=720–748 |url=http://supertech.csail.mit.edu/papers/ft_gateway.pdf |doi=10.1145/324133.324234|s2cid=5428476 }}</ref> This algorithm implements task scheduling for several processors. A separate deque with threads to be executed is maintained for each processor. To execute the next thread, the processor gets the first element from the deque (using the "remove first element" deque operation). If the current thread forks, it is put back to the front of the deque ("insert element at front") and a new thread is executed. When one of the processors finishes execution of its own threads (i.e. its deque is empty), it can "steal" a thread from another processor: it gets the last element from the deque of another processor ("remove last element") and executes it. The work stealing algorithm is used by Intel's Threading Building Blocks (TBB) library for parallel programming.


== See also ==
== See also ==
*[[Pipeline (Unix)#pipe character|Pipe]]
*[[Pipeline (Unix)#pipe character|Pipe]]
*[[Queue (data structure)|Queue]]
*[[Priority queue]]
*[[Priority queue]]


Line 119: Line 149:
* [http://www.sgi.com/tech/stl/Deque.html SGI STL Documentation: deque<T, Alloc>]
* [http://www.sgi.com/tech/stl/Deque.html SGI STL Documentation: deque<T, Alloc>]
* [http://www.codeproject.com/KB/stl/vector_vs_deque.aspx Code Project: An In-Depth Study of the STL Deque Container]
* [http://www.codeproject.com/KB/stl/vector_vs_deque.aspx Code Project: An In-Depth Study of the STL Deque Container]
* [http://www.martinbroadhurst.com/articles/deque.html Deque implementation in C]
* [http://www.martinbroadhurst.com/articles/deque.html Deque implementation in C] {{Webarchive|url=https://web.archive.org/web/20140306145414/http://www.martinbroadhurst.com/articles/deque.html |date=2014-03-06 }}
* [https://web.archive.org/web/20110714000645/http://www.ludvikjerabek.com/downloads.html VBScript implementation of stack, queue, deque, and Red-Black Tree]
* [https://web.archive.org/web/20110714000645/http://www.ludvikjerabek.com/downloads.html VBScript implementation of stack, queue, deque, and Red-Black Tree]
* [https://code.google.com/p/deques/source/browse/ Multiple implementations of non-catenable deques in Haskell]
* [https://code.google.com/p/deques/source/browse/ Multiple implementations of non-catenable deques in Haskell]

Latest revision as of 01:27, 27 May 2024

In computer science, a double-ended queue (abbreviated to deque, /dɛk/ DEK[1]) is an abstract data type that generalizes a queue, for which elements can be added to or removed from either the front (head) or back (tail).[2] It is also often called a head-tail linked list, though properly this refers to a specific data structure implementation of a deque (see below).

Naming conventions[edit]

Deque is sometimes written dequeue, but this use is generally deprecated in technical literature or technical writing because dequeue is also a verb meaning "to remove from a queue". Nevertheless, several libraries and some writers, such as Aho, Hopcroft, and Ullman in their textbook Data Structures and Algorithms, spell it dequeue. John Mitchell, author of Concepts in Programming Languages, also uses this terminology.

Distinctions and sub-types[edit]

This differs from the queue abstract data type or first in first out list (FIFO), where elements can only be added to one end and removed from the other. This general data class has some possible sub-types:

  • An input-restricted deque is one where deletion can be made from both ends, but insertion can be made at one end only.
  • An output-restricted deque is one where insertion can be made at both ends, but deletion can be made from one end only.

Both the basic and most common list types in computing, queues and stacks can be considered specializations of deques, and can be implemented using deques.

Operations[edit]

UML class diagram of a double-ended queue
UML class diagram of a double-ended queue

The basic operations on a deque are enqueue and dequeue on either end. Also generally implemented are peek operations, which return the value at that end without dequeuing it.

Names vary between languages; major implementations include:

operation common name(s) Ada C++ Java Perl PHP Python Ruby Rust JavaScript
insert element at back inject, snoc, push Append push_back offerLast push array_push append push push_back push
insert element at front push, cons Prepend push_front offerFirst unshift array_unshift appendleft unshift push_front unshift
remove last element eject Delete_Last pop_back pollLast pop array_pop pop pop pop_back pop
remove first element pop Delete_First pop_front pollFirst shift array_shift popleft shift pop_front shift
examine last element peek Last_Element back peekLast $array[-1] end <obj>[-1] last back <obj>.at(-1)
examine first element First_Element front peekFirst $array[0] reset <obj>[0] first front <obj>[0]

Implementations[edit]

There are at least two common ways to efficiently implement a deque: with a modified dynamic array or with a doubly linked list.

The dynamic array approach uses a variant of a dynamic array that can grow from both ends, sometimes called array deques. These array deques have all the properties of a dynamic array, such as constant-time random access, good locality of reference, and inefficient insertion/removal in the middle, with the addition of amortized constant-time insertion/removal at both ends, instead of just one end. Three common implementations include:

  • Storing deque contents in a circular buffer, and only resizing when the buffer becomes full. This decreases the frequency of resizings.
  • Allocating deque contents from the center of the underlying array, and resizing the underlying array when either end is reached. This approach may require more frequent resizings and waste more space, particularly when elements are only inserted at one end.
  • Storing contents in multiple smaller arrays, allocating additional arrays at the beginning or end as needed. Indexing is implemented by keeping a dynamic array containing pointers to each of the smaller arrays.

Purely functional implementation[edit]

Double-ended queues can also be implemented as a purely functional data structure.[3]: 115  Two versions of the implementation exist. The first one, called 'real-time deque, is presented below. It allows the queue to be persistent with operations in O(1) worst-case time, but requires lazy lists with memoization. The second one, with no lazy lists nor memoization is presented at the end of the sections. Its amortized time is O(1) if the persistency is not used; but the worst-time complexity of an operation is O(n) where n is the number of elements in the double-ended queue.

Let us recall that, for a list l, |l| denotes its length, that NIL represents an empty list and CONS(h, t) represents the list whose head is h and whose tail is t. The functions drop(i, l) and take(i, l) return the list l without its first i elements, and the first i elements of l, respectively. Or, if |l| < i, they return the empty list and l respectively.

Real-time deques via lazy rebuilding and scheduling[edit]

A double-ended queue is represented as a sextuple (len_front, front, tail_front, len_rear, rear, tail_rear) where front is a linked list which contains the front of the queue of length len_front. Similarly, rear is a linked list which represents the reverse of the rear of the queue, of length len_rear. Furthermore, it is assured that |front| ≤ 2|rear|+1 and |rear| ≤ 2|front|+1 - intuitively, it means that both the front and the rear contains between a third minus one and two thirds plus one of the elements. Finally, tail_front and tail_rear are tails of front and of rear, they allow scheduling the moment where some lazy operations are forced. Note that, when a double-ended queue contains n elements in the front list and n elements in the rear list, then the inequality invariant remains satisfied after i insertions and d deletions when (i+d) ≤ n/2. That is, at most n/2 operations can happen between each rebalancing.

Let us first give an implementation of the various operations that affect the front of the deque - cons, head and tail. Those implementations do not necessarily respect the invariant. In a second time we'll explain how to modify a deque which does not satisfy the invariant into one which satisfies it. However, they use the invariant, in that if the front is empty then the rear has at most one element. The operations affecting the rear of the list are defined similarly by symmetry.

empty = (0, NIL, NIL, 0, NIL, NIL)
fun insert'(x, (len_front, front, tail_front, len_rear, rear, tail_rear)) =
  (len_front+1, CONS(x, front), drop(2, tail_front), len_rear, rear, drop(2, tail_rear))
fun head((_, CONS(h, _), _, _, _, _)) = h
fun head((_, NIL, _, _, CONS(h, NIL), _)) = h
fun tail'((len_front, CONS(head_front, front), tail_front, len_rear, rear, tail_rear)) =
  (len_front - 1, front, drop(2, tail_front), len_rear, rear, drop(2, tail_rear))
fun tail'((_, NIL, _, _, CONS(h, NIL), _)) = empty

It remains to explain how to define a method balance that rebalance the deque if insert' or tail broke the invariant. The method insert and tail can be defined by first applying insert' and tail' and then applying balance.

fun balance(q as (len_front, front, tail_front, len_rear, rear, tail_rear)) =
  let floor_half_len = (len_front + len_rear) / 2 in
  let ceil_half_len = len_front + len_rear - floor_half_len in
  if len_front > 2*len_rear+1 then
    let val front' = take(ceil_half_len, front)
        val rear' = rotateDrop(rear, floor_half_len, front)
    in (ceil_half_len, front', front', floor_half_len, rear', rear')
  else if len_front > 2*len_rear+1 then
    let val rear' = take(floor_half_len, rear)
        val front' = rotateDrop(front, ceil_half_len, rear)
    in (ceil_half_len, front', front', floor_half_len, rear', rear')
  else q

where rotateDrop(front, i, rear)) return the concatenation of front and of drop(i, rear). That isfront' = rotateDrop(front, ceil_half_len, rear) put into front' the content of front and the content of rear that is not already in rear'. Since dropping n elements takes time, we use laziness to ensure that elements are dropped two by two, with two drops being done during each tail' and each insert' operation.

fun rotateDrop(front, i, rear) =
  if i < 2 then rotateRev(front, drop(i, rear), NIL)
  else let CONS(x, front') = front in
    CONS(x, rotateDrop(front', j-2, drop(2, rear)))

where rotateRev(front, middle, rear) is a function that returns the front, followed by the middle reversed, followed by the rear. This function is also defined using laziness to ensure that it can be computed step by step, with one step executed during each insert' and tail' and taking a constant time. This function uses the invariant that |rear|-2|front| is 2 or 3.

fun rotateRev(NIL, rear, a) =
  reverse(rear)++a
fun rotateRev(CONS(x, front), rear, a) =
  CONS(x, rotateRev(front, drop(2, rear), reverse(take(2, rear))++a))

where ++ is the function concatenating two lists.

Implementation without laziness[edit]

Note that, without the lazy part of the implementation, this would be a non-persistent implementation of queue in O(1) amortized time. In this case, the lists tail_front and tail_rear could be removed from the representation of the double-ended queue.

Language support[edit]

Ada's containers provides the generic packages Ada.Containers.Vectors and Ada.Containers.Doubly_Linked_Lists, for the dynamic array and linked list implementations, respectively.

C++'s Standard Template Library provides the class templates std::deque and std::list, for the multiple array and linked list implementations, respectively.

As of Java 6, Java's Collections Framework provides a new Deque interface that provides the functionality of insertion and removal at both ends. It is implemented by classes such as ArrayDeque (also new in Java 6) and LinkedList, providing the dynamic array and linked list implementations, respectively. However, the ArrayDeque, contrary to its name, does not support random access.

Javascript's Array prototype & Perl's arrays have native support for both removing (shift and pop) and adding (unshift and push) elements on both ends.

Python 2.4 introduced the collections module with support for deque objects. It is implemented using a doubly linked list of fixed-length subarrays.

As of PHP 5.3, PHP's SPL extension contains the 'SplDoublyLinkedList' class that can be used to implement Deque datastructures. Previously to make a Deque structure the array functions array_shift/unshift/pop/push had to be used instead.

GHC's Data.Sequence module implements an efficient, functional deque structure in Haskell. The implementation uses 2–3 finger trees annotated with sizes. There are other (fast) possibilities to implement purely functional (thus also persistent) double queues (most using heavily lazy evaluation).[3][4] Kaplan and Tarjan were the first to implement optimal confluently persistent catenable deques.[5] Their implementation was strictly purely functional in the sense that it did not use lazy evaluation. Okasaki simplified the data structure by using lazy evaluation with a bootstrapped data structure and degrading the performance bounds from worst-case to amortized.[6] Kaplan, Okasaki, and Tarjan produced a simpler, non-bootstrapped, amortized version that can be implemented either using lazy evaluation or more efficiently using mutation in a broader but still restricted fashion.[7] Mihaescu and Tarjan created a simpler (but still highly complex) strictly purely functional implementation of catenable deques, and also a much simpler implementation of strictly purely functional non-catenable deques, both of which have optimal worst-case bounds.[8]

Rust's std::collections includes VecDeque which implements a double-ended queue using a growable ring buffer.

Complexity[edit]

  • In a doubly-linked list implementation and assuming no allocation/deallocation overhead, the time complexity of all deque operations is O(1). Additionally, the time complexity of insertion or deletion in the middle, given an iterator, is O(1); however, the time complexity of random access by index is O(n).
  • In a growing array, the amortized time complexity of all deque operations is O(1). Additionally, the time complexity of random access by index is O(1); but the time complexity of insertion or deletion in the middle is O(n).

Applications[edit]

A double-ended queue can be used to store the browsing history: new websites are added to the end of the queue, while the oldest entries will be deleted when the history is too large. When a user asks to clear the browsing history for the past hour, the most recently added entries are removed.

One example where a deque can be used is the work stealing algorithm.[9] This algorithm implements task scheduling for several processors. A separate deque with threads to be executed is maintained for each processor. To execute the next thread, the processor gets the first element from the deque (using the "remove first element" deque operation). If the current thread forks, it is put back to the front of the deque ("insert element at front") and a new thread is executed. When one of the processors finishes execution of its own threads (i.e. its deque is empty), it can "steal" a thread from another processor: it gets the last element from the deque of another processor ("remove last element") and executes it. The work stealing algorithm is used by Intel's Threading Building Blocks (TBB) library for parallel programming.

See also[edit]

References[edit]

  1. ^ Jesse Liberty; Siddhartha Rao; Bradley Jones. C++ in One Hour a Day, Sams Teach Yourself, Sixth Edition. Sams Publishing, 2009. ISBN 0-672-32941-7. Lesson 18: STL Dynamic Array Classes, pp. 486.
  2. ^ Donald Knuth. The Art of Computer Programming, Volume 1: Fundamental Algorithms, Third Edition. Addison-Wesley, 1997. ISBN 0-201-89683-4. Section 2.2.1: Stacks, Queues, and Deques, pp. 238–243.
  3. ^ a b Okasaki, Chris (September 1996). Purely Functional Data Structures (PDF) (Ph.D. thesis). Carnegie Mellon University. CMU-CS-96-177.
  4. ^ Adam L. Buchsbaum and Robert E. Tarjan. Confluently persistent deques via data structural bootstrapping. Journal of Algorithms, 18(3):513–547, May 1995. (pp. 58, 101, 125)
  5. ^ Haim Kaplan and Robert E. Tarjan. Purely functional representations of catenable sorted lists. In ACM Symposium on Theory of Computing, pages 202–211, May 1996. (pp. 4, 82, 84, 124)
  6. ^ Chris Okasaki (Aug. 1997), Catenable double-ended queues, ACM SIGPLAN Notices Volume 32 Issue 8
  7. ^ Haim Kaplan, Chris Okasaki, and Robert E. Tarjan (2000), Simple Confluently Persistent Catenable Lists, SIAM Journal on Computing Vol. 30, Iss. 3
  8. ^ Radu Mihaescu and Robert Tarjan (Aug. 2003), Notes on Catenable Deques in Pure Lisp, Princetown University, COS 528, Fall 03
  9. ^ Blumofe, Robert D.; Leiserson, Charles E. (1999). "Scheduling multithreaded computations by work stealing" (PDF). J ACM. 46 (5): 720–748. doi:10.1145/324133.324234. S2CID 5428476.

External links[edit]