You are on page 1of 19

UNI

T4
Agr
aphi
s anon-
li
neardat
ast
ruct
ureconsi
sti
ngof a setof v
ert
ices(
also cal
l
ed
nodes){
v1,
v2,
…,v
n)andasetofedges{
e1,
e2,
…,en}
.Anedgei
sapai
rofv
ert
ices{
v,
iv}i
j ,
j∈
{1…n}
.Thetwov er
ti
cesar
ecall
edtheedgeendpoint
s.

For
mal
l
y:G=(
V,E)
,wher
eVi
sasetandE⊆V×V

Theyar eusedt omodelr eal


-worldsyst
emssuchast heInt
ernet(eachnoderepresents
ar outerandeachedger epr
esent saconnecti
onbet weenrouters)
;ai
rli
neconnections
(eachnodei sanai rpor
tandeachedgei saf l
ight
);oracityr oadnetwork(
eachnode
representsaninter
secti
on andeachedger epr
esentsablock).Thewiref
ramedrawi ngs
i
ncomput ergr
aphicsareanot herexampleofgr
aphs.

Agr aphmaybeei therundir


ect edordi r
ected.I
ntui
ti
vely
,anundi rectededgemodel sa
"t
wo-way "or"duplex"connectionbet weenitsendpoi
nts,whileadirectededgei saone-
wayconnection,andi stypi
callydr awnasanar r
ow.Adi rectededgei softencall
edan
arc.Mathematical
ly,anundirect ededgei sanunorderedpairofv ert
ices,andanar cis
anor der
edpair.Themaxi mum numberofedgesi nanundi rectedgraphwi thoutasel
f-
2
l
oopi sn(
n-1)/2whi l
eadirectedgr aphcanhav eatmostn edges

G=(V,
E)undi
rectedif
for
allv
,w∈V:
(v,
w)∈E⇐⇒(
w,v
)∈E.
Other
wisedirected.
Foreg.

Fi
g1:
Undi
rect
Graph Fi
g2:
Dir
ect
Graph

Graphscanbecl
assi
fi
edbywhet
herornott
heiredgeshaveweight
s.I
nWei
ght
edgr
aph,
edgeshaveawei
ght.Wei
ghtt
ypi
cal
lyshowscostoftrav
ersi
ng

Exampl
e:wei
ght
sar
edi
stancesbet
weenci
ti
es

I
nUnwei
ght
edgr
aph,
edgeshav
enowei
ght
.Edgessi
mpl
yshowconnect
ions.
4.
1TERMI
NOLOGY

 Agr
aphconsi
stsof
:

Aset,V,
ofver
ti
ces/nodes
(
Indi
vi
dualdat
aelementofagr
aphi
sknownasav
ert
ex)

 Acol
l
ection,
E,ofpai
rsofv
ert
icesfr
om Vcal
l
ededges(ar
cs)
(
Anedgeisaconnecti
ngli
nkbet
ween2vert
ices)

Edges,
alsocal
l
edar
cs,
arer
epr
esent u,
edby( v
)andar
eei
ther
:

Di
rect
edifthepai
rsar
eor
der u,
ed( v
)
ut
heor i
gin
vt
hedestinat
ion

 Undi rect edEnd- verti


cesofanedgear et heendpoi nt soft heedge.
 Twov erticesar eadj acenti ft heyar eendpoi ntsoft hesameedge.
 Anedgei sincidentonav ertexi ft hev ertexisanendpoi ntoft heedge.
 Out goi ngedgesofav ertexar edi rect ededgest hatt hev ertexist heor i
gin.
I
ncomi ngedgesofav ertexar edi rectededgest hatt hev ertexist hedest i
nation.
 Degr eeofa v ertex,v,denot eddeg( v)i st henumberofi nci dent edges.Out -
degr ee,out deg( v), i s t he number of out going edges. I n-
degr ee, indeg( v),isthenumberofi ncomi ngedges.
 Par all
eledgesormul tipl
eedgesar eedgesoft hesameend- v erti
ces.Anedgei s
aSel f-loopi fi
ts2endpoi ntscoi ncide.
 Si mpl egr aphshav enopar allel edgesorsel f -
loops
 Pat hi sasequenceofal ternat ingv erticesandedgessucht hateachsuccessi ve
vertexi sconnect edbyt heedge.Fr equent lyonl yt hev erti
cesar elist
edespeci all
y
i
fther ear enopar all
eledges.
 Cy cl ei sapat ht hatstar t
sandendsatt hesamev ertex.
 Si mpl epat hi sapat hwi t
hdi st i
nctv ertices.
 Di rect edpat hisapat hofonl ydi rect ededges
 Di rect edcy cleisacy cleofonl ydirect ededges.
 Sub- graphi sasubsetofv erticesandedges.
 Spanni ngsub- graphcont ainsal lthev er ti
ces.
 Connect edgr aphhasal lpairsofv er ti
cesconnect edbyatl eastonepat h.
 Connect edcomponenti st hemax i
mal connect edsub- graphofaunconnect edgraph.
 For esti sagr aphwi t
houtcy cl es.
 Tr eei saconnect edf orest( pr ev i
oust y peoft reesar ecalledr oot edtrees,these
aref reet r
ees)
 Spanni ngt reei saspanni ngsubgr apht hatisal soat r
ee.
4.
2GRAPHREPRESENTATI
ONS
Ther
ear
etwost
andar
dway
sofmai
ntai
ningagr
aphGi
nthememor
yofacomput
er.

1.
Thesequenti
alrepresentat
ion
2.
Thel
inkedrepresentati
on

4.
3.1SEQUENTI
ALREPRESENTATI
ONOFGRAPHS
Adj
acencyMat
ri
xRepr
esent
ati
on
An adj
acencymat
ri
xisoneoft
het
wo common way
stor
epr
esentagr
aph.The
adj
acencymat
ri
xshowswhi
chnodesar
eadj
acentt
ooneanot
her
.Twonodesar
e
adj
acenti
fther
eisanedgeconnect
ingt
hem.I
nthecaseofadi
rect
edgr
aph,
ifnodeji
s
adj
acentt
onodei
,ther
eisanedgef
rom it
oj.I
not
herwor
ds,i
fji
sadj
acentt
oi,y
ou
cangetf
rom it
ojbyt
rav
ersi
ngoneedge.Foragi
vengr
aphwi
thnnodes,
theadj
acency
mat
ri
xwi
l
lhav
edi
mensi
onsofnxn.Foranunwei
ght
edgr
aph,t
headj
acencymat
ri
xwi
l
l
bepopul
atedwi
thBool
eanv
alues.

Foranygi
vennodei
,youcandet
ermi
nei
tsadj
acentnodesbyl
ooki
ngatr
ow (
i,
[1…n]
)
adj
acencymat
ri
x.Av
alueoft
rueat(
i,
j)i
ndi
cat
est
hatt
her
eisanedgef
rom nodeit
o
nodej
,andf
alsei
ndi
cat
ingnoedge.I
nanundi
rect
edgr
aph,
thev
aluesof(
i,
j)and(
j,
i)
wil
l
beequal
.Inawei
ght
edgr
aph,t
hebool
eanv
alueswi
l
lber
epl
acedbyt
hewei
ghtoft
he
edgeconnect
ingt
het
wonodes,wi
thaspeci
alv
aluet
hati
ndi
cat
est
heabsenceofan
edge.

Thememor
yuseofanadj
acencymat
ri
xi n2)
sO( .

 Exampl
eundi
rect
edgr
aph(
assumesel
f-
edgesnotal
l
owed)
:
ABCD
A0111
B10∞1
C1∞ 01
D1110

 Exampl
edi
rect
edgr
aph(
assumesel
f-
edgesal
l
owed)
:

ABCD
A∞ 111
B∞ ∞ ∞
1C∞ ∞
∞ ∞ D∞
∞ 1∞
Pr
os:
 Representat
ioni
seasi
ert
oimpl
ementandf
oll
ow.RemovinganedgetakesO(
1)ti
me.Quer
iesl
i
ke
whet
herthereisanedgefr
om v
ert
ex‘
u’t
overt
ex‘
v’ar
eeff
ici
entandcanbedoneO(1)
.

Cons: 
ConsumesmorespaceO( V^
2).Evenift
hegraphi
sspar
se(
cont
ainsl
essnumberofedges)
,it
consumesthesamespace.Addi
ngavert
exisO(V^
2)ti
me.

LI
NKEDLI
STREPRESENTATI
ONOFGRAPH

Adj
acencyLi
stRepr
esent
ati
on
Theadj
acencyl
i
sti
sanot
hercommonr
epr
esent
ati
onofagr
aph.Ther
ear
emanyway
s
t
oimpl
ementt
hisadj
acencyr
epr
esent
ati
on.Onewayi
stohav
ethegr
aphmai
ntai
nal
i
st
ofl
i
sts,
inwhi
cht
hef
ir
stl
i
sti
sal
i
stofi
ndi
cescor
respondi
ngt
oeachnodei
nthegr
aph.
Eachoft
heser
efert
oanot
herl
i
stt
hatst
oresat
hei
ndexofeachadj
acentnodet
othi
s
one.I
tmi
ghtal
sobeusef
ult
oassoci
atet
hewei
ghtofeachl
i
nkwi
tht
headj
acentnode
i
nthi
sli
st.

Exampl
e:Anundi
rect
edgr
aphcont
ainsf
ournodes1,
2,3and4.1i
sli
nkedt
o2and3.2i
s
l
i
nkedt
o3.3i
sli
nkedt
o4.

1-[
2,3]

2-[
1,3]

3-[
1,2,
4]

4-[
3]

 Exampl
e(undi
rect
edgr
aph)
:
numberofedgesinagraphthusconsuming
O(V^
2)space.Addingaver
texiseasi
er.

Cons:
 Quer
iesl
ikewhetherther
eisanedge
fr
om ver
texutovertexvarenoteff
ici
ent
andcanbedoneO( V).

 Exampl
e(di
rect
edgr
aph)
:

Pr
os: SavesspaceO(|
V|+|
E|).Int
he
worstcase,t
herecanbeC(V, 2)
o A:B,
C,D
o B:
A,D
o C:A,
D
o D:A,
B,C

o A:B,
C,D
o B:
D
o C:
Nil
o D:
C
4.
4GRAPHTRAVERSAL
Graphtrav
ersali
stheproblem ofvi
sit
ingal
lthenodesinagraphinaparti
cul
armanner,
updati
ngand/orchecki
ngthei
rv al
uesalongtheway.Theorderi
nwhi cht
hevert
icesar
e
vi
sit
edmaybei mportant
,andmaydependupont hepart
icul
aral
gori
thm.

Thet
wocommont
rav
ersal
s:

br
eadt
h-

f
ir
stdept
h-

f
ir
st

Br
eadt
hFi
rstSear
ch

Thebr ead-
fi
rst
-sear
chalgor
it
hm start
satav er
tex andvi
sit
s,fi
rstt
henei
ghboursof ,
thentheneighboursoftheneighboursof ,thenthenei
ghboursofthenei
ghboursof
theneighboursof ,andsoon.Thi salgor
it
hm isagener
ali
zati
onofthebreadt
h-f
ir
st
trav
ersalal
gori
thm forbi
nar
ytrees.I
tusesaqueue.
• A br eadt
h-f
ir
st sear ch (BFS)
explor
es nodes nearestthe r
oot
before expl
ori
ng nodes
fur
theraway
• Forexample,aft
ersearchi
ng A,
then B, then C, the sear
ch
proceedswi
thD,
E,F,
G
• Nodeareexpl
oredintheor
derAB
CDEFGHIJKLM NOPQ
• Jwil
lbefoundbefor
eN

Al
gor
it
hm_
BFS

1.Ini
tial
izeanar rayAandaqueueQ.
2.Readt hev ertexVif rom whi
chyouwantt ostartthet
rav
ersal
.
3.Ini
tial
izet hei ndexoft hearr
ayequalto1att heindexofVi
4.Insertt hev ertexVii nt
othequeueQ
5.Visitthev er t
exwhi chisatt
hefrontofthequeue.Delet
eitfr
om t
hequeueand
placeitsadj acentnodesi nt
hequeue(ifatt
hatindexinthearr
ayt
heentr
yisnot
equalt o1)
6.Repeatst ep5t il
lthequeueQisempt y
Dept
hFi
rstSear
ch

Thedept h-
fir
st-
sear
chal gori
thm issi mil
artothest andardal gori
thm fortraver
sing
binar
ytrees;i
tfir
stful
lyexploresonesubt r
eebeforeretur
ningt othecur r
entnodeand
thenexplor
ingtheothersubtree.Anotherwaytothinkofdept h-
fi
rst-
searchisbysaying
thati
tissimil
artobr
eadth-fi
rstsearchexceptt
hatitusesast ackinsteadofaqueue.

• Eg.Adepth-
fi
rstsearch(DFS)
expl
oresapathallthewaytoa
l
eafbefor
ebacktrackingand
expl
ori
nganotherpath

• Forexample,aft
ersearchi
ngA,
then B, then D, the search
backt
racks and tr
ies another
pathf
romB

• Nodear
eexplor
edintheor
derA
BDEHLM NIOPCFGJK
• Nwil
lbefoundbef
oreJ

Al
gor
it
hm_
DFS

1.I
nit
ial
i
zeanar
rayAandast
ackS.
2.Readt
hev
ert
exVif
rom whi
chy
ouwantt
ost
artt
het
rav
ersal
.
3.I
nit
ial
i
zet
hei
ndexoft
hear
rayequalt
o1att
hei
ndexof
Vi
4.I
nser
tthev
ert
exVii
ntot
heSt
ackS
5.Visi
tthevert
exwhi chi
satthet
opoft hest
ack.Del
etei
tfr
om t
hequeueand
pl
acei t
sadjacentnodesi
nthestack(
ifatt
hati
ndexint
hearr
aytheent
ryi
snot
equalto1)
6.Repeatst
ep5t
il
lthequeueQi
sempt
y.

Forexampl
e:Gr
aphTr
aver
sal
s-BFS&DFS-
Breadt
hFi
rstSear
chandDept
hFi
rstSear
ch
byabdul
bar
iht
tps:
//www.
yout
ube.
com/
wat
ch?
v=pcKY4hj
Drxk

CONNECTEDCOMPONENT
Aconnectedcomponent( orjustcomponent )ofanundirect
edgraphisasubgr aphin
whi
chanyt wov er
ticesareconnect edtoeachotherbypaths,
andwhichisconnectedto
noaddi
tionalvert
icesi nthesuper gr wecaneasi
aph.( l
yfi
ndconnectedcomponent sof
anundi
rectedgraphj ustbyfindingBFSorDFSst arti
ngfr
om ever
yunvisi
tedver
tex)
Forexample,t
hegr aphshownintheill
ustr
ati
onabovehasfourconnect
edcomponents
{a,
b,
c,d}
,{e,
f,
g},{
h,i}
,and{j
}.Agr
apht hati
sitsel
fconnect
edhasexactl
yoneconnect
ed
component,consisti
ngofthewholegraph.

4.
5SPANNI
NGTREE
Aspanni
ngtr
eeofagr
aph,G,
isasetof|V|
-1edgesthatconnectall
v er
ti
cesofthegr
aph.
Thus
amini
mum spanni
ngt
reef
orGisagraph,T=(V’,E’
)withthefol
lowingproper
ti
es:

• V’
=V (
i.
esub-
graphcont
ainsev
eryv
ert
exofG)
• Ti
sconnect
ed(
connect
edgr
aphwi
thami
n.no.ofedges(
|V|
-1)
• Ti
sacy
cli
c.
Removi
nganyedgewi
l
lmaket
hegr
aphdi
sconnect
ed.Addi
ngoneedgewi
l
lcr
eat
eaci
rcul
ar
l
oop.
Mi
nimum Spanni
ngTr
ee

Ingeneral
,i
tispossi
blet
oconstructmulti
plespanni
ngtr
eesf
oragraph,
G.I
facost,
Cij
,
i
sassociat
edwi t
heachedge,Eij=(Vi,
Vj)
,thenthemini
mum spanni
ngtr
eei
sthesetof
edges,
Espan,for
mingaspanningtree,
sucht hat
:

C=sum(Ci
j|al
lEi
ji
nEspan)i
sami
nimum.
Inotherwords,LetG=(V,
E)beaconnected
undir
ectedweightedgraphand(V,T)i
sa
spanningtr
eeofG.I fGisweight
edandthe
sum ofweightsoftheedgesinTis
minimum, t
hen( V,
T)iscal
ledasmi ni
mum
spanningtr
ee.

Eg.Thegr
aph

Has16spanni
ngt
rees.Somear
e:
Thegr
aphhast
womi
nimum-
costspanni
ngt
rees,
eachwi
thacostof6:

Mi
nimum Spanni
ngTr
ees:whydoneedi
t?

Supposewehav eagr oupofi slandsandwewi sht oli


nkthem wi t
hbridgessothatitis
possibletotravelfrom onei slandt oanyotherinthegroup.Fur t
hersupposethat( as
usual)ourgov ernmentwi shest ospendtheabsoluteminimum amountont hi
sproject
(becauseotherfactors
l
iket hecostofusi ng,maintaining,etc,t
hesebri
dgeswi l
lprobablybetheresponsi
bili
t y
ofsomef uturegov ernment) .Theengi neer
sareabl etopr oduceacostf orabr i
dge
l
inkingeachpossi blepairofi slands.Thesetofbridgeswhichwi l
lenableonetotrav el
from anyisl
andt oanyot heratmi ni
mum capit
alcosttothegov er
nmenti st
hemi ni
mum
spanningtree.

Ther
ear
etwobasi
cAl
gor
it
hm r
egar
dingMi
nimum Spani
ngTr
ee

Kr
- uskal
'
sAl
gor
it
hm and

Pr
- i
m'sAl
gor
it
hm

Tounder
stand:ht
tps:
//www.
yout
ube.
com/
wat
ch?
v=4Zl
RH0eK-
qQ

Kr
uskal
'
sAl
gor
it
hm

Kr uskal
'salgori
thm i
sagreedyal
gorit
hm ingrapht
heorythatfi
ndsami ni
mum spanning
treef oraconnect edweight
edgraph.Thismeansi tfi
ndsasubsetoft heedgesthat
for msat reethati
ncl
udeseveryv
ertex,wher
et het
otal
wei ghtofal
ltheedgesi
nthetree
i
smi nimized.

Al
gor
it
hm

1.cr
eat
eaf
orestF(
asetoft
rees)
,wher
eeachv
ert
exi
nthegr
aphi
sasepar
atet
ree
2.cr
eat
easetScont
aini
ngal
ltheedgesi
nthegr
aph
3.whil
eSisnonempt yandFisnotyetspanning
4.r
emov eanedgewi thmini
mum weightfromS
5.i
fthatedgeconnectstwodif
fer
enttrees,t
henaddi
ttot
hef
orest
,combi
ningt
wo
t
reesi
ntoasingl
etree
Atthet
erminat
ionofthealgori
thm,t
hef orestfor
msami ni
mum spanni
ngfor
est
ofthegr
aph.Ifthegraphisconnected,thef or
esthasasingl
ecomponentand
for
msami nimum spanni
ngtree.

Eg:
TraceKr
uskal
'
salgori
thm i
nfi
ndi
ngami
nimum-
costspanni
ngt
reef
ort
heundi
rect
ed,
wei
ghtedgr
aphgivenbel
ow:

Ther
efor
eThemi
nimum costi
s:24

Pr
im’
sAl
gor
it
hm

ThePri
m’salgor
it
hm makesanat
urechoi
ceofthecuti
neachi
ter
ati
on–i
tgr
owsa
si
ngl
etreeandaddsali
ghtedgei
neachit
erat
ion.

Al
gor
it
hm

6.I
nit
ial
i
zeat
reewi
thasi
ngl
ever
tex,
chosenar
bit
rar
il
yfr
om t
hegr
aph.
7.Gr
owt
het
reebyoneedge:oft
heedgest
hatconnectt
het
reet
over
ti
cesnot
y
eti
nthet
ree,
findt
hemi
nimum-
wei
ghtedge,
andt
ransf
eri
ttot
het
ree.
8.Repeatst
ep2( unti
lallv
ert
icesarei
nt hetree)
.
Eg.
:UsePrim’salgori
thm t
of i
ndami nimum spanni
ngtreei
nthe
fol
l
owingweightedgraph.Usealphabeti
.
r
calor
dert
obreakti
es.

.
r

Solut
ion:Pri
m’sal
gorit
hm wil
lproceedasfoll
ows.Fir
stweaddedge{ d,e}ofweight1.
Next,weaddedge{ c,e}ofweight2.Next
,weaddedge{ d,
z}ofweight2.Next,weadd
edge{ b,e}ofwei
ght3.Andf i
nall
y,weaddedge{ a,b}ofweight2.Thisproducesa
minimum spanni
ngtreeofweight10.Aminimum spanni
ngtreei
sthef
ollowi
ng.

Di
ff
erencebet
weenKr
uskal

sandPr
im’
sAl
gor
it
hm
Pr
im’
salgor
it
hm i
nit
ial
i
zeswi
thanodewher
easKr
uskal
algor
it
hm i
nit
ial
i
zes
wi
thanedge.
Kruska'
sbuil
dsamini
mum spanni
ngtr
eebyaddi
ngoneedgeatati
me.The
nextli
neisal
wayst
heshor
test(mi
nimum wei
ght
)ONLYifi
tdoesNOTcreat
ea
cycl
e.

Pri
msbuil
dsamimi
mum spanni
ngtr
eebyaddi
ngonever
texatati
me.Thenext
ver
text
obeaddedi
sal
waystheonenear
estt
oavert
exal
readyonthegr
aph.
I
npri
m’sal
gor
ithm,graphmustbeaconnect
edgr
aphwhi
l
etheKr
uskal

scan
f
unct
ionondi
sconnectedgr
aphstoo.

4.
7TRANSI
TIVECLOSUREANDSHORTESTPATHALGORI
THM

Tr
ansi
ti
vecl
osur
eofagr
aph

Givenadi rectedgraph,f
indoutifav ert
exji sr eachablefr
om anot herver
texiforall
vertexpair
s( i
,j)i
nthegiv
engraph.Herereachablemeant hattherei
sapat hfrom v
ertex
itoj.Thereachabi l
it
ymatri
xiscal
ledtransit
iveclosureofagr aph.Thegraphisgivenin
t
hef or
m ofadj acencymatr
ixsay‘graph[V][
V]‘wher egraph[
i]
[j]i
s1i fther
eisanedge
fr
om v ert
exi t
ov ert
exjorii
sequaltoj,otherwisegraph[i
][
j]i
s0.

Mai
nidea:
apat
hexi
stsbet
weent
wov
ert
icesi
,j
,i
ff

t
her
eisanedgef
rom i
toj
;or
thereisapat
hfr
om i
toj
goi
ngt
hroughi
nter
medi
atev
ert
iceswhi
char
edr
awnf
rom set
{
v er
tex1};
or
thereisapathf
rom i
toj
goi
ngt
hroughi
nter
medi
atev
ert
iceswhi
char
edr
awnf
rom set
{
v er
tex1,2}
;or
thereisapathf
rom i
toj
goi
ngt
hroughi
nter
medi
atev
ert
iceswhi
char
edr
awnf
rom set
{
v er
tex1,2,
…k-1};
or
t
her
eisapat
hfrom it
ojgoi
ngt
hroughi
nter
medi
atev
ert
iceswhi
char
edr
awnf
rom set
{
ver
tex1,
2,…k}
; or
t
her
eisapat
hfr
om i
toj
goi
ngt
hroughanyoft
heot
herv
ert
ices

Shor
testPat
hal
gor
it
hm

Theshort
estpat
hpr obl
em i
st heprobl
em off
indi
ngapat
hbet
weentwovert
ices(
or
nodes)i
n agraphsucht hatthesum oft hewei
ght
sofit
sconst
it
uentedges
i
sminimi
zed.
Thisisanal
ogoustotheprobl
em offindi
ngt heshortestpat
hbetweent wointer
secti
ons
on a road map:t he gr
aph'
sv ert
ices correspond tointer
secti
ons and the edges
corr
espondtor oadsegments,eachweight edbyt helengt
hofi tsroadsegment .The
MinimalSpanningTreeproblem i
st osel ectasetofedgessot hatthereisapat h
betweeneachnode.Thesum oftheedgel engthsistobemi ni
mized.
TheShort
estPathTr
eeprobl
em i
stofi
ndthesetofedgesconnect
ingal
lnodessuch
thatt
hesum oft
heedgel
engt
hsfr
om t
heroott
oeachnodeisminimi
zed.
Di
j
ikst
raAl
gor
it
hm (
singl
esour
ceshor
testpat
h)
Dji
kstr
a'salgor
it
hm solvestheproblem off i
ndingtheshort
estpat
hf rom apointina
graph (hesour
t ce)toadest i
nat
ion.I
tt urnsoutt hatonecanfi
ndt heshor
testpat
hs
fr
om agi vensour oal
cet lpointsinagr aphint hesametime,hencethisprobl
em i
s
sometimescall
edt hesi
ngle-sour
ceshort estpathsprobl
em.
Thesomewhatunexpect
edresul
tt hatal
lthepathscanbefoundaseasi
l
yasone
fur
therdemonst
rat
est
heval
ueofreadingt
heli
ter
atur
eonal
gor
it
hms!

Thisprobl
em isrel
atedtothespanningt r
eeone.Thegraphrepresenti
ngal
lthepaths
fr
om onev er
textoalltheother
smustbeaspanni ngt r
ee-itmusti ncl
udeal
lver
ti
ces.
Therewillal
sobenocy cl
esasacy cl
ewoul ddefi
nemor ethanonepat hfrom t
he
sel
ectedvert
extoatleastoneotherv
er t
ex.St
epsofthealgor
it
hm are

1.I
nit
ial
Sel
ecttherootnodetoformthesetS1.Assi
gnt
hepat
hlengt
h0t
othi
snode.Put
al
lothernodesinthesetS2.

2.Sel
ect
ion

Computet
helengt
hsofthepathst
oal
lnodesdir
ect
lyr
eachabl
efrom S1
thr
oughanodeinS1.Sel
ectt
henodei
nS2wi t
hthesmall
estpat
hlengt
h.

Lettheedgeconnect
ingthi
snodewit
hS1be(i,j
).Addthi
sedgetot
heshor
test
pathtr
ee.AddnodejtothesetS1anddel
etei
tfrom t
hesetS2.

3.Fi
nish

I
fthesetS1incl
udesal
lthenodes,st
opwi
tht
heshor
testpat
htr
ee.
Other
wiser
epeattheSel
ecti
onstep.

For
Eg:

4.
7.2War
shal
l
’sAl
gor
it
hm
Warshal
l
’sAlgor
it
hm i sagr aphanal
ysisalgor
it
hm forf i
nding shor
testpathsina
wei
ghtedgraphwit
hpositi
veornegati
veedgeweights(butwit
hnonegat i
vecycles,
see
bel
ow)andalsof
orfindi
ngtransi
ti
vecl
osureofarel
ati
onR.
Floyd-
Warshallalgor
it
hm usesamat r
ixoflengthsD0 asitsinput.I
fthereisanedge
betweennodesiandj ,t
hant hemat r
ixD0 contai
nsi t
slengthatt hecorresponding
coordi
nat
es.Thedi agonalofthematri
xcontai
nsonl yzer
os.Ifther
eisnoedgebet ween
edgesiandj ,thantheposit
ion(i
,j
)contai
nspositi
veinfi
nit
y.Inotherwords,themat ri
x
repr
esent
sl engthsofallpathsbetweennodest hatdoesnotcont ai
nanyintermediate
node.
I
neachi ter
ati
onofFl oyd-
War shal
lalgor
it
hm i sthismat rixrecalcul
ated,soi tcont
ains
l
engthsofpathsamongal lpairsofnodesusinggr aduall
yenlargingsetofi nter
mediate
nodes.The mat r
ixD1,whichiscreatedbyt hefir
sti t
erati
onoft hepr ocedure,cont
ains
pathsamongal lnodesusingexact l
yone( predefined)inter
medi atenode.D2 contains
l
engths usi
ng two pr edef
ined int
ermediate nodes.Fi nal
lyt he mat ri
x Dn uses n
i
ntermedi
atenodes.
Thi
str
ansf
ormat
ioncanbedescr
ibedusi
ngt
hef
oll
owi
ngr
ecur
rentf
ormul
a:

ForEg.

Exampl
edi
scussedi
ncl
ass:ht
tps:
//www.
yout
ube.
com/
wat
ch?
v=oNI
0rf
2P9gE

I
NTRODUCTI
ONTOACTI
VITYNETWORKS
Anacti
v i
tynetworkisadirect
edgr aphi
nwhichthevert
icesr
epr
esentacti
vi
ti
esand
edgesrepresentpr
ecedencerelat
ionbetweent
hetasks.Theact
ivi
tynet
workisal
so
cal
ledActivi
tyOnVertex(
AOV)net work.

Topol
ogi
cal
sor
tgi
vest
heor
deri
nwhi
chact
ivi
ti
escanbeper
for
med.

St
ep1.chooset
henodehav
ingi
ndegr
ee=0.
St
ep2.r
emov
ethatnodeandedges
connect
ingt
oit
.St
ep3.Got
ost
ep1.
Eg:

Topol
ogi
cal
sor
twi
l
lgi
vet
hef
oll
owi
ngsequenceof
act
ivi
ti
esB-
>A-
>D-
>C-
>E

You might also like