140 likes | 484 Views
Priority Queues. A priority queue is an ADT where: Each element has an associated priority Efficient extraction of the highest-priority element is supported Typical operations of a priority queue are: Enqueue (insert an element) Dequeue (remove the highest-priority element)
E N D
Priority Queues • A priority queue is an ADT where: • Each element has an associated priority • Efficient extraction of the highest-priority element is supported • Typical operations of a priority queue are: • Enqueue (insert an element) • Dequeue (remove the highest-priority element) • Max (return the highest-priority element) • Can be implemented as a Binary Heap
Binary Heaps • Binary heap = • a binary tree that is • complete • every level except possibly the bottom one is completely filled and the leaves in the bottom level are as far left as possible • satisfies the heap property: • the key stored in every node is greater than or equal to the keys stored in its children • this is called a max-heap. If the key at each node is smaller than or equal to the keys of its children, then we have a min-heap
Binary Heaps? 16 1 10 4 3 2 7 9 3 14 7 4 6 5 2 8 1 8 10 9
Observe the Index • Since the structure of a heap is so regular (i.e., always a complete binary tree) they can easily be stored in arrays: 18 1 9 12 3 2 • Given index iof a node, • the index of its parent is i / 2 • the indices of its children are 2i and2i+1 8 10 7 5 4 6 7 5 1 4 3 8 10 9 18 12 9 8 10 7 5 1 4 3 index 1 2 3 4 5 6 7 8 9 10
Binary Heaps • Is every array a heap? • Not necessarily. But every sorted array is a • max-heap, if sorted in descending order • min-heap, if sorted in ascending order • What is the height of a heap? • O(lg n) where n is the number of elements in the heap • Where in a max-heap is the smallest element? • It is always a leaf but we cannot know exactly where it is located
Binary Heaps: Heapify (or percolateDown) Heapify: Given node i of heap that potentially violates the heap property, fix it Input: Array A storing a heap, index i Preconditions: The children of node i are heaps Algorithm: while (i < n) { max_i = max_index(A[i], A[2i], A[2i+1]) if (max_i == i) return; swap(A, i, max_i); // swap contents at indices i, max_i i = max_i; // Since we moved A[i] to A[max_i], the heap // property may now be violated there }
Binary Heaps: MakeHeap • Given a random array A with n elements, create a heap • How? • Algorithm sketch: • Elements A[n/2 +1] through A[n] can be considered one-element heaps. WHY? • Call Heapify in a bottom-up manner starting at index n/2
Binary Heaps: MakeHeap (cont’d) • MakeHeap: Given random array A, turn it into a heap • Input: Array A • Output: Array A as a heap • Algorithm: • fori= n/2downto 1 • Heapify(A, i) • endfor Transform the array 2 8 6 1 10 15 3 12 11 with the bottom up heapify method
Binary Heaps: MakeHeap • Running time : • Easy analysis: O(nlgn) • More exact analysis (proof omitted): O(n)
Binary Heaps: Insert • New elements are always inserted at the end (last element of the array), Example • This may violate the heap property: • Travel up the tree looking for an appropriate position for the new node (one that maintains the heap property). As you go, copy (shift) down the values of the nodes you pass, to make room (i.e., percolateUp) Running time? HeapInsert (A, key) i = ++n while (i >1 and A[parent(i)]<key) A[i] = A[parent(i)] i = parent(i) A[i] = key
Binary Heaps: ExtractMax • The maximum element is always at the root • Extract the maximum by replacing it with the last element of the array and then Heapifying Running time? ExtractMax (A) max= A[1] A[1] = A[n] n = n-1 Heapify(A,1) return max
Running Time of Typical Priority Queue Operations if using a Heap • Enqueue (insert an element)? • Dequeue (remove the highest-priority element)? • Max (return the highest-priority element)?