You are on page 1of 12

4.

GRAPHS
 Graphisacoll
ecti
onofnodesandedgesi
nwhi
chnodesar
e
connect
edwi
thedges
 Gener
all
y,agraph G 
isrepr
esent
edas 
G=(V,E)
,wher
e Vi
sset
ofver
ti
ces 
and Eissetofedges.
 Exampl
e
 Thefol
lowi
ngisagr aphwith5vert
icesand6edges.Thi
sgr
aphG
canbedefi
nedasG=(V, E)WhereV={ A,
B,C,
D,E}andE={(A,
B),
(A,
C),(
A,D)
,(
B,D)
,(
C,D),(
B,E)
,(
E,D)
}.

 From thedir
ectedgr
aphG(V,
E)
V={A,B,
C,D}
E={(A,B)
,(D,
A),
(D,
C),
(C,
B)}

i
ncomi
ng out
goi
ng
A B 1 1
B 0 2 0
C B 1 1
D A,
C 0 2
 Repr
esent
ati
onofgr
aphs:

Agraphcanber epr
esent
edi
n2way
s
1.
usingarr
ays
2.
usingl
inkedl
ist

1.
Graphr
epr
esent
ati
onusi
ngar
ray
s:

AGr aphcanberepr
esent
edusi
ngar
ray
sin3way
s
i.Adjacencymatr
ix
i
i.Inci
dence matr
ix
i
i
i .pathmatri
x

i
.Adj
acencymat
ri
x:

 Here graphisr epresentedusi


ngamat ri
xofsizet ot
alnumberof
ver
ticesbyat otalnumberofv er
ti
ces.
 Int hi
smat r
ix,bothr owsandcolumnsr epr
esentv ert
ices.Thi
s
matrixi
sf i
l
ledwi t
hei ther1or0.
 Here,1representst hatther
eisanedgefrom r
owv er
textocolumn
ver
texand0r epresent sthatt
hereisnoedgef r
om row vert
exto
col
umnv er
tex.
Eg1;
A B adj
acencylist
A B,
C,D
B A,
D

C D C A,
D
D A,
B,C
A B C D
A 0 1 1 1
B 1 0 0 1
C 1 0 0 1
D 1 1 1 0
4×4

Eg2:
i
i.
Inci
dencemat
ri
x:
 Heregr aphi sr epresentedi nmat ri
xformatusingver
ticesand
edges.
I ti
samat rixr epresent at
ionofn×m wher e
n=no.ofv ertices,m=no.ofedges
 Themat ri
xent ri
esf oll
owst hef ol
lowingr
ules
Forundirectedgr aph:
iftherei sanedgeeif rom vit ov jthenvi=1,vj=1andremaini
ng
arezer oes.
Fordir
ect edgr aph:
ift hereisanedgeei f rom v i to vj then vi=1,vj=- 1and
remaining
arezer oes.
Eg1:
A e1 B
e1 e2 e3 e4 e5
e2 eE3 e4 I4×5
A 1 1 1 0 0
B 1 0 0 1 0
C 0 1 0 0 1
C e5 D D 0 0 1 1 1

2.
Graphr
epr
esent
ati
onusi
ngl
i
nkedl
i
st:
 Tor
epr
esentt
hegr
aphi
nli
nkedl
i
stf
ormat.
 Wear
eusi
ng2l
i
sts.
i
.nodel
i
st
i
i
.edgel
i
st
1.
Nodel
i
st:
I
tisanodest
ruct
urewhi
chr
epr
esent
stheact
ual
node.
I
tsst
ruct
urei
s
node nex
t adj
acent
Node–i
trepr
esent
sact
ual
nodev
alue.
Node–i
trepr
esent
sact
ual
nodev
alue.
Next-i
tispoi
ntedt
othenextnode.
adjacent– i
tis a poi
nterpoi
nti
ng t
othe f
ir
stnode edge l
i
st
(
adj
acencylist
)

2.
Edgelist:
Itisusedtorepr
esentt
headj
acentnodeofaspeci
fi
cnode.
Itsstr
ucturei
s

dest l
i
nk

Dest–i
tisapoi
nterpoi
nti
ngt
otheact
ual
node.

* Gr
apht
rav
ersal
:
 The processofv
isi
ti
ngeachnodei
nthegr
aphi
scal
l
edasgr
aph
tr
aver
sal.
 Ther
ear
e2gr
apht
rav
ersal
techni
ques
1.
Breadt
hfi
rstsear
ch(
BFS)
2.
Dept
hfi
rstsear
ch(
DFS)
1.
BFS:
I
tisal
evel
byl
evel
trav
ersal
.
I
tst
andsf
orbr
eadt
hfi
rstsear
ch.
 Toi
mpl
ementt
his,
wear
eusi
ng
i
.st
atusar
ray
i
i
.Queue
I.st
atusar
ray:
I
tisamatri
xrepr
esent
ati
onwhi
chmai
ntai
nst
hev
isi
tedst
atusof
t
henode.
 Di
ff
erentst
atusar
e
 Notv
isi
ted(
ini
ti
alst
ate)
 Runni
ng(
nodei
sinqueue)
 Vi
sit
ed
i
i.Queue:
 Here queue dat
a st
ruct
ureis used to mai
ntai
nt he t
rav
ersal
or
der.
thispr
ocesswil
lcont
inueunt
ilqueuei
sempty.
Eg:
A B
A B,
C
B A,
D
C A,
D
C D
D B,
C
STATUSARRAY
A B C D QUEUE
NV NV NV NV A

R
Del
eteA -v
isi
tA -adj
acentB,
C

A B C D B C

V NV NV NV
R R
Del
eteB-v
isi
tB -adj
acentA,
D

A B C D

V V R NV C D
R

Del
eteC -v
isi
tC-adj
acentA,
D
A B C D D

V V V R

Del
eteD -v
isi
tD-adj
acentC,
B

A B C D
V V V V

BFSi
s A-
B-C-
D
2.DFS:
I
tisagr
apht
rav
ersal
techni
que.
I
tst
andsf
ordept
hfi
rstsear
ch.
I
tmai
ntai
nst
atusar
rayandst
ackdat
ast
ruct
uret
oimpl
ementi
t.
Eg:
V1 V5-V3,
V4,
V6

v
1–v
2,v
3

v
2 v
3 v
2–v
1,v
4

v
3–v
1,v
5.

v
4 v
5 v
4–vv
2, v
5, 6

v
6 v
6–v
4,v
5

STATUSARRAY
V1 V2 V3 V4 V5 V6
NV NV NV NV NV NV
R V1
Del
etev
1-vi
sitv
1-Adj
acentvv
2,3

V1 V2 V3 V4 V5 V6
V NV NV NV NV NV V3
R R V2
Del
etev
3-vi
sitv
3-Adj
acentvv
1,5
V1 V2 V3 V4 V5 V6
V R V NV NV NV
V5
R
V2
Del
etev
5 - v
isi
tv5

Adj
acentvv
3, v
4, 6

V1 V2 V3 V4 V5 V6
V R V NV V NV V6
R R V4
Del
etev
6 - v
isi
tv6 V2
Adj
acentvv
4, 5

V1 V2 V3 V4 V5 V6
V R V R V V
V4
Del
etev
4 -v
isi
tv4
V2
Adj
acentvv
2, v
5, 6

V1 V2 V3 V4 V5 V6
V R V V V V V2

Del
etev
2 - v
isi
tv2

Adj
acentvv
1, 4
V1 V2 V3 V4 V5 V6
V V V V V V

DFSi
sV1–V3–V5–V6 -V4 -V2
/
DFSpr
ogr
am/
#i
ncl
ude<st
dio.
h>
#i
ncl
ude<st
dli
b.h>

i
ntv
isi
ted[
7]={
0,0,
0,
0,
0,
0,
0};
i
ntA[
7][
7]={
{
0,1,
1,
1,
0,
0,
0},
{
1,0,
1,
0,
0,
0,
0},
{
1,1,
0,
1,
1,
0,
0},
{
1,0,
1,
0,
1,
0,
0},
{
0,0,
1,
1,
0,
1,
1},
{
0,0,
0,
0,
1,
0,
0},
{
0,0,
0,
0,
1,
0,
0}
}
;

v
oidDFS(
inti
){
pr
int
f("
%d"
,i)
;
v
isi
ted[
i]=1;
f
or(
intj
=0;
j<7;
j++)
{
i
f(
A[i
][
j]
==1&&!
vi
sit
ed[
j]
){
DFS(
j)
;
}
}
}

i
ntmai
n()
{
/
/DFSI
mpl
ement
ati
on
DFS(
0);
r
etur
n0;
}
/
BFSpr
ogr
am/
#i
ncl
ude<st
dio.
h>
#i
ncl
ude<st
dli
b.h>
st
ructqueue
{
i
ntsi
ze;
i
ntf
;
i
ntr
;
i
nt*ar
r;
}
;
i
nti
sEmpt
y(st
ructqueue*
q){
i
f(
q->r
==q-
>f)
{
r
etur
n1;
}
r
etur
n0;
}
i
nti
sFul
l
(st
ructqueue*
q){
i
f(
q->r
==q-
>si
ze-
1){
r
etur
n1;
}
r
etur
n0;
}
v
oidenqueue(
str
uctqueue*
q,i
ntv
al)
{
i
f(
isFul
l
(q)
){
pr
int
f("
Thi
sQueuei
sful
l
\n"
);
}
el
se{
q-
>r++;
q-
>ar
r[
q->r
]=v
al;
/
/pr
int
f("
Enquedel
ement
:%d\
n",
val
);
}
}
i
ntdequeue(
str
uctqueue*
q){
i
nta=-
1;
i
f(
isEmpt
y(q)
){
pr
int
f("
Thi
sQueuei
sempt
y\n"
);
}
el
se{
q-
>f++;
a=q-
>ar
r[
q->f
];
}
r
etur
na;
}
i
ntmai
n()
{
/
/Ini
ti
ali
zi
ngQueue(
Arr
ayI
mpl
ement
ati
on)
st
ructqueueq;
q.
size=400;
q.
f=q.
r=0;
q.
arr=(
int
*)mal
l
oc(
q.si
ze*
sizeof
(i
nt)
);

/
/BFSI
mpl
ement
ati
on
i
ntnode;
i
nti
=1;
i
ntv
isi
ted[
7]={
0,0,
0,
0,
0,
0,
0};
i
nta[
7][
7]={
{
0,1,
1,
1,
0,
0,
0},
{
1,0,
1,
0,
0,
0,
0},
{
1,1,
0,
1,
1,
0,
0},
{
1,0,
1,
0,
1,
0,
0},
{
0,0,
1,
1,
0,
1,
1},
{
0,0,
0,
0,
1,
0,
0},
{
0,0,
0,
0,
1,
0,
0}
}
;
pr
int
f("
%d"
,i)
;
v
isi
ted[
i]=1;
enqueue(
&q,
i);
//Enqueuei
forexpl
orat
ion
whi
l
e(!
isEmpt
y(&q)
)
{
i
ntnode=dequeue(
&q)
;
f
or(
intj
=0;
j<7;
j++)
{
i
f(
a[node]
[j
]==1&&v
isi
ted[
j]==0)
{
pr
int
f("
%d"
,j)
;
v
isi
ted[
j]=1;
enqueue(
&q,
j);
}
}
}
r
etur
n0;
}

You might also like