CS 1501 www.cs.pitt.edu/~nlf4/cs1501/ Priority Queues We mentioned - - PowerPoint PPT Presentation

cs 1501
SMART_READER_LITE
LIVE PREVIEW

CS 1501 www.cs.pitt.edu/~nlf4/cs1501/ Priority Queues We mentioned - - PowerPoint PPT Presentation

CS 1501 www.cs.pitt.edu/~nlf4/cs1501/ Priority Queues We mentioned priority queues in building Huffman tries Primary operations they needed: Insert Find item with highest priority E.g., findMin() or findMax() Remove


slide-1
SLIDE 1

CS 1501

www.cs.pitt.edu/~nlf4/cs1501/

Priority Queues

slide-2
SLIDE 2
  • Primary operations they needed:

○ Insert ○ Find item with highest priority ■ E.g., findMin() or findMax() ○ Remove an item with highest priority ■ E.g., removeMin() or removeMax()

  • How do we implement these operations?

○ Simplest approach: arrays

We mentioned priority queues in building Huffman tries

2

slide-3
SLIDE 3
  • Insert:

○ Add new item to the end of the array ○ Θ(1)

  • Find:

○ Search for the highest priority item (e.g., min or max) ○ Θ(n)

  • Remove:

○ Search for the highest priority item and delete ○ Θ(n)

  • Runtime for use in Huffman tree generation?

Unsorted array PQ

3

slide-4
SLIDE 4
  • Insert:

○ Add new item in appropriate sorted order ○ Θ(n)

  • Find:

○ Return the item at the end of the array ○ Θ(1)

  • Remove:

○ Return and delete the item at the end of the array ○ Θ(1)

  • Runtime for use in Huffman tree generation?

Sorted array PQ

4

slide-5
SLIDE 5
  • What about a binary search tree?

○ Insert ■ Average case of Θ(lg n), but worst case of Θ(n) ○ Find ■ Average case of Θ(lg n), but worst case of Θ(n) ○ Remove ■ Average case of Θ(lg n), but worst case of Θ(n)

  • OK, so in the average case, all operations are Θ(lg n)

○ No constant time operations ○ Worst case is Θ(n) for all operations

So what other options do we have?

5

slide-6
SLIDE 6
  • Our find and remove operations only need the highest

priority item, not to find/remove any item

○ Can we take advantage of this to improve our runtime? ■ Yes!

Is a BST overkill?

6

slide-7
SLIDE 7
  • A heap is complete binary tree such that for each node T in

the tree:

○ T.item is of a higher priority than T.right_child.item ○ T.item is of a higher priority than T.left_child.item

  • It does not matter how T.left_child.item relates to

T.right_child.item

○ This is a relaxation of the approach needed by a BST

The heap

The heap property

7

slide-8
SLIDE 8
  • Find is easy

○ Simply the root of the tree ■ Θ(1)

  • Remove and insert are not quite so trivial

○ The tree is modified and the heap property must be maintained

Heap PQ runtimes

8

slide-9
SLIDE 9
  • Add a new node at the next available leaf
  • Push the new node up the tree until it is supporting the

heap property

Heap insert

9

slide-10
SLIDE 10

Min heap insert

7 42 37 5 5 42 7 5 8 15 15 37 12 15 12 9 9 42 3 3 9 3 7 3 5 Insert: 7, 42, 37, 5, 8, 15, 12, 9, 3

10

slide-11
SLIDE 11
  • Tricky to delete root…

○ So let's simply overwrite the root with the item from the last leaf and delete the last leaf ■ But then the root is violating the heap property…

  • So we push the root down the tree until it is supporting the

heap property

Heap remove

11

slide-12
SLIDE 12

Min heap removal

8 37 15 12 42 9 7 3 5 9 9 5 9 8

NO!

7 9 42 7 42 8 42

12

slide-13
SLIDE 13
  • Find

○ Θ(1)

  • Insert and remove

○ Height of a complete binary tree is lg n ○ At most, upheap and downheap operations traverse the height of the tree ○ Hence, insert and remove are Θ(lg n)

Heap runtimes

13

slide-14
SLIDE 14
  • Simply implement tree nodes like for BST

○ This requires overhead for dynamic node allocation ○ Also must follow chains of parent/child relations to traverse the tree

  • Note that a heap will be a complete binary tree…

○ We can easily represent a complete binary tree using an array

Heap implementation

14

slide-15
SLIDE 15
  • Number nodes row-wise starting at 0
  • Use these numbers as indices in the array
  • Now, for node at index i

○ parent(i) = ⌊(i - 1) / 2⌋ ○ left_child(i) = 2i + 1 ○ right_child(i) = 2i + 2

Storing a heap in an array

For arrays indexed from 0

15

slide-16
SLIDE 16

Heap Sort

  • Heapify the numbers

○ MAX heap to sort ascending ○ MIN heap to sort descending

  • "Remove" the root

○ Don’t actually delete the leaf node

  • Consider the heap to be from 0 .. length - 1
  • Repeat

3 5 12 7 8 37 15 42 9 3 9 9 5 9 7 5 42 7 42 8 42 7 15 15 8 9 15 37 8 37 9 37 15 42 9 12 42 37 12 37 15 42 15 37 42 42 37

16

slide-17
SLIDE 17
  • Runtime:

○ Worst case: ■ n log n

  • In-place?

○ Yes

  • Stable?

○ No

Heap sort analysis

17

slide-18
SLIDE 18
  • Let's say I'm shopping for a new video card and want to

build a heap to help me keep track of the lowest price available from different stores.

  • Keep objects of the following type in the heap:

Indirection example setup

class CardPrice implements Comparable<CardPrice>{ public String store; public double price; public CardPrice(String s, double p) { … } public int compareTo(CardPrice o) { if (price < o.price) { return -1; } else if (price > o.price) { return 1; } else { return 0; } } }

18

slide-19
SLIDE 19
  • What if we want to update an Object?

○ What is the runtime to find an arbitrary item in a heap? ■ Θ(n) ■ Hence, updating an item in the heap is Θ(n) ○ Can we improve on this? ■ Back the PQ with something other than a heap? ■ Develop a clever workaround?

Storing Objects in PQ

19

slide-20
SLIDE 20
  • Maintain a second data structure that maps item IDs to each

item’s current position in the heap

  • This creates an indexable PQ

Indirection

20

slide-21
SLIDE 21

"NE":0

Indirection example

  • n = new CardPrice("NE", 333.98);
  • a = new CardPrice("AMZN", 339.99);
  • x = new CardPrice("NCIX", 338.00);
  • b = new CardPrice("BB", 349.99);

n a x b Indirection "AMZN":1 "NCIX":2 "BB":3

  • Update price for NE: 340.00

n x "NE":2 "NCIX":0

  • Update price for NCIX: 345.00

a x "AMZN":0 "NCIX":1

  • Update price for BB: 200.00

x b "BB":1 "NCIX":3 "AMZN":1 "BB":0 a b

21