0% found this document useful (0 votes)
3 views

21. Graphs

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 views

21. Graphs

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/ 12

Grants

a = (V , E )

self V= Set of vertices


loop
MV E =
Set of
edges
"

7¥18! !!:[ having


# !
directions

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: ÷÷÷÷:÷
. .. .. . ."

connected components divide the into multiple


graph
components
"
7¥: :O:c:c::c:* .im: imam
.

vertex
any
O O •
There is a
path between
every pair
of vertices

Path is
of vertices which

set are

connecting pair of vertices


Cycle is circular
path that is

a

starting from same vertex and


ending at same vertex

Dined.ae#e:cg::g:im-a
'

with
cycles

no

O O O These be
arranged linearly that
.
can such

edges forward direction


are
going in
only

- -

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 )

①-20 ① ① Multiple trees can be


generated from

1) / (/) (/) the


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

TREECE FREEL BFS Trees


Spanning

BFS TREE 1 : 1,2 , 3,4 , 5,6 , 7 11 Same as Level order Traversal in trees

BFS TREE 2 : 1,4 , 3,2 , 5,7 , G

c- Starting vertex

void BFS ( int i) 01h27 A 0 I 2 3 4 5 6 7

{ 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 )
{

printfl div ) ; Queuqe I


"
i.

visited -43=1 ;

enquevelqiv) ;
} visited I
Toto
}
}
}
DEPTHFIRSTS-EARCHCDF.SI

Void DFS ( int u)

{
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
.

but not include 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

weights is called minimum tree


cost
spanning .

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

weight along with its vertex

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] ;

I Initialize as the ( Replace '


i
with
' I
'
)
void
{
main 17
given
matrin
) INITIAL
PROCEDURE

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 )
-

;
-

if ( near [ i] ! = o SS cost CITED a cost fi ) Ev) )

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 )
-
-

if ( near Cj ) ! = 0 Sd cost Ej ] The ] a cost Ej ] [ new Ej ) ) )


near [j ) =k ;

}
}

}
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

DISJOINTSUBSETC consider its two subsets

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
;
-

union I find ( u) , find (v) );


itt ;
}

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

Lower Bound SL less than to


Omega or
equal
0
Upper Bound
Big 0 Greater than or
equal to

O
Tight Bound Theta equal

You might also like