CS 2230 CS II: Data structureshomepage.cs.uiowa.edu/~bdmyers/cs2230_fa17/public/... ·...
Transcript of CS 2230 CS II: Data structureshomepage.cs.uiowa.edu/~bdmyers/cs2230_fa17/public/... ·...
![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](https://reader030.fdocuments.in/reader030/viewer/2022041102/5edca237ad6a402d66676225/html5/thumbnails/1.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022041102/5edca237ad6a402d66676225/html5/thumbnails/2.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022041102/5edca237ad6a402d66676225/html5/thumbnails/3.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022041102/5edca237ad6a402d66676225/html5/thumbnails/4.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022041102/5edca237ad6a402d66676225/html5/thumbnails/5.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022041102/5edca237ad6a402d66676225/html5/thumbnails/6.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022041102/5edca237ad6a402d66676225/html5/thumbnails/7.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022041102/5edca237ad6a402d66676225/html5/thumbnails/8.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022041102/5edca237ad6a402d66676225/html5/thumbnails/9.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022041102/5edca237ad6a402d66676225/html5/thumbnails/10.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022041102/5edca237ad6a402d66676225/html5/thumbnails/11.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022041102/5edca237ad6a402d66676225/html5/thumbnails/12.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022041102/5edca237ad6a402d66676225/html5/thumbnails/13.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022041102/5edca237ad6a402d66676225/html5/thumbnails/14.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022041102/5edca237ad6a402d66676225/html5/thumbnails/15.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022041102/5edca237ad6a402d66676225/html5/thumbnails/16.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022041102/5edca237ad6a402d66676225/html5/thumbnails/17.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022041102/5edca237ad6a402d66676225/html5/thumbnails/18.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022041102/5edca237ad6a402d66676225/html5/thumbnails/19.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022041102/5edca237ad6a402d66676225/html5/thumbnails/20.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022041102/5edca237ad6a402d66676225/html5/thumbnails/21.jpg)
“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](https://reader030.fdocuments.in/reader030/viewer/2022041102/5edca237ad6a402d66676225/html5/thumbnails/22.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022041102/5edca237ad6a402d66676225/html5/thumbnails/23.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022041102/5edca237ad6a402d66676225/html5/thumbnails/24.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022041102/5edca237ad6a402d66676225/html5/thumbnails/25.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022041102/5edca237ad6a402d66676225/html5/thumbnails/26.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022041102/5edca237ad6a402d66676225/html5/thumbnails/27.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022041102/5edca237ad6a402d66676225/html5/thumbnails/28.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022041102/5edca237ad6a402d66676225/html5/thumbnails/29.jpg)
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](https://reader030.fdocuments.in/reader030/viewer/2022041102/5edca237ad6a402d66676225/html5/thumbnails/30.jpg)
acknowledgements
heapinsert/deleteMin diagramshttp://homepage.divms.uiowa.edu/~ghosh/2116.8.pdf