mit6_851s12_ps10sol
TRANSCRIPT
-
8/13/2019 MIT6_851S12_ps10sol
1/2
6.851AdvancedDataStructures(Spring12)Prof.ErikDemaine
Problem10 SamplesolutionCompactbalancedparentheses.
1. ConstructagraphGwithavertexforeachblock,andanedgebetweenthetheblockcontain-ingeachparenthesisandtheblockcontaining itsmatch. ObservethatG isasimplegraph,since if twopioneer parenthesis lie inthe same block, their matchingparens cannot also liein the same block. We construct the following planar embedding for this graph: place theverticesinorderalongahorizontalline,anddrawtheedgesbelowthislinesuchthattheedgesdonotcross. Thisispossiblebecauseofthenestingpropertythattheparenthesispairsmustobey. Sinceaplanarembeddingexists,G isaplanargraphandthusthenumberofedges isatmostthreetimesthenumberofvertices. Thereare|S|/Bvertices,andthereisabijectionbetween
the
edges
and
pioneer
parenthesis,
thus
there
are
at
most
3|S|/B
=
O(|S|/B)
pioneer
parenthesis,asdesired. Forthetightboundof2|S|/B3,observethatthisgraphisalsoanouterplanargraph(asshownbythesameembedding).
2. DivideS intoblocksofsizeB= 1/2lg|S|.Makeatablegivingtheblockcontainingthematchingparenthesisofeachpioneerparen-
thesis.Makea rankdatastructureoverthebitvectorof pioneerparenthesis(1 forpioneer, 0
otherwise).MakearankdatastructureovertheopenparensinS,andanotheroverthecloseparens
inS.aMakealookuptableT1 whichgivenablockandanindexintotheblock( |S|1/2lg|S|
possibilities)returns itsmatchingparenif it is intheblock,orsay itisntthere.Make a lookup table T2 which given a block and an integer k between B and Ba
( |S| lg|S| possibilities) return the first closing paren in the block such that thenumberofopenparensminusthenumberofclosingparensintheblockuptothatparenequalsk.
Todoqueryonaparenp,firstcheckT1 foritsblock,ifitsclosingparenisthere,thenreturnit. Otherwise,userankoverpioneersto look intothepioneertabletofindwhichblockb psmatchingparenisin. ThenuseT2 forbwithkequaltothedifferencebetweenthenumberofopenparensandthenumberofcloseparensuptopminusdifferencebetweenthenumberofopenparensandthenumberofcloseparensuptob(computedusingrankoveropenparenandrankovercloseparenstructures).
1
-
8/13/2019 MIT6_851S12_ps10sol
2/2
MIT OpenCourseWarehttp://ocw.mit.edu
6.851 Advanced Data Structures
Spring 2012
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.
http://ocw.mit.edu/http://ocw.mit.edu/termshttp://ocw.mit.edu/http://ocw.mit.edu/terms