Presentation is loading. Please wait.

Presentation is loading. Please wait.

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”

Similar presentations


Presentation on theme: "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”"— Presentation transcript:

1 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

2 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 parent – child relationships heap-size[A] < lenght[A] sometime useful to have elements of A not in heap

3 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) Max and Min Heaps

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

5 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) Find largest of A(i),A(l),A(r)

6 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 h max Let r be n/(size of left subtree) h max r 23/5 = 0.6 37/11 = 0.6363 415/23 = 0.6522 r = 2/3 in the limit of very large h max (part of hw7) Worst case runtime of max heapify satisfies the recurrence T(n) = T(2n/3) +  (1) (overhead is time to find “largest”) By Master theorem (case 2) T(n) = O(lg n) = O(h max )

7 Build max heap(A) n←length(A) for k←|_n/2_| down to 1 do Max-Heapify(A,k) Start at first non-leaf, work back to root with calls to max heapify Before Build max heap(A) contains violations of max heap property

8 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?

9 Run time of build max heap A complete binary heap with n nodes has (n+1)/2 h+1 nodes or leaves at height h (part of hw7) Cost of calls to max heapify Big O passes through sums Factor out (n+1)/2 OK to extend sum to infinity since looking for upper bound x=1/2; sum=2

10 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

11

12 CptS 450 Spring 2015 [All problems are from Cormen et al, 3rd Edition] Homework Assignment 9: due 4/1/2015 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)/2 h+1 nodes or leaves at height h

13 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 key is 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.

14 Implementation of Max Heap as Priority Queue Heap-Maximum(A) return A[1]root of a max-heap has largest value T(n) =  (1) because independent of queue size Heap-Extract-Max(A) If heap-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) return max Runs in O(lg n) Operations in addition to Max-Heapify are independent of queue size

15 Implementation of Max Heap as Priority Queue Heap-Increase-Key(A,i,key) increases the priority of i th element If key < A[i] then error “new key < current key” A[i]  key while i >1 and A[parent(i)] < A[i] do exchange A[i]  A[parent(i)] i  parent(i) Changing A[i] to key is likely to violate the max-heap property that 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.

16 4 15 Example Heap-Increase-Key

17 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. Implementation of Max Heap as Priority Queue

18 Summary: 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 Remember were to find these pseudo-codes and how to analyze their runtime.


Download ppt "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”"

Similar presentations


Ads by Google