This preview shows page 1. Sign up to view the full content.
Unformatted text preview: UMass Lowell Computer Science 91.404 Analysis of Algorithms
Prof. Karen Daniels
Fall, Fall 2011 Heap Lecture Chapter 6 Ch t
Use NOTES feature to see explanation accompanying some slides. PseudoPseudocode is from Cormen et al. textbook. Heap Basics Structure: 16 14 10 Nearly complete binary tree y p y Convenient array representation Parent's label not less than 2 Parent s that of each child
16 14 10 8 7 9 HEAP Property: (for MAX HEAP *) 8 4 3 2 7 1 4 1 9 3 1 2 3 4 5 6 7 8 9 10 * analogous property and operations for MIN HEAP Operations on a Heap
assuming array representation MAXHEAPIFY: MAXHEAPIFY: for a given node that is the root of a subtree, if both subtrees subtree, of that node are already HEAPs, MAXHEAPIFY enforces y , MAXthe maximum HEAP PROPERTY via "downward swaps" so that the node together with its subtrees form a MAX HEAP 16 14 8 7 10 9 3 2 4 1 16 14 10 8
1 2 3 4 7
5 9
6 3 2
7 8 4
9 1
10 Operations on a Heap
assuming array representation BUILDMAXHEAP: BUILDMAXHEAP: builds a MAX HEAP from scratch using MAXHEAPIFY MAX16 14 8 7 10 9 3 2 4 1 16 14 10 8
1 2 3 4 7
5 9
6 3 2
7 8 4
9 1
10 Building a Max Heap using MAXHEAPIFY vs. MAXHEAPINSERT MAX HEAPIFY vs MAX HEAP INSERT MAXMAXHEAPIFY 16 14 8 7 10 9 3
number of height levels MAXHEAPMAXHEAPINSERT swaps down compares parent with both children before each swap
lg n swaps up compares parent with one child before each swap
maximum number of swaps = length of path f from this level up to root number of height levels bound on number of nodes in this level n 2h+1 O(h) h =0 h = O n h h =0 2 lg n 2 4 1
maximum number of swaps = length of path from this level down to leaf n 0 2 h +1 ( lg n  h ) bound on number of h = nodes in this level lg n lg n 1 = O ( n lg n ) h h=0 2 lg n lg n n  h +1 h h=0 2 h h = O n h = O ( n 2) = O ( n ) h =0 2 h=0 1 = h 2 lg n 1 0 2 h= h 1 0 2 = 2 O(n lg n) h= O(n) as in HEAPIFY Asymptotic worstcase running time of BUILDMAXHEAP using MAXHEAPIFY is in worstBUILDMAXMAXO(n). However, using MAXHEAPINSERT the time would only be in O(n lg n). MAXHEAP Operations on a Heap
assuming array representation HEAPSORT: sorts an array by first using BUILDMAXBUILDMAXHEAP th repeatedly swapping out root then t dl i t t and calling MAXHEAPIFY 16 MAX14 8 7 10 9 3 2 4 1 16 14 10 8
1 2 3 4 7
5 9
6 3 2
7 8 4
9 1
10 Operations on a Max Heap
assuming array representation MAXHEAPIFY: MAXHEAPIFY: Asymptotic worstcase worstrunning time is in O(lg n). O(lg For a node at height h, time is in O(h). T(n) T(2n/3) + (1) T(2n/3) is in O(lgn) using O(lgn) Master Theorem BUILDMAXHEAP: BUILDMAXHEAP: lg n Asymptotic worstworstcase running time is lg n h in O(n lg n). = O n h However, this is a h =0 2 loose bound! Time is also in O(n). = O n h = O(n2) = O(n) h n 2h+1 O(h) h =0 h =0 2 HEAPSORT: HEAPSORT:
Asymptotic worstcase worstrunning time is in O(n lg n).
nO(lgn) O(n) + O(lgn) Operations on a Max Heap
assuming array representation PRIORITY QUEUE SUPPORT: MAXHEAPMAXHEAPINSERT adds new leaf to the tree and then "swaps up" to restore p p MAX HEAP PROPERTY MAX HEAP PROPERTY guarantees that maximum is at the root of a MAX HEAP removes the maximum value from the root by swapping it out restores MAX HEAP PROPERTY using MAXHEAPIFY MAX HEAPHEAP MAXIMUM HEAP EXTRACTHEAP EXTRACTMAX Applications: Applications: Job Scheduling, Event Scheduling Operations on a Max Heap
assuming array representation PRIORITY QUEUE SUPPORT: MAXHEAPINSERT MAXHEAPAsymptotic worstcase worstrunning time is in O(lg n). O(lg For a node at height h time is in O(h) h, O(h). HEAPHEAP MAXIMUM
Asymptotic worstcase y p worstrunning time is in O(1). HEAP EXTRACTHEAP EXTRACTMAX O(1) + Asymptotic worstcase worstO(lgn) O(lgn) running time is in O(lg n). O(lg For a node at height h, time is in O(h). ...
View
Full
Document
This note was uploaded on 02/13/2012 for the course CS 91.404 taught by Professor Dr.karendaniels during the Fall '09 term at UMass Lowell.
 Fall '09
 DR.KARENDANIELS
 Algorithms

Click to edit the document details