RMQENGLISH.docx
- 文档编号:2830817
- 上传时间:2022-11-15
- 格式:DOCX
- 页数:19
- 大小:56.83KB
RMQENGLISH.docx
《RMQENGLISH.docx》由会员分享,可在线阅读,更多相关《RMQENGLISH.docx(19页珍藏版)》请在冰豆网上搜索。
RMQENGLISH
RangeMinimumQueryandLowestCommonAncestor
By danielp
TopCoderMember
Introduction
Notations
RangeMinimumQuery(RMQ)
TrivialalgorithmsforRMQ
A
SparseTable(ST)algorithm
SegmentTrees
LowestCommonAncestor(LCA)
A
Anothereasysolutionin
ReductionfromLCAtoRMQ
FromRMQtoLCA
An (1)>algorithmfortherestrictedRMQ Conclusion Introduction TheproblemoffindingtheLowestCommonAncestor(LCA)ofapairofnodesinarootedtreehasbeenstudiedmorecarefullyinthesecondpartofthe20thcenturyandnowisfairlybasicinalgorithmicgraphtheory.Thisproblemisinterestingnotonlyforthetrickyalgorithmsthatcanbeusedtosolveit,butforits numerousapplicationsinstringprocessingandcomputationalbiology,forexample,whereLCAisusedwithsuffixtreesorothertree-likestructures.HarelandTarjanwerethefirsttostudythisproblemmoreattentivelyandtheyshowedthatafterlinearpreprocessingoftheinputtreeLCA,queriescanbeansweredinconstanttime.Theirworkhassincebeenextended,andthistutorialwillpresentmanyinterestingapproachesthatcanbeusedinotherkindsofproblemsaswell. Let'sconsideralessabstractexampleofLCA: thetreeoflife.It'sawell-knownfactthatthecurrenthabitantsofEarthevolvedfromotherspecies.Thisevolvingstructurecanberepresentedasatree,inwhichnodesrepresentspecies,andthesonsofsomenoderepresentthedirectlyevolvedspecies.Nowspecieswithsimilarcharacteristicsaredividedintogroups.ByfindingtheLCAofsomenodesinthistreewecanactuallyfindthecommonparentoftwospecies,andwecandeterminethatthesimilarcharacteristicstheyshareareinheritedfromthatparent. RangeMinimumQuery(RMQ)isusedonarraystofindthepositionofanelementwiththeminimumvaluebetweentwospecifiedindices.WewillseelaterthattheLCAproblemcanbereducedtoarestrictedversionofanRMQproblem,inwhichconsecutivearrayelementsdifferbyexactly1. However,RMQsarenotonlyusedwithLCA.Theyhaveanimportantroleinstringpreprocessing,wheretheyareusedwithsuffixarrays(anewdatastructurethatsupportsstringsearchesalmostasfastassuffixtrees,butuseslessmemoryandlesscodingeffort). InthistutorialwewillfirsttalkaboutRMQ.Wewillpresentmanyapproachesthatsolvetheproblem--someslowerbuteasiertocode,andothersfaster.InthesecondpartwewilltalkaboutthestrongrelationbetweenLCAandRMQ.FirstwewillreviewtwoeasyapproachesforLCAthatdon'tuseRMQ;thenshowthattheRMQandLCAproblemsareequivalent;and,attheend,we'lllookathowtheRMQproblemcanbereducedtoitsrestrictedversion,aswellasshowafastalgorithmforthisparticularcase. Notations Supposethatanalgorithmhaspreprocessingtimef(n)andquerytimeg(n).Thenotationfortheoverallcomplexityforthealgorithmis WewillnotethepositionoftheelementwiththeminimumvalueinsomearrayAbetweenindicesiandjwithRMQA(i,j). Thefurthestnodefromtherootthatisanancestorofbothuandvinsomerooted treeTisLCAT(u,v). RangeMinimumQuery(RMQ) GivenanarrayA[0,N-1]findthepositionof theelementwiththeminimumvaluebetweentwogivenindices. TrivialalgorithmsforRMQ Foreverypairofindices(i,j)storethevalueofRMQA(i,j)inatableM[0,N-1][0,N-1].Trivialcomputationwillleadustoan (1)>complexity.However,byusinganeasydynamicprogrammingapproachwecanreducethecomplexityto (1)>.Thepreprocessingfunctionwilllooksomethinglikethis: voidprocess1(intM[MAXN][MAXN],intA[MAXN],intN) { inti,j;
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- RMQENGLISH
![提示](https://static.bdocx.com/images/bang_tan.gif)