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