Heaps Chapter 6 1 Objectives Understand what a priority queue is - - PowerPoint PPT Presentation

heaps
SMART_READER_LITE
LIVE PREVIEW

Heaps Chapter 6 1 Objectives Understand what a priority queue is - - PowerPoint PPT Presentation

Heaps Chapter 6 1 Objectives Understand what a priority queue is Identify the applications where a priority queue can be used Analyze the running time and space overhead of the heap data structure Use heaps to solve the top-k problem 2


slide-1
SLIDE 1

Heaps

Chapter 6

1

slide-2
SLIDE 2

Objectives

Understand what a priority queue is Identify the applications where a priority queue can be used Analyze the running time and space

  • verhead of the heap data structure

Use heaps to solve the top-k problem

2

slide-3
SLIDE 3

Top-K

Given n numbers, find the k largest numbers k=1 k=2 k=n/100 (Top 1%) Sort and select Use a BST (or AVL tree) to keep track of the top-k items Is there something more efficient?

3

slide-4
SLIDE 4

Top-K

Given n numbers, find the k largest numbers k=1 k=2 k=n/100 (Top 1%) Sort and select O(n log n) Use a BST (or AVL tree) to keep track of the top-k items O(n log k) Is there something more efficient?

4

slide-5
SLIDE 5

Regular Queues

5

HPC

Jobs

slide-6
SLIDE 6

Priority Queue

Airport check-in

6

HPC

! Jobs

slide-7
SLIDE 7

Priority Queue ADT

Queue

Enqueue Dequeue Front Empty?

Priority Queue

Insert DeleteMin/Max PeekMin/Max Empty? IncreaseKey DecreaseKey Remove

7

slide-8
SLIDE 8

Priority Queue Implementation

Straight-forward implementations? Unsorted list Sorted list BST/AVL

8

Unsorted list Sorted list BST/AVL Insert PeekMin DeleteMin IncreaseKey DecreaseKey

slide-9
SLIDE 9

Priority Queue Implementation

Straight-forward implementations Unsorted list Sorted list BST/AVL

9

Unsorted list Sorted list BST/AVL Insert O(1) O(n) O(log n) PeekMin O(n) O(1) O(log n) DeleteMin O(n) O(1) O(log n) IncreaseKey O(1)* O(n) O(log n) DecreaseKey O(1)* O(n) O(log n)

* Assuming the record is already located

slide-10
SLIDE 10

Heap Implementation

10

13 21 16 Example of a MinHeap 24 31 19 68 65 26 32

slide-11
SLIDE 11

Heap Implementation

11

68 32 65 Example of a MaxHeap 24 31 19 16 13 21 26

slide-12
SLIDE 12

Heap Properties

A complete binary tree For any internal node n

n.Key ≤ n.child.Key (Min Heap) n.Key ≥ n.child.Key (Max Heap)

12

slide-13
SLIDE 13

MinHeap Insertion

13

13 21 16 24 31 19 68 65 26 32 Insert 15 15

slide-14
SLIDE 14

MinHeap Insertion

14

13 21 16 24 15 19 68 65 26 32 Insert 15 31

slide-15
SLIDE 15

MinHeap Insertion

15

13 15 16 24 21 19 68 65 26 32 Insert 15 31

slide-16
SLIDE 16

MinHeap Insertion

16

13 15 16 24 21 19 68 65 26 32 Insert 15 31

slide-17
SLIDE 17

MinHeap Insertion

17

13 15 16 24 21 19 68 65 26 32 Insert 25 31 25

slide-18
SLIDE 18

MinHeap Insertion

18

13 15 16 24 21 19 68 65 26 32 Insert 10 31 25 10

slide-19
SLIDE 19

MinHeap Insertion

19

13 15 16 24 21 10 68 65 26 32 Insert 10 31 25 19

slide-20
SLIDE 20

MinHeap Insertion

20

13 15 10 24 21 16 68 65 26 32 Insert 10 31 25 19

slide-21
SLIDE 21

MinHeap Insertion

21

10 15 13 24 21 16 68 65 26 32 Insert 10 31 25 19

slide-22
SLIDE 22

MinHeap Insertion

22

10 15 13 24 21 16 68 65 26 32 Insert 10 31 25 19

slide-23
SLIDE 23

MinHeap Deletion

23

10 15 13 24 21 16 68 65 26 32 DeleteMin 31 25 19

slide-24
SLIDE 24

MinHeap Deletion

24

15 13 24 21 16 68 65 26 32 DeleteMin 31 25 19

slide-25
SLIDE 25

MinHeap Deletion

25

19 15 13 24 21 16 68 65 26 32 DeleteMin 31 25

slide-26
SLIDE 26

MinHeap Deletion

26

13 15 19 24 21 16 68 65 26 32 DeleteMin 31 25

slide-27
SLIDE 27

MinHeap Deletion

27

13 15 16 24 21 19 68 65 26 32 DeleteMin 31 25

slide-28
SLIDE 28

MinHeap Deletion

28

13 15 16 24 21 19 68 65 26 32 DeleteMin 31 25

slide-29
SLIDE 29

MinHeap Deletion

29

15 16 24 21 19 68 65 26 32 DeleteMin 31 25

slide-30
SLIDE 30

MinHeap Deletion

30

25 15 16 24 21 19 68 65 26 32 DeleteMin 31

slide-31
SLIDE 31

MinHeap Deletion

31

15 25 16 24 21 19 68 65 26 32 DeleteMin 31

slide-32
SLIDE 32

MinHeap Deletion

32

15 21 16 24 25 19 68 65 26 32 DeleteMin 31

slide-33
SLIDE 33

MinHeap Deletion

33

15 21 16 24 25 19 68 65 26 32 DeleteMin 31

slide-34
SLIDE 34

Array Representation

34

15 21 16 24 25 19 68 65 26 32 31 Level-order Traversal 15 21 16 24 25 19 68 65 26 32 31

slide-35
SLIDE 35

Array Representation

35

15 21 16 24 25 19 68 65 26 32 31 Level-order Traversal 15 21 16 24 25 19 68 65 26 32 31 1 2 3 4 5 6 7 8 9 10 11

1 2 3 4 5 6 7 8 9 10 11

slide-36
SLIDE 36

Array Implementation

36

template <typename T> class MinHeap { T[] keys; int size; int capacity; }; #define ROOT 1 #define LEFT_CHILD(p) p*2 #define RIGHT_CHILD(p) p*2+1 #define PARENT(p) p/2 int treePointer = ROOT; Do you spot any mistakes or bad practices?

slide-37
SLIDE 37

Array Implementation

37

template <typename T> class MinHeap { T[] keys; int size; int capacity; }; #define ROOT 1 #define LEFT_CHILD(p) ((p)*2) #define RIGHT_CHILD(p) ((p)*2+1) #define PARENT(p) ((p)/2) int treePointer = ROOT;

slide-38
SLIDE 38

Top-K Revisit

Given an unordered list of n values, and an integer K, find the K largest elements

Initialize a MinHeap of size K Insert the first K values in the list into the Heap For each additional value x

If x is smaller than the top of the heap (PeekMin)

Discard x

Else

DeleteMin Insert(x)

Return all the values in the MinHeap

38

O(n log k)