You are on page 1of 4

# AlgorithmicComplexity

Introduction
complexityasanumericalfunctionT(n)timeversustheinputsizen.Wewanttodefinetimetakenbyan
algorithmwithoutdependingontheimplementationdetails.ButyouagreethatT(n)doesdependonthe
implementation!Agivenalgorithmwilltakedifferentamountsoftimeonthesameinputsdependingonsuch
factorsas:processorspeedinstructionset,diskspeed,brandofcompilerandetc.Thewayaroundistoestimate
efficiencyofeachalgorithmasymptotically.WewillmeasuretimeT(n)asthenumberofelementary"steps"
(definedinanyway),providedeachsuchsteptakesconstanttime.

integerstakesnsteps.Consequently,thetotalcomputationaltimeisT(n)=c*n,wherecistimetakenby
machinesresultindifferentslopes,buttimeT(n)growslinearlyasinputsizeincreases.

Theprocessofabstractingawaydetailsanddeterminingtherateofresourceusageintermsoftheinputsizeis
oneofthefundamentalideasincomputerscience.

AsymptoticNotations
Thegoalofcomputationalcomplexityistoclassifyalgorithmsaccordingtotheirperformances.Wewill
representthetimefunctionT(n)usingthe"bigO"notationtoexpressanalgorithmruntimecomplexity.For
example,thefollowingstatement

T(n)=O(n2)

Definitionof"bigOh"

Foranymonotonicfunctionsf(n)andg(n)fromthepositiveintegerstothepositiveintegers,wesaythatf(n)=
O(g(n))whenthereexistconstantsc>0andn0>0suchthat

f(n)c*g(n),forallnn0

Intuitively,thismeansthatfunctionf(n)doesnotgrowfasterthang(n),orthatfunctiong(n)isanupperbound
forf(n),forallsufficientlylargen

Hereisagraphicrepresentationoff(n)=O(g(n))relation:
Examples:

1=O(n)
n=O(n2)
log(n)=O(n)
2n+1=O(n)

The"bigO"notationisnotsymmetric:n=O(n2)butn2O(n).

Exercise.Letusproven2+2n+1=O(n2).Wemustfindsuchcandn0thatn2+2n+1c*n2.Letn0=1,
thenforn1

1+2n+n2n+2n+n2n2+2n2+n2=4n2
Therefore,c=4.

ConstantTime:O(1)
Analgorithmissaidtoruninconstanttimeifitrequiresthesameamountoftimeregardlessoftheinputsize.
Examples:

array:accessinganyelement
fixedsizestack:pushandpopmethods
fixedsizequeue:enqueueanddequeuemethods

LinearTime:O(n)

Analgorithmissaidtoruninlineartimeifitstimeexecutionisdirectlyproportionaltotheinputsize,i.e.time
growslinearlyasinputsizeincreases.Examples:

array:linearsearch,traversing,findminimum
ArrayList:containsmethod
queue:containsmethod

LogarithmicTime:O(logn)
Analgorithmissaidtoruninlogarithmictimeifitstimeexecutionisproportionaltothelogarithmoftheinput
size.Example:
binarysearch

youmakeaguess,youaretoldwhetheryourguessisstoohighortoolow.Twentyquestionsgameimploiesa
strategythatusesyourguessnumbertohalvetheintervalsize.Thisisanexampleofthegeneralproblem
solvingmethodknownasbinarysearch:

locatetheelementainasorted(inascendingorder)arraybyfirstcomparingawiththemiddleelement
andthen(iftheyarenotequal)dividingthearrayintotwosubarraysifaislessthanthemiddleelement
yourepeatthewholeprocedureintheleftsubarray,otherwiseintherightsubarray.Theprocedure
repeatsuntilaisfoundorsubarrayisazerodimension.

Note,log(n)<n,whenn.AlgorithmsthatruninO(logn)doesnotusethewholeinput.

Analgorithmissaidtoruninlogarithmictimeifitstimeexecutionisproportionaltothesquareoftheinput
size.Examples:

bubblesort,selectionsort,insertionsort

Definitionof"bigOmega"

Weneedthenotationforthelowerbound.Acapitalomeganotationisusedinthiscase.Wesaythatf(n)=
(g(n))whenthereexistconstantcthatf(n)c*g(n)forforallsufficientlylargen.Examples

n=(1)
n2=(n)
n2=(nlog(n))
2n+1=O(n)

Definitionof"bigTheta"
Tomeasurethecomplexityofaparticularalgorithm,meanstofindtheupperandlowerbounds.Anewnotation
isusedinthiscase.Wesaythatf(n)=(g(n))ifandonlyf(n)=O(g(n))andf(n)=(g(n)).Examples

2n=(n)
n2+2n+1=(n2)

AnalysisofAlgorithms

Thetermanalysisofalgorithmsisusedtodescribeapproachestothestudyoftheperformanceofalgorithms.In
thiscoursewewillperformthefollowingtypesofanalysis:

theworstcaseruntimecomplexityofthealgorithmisthefunctiondefinedbythemaximumnumberof
stepstakenonanyinstanceofsizea.
thebestcaseruntimecomplexityofthealgorithmisthefunctiondefinedbytheminimumnumberofsteps
takenonanyinstanceofsizea.
theaveragecaseruntimecomplexityofthealgorithmisthefunctiondefinedbyanaveragenumberof
stepstakenonanyinstanceofsizea.
theamortizedruntimecomplexityofthealgorithmisthefunctiondefinedbyasequenceofoperations
appliedtotheinputofsizeaandaveragedovertime.
Example.Letusconsideranalgorithmofsequentialsearchinginanarray.ofsizen.

ItsworstcaseruntimecomplexityisO(n)
ItsbestcaseruntimecomplexityisO(1)
ItsaveragecaseruntimecomplexityisO(n/2)=O(n)

AmortizedTimeComplexity
Sincecopyingarrayscannotbeperformedinconstanttime,wesaythatpushisalsocannotbedoneinconstant
time.Inthissection,wewillshowthatpush()takesamortizedconstanttime.

Letuscountthenumberofcopyingoperationsneededtodoasequenceofpushes.

## push() copy oldarraysize newarraysize

1 0 1
2 1 1 2
3 2 2 4
4 0 4
5 4 4 8
6 0 8
7 0 8
8 0 8
9 8 8 16

Weseethat3pushesrequires2+1=3copies.

Weseethat5pushesrequires4+2+1=7copies.

Weseethat9pushesrequires8+4+2+1=15copies.

Ingeneral,2n+1pushesrequires2n+2n1+...+2+1=2n+11copies.