高级算法优质PPT.ppt
《高级算法优质PPT.ppt》由会员分享,可在线阅读,更多相关《高级算法优质PPT.ppt(32页珍藏版)》请在冰豆网上搜索。
BinarysearchFindanelementinasortedarray:
Divide:
Findandcheckthemiddleelement.Conquer:
Recursivelysearch1subarray.Combine:
Trivial.Example:
Find9BinarysearchFindanelementinasortedarray:
Find9ComplexityforbinarysearchT(n)=1T(n/2)+
(1)#subproblemssubproblemsizecostofdividingandcombiningRecallourMasterMethodSolverecurrencesoftheform:
T(n)=aT(n/b)+f(n),wherea1,b1,andf(n)isasymptoticallypositive.RecurrenceforbinarysearchT(n)=1T(n/2)+
(1)b=2,a=1Case2(k=0)#subproblemssubproblemsizecostofdividingandcombiningExam2:
PoweringaNumberProblem:
Computean,wherenN.Naivealgorithm:
How?
Complexity?
TheSpotCreativity:
Isthistheonlyandthebestalgorithm?
AnySuggestionsonusingdivide-and-conquerstrategy?
(n)Divide-and-conquer:
Complexity:
T(n)=T(n/2)+
(1)a=1,b=2=n0=1case2(k=0)T(n)=(lgn)PoweringaNumberTheBirthdayParadoxHowmanypeoplemusttherebeinaroomiftherearetwoofthemwerebornonthesamedayoftheyear?
Howmanypeoplemusttherebeinaroomifthereisabigchancethattwoofthemwerebornonthesameday?
Suchasprobabilityofmorethan50%?
IndicatorRandomVariableWeknowthattheprobabilityofisbirthdayandjsbirthdaybothfallonthesamedayris1/n,n=365WedefinetheindicatorrandomvariableXijfor1ijk,byIndicatorRandomVariableThuswehaveEXij=Prpersoniandjhavethesamebirthday=1/n.LettingXbetherandomvariablethatcountsthenumberofpairsofindividualshavingthesamebirthdayTheBirthdayParadoxIfwehaveatleastindividualsinaroom,wecanexpecttwotohavethesamebirthday.Forn=365,ifk=28,theexpectednumberofpairswiththesamebirthdayis(2827)/(2365)1.0356.FibonaccinumbersBackgroundofFibonacci0112358132134.GivemetherecursivedefinitionofFibonacciNumbers?
GivemeanapplicationofFibonaccinumbers.ApplicationofFibonacciThereisastaircaseof10steps,andyoucanstepacrossoneortwostepsasyoulikeeachtime.Howmanywayswehavetosteponthestage?
WhatstheratioofthetwoadjacentnumbersintheFibonacciSequence,thatstosay,whatsthevalueofFn-1/Fnwhenntendsto?
ElliottWavePrinciple:
KeytoMarketBehaviorQuestion:
HowcanwecomputeFibonacciasfastaspossibleincomputer?
NavealgorithmofFibonacciTocomputerFibonaccinumberFn=Fn-1+Fn-2Howmuchtimedoesittake?
SubstitutionmethodRecursion-treemethodMastermethodRecallSubstitutionmethodThemostgeneralmethodGuesstheformofthesolution.Verifybyinduction.Solveforconstants.Substitutionmethodisusedtodeterminetheupperorlowerboundsofrecurrence.AnalyzeofFibonacciTocomputerFibonaccinumberFn=Fn-1+Fn-2Howmuchtimedoesittake?
SubstitutionmethodRecursion-treemethodMastermethodSubstitutionmethodGuessO(2n)Guess=T(n)=Infact,T(n)=(1.618)nQuestionsWhythisrecursivealgorithmtakessomuchtime?
BuildingouttherecursiontreeforF(n),wecanseethattherearelotsofcommonsubtrees.ToomanyduplicationsthatwastetimeImportantinRecursionCompoundInterestRuleCanweimprovethisalgorithm?
Thinkaboutthebottom-upimplantationofthisrecursivealgorithmComputetheFibonaccinumbersinorder.IterativeTocomputerFibonaccinumberF(n),Howmuchtimedoesittake?
T(n)=(n)Isthatthebestwecando?
AnyideasonhowwecouldcomputeFibonacciofnfasterthanlineartime?
UsingmathematicalpropertiesofFibonaccinumbersroundedtothenearestinteger,whereWhatwecallNaiverecursivesquaring.TimeComplexity:
Doesthisworkwell?
Thismethodisunreliable,sincefloating-pointarithmeticispronetoround-offerrors.RecursiveSquaringAnothermathematicalpropertyofFibonaccinumbersProofofTheoremRecursiveSquaringAnothermathematicalpropertyofFibonaccinumbersWhatsthecostofcomputingFn?
HomeworkCode