0% found this document useful (0 votes)
8 views25 pages

AGT Lab File

The document outlines various implementations of graph representations, including adjacency matrices and lists for different types of graphs such as directed, undirected, weighted, and unweighted graphs. It discusses algorithms for constructing these representations, their computational complexities, and special features of each method. Additionally, it includes problem statements for assignments related to graph theory and their implementations.

Uploaded by

Shirshak Ghosh
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)
8 views25 pages

AGT Lab File

The document outlines various implementations of graph representations, including adjacency matrices and lists for different types of graphs such as directed, undirected, weighted, and unweighted graphs. It discusses algorithms for constructing these representations, their computational complexities, and special features of each method. Additionally, it includes problem statements for assignments related to graph theory and their implementations.

Uploaded by

Shirshak Ghosh
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/ 25

fage No, - 1

Pase
AssIGNMENT NO: 1 Dalo. o2.1) .22

Pseoalem stalement
Matrie

a) Jmpltmentalin ot undiecled anseigted graph


b) Implemaalim ot undoelos
e) Dmplenenat
) Smplemantatin o doutlod onigli aph
Dict Diseusion i
Matti to
Sepresent
maria is wyiat a 20 44are matria of nxn, ohere
nis the munaer ot rodes in that
Colnns et the mait epesento thenodes qoaph.lhe gous and
Ohder. The vale of a cell io in o speeitie
the edge tson the node of aepresentad a he noeiglt
oolmn mumber. he valeJo aMnben to the noie
ean be in the ca

palel hoop in the apl.


Dutline ot the adgonithdoxt ;
ut usuppose tha a
Lek ot vetes and E s graph G1(v,E) where v
the set of
blow
AwiAed into toh difforent edges.The problenm
support, whieh
To sepre,ent
wumloer of verties, ve
Page NO. - 2

nxm matia A.fael oow and eolmn oU sopre


sent a spe etfie veten ,uek s
Oeprevent ventex i .Eoel edgee is dmplemonodto the
matix as a salue in the eell of a speife sow and
column
Hoe,the fous types ot qraph and their implomala lon
deserileet
i) Undireetad,
wnlieoted wnseigllad raphyan
e thom ventax i to verlexj is implemeod n
te matrit lay plaeing. 1 in the posttion uj aud j,i
t the mathiisinee the e
i) Undieted, aeiglted qap:
edge e
fsuem vertax to ventea jis ünplamented n he mati
placint position ij andj
Hhe matit, <inee the edge i bidielional and eigt
ed

fusm Veai to ventea j is


mplemened un tae matria
edqe into posiim i,j ton the
disuctid ehge foom verteaito in the
gapl.
fage NO. - 3
Alqosithm at aglanee ;
Input: Aqaapk oGr (v,E) nd type of Goaph .
Output: Aliaesuey matnis of the gaph.
Assumplons Vis the set of vertices
E is the gel ot
edges
cdge inRoomal lu;) ohere
A io a mahia o NxV, filed
w holds Nalue of an ithO
Bor eaeh edge in E elge
ndieetod weigte
ALi,1: 1
wndiected noeigetid
ebe apa sdiraeld uaweigtud
A[ii) 1

Tet Rn
Page NO, - 4
2) Uadiüee tad Weigltod goaph
a

s) Dineetad Onweiglted

4) Direeted Weighad graph :

Da stsuetue wel:
In tthis algositm
the matin , ohere n is eoay nins used to
the mhe of vextices in
Computatim Compleily i
The adjaeoney matia tales ov) tne to
ee eaeh oeain of the implemet
woih orne value.
yare maie vxv s initialael
The spaee eomgleg is O(v)
Speeial Featues:
) Eay to mplement, manipelale and deeole.
2) Undüheelal
Undieeted qaph ereae symmetie matix
Page N0,- 5
) e pare mathit

4)
<dac nd self loop.
Starehing valvalueue olol aa chas takes oli) int
Page NO-6
ASSIGNMENT NO: 2 Dalo, 02.11.22

Psoslom Statement
Iumplementatim ot apa uingintdouee mais

) Tgementa tion of wndireeta wnigted gap


1) implmentatin of tuetd ueited ap
) Smglemtuiation ot Aiseted
bsiet isewsin woeiyad grop
Mati the ways to seprpt
epentatin is onee efof ttue
hagh. In the ineidenee máthie sepreentain the
aph is ephesenad tay aa mxn mai, n tue no o
mode im the gaph and
mnpsento an

node in the qragh.Eaeh eolumn has tuso non2ho


(salues sichepusent the eonneead nodes of the
qsapa. An edqe e eonneting a and b mode ea ke
upresentd as ALae? 1 and Cb,e 1 . u case
the weigt ot the adgeuCase of dieoted edge,te
vame.Ther m t 2non geo waey in eaeh
eoumn to eppnt the edge.

Lut us uppose that a ghaph


Hae et ot vetiees blu,E
., åided into and £ is the set of
four ditterent subpasto edes.Tue Problem
uhieh is di
To supheynl a qhaph wiknben ot vetires and
ksll apheyenid ey the toswesprdig colunn and the
Sent the edaes eonneetod uoith the vert
tim as åeeibd.

matho ky placing 1unin the poaition , end je


et thematti.

fm vein i to j uio tmplemented inthe matia by


pacing the weiglt ct the edg into theoeatn ,z ad
,4 t the ineidenea mti.
1) direeted, Unuseigt gapa:
VænE j i impemenad in the
Joeaim i,e and -1 into loeationmai
matis placint
j,eot the ineien1ito
ee

.; to vetta jis implemen


aight oB zåge into droation uni,etheandmatiz ky pacig
(salue into loeaion i,e ot the negatwe cf tiat
netenee mati
wigkt b A
n Teyt
e]2-w ALj end
ATije?
weigted
ALie]el
oeighed
edge anweiolgt wholds
usith
0 )kilod matia VxE a A
edqes
gaph.
Assumplin
imV
s'.
the ofmathDneidonee
i
and (v,¬) graph
G Input!A
glaneei Algositm
aat
Page No,-9
)Undicetok Weighled aph i
A(S
A

11) Dineoted etdutel gauph '


a

oeighted D
cla) S

D(3)
Data stucture used:

stose the algonin Ia mm 20. axNÓN sa9as


matux, ohore nis
wsed to
ume vontiees nd m
s the munber of
edqes of the apl.
Computatinal tompleitiy i
The ineidenee matia tales olmm)
to imploment, and Dln anont ot time
the mai m)&pace compleity o to gtohe
Speial featues;
) Eay to mplement
manpulate and doeorde.
2) faeh son epresento the
edge cornected to.it
)Eaeh eolumn gtoes tuoo mon 2er0
elomt
PageN0, -10

T) Seareing wae taees olm) imes tor speeitie nolns

inding edqes tenneeted to a verte talus tine wobene


ASS 1GNMEN NO:

Psoblem Statenen:

Lmplementalin ot dietd weiglil o


Buit is euion -
|iat aepreenta lion ocne od the woay to sapssst
qap. Tae giaph presenlad bt djnceny ist is a 4D
.in the gnaph . Eaeh posiim sepresent an unie nde
et the qph and eaeh poition halds the addhenoa i d
Lit ushieh has member ef olement, eyal to that paricude
node's immidiate eiildon. Eaeh inkod it can have
Iteron iee due to the eoneetion in that
mot lee unifon tor al nodes. n ease of dirotod
oy ae eamable nodes ae ineled ntt inted list.
Tue wweight ot an edge is tosel oilin thed ist,Tue
waig et an io toed sithin lined ist s the
woeigut et edge that eonmeets tue patieulanode eotta
ib pesitionas node in asoray.Tuis Cau be aieple,ened
0#line ef the
t ws uppase thot a qraph G (y,E), wohere v
boellom is divided into tow ailferenl subpoto oias
n diseieed below.
To heprenu meune otveiees,
Page NO,-I2
We heed a elomat
neloont ray A. Eaeh elonmot in that
wsill hold a inked lit wshioh l hawe elonent euo
to the mumer Lmmodiala ellren of 4he mode. The
n tia pa
paee cán eaeh elomOt im that a t
sell toli a inked i t , ohieh uoill haue elomonto eualL to
the mmber of immediate eilon of be mode
wil a an eotha space in eaeh e lement n lined
dit based on the ueight ot the edge
and their dnplemin
tation in diseihed
) Undieeted Unuweigted gaapl :
In ease e undireeted wneighted gal an edge
e fsuom vertee i to vetex i is mplementad to the
by alkina linked list element at the end ef te postion
I 4 h matie.The einkod iit eloment at the end ol
Ahe postin i ot the matia . The linked lit node uoie
Contain the eae and viee Versa

h ease t undieelad eigted qaph yan


(2ole won vehlex i to worte j is
a y eyy adding a ined list node imolaments to the
at te end ef positio
stot
win he node and ae
Lned t hode , and Niee versa
u) Direeted Unseigted gap
Ju ease ot diected wweighed grap an edqe
yom vete to vertoa }j is implemented to the aar
by adding a listt node al enl ot poeilin i ef the aay
Condaining i in the node
i) Diuetad Weigte gap
n ease of diieled weiguted goaphs an edge
eith alue w oom yeter i to wsl ae
umplementod
Page o. -13
adding a node uwith i and wcinnode ound yalue ot
to po.sition i of the
ay.
Algoaithm ad aglanee i
Jnput. Aahph Gt(v,E) nd bee et the qaph
Output: Adjaceney it ot the giap.
Asywmptims ".
V is he et of vetieos

A a Velmental mati
Wis weigut of an
for kaoh edge n E
gprapl is ndieotd tunweigltd

node2i
Conneet mode 1 woith AC:1
Conneel mode 2 weith AL1
ese is undiieeled wseigltd
modo 4 w>
hode 2 Lj,w>
Conmee node 1 with A C
Cenneet nodo 2 th A )

Comeet node wcth ALO


qaph an diree tod weighted
Conneel nodo usith
Qases.
Tueedqes. ymaere Ahe dependen
on .lmoes
au
and ofember
. tue ealto ioops ss the sinee
mentd beto Lme O(t) takes adjacenty
it The
compeiy: Cowytatoral
thaph.
ued uoasnnodes atmogt it led qaimwnn
ith along aay elmenlo seh algoitan
n a this Iu
ststte
wed Dat
gughi weightad Direted iw)
b
b-’e’d qhaphiUnuweigatd hietd )
b-5’a
nurigated
let
Pago No,- 15
Speial Fontu

2) ay ko look fos ehil nodes.


3) Manipulatim s 4imilar to aingly inket lit.
4) Seanehing all ehild nodes of a node
6) Asmall 'ph ill ta e moe
tahes ou) tine
$cntatim since tae hodes need to storepateaddom
in this sepr
of not
nade. ît oil lae elativey doo
6) Uses Les spaee than mmathi
bigger gughg
ymamie stueture .
upresetation dus to
ASS IGNENT NO:4 Dale :04.11.22

Psoblem Statemin:

t) onniden a Diheeled gapl do DS ar


it,ard orpu
e)tompue the nemken of omponents of a giwen Undinetoi
a) loniher an Ulinectad ghaph do DFS on it and diieye
e) londer a Dicted kaph, Do DES on
it and dFan
tialk it <dqes kased on yewn seuenee of vitig the
vant'as.

segeta the depth is eplored finst in this alg


pevies crploel node utill a deal end eus.31 Can

Tusomees duing the DES he


sol non as DFs
numben and DFS Comglelin mbe.
Ad in DES, ehens as the DfS
eompletin numben a tes the
node has loeen mnarked oith
DFS eopletn ember ,wohieh
, uohieh am
ar
DES nember anl
alsags eeyaal to nb
Page wo,-17
AH muti tomponenl DES worey on mulliple dijoint
phs. he nembtr of eomponent, ës eualto the nemeez
ot diipint aphs.
thee ae mullple types of elailiatien ot Edqes in
a DS gsaph , nokich ase stated bolow.
Fouehd Ede! a troe
Fonuward edqes ue
-edge in the oado ot DES tnaversing the nodos eonneetod
aith tat parieula edge.n this eae, the DS nmaer ineea
> and DES oepletin number deercases.
baek fåge:
Baek edge
tppasit ot ate edg that tas inenementd DFS eormpletin
uner and devemented DS numbe. he laack ed¡c is
Comtitwe of the foont edge.
DFS Tsee
DES toee io the deied foom khe graph wsed en DES.
The e tales miinal edges to jan the vettios as in DES
and leauer the unneeery edqes avaid
to ureid eyeles.

spetial dietd clqe cin DES uohere


hot DFS uner and bEs eompletion nember inereales. t
Can be derived as (a,b) fo atb
edqe.
Outline ot the algoitum teet i
let s suppose thal a qraph Go
Gn (v,E) that vi the

The maim parblemin diided into 3 ubprolems,


and eaeh them is tarther dvidel nto
Sbpocblent.
Page no, - 12
) DIS ow a the nnd opule thr DfS n:
-

The prodem uoill ake the scl of vert


wll ereate Hhe adjaceney lit . st will atn ant odaes,and
vextx fsom uses, and sill start tobe the atatig
wsg the adjaenty lit. An aeeoible tharosing deptk nsie
node fsm the vetx
earhig a dead end and backtracking. After the apk
is hully cplored and the
hacktaeking
tasal tme
2) rmporento of a OFS tnee;
The pheem ill tale thegaph and the stating
Vets . Ator traseing all nodes aeecyyiba
fom

DIS si be peniomed
wsgthe vertex as
VenzSueh proern usl be penlosmed untilsartiy
all nodes
u eae o diu etod
edge, each time
octod,5t neeory o eheed that if amt oi the
so
Veniees ún a bEs does not eormet to a
mode in puiows DFS.f pseviony
Suehese ocCw)
then they acth wl tosum a
cum ainge Cempennt athes thas
fage No, - 19
two aifenent tomponento
wil de anmine the nmhen of
3)fde in 0rder of hauerangeompenin.
he orden ef vetices Can lbe wsed to Jepheyu the egs
in oxden of havesal . Dwrng
sed ventaa,aeh vertix haversed and
DrS,fsom -the second bhav
ual oill
thal
vertezbrarersd lasfen
upesenled the edge ohen beth ventax is direetiy
Alacnitha at aglaneu i- Jmple mentation oft OES ton
a

Tnputi Let Grlv,E) in a grapla


Undisesd
hee v set ot vetiees
and t oset ot
edgeseorneeted
Output DS taversal ot G
to the vertic,
Aumtions:
Step 1: Take 4bt Vaiahle vto toSe the no. of vert'ees
as input.
Step 2: (2itialize the
adjaleney mathia )
Titialize the variahle To o nd inement it by 4
wpto Vwng fon doop and perform the
Step 2i Sf a node i
step2.2: Marh tae
tolonij
viited then mak viitd Ii]-o
fos to
Bame
the vae o .

step?.3:VandRepeat ánNeN fon loop ky


initiaizig je o wpto
perlormineementg
the Pollowiing action 4 in each sop nd
ote 23.1: nitiate the element ot tuso
dimenional
end ot for doop et <tep 2.
Page NO.-20
Step 9! Taue input the nunmbon
the num of edqes
bon of edges and toe the vabue

Shep 4! (ereatim of mati)


lnitiatie a eounlen i and ineomenl t wpto v
in eaeh stop by 4 and
penfom the folloeg actons
Stap 4.1: Tehe nput the end vertiees OUsoeialil to ith
and tre the waues n the edge
lep 4. n eu
aable n and mnz
if the tuso eud vertees
nand n are bet
Ween e aud mumaer ok vortieas
v then peron the net
Sap 4.2. 1 Sct th adjacaney matie
as well as
elanent adn[nll
the eleyment adi
the qrph s wndireeted ,and theelge [n1[n2t as
ndes and n iqnifie tat n, o beteentote tus
and m is adjacant to n adiacent ng
Slap 4.3: othenise pouut the
Vton is available andmesage that the cinputtd
deeremenfad ay to eontinu
stop S: Cdipla tue adjaceny mahia)
Initialine the aiable: To O end
pto vuig for loop and penform aneremented
the follontng
gtop 5.2 loint each elnent of the
adaceney maix adi
Cosoesponding tothe sonwealumn mode wales
o6: pouint tthe DES tovesal Sequenee by
wsing funeti%
is passed as
skep 64 (DES Pnetion )
Taetthe vaualala
Page. O, - 2|

Slep 6.2.2 ! t he valhe o mode

Kep és' Repea the fon loop fox ieotov y inlremeuted


i lay and penfeam the aeti¡n of following
shp 6.3.I, Male suensie eall to setf
aNangmeut

end ot DES notion.


Implemiatatim of DFs
DES fon direeled Unuseigtad iap:
The
are same as above .Remams
Step 42.t: set the adja eeney maix eloment
anda nthe element adj adjt0-4
ag and the edae Cn2)[n2 2 1as the gia
is dircetod
m signikies tha
laetween thetuo oies
in not adacent to n nis adjacent to ng but
To conit the numlaer f
eomponento ot a Undieed
Her getting the DES taversal sequehee hie are
blind out tue
eponent of gapa usng the
eaanges,
step 9 : Repeal the toh lop by
iniialise the valaaleito
stap I. I: Jnitialine the veiable eeunt (ueteh assigns the
stap 4.2 Sf vila 0o then
Stap 1.2. Snease -tae tount by 4
step 1.2.2 Hahe oeewsive eal to the DfS tuneticn wsing
Page vO.-22

Jmploeuta liom of DFS tos Undieelek Uhaeigod gapl


ddenty tue dilfenent types od odges
Step : Take te ahuetne mamed as ede uolere eorponcato
integer ige sariable DES idontilias the DES
nunser and cinteger type of vaïble DFSC idastlias
-the DS Completiom numbe.
stepa: ake the adiaccney mathia adjC as inpt witadl]
1-D array and marhedI], 1-D aray s input

staps: In the DES funetin integer type o note opassed


s an agument.
Step.l: Snthe Ds funetiom intiger type of mode is passed
argument.tere we take variables.
Yap 3.2.1: Set wiiad Lrd]21
Step 3.22, set S Component ot marked nodel sith iele
mented alus of DES
end ot .
ep 3.3 : Litial2e ieo and ineremented

4hen
Stap 3.3).J: oint the wale of mode and the vertex

Sep3.l.3: Make geewive eall to DES funetion isee


nd of
end ot for lovyp
Page no.-23

waeet DESe
eud of DFS funetion.
Sep4: Tahe input the uemaen ot vertiee) and edge) and the
end woxiess of amy elge then take inpat o al the
element, sf adjaconey matrie ad{l c]and Po, Undineeed
wshere nand m he end Veres et edge
tep 5: Snttialire tuearialleto o aund
nemenled i lay
1upto ng the loop and penfem the Polloutug.
phint eaeh -elanant of adiaceney tmari
to te on eolumh node values, adjl]tileornpo
CPaintg txe-edqe)
Stap bl.1: Mae a elwre eall to DES funetion shre
gument
end etR
end ton
(Psintig the tokusad edg
Suitiallae i to to vjejt| do
Sap 4.1, Dnitialne jto o to vjkjtt do

tp 4.1, 5 DFS ermnponent of moedLDES eomponent


of maadland DES eomponent of manted C
7 DESe eomponent ot omaked il thon
gtap 4-).1.2 : Pint theedge som (it) th node to (j+1)

trp t. CPinting the back edge)


Iitializei to o to , i i t | do
Page No. 94
Vap &il! Tuitialie i to Oto Vijtjt i

marked Lile
ot masked [i7 and DFse tomponen of
the DfSe eomponent of marod Ci1 then
Stap sil.1.2 Pit the edge lactuoeen (it)th note to (ít1)h
mode

-tnd of
step: ( Printing the eros
Titialoe i to o to v ititI do
stap a.1: iinitialiej to o to V, jej+1 do
1.1: T# DrS Component of marked Li> DES Qomponint ot
Sep 41.
Component ot marked L51 d DFSe
ot
Cemponent et marked IuDESC Componnt
Stap 9l.1.2: print the edge behseen liitl) th nade to ljt )thnede
end ot l
end of

Inplementation ot DFS ton Diaetl Unaseigltd ghaph to


Tue ouy ehenges uy neded dn tap .Remains staps ae

the adia eenty mathin awign the vahe ajln2Cn7-|


Stap qi At
and ad;ln[ngIfor direetad qaph ohere ny and n
ae the end veriey ofedge
PageNo.-95

The adfatent matit çs


2 V
,

1
D

1
1 1

"he DFS tavesa se4nenei


4 s3G
For lonneeted &inet

V V3
V 1 1
1 0
1

1
1

For the mumdeer


omponento ot a guen undiebd
V3 Vy
V

1
Vu

Component, are I2 4 5 3 G

You might also like