MCS-208 · Unit 5

Queues

Section 7 of 8

Block 2 · Unit 5Queues9 min · Section 7 of 8

5.7Priority Queues

Covers IGNOU MCS-208 §Programme guide topic "Priority Queues" (not in the Unit 5 booklet)

In a hospital emergency room, patients are not treated strictly in arrival order: the most serious case goes first. A priority queue is the data structure for this rule.

The idea

Operations: insert(Q, x, p) adds item x with priority p; delete_min(Q) (or delete_max) removes and returns the most urgent item; peek(Q) returns it without removing it.

Implementations

Implementationinsertdelete most urgentNotes
Unsorted array or listO(1)O(1): add at the endO(n)O(n): search for itGood when deletions are rare
Sorted linked listO(n)O(n): find the placeO(1)O(1): it is the first nodeSimple; keeps ties in FIFO order
Binary heap (array)O(log⁡n)O(\log n)O(log⁡n)O(\log n)The standard choice (Unit 9)
One queue per priority levelO(1)O(1)O(k)O(k) for kk levelsGood for a few fixed levels (OS scheduling)

Applications

  • CPU scheduling by process priority.
  • Shortest-path algorithms such as Dijkstra’s (Unit 8) repeatedly take the closest unvisited vertex.
  • Event-driven simulation: events are processed in order of their time.
  • Bandwidth management: urgent network packets (e.g. voice) are sent before bulk data.

Key takeaways

  • Priority queue: delete returns the highest-priority item; ties are usually FIFO.
  • Sorted list: insert O(n)O(n), delete O(1)O(1); unsorted list: insert O(1)O(1), delete O(n)O(n).
  • Binary heap: insert and delete both O(log⁡n)O(\log n).
  • Used in scheduling, Dijkstra’s algorithm, simulation and networking.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

What does delete_min return in a min-priority queue?

Show answer

Answer: C. An item with the smallest priority number

Priority, not arrival time, decides; arrival order only breaks ties.

Question 2 · multiple choice

A priority queue is a sorted linked list (most urgent first). What are the costs of insert and delete?

Show answer

Answer: B. O(n)O(n) and O(1)O(1)

Insertion must find the right place; deletion just removes the first node. O(log⁡n)O(\log n) for both is a heap.

Question 3 · multiple choice

Jobs are inserted in this order with priorities (1 = most urgent): P(2), Q(1), R(2), S(1). In what order are they served (ties FIFO)?

Show answer

Answer: D. Q S P R

Priority 1 first, in arrival order: Q, S; then priority 2 in arrival order: P, R. “Q S R P” breaks FIFO order among equal priorities.

Question 4 · multiple choice

Which implementation gives O(log⁡n)O(\log n) for both insert and delete?

Show answer

Answer: C. Binary heap

A heap keeps a partial order in an array; both operations move along one path of the tree, of length O(log⁡n)O(\log n).

Question 5 · multiple choice

What does this program print?

C · q5-pq.c
#include <stdio.h>

int main(void)
{
    int pri[5] = {4, 2, 5, 2, 1};      /* unsorted array priority queue */
    int n = 5;
    for (int round = 0; round < 3; round++) {
        int best = 0;
        for (int i = 1; i < n; i++)    /* O(n) search for the minimum */
            if (pri[i] < pri[best])
                best = i;
        printf("%d ", pri[best]);
        pri[best] = pri[--n];          /* remove it: move the last item in */
    }
    printf("\n");
    return 0;
}
Show answer

Answer: B. 1 2 2

Each round removes the current minimum: 1, then 2, then the other 2. Each deletion costs O(n)O(n) because the whole array is searched.

Question 6 · short answer

Explain how a priority queue differs from an ordinary queue and give two applications.

Show worked answer

An ordinary queue removes items in the order they were inserted (FIFO). A priority queue gives every item a priority and always removes an item with the highest priority (e.g. the smallest priority number); arrival order matters only between items of equal priority. Applications: (1) CPU scheduling, where higher-priority processes run first; (2) Dijkstra’s shortest-path algorithm, which repeatedly removes the vertex with the smallest distance; (3) event-driven simulation, which processes events in order of time.

↑ ↓ move↵ openTab switch type