0% found this document useful (0 votes)
7 views18 pages

Graph Theory

A tree is a connected acyclic graph, characterized by having one unique path between any two vertices. Theorems presented include properties of trees such as the existence of at least one vertex of degree one in nontrivial trees, the relationship between vertices and edges, and the concept of pendant vertices. Additionally, the document discusses rooted trees, m-ary trees, and binary trees, detailing their structures and properties.

Uploaded by

Devansh Garg
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)
7 views18 pages

Graph Theory

A tree is a connected acyclic graph, characterized by having one unique path between any two vertices. Theorems presented include properties of trees such as the existence of at least one vertex of degree one in nontrivial trees, the relationship between vertices and edges, and the concept of pendant vertices. Additionally, the document discusses rooted trees, m-ary trees, and binary trees, detailing their structures and properties.

Uploaded by

Devansh Garg
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/ 18

267

Graph Theory

Properties of Trees
Trees and
6.13

Atree is
connected graph without any cycle. For example, the graphs in Fig. 6.37 are
a
two, three, four and five vertices. Atree must be a simple graph because self-
with one,
trees
parallel edges,
both form cycles. Agraph is called acyclic if it contains no cycles.
and

a connected acyclic graph. Acollection of trees is called a forest.


loops

treeis
Thus a

Fig. 6.37 :Trees with atmost five vertices


Atree is called trivial if it has only one vertex.
Theorem 6.23 Agraph Gis a tree iff there is one and only one path between every pair
of vertices in it.
Proof. Let Gbe a tree, then it is conneçted and there is at least one path between every
pair of vertices in it. Suppose that between two vertices u and v in G, there are two distinct
paths, then their union will contain a cycle, contradicting the fact that G is a tree. Hence there
IS One and only one path between every pair of vertices in a tree.
Conversely, let there is one and only one path between every pair of vertices inG, then,
Ocourse, Gisconnected. If G contains a cycle then there is a pair of
vertices u and v in G such
uat there are two distinct paths between them, contradicting
only one path between the fact that there is one and
0^ a tree. every pair of vertices in G. Thus G does not contain any cycle and hence it
Theorem 6.24 ln every nontrivial tree
Proof, Let Tbe a tree there is at least one vertex of degree one.
(u) is not with twO or more vertices. Choose any vertex, say v n T. If deg
vertex v,. If one,degthen it is adjacent to some vertex, say Uy in T, so traverse along an edge to the
and v3,and so (D2) is not one, continue traversing to a vertex v, if there is an edge between v
on.
would have a Since T is a tree, none of the vj's is repeated in this traversing, otherwise it
vertex is of cycle. Since Tis finite, this traversing
degree one, because we enter this must end to some vertex, say u,. Then this
vertex but cannot exit it.
268 Elements of Discrete
Mathemattes
Theorem 6.25 Atree twith n vertices has exactly (n - 1) edges.
Proof. We shail prove the statement by induction on the number of vertices n. A
tree
with one vertex is the trivial tree, whích has zero edges. So it is true for n =1. As an
induction
hypothesis, assume for some nuínber k> 1, that every tree with k vertices has exacty
edges. Now consider any tree T with (k + 1) vertices, then by the above theorem T contains (k- 1)
a
vertex of degree one, say v. Then the graph T-v is acyclic since deletingu from an
graph cannot create a cycle. Moreover, T-v is connected, since the vertex vhas degree oneacyclic
in T
Thus, T-0 is a tree with k vertices, and hence it has exactly (kk - 1) edges by indution
assumption. But, T-v is obtained by deleting the vertex vof degree one, so T - has one
edge
fewer than T. Hence Thas kedges. This completes the proof.
Corollary
Let agraph Gbe a forest with nvertices and kconnected components. Then Ghas
edges.
Proof. Let kcomponents of Gbe denoted by G, Gzm, G and suppose that each G.has .
vertices for i= 1, 2,.k. Then n=n, + t..+ n
Now each G, is a tree since a forest is a collection of trees, and its,connected components
are trees only. Thus each G, has ny -1 edges. Hence total number of edges in Gis given be
(n,-1) +(12-1) +.+(-1) =(4, + t..it ni)-k
=1-k
Theorem 6.26 Agraph Gis a tree iff it is minimally connected.
Proof. Aconnected graph is said to be minimally connected if removal of any one edge
from it disconnects the graph.
Let Gbe atree and suppose that it is not minimally connected. Then there must be an
edge, say e in G such that G -e is connected. It implies that e must be in
some cycle in G,
contradicting the fact that G is a tree without any cycle. Hence the tree G must be minimally
connected.
Conversely, suppose that G is a minimally connected graph. Then it can not have a
cycle, otherwise by deleting one of the edges in the cycle, we obtain a
Thus G is a tree. graph still connected.
Pendant vertices in a Tree
Apendant vertex is defined as a vertex of degree one. Every tree has several
vertices. To arrive at this conclusion, we find that every pendat
tree with n vertices has presey
edges. Therefore 2(n - 1)
vertex carn have degree zero.degrees are to be divided among n vertices. As atree is connected, no
nontrivial tree. Hence we must have at least two vertices of degree one each in a

Theorem 6.27 Every nontrivial tree contains at least two pendant vertices.
Proof. Suppose that Tis atree with n vertices, and let d, d,., d, denote the degrees of
its vertices and are
ordered so that di Sdz S...s d,. Since Tis a nontrivial connected tree,
G r a p hT h e o r y
269

21,for each i =1, 2,., n. If T contains only one pendant vertex then d, l and d, 22
,
therefore
Thus
3..,#.
forie2

E4, 21+2(n-1) =2n-1 .(1)


However, we have
EM
d, =2(n-1)=21-2 ...2)

contradicts
the inequality (1).
which
Tcontains at least two
pendant vertices.
Hence

Distance
between two vertices in a Graph
graph Gthe distance d(u, v) between two of its distinct vertices uand v, is
In a connected
the. shortest u-v path.
length of
the of the u -v paths between the vertices uand vin the following
example, some
For
ucvfusho, ucvyevsgVsh, ucvfosjv,lu, ucvevsgvgjv,lu, ubvdugevfu_hv. But the
graph are which is of length 3. Hence d(u, v) = 3.
ucvsfushu,
shortestpath is pair of vertices, so it becomes much
t atree, there is precisely one path between any
vertices.
distance between any two of
its
oasier to determine the
k m V10
a

p
e Ve
b
h

V Vg Vg
V d

three
Fig. 6.38 : Distance between u and v is
Eccentricity cf a vertex
eccentricity E(v) of avertex in
v a graph G=(V, E)is the distance of the farthest
The
vertex v, from vin G. Thus
E(v) = max (d(u,v) lv;e V}.
Center of aGraph A
called the center of G.
The vertex in the graph Ghaving minimum eccentricity is center. However,
graph may have many centers. For example, in a cycle graph every vertex is a
atree has one or two centers
only.
Theorem 6.28 Every tree has either one or two centers.
of T. If Thas two
Proof, Let T"be atree. If T has only one vertex, then it is the center vertices. Since
vertices, then both are the centers of T. Now Suppose that T has more than two
the maximum occurs only when
v; is a
distance other vertex v, the
pendant vertex and from a given vertex vto any
tree has at least two pendant vertices, therefore,
if we delete all
every
Elements of
270 Discrete
Mathematies
pendant vertices from the tree T, the resulting graph T; is still a tree in which the eccentricities
vertices that are the
of all the vertices are reduced by one. Therefore, all the centers of T.
remain the centers in T1. Again we delete all the perndant vertices in TË to get another still
that has same centers as that of T1. We continue the process of deletion of pendant tree T
vertices
we are left with either one vertex (which is the center of T) or an edge (whose end
until
the centers of T). This completes the proof.
vertices are
The deletion process of pendant vertices in a tree T is shown in the following figure.
The numbers associated with the vertices denote their eccentricities.

a(7) b(7) J6) k(6)


I(6) p(7)
c(6) i(6) h(5)
O(6)
f(4) m(5)
d(5) e(4)
g(5) n(6) ba7)
T

C(5) h(4)

O(5)
d(4) e(3) f(3) m(4) d(3) e(2) f(2) m(3)
T2

e(1) f(1)
two centers e and fofG
T3
Fig. 6.39: Tree centers
Radius and Diamneter of a Tree
In a tree, the eccentricity of the center (or centers) (i.e., the distance from the center o
the tree to the farthest vertex) is said to be the radius of the tree.
The diameter of a tree Tisdefined as the length of the longest path in T.
The radius of the tree Tin Fig. 6.39 is thus 4 and the diameter in 7.
Rooted Tree
of a
In many trees a particular vertex of the tree is designated as the root. Once a root
tree is specified, the direction of each edge can be assigned. Since there is only one path from
the root to each vertex of the tree, we direct each vertex away from the root. Thus a tree

together with its root intends a directed graph called the rooted tree. from all the
Thus in a rooted tree one root is distinguished
particular vertex called the the tree is
other vertices. To differentiate the from nonrooted trees, the root of
rooted tree
marked distinctly by enclosing a small triangle.
271
Graph Theory

Fig. 6.40:Rooted trees


The terminology often used with rooted tree has botanical and genealogical origins.
Suppose that T is a rooted tree. If a vertex u of T other than the root is adjacent to the vertex v
and lies in the level below u, then v is called a child of uand u is called the parent of v.
Vertices having the same parent are called siblings. The ancestors of avertex other than the
root are the vertices in the path formn the root to this vertex. All these vertices that have a
vertex v as an ancestor, are called descendants of v. A vertex in a rooted tree without any child
is called a leaf. All these vertices that have children are called the internal vertices. The root
ôf a rooted tree T is an internal vertex unless it is the only vertex in T, in which case it is a leaf.
Subtree
A subtree of a rooted tree T, induced by avertex v as its root, is a subgraph of T,
consisting of v and its descendants and all edges incident to these descendants.

b
k

Fig. 6.41:Rooted tree and its subtree, rooted at the vertex d


In the Fig. 6.41, the vertex c is a parent of both f and g. The
children of c and so are siblings. The vertex j is descendant of c and c is vertices f and g are the
an ancestor of i (since the
path c’f’j lies below c).
m-ary Tree
A rooted tree T is said to be an m-ary tree if
every internal vertex of T has at most m
children. A tree T is said to be a complete m-ary tree if
or no children. every vertex in T has exactly m children
Ordered Rooted Tree
An ordered rooted tree is a rooted tree, in which
are ordered. In ordered rooted three., the children the children of each internal vertex
from left to of the internal vertices are drawn ordered
right.
272 Elements of Discrete
M
Mathematies
Binary Tree
A binary tree is a tree in which
there is exactly one vertex of degree Level 0
two and each of the remaining vertices Level 1
is of degree one or three. The vertex of
Level 2
degree two is distinct from all other
Level 3
vertices and therefore it is the root of
Level 4
the binary tree. Thus every binary tree
is a rooted tree. For example, the tree
shown in Fig. 6.42 is a binary tree. Fig, 6.42 :A binary tree with 17 vertices and four
levels
In a binary tree with n vertices there is precisely one vertex of even degree and a
remaining (n - 1) vertices are of odd degrees. Since in a graph the number of vertices ofod
degrees is always even, (n - 1) is an even number. Consequently nis odd. Thus the numbat
vertices in a binary tree is always odd.
The number of pendant vertices in a binary tree with n vertices is n +1
Since there is
2
precisely one vertex of degree two, and if the binary tree consists of p pendant vertices. then
remaining (7-p-1) vertices are of degreethree, so that
p+3(n-p-1) +2 =2 (1-1) (since tree with n vertices has n -1 edges)
It follows,
n+1
2
A nonpendant vertex in a tree is called an internal vertex and these are

=p-1.
In abinary tree a vertex v is said to be at a level l, if vis at a distance l, from
the root of
the tree. The root of the binary tree is said to be at level
zero. There can be only one vertex at
level zero (.e., the root), at most 2', i.e., two vertices at level one, at
most 22, i.e., four vertices
at level two and so on. Thus the maximum number of
with n vertices is
vertices possible in a k-level binary ree
20 + 2' +...+ 2* 2n ..(1)
Ine max1mum level may of any vertex in a binary tree is called the height of the binary
tree.

From (1), we have


2k+1-1 2n
k 2(log, (n +1)-1]
Graph Theory 273

Thus the minimum possible height of an n-vertex binary tree is


min /max =| logz (n+1) -11, ..(.2)
where fxldenotes the smallest integer greater than or equal to x.
On the other hand,
max max =2 , ...(3)

since to construct an n-vertex binary tree such that the farthest vertex is as far as possible from
the root, we must have exactly two vertices at each level, and of course one at zero level.
For example, for n = 19, we have

level 0
level 0
level 1
level 1

level 2 level 2
level 3 level 3

level 4 level 4

level5

level6
level 7

level 8

level 9

min mx =f(log, 20) - 1]=4 max max =


19-1
-=9
2
Fig,6.43 : Two 19-vertex binary trees
Path Length of a Binary 'Tree
The path length of a binary tree is defined as the sum of the path lengths from the root
to all the pendant vertices. Thus the path length of the binary tree in
Fig.6.42 is
2+2+3 +4+4+4+4+4+4=31.
Theorem 6.29 A complete m-ary tree with i internal vertices has a total of inm + 1
vertices.
Proof. Let Tbe a compete m-ary tree with p vertices andiinternal vertices. Since every
aenal Vertex in a complete m-ary tree, has m children and every child has a unique parent
and also the root of the tree is not a
child, therefore p = im + 1.
274 Elements of Discrete
Mathematics
Theorem 6.30 IfT is a binary tree of height h and with n vertices, then
h+1snsz*-1.
Proof. Let n, (0 s ksh) denote the number of vertices in T at the kth level,
then
n =n
k=0
..(1)
Also since ng 21 for each k = 0, 1,..h, therefore

h (k =0, 1,., h)
n = 2k= 2h+1-1
k=0 k=0
..2)
From (1) and (2), we have
n s2h+1-1
Again since *..(3)

h+1 =) 1s) ny =1
k=0 k=0

therefore, h+1 sn
From (3) and (4), we get .(4)
h+1 sns2h+1 1.
Spanning trees
There are many subgraphs of a givern graph G. Some of
are interested in those trees which contain all the these subgraphs are trees. We
vertices of G. These are called spanning trees
oG. These tress are the largest among all the trees in G
with maximum number of edges, hence
these are also known as maxinal tree subgraphs.
Atree Tis called a spanning tree (maxi
mal tree of a connected graph G if T is a sub
graph ofG and it contains all vertices of G. For
example, the subgraph of Gshown in bold lines,
in Fig. 6A4 is a spanning tree of G.
G
A full spanning forest of a graph Gis
a
spanning forest consisting of a collection of
trees, such that each tree is a
spanning tree of a
differentcomponent of G. Fig. 6.44 : Spanning tree of the graph G
Theorem 6.31
Agraph G is
Proof. Let us first assume thatconnected if and onlygraph
Gis aconnected if it has
witha spanning
nvertices tree.
and eedges. Then
e2n-1. If e =n-1, thern Gis atree and so we can say that T=G is a spanning tree of G. But if
e>n-1, then Gis not atree. Therefore it: must contain acircuit. Let e, be an edge in this circuit.
275
Graph Theory

Deleting this edge e from G, the remaining graph G-e, is connected that has n vertices and
e-1edges. Ife-1 =n -1, thenG-e, is a tree and so T =G-e, is a spanning tree of G. But if
e-1>n-1, then G-e, is not atree and so it must have some circuit. Deletion of an edge e, from
his circuit, gives us a subgraph G- (e, eg) which is connected and has n vertices and e -2
dces We continue to delete edges from Guntil we are leftwith e- (e-n + 1)edges, arnd get a
connected subgraph TofGthat has nvertices and e-(e-n+1), ie, n-1 edges. This shows that
Tis a tree. Since Thas the same vertices as that in G, itis a spanning tree of. G.
Conversely, let Tbe a spanning tree of G. Suppose that uand v are any two arbitrary
vertices in G. Then u and vare the vertices of Talso. Since Tis a tree, uand vare connected by a
path in T. Consequently, uand vare connected by apath in Gand so Gis connected.
Above theorem shows that finding a spanning tree of aconnected graph Gis simple,
because if Ghas no circuit, it is already a spanning tree and if it has a circuit then deleting an
edge from the circuit leave that graph still connected. Repeating this operation till a circuit
free graph is obtained containing all the vertices of G. An edge in a spanning tre Tis called a
branch of Tand an edge of Gthat is notin its spanning tree T is called a chord with respect to T.
So a connected graph G is aunion of two subgraphs consisting of branches and chords
respectively with respect to a spanning tree T. It iseasy to see that if Ghas n vertices and e
edges then there are (n - 1)tree branches and (e-n + 1) chords. If G is disconnected with k
components then it has a spanning forest consisting of k spanning trees. We define two
important numbers as
(1)Rank of a graph G=n-k = number of branches in any spanning tree or forest
of G,
(2) Nullity of agraph G=e-n +k= number of chords in G.
Thus we have,
number of edges in G= Rank of G+Nullity of G.
If Gis a connected graph,then k= 1 and then the rank of Gis (n -1), and the nullity of G
is (e-n+ 1).
Spanning Tree in a Weighted Graph
Let G be a connected weighted graph. Recall that in a weighted graph there is a real
number associated with its each edge, called the weight or length of the edge.
Then the weight ofa spanning tree Tof Gis defined as the sum of the weights of all the
edges (branches) in T. Various spanning trees of Gmay have different weights. Aspanning tree
with the smallest weight is called a shortest or minimal spanning tree. However, there may be
several spanning trees with the smallest weight in a weighted graph.
There are several methvds available for finding a shortest spanning tree in a given
weignted graph. We shall explin two such algorithms which are due to Kruskal and Prim.
276 Elements of Discrete Mathematics
Algorithm for Shortest (minimal) Spanning Tree
One algorithm due to Kruskal is as follows:
(1) Kruskal's Algorithm
Let G be a weighted connected graph. First list all edges of the graph G in order at
nondecreasing weights. The algorithm suggests us to first select an edge of the
weighted graph G, that has the smallest weight among all the edges of G which are connected
not loops.
Again avoiding loops, we choose from the remaining edges, the one, having the smalles
weight and which does not form acircuit with the edge already chosen. We repeat the
taking edges having smallest weight among those not already chosen and not forming aprocess
cireuit
with those edges that have already been chosen. The process is
terminated after having chosen
n-1 edges (if the graph has n vertices). These edges form an acyclic subgraph
T of G, which is
minimal spanning tree of G. The whole procedure consists of three steps given below:
Step 1. Choose e, an edge of the weighted graph G, such that e
has the smallest
weight we;) and is not a loop.
Step 2. If the edges ej, e., e, have been chosen, then
choose the edge e;, that has not
been chosen so far, such that
(i) the induced subgraph of G, having the edges e,,
(ü)we.) is the smallest weight associated with
ez,ei1 iS acyclic and
the edge ej among all the weights
associated with edges not already chosen.
Step 3. If Ghas nvertices, then stop after
having chosen n -1 edges. Otherwise repeat
step 2.
Example 6.26 The air line distances (in hundred of
C, D, Eand F are given in the following kilometers) between six cities A, B,
tables:
A B D F

A 56 35 2 51 60
B 56 21 57 78 70
C 35 21 36 68 68
D 2 57 36 51 61
51 78 68 51 13
F 60 70
68 61 13
Find the minimal spanning tree in the graph G.
where the table deternne
weighted (complete) graph G with vertices A, B, C, D,
E and F.
Sol. The minimal spanning tree of the graph Gis shown in the following Fig. o4
277
c a p hT h e o r y

A
A

60 56 60 56
56
60 51 35 61 35
34
B 70 2
70 B 702
13 68 21 13 68 |21
21 78 78
78
68 68 68
13
61 61
68 51 36 51 36
61 57 36
5
D D
D
A A

56 60 56
60
56
51 35 51/ 35\
u
3 B 70
B 770
702 21 13 78 68 |21 -
|21 ’ 13 78 68
13 18 68 68
C E C
C 68 61
61 57
36
36 51
36 51
51
D
D

A
B

51 35 21
13
2
E
D

Pig. 645:Construction of minimal spanning tree using Kruskal's algorithm


(2) Prim's Algorithm
and choose one of
In this
algorithm we first choose a vertex U, of the connected graph G
the edges having the smallest weight, which is not aloop and which is incident with vy, say
e1= (v, val. Now we choose an edge of smallest weight in G, which is incident with either v, or
D but at the other end has neither of these two vertices. For example, we choose the edge
Elements of Discrete Mathematies
278

where i e (1, 2), but vy D or t2. We repeat the process of Choosing the edoeet
e= lo. Da), the end vertices of the edge is a vertex that has
weight with the property that one of
smallest first time, until alln vertices of
chosen and the other end vertex appears for the
been previously
connected by edges. The resultng subgraph is a tree and so a spanning tree r
Ghave been
following four teps:
whole algorithm involves the
of G.
Step 1. Choose any vertex v
that v, # VË and e, has the small..
Step 2. Choose an edge ej = (, D2) 0f G such
incident with U.
weight among all the edges
Step 3. If edges e eg, e, have been Chosen nvolvng end vertices Uj, V2,., Viu1, then
such that e.,hae
choose an edgee1= {o,}, with v;e (v1, V2,Vit1) and v; E {U1, V2, Vil),
smallest weight among all the edges incident with the vertex in the vertex set (u,, D,.. D. .)
Step 4. If n -1edges have been chosen, thern stop. Otherwise go to step 3.
Example 6.27 Obtain a minimal spanning tree of the following weighted graph.

3
B

3 3

Sol. We perform the Prim's algorithm, indicating at each stage, the edges chosen, by
heavy lines A

2
2 2

3
B B
3 4
3 3
3
C C
1 1
2 2
2

D D
(Vertex V, =C)
(e, ={C, D}, V, =D) (e, ={D.A). V, =A)
Graph Theory A A

B
4 3 3 3
3 4

E E
4
1
2 2
2

D
D
(e, {A,F}, V; =F) (e, =(F.E). V, =E)
(e, ={A, B}, V, = B) 2 1

D
Fig. 6.46 : Construction of minimal spanning tree using the Prim's algorithm
Depth-First Search
One of the powerful techniques for solving many graph theory problems is depth-first
search. Recall that a spanning tree of a graph G is a tree that is a subgraph of Gand has
precisely the same vertex set V of G. Also recall that G has a spanning tree if and only if G is a
connected graph. There are many graph theory algorithms that require a systematic method of
traversing the graph. One such algorithm is depth-first search.
Let G=(V,E) be agraph with V= (v1 Vyy) that is represented by its adjacency list.
Without loss of generality we can assume that in the adjacency list of a given vertex v, the
vertices adjacent to vare listed in increasing order of their subscripts. In the depth-first search
of the graph G, the vertex that is currently visited is called the active vertex. We begin a
depth-first search of G by selecting vË as the first active vertex and assign it label 1. Next we
Select avertex that is adjacent toland assign it label 2.This vertex now becomes the new actve
Vertex. We place edge joining the vertices labelled l and 2in a set S.
n general, let r denote the label of the current active vertex in the search, and let not
all vertices in G, containing r have been visited, then we proceed as follows : If there are
Vertices adjacent to r, then select the first vertex on the adjacency list of r, that nas
not been visited and assign it, the next available label. The vertex just labelled now becomes
S.
ve vertex, The edge joining Yand this new active vertex is now placed in the set
280 Elements of Discrete Mathematics

On the other hand if all vertices adjacent to r have been visited, we revisit the vertex that .
the active vertex before r was first visited, and designate as the current active vertex. W
repeat this process until every vertex in G has been visited. If all the vertices of Ghave
been visited then a vertex not yet visited so far is selected as the next active vertex and sh
process is continued.
The label assigned to a vertex vin Gby the depth-first search is called the
depth-first
search index of v and is denoted by dfi (v). After the depth-first search of Gis completed. he
number df (o) is the order in which vwas first visited during the search. Everyedge of Gin s
is the join of two vertices, one of which is being visited for the first time. < S> is called .,
depth-first search forest and is a spanning forest of G. If Gis connected, then <S > is a spannin
tree and iscalled a depth-first search tree.
The Fig. 6.47(6) displays the sequence of steps while applying the depth-first search t
the graph G in Fig. 6.47(a).
V

V
V6

Fig. 6.47(a)

(i) Active vertex changes v to Vg

2 Va V6
(ii)
Active vertex changes from v, to vs
V

3 Ve 2 V4 V6
(iii)
Active vertex changes from vs to v, (All vertices in the hrs
connected component of Gare visited, v, is the new active vertex).
1 V

3
V6 2
281
Graph Theory

Active vertex changes from v, to v4


52
(ie)
2
3 60
Vs VA

Active vertex changes from y to v, V


1 5pV
(o)
2
3 6 7

is complete 4 V,
Fig. 6.47(b) : Depth-first search 6.48la). The depth
algorithm for the graph G is shown in Fig.
The depth-first search and again in Fig. 6.48(c),
where
shown in Fig. 6.48(b),
first search tree T is Vs
dfi (v)

Va 2
V
6
7 V6
3

V6 4

V 5 V4
VA

(a)
V5 V3

V7 V6

8 Vg

V V
(b) (c)
is drawn as a rooted tree. The back edges ofFig.G6.48
are shown in Fig. 6.48(c), and the index of every
vertex of Gis also given in Fig. 6.48(c).
282
Elements of Diserete
Mathemathes
Example 6.28 Apply the depth-first search algorithm to find a spanning tree
folloving graph G. for the

Sol. To start with the depth-first search algorithm we begin, arbitrarily


with he
vertex f. Apathf’g’h’k’j can be formed by successively adding edges incident uwk
vertices not already in the path (We can note
that other paths carn also be formed). We now
revisit (backtrack to) the vertex k. There is no
b
path beginning at k containing the vertices not
already visited, so we backtrack to h. Here we
form the path h ’ iand then revisit h and
then f. From fwe again form the pathf’ d’e
’c’a and then backtrack to c and build the
path c ’ b. We thus produce the following
spanning tre that is the depth search of the
graph G. Fig. 6.49
Breadth-First Search
In breadth-first search we visit systematically, the vertices of a graph or directed
graph G, beginning at some vertex v of Gwhich is called a root. The root is supposed to be the
hrst active vertex. At any stage during the search, all those vertices adjacent to the current
achve vertex are scanned, that have not yet been visited. Each time a vertex is visited tor the
frst time, we label it according to the rule that depends on the target to be achieved, and au
it to aback of queue. The current active vertex remains at the as
front of the queue. AS SOOl a
its adjacent vertices have been visited, it is deleted from the queue. If the queue is
empry
Some of the vertices of G have not vet been visited, then we select any unvisitea veriea
assign it a label and add it to the queue. The search is complete when all the vertices of the
graph Ghave been visited.
The vertices of a graph during breadth-first search can be labelled as follows. Suppose
that G1s a graph that is represented by its adiacency lists. As a first step all the veru
283
Graph Theory

by assigningr the label 1and place r on a queue Q. Next we delete


0. Now we begin
adjacent vertices (if they exist) in order in which they appear on the
labelled

are
and scan its
rfrom
list for
The first vertex appearing on the adjacency list for r is then assigned the
2, and this vertex is added to Q. We continue to label the vertices adjacent
adjacerncy

namely,
label.
the last vertex adjacent with r is labelled deg r +1 and is added to
them to Quntil
next
add
next vertex from Q, say w, and scan its adjacent vertices in the order in
and
to r
then delete the
Q We
appear on the
adjacency list of w. Ifa vertex adjacent with wstill has label 0, then
which they available label and add it to , otherwise we do not change its label.
We
it the next
we assign untilQis empty. When
all the vertices of Gare labelled, we stop.
this
search of the graph Gof Fig. 6.48(a) is shown in Fig. 6.50.
Continue
breadth-first
The Queue

Vye6
5 V6

2V2

V,64 36V3 7ovg

2V2 5Vs

VA

V,4 3043
Fig. 6.50
tree for the following
Example 6.29 Ayply breadth-first search to find aspanning
graph. b

f
de

G
m
Elements of Discrete
284 Mathematics
Sol. We select the vertex e as the starting vertex. Then we add all the four vertices b.
them label 1. Next we add the yerti
d, f and iin order that are adiacent with e and assign
Hence the vertices adjacent with
adjacent with these vertices at label 1 not already visited.
namely aand c are added, as are the vertices
h, g andËand k. Thus we have added the vertices
+h.
we add the vertices adjacent with these vertices
a, C, h, 8. jand k at the second step. Next
m.
have not been visited so far. This adds the vertices l and

b
d
k
b C h
g i ka m
a

G
Fig. 6.51: A breadth-first search of

You might also like