+ All Categories
Home > Documents > Priority Queues

Priority Queues

Date post: 18-Jan-2016
Category:
Upload: mireya
View: 22 times
Download: 0 times
Share this document with a friend
Description:
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() - PowerPoint PPT Presentation
21
Priority Queues
Transcript
Page 1: Priority Queues

Priority Queues

Page 2: Priority Queues

• 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

What is a Priority Queue?

Page 3: Priority Queues

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

Page 4: Priority Queues

Example

• insertItem(5, A)• insertItem(9, C)• insertItem(3, B)• insertItem(7, D)• minElement()• minKey()• removeMin()

• size()• minElement()• removeMin()• removeMin()• removeMin()• removeMin()• isEmpty()

Page 5: Priority Queues

Implementation

• Using an array?

• Using a linked list?

• Using a binary search tree?

• Running time of insertItem/removeMin?

Page 6: Priority Queues

Heaps

• A heap is a priority queue implemented with a binary tree

Page 7: Priority Queues

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

Page 8: Priority Queues

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?

Page 9: Priority Queues

Example Heap

7 9

4

56 101115

root

Page 10: Priority Queues

Heap Implementation

• Insertion algorithm

7 9

4

56 101115

root

5new_node

Page 11: Priority Queues

Up-Heap Bubbling

while new node is smaller than its parentmove parent down

• Running time?

Page 12: Priority Queues

Heap Implementation

• Deletion algorithm

5 9

4

56 10117

root

15

delete 4

Page 13: Priority Queues

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

Page 14: Priority Queues

Vector Implementation

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

• Implementation of insert and remove?

0 1 2 3 4 5 6 7

Page 15: Priority Queues

Heap Sort

• Algorithm to use a heap to sort a list of numbers

• Running time?

Page 16: Priority Queues

BuildHeap Algorithm

• Goal: Insert N items into initially empty heap

• Algorithm 1: Perform N inserts– Worst-case running time?

Page 17: Priority Queues

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);

Page 18: Priority Queues

Running Time

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

15 9

56

7 10114

root

Page 19: Priority Queues

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

h

• Sum S = ∑ 2i(h-i) i=0

Page 20: Priority Queues

BuildHeap Proof

• S = h+2(h-1)+4(h-2) +...+2h-1(1) + 2h(0)

• 2S = 2h + 4(h-1) + 8(h-2)+ ... +2h(1)

• 2S-S = -h + 2 + 4 + 8 + ... + 2h-1 + 2h

• = 2h+1 -1 - 1 - h

• = 2h+1 -1 - (h+1)

• = < 2N (N = 2h -> 2h+1)

• = O(N)

Page 21: Priority Queues

Event Simulation

• Bank simulation using priority queues?


Recommended