Introduction to Algorithms learning note 6.5 Priority queue

Source: Internet
Author: User

Priority queue is a data structure that maintains a set of sets of elements, each of which has a related value, called a keyword (key). One of the largest priority queues supports the operation:

Insert (S, X): Inserts the element x into the set S.

MAXIMUM (s): Returns the collection with the largest keyword in S.

Extract-max (s): Removes and returns the element with the largest keyword in S.

Increase-key (S, X, K): Increments the key value of element x to K, assuming that the value of K is not less than the original key value of X.

There are many applications for the maximum priority queue, one of which is job scheduling on a shared computer system. The maximum priority queue records the individual jobs that will be executed and the relative priority between them. When a job is completed or interrupted, the scheduler selects the job with the highest priority to execute.

Obviously, the priority queue can be implemented with heaps. For an application such as a job scheduler or event-driven emulator, the priority queue element corresponds to an object in the application. We need to determine which object corresponds to a given priority queue element, and vice versa. When using a heap to implement a priority queue, you need to store a handle to the corresponding object in each element of the heap. Similarly, in an application, you also need to store a handle to the corresponding element in a heap. Typically, this handle is the subscript of the array element.

The procedure heap-maximum can implement the maximum operation in O (1) Time (pseudo code below).

Heap-maximum (A) 1    return a[1]
The procedure Heap-extract-max implements the Extract-max operation. This process is similar to the For loop body part in the Heapsort process:

Heap-extract-max (A) 1      if a.heap-size <                 error "heap underflow" 3      MAX = a[1]4      a[1] = a[a.heap-size]5< C4/>a.heap-size = a.heap-size-16      max-heapify (A, 1) 7      return MAX
The time complexity of Heap-extract-max is (LGN). Because in addition to the time complexity of O (LGN) max-heapify has been, the other operations are constant order. (To be Continued ...) )

Introduction to Algorithms learning note 6.5 Priority queue

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.