Sei sulla pagina 1di 12

2/19/2015

BigOAlgorithmComplexityCheatSheet

BigOCheatSheet
Searching
Sorting
DataStructures
Heaps
Graphs
Chart
Comments

Get Lump Sum + Monthly Income


Benefit* @ Affordable Rate. Buy
Now

HDFC Life
Click2Protect+
4,006

2k

9.4k
Like

Tweet

KnowThyComplexities!
Hithere!ThiswebpagecoversthespaceandtimeBigOcomplexitiesofcommonalgorithmsusedinComputer
Science.Whenpreparingfortechnicalinterviewsinthepast,Ifoundmyselfspendinghourscrawlingtheinternet
puttingtogetherthebest,average,andworstcasecomplexitiesforsearchandsortingalgorithmssothatIwouldn'tbe
stumpedwhenaskedaboutthem.Overthelastfewyears,I'veinterviewedatseveralSiliconValleystartups,andalso
somebiggercompanies,likeYahoo,eBay,LinkedIn,andGoogle,andeachtimethatIpreparedforaninterview,I
thoughttomyself"Whyohwhyhasn'tsomeonecreatedaniceBigOcheatsheet?".So,tosaveallofyoufinefolksa
tonoftime,Iwentaheadandcreatedone.Enjoy!
Good Fair Poor

Searching
Algorithm

DataStructure

TimeComplexity
Average

DepthFirstSearch(DFS)
BreadthFirstSearch(BFS)
Binarysearch
Linear(BruteForce)
ShortestpathbyDijkstra,
usingaMinheapaspriority
queue
ShortestpathbyDijkstra,
http://bigocheatsheet.com/

Graphof|V|verticesand|E|
edges
Graphof|V|verticesand|E|
edges
Sortedarrayofnelements
Array
Graphwith|V|verticesand
|E|edges

Worst

Space
Complexity
Worst

O(|E|+|V|)

O(|V|)

O(|E|+|V|)

O(|V|)

O(log(n))

O(log(n))

O(1)

O(n)

O(n)

O(1)

O((|V|+|E|)log O((|V|+|E|)log
O(|V|)
|V|)
|V|)

Graphwith|V|verticesand
1/12

2/19/2015

BigOAlgorithmComplexityCheatSheet

usinganunsortedarrayas
priorityqueue
ShortestpathbyBellmanFord

|E|edges

O(|V|^2)

O(|V|^2)

O(|V|)

Graphwith|V|verticesand
|E|edges

O(|V||E|)

O(|V||E|)

O(|V|)

Sorting
Algorithm DataStructure
Quicksort
Array
Mergesort
Array
Heapsort
Array
BubbleSort Array
InsertionSort Array
SelectSort Array
BucketSort Array
RadixSort Array

TimeComplexity
Best
Average
Worst
O(nlog(n)) O(nlog(n)) O(n^2)

WorstCaseAuxiliarySpaceComplexity
Worst
O(n)

O(nlog(n)) O(nlog(n)) O(nlog(n)) O(n)


O(nlog(n)) O(nlog(n)) O(nlog(n)) O(1)
O(n)

O(n^2)

O(n^2)

O(1)

O(n)

O(n^2)

O(n^2)

O(1)

O(n^2)

O(n^2)

O(n^2)

O(1)

O(n+k)

O(n+k)

O(n^2)

O(nk)

O(nk)

O(nk)

O(nk)

O(n+k)

DataStructures
DataStructure

TimeComplexity
Indexing

Average
Search Insertion Deletion Indexing

Worst
Search Insertion Deletion

Space
Complexity
Worst

BasicArray
DynamicArray
SinglyLinked
List
DoublyLinked
List

O(1)

O(n)

O(1)

O(n)

O(n)

O(1)

O(n)

O(n)

O(n)

O(1)

O(n)

O(n)

O(n)

O(n)

O(n)

O(n)

O(1)

O(1)

O(n)

O(n)

O(1)

O(1)

O(n)

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(n
log(n))

HashTable
BinarySearch
Tree
CartresianTree
BTree
RedBlackTree

O(n)

O(n)

O(n)

O(n)

O(log(n)) O(log(n)) O(log(n)) O(log(n)) O(n)

O(n)

O(n)

O(n)

O(n)

O(n)

O(n)

O(n)

O(n)

SplayTree
AVLTree

http://bigocheatsheet.com/

O(1)

O(1)

O(1)

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(log(n)) O(log(n)) O(log(n)) O(n)


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)
O(log(n)) O(log(n)) O(log(n))

O(log(n)) O(log(n)) O(log(n)) O(n)

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/12

2/19/2015

BigOAlgorithmComplexityCheatSheet

Heaps
Heaps

TimeComplexity
Heapify FindMax ExtractMax IncreaseKey Insert

LinkedList(sorted)
LinkedList(unsorted)
BinaryHeap
O(n)
BinomialHeap

FibonacciHeap

Delete

Merge

O(1)

O(1)

O(n)

O(n)

O(1)

O(m+n)

O(n)

O(n)

O(1)

O(1)

O(1)

O(1)

O(1)

O(log(n))

O(log(n))

O(log(n)) O(log(n))

O(m+n)

O(log(n)) O(log(n))

O(log(n))

O(log(n)) O(log(n))

O(log(n))

O(1)

O(log(n))*

O(1)*

O(1)

Storage

AddVertex

O(log(n))* O(1)

Graphs
Node/EdgeManagement
Adjacencylist
Incidencelist
Adjacencymatrix
Incidencematrix

AddEdge RemoveVertex RemoveEdge Query

O(|V|+|E|)

O(1)

O(1)

O(|V|+|E|)

O(|E|)

O(|V|)

O(|V|+|E|)

O(1)

O(1)

O(|E|)

O(|E|)

O(|E|)

O(|V|^2)

O(|V|^2)

O(1)

O(|V|^2)

O(1)

O(1)

O(|V||E|)

O(|E|)

O(|V||E|) O(|V||E|) O(|V||E|) O(|V||E|)

BigOComplexityChart

http://bigocheatsheet.com/

3/12

2/19/2015

BigOAlgorithmComplexityCheatSheet

Contributors
Editthesetables!
1. EricRowell
2. QuentinPleple
3. NickDizazzo
4. MichaelAbed
5. AdamForsyth
6. JayEngineer
7. JoshDavis
8. makosblade
9. AlejandroRamirez
10. JoelFriedly
11. RobertBurke
12. DavidDorfman
13. EricLefevreArdant
14. ThomasDybdahlAhle

AROUNDTHEWEB

http://bigocheatsheet.com/

WHAT'STHIS?

4/12

2/19/2015

BigOAlgorithmComplexityCheatSheet

LifestyleJournal

Stack

UniqueMethodRegrowsLostHair(Do VIDEO:EastbayPresentsPreparation
ThisDaily)
Nation:Crespi

TalkShop
Web2Carz.com

5WorstShiftKnobsEver
240Comments

ClothesThatDon'tWork,NoMatter
HowHardYouTry

BigOCheatSheet

Login

Share Favorite

SortbyBest

Jointhediscussion
MichaelMitchell 2yearsago

Thisisgreat.Maybeyoucouldincludesomeresources(linkstokhanacademy,moocetc)thatwouldexplain
eachoftheseconceptsforpeopletryingtolearnthem.
228

Reply Share

AmandaHarlin>MichaelMitchell 2yearsago

Yes!Please&thankyou
54

Reply Share

CamCecil>MichaelMitchell 2yearsago

Thisexplanationin'plainEnglish'helps:http://stackoverflow.com/quest...
21

Reply Share

ArjanNieuwenhuizen>MichaelMitchell 2yearsago

HerearethelinksthatIknowof.
#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.

http://bigocheatsheet.com/

5/12

2/19/2015

BigOAlgorithmComplexityCheatSheet

p.s.
https://www.coursera.org/cours...
Thiscoursehasjustbegunoncoursera(dated1July2013),andlooksverygood.
11

Reply Share

fireheron>ArjanNieuwenhuizen ayearago

ThankyouArjan.Espaeciallythecoursera.orgone)
3

Reply Share

BlakeJennings 2yearsago

i'mliterallycrying
70

Reply Share

JonRenner 2yearsago

Thisisgod'swork.
95

Reply Share

AdamHeinermann 2yearsago

Isthereaprinterfriendlyversion?
51

Reply Share

ThomasFeichtinger>AdamHeinermann 9monthsago

Actuallycopyingthecontentstoagoogledocworkedprettywell!
Ihavemadeitpublic,havealook:
https://docs.google.com/spread...
23

Reply Share

ericdrowell

Mod >AdamHeinermann

ayearago

notyet,butthat'sagreatidea!
8

Reply Share

MattLabrum>AdamHeinermann adayago

I,too,wantedaprinterfriendlyversionforstudyingbeforeaninterview,andIwasn'tsatisfiedwiththe
solutionsIfoundprovidedinthevariouscommentshere.So,IwentaheadandLaTeX'edthispageto
getanicePDF.
IhaveuploadedthePDFsIcreatedtomyGoogleDriveandmadethempublic:
https://drive.google.com/folde....InthatfolderaretwoPDFSoneisforlettersizedpaperandthe
otherisforA4sizedpaper.AssumingIdidn'tintroduceanytypos,thecontentofthosePDFsshould
matchthecontentofthispage(asitappearsatthismoment17February2015),withtheonly
noteworthydifferencebeingthatImovedtheGraphssectiontobeaftertheSortingsectiontohelp
eliminatesomeextrawhitespace.

Reply Share

JoeGibson>MattLabrum 20hoursago

Matt,
GreatjobontheLaTeXdocument.I'mpreparingforaGoogleinterviewandthiswillbealotof
help!
http://bigocheatsheet.com/

Anychanceyoucanputthe.texfileonyourdriveaswellinthesamefolder?

6/12

2/19/2015

BigOAlgorithmComplexityCheatSheet

Anychanceyoucanputthe.texfileonyourdriveaswellinthesamefolder?

Reply Share

MattLabrum>JoeGibson 19hoursago

Donethetwo.texfilesandthe.epsofthegrapharenowinthatfolder.
Edit
I'vealsoputtheRscriptandtmp.texfileIusedtocreatethegraphinthatfolder.After
creatingthe.epsfilewithR,IdidsomeprocessingonittogetthefinalBigO.epsfileI
includeinthe.texfiles.
Forcompleteness,togetfromtheRgeneratedBigO.epsfiletothefinalBigO.epsfile,I
didthefollowing:
1.OpenBigO.epswithatexteditortoensurethetextannotationshavenotbeen
brokenapart.Ipersonallyhadtoput"Operations"and"BigOComplexity"(yaxislabel
andgraphtitle)backtogether.
2.Processtmp.textogeta.dvifilethatcontainsaPSFrag'edversionofthegraph.
3.dvipsjEtmp.dvioBigO.tmp.eps
4.epstoolcopybboxBigO.tmp.epsBigO.eps
5.rmBigO.tmp.eps

Reply Share

JoeGibson>MattLabrum 19hoursago

Thanks,yourock.

Reply Share

DarrenLeRedgatr 2yearsago

Icameherefromanidletwitterclick.Ihavenoideawhatit'stalkingaboutoranyofthecomments.ButIlove
thefacttherearepeopleouttherethisclever.Makesmethinkthatonedayhumanitywillcomegood.Cheers.
50

Reply Share

GokceToykuyu 2yearsago

Couldweaddsometreealgorithmsandcomplexities?Thanks.IreallyliketheRedBlacktrees)
32

Reply Share

ericdrowell

Mod >GokceToykuyu

2yearsago

Excellentidea.I'lladdasectionthatcomparesinsertion,deletion,andsearchcomplexitiesforspecific
datastructures
29

Reply Share

ValentinStanciu 2yearsago

1.Deletion/insertioninasinglelinkedlistisimplementationdependent.Forthequestionof"Here'sapointerto
anelement,howmuchdoesittaketodeleteit?",singlelinkedliststakeO(N)sinceyouhavetosearchforthe
elementthatpointstotheelementbeingdeleted.Doublelinkedlistssolvethisproblem.
2.Hashescomeinamillionvarieties.HoweverwithagooddistributionfunctiontheyareO(logN)worstcase.
Usingadoublehashingalgorithm,youendupwithaworstcaseofO(loglogN).
3.Fortrees,thetableshouldprobablyalsocontainheapsandthecomplexitiesfortheoperation"Get
Minimum".
23

Reply Share

AlexisMas>ValentinStanciu 6monthsago

http://bigocheatsheet.com/

7/12

2/19/2015

BigOAlgorithmComplexityCheatSheet

AlexisMas>ValentinStanciu 6monthsago

Ifyoualist:ABCD,WhenyouwanttodeleteB,youcandeleteanodewithoutiteratingoverthelist.
1.B.data=C.data
2.B.next=C.next
3.deleteC
Ifyoucan'tcopydatabetweennodesbecauseitstooexpensivethenyes,it'sO(N)
4

Reply Share

Miguel>AlexisMas 2monthsago

Youstillhavetofindthepositioninthelist,whichcanonlybedonelinearly.
5

Reply Share

Guest>Miguel 12daysago

Youstillhavetofindthepositioninthelist,whichcanonlybedonelinearly.
3

Reply Share

AlexisMas>Miguel 9daysago

Yesofcourse,Ifyouneedtosearchthenodeit'sO(n),otherwiseyoucandeleteitasI
statedbefore.
1

Reply Share

Guest>AlexisMas 9daysago

NoneedtofindthepositionifyoucandeleteitasAlexismentioned
1

Reply Share

OmegaNemesis28>AlexisMas 9daysago

TogettoByouHAVEtoiterateoverthelistthough.Youcan'tjustmanipulateBwithouta
pointer.Sounlessyoudobookkeepingandhavepointerstospecificnodesyouintendto
delete/manipulate,LinkListsareO(n)insertanddelete.
2

Reply Share

AlexisMas>OmegaNemesis28 8daysago

Strictlyspeakingno,youdon't.let'ssayyouhavethisfunction.
publicvoiddelete(Nodenode)
Thatfunctiondoesn'tcarehowdidyougotthatnode.
Didyougotmypoint?
Whenyouhaveapointertoanode,andthatnodeneedstobedeletedyoudon'tneedto
iteraveoverthelist.
1

Reply Share

OmegaNemesis28>AlexisMas 8daysago

ButthatisMYpoint:p
YouhavetohavethenodeFIRST.Youhavetoiteratethroughthelistbeforeyoucando
that,unlessyoudobookkeepingandhappentohavesaidnode.RereadwhatIsaid.
"havepointerstospecificnodes"Mostofthetime,youdonotwithLinkedLists.Ifyou
http://bigocheatsheet.com/

8/12

2/19/2015

BigOAlgorithmComplexityCheatSheet

"havepointerstospecificnodes"Mostofthetime,youdonotwithLinkedLists.Ifyou
haveaLinkedListandwanttodeleteindex5,youhavetoiterateto5andsuch.Your
examplewasABCD,ourpointsarethatyoutypicallydon'thavethepointertoBjust
offhand.YouhavetoobtainitfirstwhichwillbeO(n)
2

Reply Share

PinguApp>AlexisMas 3daysago

WhatifBisthelastelementinthelist?
HowwouldB'spredecesorknowthatitsnextfieldshouldpointtoNULLandnottoafuturely
invalidmemoryaddress?
1

Reply Share

AlexisMas>PinguApp 3daysago

Inthatcaseyoucan'tdeletedthatway,you'reforcedtohaveapointertotheprevious
item.
1

Reply Share

qwertykeyboard 2yearsago

ItwouldbeveryhelpfultohaveexporttoPDF.Thx
17

Reply Share

Gene>qwertykeyboard ayearago

YoucouldconvertthedocumentyourselfusingPandoc:http://johnmacfarlane.net/pand...
Itmighttakeyoualongtimetogetitworking,butPandocisanamazingonestopshopforfile
conversion,andcrossplatformcompatible.
IfIunderstandbigohnotationcorrectlyImightsay"IestimateyourlearningrateforlearningPandoc
willbeO(1).".
2

Reply Share

Ashutosh>Gene ayearago

provedO(n),n=numberofformatconversionstolearn:)
4

Reply Share

JuanCarlosAlvarez>Gene ayearago

bigoh.hahafunny.
1

Reply Share

Guest 2yearsago

Finalsarealreadyover...Thisshouldhavebeensharedaweekago!Wouldhavesavedmelike45minutesof
usingWikipedia.
11

Reply Share

sigmaalgebra 2yearsago

Youomittedaninplace,guaranteedO(nlog(n))arraysort,
e.g.,heapsort.Youomittedradixsortthatcanbefaster
thananyofthealgorithmsyoumentioned.
MightmentionSATandrelatedproblemsinNPcomplete
wherethebestknownalgorithmfor
aproblemofsizenhasO(2^n).
http://bigocheatsheet.com/

9/12

2/19/2015

BigOAlgorithmComplexityCheatSheet

aproblemofsizenhasO(2^n).
Mightincludeanactual,precisedefinitionofO().
9

Reply Share

JonRenner 2yearsago

AnywayIcangetaPDFversionwithouttakingscreenshotsmyself?
8

Reply Share

AttilaOlh>JonRenner amonthago

PrintDestination:ChangeSelect"SaveasPDF"(inChrome).
2

Reply Share

AntoineGrondin 2yearsago

IthinkDFSandBFS,underSearch,wouldbemoreappropriatelistedasGraphinsteadofTree.
8

Reply Share

ericdrowell

Mod >AntoineGrondin

2yearsago

Fixed!Thanks
4

Reply Share

QuentinPlepl>AntoineGrondin 2yearsago

Agreed
1

Reply Share

tempire 2yearsago

Thischartseemsmisleading.BigOisworstcase,notaveragecase~isaveragecase.O(...)shouldn'tbe
usedintheaveragecasecolumns.
16

Reply Share

guest>tempire 2yearsago

IthinkbigOisjustanupperbound.Itcouldbeusedforall(best,worstandaverage)cases.AmI
wrong?
17

Reply Share

Luis>guest 2yearsago

Youareright.
6

Reply Share

Oleksandr>Luis ayearago

@LuisThatisWRONG.@tempireiscorrect.BigOcannotbeusedforlower,average,
andupperbound..BigO(Omicron)istheWorstCaseScenario.Itistheupperbound
forforthealgorithm.Forinstanceinalinearsearchalgorithm,worstcaseiswhenthe
listiscompletedoutoforder,i.e.thelistsortedbutbackwards.Omegaisthelower
bound.Thisisalmostpointlesstohave,forinstanceyouwouldratherhaveBigOthen
Omegabecauseitisexactlythesameassay"itwilltakemorethanfivedollarstogetto
N.Y.vs.Itswillalwaystake,atmost,135dollarstogettoNewYork."Thefirstbitof
informationfromOmegaisessentiallyuseless,thethirdhowevergivesyouthe
constraint.Thetaistheupperandlowerboundtogether.Thisisthemostbeneficially
pieceofinformationtohaveaboutanalgorithmbutunfortunatelyitisusuallyveryhardto
http://bigocheatsheet.com/

10/12

2/19/2015

BigOAlgorithmComplexityCheatSheet

find,butwehavedonethis.Youcanusuallyfindthataverageforanalgorithms
efficiencybytestingitinaveragecaseandworstcasestogether.Simplythisisa
computationalexercisetoextracttheempiricaldata.ThereisanotherproblemIdonot
likeisthecolorschemeissometimeswrong..O(n)isbettertheO(log(n))?Inwhatway?
1024vs10incrementsthatasortalgorithmhastoperformforinstance?Allinallthisis
goodinformationbutinitscurrentstate,tothenovice,honestlyitneedstobetakenwith
agrainofsaltandfactcheckwithagoodalgorithmbook.However,thisisinMHOsoif
I'moffbaseorincorrectthenfeelfreetoflamemelikethefantasticfouratagayparade
:)
6

Reply Share

Guest>Oleksandr ayearago

@OleksandrYouareconfused.Yourexampleaboutthedollarsstatesspecificamounts
(e.g."atmost135dollars"),butbigOandrelatedconceptsareusedtoboundtheorder
(linear,exponential,etc.)ofafunctionthatdescribeshowanalgorithmgrows(inspace,
time,etc.)withproblemsize.Tobemoreappropriate,yourexampleshouldbemodified
tosaysomethinglike"ittakesatmost2$permile"(linear).Withthisinmind,youcan
thusunderstandhowbigOcanbeusedbothfor,say,thebestandtheworstcase.
Takeyourlinearsearch.Asthesizeoftheproblemgrows(thearraytobesearched
growsinsize),thebestcasestillhasanuppertimeboundofO(1)(ittakesconstant
timetofindanelementinindex0,oranotherfixedposition),whiletheworstcase(the
objectisinthelastindexwherewelook)hasanuppertimeboundofO(n)(ittakesa
numberofstepsoforderequaltotheproblemsize,n,untilwefindtheobjectinthelast
indexwherewelook.).
(fixed:wrongautocompleteofwhoIrepliedto)
3

Reply Share

PhilipMachanick>Oleksandr ayearago

Omegaisuselessunlessitisatightbound,i.e.,itrepresentsrealminimalcasesthat
areinteresting(whenyouhaveoptionslike<=or>=inthedefinitionofabound,you
shouldatleastgetclosetothe=case,otherwiseyoumightaswellusethestrictly<or
>cases,andeventhereyoushouldtrytofindboundsthatarereasonablyclosetothe=
case).Forexample,strictlyspeaking,quicksortisOmega(1),butOmega(nlogn)is
moreinformativebecausetellsyouitsrealbestcase.
Inanycase,youdonotnormallyuseOmega,Thetaetc.fordifferentiatingaverage,best
andworstcase.Theseareboundsonanyofthesecases.Forquicksort,theworst
caseanalysisisn^2andthisisboththeupperandlowerboundontheworstcase.You
useOmega,Theta,etc.whentheanalysisforaparticularcaseisnotclearandyou
havetosayitisnobetterthanornoworsethanaparticularanalysis.
2

Reply Share

Luis>Oleksandr ayearago

http://bigocheatsheet.com/

@OleksandrYouareconfused.Yourexampleaboutthedollarsstatesspecificamounts
(e.g."atmost135dollars"),butbigOandrelatedconceptsareusedtoboundtheorder
(linear,exponential,etc.)ofafunctionthatdescribeshowanalgorithmgrows(inspace,
time,etc.)withproblemsize.Tobemoreappropriate,yourexampleshouldbemodified
tosaysomethinglike"ittakesatmost2$permile"(linear).Withthisinmind,youcan
thusunderstandhowbigOcanbeusedbothfor,say,thebestandtheworstcase.
Takeyourlinearsearch.Asthesizeoftheproblemgrows(thearraytobesearched 11/12

2/19/2015

BigOAlgorithmComplexityCheatSheet

Takeyourlinearsearch.Asthesizeoftheproblemgrows(thearraytobesearched
growsinsize),thebestcasestillhasanuppertimeboundofO(1)(ittakesconstant
timetofindanelementinindex0,oranotherfixedposition),whiletheworstcase(the
objectisinthelastindexwherewelook)hasanuppertimeboundofO(n)(ittakesa
numberofstepsoforderequaltotheproblemsize,n,untilwefindtheobjectinthelast
indexwherewelook.).
2

Reply Share

Oleksandr1>Luis ayearago

Youmakeaverypoorassumptionthatbecauseaspecificvalueisgiven,
thanitmustbealinearfunction.Itisinfactanypolynomialfunction
ofmychoicegivenitsparametersandanyamountofLagrangeconstants

PagestylingviaBootstrap
CommentsviaDisqus
AlgorithmdetailviaWikipedia
BigOcomplexitychartviaMeteorCharts
TablesourcehostedonGithub
Mashupvia@ericdrowell

http://bigocheatsheet.com/

12/12

Potrebbero piacerti anche