Upload
others
View
2
Download
0
Embed Size (px)
Citation preview
Zip TreesRobertE.Tarjan
PrincetonUniversity&IntertrustTechnologies
Pitea,Sweden,9January2018
ToDon,whoinspiredmetodesignandanalyzealgorithms
ObservationsOverthelast60years,computerscientistshavedevelopedmanybeautifulandtheoreticallyefficientalgorithms.
Butcomputerscienceisstill ayoungfield.
Wehaveoftensettledforthefirst(goodenough)solution.
Itmaynotbethebest– thedesignspaceisrich.
Goal:simplicityIdentifythesimplestpossibleefficientmethodstosolvebasicproblems:algorithmsfrom“thebook”
ala“proofsfromthebook”(Erdős)Algorithmsassimpleaspossible,withprovable resourcebounds
forimportantinputclasses,andefficientinpractice
“Makeeverythingassimpleaspossible,butnotsimpler”- Einstein
Sometimes,simplicityiscritical,notjustdesirable,notablyinconcurrent algorithms.
Makingaconcurrentalgorithmcorrectisnotoriously hard.Thesimplertheunderlyingidea,thegreaterthechanceofsuccess.
Exampleofsimplicity:Zip Trees
(imagefromStreetArtonPinterest)
Dictionary:containsasetS ofitems,eachwithassociatedinformation.
Operations:Access(x):Determineifx isinS.Ifso,returnx’s information.
Insert(x):(x notinS)Insertx anditsinformation.
Delete(x):(x inS)Deletex anditsinformation.
BinarysearchUniverseofitems(orofaccesskeysorindexvalues)istotallyordered,allowingbinarycomparison
Binarysearch:MaintainS insortedorder.Tofindx inS:IfS empty,stop(failure).IfS non-empty,comparex tosomeitemy inS.Ifx =y,stop(success).Ifx <y,searchin{z inS|z <y}.Ifx >y,searchin{z inS|z >y}.
Implementation:binarysearchtree
F
M
X
P
D
B E
Binarytree:Eachnodex hasaleftchildx.leftandarightchildx.right,eitherorbothofwhichcanbenull.Anodeisbinary,unary,oraleaf ifithas0,1,or2nullchildren,respectively.
n =#(non-null)nodesdepthofx:lengthofpathfromroottox,roothasdepth0
heightofx:maxlengthofapathfromx toaleaf,leaveshavedepth0
Binarysearchtree
Items(keyplusdata)innodes,onepernode,insymmetricorder(in-order):itemsinleftsubtreeareless,itemsinrightsubtree aregreaterinkeyorder.
TofindanitemtakesO(d +1)time,whered =depthofitem’snode,orofnullnodereachedbysearchifitemisnotintree.
Binarysearchtree
F
M
X
P
D
B E
BestcaseAllleaveshavedepthswithin1:depth⌊lgn⌋.
(lg:base-twologarithm)
Canachieveiftreeisstatic(orinsertionorderdeterminedoff-line)
E
MB
F I L O RD TA
G S
Q
K
C JH P U
(Leaf)InsertionSearch.Replacenullbynodewithitem.InsertR F
M
X
P
D
B E
LeafInsertionSearch.Replacenullbynodewithitem.InsertR F
M
X
P
D
B E
R
WorstcaseNaturalbutbadinsertionorder:sorted.InsertA,B,C,D,E,F,G,…
Depthoftreeisn – 1.Worst-caseaccesscostisn.
=list!
A
B
C
D
E
F
G
Waystoimproveefficiency
• Balance: AVLtrees(Adelson-Velskii &Landis1962),red-blacktrees(Bayer1972;Guibas &Sedgewick1978),weakAVLtrees(Sen&Tarjan 2009)…
• Self-adjustment: Splaytrees(Sleator &Tarjan1983)
• Randomization:Zip trees
AnAVLtree(imagefromWikipedia)
Ared-blacktree(imagefromWikipedia)
ASplayTree
(imagebyJorgeStolfi)
Rotation(restructuringprimitive)
rotateatxrotateatyy
x
x
y
A B
C A
B C
z z
right
left
20
Zip Trees
Idea:Oninsertion,chooseaheightforanitemandinsertitatthegivenheight,orclosetoit.Chooseheightslikethoseinabest-caseBST:½thenodesatheight0,¼atheight1,1/8atheight2…
Choosetheheightsrandomly.
Wecannotchooseheightsexactly.
Instead,foreachnodetobeinsertedwechoosearank,asfollows:flipafaircoinandcountthenumberofheadsbeforethefirsttail.Therankofanodedoesnotchangewhileitisinthetree.
Therankofanodehasageometric distribution:anodehasrankk withprobability1/2k +1.
Wewanttheheightofanodetobewithinaconstantfactorofitsrank.
Zip*TreeAbinarysearchtreeinwhicheachnodehasarankchosenrandomlyoninsertion,withnodessymmetricallyorderedbykeyandheaporderedbyrank,breakingranktiesinfavorofsmallerkey:
x.left.key <x.key <x.right.keyx.left.rank <x.rankx.right.rank ≤x.rank
*Zip:“tomoveveryfast”
AZip Tree
F
P
X
R
D
C E
Q
Y
H
A0
1 0
0
1
21
2 3
MG0
3
11
Zip treeinsertion?
Rootinsertion(Stephenson1980)
Letx betheitemtobeinserted.Followthesearchpathforx,unzipping itbysplittingitintoapathPofnodeswithkeyslessthanthatofxandapathQofnodeswithkeysgreaterthanthatofx.MakethetopnodeofPtheleftchildofx andthetopnodeofQ therightchildofx.
F
P
X
R
D
C E
Q
Y
H
A MG
InsertN
F
P
X
R
D
C E
Q
Y
H
A MG
InsertN N
F
P
X
R
D
C E
Q
Y
H
A MG
InsertNUnzipsearchpath
N
FP
X
R
D
C E
Q
Y
H
A MG
N
Ziptreeinsertion:hybridofleaf&rootinsertion
(Sprugnoli 1980;Martinez&Roura 1998)
Toinsertx: Chooseitsrank.Searchforxuntilreachingthenodey thatx shouldreplace. Unziptherestofthesearchpath(fromydown)bysplittingitintoapathPofnodeswithkeyslessthanx’sandapathQofnodeswithkeysgreaterthanx’s.MakethetopofPtheleftchildofxandthetopofQ therightchildofx.Makex achildoftheoldparentofy.
F
P
X
R
D
C E
Q
Y
H
A0
1 0
0
1
21
2 3
MG0
3
11
F
P
X
R
D
C E
Q
Y
H
A0
1 0
0
1
21
2 3
MG0
3
11
InsertJ
F
P
X
R
D
C E
Q
Y
H
A0
1 0
0
1
21
2 3
MG0
3
11
InsertJrank=2
F
P
X
R
D
C E
Q
Y
H
A0
1 0
0
1
21
2 3
MG0
3
11
InsertJrank=2ReplaceH
F
P
X
R
D
C E
Q
Y
H
A0
1 0
0
1
21
2 3
MG0
3
11
InsertJrank=2ReplaceHUnzippathfromH
F
P
X
R
D
C E
Q
Y
H
A0
1 0
0
1
21
2 3
MG0
3
11
InsertJrank=2ReplaceHUnzippathfromH J2
F
P
X
R
D
C E
Q
Y
H
A0
1 0
0
1
21
2 3
MG0
3
11
J2
Aziptree(imagefromStreetArtonPinterest)
Ziptreedeletion:Inverseofinsertion
Searchforthenodex tobedeleted.Zip thepathfromx toitspredecessor(inkeyorder)withthepathfromx toitssuccessor(inkeyorder),bymergingthemindecreasingrankorder,breakingtiesinfavorofsmallerkey,toformasinglepathP.ReplacexasachildofitsparentbythetopnodeofP.
F
P
X
R
D
C E
Q
Y
H
A0
1 0
0
1
21
2 3
MG0
3
11
DeleteP
J2
F
P
X
R
D
C E
Q
Y
H
A0
1 0
0
1
21
2 3
MG0
3
11
DeletePZipthepathsfromPtoMandPtoQ
J2
F
X
R
D
C E
Q
Y
H
A0
1 0
0
1
21
2
MG0
3
11
DeletePZipthepathsfromPtoMandPtoQ J2
F
X
R
D
C E
Q
Y
H
A0
1 0
0
1
21
2
MG0
3
11
J2
Staticpropertiesofziptrees
Expectedrootrank:lgn +O(1)RootrankisO(logn)withhighprobabilityE(nodedepth)=1.5lgn+O(1)(Prodinger 1996)TreedepthisO(logn)withhighprobabilityTreestructureisuniquelydeterminedbyranks:
History-independentdatastructure
Dynamicpropertiesofzip trees
Expectedrestructuringtimetoinsertordeleteanodeofrankk =O(k).ExpectedrestructuringtimeisO(1)ProbabilityofrestructuringtakingO(k)timeisexponentiallysmallinkInsertion/deletioncanbedonepurelytop-downNorotationcases,justunzippingandzippingNoswappingondeletion
Arezip treesnew?
Azip treeisatreap (Aragon&Seidel1996)withadifferentwayofchoosingranksanddifferentinsertion/deletionalgorithms.Zip treesallowrankties;treaps don’t.Azip treeisabinary-treerepresentationofaskiplist(Pugh1990)
Atreap(imagefromWikipedia)
Askiplist(imagebyIgorOstrovsky)
Zip Trees:Summary
Insertionsanddeletionsaresimpleandefficient,takeO(1)expectedrestructuring,andarepurelytop-down
History-independentNorestructuringonaccessStandardbinarytreerepresentation:lessspacethanskiplists
O(lglgn)balancebitspernodeCanbreakranktiesbyusingfractionalranks,
improvesE(depth)by8%Canmodifytosupportfrequency-biasedaccess
Currentwork
Develop,analyze,andimplementefficientnon-blockingconcurrent zip trees
Thanks!