Priority Queue and Heap What is Priority Queue

  • Slides: 36
Download presentation
Priority Queue and Heap

Priority Queue and Heap

What is Priority Queue? n Definition n n Similar to a regular queue (FIFO)

What is Priority Queue? n Definition n n Similar to a regular queue (FIFO) The key difference is that each element has a priority. n An element with high priority is served before an element with low priority. High priority Low priority 2

Implementing Priority Queue n How to implement a priority queue n n n Array-based

Implementing Priority Queue n How to implement a priority queue n n n Array-based implementation Linked-list-based implementation Heap-based implementation 0 1 2 3 4 3 … MAX_LIST-1

Implementing Priority Queue n 9 1 5 3 2 6 8 7 11 Insert

Implementing Priority Queue n 9 1 5 3 2 6 8 7 11 Insert 10 4

Implementing Priority Queue n 11 9 8 7 6 5 Insert 10 5 3

Implementing Priority Queue n 11 9 8 7 6 5 Insert 10 5 3 2 1

Implementing Priority Queue n Comparison for various implementations Representation Search max Delete max Unordered

Implementing Priority Queue n Comparison for various implementations Representation Search max Delete max Unordered array Unordered linked list Ordered array Ordered linked list Max heap 6 Insert

What is Heap? n 9 1 7 8 5 2 4 1 6 3

What is Heap? n 9 1 7 8 5 2 4 1 6 3 4 7 3 3 8 Max heap 6 9 5 8 Min heap 7 4

What is Heap? n Level 0 9 Level 1 Level 2 Level 3 7

What is Heap? n Level 0 9 Level 1 Level 2 Level 3 7 8 5 2 4 1 3 8 6 3

Examples of Heap 7 6 8 9 5 8 7 6 3 4 9

Examples of Heap 7 6 8 9 5 8 7 6 3 4 9 4 5 9 4 8 6 7 3 5 7 5 6 9 7 3 6 4 8 Min heaps 8 5 4 Max heaps 9 7 6 5 3

Array-based Implementation n [1 ] [4 ] Index key [2 ] 8 5 [5

Array-based Implementation n [1 ] [4 ] Index key [2 ] 8 5 [5 4 ] 9 [3 ] 7 [6 6 ] [7 3 ] 2 1 [8 [9 0 ] 1 ]2 3 [10] 3 4 5 6 7 8 9 10 - 7 5 4 6 3 2 1 3 9 8 10

Array-based Implementation items[] num 0 0 1 2 3 #define MAX_HEAP 4 … 100

Array-based Implementation items[] num 0 0 1 2 3 #define MAX_HEAP 4 … 100 typedef enum { false, true } bool; typedef char Data; typedef struct { Data data; int priority; } HNode; typedef struct { HNode items[MAX_HEAP + 1]; int num; } Heap; 11 MAX_HEAP

Array-based Implementation / Make a heap empty. void Init. Heap(Heap *pheap); // check whether

Array-based Implementation / Make a heap empty. void Init. Heap(Heap *pheap); // check whether a heap is empty. bool Is. Empty(Heap *pheap); // Check whether a heap is full. bool Is. Full(Heap *pheap); // Insert a node to the heap. void Insert(Heap *pheap, Data data, int priority); // Remove the maximum data from the heap. Data Delete(Heap *pheap); // Get a parent index for a given index. int Get. Parent(int idx); // Get a left child index for a given index. int Get. LChild(int idx); // Get a right child index for a given index. int Get. RChild(int idx); // Get a child index with high priority between two child nodes. int Get. High. Prioity. Child(Heap* pheap, int idx); 12

Initializing Heap n Init. Heap, Is. Empty, and Is. Full operations // Make a

Initializing Heap n Init. Heap, Is. Empty, and Is. Full operations // Make a heap empty. void Init. Heap(Heap *pheap) { pheap->num = 0; } // check whether a heap is empty. bool Is. Empty(Heap *pheap) { return pheap->num == 0; } // Check whether a heap is full. bool Is. Full(Heap *pheap) { return pheap->num == MAX_HEAP; } 13

Insertion in Heap n Key idea n n n Insert a new node to

Insertion in Heap n Key idea n n n Insert a new node to the last position. Check if the new node is better than its ancestors. https: //visualgo. net/en/heap new one better? Yes 14 new one better?

Insertion in Heap n Step 1: Inserting a new node n Insert a new

Insertion in Heap n Step 1: Inserting a new node n Insert a new node into a bottom-rightmost leaf node available. 9 7 8 5 2 4 1 3 6 3 Inserting 10 into the last position 10 15

Insertion in Heap n Step 2: Updating the heap n Compare the new node

Insertion in Heap n Step 2: Updating the heap n Compare the new node with its parent. n n If the new node is higher priority than its parent, exchange them. Do this until the new parent is greater than the new node or the new node becomes the root. 9 7 8 5 2 4 1 3 6 3 10 Comparing 4 with 10 16

Insertion in Heap n Step 2: Updating the heap n Compare the new node

Insertion in Heap n Step 2: Updating the heap n Compare the new node with its parent. n n If the new node is higher priority than its parent, exchange them. Do this until the new parent is greater than the new node or the new node becomes the root. 9 7 8 Comparing 10 with 8 5 2 10 1 3 6 4 17 3

Insertion in Heap n Step 2: Updating the heap n Compare the new node

Insertion in Heap n Step 2: Updating the heap n Compare the new node with its parent. n n If the new node is higher priority than its parent, exchange them. Do this until the new parent is greater than the new node or the new node becomes the root. 9 Comparing 10 with 9 7 10 5 2 8 1 3 6 4 18 3

Insertion in Heap n Step 2: Updating the heap n Compare the new node

Insertion in Heap n Step 2: Updating the heap n Compare the new node with its parent. n n If the new node is higher priority than its parent, exchange them. Do this until the new parent is greater than the new node or the new node becomes the root. 10 Done. 7 9 5 2 8 1 3 6 4 19 3

Exercise: Insertion in Heap n Inserting 39 and 43 to the heap 43 41

Exercise: Insertion in Heap n Inserting 39 and 43 to the heap 43 41 3 2 n 20 1 12 41 39 35 33 3 9 2 16 What is the time complexity for insertions? 20 33 1 16 35 20 12 9

Implementation of Insertion void Insert(Heap *pheap, Data data, int priority) { HNode new. Node;

Implementation of Insertion void Insert(Heap *pheap, Data data, int priority) { HNode new. Node; int idx = pheap->num + 1; if (Is. Full(pheap)) exit(1); // Compare the new node with its parent. while (idx > 1) { int parent = Get. Parent(idx); if (priority > pheap->items[parent]. priority) { pheap->items[idx] = pheap->items[parent]; idx = parent; } else break; } new. Node. data = data; new. Node. priority = priority; pheap->items[idx] = new. Node; pheap->num++; } 21

Deletion in Heap n Key idea n n n Replace the last node into

Deletion in Heap n Key idea n n n Replace the last node into the root Check if the new root is better than its descendants. https: //visualgo. net/en/heap The new root worse? Yes The new root worse? 22

Deletion in Heap n Step 1: Removing the root from the heap n n

Deletion in Heap n Step 1: Removing the root from the heap n n Remove the max node from the root of the heap. Place the last node to the root. 9 Removing the root 7 8 5 1 4 2 6 3 23 3

Deletion in Heap n Step 1: Removing the root from the heap n n

Deletion in Heap n Step 1: Removing the root from the heap n n Remove the max node from the root of the heap. Place the last node to the root. 3 7 8 5 1 4 2 3 6 3 Placing 3 to the root 24

Deletion in Heap n Step 2: Updating the heap n Compare the root with

Deletion in Heap n Step 2: Updating the heap n Compare the root with its children. n n If it is smaller than any of children, exchange the position with its max child. Do this until the heap is reestablished. 3 Comparing 3 with 8 7 8 5 1 4 6 2 25 3

Deletion in Heap n Step 2: Updating the heap n Compare the root with

Deletion in Heap n Step 2: Updating the heap n Compare the root with its children. n n If it is smaller than any of children, exchange the position with its max child. Do this until the heap is reestablished. 8 Comparing 3 with 5 5 1 7 3 4 6 2 26 3

Deletion in Heap n Step 2: Updating the heap n Compare the root with

Deletion in Heap n Step 2: Updating the heap n Compare the root with its children. n n If it is smaller than any of children, exchange the position with its max child. Do this until the heap is reestablished. 8 7 5 Comparing 3 with 2 1 3 4 6 2 27 3

Deletion in Heap n Step 2: Updating the heap n Compare the root with

Deletion in Heap n Step 2: Updating the heap n Compare the root with its children. n n If it is smaller than any of children, exchange the position with its max child. Do this until the heap is reestablished. 8 7 5 Done. 1 3 4 6 2 28 3

Exercise: Deletion in Heap n Deleting 43 and 41 from the heap 39 43

Exercise: Deletion in Heap n Deleting 43 and 41 from the heap 39 43 41 39 3 1 n 33 2 16 35 20 35 33 3 9 1 12 What is the time complexity for deletions? 29 20 2 16 12 9

Implementation of Deletion Data Delete(Heap *pheap) { Data max = pheap->items[1]. data; HNode last

Implementation of Deletion Data Delete(Heap *pheap) { Data max = pheap->items[1]. data; HNode last = pheap->items[pheap->num]; int parent = 1, child; // Compare the root with its child nodes. while (child = Get. High. Prioity. Child(pheap, parent)) { if (last. priority < pheap->items[child]. priority) { pheap->items[parent] = pheap->items[child]; parent = child; } else break; } pheap->items[parent] = last; pheap->num--; return max; } 30

Utility Functions for Deletions n Given an index, find a parent or a child

Utility Functions for Deletions n Given an index, find a parent or a child index. // Get a parent index for a given index. int Get. Parent(int idx) { return idx / 2; } // Get a left child index for a given index. int Get. LChild(int idx) { return idx * 2; } // Get a right child index for a given index. int Get. RChild(int idx) { return idx * 2 + 1; } 31

Utility Functions for Deletions n Given an index, find a child node with the

Utility Functions for Deletions n Given an index, find a child node with the highest priority among its child nodes. int Get. High. Prioity. Child(Heap* pheap, int idx) { if (Get. LChild(idx) > pheap->num) // No child nodes exist. return 0; else if (Get. LChild(idx) == pheap->num) // Exist a left child only. return Get. LChild(idx); else // Choose a child node with the highest priority. { int left = Get. LChild(idx), right = Get. RChild(idx); if (pheap->items[left]. priority > pheap->items[right]. priority) return left; else return right; } } 32

Implementing Priority Queue n Heap-based implementation n The priority queue is simply implemented by

Implementing Priority Queue n Heap-based implementation n The priority queue is simply implemented by using a heap. #include "Heap. h" typedef Heap PQueue; void Init. PQueue(PQueue* ppqueue); bool Is. PQEmpty(PQueue * ppqueue); bool Is. PQFull(PQueue* ppqueue); // Insert an item to the priority queue. void Enqueue(PQueue * ppqueue, Data data, int priority); // Delete an item to the priority queue. Data Dequeue(PQueue * ppqueue); 33

Implementing Priority Queue n Operations for priority queue void Init. PQueue(PQueue* ppqueue) { Init.

Implementing Priority Queue n Operations for priority queue void Init. PQueue(PQueue* ppqueue) { Init. Heap(ppqueue); } bool Is. PQEmpty(PQueue * ppqueue) { return Is. Empty(ppqueue); } bool Is. PQFull(PQueue* ppqueue) { return Is. Full(ppqueue); } void Enqueue(PQueue * ppqueue, Data data, int priority) { Insert(ppqueue, data, priority); } Data Dequeue(PQueue * ppqueue) { return Delete(ppqueue); } 34

Heap Sort n Sorting elements by using a max heap n n Step 1:

Heap Sort n Sorting elements by using a max heap n n Step 1: Inserting each element to the heap Step 2: Removing all elements from the heap in sequence n When deleting elements, it ensures to return the maximum element. Unsorted Data Heap 35 Sorted Data

Implementing Heap Sort n What is the time complexity of heap sort? void Heap.

Implementing Heap Sort n What is the time complexity of heap sort? void Heap. Sort(Data a[], int n) { Heap heap; Init. Heap(&heap); // Insert all elements to the heap. for (int i = 0; i < n; i++) Insert(&heap, a[i]); // Remove all elements from the heap. for (int i = n - 1; i >= 0; i--) a[i] = Delete(&heap); } n How to find k largest elements? 36