Upload
others
View
3
Download
0
Embed Size (px)
Citation preview
CS2230CSII:Datastructures
Meeting31:PriorityqueueBrandonMyers
UniversityofIowa
Today’sLearningobjectives
• DescribehowaPriorityQueue wouldbeusedinaspecificrealapplication• InterpretcodethatusesPriorityQueue
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...])
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
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
DescribehowaPriorityQueue wouldbeusedinaspecificrealapplicationSituationswheretheorderanelemententersthequeuedoesn’tdeterminetheorderitleaves,duetoprioritiesscenario element enterqueuewhen prioritydetermined
bywebcrawlerinasearch engine
awebpage webcrawlerfindstheneworchangedpage
relevancetokeyword,qualityofsite,...
? ? ? ?
https://b.socrative.com/login/student/CS2230Aids1000-4999CS2230Bids5000+
giveanexamplescenario(limityoursubmittedanswertothiscolumn)
fillintheother3columnsforyourscenario
PriorityqueueapplicationsSituationswheretheorderanelemententersthequeuedoesn’tdeterminetheorderitleaves,duetoprioritiesscenario element enterqueuewhen prioritydetermined
bywebcrawlerinasearch engine
awebpage webcrawlerfindstheneworchangedpage
relevancetokeyword,qualityofsite,...
airtrafficcontrol airplane theflyingairplanegets closetotheairport
late flightsgofirst,planeslowonfuelgofirst,...
UIcoursewaitlist student registerforaclass classlevel,whetheryouareinthemajor,...
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
}
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+
Today’sLearningobjectives
• DescribehowaPriorityQueue wouldbeusedinaspecificrealapplication• InterpretcodethatusesPriorityQueue
CS2230CSII:DatastructuresMeeting32:Priorityqueueimplementations,binary
heapsBrandonMyers
UniversityofIowa
Today’sLearningobjectives
• AnalyzerunningtimeofmethodsforvariousPriorityQueue implementations• Identifywhetherabinarytreeisaheap• Identifythearrayrepresentationofaheap• ExecuteheapalgorithmsforinsertanddeleteMin• WriteanalgorithmthatusesaPriorityQueue
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
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+
binaryheapanotherrepresentationforapriorityqueueisabinarytreefilledtoptobottomthathasthe
heapproperty• foraminheap:Keyatanode≥Keyatitsparent• foramaxheap:Keyatanode≤ Keyatitsparent
exampleofabinaryminheap
Whichofthefollowingisavalidbinarymaxheap?
a)
b)
Identifywhetherabinarytreeisaheap
c)
d)
16
13
142
20
17
https://b.socrative.com/login/student/roomCS2230Xids1000-2999roomCS2230Yids3000+
Arrayrepresentationofabinaryheap
forparentatindexk,childnodesarestoredatleft=2kright=2k+1parent= 𝑘/2
weareleavingindex0empty...why?
Identifythearrayrepresentationofaheap
Whatisthearrayrepresentationofthisminheap?
A
B E
C D F G
H
https://b.socrative.com/login/student/roomCS2230Xids1000-2999roomCS2230Yids3000+
giveyouranswerasacommaseparatedlist
Projectnotes
Insertingintoaheap
putitinthenextavailablespot
“bubbleup”“bubbleup”
bubbleuptheinsertednodeuntilitsparentissmallerthanit
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
Deletingfromabinaryheap
deleteMin
Deletingfromabinaryheap
deleteMin
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
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+
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
Today’sLearningobjectives
• AnalyzerunningtimeofmethodsforvariousPriorityQueue implementations• Identifywhetherabinarytreeisaheap• Identifythearrayrepresentationofaheap• ExecuteheapalgorithmsforinsertanddeleteMin• WriteanalgorithmthatusesaPriorityQueue
resources
• animationsofheapshttp://www.cs.usfca.edu/~galles/visualization/Heap.html
acknowledgements
heapinsert/deleteMin diagramshttp://homepage.divms.uiowa.edu/~ghosh/2116.8.pdf