403: Algorithms and Data Structures Heapsort and …petko/classes/FALL16-CSI403-slides/07...403:...

19
403: Algorithms and Data Structures Heapsort and Priority Queues Fall 2016 UAlbany Computer Science Some slides borrowed from David Luebke 1

Transcript of 403: Algorithms and Data Structures Heapsort and …petko/classes/FALL16-CSI403-slides/07...403:...

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

BuildHeap()Example

•  WorkthroughexampleA={4,1,3,2,16,9,10,14,8,7}

4

1 3

2 16 9 10

14 8 7

6

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

Example

10

16

14 10

8 7 9 3

2 4 1

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

14

Assassin'sprioriIzedTODOmanager

15

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)

Announcements

•  ReadthroughChapter6– Nextclass:Buildheap,HeapSort,PriorityQueues

•  HW2duenextWednesday

19