Presentation is loading. Please wait.

Presentation is loading. Please wait.

Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R3. Priority Queues.

Similar presentations


Presentation on theme: "Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R3. Priority Queues."— Presentation transcript:

1 Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R3. Priority Queues

2 Prof. Amr Goneid, AUC2 Definition of Priority Queue The Binary Heap Insertion and Removal A Priority Queue Class Priority Queues

3 Prof. Amr Goneid, AUC3 Definition of Priority Queue A Priority Queue (PQ) is a set with the operations: Insert an element Remove and return the smallest / largest element Priority queues enable us to retrieve items not by the insertion time (as in a stack or queue), nor by a key match (as in a dictionary), but by which item has the highest priority of retrieval. 1. Definition of Priority Queue

4 Prof. Amr Goneid, AUC4 Priority queues are used to maintain schedules and calendars. They govern who goes next in simulations of airports, parking lots, and the like One famous application is an efficient sorting method called Heap Sort. Applications

5 Prof. Amr Goneid, AUC5 2. The Binary Heap Property (1): Partially Balanced: The heap is as close to a complete binary tree as possible. If there are missing leaves, they will be in the last level at the far right. The Binary Heap is an array implementation of PQ. The Heap is visualized as a binary tree with the following properties:

6 Prof. Amr Goneid, AUC6 The Heap as a Complete Tree 1 23 4 5 67 8 9 10 Missing Leaves

7 Prof. Amr Goneid, AUC7 The Heap as a Complete Tree 57189 37 1 1678 1 23 4 5 67 8 9 10 Property (2): Heap Condition: The value in each parent is ≤ the values in its children.

8 Prof. Amr Goneid, AUC8 The Heap as a Complete Tree 57189 37 1 1678 1 23 4 5 67 8 9 10 Property (3): The Heap array The Heap array contains the level order traversal of the tree

9 Prof. Amr Goneid, AUC9 Example The Heap Array 137571891678 Parent at location (i), children at locations (2i) and (2i + 1) Parent of child at (i) is in location (i / 2) The heap condition is: a[i] ≤ a[2*i] && a[i] ≤ a[2*i+1]

10 Prof. Amr Goneid, AUC10 The Heap Array The binary heap data structure supports both insertion and extract-min in O(log n) time each. The minimum key is always at the root of the heap. New keys can be inserted by placing them at an open leaf and up-heaping the element upwards until it sits at its proper place in the partial order.

11 Prof. Amr Goneid, AUC11 3. Insertion and Removal Insert in The Heap 54 6 32 1 54 1 36 2 53 46 2 4 22 insert 2 insert 4 insert 6,5,3 insert 1 Insert array X[ ] = {2,4,6,5,3,1} into a heap Resulting heap = {1,3,2,5,4,6}

12 Prof. Amr Goneid, AUC12 Remove from The Heap 54 6 32 1 54 32 6 54 36 2 5 3 6 4 5 4 6 3 46 5 5 6 12 56 4 34 6 5 5 6 6 Input heap = {1,3,2,5,4,6}, Output = {1,2,3,4,5,6}

13 Prof. Amr Goneid, AUC13 http://www.cosc.canterbury.ac.nz/people/ mukundan/dsal/MinHeapAppl.htmlDemo

14 Prof. Amr Goneid, AUC14 4. A Priority Queue Class // File: PQ.h // Priority Queue header file (Minimum Heap) /* The PQ is implemented as a minimum Heap. The elements of the heap are of type (E). The top of the heap will contain the smallest element. The heap condition is that a parent is always less than or equal to the children. The heap is implemented as a dynamic array a[ ] with a size specified by the class constructor. Location a[0] is reserved for a value "itemMin" smaller than any possible value (e.g. a negative number) */

15 Prof. Amr Goneid, AUC15 A Priority Queue Class #ifndef PQ_H #define PQ_H template class PQ { public: // Class Constructor with input size parameter PQ (int ); // Class Destructor ~PQ ( ); // Member Functions void insert (E );// insert element into heap E remove ( );// remove & return the top of the heap

16 Prof. Amr Goneid, AUC16 A Priority Queue Class private: E *a; // Pointer to Storage Array int MaxSize;// Maximum Size (not including a[0]) int N;// index of last element in the array E itemMin;// itemMin to be stored in a[0] // Heap Adjustment Functions void upheap(int k); void downheap(int k); }; #endif // PQ_H #include "PQ.cpp"

17 Prof. Amr Goneid, AUC17 Implementation // File:PQ.cpp // PQ (min Heap) Class implementation template PQ ::PQ (int mVal) { MaxSize = mVal; a = new E[MaxSize+1]; N=0; itemMin = -32767; // Minimum Heap a[0] = itemMin ; } // Class Destructor template PQ ::~PQ ( ) { delete [ ] a;}

18 Prof. Amr Goneid, AUC18 Insert New Priority // Increment heap size and insert element (v) // at end (first rightmost empty leaf) // and then adjust heap template void PQ ::insert (E v) { a[++N] = v ; upheap(N) ; }

19 Prof. Amr Goneid, AUC19 UpHeap Algorithm // Upheap element at location (k) in the heap // as long as it is less than the parent // Assume a[0] = itemMin template void PQ ::upheap(int k) { E v = a[k] ; while ( a[k/2] >= v) { a[k] = a[k/2] ; k = k/2 ; } a[k] = v ; }

20 Prof. Amr Goneid, AUC20 Remove Highest Priority // remove and return top of the heap, then adjust heap: // Get first element and replace it by last element. // Decrement heap size. Bubble down to adjust heap template E PQ ::remove() { E v = a[1] ; a[1] = a[N--] ; downheap(1) ; return v; }

21 Prof. Amr Goneid, AUC21 DownHeap Algorithm // downheap element at (k) in the heap template void PQ ::downheap(int k) { int j = 2 * k ; E v = a[k] ; while (j <= N) { if ((j a[j+1])) j++ ; if ( v <= a[j] ) break; a[j/2] = a[j] ; j *= 2 ; } a[j/2] = v ; }

22 Prof. Amr Goneid, AUC22 Implementation Files Full implementation of the PQ class is found at: http://www.cse.aucegypt.edu/~csci321/co des.zip


Download ppt "Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R3. Priority Queues."

Similar presentations


Ads by Google