heaps
play

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


  1. Heaps Chapter 6 1

  2. 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

  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

  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

  5. Regular Queues HPC Jobs 5

  6. Priority Queue Airport check-in HPC ! Jobs 6

  7. Priority Queue ADT Queue Priority Queue Enqueue Insert Dequeue DeleteMin/Max Front PeekMin/Max Empty? Empty? IncreaseKey DecreaseKey Remove 7

  8. Priority Queue Implementation Straight-forward implementations? Unsorted list Sorted list Unsorted list Sorted list BST/AVL Insert BST/AVL PeekMin DeleteMin IncreaseKey DecreaseKey 8

  9. Priority Queue Implementation Straight-forward implementations Unsorted list Sorted list Unsorted list Sorted list BST/AVL Insert O(1) O(n) O(log n) BST/AVL 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 9

  10. Heap Implementation Example of a MinHeap 13 21 16 24 31 19 68 65 26 32 10

  11. Heap Implementation Example of a MaxHeap 68 32 65 24 31 19 16 13 21 26 11

  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

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

  14. MinHeap Insertion Insert 15 13 21 16 24 15 19 68 65 26 32 31 14

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

  16. MinHeap Insertion Insert 15 13  15 16 24 21 19 68 65 26 32 31 16

  17. MinHeap Insertion Insert 25 13 15 16 24 21 19 68 25  65 26 32 31 17

  18. MinHeap Insertion Insert 10 13 15 16 24 21 19 68 10 65 26 32 31 25 18

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

  20. MinHeap Insertion Insert 10 13 15 10 24 21 16 68 19 65 26 32 31 25 20

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

  22. MinHeap Insertion  Insert 10 10 15 13 24 21 16 68 19 65 26 32 31 25 22

  23. MinHeap Deletion DeleteMin 10 15 13 24 21 16 68 19 65 26 32 31 25 23

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

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

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

  27. MinHeap Deletion DeleteMin 13 15 16 24 21 19 68 65 26 32 31 25 27

  28. MinHeap Deletion DeleteMin 13 15 16 24 21 19 68  65 26 32 31 25 28

  29. MinHeap Deletion DeleteMin 15 16 24 21 19 68 65 26 32 31 25 29

  30. MinHeap Deletion DeleteMin 25 15 16 24 21 19 68 65 26 32 31 30

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

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

  33. MinHeap Deletion DeleteMin 15 21 16 24 25 19 68  65 26 32 31 33

  34. Array Representation Level-order Traversal 15 21 16 24 25 19 68 65 26 32 31 15 21 16 24 25 19 68 65 26 32 31 34

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

  36. Array Implementation 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? 36

  37. Array Implementation 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; 37

  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 O(n log k) DeleteMin Insert(x) Return all the values in the MinHeap 38

Recommend


More recommend