You are on page 1of 27

Data Structures and Algorithms

Priority Queues Sundaresan Raman


Process Scheduling in an Operating System
• In a multiuser computer system, multiple users submit jobs to run on a single processor.
• The processor is capable of running only one job at a time.
• Assume that the time required by each job is known in advance. Further, jobs can be
preempted (stopped and resumed later).

Processor

Job1 Job2 Job3 Jobn

A pool of jobs submitted by multiple users waiting for the processor


*A few jobs are partially complete and are waiting for the processor to get completed.
Process Scheduling in an Operating System
SRPT Scheduling Policy
• SRPT (Shortest Remaining Processing Time) is a policy that minimizes the
average waiting time of the processes.
• The processor schedules the job with the smallest remaining processing time.
• While a job is running, if a new job arrives with processing time less than the
remaining time of the current job, the current job is preempted.
• Goes back to the pool without completion.
Data Structure for SRPT
• Need to maintain the remaining processing time of the unfinished jobs at
any point in time.
• Need to find the job with the shortest remaining processing time.
• When a job finishes, remove it from the collection/pool.
• When a new job arrives, need to add it to the collection/pool.
Priority Queues
• Priority Queue (PQ) is an ADT for maintaining a set S of elements, each with
an associated value called priority.
• A PQ supports the following operations:
• Insert(x) insert element x in set S (SS{x})
• Minimum() returns the element of S with smallest priority
• Delete-min() returns and removes the element of S with smallest priority
Priorities in Priority Queues
• A PQ ranks its elements by priority.
• Every element has a priority. Priorities are not necessarily unique and are
totally ordered.
• Total order relation, denoted by 
• Reflexive: k  k
• Antisymmetric: if k1  k2 and k2  k1, then k1 = k2
• Transitive: if k1  k2 and k2  k3, then k1  k3
Priority Queue: Applications
• Priority Scheduling
• Event driven simulation
• As a building block for other algorithms
• Huffman encoding
• Dijkstra’s single source shortest paths
• Prim’s minimal spanning tree
• A heap can be used to implement a priority queue
Heaps
A heap is a binary tree that stores
priorities (or priority-element) pairs at
nodes. 11

• Structural property: All levels


except the last are full. The last 17 13
level is left-filled.
• Heap property: The priority of the
18 21 19 17
node is at least as large as that of
its parent.
43 23 26 29 31
Examples of Non-Heaps

Heap property
violated 11

19 13

18 21 19 17

43 23 26 29 31
Examples of Non-Heaps

Last level not


left-filled 11

17 13

18 21 19 17

43 26 29 31
Finding the Minimum Element
• The element with the smallest Minimum
priority always sits at the root of the element
heap.
• This is because if it were elsewhere, 11
it would have a parent with larger
priority, which would violate the heap
17 13
property.
• Hence, minimum() can be done in
O(1) time. 18 21 19 17

43 23 26 29 31
Height of a Heap
• Suppose a heap of n nodes has
height h.
• Recall: perfect binary tree of height 11
h has 2h+1 − 1 nodes.
• Hence, 2h-1 < n ≤ 2h+1 − 1. 17 13
• h = log2 n

18 21 19 17

43 23 26 29 31
Implementing Heaps

• Using array A[1:n] for storing 11


a heap with n nodes
17 13
• Parent (i)
return i/2 18 21 19 17
• Left (i)
return 2i 43 23 26

• Right (i) 1 2 3 4 5 6 7 8 9 10
return 2i+1
A 11 17 13 18 21 19 17 43 23 26
Level: 0 1 2 3
Heap property: A[i.parent] <= A[i]
Implementing Heaps
• Notice the implicit tree
links; children of node i 11
are 2i and 2i + 1
17 13
• Why is this useful?
18 21 19 17
• In a binary
representation, a
43 23 26
multiplication/division
by two is left/right shift.
1 2 3 4 5 6 7 8 9 10
• Adding 1 can be done
by adding the lowest A 11 17 13 18 21 19 17 43 23 26
bit. Level: 0 1 2 3
Insertion in a Heap
• Insert 12 • Only the nodes in the path from the
root to the newly inserted node are
• Insert 8
affected in the insertion process.
11 • The process of shifting the nodes is
required to restore the heap
17 13 property if it gets disturbed.

18 21 19 17

43 23 26 29 31 12 8
Delete-Min in a Heap
• The minimum element is the one at the top of the heap.
• Can be deleted and one of its children can be moved up to fill the space.
• Empty location moves down the tree.
• Might end up at any position on last level.
• Resulting tree would not be left filled.
Delete-Min in a Heap

10 11

18 21 13 12

43 23 26 29 31 19 17

Moving down the space created at the root would not necessarily create a heap
Delete-Min in a Heap

10 11

16 21 13 12

43 23 26 29 31 19 17

• Replace top element with last element of heap.


• Heapify(1)
Heapify
• i is index into the array A.
• Binary trees rooted at Left(i) and Right(i) are heaps.
• But, A[i] might be larger than its children, thus violating the heap property.
• The method Heapify makes binary tree rooted at i, a heap by moving A[i]
down the heap.
Heapify Illustration
• Heap property violated at node
with index 1 but subtrees rooted
at 2, 3 are heaps. 17
• Heapify(1)
• This restores the heap property! 10 11

16 21 13 12

43 23 26 29 31 19
Running Time Analysis
• In Heapify, the element might be moved all the way to the last level.
• Hence Heapify requires O(log n) time.
• Delete-Min
• Replaces root node with last element- O(1) time
• Employs Heapify(1) - O(log n) time complexity
Running Time Analysis:
• A heap of n nodes has height O(log n).
• While inserting you might have to move the element all the way to the top.
• Hence at most O(log n) steps required.
Building a Heap
• Given an array A of elements
• Start heapifying the elements of the 23
array from the bottom and move up
• All leaves are heaps to begin with
43 26
• Start heapifying upper levels of the
tree
10 21 13 31
BUILD-HEAP (A)
1 for i ← [n/2] downto 1
2 do HEAPIFY (A, i) 16 12 8 29 11 19 17
Building a Heap: Analysis
• Running time: n calls to Heapify = n O(lg n) = O(n lg n)
• Provide a better O(n) bound
• Intuition: for most of the time, Heapify works on smaller than n element heaps
• Proof: Exercise
Insert and Delete-Min (Complexities)
• Insert:
• O(log n) to insert down the heap + O(log n) to restore the heap property =
O(log n)
• Heapify:
• O(log n) (restores heap property)
• Delete-Min:
• O(1) to remove the top most element + O(log n) to restore the heap
property = O(log n)
• Find-Min:
• O(1)
Heap Sort
• Create a heap
• Do delete-min repeatedly till heap becomes empty
• The order in which the elements come out is the sorted order
• Store the elements in a separate array
• Time complexity: O(n log n)
• Extra space required: O(n)
• Can sort “in-place” as well with only O(1) extra space
Thank You!

You might also like