0% found this document useful (0 votes)
6 views

Algorithms!

Uploaded by

ptrial1900
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Algorithms!

Uploaded by

ptrial1900
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 107

<br>

was meve ment to tuou into this l

Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>

Why alao nittawm ae iport aWtt


?sRAr le nsiNI
wmay
H
they tee loiid

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

cowut for each


di
ficutt und
contu g Hhe

Thercfore, we
Se
ext

improved version et the stp Coint

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 o(9n1)i ttheeeist conett aud

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

|2- Big Omega 2


Notatiano ao-prd

T(n) teeefv 'exet

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.

&ues tion - and


2
tiu)
rot

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) uboy (u)6 uo'Go

(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,

bog logn < ogn <


(log)K2gwo

4. Little -oh ò Notation

T(n)
uy cs teut C
>0 ad

So terat for

thene exist (onitauct


Such tat
vela to

for a
Conitouts c>o

Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>

Nototion

cog(n) for conetat


Tln)
+aat tor
Suc

pol
gCu) o(T(n1)

Incom pai biltey


oscilde
siuee

-) ad
no vale for

-TIu) 2gca)

hopey tias es ay mptsto nototi

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

loe prored taat

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>

ene elet funcuong bct


with conmp n

wattemetieol
Cnweenee
o(u)

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

flu) > g (n)


(gm))

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>

Analyging lop complezity


Importat Suwwwation for mlae
n(M+)
1) l+2+3t 2

n(u+) (2ut)
42+ 3+ Lall,)

Hawo nic ses


3)

logi t +logn
logl+log2+

for( iz 2; i<zn; iei?)

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>

dor =n; i=2,; )

oLog log,n)
o( og,2og

TC
(

3 Teiaplat
T(= O(
a)

TC= o(log logu)

Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>

iceN; it+) for (i=l; i<= N; it+)


or(il;
tor ( i: 1; J<eN; j=jt1)
9
becoe Ruis

Sut+
0(N) nne

for (i=l ; i<= N; it+)o 4or ( i= ); i<N) it+ )

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)

(8) flj=v jcoij Jt) – ot)

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

venal log log. NwgN

for (i: 2; iK:n; i++)


(x7.C - so) worst Care
4
brea' bet Case
Suntti

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

wort Care atgoita


Haat ayo
wthmi
best co
hent ca be

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

HIteration mothod Repeted Sostit utn metod

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

(u)1: +(1)1 t.2) tE (u-

(<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.")

Since bae wot attn

T(n) T
2) T
)t
12

2u
3

Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>

T(n): T(3)+ T()+


2

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

Tln): aT)+in) whee

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

(6) i! p<o (*).


T

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>

Recusseeee et selvalole Mastu Teo rem


n
T(n)
=
Jm toT)+
i not contt

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!

T(n)e TI)t 2T (a)+


b
a
ad

Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>

hamae ef vasuiasle

=
T(n)

wetud.

let n:2. Ti)Tajr

T[2"):
T()
Let
-(
s(): s(w)+

le. T(") B()

Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>

+
(<}m)s

()@(w)1

()L. (u) + Bo bon u


w
bon

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]

+
)

(w)s +(zm)s an1

aT3 (10

(6]5 bon) bon (

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

Hue corect poibn Suwp ( ak-1, alk]);


1(

(estion ort
mohod).

4. SoTt (oje
Merge

mergesort (a,i, j)d merge (a, e, mmit, j)

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>

towpani ioms beng dee


Nmber S0at

case (mtn-) ommpaione dene


worst
win (m,n) cowpaui sena
best Case

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

sort talee soYt


Hw may conpaniime doe 'mg te
N peer

42
N
eleweuds

u worst case sceMaD,


soyt
A+

BBBB AAAA BLBB


A AAA

AAAAAAANBLBBL888

to merge

Laut Rs Bs Ce

Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>

Iterotire merge Sort

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

Prho2 9ld ot2


S
Method 5
cuo an bettoym g surt )
Pair wiseweng P

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

Phoperties sorting algohithms

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

Merge sort exto spoe


bTime re
aloout
dot Care
stack daptn .
o(u). wel teursi
ie
ext
sUr pae

wot
lyut
Mer ge sot staloe
but

Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>

MaimuM Omd inimum


e Nuwbers
Niwiium
maxfajo] mi= ao]:
Sluewed
Tburnauwent Y(alil wax)

ail;

wetlod (Balaneed unamt)


Metuod 2: Tournamet

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

maninuu and second waxivmwm in


ind
> ali1)
i alol

Second nax= al;


ele
=
seuondwax

Y(al°l > wax)4


&m-) compauitna.
secondax 2u-3 compaet
ali);

best Case
| t
n
-2 n-l cowpawto

s
compauio
2n-3,
l+ 2(n -2)

tlod
Method i
touat
be

olosenvtin 2uc tagt


s
2nd, im
max

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>

Method 3: Tournawent wetod

paunoie
elets pain plad
( eleweut Grecteu, elewut
elewtGwalinélemeut
Hinimw fow lst
utl
Maiw

uo
Tbtal

wax aund wiw ane


n and 2compawte
kound a -1) elenet
(at elweut wth
ae dee

tour namet
72.

Sum mahy

n +Log, n - 2
No
compauisons regyu'red to
langet & second angest eleet
No e companisome equired to find

aud snmallest elmt -2


Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>

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

Sues mverse terat 6


elwt
Langt po ssible (nbe,
uhove;
l5

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>

Brude toree mhad 2V912Y9vni


Cbwpare evey pain e eleuente

Covpieity

Dsng Merge sot ( biide aud (oge)


tme

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

Ftnd t o(u) tine

dereey
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>

second uoel and


Hhe
Rerete
mergo procedure:

wvers ierns
Total

3hor

ever aked

Closest fai w
2D

. -
(0
6

l3
7

tee miiuw distce


(xpod 2
pointo Au3

Input i pots th <y) COoYdenater


miuin di fauee.

dutueeb ay pair

for poute
Or d

te poiut
divide eund.conquen

ww
SL Sp, )

Priyanshu (GATE 2024 AIR ognt mogntn nojn


T) 384) @ https://x.com/Priyanshu__2109
TC 379, GATE 2023 AIR
<br>

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 ;

etun elne! Yelt x***a)

Motnix multiplicotion
Mutiply (A, 6)

foree
sikken; kt+)

Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>

Divide and congu pra

42
A2
A2
A A
At2

AuBt Boces

to, blodr of

e
rgued
e.
for cddit

2
NoiioDiSqitluw

By

P:= (Ay+ A2) (Bu


(À21 + Ag2) Bin
t Bz) Cu
Pt -T +V

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>

auick sort innpteus


Roare
pivot ajol
quick sort (a, i, j)
c4Ci==j) wtileli<} aud
ati) <: pivot)

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

Sunp (oltons), ý),


61, 49 retu
16 19 ] 25 34,10
l0
? 29

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),

styucte ef w0rst caye


leuea

TC- (u).

&! loest care


4eueral sthucue
uto 2
pouta aueh at

structuu e wov«t Cane

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

Worst care tin

2
elnts ae
worst. cage,
side
Juasautud be

fe fon wors

T(:
=
e()

Select Problem
The efiye
A

Input.
|22|69 5 4
A

Quctput Salte Select (A, k): Swallest eleet


sEECT (A, 8)
SELECT|A,):)
2
SELECTe,
2023 AIR (A,3):
y
SELET
Priyanshu (GATE 2024 AIR 379, GATE 384) @ https://x.com/Priyanshu__2109
<br>

) :
SELECT A,
Miiua et 923is
:
SELECT (, {2) Medm
SELECT (A,n) Maximuum elmt

The partutio method W


ick sot etu
kt Swmallesteleeitw
ewet
E:
padtion: etk, pvot e

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;

pandiion (A, p,4)

SELECT (A, p,m-l, k)

yetuvn sELECT A, mtl, V

Worst cae
+
T(n): T(n-i) t(n)
not a
go.
agonm

Thi probleu be soed

„T() + n

Peuit to vewenber à sELE() prolbleu


soed complt

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

Atn bsA, wid+l, , y


)

Best caie 9l) Tln) J.

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

beole wapped taue


elneut j< n-i-'; jt+)
at coneet glj=o;
posttbn
52|]e]2 Jwapped
tuap (alj]olj4);

F4 ( suwopped faae)
breaej
Time complex

HBeat care e(u)

HWost Caue - Tnplace soxfing


(n'). algontaub YESI
ae
?
Stasle YES]

Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>

|2 Insertion
At teration lpas
}ro slcdduå
abeady SOrtd

i1, ikn-); t+)


iforl
J i-)

aljai3: alj3
Lij;

Time conapLt Bin

Wost Cae

(n)0-3

Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>

GRAPHS

Adioey ist Adjaeuey matix


feration
e (v+e) 6(v')
o(1+ deg(u))
Test
elv)
Lo List v'5

List al cages (v+ E)

Ol)
ol)
Delets ed

Deue groph
in worrt, cae
thetare

Deptn First Search

3DFS trarers al

d
b d e 4
C

Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>

Stat tine tue kirst tuie


Lot

For
ere wde

Depta first tree

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- VIsiT (u)


visited u] : ue;
tor( eaci viadjoiet to u)-.

DFS- IsT ()

DFS Pasen tthecis Tieorew

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

wal Lud, uf) eotaud


theten val tthe
wti tree
deptl
fit
coutaied etiely
twetlin iudnval [ud, f)
dcendt

Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>

pues dala .dorectd


qtven tt
necone truct
toleasg tree

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

For ward edge- edAg wlich wt ree


ro aucestor decendart

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

dlx] < dly] < 1y]< tx].

O Backuward edg a'on tee edgeo(4 faat

a tuee ecye (uey such tuat


L2and
e
YO

NOTE
wmdirectid qiaph
In daptn frst search' ot
a tree edq hac
eithen etg

o oss
ed

In DPT undire ctd gape,


fere More

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

a) Au ts DFS oret (for tauersals


at True
stating ittt trees
tin
have Sa

b) AU DFS will hove


tree edge sae Trùe
Sawe and
back
uwbea dge.
Tree edge
+
Bace edge

DFS foreat,

IMP
ghap

bach

beth iretd
well
direeked

ue caw lad to utple cyclee


back
Cateast
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
hach ed
<br>

atteart
One back edge

Two back
ateast tuso
edges?

groph co tainn exaey e back edg Hae

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

qcle i (check Foyyle le, -), is, adj)


DFS
tranersal we

Such teuat ue Pusiie boolean checkforcydel t wo di, wt


beeau visl 1, aylist <Arrayi st <pteqn aa)
aich -lU vis
[nodltrue,
adj getl nodi))
paneut
2ud,eCIntegu t:
ahendy 4 (vislt l==falie)
visit d
ady))
okfaba(cleclu for Cycte (a, wode, vis,
yetu
tue
elie 4lit |: pareutE)

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.

sgst yeto, true,

For undirecled grop


yele
wocey o clech

Pos directd grfl

Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>

Directad acydec qrapl


direc qupa ulieh dee ot
cotaun a

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

afu]< a[v] Direetud


acyelie goph
B
afu) < {[v]
oqor

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,

stack <Inkqea > 'st StacaIiteqs();


wtl] vis =
mew wut
[N1: 310

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;

lbnkirdto posovt (it, us, adfi t);

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

(9Z taeyt cauit be


Cross

wot
poitrble foo
wdireted
gropl

t dsptl hust ee, tthe tet


tt Comenecd

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>

Theore tenna woce X

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

tree pick Leat


Rs PS Mae
Maue cpittt
DES hut
uill e
fee

wode
Cutwcuat psat

Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>

BFS ( G, )

Breadth First Searc visted lu]: tbue,


add lv, a)
iu wot
TC wilel Queueu
delet (&)
ut eal adoent x
to u

olv).
matnx

BFS or Suort est patin

BFSJoalusays giwe; hortet patl trona ouree vet


to

6ES

Suyteit
pau tol I.
Shotet

pantiae BeS e

wkbeut wsteul wodes

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>

Suppose tat egeotore


Bes and that ne.
Taen,

BfST

A
qapa

Undirecked Jraph 218


CYOSS ipatt 8-pea.

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

PtLAxu, ,w) Iucialy pronty ee &


to al vtiet
seile
Ca wot eupt
EX
TRACT- MIN )
Pareut [v]=u.

tor eac adjoceut


232
RELAX (u, v, w)
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>

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

uonty Quw Pat


'-=.0 aud 12 12.

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

Tma comp lexity Diktra

¢E. T deurease -key) + VTYeuove win)


ia ed as PS,
(O1orelar'ed9ee.aost
o(log v)

M T( aeueace lay)

tor all vecter

Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>

Time complexy
+

+
Z
T(rewove-i)

be v).

Nsq V t. ucvvEadi(a) T( decreaue


lay)
Adjacewy st

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

for adijacewey waatrin


t aap
olE+y)ogv)
a
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
TC,olEvlosvy)
f8,
<br>

Shortest potn Directid Auyie qrepu (DAG)


Pck vetices epotogical orde and velar

ast. ad dalile yelo ugt

DFS
as]:o ad
ofv+E) Tupetogay Sort the veutices G

ui taeu
w

For eacth vet


Suytrd order,

Relar (uv)

2 eud tor
Vn deg (va)+)

thene
:.TU* o( v€) -V+6ls

|2 Bell man Ford Aqorntt


path algot
Relaxer edgeu oYden (v) tie
veti
-ve wel

Based lath Relaxati o


relax wde
elaratien) theu
iutixed ete We

Serotest þoth cost

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)

Dijk stra witth beap Tl- o(E+v) togv)


Bella ford o(ve) Belmau ford

Ady. of ford Dijkcta


belmo
-Ve edge.

relax aten ae al
we hve
hore
atwot
totet
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>

MinimunM sponning Tree


tree e uwdirectad qape a
connetd sudoaph wleh
cotaie tere vetie hae o
yle,r

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.

Tuwo hop entiel o MST - The


The smalest
ut
MST

3 Kruskal aigoriHm (yeedy)


o(Elogt)
8bs Sort edges
T=
each
sotedorder

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>

4 Prims aloittosl oge UNviNIM


Tale testt reglt edg
o
aluwys pant MST

Greedy singli tce and qeedly odd


tte skuOtest edge that could thee

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,

mut be port o MST False


n some
oe ma
bemaxm wt
min in
wax .
in wot

uot MST i,
4 e
SOme yele
be marimm

Suppose tte The, the


8ortet blw veties met
2 be faut St MST
Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
Palie
<br>

a |en MST ).
Cauging wigt decreased,
MST
MST stie MST.
(wcet
w
s wot MST
2 Edge

Coet MST esti MST

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

o(v+E) + O(E) covuponets.


Cowmected find betlh ttie
Cowponututs iang DPS/ BPs.

Now, tud te erat


win.d (ownet tuee cavnponeuts

uyed

the tovngat codecuá cold


be?
og
Codewod be togth
n wcth n2
Cund fwo

Priyanshu (GATE 2024 AIR 379, GATE 2023 AIR 384) @ https://x.com/Priyanshu__2109
<br>

man encodingge daraptst e)eucoes

but
A way to ecode ttu chanactu
possilole.

free code
code A ode called pregix
Refix free
code uoter code

coding gReedily builda Bubtree


kema

kon $TER EoTELMET:ER


j100

R- 2

T-3
E-6
are alsa
ulmau codes
pelit-4yee Lode
becCause ae
Leat
He chaactue ae ct e

below a cuaata). HUFFMAN (c)


tuee 2 a8 cuut
Priorty gue
8= C
tor C=l to n-l
-
X= EX1RA CT
MIN)
y: ExTRA CT - MN(Q)
togn

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

Irterval scheduing. Prslole


-,
Input Set ot ctvals
Butput -Seloset wtevals t

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


tertett opbial te vali L4)

(3)
wot

Aqo3
Pick the utval usth eariert kie twe

Huuoose the wtval- x Stas lat diicard


x

clases tat ceuit tth cnd reurte


al
the wtval
X
aat
witlh
uss x
rst, dieca

all lasses fehat oufit aud veuirse

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>

Jeb Seheduling. witlu Deadlimes


Deadlie Paorit
5
Eah Jeb taees 1
wit
2

25 scladule al elos uch Hat


20 get aew
5

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

You might also like