Lecture 11
Lecture 11
Definition of Graphs and Related Concepts Representation of Graphs The Graph Class Graph Traversal Graph Applications
CS 103 1
Definition of Graphs
A graph is a finite set of nodes with edges between nodes or!all"# a graph G is a str$ct$re %&#'( consisting of
) a finite set & called the set of nodes# and ) a set ' that is a s$bset of &*&+ That is# ' is a set of pairs of the for! %*#"( where * and " are nodes in &
CS 103 2
Examples of Graphs
&,-0#1#2#3#./ ',-%0#1(# %1#2(# %0#3(# %3#0(# %2#2(# %.#3(/
0 1 2 . 3 CS 103 0hen %*#"( is an edge# we sa" that * is adjacent to "# and " is adjacent from *+ 0 is ad1acent to 1+ 1 is not ad1acent to 0+ 2 is ad1acent fro! 1+ 3
To! CS 103
7a$l .
Graph Representation
or graphs to be co!p$tationall" $sef$l# the" have to be convenientl" represented in progra!s There are two co!p$ter representations of graphs3
) Ad1acenc" !atri* representation ) Ad1acenc" lists representation
CS 103 2
CS 103
>
CS 103
7a$l 8
CS 103
Cons3
) Eo !atter how few edges the graph has# the !atri* taDes F%n2( in !e!or"
CS 103 10
CS 103
11
CS 103
12
Cons3
) =t can taDe $p to F%n( ti!e to deter!ine if a pair of nodes %i#1( is an edge3 one wo$ld have to search the linDed list G@iA# which taDes ti!e proportional to the length of G@iA+
CS 103 13
To! CS 103
Ad1acenc" Gists3
) G@iA is the linDed list containing all the neighbors of i
CS 103 1B
Example of Representations
GinDed Gists3 G@0A3 1# 2# 3 G@1A3 0# 2# 3 G@2A3 0# 1# 3 G@3A3 0# 1# 2 [email protected] 8 G@8A3 .
5ar" 0 4ohn 2 To! . Ad1acenc" 5atri*3 0 1 1 1 0 0 1 0 1 1 0 0 A, 1 1 0 1 0 0 1 1 1 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 CS 103 1C 6elen 1 4oe 3 7a$l 8
CS 103
21
!aths
A path in a graph G is a seQ$ence of nodes *1# *2# ?#*D# s$ch that there is an edge fro! each node the ne*t one in the seQ$ence or e*a!ple# in the first e*a!ple graph# the seQ$ence 3# 0# 1# 2 is a path# b$t the seQ$ence 0# 3# . is not a path beca$se %0#3( is not an edge =n the :sibling<of; graph# the seQ$ence /ohn0
Mary0 /oe0 1elen is a path# b$t the seQ$ence 1elen0 %om0 !aul is not a path
CS 103 22
Graph "onnecti'ity
An $ndirected graph is said to be connected if there is a path between ever" pair of nodes+ Ftherwise# the graph is disconnected =nfor!all"# an $ndirected graph is connected if it hangs in one piece
Disconnected CS 103
Connected 23
"onnected "omponents
=f an $ndirected graph is not connected# then each :piece; is called a connected co!ponent+
) A piece in itself is connected# b$t if "o$ bring an" other node to it fro! the graph# it is no longer connected
=f the graph is connected# then the whole graph is one single connected co!ponent Ff =nterest3 Given an" $ndirected graph G#
) =s G connectedR ) =f not# find its connected co!ponents+
CS 103
2.
Depth-5irst ,earch
D S follows the following r$les3
1+ Select an $nvisited node *# visit it# and treat as the c$rrent node 2+ ind an $nvisited neighbor of the c$rrent node# visit it# and !aDe it the new c$rrent nodeH 3+ =f the c$rrent node has no $nvisited neighbors# bacDtracD to the its parent# and !aDe that parent the new c$rrent nodeH .+ Repeat steps 3 and . $ntil no !ore nodes can be visited+ 8+ =f there are still $nvisited nodes# repeat fro! step 1+ CS 103 2>
Illustration of D5,
0 0 . 2 8 2 > B D S Tree C 11 10 1 C 10 11 2 Graph G 8 > B 2 1 .
CS 103
2B
Implementation of D5,
Fbservations3
) the last node visited is the first node fro! which to proceed+ ) Also# the bacDtracDing proceeds on the basis of Tlast visited# first to bacDtracD tooT+ ) This s$ggests that a stacD is the proper data str$ct$re to re!e!ber the c$rrent node and how to bacDtracD+
CS 103 2C
CS 103
30
CS 103
32
JJ 7$t this before dfs%?( JJ ret$rns the left!ost $nvisited int t,S+peeD% (H int ",getEe*tUnvisitedEeighbor% JJ neighbor of node t+ =f none JJ re!ains# ret$rns n+ t#G#visited#n(H int getEe*tUnvisitedEeighbor%int t# if %"Ln(graph G# &ool visited@A#int n(visited@"A,trueH for %int 1,0H1LnH1MM( S+p$sh%"(H if %G+is'dge%t#1( NN Ovisited@1A( parent@"A,tH return 1H / JJ if no $nvisited neighbors left3 else S+pop% (H return nH / / JJ7$t this before dfs%?(+ This ret$rns the ne*t $nvisited node# or n otherwise int -et)ext*n'isited3&ool visited@A#int n# int last&isited(int 1,last&isitedM1H +hile %visited@1A NN 1Ln( 1MMH return 1H / -
CS 103
33
Breadth-5irst ,earch
S S follows the following r$les3
1+ Select an $nvisited node *# visit it# have it be the root in a S S tree being for!ed+ =ts level is called the c$rrent level+ 2+ ro! each node W in the c$rrent level# in the order in which the level nodes were visited# visit all the $nvisited neighbors of W+ The newl" visited nodes fro! this level for! a new level that beco!es the ne*t c$rrent level+ 3+ Repeat step 2 $ntil no !ore nodes can be visited+ .+ =f there are still $nvisited nodes# repeat fro! Step 1+ CS 103 3.
Illustration of B5,
0 0 1 8 2 > B 2 C 11 . 10 C 10 11 Graph G 2 8 > B 2 1 .
S S Tree
CS 103
38
Implementation of D5,
Fbservations3
) the first node visited in each level is the first node fro! which to proceed to visit new nodes+
This s$ggests that a Q$e$e is the proper data str$ct$re to re!e!ber the order of the steps+
CS 103
32
CS 103
3>