21. Graphs
21. Graphs
a = (V , E )
In degree :z ←
out
degree I
:
[ Parallel
Edges
si.mg?.m!iias;m.oop
¥1
① y
•
Without parallel edges
degree
a
:p;÷:: fire:{Imam
O O
①-20 Non -
connected Graph
2
components
0-20
i: ÷÷÷÷:÷
. .. .. . ."
vertex
any
O O •
There is a
path between
every pair
of vertices
Path is
of vertices which
•
set are
Dined.ae#e:cg::g:im-a
'
with
cycles
•
no
O O O These be
arranged linearly that
.
can such
- -
①
Topological ordering
REPRESENTAT10NOFUNDlRECTEDGRAP#
4)
Adjacency Matrix
(2)
Adjacency list
(3) Compact list
:/
ADJACENATR1X ADJACENsT
, z , y ,
①-
⑦ I O l l l O
I
:¥¥i¥ ¥¥¥
Z l O l O O l
-
21 -
3 I -
4 /
: : : : :
5×5 5 #-
⑤ I VI =
n = 5
I El = e = 7
( i ,j )
Afi ) (j) = I
:/
(0STADJACENCYMAT ( Representation of
weight) C0STADJACE1ST
i 2 5
q
3 4
Add another area to node for
① ⑦ I 0 9 4 6 °
of
*Ets
weight edge
: : :O: :
\/
5 7
02
O O 2
7 5×5
I ⑤
of edge
weight
fiendish
en
compact
#i¥E
' ' ' '
[
' ' '
Ii T I T
Starting indexes
IVI t 21 Et t I
5 t 2×7 1- 1=20
20 1- I = 21
REPRESENTAT10NOFDlRECTEDGRAP#
/
ADJACENATR1X ADJACENCY-t.IT
, z , y
# t
O l O l
Ext : :
:÷ :
BREADTH
#
FIRST SEARCH ( BlIs )
•
same
we can
graph
start from any
④-30 ⑦ ③ -
④ ① ⑤ ' vertex
ji
-
Ii
\/
-
/ /
- - - '
. . - - - -
. . . - -
- -
-- -
- - -
Each should be
'
i -
- •
veoten
-
. .
employed fully
. .
.
-
-
. -
⑤ ③ ⑤
. -
-
•
Dotted pink lines represent
✓) /) /) edges
cycle
that
called
make
cross
a
edges
complete
③ ⑦ ④ ① ⑦ ③ .
These trees are called
BFS TREE 1 : 1,2 , 3,4 , 5,6 , 7 11 Same as Level order Traversal in trees
c- Starting vertex
{ o
int U ,
V
; I O l l l O O O
''
d );
point fl
"
i O l
I. ,
2 I O O O O
visited [i ) = l ; 3 l l O l l O O
); Y
enqueuelq ,
i l O l O l O O
5 O O l l O l l
while C ! is Empty ( q ) ) G O O O O l O O
{ 7 O O O O '
O O
←
✓ =
dequeuecq) ; Take out veoten from queue
For
scanning
row of
→ fool V= l ; Ve -
n ; Vtt ) Should not be
f
matrix visited
q
if ( Afv][D= -
-
I LL visited -43=-0 )
{
visited -43=1 ;
enquevelqiv) ;
} visited I
Toto
}
}
}
DEPTHFIRSTS-EARCHCDF.SI
{
int v
;
if C visited Eu) ==
o )
{
"
print fl
"
t -
d ,
u ) ;
visited Eu ) = l
;
)
fool V= l
;
vs = n
; Vtt
{
if I A- CUT [D= =L Sd visited [ V ) ! = O)
DFS LV ) ;
}
}
}
SPANNlNG-REE#
and
Spanning tree is a sub
graph of a
graph having all vertices of a
graph IVI -
l
edges
and there should not be
any cycle .
x
: :*:
IVI = y
IE I = G
6
IE of
IC =
Cz ways or number
µ, ,
spanning trees
.
forming cycles
' F- '
Cw , -
,
-
cycles I
- 4 HIM this
en ample
MINIMUMCOSTSPANNINGTREEIF
weights edges
are added to of
graph ,
then the
spanning tree with minimum cost of
PRIM.SN/NlMUMC0STSPANNlNGTRE# ( IVI -
l ) IE I
ne =
n x n
5 # 01h21
⑤ # ⑥ #
is
I 19 18
I ⑦ la
⑤
In ③ ⑤ ③
¥ ¥
Step : Select the
edge with least
weight along with its vertices
and
SP2 : Then compare the edges joined with the vertices select the one with least
steps :
Repeat step 2 until
spanning tree is obtained
PR0GRAM-
cost o , z y 5 6 7
z
①
-
i÷÷÷:÷÷÷÷÷÷÷÷÷
o
i i.
near
I
.
5
•
1
7
11 either of lower
Only or
upper triangular Motrin will be sufficient
← Largest possible integer value
# define I 32767
IInitialize as { I }
int cost [8) [83 ,
near [8 ] ,
t [23-16] ;
int i K V 7 min I
,j U - =
n
, , , , , ;
for ( it l
;
is -_
n
;
it t )
{ For matrix
-
< accessing upper triangular only
for Cj = i
;jc=n ; jtt )
{
if ( cost [ i )[j ) < min )
{
min = cost [ i ][j ) ;
u
-
- i , V=j ;
}
}
}
+ Eo][o) =
u ;
t [ DEO] = v
;
near [ D= near Ev ] - o
;
for Li l ; ien it -1 )
-
;
-
near Ei ) - u
;
else
}
near Ei ) =v
;
)
)
RECURSIVE
for ( i =
l ; i en -
l ; i + t )
{ PROCEDURE
min = I ;
for Lj = I
;jc=n ; jtt )
{
if ( near Ej ] ! -
- o SS cost [j3[ near 3) < min )
{
min =
cost Cj ] [ near Ej ] ) ;
k=j ;
}
}
t [ o ) Ei ) =
k ;
t [ 1) Ci ) =
near Ek) ;
near Ek) = 0 ;
for (j l
; jc=n ; jet )
-
-
}
}
}
Point t ;
J
KRUSKAL 'S METHOD
FF
⑥ # ⑥ #
I
18
19 18
I ⑦ la
⑤
In ③ ⑤ ③
¥ ¥
Step : Select the
edge with least
weight along with its vertices
if it
ST : Select the next minimum
edge is not
forming a
cycle
U =
{ 1,2 , 3 ,
4 ,
5 , 6 ,
7
,
8
,
9 ,
to
} A =
{ 3 ,
5 , 9
}
B =
{ 4,7 , 8,10 }
11
A AB =p Disjoint subset
A =
{ 3 ,
5 , 9
} B =
{ 4,7 , 8,10 }
③ ①
IT AT
⑤ ⑨ ① ④ ④
i
No of nodes in Rest are
representing parent
negative
( 4 nodes)
4 is
having
Representations
(1) Union
④ The parent with more number of nodes
\
① ④ ④
1
Ii-i .
⑤ ④
parent of nodes
t t
void Union ( int u , int v) ( 2) FINI
{
if l SED s SEV) ) int Find ( int u)
{ {
s Ev) =
Stu ] t SEV ) ; int n =
u
;
s Ev) =
u ;
} while ( s En] 70 )
else n = s Tn ) ;
{
vertices
s [v) = SEU ] + s Ev ) ; connecting while ( u ! =
n)
Sfu ) = V; directly {
} parent v = SEU ] ;
} s Ev) =
n ;
u = V
;
}
}
° ' 2 34 5 6 78
/j
µ ! ! !!! ! ! !!
o
,
I set
④ .
201 l "
edges i#7
⑤
if ③
⑦ included
t
T.ci,
if p
① ③
# define I 32767
int edges [ 3) [ 9 ]
=
{ { I
,
I
, 2,2 ,
3 , 4,4 , 5,5 } ,
{ 2,6 , 3,7 ,
4
,
5
,
7 ,
6 ,
7
} ,
{ 25,5 , 12 , 10
,
18 , 16,14 , 20,18 } } ;
int set [ 8) =
{ -
I };
int included [9) =
{ o} ,
t [ 2) CG ) ;
void maine )
{
int i k 7 9
o
j e min v
= = =
, , ,
n
, , , u , ;
NO of vertices -
i
while ( i s n
{
min =
It
finding minimum for cost
edge
I
for lj=o;jce ; jet )
{
( included Cj ]==o edges [ 2) Ej ]
igf
SS a min )
[ 2) Cj ) ;
min =
edges
K -
-
j ;
u =
edges to ] Cj ] ;
✓ =
edges El ] [ j ] ;
}
}
check if is
if l find ( u) ! =
find ( v) )
To
cycle
{ ←
forming or not
tEo3[ i ) -
u
;
t [ DED -
v
;
-
included Ek ] =L
;
}
ASYMPTOTICNOT.AT#
n
enact
we can
get
f ( ) Ei 1+2+3
nln 01h21 value
= =
n -
. . . . .
th =
←
÷ Enact value is
f ( n ) =
I ixzi not
possible that is simplified
i =L
So ,
we use
asymptotic notations
O
Tight Bound Theta equal