403: algorithms and data structures heapsort and …petko/classes/fall16-csi403-slides/07...403:...
TRANSCRIPT
403:AlgorithmsandDataStructures
HeapsortandPriorityQueues
Fall2016UAlbany
ComputerScienceSomeslidesborrowedfromDavidLuebke1
Context• Wedefinedheaps– ”almost”completebinarytrees– A[Parent(i)]≥A[i] forallnodesi>1
• HeapoperaIons:Heapify()– FixasingleviolaIonoftheheapproperty– “Float”valuesdownthetree– O(logn),wherenistheheapsize
• Whatisthebaseofthelog?16
14 10
8 7 9 3
2 4 1
16 14 10 8 7 9 3 2 4 1A= =
2
HeapOperaIons:BuildHeap()
• Input:ArrayA[1…n]• Required:ConvertAintoaheap• Idea:buildaheapinabo[om-upmannerbyrunningHeapify()onsuccessivesubarrays
3
HeapOperaIons:BuildHeap()
• Fact:forarrayoflengthn,allelementsinrangeA[⎣n/2⎦+1..n]areleaves(Why?)– Lec(⎣n/2⎦+1)=2*(⎣n/2⎦+1)>n
• Anotherfact:Leavesare(trivially)heaps• Walkbackwardsthroughthearrayfrom⎣n/2⎦to1,callingHeapify()oneachnode.– Orderofprocessingguaranteesthatthechildrenofnodeiareheapswheniisprocessed
– Whyisthisimportant?
4
BuildHeap()// given an unsorted array A, make A a heap BuildHeap(A) { heap_size(A) = length(A); for (i = ⎣length[A]/2⎦downto 1) Heapify(A, i); }
5
CrudeAnalysisofBuildHeap()
• EachcalltoHeapify()takesO(lgn)Ime• ThereareO(n)suchcalls(specifically,⎣n/2⎦)• ThustherunningImeisO(nlgn)– Isthisacorrectasympto2cupperbound?– Isthisanasympto2cally2ghtbound?
• AIghterboundisO(n)– Howcanthisbe?Isthereaflawintheabovereasoning?
7
AnalyzingBuildHeap():Tight• ToHeapify()asubtreetakesO(h)Imewherehistheheightofthesubtree– h=O(lgm),m=#nodesinsubtree– IntuiIon:Theheightofmostsubtreesissmall,i.e.O(logn)istoo“generous”
• Fact1:ann-elementheaphasatmost⎡n/2h+1⎤nodesofheighth– Proof?
• UsingFact1wecanshowthatBuildHeap()takesO(n)Ime– Proof?
8
Heapsort• GivenBuildHeap(),anin-placesorIngalgorithmiseasilyconstructed:– MaximumelementisatA[1]– DiscardbyswappingwithelementatA[n]
• Decrementheap_size[A]• A[n]nowcontainscorrectvalue
– RestoreheappropertyatA[1]bycallingHeapify()
– Repeat,alwaysswappingA[1]forA[heap_size(A)]
9
HeapsortHeapsort(A) { BuildHeap(A); for (i = length(A) downto 2) { Swap(A[1], A[i]); heap_size(A) -= 1; Heapify(A, 1); } }
11
AnalyzingHeapsort
• ThecalltoBuildHeap()takesO(n)Ime• Eachofthen-1callstoHeapify()takesO(lgn)Ime
• ThusthetotalImetakenbyHeapSort()=O(n)+(n-1)O(lgn)=O(n)+O(nlgn)=O(nlgn)
12
PriorityQueues
• Heapsortisanicealgorithm,butinpracIceQuicksort(comingup)usuallywins
• ButtheheapdatastructureisincrediblyusefulforimplemenIngpriorityqueues– AdatastructureformaintainingasetSofelements,eachwithanassociatedvalueorkey
– SupportstheoperaIonsInsert(),Maximum(),andExtractMax()
– Whatmightapriorityqueuebeusefulfor?
13
PriorityQueueOperaIons
• Insert(S,x)insertstheelementxintosetS• Maximum(S)returnstheelementofSwiththemaximumkey
• ExtractMax(S)removesandreturnstheelementofSwiththemaximumkey
• Howcouldweimplementtheseopera2onsusingaheap?
16
PriorityQueueOperaIons• Insert(S,x)– Incrementheapsizeandaddxattheend– movethenewelement“upwards”(reverse-heapify)– O(logn)
• Maximum(S)– returnS[1]– Timecomplexity?
• ExtractMax(S)removesandreturnstheelementofSwiththemaximumkey– saveS[1],placeS[heap_size(S)]inS[1];Heapify(S,1)– Time?
17
HeapvsAll(forPriorityqueues)
18
DataStructure
Pre-processing
Insert Max ExtractMax
LinkedList O(n) O(1) O(n) O(n)
SortedArray O(nlogn) O(n)(shicing)
O(1) O(1)
Heap O(n) O(logn) O(1) O(logn)