Priority Queues & Heaps CS16: Introduction to Data Structures - - PowerPoint PPT Presentation

priority queues heaps
SMART_READER_LITE
LIVE PREVIEW

Priority Queues & Heaps CS16: Introduction to Data Structures - - PowerPoint PPT Presentation

Priority Queues & Heaps CS16: Introduction to Data Structures & Algorithms Spring 2020 Outline Priority Queues Motivation ADT Implementation Heaps insert( ) and upheap( ) removeMin( ) and downheap( )


slide-1
SLIDE 1

Priority Queues & Heaps

CS16: Introduction to Data Structures & Algorithms Spring 2020

slide-2
SLIDE 2

Outline

  • Priority Queues
  • Motivation
  • ADT
  • Implementation
  • Heaps
  • insert( ) and upheap( )
  • removeMin( ) and downheap( )
slide-3
SLIDE 3

Motivation

  • Priority queues store items with various priorities
  • Priority queues are everywhere
  • Plane departures: some flights have higher priority than
  • thers
  • Bandwidth management: real-time traffic like Skype

transmitted first

  • Student dorm room allocations
3
slide-4
SLIDE 4

Priority Queue ADT

  • Stores key/element pairs
  • key determines position in queue
  • insert(key, element):
  • inserts element with key
  • removeMin( ):
  • removes pair w/ smallest key and

returns element

slide-5
SLIDE 5

Priority Queue Implementations

5

2 min

Activity #1

slide-6
SLIDE 6

Priority Queue Implementations

6

2 min

Activity #1

slide-7
SLIDE 7

Priority Queue Implementations

7

1 min

Activity #1

slide-8
SLIDE 8

Priority Queue Implementations

8

0 min

Activity #1

slide-9
SLIDE 9

Priority Queue Implementation

9

Implementation insert removeMin

Unsorted Array O(1) O(n) Sorted Array O(n) O(1) Unsorted Linked List O(1) O(n) Sorted Linked List O(n) O(1) Hash Table O(1) O(n) Heap O(log n) O(log n)
slide-10
SLIDE 10

What is a Heap?

  • Data structure that implements priority queue
  • Heaps can be implemented with
  • Tree
  • Array
  • Tree-based heap
10

2 6 5 7 9

slide-11
SLIDE 11

Heap Properties

  • Binary tree
  • each node has at most 2 children
  • Each node has a priority (key)
  • Heap has an order
  • min-heap: n.parent.key ≤ n.key
  • max-heap: n.parent.key ≥ n.key
  • Left-complete
  • Height of O(log n)
11
slide-12
SLIDE 12

Heap Properties

  • To implement priority queue
  • insert key/element pair at each node
12 (1, Tsunade) (5, Kakashi)

(3, Danzo)

(9, Naruto)

(7, Sakura)

slide-13
SLIDE 13

Heap: insert

  • Need to keep track of “insertion node”
  • leaf where we will insert new node…
  • …so we can keep heap left-complete
13

2 6 5 7 9 insertion node

slide-14
SLIDE 14

Heap: insert

  • Ex: insert(1)
  • replace insertion node w/ new node
14

2 6 5 7 9

1

Heap order violated!

slide-15
SLIDE 15

Heap: upheap

  • Repair heap: swap new element up tree until keys are sorted
  • First swap fixes everything below new location
  • since every node below 6’s old location has to be at least 6…
  • …they must be at least 1
15

2 1 5 7 9

6

slide-16
SLIDE 16

Heap: upheap

  • One more swap since 1≤2
  • Now left-completeness and order are satisfied
16

1 2 5 7 9

6

slide-17
SLIDE 17

Heap: insert

17

2 min

Activity #1

slide-18
SLIDE 18

Heap: insert

18

2 min

Activity #2

slide-19
SLIDE 19

Heap: insert

19

1 min

Activity #1

slide-20
SLIDE 20

Heap: insert

20

0 min

Activity #1

slide-21
SLIDE 21

Heap: upheap Summary

  • After inserting a key k, order may be violated
  • upheap restores order by
  • swapping key upward from insertion node
  • terminates when either the root is reached…
  • …or some node whose parent has key less or equal than k
  • Heap insertion has runtime
  • O(log n), why?
  • because heap has height O(log n)
  • perfect binary tree with n nodes has height log(n+1)-1
21
slide-22
SLIDE 22

Heap: removeMin

  • Remove root
  • because it is always the smallest element
  • How can we remove root w/o destroying heap?
22 1 2 5 7 9 6 2 5 7 9 6

Heap destroyed!

slide-23
SLIDE 23

Heap: removeMin

  • Instead swap root with last element & remove it
  • removing last element is easy
23 1 2 5 7 9 6 6 2 5 7 9 1

Order destroyed!

6 2 5 7 9
slide-24
SLIDE 24

Heap: removeMin

  • Now swap root down as necessary
24

Heap is in

  • rder!
6 2 5 7 9 2 6 5 7 9
slide-25
SLIDE 25

Heap: downheap Summary

  • downheap restores order by
  • swapping key downward from the root…
  • …with the smaller of 2 children
  • terminates when either a leaf is reached or
  • …some node whose children has key k or more
  • downheap has runtime
  • O(log n), why?
  • because heap has height O(log n)
25
slide-26
SLIDE 26

Heap: removeMin

26

2 min

Activity #1

slide-27
SLIDE 27

Heap: removeMin

27

2 min

Activity #1

slide-28
SLIDE 28

Heap: removeMin

28

1 min

Activity #1

slide-29
SLIDE 29

Heap: removeMin

29

0 min

Activity #1

slide-30
SLIDE 30

Summary of Heap

  • insert(key, value)
  • insert value at insertion node
  • insertion node must be kept track of
  • upheap from insertion node as necessary
  • removeMin( )
  • swap root with last item
  • delete (swapped) last item
  • downheap from root as necessary
30
slide-31
SLIDE 31

Array-based Heap

  • Heap with n keys can be represented

w/ array of size n+1

  • Storing nodes in array
  • Node stored at index i
  • left child stored at index 2i
  • right child stored at index 2i+1
  • Leaves & edges not stored
  • Cell 0 not used
  • Operations
  • insert: store new node at index n+1
  • removeMin: swap w/ index n and remove
2 6 5 7 9

2 5 6 9 7

1 2 3 4 5
slide-32
SLIDE 32

Finding Insertion Node

  • Can be found in O(log n)
  • Start at last added node
  • Go up until a left child or root is reached
  • If left child
  • go to sibling (corresponding right child)
  • then go down left until leaf is reached
32 Can be done in O(1) time by using additional data structure…need this for project!
slide-33
SLIDE 33

Priority Queue Implementation

33

Implementation insert removeMin

Unsorted Array O(1) O(n) Sorted Array O(n) O(1) Unsorted Linked List O(1) O(n) Sorted Linked List O(n) O(1) Hash Table O(1) O(n) Heap O(log n) O(log n)
slide-34
SLIDE 34

References

  • Slide #4
  • “Queue” in French means tail
  • The picture depicts the tail of a whale
  • Slide #7
  • The picture is of a Transformers character named Junkheap which transforms
from a waste management garbage truck
  • Slide #8
  • The names are characters from the Anime series Naruto (https://
en.wikipedia.org/wiki/Naruto)
  • The picture is the symbol of the Hidden Leaf Village (where the character
Naruto is from)
  • The heap priorities represent the importance of the character in the village
34