Algorithm-heap-based priority queue,

Source: Internet
Author: User

Algorithm-heap-based priority queue,

1. Specific Algorithms

Public class MaxPQ <Key> implements Iterable <Key> {private Key [] pq; // a full heap-based Binary Tree private int N; // stored in pq [1 .. in N], pq [0] does not use private Comparator <Key> comparator; // optional Comparator public MaxPQ (int initCapacity) {pq = (Key []) new Object [initCapacity + 1]; N = 0;}/*** whether the returned queue is empty */public boolean isEmpty () {return N = 0 ;} /*** return the number of elements in the priority queue */public int size () {return N;}/*** returns the maximum element */publ Ic Key max () {if (isEmpty () throw new NoSuchElementException ("Priority queue underflow"); return pq [1];} /*** insert an element into the priority queue */public void insert (Key x) {// double size of array if necessary if (N> = pq. length-1) resize (2 * pq. length); // add x, and percolate it up to maintain heap invariant pq [++ N] = x; swim (N); assert isMaxHeap ();} /*** Delete and return the maximum element */public Key delMax () {if (isEmpty () th Row new NoSuchElementException ("Priority queue underflow"); Key max = pq [1]; // obtain the maximum element exch (1, N --) from the root node --); // swap it with the last node sink (1); // restore the heap's orderliness pq [N + 1] = null; // prevent cross-border if (N> 0) & (N = (pq. length-1)/4) resize (pq. length/2); assert isMaxHeap (); return max;} private void swim (int k) {while (k> 1 & less (k/2, k )) {exch (k, k/2); k = k/2;} private void sink (int k) {while (2 * k <= N) {int j = 2 * K; if (j <N & less (j, j + 1) j ++; if (! Less (k, j) break; exch (k, j); k = j ;}}}

 2. Algorithm Analysis

The priority queue is represented by a full heap-based Binary Tree and stored in the array pq [1... N]. pq [0] is not used. In insert (), we add N together to add the new elements to the end of the array, and then use swim () to restore the heap order. In delMax (), we get the element to be returned from pq [1], then move pq [N] To pq [1], and subtract N with sink () restore the heap order. In addition, we also set the pq [N + 1] that is no longer in use to null, so that the system can reclaim the space occupied by it.

Proposition:For a heap-based priority queue containing N elements, the insert element operation only needs to be compared no more than (lgN + 1) times, and the operation to delete the maximum element must be compared no more than 2lgN times.

Proof:From the previous proposition, we can see that both operations need to move elements between the root node and the heap bottom, and the path length cannot exceed lgN. For each node in the path, the maximum element to be deleted needs to be compared twice (except for the heap bottom element), one for finding a large subnode, and one for determining whether the subnode needs to go up.

For typical applications that require a large number of mixed insert and delete operations on the largest element, the above proposition represents an important performance breakthrough. The initial implementation of priority queues using ordered or unordered arrays always requires a linear time to complete one of the operations, but the heap-based implementation can ensure that they are completed within the logarithm time. This difference allows us to solve problems that we previously could not solve.

The priority queue operations on the stack are as follows:

Source code download]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.