Presentation is loading. Please wait.

Presentation is loading. Please wait.

Double-Ended Priority Queues

Similar presentations


Presentation on theme: "Double-Ended Priority Queues"— Presentation transcript:

1 Double-Ended Priority Queues
Primary operations Insert Remove Max Remove Min Note that a single-ended priority queue supports just one of the above remove operations. Find min and max.

2 General Methods Dual min and max single-ended priority queues.
Correspondence based min and max single-ended priority queues. The general methods allow one to construct a double ended priority queue data structure from one for single ended priority queues.

3 Specialized Structures
Symmetric min-max heaps. Min-max heaps. Deaps. Interval heaps.

4 Dual Single-Ended Priority Queues
Each element is in both a min and a max single-ended priority queue. Single-ended priority queue also must support an arbitrary remove. Each node in a priority queue has a pointer to the node in the other priority queue that has the same element.

5 9-Element Example Only 5 of 9 two-way pointers shown.
1 4 5 7 9 3 8 6 2 Min Heap 9 6 5 1 4 7 2 3 8 Max Heap Only 5 of 9 two-way pointers shown. Insert, remove min, remove max, initialize. Operation cost is more than doubled relative to heap. Space for 2n nodes.

6 Correspondence Structures
Use a min and a max single-ended priority queue. At most 1 element is in a buffer. Remaining elements are in the single-ended priority queues, which may be of different size. No element is in both the min and max single-ended priority queue. Establish a correspondence between the min and max single-ended priority queues. Total correspondence. Leaf correspondence. Single-ended priority queue also must support an arbitrary remove.

7 Total Correspondence The min- and max-priority queues are of the same size. Each element of the min priority queue is paired with a different and >= element in the max priority queue.

8 Total Correspondence Example
1 5 9 14 17 Min Heap 20 10 18 2 7 Max Heap Overall min element is either in the buffer or in the root of the min heap. Buffer = 12

9 Insert Buffer empty => place in buffer.
1 5 9 14 17 Min Heap 20 10 18 2 7 Max Heap Buffer = 12 Buffer empty => place in buffer. Else, insert smaller of new and buffer elements into min priority queue and larger into max priority queue; establish correspondence between the 2 elements.

10 Remove Min Buffer is min => empty buffer.
1 5 9 14 17 Min Heap 20 10 18 2 7 Max Heap Buffer = 12 Can be done as change key (of min and max) operations. Change 1 to 2 and 2 to 12. Or, if buffer is <= corresponding element, do not remove corresponding element from maxPQ; just do a changeMin on min element in minPQ. Buffer is min => empty buffer. Else, remove min from min PQ as well as corresponding element from max PQ; reinsert corresponding element.

11 Leaf Correspondence Min- and max-priority queues may have different size. Each leaf element of the min priority queue is paired with a different and >= element in the max priority queue. Each leaf element of the max priority queue is paired with a different and <= element in the min priority queue.

12 Added Restrictions When an element is inserted into a single-ended PQ, only the newly inserted element can become a new leaf. When an element is deleted from a single-ended PQ, only the parent of the deleted element can become a new leaf. Min and max heaps do not satisfy these restrictions. So, leaf correspondence is harder to implement using min and max heaps. Note that in restriction 2, we are talking about an arbitrary delete (not necessarily the deletion of the min or max element).

13 Leaf Correspondence Example
1 5 3 14 17 Min Heap 20 10 18 6 7 Max Heap The smallest in the max heap is in a leaf. This leaf has a <= corresponding element in the min Heap. So, overall smallest is in the min Heap. Buffer = 12

14 Insert Buffer empty => place in buffer.
1 5 3 14 17 Min Heap 20 10 18 6 7 Max Heap Buffer = 12 In the insert, if the smaller element is not a leaf following the insertion, the larger element is left in the buffer. When the smaller element is a leaf, the larger is inserted into the max PQ and correspondence pointers set between the two inserted elements. This doesn’t work always for min and max heaps, because they do not satisfy the restrictions for the simple insert operation on leaf correspondence DEPQs. When the min and max heaps have an odd number of elements each prior to the insert a non-leaf cannot become a leaf. So we are OK. When each has an even number of elements prior to the insert, a new right child node is added in each heap. So, a non-leaf can become a leaf. Note that min and max heaps may have different size! Buffer empty => place in buffer. Else, insert smaller of new and buffer elements into min priority queue; insert larger into max priority queue only if smaller one is a leaf.

15 Insert 1 5 3 14 17 Min Heap 20 10 18 6 7 Max Heap Buffer = 12 Case when min and/or max heap originally have an even number of elements is more involved, because a nonleaf may become a leaf. See reference.

16 Remove Min Buffer is min => empty buffer.
1 5 3 14 17 Min Heap 20 10 18 6 7 Max Heap Buffer = 12 Once again. The remove min operation is more involved for DEPQs based on heaps than for DEPQs based on other single ended PQ structures, because heaps do not satisfy the stated restrictions. Breaks into cases depending on whether the buffer is empty or not and further on whether the buffer has the min element. Some optimization is possible and is done in the paper. For example, if buffer <= corresponding element, may avoid deleting from max heap and just insert buffer into min heap. E.g., 18 has a left child 4 and 1 corresponds with 6. When 1 is deleted, 6 must also be deleted. 6 gets replaced with 4 and 18 becomes a new leaf. Buffer is min => empty buffer. Else, remove min from min PQ as well as corresponding leaf element (if any) from max PQ; reinsert removed corresponding element (see reference for details).


Download ppt "Double-Ended Priority Queues"

Similar presentations


Ads by Google