Lably
Skip to content

Priority queue

Abstract data type in computer science

  • 15 min read
  • 6 chapters

Related on Lably

Through Computer science, Java (programming language) and Linked list

While priority queues are often implemented using heaps, they are conceptually distinct. A priority queue can be implemented with a heap or with other methods; just as a list can be implemented with a linked list or with an array.

Operations

A priority queue has the following operations:

Max-priority queue

  • insert(S, element, priority): add an element to set S with an associated priority.
  • maximum(S): return the element with highest priority.
    This is also known as "find_max".
  • extract_max(S): remove the element from set S with highest priority, and return it.
    This is also known as "delete", or "extract".
  • increase_key(S, element, k): increase the associated priority with an element to the new value k.

Min-priority queue

  • insert(S, element, priority): add an element to set S with an associated priority.
  • minimum(S): return the element with lowest priority.
    This is also known as "find_min".
  • extract_min(S): remove the element from set S with lowest priority, and return it.
    This is also known as "delete", or "extract".
  • decrease_key(S, element, k): decrease the associated priority with an element to the new value k.

Stacks and queues can be implemented as particular kinds of priority queues, with the priority determined by the order in which the elements are inserted. In a stack, the priority of each inserted element is monotonically increasing; thus, the last element inserted is always the first retrieved. In a queue, the priority of each inserted element is monotonically decreasing; thus, the first element inserted is always the first retrieved.

In some implementations, if two elements have the same priority, they are served in the same order in which they were enqueued. In other implementations, the order of elements with the same priority is undefined.

Sign in to keep reading

5 more chapters are free with a Lably account.

More in Computing and communication