Sei sulla pagina 1di 2

UsefulAlgorithmConcepts KaiserMd.

Nahiduzzaman Feb2010

#A.Greedyalgorithm Somelocaloptimumischosen;whenthealgorithmterminates,wehopethatthelocaloptimumisequaltotheglobal optimum. ExamplesofPopularGreedyAlgorithms:Dijkstra,Prim,Kruskalalgorithm. ACMUvaExamples:10020,10340,10440 Reallifeexample: Tomakechangeincurrency,repeatedlydispensethelargestdenomination.Togiveoutsixtyninedollarswegiveouta fiftydollarbill,atendollarbill,afivedollarbill,twotwodollarbills.Inthisway,weareguranteedtominimizethe numberofbills.Thatis,firstconsiderthelargestdenominatorin69whichis50andtherestis19.Nowconsiderthe largestdenominatorin19whichis10andtherestis9.Doitrepeatedly. #B.DivideandConquer Divide:Smallerproblemsaresolvedrecursively(exceptthebasecases). Conquer:Solutiontotheoriginalproblemisformedfromthesolutionstothesubproblems. Thesubproblemsshouldbedisjoint(nonoverlapping). Examples:maximumsubsequencesumproblem,lineartimetreetraversalstrategies,mergesort,quicksort. RunningTime:T(N)=2T(N/2)+O(N) i.e O(NlogN) #C.DynamicProgramming Aproblemthatcanbemathematicallyexpressedrecursivelycanalsobeexpressedasarecursivealgorithm,yieldinga significantperformanceimprovementoveranaivesearch. Anyrecursivemathematicalformulacouldbedirectlytranslatedtoarecursivealgorithm,buttheunderlyingrealityis thatoftenthecompilerwillnotdojusticetotherecursivealgorithm,andaninefficientprogramresults.Whenwe suspectthatthisislikelytobethecase,wemustprovidealittlemorehelptothecompiler,byrewritingtherecursive algorithm as anonrecursive algorithmthat systematicallyrecords theanswers tothesubproblems inatable.One techniquethatmakesuseofthisapproachisknownasdynamicprogramming. Examples:Matrixmultiplication,allpairsshortestpath,optimalbinarysearchtree,longestcommonsubsequence ACMUvaExamples:10131,10069,10154,116,10003,10261,10271,10201 #D.BacktrackingAlgorithms It'sacleverimplementationofexhaustivesearch.Inthisalgorithm,alargegroupofpossiblitiesareeliminatedinone stepwhichisknownaspruning. Example: SupposewearegivenNpoints,locatedonthexaxis.Letusassumethatthefirstpoint'sxcoordinateis0andthe pointsaregivenfromlefttoright. Ifwearegivenasetofpoints,itiseasytoconstructthesetofdistancesbetweeneverypairofpoints. Buttheturnpikereconstructionproblemistoreconstructapointsetfromthedistances. SupposewearegiventhedistancesetD={1,2,2,2,3,3,3,4,5,5,5,6,7,8,10}. WeknowthatN=6.Thefirstnumberis0andthelast(6th)numberis10. Weremove10fromD.Nextthelargestremainingdistanceis8,whichmeansthateitherthesecondelementis2orthe 5thelementis8.Weconstructatreeforthesolutionsandafterpropagatingacertainpath,wewillconcludethatone pathisnotthecorrectpathandwewillbacktracktolastcorrectposition.Weneedtodothesepruningsalloverthetree

togetthecorrectresult. ACMUvaExamples:861,10181,10128,10160,10032,10001,704,10270 #E.Appendix: IMPORTANTWEBSITES/FORACM/ICPCPROGRAMMERS: ACMonlinesite:http://onlinejudge.uva.es/ and http://ciijudge.baylor.edu/ Helpingsite:http://www.comp.nus.edu.sg/~stevenha/programming/acmoj.html Onlinebook:http://acm.uva.es/p/Art_of_Programming_Contest_SE_for_uva.pdf References 1.DataStructuresandAlgorithmAnalysisMarkAllenWeiss 2.ProgrammingChallengesStevenS.Skiena,MiguelA.Revilla 3.ArtofProgrammingContestAhmedShamsulArefin

Potrebbero piacerti anche