You are on page 1of 44

PRIORITY QUEUES

(HEAPS)
Priority Queue
 essentially a list of items in which each item
has associated with it a priority
 items are inserted into a priority queue in any,
arbitrary order
 items are withdrawn from a priority queue in
order of their priorities starting with the
highest priority item first
Model
Priority Queue
 A priority queue is a container which provides
the following three operations:
 enqueue
 used to put objects into the container;
 findMin
 returns the smallest object in the container; and
 dequeueMin
 removes the smallest object from the container.
Priority Queue
 Or in other implementations:
 enqueue
 used to put objects into the container;
 deleteMin
 finds,
returns, and removes the minimum element in
the priority queue.
Priority Queue
 A priority queue is used to store a finite set of
keys drawn from a totally ordered set of
keys K
 duplicate keys are allowed in priority queues
Applications
 Operating systems
 Sorting
 Greedy algorithm implementations
Simple Implementations
 Linked list
 Insertin front at O(1)
 Delete minimum by traversing the list at O(N)

 Linked list (always sorted)


 Insertionswill be O(N)
 Delete minimum at O(1)
Simple Implementations
 Binary Search Trees
 Averagerunning time of O(log N) for both
operations
Binary Heap
 Or heaps
 Properties:
 Structure
property, and
 Heap order property
Heap: Structure Property
 A heap is a binary tree that is completely
filled, with the possible exception at the
bottom level, which is filled from left to right
 Complete binary tree
Heap: Structure Property
 A complete binary tree
Heap: Structure Property
 An array representation of the binary tree
Heap Order Property
 Allows operations to be performed quickly
 Minimum element can always be found in the
root

 Consider that any subtree should also be a


heap (any node should be smaller than its
descendants)
Heap Order Property
 In a heap, for every node X, the key in the
parent of X is smaller than or equal to the key
in X, with the exception of the root (which has
no parent).

 findMin → constant time!


Heap Order Property
 Which tree is a heap?
Basic Heap Operations
 ensuring that the heap-order property is
maintained

 Insert
Basic Heap Operation: Insert
 General Strat
 Percolate up
 New element percolated up until correct location is
found

 Orperform repeated swaps until correct order is


established
 But swapping requires 3 assignment
Basic Heap Operations
 Insert
Basic Heap Operation: Insert
 To insert 14

Create a hole
Basic Heap Operation: Insert
 To insert 14

Bubble up the hole


Basic Heap Operation: Insert
 To insert 14

Bubble up the hole


Basic Heap Operation: Insert
 To insert 14

Insert 14
Basic Heap Operations
 deleteMin
 Find
minimum - easy!
 Removing it – hard
 A hole is created in removing the root
 Last element X in the heap must move somewhere

 Can we put X in the root?


Basic Heap Operations
Basic Heap Operations
Basic Heap Operations
 Delete 13
Basic Heap Operations
 Delete 13

Hole in the root

Last element X
Basic Heap Operations
 Delete 13

Move 14 up

Slide the smaller of the


hole’s children into the
hole

Move hole one level down

Repeat until X can be


placed in the hole
Basic Heap Operations
 Delete 13

Move 19 up
Basic Heap Operations
 Delete 13

Move 26 up
Basic Heap Operations
 Delete 13

Worst case running time


O(log N)

Insert 31
Other operation: buildHeap
Other operation: buildHeap

Uses this method


Other operation: buildHeap
 Initial heap
Each dashed line corresponds
to two comparisons

1. Find the smaller child


2. Compare smaller child
with the node
Other operation: buildHeap

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
150 80 40 30 10 70 110 100 20 90 60 50 120 140 130
Other operation: buildHeap
 percolateDown(7)
Other operation: buildHeap
 percolateDown(6)
Other operation: buildHeap
 percolateDown(5)
Other operation: buildHeap
 percolateDown(4)
Other operation: buildHeap
 percolateDown(3)
Other operation: buildHeap
 percolateDown(2)
Other operation: buildHeap
 percolateDown(1)
Applications of Priority Queues
 The selection problem
 findthe kth largest element
 Input is a list of N elements and an integer k.

You might also like