0% found this document useful (0 votes)
3 views53 pages

ADA - Module 2 Notes

ada2

Uploaded by

manyap173
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)
3 views53 pages

ADA - Module 2 Notes

ada2

Uploaded by

manyap173
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

.

IN
C
N
SY
U
VT

Studied smart, not hard — thanks to [Link]


.IN
C
N
SY
U
VT

Studied smart, not hard — thanks to [Link]


.IN
C
N
SY
U
VT

Studied smart, not hard — thanks to [Link]


.IN
C
N
SY
U
VT

Studied smart, not hard — thanks to [Link]


.IN
C
N
SY
U
VT

Studied smart, not hard — thanks to [Link]


.IN
C
N
SY
U
VT

Studied smart, not hard — thanks to [Link]


.IN
C
N
SY
U
VT

Studied smart, not hard — thanks to [Link]


.IN
C
N
SY
U
VT

Studied smart, not hard — thanks to [Link]


.IN
C
N
SY
U
VT

Studied smart, not hard — thanks to [Link]


.IN
C
N
SY
U
VT

Studied smart, not hard — thanks to [Link]


.IN
C
N
SY
U
VT

Studied smart, not hard — thanks to [Link]


.IN
C
N
SY
U
VT

Studied smart, not hard — thanks to [Link]


.IN
C
N
SY
U
VT

Studied smart, not hard — thanks to [Link]


.IN
C
N
SY
U
VT

Studied smart, not hard — thanks to [Link]


.IN
C
N
SY
U
VT

Studied smart, not hard — thanks to [Link]


.IN
C
N
SY
U
VT

Studied smart, not hard — thanks to [Link]


.IN
C
N
SY
U
VT

Studied smart, not hard — thanks to [Link]


.IN
C
N
SY
U
VT

Studied smart, not hard — thanks to [Link]


.IN
C
N
SY
U
VT

Studied smart, not hard — thanks to [Link]


.IN
C
N
SY
U
VT

Studied smart, not hard — thanks to [Link]


.IN
C
N
SY
U
VT

Studied smart, not hard — thanks to [Link]


.IN
C
N
SY
U
VT

Studied smart, not hard — thanks to [Link]


.IN
C
N
SY
U
VT

Studied smart, not hard — thanks to [Link]


.IN
C
N
SY
U
VT

Studied smart, not hard — thanks to [Link]


.IN
C
N
SY
U
VT

Studied smart, not hard — thanks to [Link]


.IN
C
N
SY
U
VT

Studied smart, not hard — thanks to [Link]


.IN
C
N
SY
U
VT

Studied smart, not hard — thanks to [Link]


.IN
C
N
SY
U
VT

Studied smart, not hard — thanks to [Link]


.IN
C
N
SY
U
VT

Studied smart, not hard — thanks to [Link]


.IN
C
N
SY
U
VT

Studied smart, not hard — thanks to [Link]


.IN
C
N
SY
U
VT

Studied smart, not hard — thanks to [Link]


.IN
C
N
SY
U
VT

Studied smart, not hard — thanks to [Link]


.IN
C
N
SY
U
VT

Studied smart, not hard — thanks to [Link]


.IN
C
N
SY
U
VT

Studied smart, not hard — thanks to [Link]


(19)
Depth ogt Sensch :
" DFS lato vie lng Volices a qoaph at af askiteay vestex
by maskinq it aa hoxing been' iaited on eh itevalien algethrn
Puoceeds o a iaited Vestex tht s
atfacert ne

withne
the peoceg8 Cenlenues w l a dead erd i.e,a veetex
adjacent uneaited Vesices te encoutesed.
o tbe Vestex t
" At a decl end, algeibm bares up ne eclge Wieled velices
ame aom and ies do coniie wialing
foon there.
" Algtritm erenually halt e allen backeng up he alasding

.IN
a de enl.
Vetex, sitt, te littee betnq
then, all the veolices in the Bame chete conpenent ae he

alauing
4a y uriated
Yeetex haure been iailed.
Vealites lil
C enain, DFS muet be 9esloted
N
ot term
SY

opeRalion DFS
we pueh a Vetex enlo the aSock hen the Vetek Ze ecche
o the jstime and e pop a vetex aee e alk hen it
U

becemes a dea en.


VT

DFS useg Toree edqe and Bat elge


Taee edge;- oheneree a new uiBted vestex ie ehed poe

t ia being Leached. Such an eae ia


called a siee edlge
hich
Because tte pet ala h edgee fon a feet.
pouenieusly
poonioualy viatted vestex
edge deoding
Bask edge r An imneliate too
puedeesso i.e ta paent. such an
pthe ton ita it connets a vestex o
is called a back ele - Beccuwse
ite ancesl gthe tban he panent.
Toee edqes ae Lepes eted Aing Boid ines.
Back Le tepuesented dstted lin es.
Studied smart, not hard — thanks to [Link]
Alqettm;
Alqotm dte la,m,
| TsaesLSe e goeaph touom the qiven node 2U9te in DES

adiacency malix
nunbee nodein qoaph
4oorn ohene the Joavess al ie to be alastel
’ viated andnst
ttat ose
3 ’ Ldictee tbe veeeg
Vaeted.
l Output; u,), nodes v eecuhable focom
Vecloe t

.IN
C
t[ J v
N
SY

des la,n, V, 3,
end
U
VT

Tuaree the qatph wsg DFS ah cene lort he DEs ee.


Give t e deN in huh he Velicos e teccted he
1* JEme pushel enlo alak) anddee i whict the Vesiees
boceme dend end (popped a e altaek). Menlien ee edes
edg

Studied smart, not hard — thanks to [Link]


Step1 Step-a Step-3 Step- 4 Step-5 (19)
stak
toe)Node Vtsiked (9) Pop (atatk) Dutput
a, c,
a,c, d
C, c, d
a,c,d, p
a,b, c,d, e-b
b-e
a,b, c,d, e, 4
bse Js b, b, C, d, e,

.IN
a b, , d, e, bs, 3
ajb, C, d,e,f
C
a,b, c,d, e,
Ca,5
a, ?. a,b, c,d, e,f
N
( a,b, c, d, e,f
SY

Nete i- slack ia emply. so, Jake the mext rieited Veotex


h a, b, e, d,e,
U

a,b, c,a,e,P,
VT

a,b,c, d, e,P, q
h,i
a,b, c, d, e, t,q
h,t,
ab, C, d, e,fqbi
a,b, c,d,e,t,9,

la,be4,e,2,q,5i
a,b,c,d,e,t.9.h
DFS lcavesal
Studied smart, not hard — thanks to [Link]
Toearel Asouk (oe) the Be in thiel, Veoit er pushed ane
PO Pped but o the lak aste ohlanod oeorn
g tep-4 as ahour
o,1

8,9

3W6AtNlpt numbe indiotes' toe Beden in ohict a Vetex oas


Eated ie, pushed onto the alak
ubertot umou enAitoteg tte BdeL in hhich a vestex becane
a dead end ie, popped eute he alAck

.IN
DES ee and aaveal e obiainee by lotine, tt he sedpct ceumn
C
N
SY

)
DFS ar osal
U
VT

Fiq :- DFSRee
Nte;
Alter Istarealng the qoaph in DFS, t,ere asene bak edges
tben the qoaph is acyelie. thewe ta a back ege oom som
Vesctex u o te ahcesti V, then the qph ayce.
Analyats i
" Analyata e DES depend ori Be humben valicea dnd hcthea the
adfaieny maoix

Studied smart, not hard — thanks to [Link]


" BFS is a methodmettod oeareAing tthe qagh by
nole ag 8re qoaph iin a Byatenalie tulen.
Ble.
t poceeds in a Concenloiçt ranes by vieiling iat dl
Vesires hat ae adjacent to a ptosling vestex en al the
all te Veslicee in he Bame connetel cerpone
Blaalng vestex ase wiBited olytitt hes
semaün uhwieited vesices,
thene a e all vesex e cnsthes connetetl
teelate at on aebiloy
o be
Conponent the qooph. soeare t e opelion BFS
ee queue lo

.IN
t is conveniet o Ene,it ie
iAeoted
ten a Veste 89 eached e the
inlo eca en e aee a
C aleendy explared
the vestex
hen & we qot a dead end i.e,
be oiont ueue.
N
we delete avesdex locom
Pdeing veices ie, The flee
SY

Thus, BF$ yielda only


in nhie the Veslices asee nBeotedii queue i& 2ane ae the
Glee n hich Veices ae deleted Qoom aee.
U

-Tree edge andd ooeA edge.


VT

Toee ede i- nheneren a new wwieited veotex s heachedl


the ot ane te Vete is attached as a child o te Votex t
edqe colled nee edges wtich
ae uejes ented as sdid lnes.
edqei- 4an edqe leadin to a paertoualy viat Vetex
poetously viated
sthen than ite mmedlate puideceAAot i.e, wte pasent ia
encounteeed, edqe is neted as coesA
o dotted enes.
-

AlgBttm BFSa, n, Buue, T)


uce ede in BFS
|| Taveee the qiven
I Trputi- a ’ the
gen guagb
qeagh.
Studied smart, not hard — thanks to [Link]
Souste>
Il Butput;- u, ) ’nodes Vaeatahle fouorn
Veclee T

s[i7e- 0

]BUHce

end

.IN
end'nue
C
" Analyae BFS deperda on the nunbee ef veices and hetthe
N
e otjaceney ipsenenlalion a
SY

"Ttme eptterey o aljacenty dirkedpQtat sepsesenlelion


U

0(iv¥E)
VT

E BFs oee. how


Touare2e the qaoph by BFS hd conAlout the
tee odqjes and cOBA elea.

Studied smart, not hard — thanks to [Link]


step- | Step - & Step- 3
Euene, output, Tu
a

C, d, e

ad,e a,c, d, e4 d,e,


a,c, d, e, e e,
a,b, f a, C, d, e, ,b e-b
C, b, e a, b, c,d, e, b

a, b,c, d,e, e

.IN
Nete:- Reue ls emply, b t thee asee Beme Veices that ase
et enitel. So, lo the net aubqaph sedo the above poo eduee
a,b, c, d e,* C
N
h
h
SY

a,b, e,4,e,t,qh,ij h-i


a,b, c,d,eAqh.i
a,b, c,d,e, #,qh/
U
VT

BFS
"Rdeainq g veices is obtalnel by looking ot the lavessal
12t ceumn nhee te veslices ave deletecl qoom
queue
BFS Saresal ia obtainel by
lookin ot te output celumn

Fi:- BFS iee


Ftg ;- BFS aveLAal
Studied smart, not hard — thanks to [Link]
Toplagicnl Setir:
" Top ofogial pot gaa nineted Acyclie Gooph (DAG) Gu=, ) i aa
necse Beine atl the veslices Auh dot
un a qoaph G, he Vestex u ap era belfre te Veotex v in

" VAcolces
loploqical eota qoapb cacan be Vewed a an Bdesine
along a hü zoal ine ao that all lected adqes qo'

F a cydic qsaph, me linaos Bleing io poasle.


cycles, loplogical
has a eulion

.IN
) DFS mettod C
Sousce Removal metto
N
Topological oEng sdulion oblained by thehe BUe 4emovl
the one obtained by tte DFS metod
SY

Beth ten Cee cooe e couSe, the


pocblem may have Bevesal tesunalive aulion.
U

DTopelogical set uein DFS met-od;


VT

metod can be obtainel as shoon in


Topoloquca sot uslng DFS
belaw steps i
asboiay Vesdex
ten a vete ds IAted lo te t me, it is pushed onlo
he aouk
) when a vestex becemeg a dead end, tis ernoved loom
the
O
alack
d) Repoat atep-2 o 3 f al he Vesices in tbe
Revesse the ode e eleted iens o qet te topclogical
BecLerce.

Studied smart, not hard — thanks to [Link]


Apply te DFS based method o a oBve de
Eq -
potlem qdlousing qeaph.

L` V= adj(o[op) Nodes Ntazted, S Pop (stackH


b a,b

.IN
a,b a,b,e
a,b,e a, b, e
a,b a,b, e,
C
a, b, e,q
N
a,b,q, a, b, e, Çß
SY

a,b,
a,b, e,f,
a,b a,b, e, .9 b
a,b, c, e,f.9
U

a,C
a
a, b, c, e, . C
VT

a,b,C, e,.9
a, b, c, e,f.4
a, b, C,
,.
a,b, C, e, .q,
a,b, c, a,e, #.9|
- The Ble in hich Velices ae eemoved loon the alaek
sLemored
obtained toeon the laat colum. The P°pped
e, f,9, b, C, a, d ’ poppedd seaence.
Toplogionl oden ia ie obtoined by eresing the
he above
Poppel
Seauence hict te qiven by
d a c ’ b ’ q7 ’e
Studied smart, not hard — thanks to [Link]
Alqethm toplogteal Pden (n,
/ To oblain te genLence fota to be executel seesueng in Mopeloaiti
ajacerey malix e
niunbe Veslicos in the qsaph
hot ooe e nodes
nodeg NY9itecd and
S’ to tnew what ae the
a e tbe Vetc es (nodes nhict ose deal
’ Inelex Yaslabee o
en
iueb t e Veslices ase

.IN
nghac helle t e Gtle in

Popped.
indicctes he Veices in
C he sereseae dee ha
loutput Hes
ase o be exeted
N
SY
U
VT

DFS (u, n, a)
e

olusun

Aralya
Aafacency malsix
Jine DFS funelion is the ne aeaeied o ex eeute
Running
he olenent
Studied smart, not hard — thanks to [Link]
malix ine
Tln) = S |
V=0

DFS
V=o

Veo
= n(n--0+) =n
T[)= n
Time qves by e(n). Fe a
veslic es. Hence, n=lv
Time Complexit ds qiven by elv

.IN
Aaijananey C
N
" Let Gi-,E) be a gopl vohoue V’st e velfes arndEet
a
SY

" Each Yétey io Kiaitet eact elae ie ken Qocom tte iet. So, ene
peeteny acesaing ench Vetex and Ume epctency oaeese
U

he edqe ts ohla~ned by Iv+ ]E)


VT

depesenlalio then tte Eme


ftezeray
oatcees the Veotlic eg anc

T(n) lv]+|e] i.e, Tn) e(tv+\=)


ein Bouce emoral metod:
This mothod i based en deceese and Conaue techniaue.
In thie mothod, avetex noilb ne inceming eelges Bgdecte and
delcted along usits te sutgong edyeg.
dsene aee'8weal veces bit noircominq edees, astzsaoily
a Vetex is letel
The dee in shicb tbe Velirea a e viatel & lelete one by.
one selts in opooqi col
Studied smart, not hard — thanks to [Link]
the goaph ahong loske to be tonplted.
St";- Coneapordlng ajoreney maläx 5

D 1

Yeslices fo thieh the


undegee
undepancent lak hothiot 5

.IN
do net depend ask
and can be computed o o1313’ Sume coluns
Sun ef ndegsee oaeh node
indepondertly
C
les ttbat
N
" Fson the malixk is
]= o. since
Indeguee [ol = Indeguee inolependent
SY

they ae conaidied pobs.


0, l job, hamely
Indeqee [o) , inltcates that fo a dRepends on
la]
U

0.
foba, hanaly
Indeqei[ 3 , indieetes hat fe 3 depends en 3
VT

0,1& Q.
Incegoee lA]= 1, ndicotes tat
3, indicotes ttat jot 5
5 depends on
Indeqnee (3] =
&, 3 and 4.

The osUs Bteps irrebred in the deaigo hare to be pofom ed


epeatedly iu tse alack e
.Find 9e he Velicas hose ihlegee is eno an
on the alak. These vesliees lenste the febe ofict o het deperd
ny othe jb andand can be pegorme independenty
Yeotex u andit o be ene.
ia the lask
Pop a Studied smart, not hard — thanks to [Link]
Add de Vetex u' o the eolulion Vet. This
Vesutey [Link] be next fol o be conaileed
" Fndthe veicoa Vadorert o the vetex u'. The Nesiresv
Sepoeserte he fobs t depend on joß u
Demenit indegoee [v bu one [Link] oeducinq e umes
depenlenies enY by bne. lnce, Lndeasee [ givea tthe nunban
ots to be completed belee job Vahd jeb
dependent iobs vae neluced by tne.
stock U= Pop C) sllin T |V=adj(y) Trdeqpee fota
Fnd indecuee ¢ each node

.IN
3
foen cot adjarency ralbux 3
O3
0, I 3, 4
0,4
C 5
N
1, 40 2,3
SY

3
J, 4,0, 2 3, 5
I, 4, 0, 3
5
I, A, 0, &, 3,5
U
VT

Topologieal
oblained úe ’ l , 4 0, Q, 3, 5
Topdogteal 8equence

Ftq:- Gaths obtained afteu gamorg the Vestex t, indegee


re0. Studied smart, not hard — thanks to [Link]
l To ttain the Beauence af jotbs to be ey ecuted suewllihg n
Il Input - a aj oueney malsix
I Output i S indictes the ttotosue to be executed in Bdex

ZndegeLjl+ sun
.IN
end
C
N
SY

q(undegaee s]==0)
U
VT

end

wes[LoP]
Add u o glu¡n Vec T
o ench Vetex Vadj orent o u
derement

end le
hoteT Studied smart, not hard — thanks to [Link]
Many dmpet ant potlerns seoyve findlng an dement ndt, a
inetut elre.
lypicalls, suet ptlome atee in eiaalin ttat wolves
Comlinaloial objesta 2uch as pounulalin, conhinalion and
Bubsets ee a given eet.
" Exhoulire a cmch 2s Bimply a boute ktee appRoach o
Combin&ial pueklme.
t Bugg ets qenenanq each and eresy elenment the puctlan'a
domon seleeting dth2 .em bat ealiely ae e conaleaint.
aund then indlhg

.IN
a deszed elenet.

inpotant poulema, C
palesnan potlem
N
knapaak potlem
SY

Tavellnq Salesman Psuoblem Tsp


Delinln '- Giren n'ces, a Aalespeoen tats ata Apecied
U
VT

theciy loon hee he hos alated


The pbjecive g s potlem is lo fend a sete thaougb #he
e coat #hee by mauimiaing the puit
Thie psblem Can be modiled by an ndinected otqhted aaph
as ghoon, hshee
The veiees g the qoph suepoesert the vaotous iies.
wetghts aasocated iith edqe sepsesent dbe daances batuieen
Dne

QD- a99ay and ci, ives the cogt qreom cti


Then this poublem can be slated c the
pestlen
shteat Hanillenian i i t a qaaph..
Studied smart, not hard — thanks to [Link]
thoeugb all t4,e Velces e! qoaph exactly nce. Al ciseuit atats
ud ends at one paiulas wentex.
TSP Can be eotved ustng metoda Bsute qoe appaosh and
Dynamie pooqoamntng
boute. yoce appOeh TSP Ceun be 2eved ag shoon belaw:
) Get all the soutes ocom rne cly ooansthea aly by lakinq
Vosius peunutalion d
) Compte the sute lergth fo eaeh ponutalin and aelet the
ghitest te aumong ttoem.
em.s
exeuion ine iorse0se%

.IN
t Ppblen incesses
apidly
Eagtegt waj lo th an exact auion to. T9p t lo cenpute te
C
COst le lous Cn dtesunine dthe lous hoith te menimun
COAt. Thee metod is coclled exhawalive seoenial aeseh and
N
uses boute toree opptoach.
SY
U

aue
e epse2ented P. args and #he diatnce belioeen vasiga
VT

cles ae guepses ented hebet in eah ef the ede.


3 Soe'i- Cost
P
6
P 3 8

30
6 9

Vosious aLtes uaEng tih he can iat each and ereycly


exatly ece and ellisns back o the slat cy polen
hoith e coat inuel
Studied smart, not hard — thanks to [Link]
Q6
P cazt =a)
P

P
p s p
P
P A
P oat =

. Even th ugh we harewe vantely utee woit Vasuing copta,


oe haYe lo c•nile the octe noth minimun cOBt Jo qet
the folloustng tgutee can be seleted by ae
maximuin t. 90,

.IN
P C
ps s p Coat =15)
N
SY

" Let us zee the vasieus


Vasieus outes (peumtalion)that Ceun be wsed by
sDutes (pemutalior)
a beaveeng Balapesuon ohen he iacts each endd er eoy cily exoutty
enee and elusna 'bact to the cila foum otere be haa Aated.
U

Let ua qind out he pasazble sesutes kaken by tbe 2oleapoAT by


VT

he has alatel feom the cil a'. Let'denste tte

iq i- ciles mepsea eted


hen n =
t SA cleoe pom eique tat the Vaseipus suotes ane
i.e, no eates =|
hen m=3

leme fom fique taat


tbt
Studied smart, de
not hard —e vaisus
thanks to [Link] se
e a-b’C-’ a
a c b a feoutes 3-))
hshen n=4i

a’c’ba
6satea a-D

Ih qereal, le en'ios
n'ies unbee ee esutes = n-)l

.IN
i.e,P(n) = n-))
So, Eme omplex gdo given by t()eo (n) C
Knapzack Packlem :
N
Deltnlion - Given n'item kenon welghts ,, [Link] and
SY

values Vy; Vg Vm and a knapsatk capaily w, end t e mnast


Valuable ubset e he item thot ft inlo t e knapeatk.
U

tn' tems qiven, Cormpuling


VT

1denity eaaikle uba ds


i.e, the mes noth the olal weight not exceedlng the Khapsak
capaciliyl and fendingoa Bubset value anen
the lasgeat Value am em
Egi Knapeack capaily w=1D
Item-} 1
Hem--);- ,-3
Item -3;- Wg 4 Va =40

Item- 4 -

Studied smart, not hard — thanks to [Link]


Sei- Capaiy, W,= 10 i.e, Toal weight

Subset Ttal weight Ttal Volue

3
40
5 Q5

7+3 = 10 4&+1=54

i,3 7+4=|| Not eastble, 1>) D


i),43 745-|2 Not ealle, 12>10
3+4=7

.IN
1 + 5 = 37
{a,43 3+5=8
3, 47 |A+5=9 40+ 5 = 65

), , 3} 7+3+4 =14
C
Not feceible, 14>ID
Net feasible, I5>10
N
7+3+5= 15
1, 3, 43 7+4+5=)6 Net feoeible, 16.
16>10
SY

^a,3, 4 3+4t5= 12
i),2,3, 43 7+3+4+5 = 19 Net feaible, 19 >10
U

Subset tthe laqeat value is 3, 43 wbee t e


VT

Lasqest value 65 1oeiaht = 9

Arnalypis -
cun element et ie , tbe exhauelive
. The nunbe e tsete
Aecuhlecds o a Eme etcienc
i.e, )e ()

Studied smart, not hard — thanks to [Link]

You might also like