Jump to content
 







Main menu
   


Navigation  



Main page
Contents
Current events
Random article
About Wikipedia
Contact us
Donate
 




Contribute  



Help
Learn to edit
Community portal
Recent changes
Upload file
 








Search  

































Create account

Log in
 









Create account
 Log in
 




Pages for logged out editors learn more  



Contributions
Talk
 



















Contents

   



(Top)
 


1 Operations  





2 Implementation  



2.1  Dual structure method  





2.2  Total correspondence  





2.3  Leaf correspondence  





2.4  Interval heaps  



2.4.1  Inserting an element  





2.4.2  Deleting an element  









3 Time complexity  



3.1  Interval heaps  





3.2  Pairing heaps  







4 Applications  



4.1  External sorting  







5 See also  





6 References  














Double-ended priority queue






فارسی
Українська


 

Edit links
 









Article
Talk
 

















Read
Edit
View history
 








Tools
   


Actions  



Read
Edit
View history
 




General  



What links here
Related changes
Upload file
Special pages
Permanent link
Page information
Cite this page
Get shortened URL
Download QR code
Wikidata item
 




Print/export  



Download as PDF
Printable version
 
















Appearance
   

 






From Wikipedia, the free encyclopedia
 


Incomputer science, a double-ended priority queue (DEPQ)[1]ordouble-ended heap[2] is a data structure similar to a priority queueorheap, but allows for efficient removal of both the maximum and minimum, according to some ordering on the keys (items) stored in the structure. Every element in a DEPQ has a priority or value. In a DEPQ, it is possible to remove the elements in both ascending as well as descending order.[3]

Operations

[edit]
UML class diagram of a double-ended priority queue.
UML class diagram of a double-ended priority queue.

A double-ended priority queue features the following operations:

isEmpty()
Checks if DEPQ is empty and returns true if empty.
size()
Returns the total number of elements present in the DEPQ.
getMin()
Returns the element having least priority.
getMax()
Returns the element having highest priority.
put(x)
Inserts the element x in the DEPQ.
removeMin()
Removes an element with minimum priority and returns this element.
removeMax()
Removes an element with maximum priority and returns this element.

Also, the priority of any element can be changed once it has been inserted in the DEPQ.[4]

Implementation

[edit]

Double-ended priority queues can be built from balanced binary search trees (where the minimum and maximum elements are the leftmost and rightmost leaves, respectively), or using specialized data structures like min-max heap and pairing heap.

Generic methods of arriving at double-ended priority queues from normal priority queues are:[5]

Dual structure method

[edit]
A dual structure with 14,12,4,10,8 as the members of DEPQ.[1]

In this method two different priority queues for min and max are maintained. The same elements in both the PQs are shown with the help of correspondence pointers.
Here, the minimum and maximum elements are values contained in the root nodes of min heap and max heap respectively.

Total correspondence

[edit]
A total correspondence heap for the elements 3, 4, 5, 5, 6, 6, 7, 8, 9, 10, 11 with element 11 as buffer.[1]

Half the elements are in the min PQ and the other half in the max PQ. Each element in the min PQ has a one-to-one correspondence with an element in max PQ. If the number of elements in the DEPQ is odd, one of the elements is retained in a buffer.[1] Priority of every element in the min PQ will be less than or equal to the corresponding element in the max PQ.

Leaf correspondence

[edit]
A leaf correspondence heap for the same elements as above.[1]

In contrast to a total correspondence, in this method only the leaf elements of the min and max PQ form corresponding one-to-one pairs. It is not necessary for non-leaf elements to be in a one-to-one correspondence pair.[1] If the number of elements in the DEPQ is odd, one of the elements is retained in a buffer.[1]

Interval heaps

[edit]
Implementing a DEPQ using interval heap.

Apart from the above-mentioned correspondence methods, DEPQ's can be obtained efficiently using interval heaps.[6] An interval heap is like an embedded min-max heap in which each node contains two elements. It is a complete binary tree in which:[6]

Depending on the number of elements, two cases are possible[6] -

  1. Even number of elements: In this case, each node contains two elements say p and q, with p ≤ q. Every node is then represented by the interval [pq].
  2. Odd number of elements: In this case, each node except the last contains two elements represented by the interval [pq] whereas the last node will contain a single element and is represented by the interval [pp].

Inserting an element

[edit]

Depending on the number of elements already present in the interval heap, following cases are possible:

The time required for inserting an element depends on the number of movements required to meet all the conditions and is O(log n).

Deleting an element

[edit]

Thus, with interval heaps, both the minimum and maximum elements can be removed efficiently traversing from root to leaf. Thus, a DEPQ can be obtained[6] from an interval heap where the elements of the interval heap are the priorities of elements in the DEPQ.

Time complexity

[edit]

Interval heaps

[edit]

When DEPQ's are implemented using Interval heaps consisting of n elements, the time complexities for the various functions are formulated in the table below[1]

Operation Time Complexity
init( ) O(n)
isEmpty( ) O(1)
getmin( ) O(1)
getmax( ) O(1)
size( ) O(1)
insert(x) O(log n)
removeMin( ) O(log n)
removeMax( ) O(log n)

Pairing heaps

[edit]

When DEPQ's are implemented using heaps or pairing heaps consisting of n elements, the time complexities for the various functions are formulated in the table below.[1] For pairing heaps, it is an amortized complexity.

Operation Time Complexity
isEmpty( ) O(1)
getmin( ) O(1)
getmax( ) O(1)
insert(x) O(log n)
removeMax( ) O(log n)
removeMin( ) O(log n)

Applications

[edit]

External sorting

[edit]

One example application of the double-ended priority queue is external sorting. In an external sort, there are more elements than can be held in the computer's memory. The elements to be sorted are initially on a disk and the sorted sequence is to be left on the disk. The external quick sort is implemented using the DEPQ as follows:

  1. Read in as many elements as will fit into an internal DEPQ. The elements in the DEPQ will eventually be the middle group (pivot) of elements.
  2. Read in the remaining elements. If the next element is ≤ the smallest element in the DEPQ, output this next element as part of the left group. If the next element is ≥ the largest element in the DEPQ, output this next element as part of the right group. Otherwise, remove either the max or min element from the DEPQ (the choice may be made randomly or alternately); if the max element is removed, output it as part of the right group; otherwise, output the removed element as part of the left group; insert the newly input element into the DEPQ.
  3. Output the elements in the DEPQ, in sorted order, as the middle group.
  4. Sort the left and right groups recursively.

See also

[edit]

References

[edit]
  • ^ Brass, Peter (2008). Advanced Data Structures. Cambridge University Press. p. 211. ISBN 9780521880374.
  • ^ "Depq - Double-Ended Priority Queue". Archived from the original on 2012-04-25. Retrieved 2011-10-04.
  • ^ "depq".
  • ^ Fundamentals of Data Structures in C++ - Ellis Horowitz, Sartaj Sahni and Dinesh Mehta
  • ^ a b c d e f g http://www.mhhe.com/engcs/compsci/sahni/enrich/c9/interval.pdf [bare URL PDF]

  • Retrieved from "https://en.wikipedia.org/w/index.php?title=Double-ended_priority_queue&oldid=1223688180"

    Categories: 
    Abstract data types
    Heaps (data structures)
    Priority queues
    Hidden categories: 
    All articles with bare URLs for citations
    Articles with bare URLs for citations from March 2022
    Articles with PDF format bare URLs for citations
     



    This page was last edited on 13 May 2024, at 18:32 (UTC).

    Text is available under the Creative Commons Attribution-ShareAlike License 4.0; additional terms may apply. By using this site, you agree to the Terms of Use and Privacy Policy. Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc., a non-profit organization.



    Privacy policy

    About Wikipedia

    Disclaimers

    Contact Wikipedia

    Code of Conduct

    Developers

    Statistics

    Cookie statement

    Mobile view



    Wikimedia Foundation
    Powered by MediaWiki