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

Graphs Notes

This document discusses several graph algorithms including: 1. Breadth-first search (BFS) for finding the shortest path from a source node to other nodes in a graph. 2. Kahn's algorithm for topological sorting of a directed acyclic graph (DAG). It uses BFS to find a linear ordering of the nodes in a DAG. 3. Cycle detection using a depth-first search (DFS) approach to find cycles in a graph. 4. Disjoint-set data structure (DSU) for tracking connected components in a graph and finding the size of each component. It can be used to count the number of connected components.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Graphs Notes

This document discusses several graph algorithms including: 1. Breadth-first search (BFS) for finding the shortest path from a source node to other nodes in a graph. 2. Kahn's algorithm for topological sorting of a directed acyclic graph (DAG). It uses BFS to find a linear ordering of the nodes in a DAG. 3. Cycle detection using a depth-first search (DFS) approach to find cycles in a graph. 4. Disjoint-set data structure (DSU) for tracking connected components in a graph and finding the size of each component. It can be used to count the number of connected components.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

)BFS 6rodth ut Sarch

OE)
GRAPH 3) Ditha 7Sahustst
l
path aom
ius
Bhaicodekaro- Sarthak Pruthi

sou o

vod bs(vectosvectoy it 22ak, it v) ontve d


vecros dpol> vis(v, o);
MAmouaN

1o

whle4ephy)){ T-213 oid dikKsha(vecbr<vecto¥z


2-115
3- PP77 au, intv,intsc)! ?26) (32
os int abr. a[J)
1-E313 P - pe.vectbr <tr>, 5 a
i(visnbx) cenhquu 5-29H 65a5
Posh src,0 3);
sh(ob) 6-5 vectorLbool > Vislv,o) 7G35
vs[ooa-
vecox iat z dis(o, twTLMAxi short es t dushOnel
3 2 3 45 6
11 hemsou to ih wt
vectos ziyt pao) I1A outx ka püuNr
)PRIMSlgo fos MSTGza dy Algo das
iss-0; a(sa)-15
maiy toneckd wh cale. ans.
whilel Loemdpty
ord
oPamoua
10
PP -topt); Pypop)
int nod P- fiot, tost P.seondy
(vis fned)) confnu; dDSo uto
cuyinalu
Onp O+ l0410+IO+ vis[nod-y (nedede)
+343 or (Olto nbs: a n» )
tnt Psio(vecorévedtosz-pp72 Q4,|5 int oDnods nbx o t , obv(ost= nbs-Sltond;
int, ntsrc){ 6
NIS vistn vomod J) onhud,
Patosh-Rtue LpoNechov 07,On P0us{abraoda ,costtnbxiest 3);
17
sh({sre,o 3); linod, cos du[nyonodi) yduyfnod) gbesstA
whilel fpy- en )K dis[nbonodi) - du [qudi) tobY (ost
PrPPep(),p«Pp
vst.fuat)) ConRnk
paundCob nod) qoda
3
3 das vshy ConkQ dus-Oru hom
Onp T .second;
Conln tom u COn
o
1dlvslabs.}0)0.Push[fsbr-
nx-ietond3 Sd Hho pah iy mnimu.
To sd pakh Pmy Djiksha
vecios inl z pah mST 0Uu) Shostot pak
whdu (dest-)
A905{{avs, (ostefnby 3) posh{{ nbr, ces
Onpush-bok (dest ) ; cost otnbs 3)
dest pa[das),

Sa:o O
) DeS o(E) det 2
oe duptr unl ns-hn Shtol parh Cost:S

nodu u nor. Sx0


deyy:
vOd db(vecoxe vecoxéint77&,ntv Cost:4

m s e ouau

Cou& $Ttue ; vs[s)- 15 vIsT5; Isox une sve

fos (it nbs: a [ss) Hor (int nbs : O [svl)

(vnbe)) iJds la, vis,nbi)-:0) un0,


d js(on,v, oba); uc put.
vis ls ; IlSouxu h0 US ied a
o423
0-00 3-bad(Sr Chld noduy w/o uc

5 TogoloRal Sosin 7
pusw inko my or u n

3 CoLOUR IN ALCORITHM>OE)O fopoSot(vechoxd veclox ant> 20, intv)


Topoloal sostinaa vector bhd> vslv,o);
ps(int c:O; lLV; l{t)
94 cyela
ds(an, vis,i)}
boo dsl vectos vecor éint>7 &u
vcor unt z A vis, int ssC)
Nuuhu isa
J s s ) 2) kukum l no ycla, s c
h a s Completd

is(vistsrc):-) xhun O ll yl Src hay not


se (o.hegun , a
nd 0);| Topole al
l complekd Hs cab you HSikdit again lut d: OCoud zzilen . nSo-tt
Bhaicodekaro- Sarthak Pruthi
) Cucle DekeHon Coduuckd uaph
boo d( vecloxévedey tvub> kaxn, vegho vis, ntsc
4 5 23 1O
vs[svC): 1; int pa

visfd:0l2 iJCvistob))
0 unvisitd
4ldjs lor,vis,obs,src))Hukum i
1Cuelu
else 14l nbs l=pax) u n
he nbs u viskd &not th
Bes (KAHN'S ALGO) - OE)

Toglel sortig 3

a o t p a i )

u Visite
-32)
oxl int: t) indga [)+1 -olE
5-0 40 Plood
3 0-2 2-)
3 - 1-2
intdx[u): { o , 0 , 1 , -1S 0)

d[4): A, O, o?
YCindg Ti3o)push(1); GHT at
whiu tq empy -+d[13; JtdK)
tntw:4hont); eP),
n5 int
dx[3){001, \ , ,S
Ond posh -bac(M); t dy 3-{\,0.9l l 3
Jes lint t. out[ ) set nton
o-1
DSU- DIsjoint
inda)-i -Union Hnd
indaj-0) Qosh (), 12|
Uses ycle Dekehon utd
3
3 Kual -> Shott 0aHh Ago
Numbet Cennctd tompon
(omponUnd
9Nunh e nodl n a

Si2e

Bhaicodekaro- Sarthak Pruthi


clos DSUL
pubic: y3
Componunh 3
nt
0; no.outw, Ardparullw) -- padgouat
vector inlz Qank; / uHL would b y
vettts ntz Size
e wwt
DSU tnt n) eseuk (
thus2n 0; 4No. oconutd Compon
Jos lint i-0; izo;itt) dsu. n
O push-vack lE );
43 N.onodu in eonuktd comont

Sna uh-boude 1),. dso-s1 ndpatat(«)


Stahc Goaph Dynamc Gaph
DES DS DESLDSU
tat Jind poud ( tnt ) oL) oltloj ) olge olFle
3 45
oo 23 45

uun pdl)- indpou pau[)); 78 9 o 1

7Pah Compasion 1213 4 Is

18 1 2 2 2223
Bon
u s
no.Cownd
Void i l int w, inta

int P hadgaud(u), Py3:fdpaan 9) 9 Eule Path k Ciit


(exP) uun;

i}(51%Ley) Siu Lex)) swaplpx,f3 Eul Poth visít edge enlxone


9 ww to mae Py pa of PX LeP
must han DM nody
skaxt end a diff. kx
s o êt
Sta end a S im ue
poa[px)=P2i
ukx odd dagul
Si4[Py+ siu[Px); OEP- oay
cnUnt wh e eVec ol eun dous
to
DEP
0 -0
dd
3;
Bhaicodekaro- Sarthak Pruthi
o s oll node
indaa loukd So 4o dekwt -ve uwt ycda ,

nag Outdo ol
22 4 paM> pahtu +ust)
1
nodth ndau=oudn 4v xhuu l egcle o-
pM[y* 9am[Jtwt; u
, 0s

n odiindp oukdgntl V Hm m
3
updak hojona
DeC indaM Duhdgu. 0 0 - I ho but ek ov

bad nogmaluse huoQ


| r i e thex y - wt yulu.

o) KRUSKAL PMST
ellman forod
SElo V Djiksho
UsiRDS0 m o
EV
Elo
be Connuckd| wt
class DsuU ca handle -t w, Canhande -*

Cot dett R ucle con dtRr - ele


--(8)
oo eda LoLJ
itFauskalvectosvectorzintz7 ax)
3
Sost(aso gtat) axr .endt), tmp);/s n
weiqhs O-4 shortst pa-
DS dsv(Oiakd) ydounsf veshcu we'l get b 4 n 4

tHDHons o
would be
fos(ouho it:0) Se or veattx (v-)
v-i 0
Pom le8h cot be a t Hhon

4 (fidpatt Li to)1: dpavad( HCU))Ncc uRCont go in cle (eez »t ns-ve oncla)


fox ut.ffaduny un loep on edgtd
butlits), itC);
Ccle, os done i ohove .

hun ;
2) floy de Waha
poth
Seuwes shuta pa A po shotest
) Bellmon ford Single
ovx¬)vcbadoataN

or lnt e0; iLet) hel f hu

(ea Tuj-: INt-Mx) toninud


Pa Lmintpam[v, pututwt);
Bhaicodekaro- Sarthak Pruthi
NoR
Opton-Polt, fom 9-4
cle bu
Sto aonntced
cmon êr

o ist ess1ble
miy imom
3 - 5se wdpo
wsan e goth A so ek speeikc ES

(1 mlQ9, Q+t9-)
2,3.4.6.1.Q10, I,)3)

-9 (3-1-0-2) Kosa04u He,

o tnt k: O; K<V; KtT)


Hg2 Re t aph
Hu oph on

u K a d Stak elunenks.

fox Lint J 0 jiV; jtt) &s) f


votd d( o , vis,Sse, gtodkct>
visisrcJ

Gex Coti- mn(o[iJC) ti C).onuikig]) usnbo)


,
ds(a,vis, nbss)
3 Siealar o topolotta
S.pushlsre); l|
odi,vdia oj d4ptad
5
3 3
kosaau a , tut v)
3 Shongty Coomechd omponenls int
&
vecarhol vis(y,
y uachobl fom wtuy o
Evouy vbkx (tov)
i,S); 3
vbukx
'
H(vist]--o) dslo,Uis,
DucHod
Unduuckd
Cio-v) n b : a ) & e v[obrj.p ush.bauk(t):
LShong Comntckd lop.
LConnettd componeb) pr (nt
ho sol pyoblem ed
int : 0 ,

- o Auvis.bynl),vis.endD,D}
Stockcnt z novse;

Com.
3-5 we tCoa osk Bhaicodekaro- Sarthak Pruthi
whik!s embhd)) { s) Axtulakon Point (TARIAn's ALGo
int xS top); s-top);

1(vsCxJ:o) Undachd
Potmoued duS(ennecks
d srev,vis,X ,nuuse);

Cagnbo)
nbs pax (s)) contnue
Onp0
Urwis
visikd l dasnbx)!-)
arstt 3,10,11,12 low[s) i w (src), Coahnue
dsl) vis1kd
dusnb]3);
13 else

djsdsikd
ajslnv);
ouwer-min (to (or), los(nbl);
tt 43,1
dsts)vsikd distovt lou
we need to fnd he
olo
ordtT 5,81,
zene -0lto

oCvE)+o(vt¬) olvtE) Lott to detc

Soat cell nodes nosdux ot hshi hi 34 backedg


w e vse kmesk0np
Sin. to oqosoat 414 ho mak nedes
uwnth inlseaoi uay
DfS e to uush time e do h i ba

4) moth Vttt
assin dscoo tine.
S w e neRd to mounl
6185
veukces h a can lLd au possible ncde
accAsible
eauest
ve aenh ou
whuch w ill
OnteseskoY lo or en node
tndacak if we hae backed
Skp kosaNa
Case
vod dis-) vestx edge
Souc
Nous, s
MOR a n one
St pushlcrd; childn than if
3 2or mo
Amotd
dnudy a p n iako
un dist4 0Hulokon
pont
thu itl>
all ge b ,

oph
VisRd
you
tunk os Comple k
NoN AP X
would qi
else ho sudh
ndy chld >1
vekx AP: 2,3
Th's uhan-
b u t tw AP
ou
No:hu Cont be a
s k bw|
SD chldrn tedat Cas On
hain so }op pe stoatina nods hogi jisne
baa t pauchne h K ox Sou vety
3
vector int7 dise, louo, paN, ap Nohe
void ds( , tn} sxe) S h o t a ot Alera Brs edae count
A B a ( t nedu
Shahe t Hme:O: OvUall
tim
Elog
dus (s lous[src) Hne tt; Pua mumise Bellmon ford
inl chuld:0; Exu)
9floud W a s h a

orCint nb: 0A Tssc))

Diyhe ,Gellnan fovd, fleyd


lnbe gon[Src))tonhnue:*
Jdase[nbs) - 1) 1 visitd r Un datsckd

low[sT: nin( louw [s9, dks e(nb«J)


e se
c'hildtt
bidg nove
dis( ox, abr); L a t edga
jisko
Kane se we get
louw[src): nin( touw[),laufnbd),
ouyf compon enh
) / sowq
-1 and chuld
4(po [src) vtRx
aps
low[nb]-dusc[so)
ond
4(pax(sr) 1 - i bad hi only h n
nby mw
// mu haki saAn
apsrcJ;
3 KmPyaN ie ed
on m Hhus , vn my may s C s * )
lonn cch
A h dus se bi r u

uska haal tale


int main ) ap.sts«ly, o);
p.resial v,-D;
dusc-&8iul v-9; low.siu(u -D; muu h a

ttbid
0 fuy.gush h t ( )

d Ss aD);
izv, i tt) oxhulakton

snt c:0; ball


f p t t ) ) couteiu"
flap[i))
Coutefzzt
.

"") polat inaph

Bhaicodekaro- Sarthak Pruthi

You might also like