1 / 15

Priority Queues

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)

nolcha
Download Presentation

Priority Queues

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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) • Meld (join two priority queues into one)

  2. Priority Queues: Applications • Scheduling • Examples: • Printer scheduling. • Jobs sent to a printer by faculty have a higher priority than jobs sent by students • Process scheduling. • Several processes require CPU time. Priority may be based on various factors: • Shortest jobs have higher priority • Jobs with a shorter remaining time have higher priority • Jobs that have waited the longest have higher priority • etc.

  3. Priority Queues: Applications • Discrete-event simulations • Simulations of systems modeled as collections of entities that interact with one another through events that occur at discrete times. • The priority assigned to an event is its start time. • Example: • PA1, bank simulation

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

  5. Binary Heaps • Since heaps are complete trees, 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 7 8 10 7 5 4 6 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

  6. 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(lgn) 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. • What are the maximum and minimum numbers of elements in a heap of height h? • max #elements: 2h+1-1 • min #elements: 2h

  7. Binary Heaps: Heapify • In the discussion that follows, "heap" refers to a max-heap unless otherwise noted. • Every time we extract or insert an element, the heap property may be violated. 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 < heapsize) { max_index = max(A[i], A[2i], A[2i+1]) if (max_index == i) return; swap(A, i, max_index); // swap contents at indices i, max_index i = max_index; // Since we moved A[i] to A[max_index], the heap // property may now be violated there. }

  8. Binary Heaps: Heapify • Running time of Heapify: • O(lgn) because we may have to travel all the way from the root to a leaf, fixing the heap property. • Can you come up with an array that causes Heapify to be called recursively on every node on a path from the root to a leaf? • Any array sorted in ascending order.

  9. Binary Heaps: MakeHeap • Given a random array, create a heap • How? • Algorithm sketch: • Elements A[n/2 +1] through A[n] are leaves, so they can be considered one-element heaps. • Call Heapify in a bottom-up manner starting at index n/2. • Running time : • easy analysis: O(nlgn) • more exact analysis (proof omitted): O(n)

  10. Binary Heaps: MakeHeap • MakeHeap: Given random array A, turn it into a heap • Input: Array A • Output: Array A as a heap • Algorithm: • heapsize(A) = length(A) • fori= length(A)/2downto 1 • Heapify(A, i) • endfor • Why does the loop run in decreasing order? • If it ran in increasing order, would we still get a heap?

  11. Binary Heaps: Insert • New elements are always inserted at the end (last element of the array) • 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 HeapInsert (A, key) i = ++heapsize(A) while i >1 and A[parent(i)]<key A[i] = A[parent(i)] i = parent(i) A[i] = key Running time: O(lgn) since we may end up traveling from a leaf all the way to the root.

  12. 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. ExtractMax (A) max= A[1] A[1] = A[heapsize(A)] heapsize(A) = heapsize(A)-1 Heapify(A,1) return max Running time: O(lgn) since we may end up traveling from the root all the way to a leaf.

  13. Binary Heaps: Sorting • Idea: • Since the maximum element is at the root, we can move it to the last position in the array, and decrement the size of the heap. • The root will now contain the new maximum • Repeat... Heapsort(A) Input: Array A Output: Array A in ascending order. Algorithm: Build-heap(A) for i = length(A) downto 1 swap(A[i], A[1]) heapsize = heapsize – 1 Heapify(A, i) Running time: O(nlgn) since the loop is executed n times and Heapify takes O(lgn)

  14. Priority queues in the STL #include<vector> #include<iostream> #include<queue> int main() { priority_queue<int, vector<int>, greater<int> > q; int nums[10] = {10,6,8,1,5,3,7,2,9,4}; for (int i =0; i < 10; ++i) q.push(nums[i]); std::cout << q.size() << "\n"; while (!q.empty() ) { std::cout << q.top() << "\n"; q.pop(); } return 0; } based on a vector higher values = lower priority

  15. Heaps in the STL #include<algorithm> #include<iostream> #include<vector> #include<functional> int main () { vector<int> nums; for (int i=1; i<11; i++) nums.push_back(i); make_heap(nums.begin(), nums.end()) ; sort_heap(nums.begin(), nums.end()) ; nums.push_back(32); make_heap(nums.begin(), nums.end()); // remake heap pop_heap(nums.begin(), nums.end()); // extraxt max return 0; }

More Related