CS171 Introduction to Computer Science II Priority Queues and Binary - - PowerPoint PPT Presentation

cs171 introduction to computer science ii priority queues
SMART_READER_LITE
LIVE PREVIEW

CS171 Introduction to Computer Science II Priority Queues and Binary - - PowerPoint PPT Presentation

CS171 Introduction to Computer Science II Priority Queues and Binary Heap Priority Queues and Binary Heap Review Binary Search Trees (BST) Balanced search trees Hash tables Priority Queues Need to process/search an item with


slide-1
SLIDE 1

CS171 Introduction to Computer Science II Priority Queues and Binary Heap Priority Queues and Binary Heap

slide-2
SLIDE 2

Review

Binary Search Trees (BST) Balanced search trees Hash tables

slide-3
SLIDE 3
slide-4
SLIDE 4
slide-5
SLIDE 5

Priority Queues

Need to process/search an item with largest (smallest) key, but not necessarily full sorted

  • rder

Support two operations Support two operations

Remove maximum (or minimum) Insert

Similar to

Stacks (remove newest) Queues (remove oldest)

slide-6
SLIDE 6

Example

slide-7
SLIDE 7

Applications

Job scheduling

Keys corresponds to priorities of the tasks

Sorting algorithm

Heapsort Heapsort

Graph algorithms

Shortest path

Statistics

Maintain largest M values in a sequence

slide-8
SLIDE 8
slide-9
SLIDE 9
slide-10
SLIDE 10

Possible implementations

Sorting N items

Time: NlogN Space: N

Elementary PQ - Compare each new key Elementary PQ - Compare each new key against M largest seen so far

Time: NM Space: M

Using an efficient MaxPQ Implementation

slide-11
SLIDE 11
slide-12
SLIDE 12

Implementations

Elementary representations

Unordered array (lazy approach)

  • rdered array (eager approach)

Efficient implementation Efficient implementation

Binary heap structure

Can we implement priority queue using Binary Search Trees?

slide-13
SLIDE 13
slide-14
SLIDE 14

Sequence-based Priority Queue

Implementation with an unsorted list Performance: Implementation with a sorted list Performance:

4 5 2 3 1 1 2 3 4 5

14

Performance:

insert takes time since we can insert the item at the beginning or end of the sequence removeMin and min take time since we have to traverse the entire sequence to find the smallest key

Performance:

insert takes time since we have to find the place where to insert the item removeMin and min take time, since the smallest key is at the beginning

slide-15
SLIDE 15
slide-16
SLIDE 16
slide-17
SLIDE 17

Binary Heap Tree

A heap is a binary tree storing keys at its nodes and satisfying two properties:

Heap-Order: for every internal node v

  • ther than the root,

  • 17

≥ Complete Binary Tree: let be the height of the heap

for = − there are nodes

  • f depth

at depth − , the internal nodes are to the left of the external nodes The last node of a heap is the rightmost node of depth

  • last node
slide-18
SLIDE 18
slide-19
SLIDE 19

Height of a Heap

  • Theorem: A heap storing keys has height

Proof: (we apply the complete binary tree property)

Let be the height of a heap storing keys Since there are keys at depth = − and at least one key at depth , we have ≥ + + + + − + Thus, ≥ , i.e., ≤

19

Thus, ≥ , i.e., ≤

  • keys
  • depth
slide-20
SLIDE 20
slide-21
SLIDE 21
slide-22
SLIDE 22
slide-23
SLIDE 23

Insert/Remove and Maintaining Heap

  • rder

When a node’s key is larger than its parent key

Upheap (promote, swim)

When a node’s key becomes smaller than its children’s keys children’s keys

Downheap (demote, sink)

slide-24
SLIDE 24
slide-25
SLIDE 25
slide-26
SLIDE 26
slide-27
SLIDE 27
slide-28
SLIDE 28

Demo

slide-29
SLIDE 29
slide-30
SLIDE 30