30
CS 2230 CS II: Data structures Meeting 31: Priority queue Brandon Myers University of Iowa

CS 2230 CS II: Data structureshomepage.cs.uiowa.edu/~bdmyers/cs2230_fa17/public/... · 2017-11-30 · CS 2230 CS II: Data structures Meeting 32: Priority queue implementations, binary

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS 2230 CS II: Data structureshomepage.cs.uiowa.edu/~bdmyers/cs2230_fa17/public/... · 2017-11-30 · CS 2230 CS II: Data structures Meeting 32: Priority queue implementations, binary

CS2230CSII:Datastructures

Meeting31:PriorityqueueBrandonMyers

UniversityofIowa

Page 2: CS 2230 CS II: Data structureshomepage.cs.uiowa.edu/~bdmyers/cs2230_fa17/public/... · 2017-11-30 · CS 2230 CS II: Data structures Meeting 32: Priority queue implementations, binary

Today’sLearningobjectives

• DescribehowaPriorityQueue wouldbeusedinaspecificrealapplication• InterpretcodethatusesPriorityQueue

Page 3: CS 2230 CS II: Data structureshomepage.cs.uiowa.edu/~bdmyers/cs2230_fa17/public/... · 2017-11-30 · CS 2230 CS II: Data structures Meeting 32: Priority queue implementations, binary

Highlevelviewofawebsearchengine

Preparationbeforeyousearchedthismorning

1.A”webcrawler”findsnewormodifiedpagesandputsthemintoaMap<String,List<String>>where• keysarewordsfoundinthepage• valuesarethelistofURLswherethatwordisfound(dog,[www.petco.com,en.wikipedia.org/wiki/Dog])(leash,[www.petco.com,https://en.wikipedia.org/wiki/Leash,amazon.com])(Iowa,[uiowa.edu,en.wikipedia.org/wiki/Iowa,azdailysun.com/.../iowa/article...])

Page 4: CS 2230 CS II: Data structureshomepage.cs.uiowa.edu/~bdmyers/cs2230_fa17/public/... · 2017-11-30 · CS 2230 CS II: Data structures Meeting 32: Priority queue implementations, binary

Highlevelviewofawebsearchengine

Preparationbeforeyousearchedthismorning

1.A”webcrawler”findsnewormodifiedpagesandputsthemintoaMap<String,List<String>>where• keysarewordsfoundinthepage• valuesarethelistofURLswherethatwordisfound(dog,[www.petco.com,en.wikipedia.org/wiki/Dog])(leash,[www.petco.com,https://en.wikipedia.org/wiki/Leash,amazon.com])(Iowa,[uiowa.edu,en.wikipedia.org/wiki/Iowa,azdailysun.com/.../iowa/article...])

2.Abaserankingofeachpageiscalculatedbasedonfeatureslike• howrelevantthewebsiteisforthatsearchterm• thequalityofthewebsite• ...

(dog,[1en.wikipedia.org/wiki/Dog,2www.petco.com,...])(leash,[1www.petco.com,1amazon.com,5https://en.wikipedia.org/wiki/Leash,...])(Iowa,[1uiowa.edu,1en.wikipedia.org/wiki/Iowa,...,944azdailysun.com/.../iowa/article,...])

Listofrankedpagescouldinsteadbea“priorityqueue”thatsortspagesbyrank

Page 5: CS 2230 CS II: Data structureshomepage.cs.uiowa.edu/~bdmyers/cs2230_fa17/public/... · 2017-11-30 · CS 2230 CS II: Data structures Meeting 32: Priority queue implementations, binary

Whenyousearch

note:Inrealsearchengines,thecombinationofwords isvitallyimportanttoranking.Forexample,wikipedia’s rankfor“iowa dogs”willbemuchlowerthanitsrankingsfortheindividualwords“iowa”and“dog"

(Iowa,[1en.wikipedia.org/wiki/Iowa,1iowa.gov,1uiowa.edu, ...,944azdailysun.com/.../iowa/article])

lookupkey“iowa”

returnjustthetop10rankedresultsatatime

Page 6: CS 2230 CS II: Data structureshomepage.cs.uiowa.edu/~bdmyers/cs2230_fa17/public/... · 2017-11-30 · CS 2230 CS II: Data structures Meeting 32: Priority queue implementations, binary

DescribehowaPriorityQueue wouldbeusedinaspecificrealapplicationSituationswheretheorderanelemententersthequeuedoesn’tdeterminetheorderitleaves,duetoprioritiesscenario element enterqueuewhen prioritydetermined

bywebcrawlerinasearch engine

awebpage webcrawlerfindstheneworchangedpage

relevancetokeyword,qualityofsite,...

? ? ? ?

https://b.socrative.com/login/student/CS2230Aids1000-4999CS2230Bids5000+

giveanexamplescenario(limityoursubmittedanswertothiscolumn)

fillintheother3columnsforyourscenario

Page 7: CS 2230 CS II: Data structureshomepage.cs.uiowa.edu/~bdmyers/cs2230_fa17/public/... · 2017-11-30 · CS 2230 CS II: Data structures Meeting 32: Priority queue implementations, binary

PriorityqueueapplicationsSituationswheretheorderanelemententersthequeuedoesn’tdeterminetheorderitleaves,duetoprioritiesscenario element enterqueuewhen prioritydetermined

bywebcrawlerinasearch engine

awebpage webcrawlerfindstheneworchangedpage

relevancetokeyword,qualityofsite,...

airtrafficcontrol airplane theflyingairplanegets closetotheairport

late flightsgofirst,planeslowonfuelgofirst,...

UIcoursewaitlist student registerforaclass classlevel,whetheryouareinthemajor,...

Page 8: CS 2230 CS II: Data structureshomepage.cs.uiowa.edu/~bdmyers/cs2230_fa17/public/... · 2017-11-30 · CS 2230 CS II: Data structures Meeting 32: Priority queue implementations, binary

AnADTforpriorityqueuesinterface PriorityQueue<P extends Comparable<P>,V> {

// adds value with the given priorityvoid insert (P priority, V value);

// returns and removes the value with minimum priorityV deleteMin();

// returns the value with the minimum keyV min();

int size(); // return # of entriesboolean isEmpty(); // return true if empty

}

Page 9: CS 2230 CS II: Data structureshomepage.cs.uiowa.edu/~bdmyers/cs2230_fa17/public/... · 2017-11-30 · CS 2230 CS II: Data structures Meeting 32: Priority queue implementations, binary

InterpretcodethatusesPriorityQueue

PriorityQueue<Integer,String>q=//createit;q.insert(3,“Cat”)q.insert(5,“Doggy”)q.insert(2,“Jo”)print(q.min()+““)println(q.deleteMin()+““)q.insert(2,“Oh”)q.deleteMin()println(q.deleteMin()+““)

Whatdoestheprogramprint?

https://b.socrative.com/login/student/roomCS2230Xids1000-2999roomCS2230Yids3000+

Page 10: CS 2230 CS II: Data structureshomepage.cs.uiowa.edu/~bdmyers/cs2230_fa17/public/... · 2017-11-30 · CS 2230 CS II: Data structures Meeting 32: Priority queue implementations, binary

Today’sLearningobjectives

• DescribehowaPriorityQueue wouldbeusedinaspecificrealapplication• InterpretcodethatusesPriorityQueue

Page 11: CS 2230 CS II: Data structureshomepage.cs.uiowa.edu/~bdmyers/cs2230_fa17/public/... · 2017-11-30 · CS 2230 CS II: Data structures Meeting 32: Priority queue implementations, binary

CS2230CSII:DatastructuresMeeting32:Priorityqueueimplementations,binary

heapsBrandonMyers

UniversityofIowa

Page 12: CS 2230 CS II: Data structureshomepage.cs.uiowa.edu/~bdmyers/cs2230_fa17/public/... · 2017-11-30 · CS 2230 CS II: Data structures Meeting 32: Priority queue implementations, binary

Today’sLearningobjectives

• AnalyzerunningtimeofmethodsforvariousPriorityQueue implementations• Identifywhetherabinarytreeisaheap• Identifythearrayrepresentationofaheap• ExecuteheapalgorithmsforinsertanddeleteMin• WriteanalgorithmthatusesaPriorityQueue

Page 13: CS 2230 CS II: Data structureshomepage.cs.uiowa.edu/~bdmyers/cs2230_fa17/public/... · 2017-11-30 · CS 2230 CS II: Data structures Meeting 32: Priority queue implementations, binary

Useanarraythatstoreselementsinarbitraryorder.Whatistheinserttime?WhatisthedeleteMin time?

10 14 4 15 7 21

insert,deleteMin

a) O(1)amortized,O(logn)b) O(logn),O(logn)c) O(n),O(1)d) O(n),O(n)e) O(1)amortized,O(n)

https://b.socrative.com/login/student/roomCS2230Xids1000-2999roomCS2230Yids3000+

AnalyzerunningtimeofmethodsforvariousPriorityQueue implementations

Page 14: CS 2230 CS II: Data structureshomepage.cs.uiowa.edu/~bdmyers/cs2230_fa17/public/... · 2017-11-30 · CS 2230 CS II: Data structures Meeting 32: Priority queue implementations, binary

AnalyzerunningtimeofmethodsforvariousPriorityQueue implementations

Useanarraythatstoreselementsinsortedorder inalinkedlist.Whatistheinserttime?WhatisthedeleteMin time?

insert,deleteMin

a) O(1),O(logn)b) O(n),O(logn)c) O(n),O(1)d) O(logn),O(1)e) O(n),O(n)

7 12 \2

https://b.socrative.com/login/student/roomCS2230Xids1000-2999roomCS2230Yids3000+

Page 15: CS 2230 CS II: Data structureshomepage.cs.uiowa.edu/~bdmyers/cs2230_fa17/public/... · 2017-11-30 · CS 2230 CS II: Data structures Meeting 32: Priority queue implementations, binary

binaryheapanotherrepresentationforapriorityqueueisabinarytreefilledtoptobottomthathasthe

heapproperty• foraminheap:Keyatanode≥Keyatitsparent• foramaxheap:Keyatanode≤ Keyatitsparent

exampleofabinaryminheap

Page 16: CS 2230 CS II: Data structureshomepage.cs.uiowa.edu/~bdmyers/cs2230_fa17/public/... · 2017-11-30 · CS 2230 CS II: Data structures Meeting 32: Priority queue implementations, binary

Whichofthefollowingisavalidbinarymaxheap?

a)

b)

Identifywhetherabinarytreeisaheap

c)

d)

16

13

142

20

17

https://b.socrative.com/login/student/roomCS2230Xids1000-2999roomCS2230Yids3000+

Page 17: CS 2230 CS II: Data structureshomepage.cs.uiowa.edu/~bdmyers/cs2230_fa17/public/... · 2017-11-30 · CS 2230 CS II: Data structures Meeting 32: Priority queue implementations, binary

Arrayrepresentationofabinaryheap

forparentatindexk,childnodesarestoredatleft=2kright=2k+1parent= 𝑘/2

weareleavingindex0empty...why?

Page 18: CS 2230 CS II: Data structureshomepage.cs.uiowa.edu/~bdmyers/cs2230_fa17/public/... · 2017-11-30 · CS 2230 CS II: Data structures Meeting 32: Priority queue implementations, binary

Identifythearrayrepresentationofaheap

Whatisthearrayrepresentationofthisminheap?

A

B E

C D F G

H

https://b.socrative.com/login/student/roomCS2230Xids1000-2999roomCS2230Yids3000+

giveyouranswerasacommaseparatedlist

Page 19: CS 2230 CS II: Data structureshomepage.cs.uiowa.edu/~bdmyers/cs2230_fa17/public/... · 2017-11-30 · CS 2230 CS II: Data structures Meeting 32: Priority queue implementations, binary

Projectnotes

Page 20: CS 2230 CS II: Data structureshomepage.cs.uiowa.edu/~bdmyers/cs2230_fa17/public/... · 2017-11-30 · CS 2230 CS II: Data structures Meeting 32: Priority queue implementations, binary

Insertingintoaheap

putitinthenextavailablespot

Page 21: CS 2230 CS II: Data structureshomepage.cs.uiowa.edu/~bdmyers/cs2230_fa17/public/... · 2017-11-30 · CS 2230 CS II: Data structures Meeting 32: Priority queue implementations, binary

“bubbleup”“bubbleup”

bubbleuptheinsertednodeuntilitsparentissmallerthanit

Page 22: CS 2230 CS II: Data structureshomepage.cs.uiowa.edu/~bdmyers/cs2230_fa17/public/... · 2017-11-30 · CS 2230 CS II: Data structures Meeting 32: Priority queue implementations, binary

ExecuteheapalgorithmsforinsertanddeleteMin

draw theheapafterGisinserted

whatlevelisGat?a)1(whereAisnow)b)2c)3d)4(whereMisnow)

https://b.socrative.com/login/student/roomCS2230Xids1000-2999roomCS2230Yids3000+

A

F J

H P Q N

M

Page 23: CS 2230 CS II: Data structureshomepage.cs.uiowa.edu/~bdmyers/cs2230_fa17/public/... · 2017-11-30 · CS 2230 CS II: Data structures Meeting 32: Priority queue implementations, binary

Deletingfromabinaryheap

deleteMin

Page 24: CS 2230 CS II: Data structureshomepage.cs.uiowa.edu/~bdmyers/cs2230_fa17/public/... · 2017-11-30 · CS 2230 CS II: Data structures Meeting 32: Priority queue implementations, binary

Deletingfromabinaryheap

deleteMin

Page 25: CS 2230 CS II: Data structureshomepage.cs.uiowa.edu/~bdmyers/cs2230_fa17/public/... · 2017-11-30 · CS 2230 CS II: Data structures Meeting 32: Priority queue implementations, binary

draw theheapafterdeleteMin()

whatlevelisMat?a)1(whereAisnow)b)2c)3d)4(whereMisnow)

https://b.socrative.com/login/student/roomCS2230Xids1000-2999roomCS2230Yids3000+

A

F J

H P Q N

M

ExecuteheapalgorithmsforinsertanddeleteMin

Page 26: CS 2230 CS II: Data structureshomepage.cs.uiowa.edu/~bdmyers/cs2230_fa17/public/... · 2017-11-30 · CS 2230 CS II: Data structures Meeting 32: Priority queue implementations, binary

AnalyzerunningtimeofmethodsforvariousPriorityQueue implementations

UsingabinaryminheapforourPriorityqueue,whatistheinsertanddeleteMin time?

insert,deleteMin

a) O(1),O(logn)b) O(logn),O(1)c) O(n),O(1)d) O(logn),O(logn)e) O(n),O(n) https://b.socrative.com/login/student/

roomCS2230Xids1000-2999roomCS2230Yids3000+

Page 27: CS 2230 CS II: Data structureshomepage.cs.uiowa.edu/~bdmyers/cs2230_fa17/public/... · 2017-11-30 · CS 2230 CS II: Data structures Meeting 32: Priority queue implementations, binary

WriteanalgorithmthatusesaPriorityQueue

Nexttopicissorting.Howwouldyousortanarrayofnumbers(smalltolarge)usingPriorityQueue?

voidsort(int[]number){

}

interface PriorityQueue<P extendsComparable<P>,V> {

void insert (P priority, V value);V deleteMin();V min();int size();boolean isEmpty();

}

https://gist.github.com/createPublicGistpostURLtoSocrative

*AssumethereisanclasscalledHeapPriorityQueuethatimplementsPriorityQueue

Page 28: CS 2230 CS II: Data structureshomepage.cs.uiowa.edu/~bdmyers/cs2230_fa17/public/... · 2017-11-30 · CS 2230 CS II: Data structures Meeting 32: Priority queue implementations, binary

Today’sLearningobjectives

• AnalyzerunningtimeofmethodsforvariousPriorityQueue implementations• Identifywhetherabinarytreeisaheap• Identifythearrayrepresentationofaheap• ExecuteheapalgorithmsforinsertanddeleteMin• WriteanalgorithmthatusesaPriorityQueue

Page 29: CS 2230 CS II: Data structureshomepage.cs.uiowa.edu/~bdmyers/cs2230_fa17/public/... · 2017-11-30 · CS 2230 CS II: Data structures Meeting 32: Priority queue implementations, binary

resources

• animationsofheapshttp://www.cs.usfca.edu/~galles/visualization/Heap.html

Page 30: CS 2230 CS II: Data structureshomepage.cs.uiowa.edu/~bdmyers/cs2230_fa17/public/... · 2017-11-30 · CS 2230 CS II: Data structures Meeting 32: Priority queue implementations, binary

acknowledgements

heapinsert/deleteMin diagramshttp://homepage.divms.uiowa.edu/~ghosh/2116.8.pdf