Presentation is loading. Please wait.

Presentation is loading. Please wait.

Priority Queues A priority queue is an ADT where:

Similar presentations


Presentation on theme: "Priority Queues A priority queue is an ADT where:"— 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) Can be implemented as a Binary Heap

2 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

3 Binary Heaps? 16 1 4 10 3 2 7 9 3 4 14 6 7 5 2 8 1 8 10 9

4 Observe the Index 18 9 12 8 10 7 5 1 4 3 18 12 9 8 10 7 5 1 4 3 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 i of a node, the index of its parent is i / 2 the indices of its children are 2i and 2i+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

5 Binary Heaps Not necessarily. But every sorted array is a
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

6 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 }

7 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

8 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: for i = n/2 downto 1 Heapify(A, i) endfor Transform the array with the bottom up heapify method

9 Binary Heaps: MakeHeap
Running time : Easy analysis: O(nlgn) More exact analysis (proof omitted): O(n)

10 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

11 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

12 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)?


Download ppt "Priority Queues A priority queue is an ADT where:"

Similar presentations


Ads by Google