Algorithms!
Algorithms!
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
st e a task:
seution
Sortg Pastlem
bubble Seectisn
Sot Soyt
ex
Post
ioi awa siso eah
hands are
dateucomput
uoita
dupudeut duteeet cok peda
with det dtnucion set
acee pud.
analyss dapudi
handane
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
Prisni auolys
thetie taleinLy
Intead
2
we stsd
We staps t tales
Thi called pioni anay
Add (a1b,f wy
Maoiitm )
co
Step Ut
foY i=| to m do
do w[n+)
ali,j] + b|j] m..
[ij]:
:. tal
To no
e skps
= m+l + m(n+ I) + wu
2mn+ 2t
3lddavd
Thercfore, we
Se
ext
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
Asymptotie aMalyss
tie
id unitats orde feas)
It is gcat tem
telle tie
wweases tee
A
synpteöie otoon
wotaton
2
ot tion
wotalu
matheodcal
Notation
ittle- t
w
tLe
Big - oh
T(n).s, cgn)
provide asywmptsticipper bo
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
Suppose
Time algoui taun ca
be
atuost
conplaty
uput
Notoat
Asy naptotie aay done
txd We
do mot albet
and So tt
T[n)
aund
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
|i-e
3. Theta Notation
T(n) :
T(M): Blgtn)
Thene exist
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
e u tue?
Querton
o(gtn)
None
ns n
alus a
=2 whi
tn):
D.None.
Remen ber
< nog n
gusre canstants ?:
mction
we gore Cau loe
redueed | expaunda
C(u) +d ipuord
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
eponengia canbie
quadri atie
wiea
agatie.
D. None
2n:o(on)
(logn)
Quertion;
nogn (logn
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
Loq we
iwitialL
Camuot the
then,
w
aud
relation blw eu) an Jlu)
J(u):
: 3toq n
loq gcu)
2logn
= tog gu)
Aymptoticaly, g
tu) tun
we caunot Comeut
{ln) <geu)
e tl wetlod
tie cae
or real praing
ordex reai
but
decreare
wortlw
commow teame (wwutiply oi divide)
et te
(onstat
Taee
e
al ager Lor waen) otvte
cant
log (ftni) > tog (gn))
teu)< g(4)
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
Cat sey
<br>
need
(ex 2k 2
et)
Exaple conpe blus
Vgn
pa
(2)*
22n
n2 o(41)
pal
Quution
g(m)
Log n
<
ie tin) olgn1)
-
mlogn 2
logn
o(
(Logn)
logog
Priyanshu (GATE 2024 AIR 379, GATE 2023 n
AIR 384) @ https://x.com/Priyanshu__2109
<br>
w
bo u
(w)
(wt
ubon
ubon Mbo
) bon >a(u
whonubon
(u)o nis 3
o(uu)6)
(t
(")}
(uy6
(w)6
ubon )bon("
bo u
(t: (un?6)o
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
nd
Question
g(n)
log,n log,
aftn
taking log
as diye
poer at a
iee
No
togio
logn
Log2
Log lo >og 2
Logn
g2
=
2(9(n)
Uog, n
M
Quertion- fnetions
Avrag guouth
aigmpttio
m) Lag log n (log agn))
Lag n (og
2K
Let 2
2k
og
(24)°:
(og )" ( og )
Log22 Log
Cog
logn
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
(Loslogn))°:
<br>
k <
k<
lag( log n) < (toglog m) < ogn < (log n)
Quertion brd
Anange
rtu
Psit to
reneber
Taknglag
nlogn
Log n nlog4 nog 2
n
Toking og mlog
both
mlogn
ae
So, remeloe A
>B c,D
teriu elation
be denired
(cez Caut for
D
n
2
> D.
Relatuon A> 6>c
2
2 <n <2
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
ta
Ques ton
- Avvange fotlowg tuwnction
2
Logn
(log n)? Jlog n log log
Let ogn k
order,
T(n)
uy cs teut C
>0 ad
So terat for
for a
Conitouts c>o
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
Nototion
pol
gCu) o(T(n1)
-) ad
no vale for
-TIu) 2gca)
Tiansitiity
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
Reflexivityo Sqmimet
fony ({(1)
flu): ({lui)
Trau pose
yety
i{g(u): tn))
Stiring's approximation
Conpaison buw nd
V2zn
V2%. nl2
p
i < e
A
kog asyptticay
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
obbipx
>(ubo
:9
ubon
ubor
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
CSE
aATE
following ü falie ??
of egn)
o(w')
fcu) ogn
72) A
o(u)
Tue
GATE CSE
o(g(ui)
fuwtu tu):
Couiie
on ogn 9 nlog
2
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
CSE
9)..
ATE
200)
nogn
nlogn
CSE
ATE
003
J. nie.
Comteds
T. u tme.
o(2")
2 2ut) 2u
2 4
o(2") o/2"
flhe
qAIE
CSEh)
2004
pal
glu) : o(L(ni))
True
talhe
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
ATE
G 2008
2
{(n):
nogn
2 < n
( Cog n) < mogw
CSE
AtE
Anage
33
e
a<c
I·75
d
nlagn
d<c
Au
a d c e b a<d<c
CSE
uATE
n
fulu): Lo,
2
(Loq,nj2
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
n(u+) (2ut)
42+ 3+ Lall,)
logi t +logn
logl+log2+
Suppost teratene
i- 2 o(u)
st
L= 2
2k
2
TC: o( loq,log.)
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
oLog log,n)
o( og,2og
TC
(
3 Teiaplat
T(= O(
a)
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
Sut+
0(N) nne
3 S
tt
o(): o()
fo i=l oop
Iunes
for [22 2
o.. )
o(N+}t.)
o
(Ngn )
1; N;
falj j<z j= j*2) for
i; i<zN; i++)
G ot woga)
Suw tt)
TC=
o (wagN)
Tl o(Nog) Suunt}
i:2 2ti
utu
tC: 0(2)
Kal
.
2
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
ol) lt2+4+8 J... N
= C* 2)
<br>
N
(C=l; i<z
tolj- l; j<= 0; j++)
2k
TC: l4 2-+ 24+ 2+ +2
i= 2 23 o(2)
og cog N timel
tor (i 2; ik=n;= ^2)
tor teu
(j=l; j<=n; jt+) N
(a
J: 2 N+
J= 3
N+4+yt-- )
fim Nlog N
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
tates
tune
casee
w
all
ne
worst Cene lus bverall TU ofn),
best cane wl alno
alo ee
J
logn
No conmmenty wort
Cane
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
Complexitye Recursiv
Recursive Paognoms
Time
Recsee relotin
Arst we meed
+ L
T(n) T( )
n
else etu
A(n)
4 (n<=) retuy ; T(n -) +1
n
T(n).:
elie retm k(n-+n;
+ n T[M): T(W) +.
2fuu (2)
ung 3 mmetods
by
The recusece, velation ca beeed
be
b
Receive wethod
Maiters method
Su stitition method
Ttesotie T
T) :
(
k: m-)
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
t(<-w)1 (-u) + w
(<w
TSW
(n)6
(4-1)z1a-):
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
(w)l
Tu
+ Bon
boBonu+)z
Bon+
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
+()13
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
lower bound
wtper bound
T[n) s n loq
(8
T(n): o(n logg.")
T(n) T
2) T
)t
12
2u
3
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
2n
2M 24 4n -(3%*j
ottoga)
Cower bonol
n 2
ol
2 |-1) k
)
dacrealiy
P sine
r<l:
dont cre
T(")
Keueuler
trie
I+C+'+. ...
0()
+ :
^ 9()
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
T(n)= r( n-1) + T
(n-2)+
n-2.
n-2
pper bound
T(n): (+ 2+ 2+ 2+
ratw
lower bond. 2
T(u) (+2+2+
1
( T(n):(2)+T()+ T()+
32
42
pper boun
T(n) l+34 3+
l+34 34 3
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
T(n) aT()+ cn
6
At
i lerel,
(-0
Comionratus=
Heuce hare,
axuk
T(n) :
ogn)
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
Theorem
Master
recwenee relatien te type
Soe
T()
logar
Masten o
reutenee the
thuorem pplies
and costout nd
gpttally poitve tunt
There
Some oot at 6,
he,
T(n) la
tuen,
T(n): len)
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
T
T(n): ()+n xsteoM
tin) n
wog
Tn): 3T)+
7(1): 0(ntogn)
(3)
net
moste tereore
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
Mastevs Theonem;
Extended
ReceMce
+
T(n) : aT)
yecurn a
real uoer.
b
Case
I- II k <
\ogan
the
T(n)
= n
(a) i4
P-1)
Case TI
(a) i! p>= D
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
-2
k=2
und
T[n)o()
=
T(n) 4T
ke2
ogs 2
).; aT[)
and
:. Tn): en)
T^): 4T (2):
P 2
T[w): [u)
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
iognt n
Log n is not a cons taut
b:
=
T(n) 2T 4
-
T("): 37()
B met postve
4(n):-n
T(n) : ()+
T wsinn
2
violae!
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
hamae ef vasuiasle
=
T(n)
wetud.
T[2"):
T()
Let
-(
s(): s(w)+
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
+
(<}m)s
()@(w)1
m
(w)s (t/m)s t Bor
m)s-(u)
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
t(zhw)sP
(w)s (u)o
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
u
(u^h bon)+
<br>
(u)l
wBon
Bon 9
(w)s )wt(h)stl
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
907
<br>
LVh
wBon
m)s ( T+(fm)s P
(m)s (m]
+
)
aT3 (10
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
Divide amd
MaxiMm
waxinum ( a, l, f
Case
Bare base case
Hhee nekurn
tne element
ttaen waxinum
s elmnt maninm (a, m);
bcelk
1= animmla wMtl, );
nekurn ( wmax(
u,)):
T(n)
T) +c
9019h1
ww
3
2. Search
k Search (a, i,J, vale)
i (alc]
=
vale)
Yeturn tej
elie
retu
aiy
,
return search ( a,i wid, valne) |
search la, a wid+l,, value )
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
3.
3 Dumb gortno) bMO ivi
may
i, j)
sort (a,
T(n)
Baue care Tn-) +4n)
4(i:=j)
always sokd eturn o(n)
Sost
eeet yort a, itl, j);
from itl, j
Put alu) n
(estion ort
mohod).
4. SoTt (oje
Merge
4lizj)
yetu;
wid: (j) l2 4(alplk ai])4
Mergesot (a, i, wid)
Megecort la, wu'dtl, j)
merge (a, i, mid, j)
ktt;
T(*): 1|2) +
ud
elenets
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
Sort divides to
Wat tle
3
egired
4 sze
3 subasaye
2ud subassay
(Megg
Compa
two1st
(ase with
Merging
3
Recurens *Total wo
Kelatiem Sn -2
3 T[ 4)+ D(")
T(n):
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
biteic
ers exsts a t Such Hat
AL.] is ieeaiing
(tve a to sot
1: Reverse
A
[t+... ]
s0tadStorde. kere-led.
auay A cevdo
al e wmbev
elr)
2- Cept odd
3. SoYt
logn
agn
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
a
you hoane
Suppose that
atnnatg and A's sacting wtth B.
BABA B,A BA
42
N
eleweuds
AAAAAAANBLBBL888
to merge
Laut Rs Bs Ce
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
botton
up wnge
Sort
6 9
45 | 2 2 3
Stragid Two
4 5 6 9 |-2 2 3|
So
2 2 3 5 65.
Tme comple *
taleing o (n)
TC o(nlogu)
code
Bottom up weg
tor
withl; widh < a.
ugth, width 2*
on
i= o; i< abugth; i: it 2* wi)
hugt t 24
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
5 Merge k sorted
Sorted and
Trinal ut.
SoYt
motod
TC=
Mekuod 2
pairwise
OR
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
SucUesive
Werge
Total
taen
243+4+ St.
Metthod 3
ing k
pouuthe fr
win
kindimimw amng
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
Metuod4
minimum lements ron ea
heap
bild
Subasay
Delstng eleet
seted asay
tog
dalete
to te heap
Qusent e
To peace eleewt
t required
loqu for
dalatin iájuutum
Total
level,
tahen
lme
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
uestom
Supposee have soytc uts
joted Wsts
wto a
Time
ki
list tsis
tae sortd
tiu
its
Hoe, ho bogn
1 Stable sorting
stalde lewnt
H sawe
Sae
ordes
Relatie brde
Input - 3
| 5
6. 2
8 statle corting
2 3a 3
5.6
nof stalsle soly
Outhut2 3
3a 5 6 9
2
Sortad
O Merge Lont
istalale
Suith sent u mot Stalsle
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109 na
<br>
2
In Place algonithm
2) soting agonitam
olgonithm te ie ttae
9)
-pace
sort (wotaploue algorit
wot
lyut
Mer ge sot staloe
but
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
ail;
-25
25
iegn
25 20 5 (wtu
25 20 15 S2.
Copauts n-)
Teguired to
fid
tuut
meiuod
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
best Case
| t
n
-2 n-l cowpawto
s
compauio
2n-3,
l+ 2(n -2)
tlod
Method i
touat
be
oetee
.
Maiim
angat lewet
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
metuo
Step Find wmaxi
tewuaet
n cop aisê
agLagnsilblnga
Step
2- Rnd 2nd
:Xb
a
a
aund
Finding
a1;
war)wman a
ll;
2
(n-) owparle.
lal]>
ese i4
(ot]< min)
Total
Best cae = n-)
Worst case = alu-)
Metuod 2:
for
Pairwise copane
cowpuud
poiy
Langut elet
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
Fo wituot,
dome
2 d
Smalen
ee 2
Tol
n-2 3
guired
1+ 3(9-)
3n -2 4
Dda, ane
itialiged to ato].
pai foned ad wade.
i4
-
-2.
-(-e)
herten
Let P
loe t
bet ogo
ad
- 2 ()
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
paunoie
elets pain plad
( eleweut Grecteu, elewut
elewtGwalinélemeut
Hinimw fow lst
utl
Maiw
uo
Tbtal
tour namet
72.
Sum mahy
n +Log, n - 2
No
compauisons regyu'red to
langet & second angest eleet
No e companisome equired to find
Counting inversies
{hat tuoo indices inwers
IWe say i<j for
2 3 5
Inrersie ma wversue Ce
Yues- Hoe
2-1 2, 5,7, 6,4,2?
4-1 3-2
4-3 5-2
5-4
7-4,7-2
Ques
T|b....] uversien (,)
n T,
theu
otteatj-t
wversi.
() cnrersim
>
alil> ali]:
and
etes blw
aju]< alj]
inrers\tn Cane
versn
aljl 2 aju] < ali)
Halk]> nver'm te ateart
-
iwers^e
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
Covpieity
merge ( e, mid,
t coit
, ar)
L2 anwid +I.... Y
t >0
lp<= hid-A
Re <>Y- wwdi)
tile
eleto
Aat co
tt M-l-p;
luertn
a t
bit i tt est
wweriee
hhat wl be te to
dereey
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
wvers ierns
Total
3hor
ever aked
Closest fai w
2D
. -
(0
6
l3
7
dutueeb ay pair
for poute
Or d
te poiut
divide eund.conquen
ww
SL Sp, )
pot 2D
First nd wini
m
duetauee
jotol
points
S.
autauee
be
Thee wie pacitto tes20)
radiue
Swbprobe.
(1
Exponent ef
X
a
buute
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
Divide ad a: a
.a
congyen-l.
Divide cud ah
as
iuged
J
T(n):
exponeut (a,n)4
K= expoeut (a, n);
yela ;
Motnix multiplicotion
Mutiply (A, 6)
foree
sikken; kt+)
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
42
A2
A2
A A
At2
AuBt Boces
to, blodr of
e
rgued
e.
for cddit
2
NoiioDiSqitluw
By
S Re2
Bu- biu)
(
T (Aut A2) Ba
V=
V
C22 P+R- +U
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
elie
(A, i, )
9: patiion and
quicksort (a, i, 4-1) aj]> pivot)
4 (i<*j)
Swop (ol:), olj);
t+;
2 70 61 |9 J--;
5 7 34
34
J61 ua)
70 61) 16 |49| 19
25 7 3y 2 9|
3
aise st
16 19|Q5|34 7o| G!
29
61)
27|9| I4 | 19] 25| 34 | 70|
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
BestcaCasei 0(ulogn)
Gdcde eost:/rtoe
paatn algo
qeiksort dides
e oD
2 subaAay
o(n)
Tl): T+ T
loo
)
best
alo
hest
caU
D(uCogm)
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
Worst
Cue Ade pattsn. ba, auy
T): (n)s,
T")
T(): Mn-)+ (n-+
(n-bt)
T(u-2)
T(u),
TC- (u).
T(n-b) + T(4)+
T(u): pats such teaat. e part
teutaiu clenet and
othn utou elmh
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
twe
Worst cae e(u)
H
Beit care Blnon)g
e(og)
Question
lbwbtine
whoet patten
ivot
st
ssud
elent freent'
pivot ee
postene,
(wjG fe
.. favourable
Rstoaicy 2
|-2x
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
Suppose
ceoosee pivot.
hae N et we d
medion
Locatod at postba
elets
(= n-i)
H
(euter (ett gat)A)
uter
2
elnts ae
worst. cage,
side
Juasautud be
fe fon wors
T(:
=
e()
Select Problem
The efiye
A
Input.
|22|69 5 4
A
) :
SELECT A,
Miiua et 923is
:
SELECT (, {2) Medm
SELECT (A,n) Maximuum elmt
Naive soucton
Sort tine and netus ttre elnt
rouj
Divide
. Selet plvot
kind o! e
2. Pasttton awd t
Sallut elemt (erept
terat the
wgldo osJse
ST
Select a pivot
klk R
Patitn aund
|3|2]|4
Repeat in
pon volu
of
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
SELECT [A, P , )
aeturn, Ao;
Worst cae
+
T(n): T(n-i) t(n)
not a
go.
agonm
„T() + n
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
Binany Seorch
Given Sorted oy, seaneh ko
eleit.
(
Tetuu
vetsn -l;
(1
relwu wiid;
elie
Cae
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
ALGORITHMS
1 Bubble sort
for (i= 0; i<n-1; i+ +)
odjacent e tems.
()flje o.j< n-i-l; jtt)
alj+i])2
re Bt o
order
4l7>
|Swap (alj3,
52G12323=<i alj+]);
6
5 |2
suapping.
! busble S2 8|G 2
Sort
F4 ( suwopped faae)
breaej
Time complex
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
|2 Insertion
At teration lpas
}ro slcdduå
abeady SOrtd
aljai3: alj3
Lij;
Wost Cae
(n)0-3
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
GRAPHS
Ol)
ol)
Delets ed
Deue groph
in worrt, cae
thetare
3DFS trarers al
d
b d e 4
C
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
For
ere wde
dscovesyzido
e
2/3 dicoveny
tw nd
4/1
DFS on tte qraph s.
tth uode
Implbnuntag. DFS
DFS (G)
or each vertex
lozrgy,tt
vici
teduj tae
DES-VISIT (u);
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
DFS- IsT ()
balaced bolaned
not balaced
balaneed
oLO)
DFS trarer sal aloay prudce balaced paratheljs.
NO2
OIn amy depth at
exadly
tuo vetue,.
toditie
ne
The ud, u41
utervads
cutiely diient,
adeseudanitti othen in He deptl
teo tree.
st
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
Vestex ( e
I|12|5|
12
d
DFS Clossification
Eage
tres edge
y wdireted grapa
bac
DES
diiccted qrapa
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
enot ne
torest
Tree edges pat
ne
free nd
Bock edg edgi 'wtvl
deeleeudant to aueet
wcetor or lescedat
B
(/)
A (s|)
4/5)
D
h forward
edges fI
Chos
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
Forwaud edge
won
-ee ege () Such Hiato
NOTE
wmdirectid qiaph
In daptn frst search' ot
a tree edq hac
eithen etg
o oss
ed
cut vntex
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
deph fist search
<br>
A
irected groplh
(A}
always poducea -ee
DES th
tere Source A
vertieA providad nd SoubceB
B
)
Falue
Let
G be wdirected n
veices and
DFS foreat,
IMP
ghap
bach
beth iretd
well
direeked
atteart
One back edge
Two back
ateast tuso
edges?
veuoving Hat
reovig Loack
edge, both tHe
eeyeved
made
qclic lht
6e
ncote exoctly e
- 2 back
edge
BA nd
D4
Remaving b c)
ane pruet.
e Searet
Appiotn bep irst
Undietd qiaph
compomt) aticulatien powe,
Connected budg°,
33ybitonnected,, coponts
Directd
sio )
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
sort
stynay
DFS
<br>
Arplications of
the qropa ( drected e
wndireta d.) (
O
Findug topodogical sort DAG(Directd)
OCut vetex or aticuotuon
an e m
mdiretad ) Stved
Cut
Pcut
baidges ( wndiiectd)
A
yapa hae
tare a a
loack
Undirected
pubic. booleau, isaycel
meus belanLv1;
C=0; i<v; it+)
knat
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
besluan isyclic
public static
Cwnethy 'prelut
fn( cut i: 0; i< N, i++)
,4( visli] =z o)
tue)
diectd
Cetaie a
Cycle a
do i urtile
beslo
pofoming Statie
DES
trawen, Tutagn 3>, ad, intl] vs t1 asvis )
we
reocha
wod
such
it nigaleus
has bee
slil ste
n. tthe' DFS
(, ad, vis, dysve)
Stace.
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
y-)
In DAG, we
pain
Connectad
edirecked A DAG.
a
fnita: [sner]
200)
GATE 2A92
Viscad
Lct a utilehn rert
tere DFS
aud (u)
tst ti
cal
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
e DAG
2- Findi9Topolo qical sort
topolo qieal
sots
Topo Logucal
s0t alug exsts duectedi
qATE
Topolosical sot rdeing
20[4
PSR& SP R:
Kalns algoitha
o
vetex usth wdegee (u)
For derease
Töpo Sort
uisud,
3.
tt ued
felaun Ust
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
toposort (it N,
findTopo ,
fort( vis) odj, at);
nt d
o:
wtule llst. is Ewpty)
retu topo}
22 N)
void tindtoposort (int wode,s intl] vis, Amoylist <Ayyraut
staca KIutegus st)
via [wode) :l;
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
ventex
/Cut wudirectd.
:C)<Au, atiulaten
wlo se reuoval
DES
see od
wudiected 5
rap untuins
(
No
wot
poitrble foo
wdireted
gropl
aneetors
tototree
wone
ee stree
St
its
du cendawt
darcctly
amcesto. cnwtd to
en
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109 neet5
<br>
ld
tlhat92
back edge trom wstree
acestor x.
(3j
(aictiulaton pémt 4 nd
DES! tree,al ts deitedats swowd ionutijFae
ita eetors.
to
euy('vlo
-- Here 4 is actiuati
even i ih deucudant(3
wnwted t ceutor
san 'aticulotii pe
inancetoy
ducecat ua T
tsn, all. padths
fom to (sane eraple
dired patt X (D
exott
wode
Cutwcuat psat
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
BFS ( G, )
olv).
matnx
6ES
Suyteit
pau tol I.
Shotet
pantiae BeS e
ere be
pauaa
(ave be preueut
State
-Oot
4 243, 4, way at
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) be
@ https://x.com/Priyanshu__2109
Preit
Undinected
<br>
dueto Coss
Level
odd
5
odes ye.
acoss (ondinuoue) leel
Directed graph
bacle edge
tre
BFS
Undireetrd qiap
G= (V, E), Hu
Sppose ttat tere
Cetaue tee venies< VVy
{ail Tueu,
ene
al
preueut
distaue lvel
wot
(at
(eel
ordeuys
2 fo.le
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
BfST
A
qapa
veties ot
sae
Leved taeu wot
bipati
bipactle
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
alqohithms
Greedy oneut
he
Whaterer seeu lbest at
Comwit to oi ced
,
at a
tine
tpack,
loert
OOþtinal substructure
etaiwe optinal Selutous to
The
sulopolele,
be aived at yselcg
Local eptiuw
suortest Pcutta
2f
Sugi SOurce
s uort est, poeth awong þotcible patle
Find
worls to wmweiged graa ta for wegatd
BFS
betaue cLoes wwt
care abeut
gaphs
DLJ: KsTRA
2 DIJKSTRA (4, s):
HLtORI
al
THM
Suppore thotyeu
a bajiktra's
gapl stadg
Priovty
15
Cwwearet
2
2
ooj 2
I5 13-0
3
b23-0 52
22 29 l|-o
54
12
36-04
(90
47
The talgle priorty gue
hau been deltid rom
Wat
wd
y 2
l2-0 Paut
and -0-0
and 2-0
d
Rz92r 2
S013o 2S2
Y=(2-0 3 23
S6
7 5 7
79 19
ad
13-0
23-0 O3
UESTION |5
29-0
= 19
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
(74, #*)
l[+X 19
<br>
des st
ODijbsha
DAdd
Some weglt to male and
opply dijkstra
wore
adds wore
st
tiis wl to eey ed
Suorut becauuse
yatt
addng
wigt
>Total wt ot wok
beconn 200 Obikstra
-Ve
does wot wOYk wth
Dikstra
M T( aeueace lay)
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
Time complexy
+
+
Z
T(rewove-i)
be v).
deg(u)- T(teat)
daglvs) T(dureueylt
ragl) ol0)
daglug) T(ureau y) t(v-dglu,) ot)
Vydeglva). T(aereouay)
nAag (V)T(dicreanely)
Total T( decreare
luy).
T(aecreane luuy)
duglv)+
loq v:E
Elogv
DFS
as]:o ad
ofv+E) Tupetogay Sort the veutices G
ui taeu
w
Relar (uv)
2 eud tor
Vn deg (va)+)
thene
:.TU* o( v€) -V+6ls
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
BellawFord
Relax ace, edges V-ees
BELLMAN POAD
9 s)
a V
for eac
to lhec
ycde yrelen talse
etu true.
o(vE+E) - o(ve)
relax aten ae al
we hve
hore
atwot
totet
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
Connected,
A
Four of fu qaning
ndirected gcpl co
"cotAALi
Miim panng tree - The
grep
wthil iwnyn
2024
GATE trees en
matk Nuwleer e pocsible spannn
questin
(owplete grp
(G
MST M-2.
TTU e.
ud dota
TC o( E logE + (V+) E) stuctus
ued, yole
TC
Caube dutocd
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
B
&
2
4 2
ML) (6)
)
lo
MST
ith
DITKSTRA(G,s)
PRIMS (Gs)
Iikal'e P& to
all
vetices
vetice
U= EXTRACT- MIN )
=
EXTRACT -MINO)
wio
for each adjaceut v
e44iod tor each adjacutv
VEQ and
pareutjv]=u
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
Prin's Dijkstra
Rota algoitt' tae Gue vetex at a
tuue 2
elax
Man dustincin
Paim's : doseat vetx to tree ia closen.
Dijkstra i closest vertex tosouYce
Paim's adgot
3
witewt-3
clooe
5 Dijkctra cgor
4
A witH t
cuooe
Ainn's
aloithm tin complaxiy
Aajateney
wetrix
e
ycle aud
ti cye) Heu,
uot MST i,
4 e
SOme yele
be marimm
a |en MST ).
Cauging wigt decreased,
MST
MST stie MST.
(wcet
w
s wot MST
2 Edge
wet u
MST ad
MST
yle
propesty Based u
i cyde propet
with vale
This (a be one lay BFS DES.
popearty
4. MST lnereaed
,Cressuip
we
'NNow, hae 2
uyed
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
but
A way to ecode ttu chanactu
possilole.
free code
code A ode called pregix
Refix free
code uoter code
R- 2
T-3
E-6
are alsa
ulmau codes
pelit-4yee Lode
becCause ae
Leat
He chaactue ae ct e
INSERT 8,z)
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
Optiwnal Mexge
Taput Set
lotan a
We waut
slied hane les diutaice
Agol/s4AA33U1!
(et'S first
pck
H H
H
ot woe
dsei
wich
miiww
Pica wtuva tuat overtaps
utuval.
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
5
Ly
Iç
tertett opbial te vali L4)
(3)
wot
Aqo3
Pick the utval usth eariert kie twe
0the problena
vasiate
HWugatid Telenal seledug
H
Suduep to
winig leta latuen
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>
2.
3
The reseue u
be uied
lioal
a
atwost jelo at
tiue earibe
Jeos skaaudot vep
Sclied wth
Moziu
ha to lae ae before,
deadwe
SoYt
3 Stut and
(luian far)
epeat sep3 for
al
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109