0% found this document useful (0 votes)
33 views24 pages

Priority Queue Operations Explained

Uploaded by

Benjamin Bravo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views24 pages

Priority Queue Operations Explained

Uploaded by

Benjamin Bravo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CS 1501

Priority Queues
The Priority Searching Problem

Given a collection of items, and the ability to determine the


relative priority of any two items, return an item with the
highest priority

2
We mentioned priority queues in building Huffman tries

● 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

3
Unsorted array PQ

● Insert:
○ Add new item to the end of the array
○ O(1)
● Find:
○ Search for the highest priority item (e.g., min or max)
○ O(n)
● Remove:
○ Search for the highest priority item and delete
○ O(n)

● Runtime for use in Huffman tree generation?

4
Sorted array PQ

● Insert:
○ Add new item in appropriate sorted order
○ O(n)
● Find:
○ Return the item at the end of the array
○ O(1)
● Remove:
○ Return and delete the item at the end of the array
○ O(1)

● Runtime for use in Huffman tree generation?

5
So what other options do we have?

● What about a binary search tree?


○ Insert
■ Average case of O(lg n), but worst case of O(n)
○ Find
■ Average case of O(lg n), but worst case of O(n)
○ Remove
■ Average case of O(lg n), but worst case of O(n)
● OK, so in the average case, all operations are O(lg n)
○ No constant time operations
○ Worst case is O(n) for all operations

6
What about a red-black BST?

● Seems like overkill...

● Our find and remove operations only need the highest

priority item, not to find/remove any item

○ Can we take advantage of this to get efficient performance

with a simpler implementation?

■ Yes!

7
The heap

● A heap is complete binary tree such that:


○ For each node T in the tree:
■ [Link] is of a higher priority than T.right_child.item
■ [Link] 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

The heap property

8
Heap PQ runtimes

● Find is easy

○ Simply the root of the tree

■ O(1)

● Remove and insert are not quite so trivial

○ The tree is modified and the heap property must be

maintained

9
Heap insert

● Add a new node at the next available leaf

● Push the new node up the tree until it is supporting the

heap property

10
Min heap insert

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

3
42
7
5 37
15
12

42
3
5
9
7 8 15
37 15
12

42
9 9
3

11
Heap remove

● 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

12
Min heap removal

42
3
9
5
7
NO!
42
5
9
7
8 12

9
7 42
8
9 37 15

42 9

13
Heap runtimes

● Find

○ O(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 O(lg n)

14
Heap implementation

● 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

15
Storing a heap in an array

● 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⌋
For arrays indexed
○ left_child(i) = 2i + 1
from 0
○ right_child(i) = 2i + 2

16
The Sorting Problem

Given a collection of items, produce a collection with items


arranged in a sorted order

17
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

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

18
Heap sort analysis

● Runtime:
○ Worst case:
■ O(n lg n)
● In-place?
○ Yes
● Stable?
○ No

19
Indirection example setup

● 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:
class CardPrice:
def __init__(self, store, price):
[Link] = store
[Link] = price
def compareTo(other):
if [Link] < [Link]:
return -1
elif [Link] > [Link]:
return 1
else:
return 0
20
The Priority Searching Problem

Given a collection of items, and the ability to determine the


relative priority of any two items, return an item with the
highest priority

21
Storing Objects in PQ

● What if we want to update an Object?

○ What is the runtime to find an arbitrary item in a heap?

■ O(n)

■ Hence, updating an item in the heap is O(n)

○ Can we improve on this?

■ Back the PQ with something other than a heap?

■ Develop a clever workaround?

22
Indirection

● Maintain a second data structure that maps item IDs to each

item’s current position in the heap

● This creates an indexable PQ

23
Indirection example

● n = CardPrice("NE", 333.98); Indirection


● a = CardPrice("AMZN", 339.99);
● g = CardPrice("GME", 338.00); "NE":0
"NE":2
● b = CardPrice("BB", 349.99);
"AMZN":0
"AMZN":1

● Update price for NE: 340.00 "GME":0


"GME":2
"GME":1
"GME":3
"BB":3
"BB":1
"BB":0
● Update price for GME: 345.00

● Update price for BB: 200.00

b
a
n
g b
g
a n
g g
b

24

You might also like