User:BPositive/Sandbox

From Wikipedia, the free encyclopedia

Proposed points for Double-ended priority queue:

Introduction[edit]

What it is all about

Operations[edit]

Operations on Double-ended priority queues

ADT for a DEPQ[edit]

An abstract data type for a DEPQ can be written as shown below:

typedef struct node
{
        int val, priority;
	struct node *previous, *next;
}node;

typedef struct depq
{
	node *head, *tail;
}depq;

void init(depq *q);
int isempty(depq *q);
int isfull(depq *q);
void removemax(depq *p);
void removemin(depq *p);
void traverse(depq *q);
void append(depq *q, int val, int priority);

Implementation[edit]

Generic methods of arriving at Double-ended priority queues from normal priority queues are:[1]

Dual Structure Method[edit]

In this method two different priority queues for min and max are maintained.Same elements in both the PQ's are shown with the help of correspondence pointers.

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

Total Correspondence[edit]

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.In case if the number of elements in the DEPQ are odd, one of the elements is retained in a buffer.[2]Priority of every element in the min PQ will be less than or equal to the corresponding element in the max PQ.

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

Leaf Correspondence[edit]

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.[2]

A leaf correspondence heap for the same elements as above[2]


Apart from the above mentioned correspondence methods, DEPQ's can be obtained efficiently using interval heaps.[3]

Interval Heaps[edit]

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[3] :

  • The left element is less than or equal to the right element.
  • Both the elements define a closed interval.
  • Interval represented by any node except the root is a sub-interval of the parent node.
  • Elements on the left hand side define a min heap.
  • Elements on the right hand side define a max heap.

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

  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 [p,q].
  2. Odd number of elements - In this case, each node except the last contains two elements represented by the interval [p,q] whereas the last node will contain a single element and is represented by the interval [p,p].
Implementing a DEPQ using interval heap.

Inserting an element[edit]

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

  1. Odd number of elements:If the number of elements in the interval heap is odd, the new element is firstly inserted in the last node. Then, it is successively compared with the previous node elements and tested to satisfy the criteria essential for an interval heap as stated above. In case if the element does not satisfy any of the criteria, it is moved from the last node to the root until all the conditions are satisfied.[3]
  2. Even number of elements::If the number of elements is even, then for the insertion of a new element an additional node is created. If the element falls to the left of the parent interval, it is considered to be in the min heap and if the element falls to the right of the parent interval, it is considered in the max heap. Further, it is compared successively and moved from the last node to the root until all the conditions for interval heap are satisfied. If the element lies within the interval of the parent node itself, the process is stopped then and there itself and moving of elements does not take place.[3]

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

Deleting an element[edit]

  • Min element: In an interval heap, the minimum element is the element on the left hand side of the root node. This element is removed and returned. To fill in the vacancy created on the left hand side of the root node, an element from the last node is removed and reinserted into the root node. This element is then compared successively with all the left hand elements of the descending nodes and the process stops when all the conditions for an interval heap are satisfied.In case if the left hand side element in the node becomes greater than the right side element at any stage, the two elements are swapped[3] and then further comparisons are done. Finally, the root node will again contain the minimum element on the left hand side.
  • Max element: In an interval heap, the maximum element is the element on the right hand side of the root node. This element is removed and returned. To fill in the vacancy created on the right hand side of the root node, an element from the last node is removed and reinserted into the root node. Further comparisons are carried out on a similar basis as discussed above. Finally, the root node will again contain the max element on the right hand side.

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[3] from an interval heap where the elements of the interval heap are the priorities of elements in the DEPQ.

Time Complexity[edit]

Calculated Time Complexities with references.

Interval Heaps[edit]

When DEPQ's are implemented using Interval heaps, the time complexities for the various functions are formulated in the table below[2]

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

Pairing heaps[edit]

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

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

Applications[edit]

Research[edit]

Proposed[edit]

  • A graphical representation of how DEPQ's work.
  • An animation.

References[edit]

  1. ^ Fundamentals of Data Structures in C++ - Ellis Horowitz , Sartaj Sahni and Dinesh Mehta
  2. ^ a b c d e f g http://www.cise.ufl.edu/~sahni/dsaaj/enrich/c13/double.htm
  3. ^ a b c d e f g http://www.mhhe.com/engcs/compsci/sahni/enrich/c9/interval.pdf