Algorithm Complexity Analysis Guide
Algorithm Complexity Analysis Guide
-Ajinkya- Rane
28/4425
ALGORITHMS Date
Page.
&tatent
to the
Count the undamcntal
Operhon the Statemsnt Istkp
ch apeation
xponsnta tunet
Tme
Bn) AC ) w n )
Avq
= A n) = w() Sort
)Bn) Mexg
Seletíon SoYt
Hep Sort
ASN
tppebanl
ISna) 1tte poper
U.B
-Oh: (O) ’ He oh: o
oweY
poper
tght eound
be fnetionsam he
of inic ges to Reay numbe
Upper bound
fln) i her
Some Conetants and
such that
COheneuer i itisedeterilnei
L'B çwe should nd thet
tnetion. ushi ch closest
to givien nétton.
Date
Page.
& flo) is 2
egzy og2egy
2
agAoga
b
og
Date
Page
Sn = arh-)
n
oheee
nk
(ommooatio 2n
nn
21
i 20
2(2n1) 2
2
giometic
So
Date
Page.
Fominanee Relatton
<taco
oleyeain jange To Pele
Ja b9
< xponntiad
<n²<n3
n(n+1)
B) £)
2
A
n(04) en+) o(n3)
fCn) 2
2 ( 2n2l-2)o(20)
) fln)
S i 2 - (n) 2n +2
Steing93 ApproRlmation:
n n (m Date
Page
9)f =
n
=|:2.3...n) =n= nl)n-)...
o(nlngn) Log n
#Sinalllitle notatHons
the bousd avid od by Bg
may
not be igkt
the bounds peovicled by s al)
not atony
Asymptghcaly aliay
hgktLobse
NOT.
bound.
5T
gor al'c'
Date
Page.
is qsb
2
ab
|ReftexieV
KIk X
Synmee X X WX X
rtans
ive
is og X
Symmehthun goiseofy
gunca
Data
Page
GArE
2022
( fn)
let n be a
pot
goegn
(Aog)
g ) is oPn).
Date
Poge,
faxing
Ang
2n. -
-Aeg n
2
2
=0
e) Fn) =
Takingng
Ang (L)
2
Date
Page
a)
An) =
kno
FALSE
then
A(6) co(cocn)
retunDo
22T(n-2)+d d.
[Link]-k+ (25-1) d.
k=n-1
Data
Puge
= 2 n - c + 2h-.d-d
Tln) e2?-d
(2°)
(2 Exgonental
GATE
Algoithm
atunCAn;
= 0t
ta
ta
+ q fa
Tín)+ 2a
T +24
T2)+ K-a
(ne 2 6 (n=2 given )
og Jogi tAogn1 Oate
fage
Tlo)=2 n=2
=nT(O(o
+n
To=n
2 i91o99+n2 +n.
314-n+2n
TD) = n
+ 3n
+ k-n
2.
#Loop Compleihexs
Qfoz i -1 ton
) B)
b) fo < 1to9)2
cohile CK<=n)
: n g n t oln) + o(n))
Pes it os ii'
Q0forielto n 6n)
for ( 4 t o n.
for /t to nl2nl2
2
for / i to n.
on
for K 1 to n,
break
Date
Page.
oktle(ic=n
let n6.
x2 ix2 2'x2,2k
=2=n.
K do4
Time
LLhile i>o)
i-il2;
Sama
oAng)
for( i=l; i<=n i=ita)
Date
fime. ndo
ing 2
fime
O(n)* oco)
Date
Page
GiATE
int fun (int o)
lntiff-4=
o7 (isl;iken; t+i) in
Totat
fhe walue
he qntian
fime compleiy
+LDESTGAN 6TRATEIES -
si2e ot
CASETIL
nl 2
Tnl4 n4
-b o(n)
Tme lonplenity -t
-so(ogn) Date
Space Comptenity Page
2:foT t 2 tO n
fAlil;
ee
itAli] < min
min ATi1:
Take an
2 3 4 5
A -615 |20 33 649
Date
Paga
'/9,fmox,fmin
64 3
3n -2
2
Non -DC
gn -2
2
bettex
betr
3n 3n -2
3) Avq Random
2 2
bette
Time Compkenity oogn).
lomplexty o(logn) Date
4 Bpaue Page.
I | 2 3 4) 6
R.
mìde-|b
Key=
height |h-olioga
Comolet | Fuu 8-T.
mamim
max:
Comps: (ni-) +n2
T: Aeauitd to
Meg
& L2 (n2) ie blw
i s n ) &Ca,n2-)
4
min. mat.
olnt2)
2-1(o2)thnnbo
Tmao o(ndogn), degn) ie: s(og).
Date
Page.
A T(n).
Line) n]2) Le
T(O2) T(n}2,
mexn BestCale
n-)
Bottom Op mexge Son
n=2k
A65 10 45 85 60 55 50 45:
Pertormante
o fime omplesy
an.
paxttian; o (n)
o(n2)
T)-o(nLogn
8EST CASE WoRST CASE
fhe meolicn
Can be tound 'oo) 6me.
median is Selected Pivot,
han +he Cae complexity ot
Soyt is o(nog n)
+MateÌx Matiplicaion
A*8= time complerihy locn3)
-lo (n)
4+Ve
or some
E>athen T(n) is
Date
Page
tazgome kthen
To) is o f l )
Hoo to_&olve
t(n =47(n2)t n
a= b =2 (n)=nn
4 = 2.
for CASE I !
is 9 (n2).
(S-2)
Data
Page.
nloga
ndog'nl
tn.
b= 3 r(n)= n
Aog
Here ASE 1 is jecte d also CASE 2:
for cAsE-3:
af n|b) 6.(n)
?22
og?
Date
Page
CASE
n 2-s
is i+ oln2-e) x.
CASE-2 n2-5
G(n ogn)
CASE-3 is it
2-5
Tr): (n25)
JUSTPR oVING
mo-min
t2
-a= 2 b2 fn)= C.
CA3E
:.Ttn) isoo)
Tn)- 2-Tnl2)t 0.
CA3E-|:n is i+
CABE-2 Kn)
Datx
CASE-)
CASE -2 : n),K=0
T(r) is e
atiplyng
0-digth eash.
Date
Page,
)tbn.
GIATt
ro) Ck):T(N})+ bn
3
To) (2K-1) I( n])+bn.
Data
Page.
# GREEDY METH OD
Step-usise manne
A+ each Step optios
Greediy seleet hat Oph'on ohich.
Satisie the qiven critei a
proslem.
Efouroinalog
Constt ai nts Cconditinna)
Req uitements
(Gounday
Solution Space All possibe ays of
o2ganizln inputs saisging ony
epiicit constaints
Aluasge to and
hence
hiven. obje ct
cweigkt Deision.
(ei)
KNAPSACK Prsbem.
Swi2M. Expi'cat
Max P: Xi 06jtunt
Subjectt +Implicat
ohere
Saution Spacee
Fracttonay
n
Date
Poge.
P1:5 7
1:66-P2
6 6
M= 1S <1+2t 4t 5 +l+ 2
) Prot 55:33
2: 213
Ji
Prot, PE
8T Deoatlie
Lunt
finiegea >o)
AT Aivalfime.
Bt Burst fime
completed uithin.
the deaa ne. +hen you get it
Date
Page
J3
2
Fn:K23,S,,.i
Cntm)
f=5F2=20
Patkm ):
<L2,3>
F3 lota eeorel
movmet 35t4o
=75
Potem
Ts]
i,
<2,8,|>
[26 |5 Tortal
f2 F3 254 lyo
frovement
=65
Date
Page.
Soutior Space
# The moyenents =
coeigated 2xterna path length as
Bnany tree
root to Fifle)
am
die dist ance tom
Fta Lsize ot
(30
> 2+ lotlx15 2 5 45
5
A
eap trior-tfnea)
n'elements n'elemeots
-Insext
least:n Delote
Inyet: - doclInc
Key.
Total: o (dog)
Date
Pgge.
Huttman coding :
ne
Time conplenity -tolne)
for undiecteel qaph -t le-a-)n
2. T
for dlsected paph
Gaaphs
Derse Spawse.
Graph. Saph
Kalmot Complete) <ot compete>
e o(n2) -’ Adi' list
on+e)
be Her)
ointe.
Tota edee
Spanning T sitth n yez hces oill
. have
ealg
Remod edge =e-(n-)
=e-nt}
foeomaplete
graph (Kn).
AlgoithmA COnatottonOtmìntmuy
) Pohm-Jaxinik Algo
CH) koruUhkalls Algo
ti) Diesera's Alga
Date
Page.
28
14
25 24 18 12
22
krLskals Algo
Kto, 12,i4,16.L8, 22, 24,252)
14 16
4 2. 16
22
Step
Cozt o¢ Gpan aing
Span ning tee ith both.
be Same.
appmaches
The
alucay
eeeuetuo may may No
be Sane
The also be Same itt all
edges have unlqu Cost Cdistinct)
Date
Page.
DiKstbos Algarithm:
I6 14
25 24 25
l2
22 22
y23
lG
X18 12
22
TRme complexig
14
tocoe)
25
12
22
Dijestras Alg
(9Singe Sowtce Shortest Poths
Bellmao-fore
S >de (n-)
dn
-Ve
Mattix method
45
26
5 35
t5
Vee
seleckee 2 3 4 5
S-I
S-2 50
S-3 f,4,5
S-4 l,2,4,64
S-5 2,34,5}
Vetex
setected 2 3 5
S-I
S-2 38
S-3 0 23
S-S s62,354 (3
Date
Page.
Relaxahon Procesji
Kold)
25
30
So
20
2s
SSsp Sp Trte:
75 <42
55
60
<2)
85 < )
mcsTi
Date
Poge
#Dynamic Proogamming
Designstkp03tel hy
Richome) Bem
Taxget mensy
CN=12) Coiny - il2,53
5+5t2 =12. (Geedy methed).
GREEDY MEHOD FALS HERE
ist clecision.
campd,-omplag Memoiz0tion
(top-Douon Anoach
DP implementat
A Tabuati on
Ceottom -UpAnnxeack
2 3
m.
L2 35
mem Ab (s)
memfib (4) Yes3
memffb(3) ey 2
memfb C2)e
memfb ()
mem fb(o)=o
mem f h ) l.
mem fib (2) =
memfb ()2.
fb (5)
fb (u) fb (3)
Tme
-IocoPlexiby
Abo)
Space Comp lesity
Date
Page
Biilding n ot the
Sol he Ablemn clone
maonei Cindepenclent) by
analying locay opions OAI4y (loca
BAceuking up of a.
Acoblen nto Geeies of "ovel apping
Sboblem & buildiNg wý Solution
1agex & tavge subo6lems.
Date
Page,
Multistoage Graph
V V2 Vs
6+4
[Link]
C41o)
5+2
Date
Page
Cest (3)- 5.
D(3, 1) = 10
Cost (3, 8) = t:
D3, 8) - lo
Cost (2,5) = |5
D(2,5) 8
minimnum cOst = 6
Path
fotlem TSp):
# Teaveleingy Satapexaan
fhe tou' ot TSP shoud staxt m
home &
Qnce &
visit Amaining
Co mes
baak to
Gties
home city Vo,S-T:ha Costot tout
is minimum.
Date
Page.
gci)
KES
gi,)= cliVo)
i- Vo
21 34
2
6 13 12
3
Vo =|.
5l=3
K=2
ghi438)- min,c02)(2,fri),
K-3 10+25
c,3) +g l 3, f2; 43), c(1e)+g(4, 2,33) 26+23.
= 35 |S+ 2S
J I iz3,43) -2
.
2i33) -c (4,3)+gC3,+)
g3,d= c(3,) =6
g C3, i23) = 13+5- I8
C3,43)= 12+8 -20.
4,23) = +s=13
Date
Page.
Ist=2
(2, i3,4 25
3(2,3,43) = 4
9(3,i2,43) = 25
3C3;2,42)
g(4nie,33) =23
J( 4, 2,33) = 2
Toux Constuctioo:
1-2- 4-3-1 = 85
gC, i2,3,43).
K=2 3
3)
(2,83,43) g3, R2,43) 9(4, 2,33).
44 k= 2 K= 2
4K=3
4
c(2,3)
g3,¿41) g(939a(z4) g423) 43,83) (3,:23)
ca4)ck,2 c23)
t + +
g(4,)
Recuxsion Toe
ACiS)
Base tion
Condition ACi) =ccij)
(o no intemealiate
Verte)
ACt) ( ) +A CHi),A(Ga)4
AKCi)
Ak-i
A (K,i
Recuaon Tbre
C I 23
2 66 o 2
3 3 o0
Date
Page
A A)A ) A°:
A 2 3 A 2 2
2 O 2 6 o 2 o
3 3 33
via
A2. 2 3 A3 23
46 fna madtiz.
6 O 2 2 5 o2
3 33
3 o
GATE
Co:)
<A B G>
<ABC
Les Cnm)
LCS,mcs (o,m-)
Lcs(n-, m)
Lcs("AB' "Ac')1
LCS
A BA R".
Time ompezity: o(n3)
Spare comp e xity o (n2). Date
Page
Total 00 o parenthesi2atn s
by Cat alon no 3
2n
(ht)
-<144)
Date
Page
<ALA 2 A3 A4)
K-l 2 3 (14L4)
(240 (230)
216
< A) CA2 A3,A)> <(AA2)(A3A <(AiA2A3) (A)
23'5 t S-g4+
K=2 K=3 2:
K=1
(220) T(216).
ka)(AA) <(A A) (A«) <A) (A2A3)> <A,A2)(A3))
3A5 S4 (3-5-8t +
3-5-4)
. nal. paenthasis
(AtA2) · (A3) (A))
k=2 K=g
=20 t 80 t 2-8.4
=14 4
for kt j to /-1
if (qs mlij
then mIij]
:o(nxM)
Time Conmp kxity
Spare (omplerity :0(nxm). Date
Poge
Su6Sek sos)
#Sum ot
A-ele ements
ments Cintegus)
Set of Coum ber) M!.
nothe elenment
is to
the problem exist a Subsets
dekemint ik thee
elemeots obose Sum
6f the given
equn to
50> M=50
Ex n=si A:K10, 20,30O
)2,3
) 53
Dexivot o Dp baded
SaS:
n=5 ALl,, M
Sos (s,3)
Sos( 43 Sos(4r2)
n=5:MesA:{2,8,4, H,4)
2 6
Ai F F F
2 F.T
8 2|TI F
2 TF F
T F
STF F TFT
intesicted path
Veties
n- vertices Cannot have
gapb, having
than
withoLLt loop l cycle
t
-2
()
Date
Page.
.The time
Complexih os
Complet qzaph. having n! yertice
o(n3).
gaphi
a) Gonnecteal Grophi
fenninola
)Stay o a nodei
E-Noclo : explomng Node
CNode ohichis eérentty
being exptored)
Livenods i Noce ohich is not
uly explored live nodey
a e Stoed -in Some Ds)
Date
Page
: A)
sho
H)
G=CyE). DFS Spanning Tre
Valid Invatd
A8, E HDE,G
A H G8D.
)H D B EA
3connected components
A
1sl24 6l22
R1l2e
(9l2o Oís121
Back
fonDord
<AD)
<CA)
<DC)
2s
9
Data
Page.
sl4
Ôs/s
3/2 lo]i!
Amag. elements in ginishing desending orel
(BO’’
EIFO -BES
A
Parons A A e 8 CC D.
GcVE).
UFO-BES live
Ptort A AC CG HHH
Date
Page
GATE
Stronglñ Connectkdr Componets
Connected Campane nts
DDirected Gaphs
Seo3
secl
Sech.
Sec 2
GATE
3
.:5- Bico n
Compo
Date
Pooe.
<Rac'x Sort
Ns Itexative
In place VsNo-IN- PLACF.
<Meege sort
t Stable Vs Unstable
Tnvetsion of an AO
- e t AÏ|n] be
incices (i+j)it Ci<y) and CAIi1>A]
thon the paix S (know
inverÉon. the axy'
2
A 8|94 5 2.
on)
BUB BLE SoR
if (ALIJ >Atitg)
2 3 S6
A: <&o 6o 20 |S 40 10>
<6o 2.0 1S
I4erat ’1
<6o 20 8o IS
<6o 20. IS 980
<6o 20
Time (oplerity
Coamp axison Suaps o (n2)
inc nn-)2
Deet n (n-)2 n(n)2 (n).
#SELECTION SORT
A 8o 60 2o IS. 40 |0
i=1; <o60 20 IS 40
4o go
Time amplfxity
4A
lomparisen
n(n)
2
#LNSERTIoN SORT
<Partt al Sorted list> usortcd list
Kaae a g Q K et 2
at thrend
(a, 42ag(-.-qk)
<patial &oYted list)
A:<8 2 49 3s2.
2. 3
Pass8 2 49
49 36
torn clmutj
P2 2 4 9 36 (n) ases
P3 2 4 9 3 6 quired.
p-t 23 4896.
pne-Sorted then
time ot o(n td); d = number ot inversions
Date
Page
Time Comp
omp krity
eest cosc
2 3
64
2. 3 6
HEAP:
32 (13)
Heap Constuction :
) BestcUE'
leatnede
2i 2it)
Time Complexity ot Heap Sort is C(ndegn)
Date
Page.
(tu)
(132)
3T6
Si S2 S3
CA)e make any elenent ay oprasentativel
Operahon on Set
Find (a) I
Find a)
fnd (5)+|
Repreientahon Of s
Aay boied
Let o=l0
Reprelontation
L 2 3 4 s| a 8
3 6-|6 ’ 2
Time compleaty '-0(edoye) Date
FoY complet Graph. Page
28
22
tl3,]
ttt,j <u,v) <2,3)
16
tl4,2] i-2ke3
25 +[32)
12 k2 kt
Et62] 22 t[2,2] <u,v)= <3,4>
I5,23 je-3, Ke4.
Kuv)= <45) <u,v) =(47
jt-3; k<-3.
it-3kS (we anit add edee).
jt-3 kt-l
Ku,v)= <5,+)
j 3 , ke-3
that the
binaxy Valee
4tee oith
each
the
node gteate
+han the valuee os ke7t chilcxon &les
than the Value ght children.
Date
Page.
(n+)
) 2cost(E:)
) cost (Ei) Uevel (Ei)-)
-qi* (leve) (Ei)-)
pxoh, of ing the ideati7iei
in he set E
# Congtucion 04 BsTi
t-hat is adlele d. to e
L:s-T & RsT to balonce it
TOot:
i) Gzaph Calauing
C) Hamilto oian cyele.
V) oll Knapsack