{
  "id": "enwiki-001369226956",
  "slug": "priority-queue",
  "title": "Priority queue",
  "qid": "",
  "lead": "In computer science, a priority queue is an abstract data type similar to a regular queue where each element has an associated priority determining its order of service. Priority queue serves highest priority items first. Priority values have to be instances of an ordered data type, and higher priority can be given either to the lesser or to the greater values with respect to the given order relation. For example, in the Java standard library, the PriorityQueue class considers the lowest element with respect to their order as having the highest priority. 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.",
  "body": "In computer science, a priority queue is an abstract data type similar to a regular queue where each element has an associated priority determining its order of service. Priority queue serves highest priority items first. Priority values have to be instances of an ordered data type, and higher priority can be given either to the lesser or to the greater values with respect to the given order relation. For example, in the Java standard library, the PriorityQueue class considers the lowest element with respect to their order as having the highest priority. 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. ===Specialized heaps=== There are several specialized heap data structures that either supply additional operations or outperform heap-based implementations for specific types of keys, specifically integer keys. Suppose the set of possible keys is \\{1, 2, ..., C\\} . When only insert , find-min and extract-min are needed and in case of integer priorities, a bucket queue can be constructed as an array of C linked lists plus a pointer \\text{top} , initially C . Inserting an item with key k appends the item to the k 'th list, and updates \\text{top} \\gets \\text{min}(\\text{top}, k) , both in constant time. extract-min deletes and returns one item from the list with index \\text{top} , then increments \\text{top} if needed until it again points to a non-empty list; this takes O(C) time in the worst case. These queues are useful for sorting the vertices of a graph by their degree. A van Emde Boas tree supports the minimum , maximum , insert , delete , search , extract-min , extract-max , predecessor and successor] operations in O(\\log \\log C) time, but has a space cost for small queues of about O(2^{m/2}) , where m is the number of bits in the priority value. The space can be reduced significantly with hashing. The Fusion tree by Fredman and Willard implements the minimum operation in O(1) time and insert and extract-min operations in O(\\log n / \\log \\log C) time. However it is stated by the author that, \"Our algorithms have theoretical interest only; The constant factors involved in the execution times preclude practicality.\" For applications that do many \"peek\" operations for every \" extract-min \" operation, the time complexity for peek actions can be reduced to O(1) in all tree and heap implementations by caching the highest priority element after every insertion and removal. For insertion, this adds at most a constant cost, since the newly inserted element is compared only to the previously cached minimum element. For deletion, this at most adds an additional \"peek\" cost, which is typically cheaper than the deletion cost, so overall time complexity is not significantly impacted. Monotone priority queues are specialized queues that are optimized for the case where no item is ever inserted that has a lower priority (in the case of min-heap) than any item previously extracted. This restriction is met by several practical applications of priority queues. ===Summary of running times=== .NET's library contains a System.Collections.Generic.PriorityQueue class, which implements an array-backed, quaternary min-heap. Scala's library contains a class, which implements a max-priority-queue. Go's library contains a module, which implements a min-heap on top of any compatible data structure. Rust's standard library contains a struct, which implements a priority queue with a binary heap. The Standard PHP Library extension contains the class . Apple's Core Foundation framework contains a structure, which implements a min-heap. Parallelization can be used to speed up priority queues, but requires some changes to the priority queue interface. The reason for such changes is that a sequential update usually only has O(1) or O(\\log n) cost, and there is no practical gain to parallelize such an operation. One possible change is to allow the concurrent access of multiple processors to the same priority queue. The second possible change is to allow batch operations that work on k elements, instead of just one element. For example, extractMin will remove the first k elements with the highest priority. In this setting, operations on a priority queue is generalized to a batch of k elements. For instance, k_extract-min deletes the k smallest elements of the priority queue and returns those. In a shared-memory setting, the parallel priority queue can be easily implemented using parallel binary search trees and join-based tree algorithms. In particular, k_extract-min corresponds to a split on the binary search tree that has O(\\log n) cost and yields a tree that contains the k smallest elements. k_insert can be applied by a union of the original priority queue and the batch of insertions. If the batch is already sorted by the key, k_insert has O(k\\log (1+\\frac{n}{k})) cost. Otherwise, we need to first sort the batch, so the cost will be O(k\\log (1+\\frac{n}{k})+k\\log k)=O(k\\log n) . Other operations for priority queue can be applied similarly. For instance, k_decrease-key can be done by first applying difference and then union , which first deletes the elements and then inserts them back with the updated keys. All these operations are highly parallel, and the theoretical and practical efficiency can be found in related research papers. The rest of this section discusses a queue-based algorithm on distributed memory. We assume each processor has its own local memory and a local (sequential) priority queue. The elements of the global (parallel) priority queue are distributed across all processors. A k_insert operation assigns the elements uniformly random to the processors which insert the elements into their local queues. Note that single elements can still be inserted into the queue. Using this strategy the global smallest elements are in the union of the local smallest elements of every processor with high probability. Thus each processor holds a representative part of the global priority queue. This property is used when k_extract-min is executed, as the smallest m elements of each local queue are removed and collected in a result set. The elements in the result set are still associated with their original processor. The number of elements m that is removed from each local queue depends on k and the number of processors p . By parallel selection the k smallest elements of the result set are determined. With high probability these are the global k smallest elements. If not, m elements are again removed from each local queue and put into the result set. This is done until the global k smallest elements are in the result set. Now these k elements can be returned. All other elements of the result set are inserted back into their local queues. The running time of k_extract-min is expected O(\\frac{k}{p} \\log(n)) , where k = \\Omega(p \\cdot \\log (p)) and n is the size of the priority queue. The priority queue can be further improved by not moving the remaining elements of the result set directly back into the local queues after a k_extract-min operation. This saves moving elements back and forth all the time between the result set and the local queues. By removing several elements at once a considerable speedup can be reached. But not all algorithms can use this kind of priority queue. Dijkstra's algorithm for example can not work on several nodes at once. The algorithm takes the node with the smallest distance from the priority queue and calculates new distances for all its neighbor nodes. If you would take out k nodes, working at one node could change the distance of another one of the k nodes. So using k-element operations destroys the label setting property of Dijkstra's algorithm.",
  "categories": [
    "10639524",
    "23603337",
    "26220375",
    "26851623",
    "55739975",
    "63697623"
  ],
  "license": "CC BY-SA 4.0",
  "source_uri": "https://en.wikipedia.org/w/index.php?oldid=1369226956",
  "content_hash": "",
  "created_at": null,
  "updated_at": 1786634373.0,
  "claims": [],
  "contradictions": [],
  "confidence": 0.6,
  "csl": {
    "id": "enwiki-001369226956",
    "type": "webpage",
    "title": "Priority queue",
    "author": [
      {
        "family": "Anticloud FZ LLE",
        "literal": "Anticloud FZ LLE"
      }
    ],
    "container-title": "encyclopegia.org",
    "publisher": "Anticloud FZ LLE",
    "publisher-place": "Dubai, United Arab Emirates",
    "URL": "https://encyclopegia.org/a/priority-queue",
    "issued": {
      "date-parts": [
        [
          2026,
          11,
          18
        ]
      ]
    },
    "accessed": {
      "date-parts": [
        [
          2026,
          11,
          18
        ]
      ]
    },
    "license": "CC BY-SA 4.0",
    "DOI": "eo1:enwiki-001369226956",
    "isBasedOn": "https://en.wikipedia.org/w/index.php?oldid=1369226956",
    "note": "Immutable snapshot enwiki-001369226956"
  }
}