CS 221

29
CS 221 Analysis of Algorithms Data Structures Priority Queues and Heaps

description

CS 221. Analysis of Algorithms Data Structures Priority Queues and Heaps. Portions of the following slides are from Goodrich and Tamassia, Algorithm Design: Foundations, Analysis and Internet Examples, 2002 and - PowerPoint PPT Presentation

Transcript of CS 221

Page 1: CS 221

CS 221

Analysis of AlgorithmsData StructuresPriority Queues and Heaps

Page 2: CS 221

Portions of the following slides are from

Goodrich and Tamassia, Algorithm Design: Foundations, Analysis and Internet Examples, 2002

and

Material provided by the publisher’s John Wiley & Son, Inc.) companion website for this book

Page 3: CS 221

Priority queues

Remember queues and stacks in both the removal of an element was

based on the order it was placed on the structure…

… but what if removal was based on other criteria

Page 4: CS 221

Priority queues

Priority queue removal based on priority value rather

than… the order it was placed on the structure,

or… even the value of the element (maybe)

Page 5: CS 221

Priority queues Key

each element to be place in a priority queue is a two part element the element value key – a value associated with the element

value Think of a take-a-number ticket for counter

service (like at DMV) (really a FIFO queue, right?)

You are the element value Your ticket number is your key

Page 6: CS 221

Priority queues

Key

Key ev

element

Key ev

Key ev

Key ev

Key ev

Key ev

Key ev

Page 7: CS 221

Priority queue ADT

A priority queue stores a collection of items

An item is a pair(key, element)

Main methods of the Priority Queue ADT insertItem(k, o)

inserts an item with key k and element o

removeMin()removes the item with smallest key and returns its element

Additional methods minKey()

returns, but does not remove, the smallest key of an item

minElement()returns, but does not remove, the element of an item with smallest key

size(), isEmpty() Applications:

Standby flyers Auctions Stock market

Page 8: CS 221

Priority queues

Need two more things Total Order Relation Comparator

to realize the Total Order Relation

Page 9: CS 221

Total Order Relation

Keys in a priority queue can be arbitrary objects on which an order is defined

Two distinct items in a priority queue can have the same key

Mathematical concept of total order relation Reflexive property:

x x Antisymmetric

property:x y y x x = y

Transitive property: x y y z x z

Page 10: CS 221

Comparator A comparator

encapsulates the action of comparing two objects according to a given total order relation

A generic priority queue uses an auxiliary comparator

The comparator is external to the keys being compared

When the priority queue needs to compare two keys, it uses its comparator

Methods of the Comparator ADT, all with Boolean return type isLessThan(x, y) isLessThanOrEqualTo(x,

y) isEqualTo(x,y) isGreaterThan(x, y) isGreaterThanOrEqualTo

(x,y) isComparable(x)

Page 11: CS 221

Priority Queue with an unordered sequence

How to implement? vector? linked list?

Page 12: CS 221

Priority Queue with an unordered sequence Selection-Sort

Input: unordered sequence Output: ordered sequence

Insert element at end of queue (first phase) O(?) remove elements in key order (second phase) Run-time? In other words-

How many steps does it take to execute removeMin(P) to remove the smallest element value?

How many times will you run removeMin(P)?

Page 13: CS 221

Priority Queue with an unordered sequence Selection-Sort First phase O(1) Second phase

O(n+(n-1)+(n-2)+…2+1) or O(Σi, i=1,n) or O(n(n+1)/2 so, O(n2)

Page 14: CS 221

Priority Queue with an ordered sequence Insertion-Sort

Input: unordered sequence Output: ordered sequence

Insert element in key order position O(?) remove elements in key order (second phase) Run-time? In other words-

How many steps does it take to execute removeMin(P) to remove the smallest element value?

How many times will you run removeMin(P)?

Page 15: CS 221

Priority Queue with an unordered sequence Selection-Sort First phase O(1)

O(n+(n-1)+(n-2)+…2+1) or O(Σi, i=1,n) or O(n(n+1)/2 so, O(n2)

Second phase O(1)

Page 16: CS 221

Heaps

Remember Trees especially binary tree we like trees, right? Why?

Page 17: CS 221

Heaps

Imagine a Priority queue but rather than linear vector or list implemented as a tree that is called a heap

Page 18: CS 221

Heaps We still need

Total Order Relation Comparators

We need a few more things Heap-Order Property

for every node v (except root) the key of v k(v) is greater than or equal to the key of its parent node

Complete Binary Tree – binary tree of height h where all levels (0…h-1) have the maximum number of nodes

Page 19: CS 221

Heaps Complete Binary Tree – what?

leaf (external nodes) hold no elements just placeholders all element values are in internal nodes

Now we need to keep track of the lastnode() The last node of a heap is the rightmost

internal node of depth h 1

Page 20: CS 221

Heaps

2

65

79

last node

Page 21: CS 221

Heaps So, let’s w=lastnode() z = insertion point in heap

first external node to the right of w this is where we will always add new elements (k,e)

2

65

79

w z

Page 22: CS 221

Heaps

Heap depth and number of keys

1

2

2h2

1

keys

0

1

h2

h1

depth

Page 23: CS 221

Heaps and Priority Queues We can use a heap to implement a priority queue We store a (key, element) item at each internal

node We keep track of the position of the last node For simplicity, we show only the keys in the pictures

(2, Sue)

(6, Mark)(5, Pat)

(9, Jeff) (7, Anna)

Page 24: CS 221

Insertion into a Heap (§2.4.3)

Method insertItem of the priority queue ADT corresponds to the insertion of a key k to the heap

The insertion algorithm consists of three steps Find the insertion node

z (the new last node) Store k at z and expand

z into an internal node Restore the heap-order

property (discussed next)

2

65

79

insertion node

2

65

79 1

z

z

Page 25: CS 221

Insertion - Upheap Bubbling

2

15

79 6z

1

25

79 6z

2

65

79 1z

Page 26: CS 221

removeMin() from heap

Page 27: CS 221

Removal from a Heap (§2.4.3) Method removeMin of

the priority queue ADT corresponds to the removal of the root key from the heap

The removal algorithm consists of three steps Replace the root key

with the key of the last node w

Compress w and its children into a leaf

Restore the heap-order property (discussed next)

2

65

79

last node

w

7

65

9w

Page 28: CS 221

7

65

9w

5

67

9w

Downheap Bubbling

2

65

79

7

65

9w

Page 29: CS 221