180 likes | 345 Views
Chapter 6: Heapsort. Combines the good qualities of insertion sort (sort in place) and merge sort (speed) Based on a data structure called a “binary heap” Binary heap is an array viewed as a (nearly complete) binary tree. each node has an index and A[k] is displayed at node k
E N D
Chapter 6: Heapsort Combines the good qualities of insertion sort (sort in place) and merge sort (speed) Based on a data structure called a “binary heap” Binary heap is an array viewed as a (nearly complete) binary tree each node has an index and A[k] is displayed at node k index of root = 1 Indices increase from left to right
parent – child relationships let i be the index of any node, then if that node has a “parent”, its index is i/2 if that node has a “left child”, its index is 2i if that node has a “right child”, its index is 2i+1 heap-size[A] <lenght[A] sometime useful to have elements of A not in heap
Max and Min Heaps max-heap: for every node i (other than root) A[parent(i)] > A[i] largest element at root max-heaps as basis for heapsort and priority queue min-heap: every node i (other than root) A[parent(i)] < A[i] smallest element at root pseudocodes essentially same as those described for max-heaps height of any node = number edges in longest path to a leaf height of heap = height of root for nearly complete binary heap heigth = lg(n) when heap-size[A] = n in general, runtimes of basic heap operations are O(lg n) (i.e. bounded by height of heap)
Basic Heap Procedures max-heapify maintains max-heap property A[parent(i)] > A[i] build-max-heap produces a max-heap from unordered input array heapsort sorts in place with runtime O(n lg n) max-heap-insert (put application into prioity queue) heap-maximum (find the application with maximum priority) heap-extract-max (get the application with the maximum priority, heap-increase-key (change the priority of an application)
Max Heapify recursively restores max heap property Inputs are an array A and an index i. If max-heap property is violated at node i exhange A[i] with larger child Correcting max heap property at node i may violate it in subtrees Left[i] or Right[i]; hence, call Max-Heapify(A,largest)
In an incomplete binary tree with n nodes, the size of the larger subtree of the root is, at most, 2n/3 Assume left subtree is larger by one level Note height of heap by hmax Let r be n/(size of left subtree) hmax r 2 3/5 = 0.6 3 7/11 = 0.6363 4 15/23 = 0.6522 r = 2/3 in the limit of very large hmax (part of hw7) Worst_case runtime of max heapify satisfies the recurrence T(n) = T(2n/3) + Q(1) (overhead is time to find “largest”) By Master theorem (case 2) T(n) = O(lg n) = O(hmax)
Build max heap(A) n←length(A) for k←|_n/2_| down to 1 do Max-Heapify(A,k) Before Build max heap(A) contains violations of max heap property Start at first non-leaf, work back to root with calls to max heapify
Build max heap(A) n←length(A) for k←|_n/2_| down to 1 do Max-Heapify(A,k) A calls to max heapify for node at height h cost O(h) How many calls at height h?
Run time of build max heap A complete binary heap with n nodes has (n+1)/2h+1 nodes or leaves at height h (part of hw7)
Heap Sort Build-Max-Heap(A) creates heap with largest element in A[1] at root Exchange A[1] and A[n] Reduce heap-size by 1 (drops A[n] out of heap) Call Max Heapify Repeat until heap-size is one . Heapsort(A) Build-Max-Heap(A) for i length[A] downto 2 do exchange A[1] A[i] heap-size[A] heap-size[A] –1 Max-Heapify(A,1) Note: sorted in place Running time: Build-Max-Heap(A) cost O(n) n-1 calls to Max-Heapify each cost O(lg n) Total runtime of Heapsort = O(n lg n) asymptotically optimal
Priority Queues: A data structure that maintains a set of elements S that are associated with a value (their priority) called a key A max-priority queue involves the following operations Insert(S, x) inserts element x into set S Maximum(S) returns the element of S with the largest key Extract-Max(S) removes the element of S with the largest key Increase-Key(S, x, k) increases to key of x to a value k Max-priority queue could be a job scheduler. Insert puts jobs in the queue at any time. Extract-Max gets the highest priority job whenever space is available. Increase-Key changes priority of jobs in the queue when appropriate Min-priority queue could be used in computer simulation keyis the time to the next event. Extract-Min finds the event in S with the smallest key Insert puts events in S because simulation of on event generates others Decrease-Key after simulation of event, update the time to all subsequent events.
Implementation of Max Heap as Priority Queue Heap-Maximum(A) return A[1] root of a max-heap has largest value T(n) = Q(1) because independent of queue size Heap-Extract-Max(A) Ifheap-size[A] <1 then error “heap underflow”max A[1] A[1] A[heap-size[A]] heap-size[A] heap-size[A] –1 Max-Heapify(A,1) returnmax Runs in O(lg n) Operations in addition to Max-Heapify are independent of queue size
Implementation of Max Heap as Priority Queue Heap-Increase-Key(A,i,key) increases the priority of ith element If key < A[i] then error “new key < current key” A[i] key while i >1 and A[parent(i)] < A[i] doexchange A[i] A[parent(i)] i parent(i) Changing A[i] to key is likely to violate the max-heap property A[parent(i)] > A[i]. To reinstate max-heap property must follow a path from i back toward the root exchanging A[i] with its parent as needed.
Example Heap-Increase-Key 4 15 15 15
Implementation of Max Heap as Priority Queue Max-Heap-Insert(A,key) inserts a new element into queue heap-size[A] heap-size[A] + 1 A[heap-size[A]] - Heap-Increase-Key(A, heap-size[A],key) Add a leaf, assign value key to leaf, reinstate max-heap property.
CptS 450 Spring 2014 [All problems are from Cormen et al, 3rd Edition] Homework Assignment 7: due 4/3/2014 1. ex 6.1-1 p 153 2. ex 6.1-2 p 153 3. Show that in an incomplete binary tree with n nodes, the size of the larger subtree of the root is, at most, 2n/3 4. Show that a complete binary tree with n nodes will have (n+1)/2h+1 nodes or leaves at height h