Chapter 6: Heapsort Combines the good qualities of insertion sort (sort in place) and merge sort...
-
Upload
griffin-warren -
Category
Documents
-
view
220 -
download
0
Transcript of Chapter 6: Heapsort Combines the good qualities of insertion sort (sort in place) and merge sort...
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
let i be the index of any node, thenif that node has a “parent”, its index is i/2if that node has a “left child”, its index is 2iif 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
max-heap: for every node i (other than root) A[parent(i)] > A[i]largest element at rootmax-heaps as basis for heapsort and priority queue
min-heap: every node i (other than root) A[parent(i)] < A[i]smallest element at rootpseudocodes 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 rootfor 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
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 childCorrecting 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)
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 levelNote height of heap by hmax
Let r be n/(size of left subtree)hmax r2 3/5 = 0.63 7/11 = 0.63634 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) + (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)
Start at first non-leaf, work back to root with calls to max heapify
Before Build max heap(A)contains violations of maxheap property
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 heapA complete binary heap with n nodes has (n+1)/2h+1 nodes or leaves at height h (part of hw7)
O(n) T(n)
x)-(1
xkx using 1)O(nT(n)
)2
h
2
11)O((nT(n)
)2
h
2
11)O((n T(n)
)2
h1)O((n T(n)
)2
1nhO(T(n)
2
1nO(h)T(n)
0k2
k
0hh
)nlg(
1hh
lg(n)
1h1h
lg(n)
1h1h
lg(n)
0h1h
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
Heap SortBuild-Max-Heap(A) creates heap with largest element in A[1] at rootExchange A[1] and A[n]Reduce heap-size by 1 (drops A[n] out of heap)Call Max HeapifyRepeat 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] –1Max-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
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 1532. ex 6.1-2 p 1533. Show that in an incomplete binary tree with n nodes, the size of the larger subtree of the root is, at most, 2n/34. Show that a complete binary tree with n nodes will have (n+1)/2h+1 nodes or leaves at height h
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 operationsInsert(S, x) inserts element x into set SMaximum(S) returns the element of S with the largest keyExtract-Max(S) removes the element of S with the largest keyIncrease-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 simulationkey is the time to the next event.Extract-Min finds the event in S with the smallest keyInsert puts events in S because simulation of on event generates othersDecrease-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) = (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] –1Max-Heapify(A,1)return max
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 elementIf key < A[i] then error “new key < current key”A[i] keywhile 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.
4 15
15
15
Example Heap-Increase-Key
Max-Heap-Insert(A,key) inserts a new element into queue
heap-size[A] heap-size[A] + 1A[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
Summary: A max-priority queue involves the following operations
Insert(S, x) inserts element x into set SMaximum(S) returns the element of S with the largest keyExtract-Max(S) removes the element of S with the largest keyIncrease-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.