Priority Queues. What is a Priority Queue?. Container of elements where each element has an associated key A key is an attribute that can identify rank or weight of an element Examples â€“ passenger, todo list. Priority Queue ADT Operations. size() isEmpty()

Download Presentation

Priority Queues

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

Container of elements where each element has an associated key

A key is an attribute that can identify rank or weight of an element

Examples â€“ passenger, todo list

Priority Queue ADT Operations

size()

isEmpty()

insertItem(k, e) â€“ insert element e with key k

minElement() â€“ return ref to min element

minKey() â€“ return const ref to smallest key

removeMin() â€“ remove and return element with smallest key

insertItem(5, A)

insertItem(9, C)

insertItem(3, B)

insertItem(7, D)

minElement()

minKey()

removeMin()

size()

minElement()

removeMin()

removeMin()

removeMin()

removeMin()

isEmpty()

Example

Implementation

Using an array?

Using a linked list?

Using a binary search tree?

Running time of insertItem/removeMin?

Heaps

A heap is a priority queue implemented with a binary tree

Heaps

Heap-Order Property: In a heap T, for every node v other than the root, the key stored at v is greater than or equal to the key stored at vâ€™s parent

Heaps

Complete Binary Tree Property: A heap T with height h is a complete binary tree, that is, levels 0,1,2,â€¦,h-1of T have the maximum number of nodes possible and all the internal nodes are to the left of the external nodes in level h-1.

What does this give us?

Example Heap

root

4

9

7

56

10

15

11

Heap Implementation

Insertion algorithm

new_node

5

root

4

9

7

56

10

15

11

Up-Heap Bubbling

while new node is smaller than its parent

move parent down

Running time?

Heap Implementation

Deletion algorithm

delete 4

root

4

9

5

56

10

7

11

15

Down-Heap Bubbling

if right child is null

if left child is null

insert

else if left child is smaller than current

move left up

if both children not null

move up smallest child

Vector Implementation

Children are positions index*2 and index*2+1

Implementation of insert and remove?

0 1 2 3 4 5 6 7

Heap Sort

Algorithm to use a heap to sort a list of numbers

Running time?

BuildHeap Algorithm

Goal: Insert N items into initially empty heap

Algorithm 1: Perform N inserts

Worst-case running time?

Efficient BuildHeap

Idea: Start at level h-1 and bubble down nodes 1 at a time

for(int i = currentSize/2; i > 0; i--)

percolateDown(i);

Running Time

Running time is no more than the sum of the heights of all nodes

root

56

9

15

7

10

4

11

BuildHeap Proof

Theorem: For the perfect binary tree of height h containing 2h+1-1 nodes, the sum of the heights of the nodes is 2h+1-1-(h+1)

Proof: 1 node at height h, 2 nodes at height h-1, 22 nodes at height h-1, 2i nodes at height h-i