Upload
robinpt
View
215
Download
0
Embed Size (px)
Citation preview
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 1/84
DATA STRUCTURES ANDALGORITHMS
MODULE 4
GRAPHS
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 2/84
Cities connected through airlines
Delhi
Bangalo
re
kochi
chennai
Bombay Calcutta
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 3/84
Cities connected through airlines
Delhi
Bangalo
re
kochi
chennai
Bombay Calcutta
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 4/84
Cities connected through airlines Use trees
With Delhi as root node Delhi
Bangalo
re
kochi
chennai
Bombay Calcutta
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 5/84
Cities connected through airlineswhich Data structure
Delhi
Bangalo
re
kochi
chennai
Bombay Calcutta
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 6/84
Cities connected through airlinesUse gra!hs
Delhi
Bangalo
re
kochi
chennai
Bombay Calcutta
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 7/84
Nonlinear data structure "hose data structures in which data elements are organi#ed in any
arbitrary order $i%e% they are not organi#ed in any se&uence' arecalled non(linear data structures%
)
* + ,
- .
+
) *
-,TREE
GRAPH
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 8/84
Gra!hs
A gra!h G consists o/ two sets0
a set 12 called the set o/ 3ertices $nodes or !oints'
a set 42 called the set o/ edges $or arc' connecting the3ertices in 1
E"a#!le$
1 5 6A2B2C2D7
4 5 6 $A2B' 2 $B2C' 2 $C2D' 2$A2C' 7
A
C B
D
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 9/84
Digra!hs
Digra!h 5 directed gra!h
A digra!h G consists o/ two sets0
a set 12 called the set o/ 3ertices $nodes or !oints'
a set 42 called the set o/ edges $ordered elements'connecting the 3ertices in 1
4dge $A2b' is not same as edge $B2A'
E"a#!le$
1 5 6A2B2C2D27
4 5 6 $A2B' 2 $A2C' 2 $C2D'2 $B2A' 7
A
C B
D
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 10/84
%eighted gra!h
A gra!h or a digra!h with all the edges labeled with some weights
A
C B
D
A
C B
D
5 6
2
3
6
5
5
3
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 11/84
Ad&acent 'ertices
A 3erte8 1i is ad9acent to $or neighbor o/ ' another 3erte8 say 1k2 i/
there is an edge /rom 1i to 1k
Ad9acent 3ertices o/ A are B and C
Ad9acent 3ertices o/ D is C
A
C B
D
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 12/84
Sel( Loo!
:/ there is an edge whose starting and ending 3ertices are same
$1i2 1i ' is an edge in the gra!h
A
C B
D
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 13/84
Sel( Loo!
:/ there is an edge whose starting and ending 3ertices are same
$1i2 1i ' is an edge in the gra!h
A
C B
D
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 14/84
)arallel edges
:/ there is more than one edge between the same !air o/ 3ertices
A
C B
D
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 15/84
)arallel edges
:/ there is more than one edge between the same !air o/ 3ertices
A
C B
D
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 16/84
Multigra!h
A gra!h which has either sel/ loo! or !arallel edges or both
A
C B
D
A
C B
D
Self Loop
Parallel Edge
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 17/84
MULTIGRA)H
A gra!h which has either sel/ loo! or !arallel edges or both
SIM)LE GRA)H
A gra!h which does not ha3e any sel/ loo! or !arallel edges %
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 18/84
Co#!lete gra!h
A gra!h or digra!h is said to be com!lete i/ each 3erte8 1i is
ad9acent to e3ery other 3erte8 1 9 in G%
"here are edges /rom any 3erte8 to all other 3ertices
A
C B
D
A
C B
D
A
C B
D
A
C B
D
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 19/84
C*cle
:/ there is a !ath containing one or more edges which starts /roma 3erte8 1i and terminates into the same 3erte8
A
C B
D
A
C B
D
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 20/84
C*cle
:/ there is a !ath containing one or more edges which starts /roma 3erte8 1i and terminates into the same 3erte8
A
C B
D
A
C B
D
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 21/84
Ac*clic gra!h
A gra!h without any cycles
A
C B
D
A
C B
D
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 22/84
Isolated +erte"
A 3erte8 with no edge connected /rom any other 3erte8 to that3erte8
A
C B
D
4
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 23/84
Degree o( 'erte"
"he number o/ edges connected with 3erte8 1i
Denoted by degree$1i '
A
C B
D
4
Degree(A) =3 Degree(B) =1 Degree(C) =2 Degree(D) =2 Degree(E) = 0
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 24/84
Degree o( 'erte"
:n a digra!h there are two degrees
:ndegree ; <o% o/ edges incident to 1i
=utdegree ; <o o/ edges emanating /rom 1i
InDegree(A) =1OutDegree(A) =2
InDegree(B) =1OutDegree(B) =1
InDegree(C) = 1OutDegree(C) =1
InDegree(D) = 1OutDegree(D) =0
A
C B
D
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 25/84
)endant 'erte"
:/ indegree$1i' 5) and =utDegree$1i'5>
InDegree(A) =1OutDegree(A) =2
InDegree(B) =1OutDegree(B) =1
InDegree(C) = 1OutDegree(C) =1
InDegree(D) = 1OutDegree(D) =0
A
C B
D
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 26/84
Connected Gra!h ,Not a digra!h-Connected 3ertices 0 i/ there is a !ath /rom 1i to 1k
Connected Gra!h0 i/ /or e3ery !air o/ distinct 3ertices 1i2 1k in G2
there is a !athA
C B
D
4
A
C B
D
4
A Connected Graph Not a Connected Graph
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 27/84
Strongl* Connected . %ea/l* connected Gra!hs
Strongly connected0 :n a digra!h2 :/ there is a directed !ath /rom 1i to1k and also /rom 1k to 1i /or e3ery !air o/ distinct 3ertices 1i and 1k
Weakly connected0 :/ a digra!h is not strongly connected but theunderlying gra!h $without direction o/ edges ' is connected%
A
C
D
A
C
D
Stronglyconnected
weakly connected
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 28/84
Co#!lete Gra!h i/ e3ery node u in G is ad9acent to e3ery other node 3 in G%
i%e% "here are edges /rom any 3erte8 to all other 3ertices%
A com!lete gra!h with n nodes will ha3e n,n0-12 edges
A
C B
D
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 29/84
Re!resentation o( Gra!hs
)% Set Re!resentation
*% ?inked re!resentation
+% Se&uential $matri8' re!resentation
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 30/84
Re!resentation o( Gra!hs
03 Set Re!resentation "wo sets are maintained
)% Set o/ 3ertices
*% Set o/ edges 2 which is a subset o/ + @ +
:/ its a weighted gra!h2 then set 4 consist o/ + tu!les $% @ + @ +' Where w5 weight o/ the edge
:ts not !ossible to store !arallel edges because in a set 2 twoidentical elements cannot e8ist
4g0. 1 $G'5 6A2 B2 C2 D7
. 4$G' 56 $A2B'2 $A2C'2 $B2C'2 $C2D'7
*% ?inked re!resentation
+% Se&uential $matri8' re!resentation
A
C B
D
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 31/84
Re!resentation o( Gra!hs
)% Set Re!resentation
23 Lin/ed re!resentation use ad&acenc* list
.
"he number o/ lists de!ends on the number o/ 3ertices in the gra!h. "he list contains the ad9acent nodes o/ the header node
+% Se&uential $matri8' re!resentation
A
C B
D
B C 8
A C 8
A B D 8
C 8
A
B
C
D
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 32/84
Re!resentation o( Gra!hs
)% Set Re!resentation
*% ?inked re!resentation
53 Se6uential ,#atri"- re!resentation Ad&acenc* Matri" ost use/ul way Uses a matri8 o/ order < @ <2 where < is the number o/ 3ertices
4ntries in the matri8 can be decided as /ollowsa u 3 5 )2 i/ there is an edge /rom U to 1
5> 2 otherwise :n multigra!h2 instead o/ ) and >2 the entries would be the
number o/ edges between two 3ertices :n the case o/ weighted gra!h2 the entries would be the weight o/
each edge 4g0
A
C B
D
A 7 C D
A > ) ) >
7 ) > ) >
C ) ) > )
D > > ) >
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 33/84
Re!resentation o( Gra!hs
)% Set Re!resentation
*% ?inked re!resentation
53 Se6uential ,#atri"- re!resentation Ad&acenc* Matri" ost use/ul way Uses a matri8 o/ order < @ <2 where < is the number o/ 3ertices
4ntries in the matri8 can be decided as /ollowsa u 3 5 )2 i/ there is an edge /rom U to 1
5> 2 otherwise :n multigra!h2 instead o/ ) and >2 the entries would be the
number o/ edges between two 3ertices
:n the case o/ weighted gra!h2 the entries would be the weight o/
each edge 4g0
A
C B
D
A 7 C D
A > ) ) >
7 ) > ) >
C ) ) > )
D > > ) )
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 34/84
Ad&acenc* Matri"
Dra89ac/s Dicult to insert and delete 3ertices
:/ number o/ edges is small ; memory wasted $s!arse matri8'
Ad'antages easy to mani!ulate
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 35/84
O!erations in a gra!h
Se6uential re!resentation ,Ad&acenc* Matri"-
Inserting in a gra!h1erte8 ; insert row and column /or that 3erte8 make
necessary changes
4dge ; !lace 0 at a!!ro!riate !osition in ad9acency matri8
Deleting /rom a gra!h1erte8 ( delete row and column /or that 3erte8 make
necessary changes4dge ; !lace : at a!!ro!riate !osition in ad9acency matri8
Tra'ersing a gra!h7;S ; breadth Erst searchD;S ; de!th Erst search
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 36/84
Tra'erse a Gra!h
Tra'ersing 'isiting all 'ertices e"actl* once
)% Start /rom a gi3en node
*% 1isit all nodes reachable /rom that starting node
"wo methods used0
53 7;S ; breadth Erst search
Similar to le3el by le3el tra3ersal o/ a tree Any 3erte8 in the le3el : will be 3isited only a/ter the 3isit o/ all
3ertices in its !receding le3el ie% i() akes use o/ a &ueue and a <ag to denote whether a node is
3isited or not
43 D;S ; de!th Erst search Similar to inorder tra3ersal o/ a binary tree Starting /rom a gi3en node2 this tra3ersal 3isits all the nodes u!to
the dee!est le3el and so on% akes use o/ a stac/ and a <ag to denote whether a node is
3isited or not
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 37/84
Tra'erse a Gra!h
03 7;S ; breadth Erst search
Similar to le3el by le3el tra3ersal o/ a tree
4g0 A C 7 E D ; G
23 D;S ; de!th Erst search
Starting /rom a gi3en node2this tra3ersal 3isits all the nodes u!to the dee!est le3el
and so on%
4g0 A C D G 7 ; E
A
C B
D
4
G
F
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 38/84
BREADTH -FIRST SEARCH
Similar to le3el by le3el tra3ersal o/ a tree Any 3erte8 in the le3el : will be 3isited only a/ter the 3isit o/
all 3ertices in its !receding le3el ie% i() akes use o/ a 6ueue and a <ag to denote whether a node
is 3isited or not
B dth Fi t S h
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 39/84
Breadth First Search
%
A
flag=0
B
flag=0
C
flag=0
E
flag=0
F
flag=0
D
flag=0
G
flag=0
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 40/84
Breadth First Search
%
A
flag=1
B
flag=0
C
flag=0
E
flag=0
F
flag=0
D
flag=0
G
flag=0
front
Start at any node, say A
Enqueue the node
Change ts flag to !sted
A
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 41/84
Breadth First Search
%
A
flag=1
B
flag=1
C
flag=0
E
flag=0
F
flag=0
D
flag=0
G
flag=0
front
"e#o!e the front node, $%
Dsplay $
Add all the negh&ours of $
to the rear of the queue and
'hange ther flag%
B$=A
dsplay
A
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 42/84
Breadth First Search
%
A
flag=0
B
flag=1
C
flag=1
E
flag=0
F
flag=0
D
flag=0
G
flag=0
front
Add all the negh&ours of $
to the rear of the queue and
'hange ther flag%
B C$=A
dsplay
A
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 43/84
Breadth First Search
%
A
flag=0
B
flag=1
C
flag=1
E
flag=1
F
flag=0
D
flag=0
G
flag=0
front
Add all the negh&ours of $
to the rear of the queue and
'hange ther flag%
B C E$=A
dsplay
A
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 44/84
Breadth First Search
%
A
flag=0
B
flag=1
C
flag=1
E
flag=1
F
flag=0
D
flag=0
G
flag=0
front
$o #ore negh&ours
re#anng B C E$=A
dsplay
A
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 45/84
Breadth First Search
%
A
flag=0
B
flag=1
C
flag=1
E
flag=1
F
flag=0
D
flag=0
G
flag=0
front
"e#o!e the ne(t front node, $%
Dsplay $
But no un!sted negh&our for $ C E$=B
dsplay
A B
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 46/84
Breadth First Search
%
A
flag=0
B
flag=1
C
flag=1
E
flag=1
F
flag=0
D
flag=0
G
flag=0
front
$o #ore negh&ours fo node B C E$=B
dsplay
A B
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 47/84
Breadth First Search
%
A
flag=0
B
flag=1
C
flag=1
E
flag=1
F
flag=0
D
flag=1
G
flag=0
front
"e#o!e the front node, $%
Dsplay $
Add all the negh&ours of $
to the rear of the queue and
'hange ther flag%
E D$=C
dsplay
A B C
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 48/84
Breadth First Search
%
A
flag=0
B
flag=1
C
flag=1
E
flag=1
F
flag=0
D
flag=1
G
flag=0
front
$o #ore negh&ours E D$=C
dsplay
A B C
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 49/84
Breadth First Search
%
A
flag=0
B
flag=1
C
flag=1
E
flag=1
F
flag=0
D
flag=1
G
flag=1
front
"e#o!e the front node, $%
Dsplay $
Add all the negh&ours of $
to the rear of the queue and
'hange ther flag%
D G$=E
dsplay
A B C E
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 50/84
Breadth First Search
%
A
flag=0
B
flag=1
C
flag=1
E
flag=1
F
flag=0
D
flag=1
G
flag=1
front
$o #ore negh&oursD G$=E
dsplay
A B C E
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 51/84
Breadth First Search
%
A
flag=0
B
flag=1
C
flag=1
E
flag=1
F
flag=1
D
flag=1
G
flag=1
front
"e#o!e the front node, $%
Dsplay $
Add all the negh&ours of $
to the rear of the queue and
'hange ther flag%
G F$=D
dsplay
A B C E D
d h i h
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 52/84
Breadth First Search
%
A
flag=0
B
flag=1
C
flag=1
E
flag=1
F
flag=1
D
flag=1
G
flag=1
front
$o #ore negh&ours%G F$=D
dsplay
A B C E D
B d h Fi S h
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 53/84
Breadth First Search
%
A
flag=0
B
flag=1
C
flag=1
E
flag=1
F
flag=1
D
flag=1
G
flag=1
front
"e#o!e the front node, $%
Dsplay $
$o negh&ours
F$=G
dsplay
A B C E D G
B dth Fi t S h
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 54/84
Breadth First Search
%
A
flag=0
B
flag=1
C
flag=1
E
flag=1
F
flag=1
D
flag=1
G
flag=1
front
"e#o!e the front node, $%
Dsplay $
$o negh&ours
$=F
dsplay
A B C E D G F
B dth Fi t S h
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 55/84
Breadth First Search
%
A
flag=0
B
flag=1
C
flag=1
E
flag=1
F
flag=1
D
flag=1
G
flag=1
front
)ueue e#pty
*ra!ersal 'o#plete
$=F
Fnal result
A B C E D G F
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 56/84
7readth ;irst Search ,7;S-
)% :nitialise all nodes by setting their ag to not 'isited $>'*% Put the starting node into the 6ueue and change its ag to 'isited
$)'
+% while $&ueue not em!ty'03 re#o'e (ront node2 < /rom &ueue23 !rocess < $dis!lay <'+% add all the neigh9ours o/ < to the rear o/ the &ueue set
<ag='isited,% end while
-% sto!
Algorith# 7;S ,start 'erte"-
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 57/84
7readth ;irst Search ,7;S-)% :nitialise all nodes by settingtheir ag to not 'isited $>'
*% Put the starting node into the6ueue and change its ag to 'isited $)'
+% while $&ueue not em!ty'
03 re#o'e (ront node2 < /rom&ueue
23 !rocess < $dis!lay <'
+% add all the neigh9ours o/ <
to the rear o/ the &ueue set <ag='isited
,% end while
-% sto!
Algorith# 7;S ,start>'erte"-
)% i5)
*% while $iI5n'
)% set 3istedJagKiL5>
*% iMM
+% endwhile
,% set 3isitedJagKstartJ3erte8L5)
-% en&ueue$startJ3erte8'
.% while$rearN5>'
)% 85de&ueue$'
*% dis!lay 8
+% set i5)
,% while$iI5n'
)% i/ $ $aK8LKiL5)' $3isitedKiL5>''
)% 3isitedKiL5)
*% en&ueue$i'*% endi/
-% endwhile
O% endwhile
% sto!
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 58/84
DEPTH -FIRST SEARCH
Similar to inorder tra3ersal o/ a binary tree Starting /rom a gi3en node2 this tra3ersal 3isits all the nodes u!to
the dee!est le3el and so on% akes use o/ a stac/ and a <ag to denote whether a node is
3isited or not
De!th First Search
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 59/84
De!th First Search
%
A
flag=0
B
flag=0
C
flag=0
E
flag=0
F
flag=0
D
flag=0
G
flag=0
De!th First Search
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 60/84
De!th First Search
%
A
flag=1
B
flag=0
C
flag=0
E
flag=0
F
flag=0
D
flag=0
G
flag=0
dsplay
top
sta'+Start th any node, say A
Change ts flag
Push t to sta'+
A
De!th First Search
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 61/84
e! s Sea c
%
A
flag=1
B
flag=0
C
flag=0
E
flag=0
F
flag=0
D
flag=0
G
flag=0
dsplay
top
sta'+Pop the top node, $
Dsplay $
Push nto sta'+ all the negh&ours
of $ and 'hange ther state
A
$=A
De!th First Search
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 62/84
!
%
A
flag=1
B
flag=0
C
flag=1
E
flag=0
F
flag=0
D
flag=0
G
flag=0
dsplay
top
sta'+CPush nto sta'+ all the negh&ours
of $ and 'hange ther state
A
De!th First Search
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 63/84
!
%
A
flag=1
B
flag=0
C
flag=1
E
flag=1
F
flag=0
D
flag=0
G
flag=0
dsplay
top
sta'+E CPush nto sta'+ all the negh&ours
of $ and 'hange ther state
A
De!th First Search
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 64/84
!
%
A
flag=1
B
flag=1
C
flag=1
E
flag=1
F
flag=0
D
flag=0
G
flag=0
dsplay
top
sta'+B E CPush nto sta'+ all the negh&ours
of $ and 'hange ther state
A
De!th First Search
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 65/84
!
%
A
flag=1
B
flag=1
C
flag=1
E
flag=1
F
flag=0
D
flag=0
G
flag=0
dsplay
top
sta'+B E C-sted all negh&ours
A
De!th First Search
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 66/84
!
%
A
flag=1
B
flag=1
C
flag=1
E
flag=1
F
flag=0
D
flag=0
G
flag=0
dsplay
top
sta'+E C
A B
Pop the top node, $
Dsplay $
Push nto sta'+ all the
negh&ours of $ and 'hange
ther state
$=B
De!th First Search
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 67/84
!
%
A
flag=1
B
flag=1
C
flag=1
E
flag=1
F
flag=0
D
flag=0
G
flag=0
dsplay
top
sta'+E C
A B
$o un!sted negh&ours
$=B
De!th First Search
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 68/84
!
%
A
flag=1
B
flag=1
C
flag=1
E
flag=1
F
flag=0
D
flag=0
G
flag=0
dsplay
top
sta'+C
A B E
Pop the top node, $
Dsplay $
Push nto sta'+ all the negh&ours
of $ and 'hange ther state
$=E
De!th First Search
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 69/84
%
A
flag=1
B
flag=1
C
flag=1
E
flag=1
F
flag=0
D
flag=0
G
flag=1
dsplay
top
sta'+G C
A B E
Push nto sta'+ all the negh&ours
of $ and 'hange ther state$=E
De!th First Search
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 70/84
%
A
flag=1
B
flag=1
C
flag=1
E
flag=1
F
flag=0
D
flag=0
G
flag=1
dsplay
top
sta'+G C
A B E
$o #ore negh&ours
$=E
De!th First Search
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 71/84
%
A
flag=1
B
flag=1
C
flag=1
E
flag=1
F
flag=0
D
flag=0
G
flag=1
dsplay
top
sta'+C
A B E G
Pop the top node, $
Dsplay $
But no negh&ours
$=G
De!th First Search
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 72/84
%
A
flag=1
B
flag=1
C
flag=1
E
flag=1
F
flag=0
D
flag=0
G
flag=1
dsplay
top
sta'+C
A B E G
$o #ore negh&ours
$=G
De!th First Search
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 73/84
%
A
flag=1
B
flag=1
C
flag=1
E
flag=1
F
flag=0
D
flag=0
G
flag=1
dsplay
top
sta'+
A B E G C
Pop the top node, $
Dsplay $
Push nto sta'+ all the negh&ours
of $ and 'hange ther state
$=C
De!th First Search
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 74/84
%
A
flag=1
B
flag=1
C
flag=1
E
flag=1
F
flag=0
D
flag=1
G
flag=1
dsplay
top
sta'+D
A B E G C
Push nto sta'+ all the negh&ours
of $ and 'hange ther state $=C
De!th First Search
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 75/84
%
A
flag=1
B
flag=1
C
flag=1
E
flag=1
F
flag=0
D
flag=1
G
flag=1
dsplay
top
sta'+D
A B E G C
$o #ore negh&ours
$=C
De!th First Search
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 76/84
%
A
flag=1
B
flag=1
C
flag=1
E
flag=1
F
flag=0
D
flag=1
G
flag=1
dsplay
top
sta'+
A B E G C D
Pop the top node, $
Dsplay $
Push nto sta'+ all the negh&ours
of $ and 'hange ther state
$=D
De!th First Search
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 77/84
%
A
flag=1
B
flag=1
C
flag=1
E
flag=1
F
flag=1
D
flag=1
G
flag=1
dsplay
top
sta'+F
A B E G C D
Push nto sta'+ all the negh&ours
of $ and 'hange ther state$=D
De!th First Search
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 78/84
%
A
flag=1
B
flag=1
C
flag=1
E
flag=1
F
flag=1
D
flag=1
G
flag=1
dsplay
top
sta'+F
A B E G C D
$o #ore negh&ours
$=D
De!th First Search
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 79/84
%
A
flag=1
B
flag=1
C
flag=1
E
flag=1
F
flag=1
D
flag=1
G
flag=1
dsplay
top
sta'+
A B E G C D F
Pop the top node, $
Dsplay $
But no negh&ours%
$=F
De!th First Search
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 80/84
%
A
flag=1
B
flag=1
C
flag=1
E
flag=1
F
flag=1
D
flag=1
G
flag=1
dsplay
top
sta'+
A B E G C D F
$o #ore negh&ours
$=F
De!th First Search
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 81/84
%
A
flag=1
B
flag=1
C
flag=1
E
flag=1
F
flag=1
D
flag=1
G
flag=1
Fnal result
top
sta'+
A B E G C D F
Sta'+ e#pty
*ra!ersal 'o#plete $=F
De!th ;irst Search ,D;S-
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 82/84
! , -
)% initialise all nodes by setting their ag to not 'isited $>'*% !ush the starting node into the stac/ and change its ag to
'isited $)'
+% while $stac/ not em!ty'03 re#o'e to! node2 < /rom stack23 !rocess < $dis!lay <'+% add all the neigh9ours o/ < to the to! o/ the stack set
<ag='isited,% end while
-% sto!
De!th ;irst Search,D;S-
Algorithm DFS $startJ3erte8'
)% i5)
* while $iI5n'
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 83/84
,D;S-
)% initialise all nodes by settingtheir ag to not 'isited $>'
*% !ush the starting node into thestac/ and change its ag to 'isited $)'
+% while $stac/ not em!ty'03 re#o'e to! node2 <
/rom stack
23 !rocess < $dis!lay <'
+% add all the neigh9ours o/ < to the to! o/ the
stack set <ag='isited,% end while
-% sto!
*% while $iI5n'
)% set 3istedJagKiL5>
*% iMM+% endwhile
,% set 3isitedJagKstartJ3erte8L5)
-% !ush$startJ3erte8'
.% while$to!N5>'
)% 85!o!$'
*% dis!lay 8+% set i5)
,% while$iI5n'
)% i/ $ $aK8LKiL5)' $3isitedKiL5>' '
)% 3isitedKiL5)
*% !ush$i'
*% endi/
-% endwhile
O% endwhile
% sto!
Co#!are algorith#s
7/18/2019 6.mod 4-6 Graphs
http://slidepdf.com/reader/full/6mod-4-6-graphs 84/84
7;S
)% initialise all nodes by setting theirag to not 3isited $>'
*% !ut the starting node into the 6ueue and change its ag to 3isited $)'
+% while $6ueue not em!ty'
)% remo3e (ront node2 < /rom&ueue
*% !rocess < $dis!lay <'
+% add all the neighbours o/ < to therear o/ the 6ueue setag53isited
,% end while-% sto!
D;S
)% initialise all nodes by setting theirag to not 3isited $>'
*% !ush starting node into the stac/
and change its ag to 3isited $)'+% while $stac/ not em!ty'
)% remo3e to! node2 < /rom stack
*% !rocess < $dis!lay <'
+% add all the neighbours o/ < to theto! o/ the stac/ set
ag53isited,% end while
-% sto!