Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.

Similar presentations


Presentation on theme: "Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University."— Presentation transcript:

1 Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University

2 Priority Queues An efficient ADT to keep a dynamic set S of elements x to support the following operations : getMax(S) : return the element with the highest priority extractMax(S) : remove and return the element with the highest priority insert(x,S) : insert element x into S increaseKey(S,x,k) – increase x’s priority to k Priority queues are implemented using a heap, which is a tree structure with special properties.

3 Heap a tree that satisfies the heap property Heap property If B is a child node of A, then key(A)>=key(B) can be reverse (max heap, min heap) Heaps are used to implement priority queue Efficiency of heap operations is crucial in some graph algorithms

4 Binary Heap Has following properties shape property all levels of the tree, except possibly the last one (deepest) are fully filled, and, if the last level of the tree is not complete, the nodes of that level are filled from left to right. heap property

5 Operations (in max heap) Insert(x,S)  Add the element on the bottom level of the heap  Compare the added element with its parent; if they are in the correct order, stop  If not, swap the element with its parent and return to the previous step.

6 Operations (in max heap) getMax(S) : trivial - O(1) extractMax(S) : O(lg n) – tree height  Replace root node with the last element on the last level  repeat swapping parent-child until heap property becomes valid

7 Building a Heap O(n lg n) : A heap can be built by successive insertions - not optimal O(n) algorithm randomly putting the elements on a binary tree if all the subtrees starting at some height h (measured from the bottom) have already been "heapified", the trees at depth h+1 can be heapified by sending their root down, along the path of maximum children

8 Example: building a heap 4132169101487 1 2 3 4 5 6 7 8 9 10 4 1 2 16 7814 3 9 10 1 23 4567 89

9 Sorting with heaps: Heap-Sort We can use the heap structure to sort an array A[i] of n elements in place: Since the maximum element of the array is its first element, A[1], it can be put in its correct final position at the end of the array, in A[n]. We can now recursively fix the heap of the sub-tree rooted at node 1 and containing n – 1 elements with Max-Heapify until two elements are left. Each call to Max-Heapify takes O(lg n), and it is called once for each element in the array, so the running time is O(n lg n) always (best, average, and worst case) with O(n) space.

10 10 Heap-Sort Heap-Sort(A) 1. Build-Max-Heap(A) 2. heapsize[A]  length(A) 3. for i  length[A] downto 2 4. do Exchange(A[1],A[i]) 5. heapsize[A]  length(A) –1 6. Max-Heapify(A,1) put maximum at the root

11 Example: Heap-Sort 16 14 87 142 10 9 3 1 23 4567 89 1614108793241

12 Example: Heap-Sort (2) 14 8 47 1612 10 9 3 1 23 4567 89

13 Example: Heap-Sort (3) 10 8 47 16142 9 1 3 1 23 4567 89 10

14 Example: Heap-Sort (4) 9 8 47 161410 3 1 2 1 23 4567 89

15 Example: Heap-Sort (5) 7 4 12 161410 3 8 9 1 23 4567 89

16 Example: Heap-Sort (6) 4 2 17 161410 3 8 9 1 23 4567 89

17 Example: Heap-Sort (7) 1 2 47 161410 3 8 9 1 23 4567 89

18 Example: Heap-Sort (8) 1 2 47 161410 3 8 9 1 23 4567 89 1234789 1416


Download ppt "Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University."

Similar presentations


Ads by Google