2/19/2015 BigOAlgorithmComplexityCheatSheet
http://bigocheatsheet.com/ 1/12
BigOCheatSheet
SearchingSortingDataStructuresHeapsGraphsChartComments
Tweet
4,006 2k
KnowThyComplexities!Hithere!ThiswebpagecoversthespaceandtimeBigOcomplexitiesofcommonalgorithmsusedinComputerScience.Whenpreparingfortechnicalinterviewsinthepast,Ifoundmyselfspendinghourscrawlingtheinternetputtingtogetherthebest,average,andworstcasecomplexitiesforsearchandsortingalgorithmssothatIwouldn'tbestumpedwhenaskedaboutthem.Overthelastfewyears,I'veinterviewedatseveralSiliconValleystartups,andalsosomebiggercompanies,likeYahoo,eBay,LinkedIn,andGoogle,andeachtimethatIpreparedforaninterview,Ithoughttomyself"Whyohwhyhasn'tsomeonecreatedaniceBigOcheatsheet?".So,tosaveallofyoufinefolksatonoftime,Iwentaheadandcreatedone.Enjoy!
Good Fair Poor
Searching
Algorithm DataStructure TimeComplexity SpaceComplexityAverage Worst Worst
DepthFirstSearch(DFS) Graphof|V|verticesand|E|edges O(|E|+|V|) O(|V|)
BreadthFirstSearch(BFS) Graphof|V|verticesand|E|edges O(|E|+|V|) O(|V|)
Binarysearch Sortedarrayofnelements O(log(n)) O(log(n)) O(1)Linear(BruteForce) Array O(n) O(n) O(1)ShortestpathbyDijkstra,usingaMinheapaspriorityqueue
Graphwith|V|verticesand|E|edges
O((|V|+|E|)log|V|)
O((|V|+|E|)log|V|)
O(|V|)
ShortestpathbyDijkstra,Graphwith|V|verticesand
Get Lump Sum + Monthly IncomeBenefit* @ Affordable Rate. Buy
Now
HDFC LifeClick2Protect+
9.4k
Like
2/19/2015 BigOAlgorithmComplexityCheatSheet
http://bigocheatsheet.com/ 2/12
usinganunsortedarrayaspriorityqueue
|E|edges O(|V|^2) O(|V|^2) O(|V|)
ShortestpathbyBellmanFord Graphwith|V|verticesand|E|edges O(|V||E|) O(|V||E|) O(|V|)
Sorting
Algorithm DataStructure TimeComplexity WorstCaseAuxiliarySpaceComplexityBest Average Worst Worst
Quicksort Array O(nlog(n)) O(nlog(n)) O(n^2) O(n)Mergesort Array O(nlog(n)) O(nlog(n)) O(nlog(n)) O(n)Heapsort Array O(nlog(n)) O(nlog(n)) O(nlog(n)) O(1)BubbleSort Array O(n) O(n^2) O(n^2) O(1)InsertionSort Array O(n) O(n^2) O(n^2) O(1)SelectSort Array O(n^2) O(n^2) O(n^2) O(1)BucketSort Array O(n+k) O(n+k) O(n^2) O(nk)RadixSort Array O(nk) O(nk) O(nk) O(n+k)
DataStructures
DataStructure TimeComplexity SpaceComplexityAverage Worst Worst
Indexing Search Insertion Deletion Indexing Search Insertion DeletionBasicArray O(1) O(n) O(1) O(n) O(n)DynamicArray O(1) O(n) O(n) O(n) O(1) O(n) O(n) O(n) O(n)SinglyLinkedList O(n) O(n) O(1) O(1) O(n) O(n) O(1) O(1) O(n)
DoublyLinkedList O(n) O(n) O(1) O(1) O(n) O(n) O(1) O(1) O(n)
SkipList O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(n) O(n) O(n) O(n) O(nlog(n))
HashTable O(1) O(1) O(1) O(n) O(n) O(n) O(n)BinarySearchTree O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(n) O(n) O(n) O(n) O(n)
CartresianTree O(log(n)) O(log(n)) O(log(n)) O(n) O(n) O(n) O(n)BTree O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(n)RedBlackTree O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(n)
SplayTree O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(n)AVLTree O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(n)
2/19/2015 BigOAlgorithmComplexityCheatSheet
http://bigocheatsheet.com/ 3/12
Heaps
Heaps TimeComplexityHeapify FindMax ExtractMax IncreaseKey Insert Delete Merge
LinkedList(sorted) O(1) O(1) O(n) O(n) O(1) O(m+n)LinkedList(unsorted) O(n) O(n) O(1) O(1) O(1) O(1)BinaryHeap O(n) O(1) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(m+n)BinomialHeap O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(log(n))FibonacciHeap O(1) O(log(n))* O(1)* O(1) O(log(n))* O(1)
Graphs
Node/EdgeManagement Storage AddVertex AddEdge RemoveVertex RemoveEdge QueryAdjacencylist O(|V|+|E|) O(1) O(1) O(|V|+|E|) O(|E|) O(|V|)Incidencelist O(|V|+|E|) O(1) O(1) O(|E|) O(|E|) O(|E|)Adjacencymatrix O(|V|^2) O(|V|^2) O(1) O(|V|^2) O(1) O(1)Incidencematrix O(|V||E|) O(|V||E|) O(|V||E|) O(|V||E|) O(|V||E|) O(|E|)
BigOComplexityChart
2/19/2015 BigOAlgorithmComplexityCheatSheet
http://bigocheatsheet.com/ 4/12
Contributors
Editthesetables!
1. EricRowell2. QuentinPleple3. NickDizazzo4. MichaelAbed5. AdamForsyth6. JayEngineer7. JoshDavis8. makosblade9. AlejandroRamirez
10. JoelFriedly11. RobertBurke12. DavidDorfman13. EricLefevreArdant14. ThomasDybdahlAhle
AROUNDTHEWEB WHAT'STHIS?
2/19/2015 BigOAlgorithmComplexityCheatSheet
http://bigocheatsheet.com/ 5/12
UniqueMethodRegrowsLostHair(DoThisDaily)
LifestyleJournal
VIDEO:EastbayPresentsPreparationNation:Crespi
Stack
5WorstShiftKnobsEverWeb2Carz.com ClothesThatDon'tWork,NoMatter
HowHardYouTry
TalkShop
240Comments BigOCheatSheet Login
SortbyBest Share
Jointhediscussion
Reply
MichaelMitchell 2yearsagoThisisgreat.Maybeyoucouldincludesomeresources(linkstokhanacademy,moocetc)thatwouldexplaineachoftheseconceptsforpeopletryingtolearnthem.
228
Reply
AmandaHarlin 2yearsago>MichaelMitchellYes!Please&thankyou
54
Reply
CamCecil 2yearsago>MichaelMitchellThisexplanationin'plainEnglish'helps:http://stackoverflow.com/quest...
21
ArjanNieuwenhuizen 2yearsago>MichaelMitchellHerearethelinksthatIknowof.
#1)http://aduni.org/courses/algor...#2)http://ocw.mit.edu/courses/ele...#3)https://www.udacity.com/course...
probablyasgoodormaybebetter#2,butIhavenothadachancetolookatit.http://ocw.mit.edu/courses/ele...
Sincerely,Arjan
p.s.
Favorite
Share
Share
Share
2/19/2015 BigOAlgorithmComplexityCheatSheet
http://bigocheatsheet.com/ 6/12
Reply
p.s.https://www.coursera.org/cours...Thiscoursehasjustbegunoncoursera(dated1July2013),andlooksverygood.
11
Reply
fireheron ayearago>ArjanNieuwenhuizenThankyouArjan.Espaeciallythecoursera.orgone)
3
Reply
BlakeJennings 2yearsagoi'mliterallycrying
70
Reply
JonRenner 2yearsagoThisisgod'swork.
95
Reply
AdamHeinermann 2yearsagoIsthereaprinterfriendlyversion?
51
Reply
ThomasFeichtinger 9monthsago>AdamHeinermannActuallycopyingthecontentstoagoogledocworkedprettywell!
Ihavemadeitpublic,havealook:https://docs.google.com/spread...
23
Reply
ericdrowell ayearagoMod >AdamHeinermannnotyet,butthat'sagreatidea!
8
Reply
MattLabrum adayago>AdamHeinermannI,too,wantedaprinterfriendlyversionforstudyingbeforeaninterview,andIwasn'tsatisfiedwiththesolutionsIfoundprovidedinthevariouscommentshere.So,IwentaheadandLaTeX'edthispagetogetanicePDF.
IhaveuploadedthePDFsIcreatedtomyGoogleDriveandmadethempublic:https://drive.google.com/folde....InthatfolderaretwoPDFSoneisforlettersizedpaperandtheotherisforA4sizedpaper.AssumingIdidn'tintroduceanytypos,thecontentofthosePDFsshouldmatchthecontentofthispage(asitappearsatthismoment17February2015),withtheonlynoteworthydifferencebeingthatImovedtheGraphssectiontobeaftertheSortingsectiontohelpeliminatesomeextrawhitespace.
JoeGibson 20hoursago>MattLabrumMatt,
GreatjobontheLaTeXdocument.I'mpreparingforaGoogleinterviewandthiswillbealotofhelp!
Anychanceyoucanputthe.texfileonyourdriveaswellinthesamefolder?
Share
Share
Share
Share
Share
Share
Share
Share
2/19/2015 BigOAlgorithmComplexityCheatSheet
http://bigocheatsheet.com/ 7/12
Reply Anychanceyoucanputthe.texfileonyourdriveaswellinthesamefolder?
Reply
MattLabrum 19hoursago>JoeGibsonDonethetwo.texfilesandthe.epsofthegrapharenowinthatfolder.
EditI'vealsoputtheRscriptandtmp.texfileIusedtocreatethegraphinthatfolder.Aftercreatingthe.epsfilewithR,IdidsomeprocessingonittogetthefinalBigO.epsfileIincludeinthe.texfiles.
Forcompleteness,togetfromtheRgeneratedBigO.epsfiletothefinalBigO.epsfile,Ididthefollowing:1.OpenBigO.epswithatexteditortoensurethetextannotationshavenotbeenbrokenapart.Ipersonallyhadtoput"Operations"and"BigOComplexity"(yaxislabelandgraphtitle)backtogether.2.Processtmp.textogeta.dvifilethatcontainsaPSFrag'edversionofthegraph.3.dvipsjEtmp.dvioBigO.tmp.eps4.epstoolcopybboxBigO.tmp.epsBigO.eps5.rmBigO.tmp.eps
Reply
JoeGibson 19hoursago>MattLabrumThanks,yourock.
Reply
DarrenLeRedgatr 2yearsagoIcameherefromanidletwitterclick.Ihavenoideawhatit'stalkingaboutoranyofthecomments.ButIlovethefacttherearepeopleouttherethisclever.Makesmethinkthatonedayhumanitywillcomegood.Cheers.
50
Reply
GokceToykuyu 2yearsagoCouldweaddsometreealgorithmsandcomplexities?Thanks.IreallyliketheRedBlacktrees)
32
Reply
ericdrowell 2yearsagoMod >GokceToykuyuExcellentidea.I'lladdasectionthatcomparesinsertion,deletion,andsearchcomplexitiesforspecificdatastructures
29
Reply
ValentinStanciu 2yearsago1.Deletion/insertioninasinglelinkedlistisimplementationdependent.Forthequestionof"Here'sapointertoanelement,howmuchdoesittaketodeleteit?",singlelinkedliststakeO(N)sinceyouhavetosearchfortheelementthatpointstotheelementbeingdeleted.Doublelinkedlistssolvethisproblem.2.Hashescomeinamillionvarieties.HoweverwithagooddistributionfunctiontheyareO(logN)worstcase.Usingadoublehashingalgorithm,youendupwithaworstcaseofO(loglogN).3.Fortrees,thetableshouldprobablyalsocontainheapsandthecomplexitiesfortheoperation"GetMinimum".
23
AlexisMas 6monthsago>ValentinStanciu
Share
Share
Share
Share
Share
Share
Share
2/19/2015 BigOAlgorithmComplexityCheatSheet
http://bigocheatsheet.com/ 8/12
Reply
AlexisMas 6monthsago>ValentinStanciuIfyoualist:ABCD,WhenyouwanttodeleteB,youcandeleteanodewithoutiteratingoverthelist.
1.B.data=C.data2.B.next=C.next3.deleteC
Ifyoucan'tcopydatabetweennodesbecauseitstooexpensivethenyes,it'sO(N)4
Reply
Miguel 2monthsago>AlexisMasYoustillhavetofindthepositioninthelist,whichcanonlybedonelinearly.
5
Reply
Guest 12daysago>MiguelYoustillhavetofindthepositioninthelist,whichcanonlybedonelinearly.
3
Reply
AlexisMas 9daysago>MiguelYesofcourse,Ifyouneedtosearchthenodeit'sO(n),otherwiseyoucandeleteitasIstatedbefore.
1
Reply
Guest 9daysago>AlexisMasNoneedtofindthepositionifyoucandeleteitasAlexismentioned
1
Reply
OmegaNemesis28 9daysago>AlexisMasTogettoByouHAVEtoiterateoverthelistthough.Youcan'tjustmanipulateBwithoutapointer.Sounlessyoudobookkeepingandhavepointerstospecificnodesyouintendtodelete/manipulate,LinkListsareO(n)insertanddelete.
2
Reply
AlexisMas 8daysago>OmegaNemesis28Strictlyspeakingno,youdon't.let'ssayyouhavethisfunction.
publicvoiddelete(Nodenode)
Thatfunctiondoesn'tcarehowdidyougotthatnode.
Didyougotmypoint?
Whenyouhaveapointertoanode,andthatnodeneedstobedeletedyoudon'tneedtoiteraveoverthelist.
1
OmegaNemesis28 8daysago>AlexisMasButthatisMYpoint:p
YouhavetohavethenodeFIRST.Youhavetoiteratethroughthelistbeforeyoucandothat,unlessyoudobookkeepingandhappentohavesaidnode.RereadwhatIsaid."havepointerstospecificnodes"Mostofthetime,youdonotwithLinkedLists.Ifyou
Share
Share
Share
Share
Share
Share
Share
2/19/2015 BigOAlgorithmComplexityCheatSheet
http://bigocheatsheet.com/ 9/12
Reply
"havepointerstospecificnodes"Mostofthetime,youdonotwithLinkedLists.IfyouhaveaLinkedListandwanttodeleteindex5,youhavetoiterateto5andsuch.YourexamplewasABCD,ourpointsarethatyoutypicallydon'thavethepointertoBjustoffhand.YouhavetoobtainitfirstwhichwillbeO(n)
2
Reply
PinguApp 3daysago>AlexisMasWhatifBisthelastelementinthelist?HowwouldB'spredecesorknowthatitsnextfieldshouldpointtoNULLandnottoafuturelyinvalidmemoryaddress?
1
Reply
AlexisMas 3daysago>PinguAppInthatcaseyoucan'tdeletedthatway,you'reforcedtohaveapointertothepreviousitem.
1
Reply
qwertykeyboard 2yearsagoItwouldbeveryhelpfultohaveexporttoPDF.Thx
17
Reply
Gene ayearago>qwertykeyboardYoucouldconvertthedocumentyourselfusingPandoc:http://johnmacfarlane.net/pand...Itmighttakeyoualongtimetogetitworking,butPandocisanamazingonestopshopforfileconversion,andcrossplatformcompatible.IfIunderstandbigohnotationcorrectlyImightsay"IestimateyourlearningrateforlearningPandocwillbeO(1).".
2
Reply
Ashutosh ayearago>GeneprovedO(n),n=numberofformatconversionstolearn:)
4
Reply
JuanCarlosAlvarez ayearago>Genebigoh.hahafunny.
1
Reply
Guest 2yearsagoFinalsarealreadyover...Thisshouldhavebeensharedaweekago!Wouldhavesavedmelike45minutesofusingWikipedia.
11
sigmaalgebra 2yearsagoYouomittedaninplace,guaranteedO(nlog(n))arraysort,e.g.,heapsort.Youomittedradixsortthatcanbefaster
thananyofthealgorithmsyoumentioned.
MightmentionSATandrelatedproblemsinNPcomplete
wherethebestknownalgorithmforaproblemofsizenhasO(2^n).
Share
Share
Share
Share
Share
Share
Share
Share
2/19/2015 BigOAlgorithmComplexityCheatSheet
http://bigocheatsheet.com/ 10/12
Reply
aproblemofsizenhasO(2^n).
Mightincludeanactual,precisedefinitionofO().9
Reply
JonRenner 2yearsagoAnywayIcangetaPDFversionwithouttakingscreenshotsmyself?
8
Reply
AttilaOlh amonthago>JonRennerPrintDestination:ChangeSelect"SaveasPDF"(inChrome).
2
Reply
AntoineGrondin 2yearsagoIthinkDFSandBFS,underSearch,wouldbemoreappropriatelistedasGraphinsteadofTree.
8
Reply
ericdrowell 2yearsagoMod >AntoineGrondinFixed!Thanks
4
Reply
QuentinPlepl 2yearsago>AntoineGrondinAgreed
1
Reply
tempire 2yearsagoThischartseemsmisleading.BigOisworstcase,notaveragecase~isaveragecase.O(...)shouldn'tbeusedintheaveragecasecolumns.
16
Reply
guest 2yearsago>tempireIthinkbigOisjustanupperbound.Itcouldbeusedforall(best,worstandaverage)cases.AmIwrong?
17
Reply
Luis 2yearsago>guestYouareright.
6
Oleksandr ayearago>Luis@[email protected],average,andupperbound..BigO(Omicron)istheWorstCaseScenario.Itistheupperboundforforthealgorithm.Forinstanceinalinearsearchalgorithm,worstcaseiswhenthelistiscompletedoutoforder,i.e.thelistsortedbutbackwards.Omegaisthelowerbound.Thisisalmostpointlesstohave,forinstanceyouwouldratherhaveBigOthenOmegabecauseitisexactlythesameassay"itwilltakemorethanfivedollarstogettoN.Y.vs.Itswillalwaystake,atmost,135dollarstogettoNewYork."ThefirstbitofinformationfromOmegaisessentiallyuseless,thethirdhowevergivesyoutheconstraint.Thetaistheupperandlowerboundtogether.Thisisthemostbeneficiallypieceofinformationtohaveaboutanalgorithmbutunfortunatelyitisusuallyveryhardto
Share
Share
Share
Share
Share
Share
Share
Share
Share
2/19/2015 BigOAlgorithmComplexityCheatSheet
http://bigocheatsheet.com/ 11/12
Reply
find,butwehavedonethis.Youcanusuallyfindthataverageforanalgorithmsefficiencybytestingitinaveragecaseandworstcasestogether.Simplythisisacomputationalexercisetoextracttheempiricaldata.ThereisanotherproblemIdonotlikeisthecolorschemeissometimeswrong..O(n)isbettertheO(log(n))?Inwhatway?1024vs10incrementsthatasortalgorithmhastoperformforinstance?Allinallthisisgoodinformationbutinitscurrentstate,tothenovice,honestlyitneedstobetakenwithagrainofsaltandfactcheckwithagoodalgorithmbook.However,thisisinMHOsoifI'moffbaseorincorrectthenfeelfreetoflamemelikethefantasticfouratagayparade:)
6
Reply
Guest ayearago>Oleksandr@OleksandrYouareconfused.Yourexampleaboutthedollarsstatesspecificamounts(e.g."atmost135dollars"),butbigOandrelatedconceptsareusedtoboundtheorder(linear,exponential,etc.)ofafunctionthatdescribeshowanalgorithmgrows(inspace,time,etc.)withproblemsize.Tobemoreappropriate,yourexampleshouldbemodifiedtosaysomethinglike"ittakesatmost2$permile"(linear).Withthisinmind,youcanthusunderstandhowbigOcanbeusedbothfor,say,thebestandtheworstcase.Takeyourlinearsearch.Asthesizeoftheproblemgrows(thearraytobesearchedgrowsinsize),thebestcasestillhasanuppertimeboundofO(1)(ittakesconstanttimetofindanelementinindex0,oranotherfixedposition),whiletheworstcase(theobjectisinthelastindexwherewelook)hasanuppertimeboundofO(n)(ittakesanumberofstepsoforderequaltotheproblemsize,n,untilwefindtheobjectinthelastindexwherewelook.).
(fixed:wrongautocompleteofwhoIrepliedto)3
Reply
PhilipMachanick ayearago>OleksandrOmegaisuselessunlessitisatightbound,i.e.,itrepresentsrealminimalcasesthatareinteresting(whenyouhaveoptionslike=inthedefinitionofabound,youshouldatleastgetclosetothe=case,otherwiseyoumightaswellusethestrictlycases,andeventhereyoushouldtrytofindboundsthatarereasonablyclosetothe=case).Forexample,strictlyspeaking,quicksortisOmega(1),butOmega(nlogn)ismoreinformativebecausetellsyouitsrealbestcase.
Inanycase,youdonotnormallyuseOmega,Thetaetc.fordifferentiatingaverage,bestandworstcase.Theseareboundsonanyofthesecases.Forquicksort,theworstcaseanalysisisn^2andthisisboththeupperandlowerboundontheworstcase.YouuseOmega,Theta,etc.whentheanalysisforaparticularcaseisnotclearandyouhavetosayitisnobetterthanornoworsethanaparticularanalysis.
2
Luis ayearago>Oleksandr@OleksandrYouareconfused.Yourexampleaboutthedollarsstatesspecificamounts(e.g."atmost135dollars"),butbigOandrelatedconceptsareusedtoboundtheorder(linear,exponential,etc.)ofafunctionthatdescribeshowanalgorithmgrows(inspace,time,etc.)withproblemsize.Tobemoreappropriate,yourexampleshouldbemodifiedtosaysomethinglike"ittakesatmost2$permile"(linear).Withthisinmind,youcanthusunderstandhowbigOcanbeusedbothfor,say,thebestandtheworstcase.Takeyourlinearsearch.Asthesizeoftheproblemgrows(thearraytobesearched
Share
Share
Share
2/19/2015 BigOAlgorithmComplexityCheatSheet
http://bigocheatsheet.com/ 12/12
Reply
Takeyourlinearsearch.Asthesizeoftheproblemgrows(thearraytobesearchedgrowsinsize),thebestcasestillhasanuppertimeboundofO(1)(ittakesconstanttimetofindanelementinindex0,oranotherfixedposition),whiletheworstcase(theobjectisinthelastindexwherewelook)hasanuppertimeboundofO(n)(ittakesanumberofstepsoforderequaltotheproblemsize,n,untilwefindtheobjectinthelastindexwherewelook.).
2
Oleksandr1 ayearago>LuisYoumakeaverypoorassumptionthatbecauseaspecificvalueisgiven,thanitmustbealinearfunction.ItisinfactanypolynomialfunctionofmychoicegivenitsparametersandanyamountofLagrangeconstants
Share
PagestylingviaBootstrapCommentsviaDisqusAlgorithmdetailviaWikipediaBigOcomplexitychartviaMeteorChartsTablesourcehostedonGithubMashupvia@ericdrowell
Top Related