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

403: Algorithms and Data Structures Heapsort and …petko/classes/FALL16-CSI403-slides/07...403: Algorithms and Data Structures Heapsort and Priority Queues Fall 2016 UAlbany Computer

Jul 09, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 403: Algorithms and Data Structures Heapsort and …petko/classes/FALL16-CSI403-slides/07...403: Algorithms and Data Structures Heapsort and Priority Queues Fall 2016 UAlbany Computer

403:AlgorithmsandDataStructures

HeapsortandPriorityQueues

Fall2016UAlbany

ComputerScienceSomeslidesborrowedfromDavidLuebke1

Page 2: 403: Algorithms and Data Structures Heapsort and …petko/classes/FALL16-CSI403-slides/07...403: Algorithms and Data Structures Heapsort and Priority Queues Fall 2016 UAlbany Computer

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

Page 3: 403: Algorithms and Data Structures Heapsort and …petko/classes/FALL16-CSI403-slides/07...403: Algorithms and Data Structures Heapsort and Priority Queues Fall 2016 UAlbany Computer

HeapOperaIons:BuildHeap()

•  Input:ArrayA[1…n]•  Required:ConvertAintoaheap•  Idea:buildaheapinabo[om-upmannerbyrunningHeapify()onsuccessivesubarrays

3

Page 4: 403: Algorithms and Data Structures Heapsort and …petko/classes/FALL16-CSI403-slides/07...403: Algorithms and Data Structures Heapsort and Priority Queues Fall 2016 UAlbany Computer

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

Page 5: 403: Algorithms and Data Structures Heapsort and …petko/classes/FALL16-CSI403-slides/07...403: Algorithms and Data Structures Heapsort and Priority Queues Fall 2016 UAlbany Computer

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

Page 6: 403: Algorithms and Data Structures Heapsort and …petko/classes/FALL16-CSI403-slides/07...403: Algorithms and Data Structures Heapsort and Priority Queues Fall 2016 UAlbany Computer

BuildHeap()Example

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

4

1 3

2 16 9 10

14 8 7

6

Page 7: 403: Algorithms and Data Structures Heapsort and …petko/classes/FALL16-CSI403-slides/07...403: Algorithms and Data Structures Heapsort and Priority Queues Fall 2016 UAlbany Computer

CrudeAnalysisofBuildHeap()

•  EachcalltoHeapify()takesO(lgn)Ime•  ThereareO(n)suchcalls(specifically,⎣n/2⎦)•  ThustherunningImeisO(nlgn)–  Isthisacorrectasympto2cupperbound?–  Isthisanasympto2cally2ghtbound?

•  AIghterboundisO(n)– Howcanthisbe?Isthereaflawintheabovereasoning?

7

Page 8: 403: Algorithms and Data Structures Heapsort and …petko/classes/FALL16-CSI403-slides/07...403: Algorithms and Data Structures Heapsort and Priority Queues Fall 2016 UAlbany Computer

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

Page 9: 403: Algorithms and Data Structures Heapsort and …petko/classes/FALL16-CSI403-slides/07...403: Algorithms and Data Structures Heapsort and Priority Queues Fall 2016 UAlbany Computer

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

Page 10: 403: Algorithms and Data Structures Heapsort and …petko/classes/FALL16-CSI403-slides/07...403: Algorithms and Data Structures Heapsort and Priority Queues Fall 2016 UAlbany Computer

Example

10

16

14 10

8 7 9 3

2 4 1

Page 11: 403: Algorithms and Data Structures Heapsort and …petko/classes/FALL16-CSI403-slides/07...403: Algorithms and Data Structures Heapsort and Priority Queues Fall 2016 UAlbany Computer

HeapsortHeapsort(A) { BuildHeap(A); for (i = length(A) downto 2) { Swap(A[1], A[i]); heap_size(A) -= 1; Heapify(A, 1); } }

11

Page 12: 403: Algorithms and Data Structures Heapsort and …petko/classes/FALL16-CSI403-slides/07...403: Algorithms and Data Structures Heapsort and Priority Queues Fall 2016 UAlbany Computer

AnalyzingHeapsort

•  ThecalltoBuildHeap()takesO(n)Ime•  Eachofthen-1callstoHeapify()takesO(lgn)Ime

•  ThusthetotalImetakenbyHeapSort()=O(n)+(n-1)O(lgn)=O(n)+O(nlgn)=O(nlgn)

12

Page 13: 403: Algorithms and Data Structures Heapsort and …petko/classes/FALL16-CSI403-slides/07...403: Algorithms and Data Structures Heapsort and Priority Queues Fall 2016 UAlbany Computer

PriorityQueues

•  Heapsortisanicealgorithm,butinpracIceQuicksort(comingup)usuallywins

•  ButtheheapdatastructureisincrediblyusefulforimplemenIngpriorityqueues– AdatastructureformaintainingasetSofelements,eachwithanassociatedvalueorkey

– SupportstheoperaIonsInsert(),Maximum(),andExtractMax()

– Whatmightapriorityqueuebeusefulfor?

13

Page 14: 403: Algorithms and Data Structures Heapsort and …petko/classes/FALL16-CSI403-slides/07...403: Algorithms and Data Structures Heapsort and Priority Queues Fall 2016 UAlbany Computer

14

Page 15: 403: Algorithms and Data Structures Heapsort and …petko/classes/FALL16-CSI403-slides/07...403: Algorithms and Data Structures Heapsort and Priority Queues Fall 2016 UAlbany Computer

Assassin'sprioriIzedTODOmanager

15

Page 16: 403: Algorithms and Data Structures Heapsort and …petko/classes/FALL16-CSI403-slides/07...403: Algorithms and Data Structures Heapsort and Priority Queues Fall 2016 UAlbany Computer

PriorityQueueOperaIons

•  Insert(S,x)insertstheelementxintosetS•  Maximum(S)returnstheelementofSwiththemaximumkey

•  ExtractMax(S)removesandreturnstheelementofSwiththemaximumkey

•  Howcouldweimplementtheseopera2onsusingaheap?

16

Page 17: 403: Algorithms and Data Structures Heapsort and …petko/classes/FALL16-CSI403-slides/07...403: Algorithms and Data Structures Heapsort and Priority Queues Fall 2016 UAlbany Computer

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

Page 18: 403: Algorithms and Data Structures Heapsort and …petko/classes/FALL16-CSI403-slides/07...403: Algorithms and Data Structures Heapsort and Priority Queues Fall 2016 UAlbany Computer

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)

Page 19: 403: Algorithms and Data Structures Heapsort and …petko/classes/FALL16-CSI403-slides/07...403: Algorithms and Data Structures Heapsort and Priority Queues Fall 2016 UAlbany Computer

Announcements

•  ReadthroughChapter6– Nextclass:Buildheap,HeapSort,PriorityQueues

•  HW2duenextWednesday

19