ArticleslgStudy

science

Priority queue

Priority queue is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Priority queue rather than just read about it. In short: 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 queue — main illustration
Priority queue — illustration

Key takeaways

  • Priority queue belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Priority queue to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Priority queue from memory before moving on to harder problems.

Reference excerpt

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.

Operations A priority queue has the following operations:

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.

Implementation

Naive implementations One can create a simple, but inefficient priority queue in a number of ways. These naive implementations can demonstrate the expected behaviour of a priority queue in a simpler manner.

insert elements into an unsorted array; find and extract element with highest priority Performance: "insert" performs in O ( 1 ) {\displaystyle O(1)} constant time, where "extract_max" performs in O ( n ) {\displaystyle O(n)} linear time. insert(element, priority): node.element ← element node.priority ← priority list.append(node)

extract_max(): highest ← 0 foreach node in list: if highest.priority < node.priority: highest ← node list.remove(highest) return highest.element

insert elements into a sorted array; extract first element Performance: "insert" performs in O ( n ) {\displaystyle O(n)} linear time, where "extract_max" performs in O ( 1 ) {\displaystyle O(1)} constant time. insert(element, priority): node.element ← element node.priority ← priority for i in [0...N]: element ← list.get_at_index(i) if element.priority < node.priority: list.insert_at_index(node, i + 1) return

extract_max(): highest ← list.get_at_index(0) list.remove(highest) return highest.element

Usual implementation To improve performance, priority queues are typically based on a heap, giving O ( log ⁡ n ) {\displaystyle O(\log n)} performance for inserts and removals, and O ( n ) {\displaystyle O(n)} to build the heap initially from a set of n {\displaystyle n} elements. Variants of the basic heap data structure such as pairing heaps or Fibonacci heaps can provide better bounds for some operations. Alternatively, when a self-balancing binary search tree is used, insertion and removal also take O ( log ⁡ n ) {\displaystyle O(\log n)} time, although building trees from existing sequences of elements takes O ( n log ⁡ n ) {\displaystyle O(n\log n)} time; this is typical where one might already have access to these data structures, such as with third-party or standard libraries. From a space-complexity standpoint, using self-balancing binary search tree with linked list takes more storage, since it requires storing extra references to other nodes. From a computational-complexity standpoint, priority queues are congruent to sorting algorithms. The section on the equivalence of priority queues and sorting algorithms, below, describes how efficient sorting algorithms can create efficient priority queues.

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 } {\displaystyle \{1,2,...,C\}} .

… excerpt ends here. Continue reading the full article.

Illustrations

Priority queue: k_extract-min is executed on a priority queue with three processors. The green elements are returned and removed from the priority queue.
k_extract-min is executed on a priority queue with three processors. The green elements are returned and removed from the priority queue.

Worked examples

Example 1 — a first encounter with Priority queue

Start with the simplest possible case. Write down what Priority queue claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Priority queue before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Priority queue ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Priority queue

In research
Priority queue appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Priority queue in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Priority queue is common in secondary-school and first-year university syllabi. It links to neighbouring topics Abstract data types, Priority queues, so understanding it makes those chapters shorter.
In everyday life
Look for Priority queue outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Priority queue in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Priority queue means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Priority queue out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Priority queue in simple terms?

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.

Why does Priority queue matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Priority queue?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Priority queue.

Tags

  • Abstract data types
  • Priority queues

Keep exploring