BM _Unit 5_Graph
BM _Unit 5_Graph
Graphs and Trees Graphs: Introduction, Definition; Initial & Terminal Nodes,
Adjacent Nodes; Directed Edge, Undirected Edge, Directed Graph (Digraph),
Undirected Graph, Mixed Graph; Loop (Sling); Distinct Edges, Parallel Edges; Multi-
graph, Simple Graph; Weighted Graph; Isolated Nodes, Null Graph; Isomorphic
Graphs; In-degree, Out-degree, Total-degree; Subgraphs; Reflexive, Symmetric,
Transitive Digraphs; Paths, Length of Path of a Graph; Simple Path (Edge Simple),
Elementary Path (Node Simple), Cycle (Circuit), Simple Cycle, Elementary Cycle;
Path of Minimum Length (Geodesic), Distance between Two Nodes, Triangle
Inequality; Reachability, Reachable Set of a Node, Connected Graphs: Strongly,
Unilaterally, Weakly Connected Graphs & Components;
Trees: Introduction, Definition, Root, Branch Nodes, Leaf (Terminal Node); Different
Representations of Trees; Forests, Subtrees; M-ary Tree, Full or Complete M-ary
Tree; Binary Tree, Full (Complete) Binary Tree; Conversion of M-ary Tree to Binary
Tree; Traversal of Binary Tree: Pre-order, In-order, and Post-order Traversal;
MOTIVATION
What is the common link between the following problems:
Traffic network design and cancer research?
Arranging marriages and scheduling flights?
Finding cure for mental illness, computer chip design, architectural
floor planning, fighting terror online?
Fighting epidemics, e-commerce, designing voting schemes, job
assignment, designing electrical networks, deciding on facility
location, etc. etc. etc.
1
GRAPH THEORY
4
ROLE OF GRAPH THEORY
Graph theory play an important role is several areas of computer
science such as
switching theory and logical design,
artificial intelligence,
formal languages,
computer graphics,
operating systems,
compiler writing,
information organization and retrieving and many more.
5
TOPICS TO BE DISCUSSED FOR INTRODUCTION TO GRAPH
Graphs: Introduction, definition, examples; Nodes, edges, adjacent
nodes, directed and undirected edge, Directed graph, undirected
graph, examples; Initiating and terminating nodes, Loop (sling),
Distinct edges, Parallel edges, Multi-graph, simple graph, weighted
graphs, examples, Isolated nodes, Null graph; Isomorphic graphs,
examples; Degree, In- degree, out-degree, total degree of a
node, examples; Subgraphs: definition, examples; Converse
(reversal or directional dual) of a digraph, examples;
DEFINITIONS AND FUNDAMENTAL CONCEPTS
As the name suggests Graphs can be represented
Graphically or diagrammatically, but this
representation is suitable in very simple cases.
Hence we have to use alternate ways to represent
graphs.
8
GRAPH (DEF.)
A graph G = < V , E, ø > consists of a nonempty set V called the set of
nodes (points, vertices) of the graph, E is said to be the set of edges of the
graph, and ø is a mapping from the set of edges E to a set of ordered (e.g.
<u,v>) or unordered (e.g. (u,v)) pairs of elements of V.
We have to assume that both the sets V and E are finite. The graph G can
be represented as <V,E>, or simply G.
9
In the diagrams
the directed
edges are shown
by means of
arrows which
also show the
directions
Adjacent Nodes: Any pair of nodes which are connected by an edge in a graph is
called adjacent nodes. i.e. Two vertices u and v are adjacent if they are connected
by an edge, in other words, unordered pair (u, v) or ordered pair <u,v> are
Adjacent nodes if an edge e joins them.
(e . g in following Fig. node 1 & 2 , 1 & 3 , 2 &3 are adjacent node)
2
Directed
edge
V1 V2
1 3 Directed
Un Directed edge
edge Directed graph
11
Directed Edge:
In a graph G=<V,E>, an edge which is associated
with an ordered pair of V X V is called directed
edge of G.
Undirected Edge :
2
In a graph G=<V,E>, an edge which is associated
with an unordered pair of nodes is called V1 v2
undirected edge.
Undirected edge
Digraph/Directed Graph : 1 3
1 1
3
3
4
4
Undirected parallel e=(a,b) Directed parallel Undirected parallel
( e = [1,2], e = [2,3], e =[2,2]) (e = [1,4] , e =[4,3])
Multigraph: Any graph which contain some parallel edges is called a multi graph.
Simple graph:
Simple graphs are graphs without multiple edges or self-loops.
(no more than one directed edge in the case of directed graph)
Weighted graph:
A graph in which weights are assigned to every edge is called Weighted Graph.
e.g. A graph representing a system of pipelines in which the weighted assigned
indicate the amount of some commodity transferred through the pipe is an
example of a weighted graph. Similarly, a graph of city streets may be assigned
weights according to the traffic density on each street.
V2
2
1
V1 V3
3
Multi Graphs
Weighted graphs
Isolated Node:
In a graph any node which is not adjacent to any
other node is called isolated node.
Here V3 is an isolated node.
2
Node In degree Out degree total degree
1 1 2 3
2 0 2 2
1 3 3 3 0 3
4 1 1 2
4
In case of an undirected graph, the total degree or the degree of a node v is
equal to the number of edges incident with v.
The concept of the degree of a node can be generalized to a set of nodes.
Let G= <V , E> be a directed graph and X ⊆ V be a subset of nodes.
The number of edges of G which have their initial node in X but their terminal
node not in X is called the out degree of X.
Similarly , the number of edges of G which have their terminal nodes in X but
their initial nodes not in X is called the indegree of X.
A simple result involving the notion of the degree of nodes of a graph is that
the sum of the degrees of all the nodes of a graph must be an even number
which is equal to twice the number of edges in the graph.
Original Graph Sub Graph
Subgraph: Let V(H) be the set of nodes of
a graph H and V(G) be the set of nodes of
a graph G such that V (H) ⊆ V (G). If in
addition, every edge of H is also an edge
of G, then the graph H is called a subgraph
of the graph G, which is expressed by
writing H ⊆ G.
Naturally, the graph G itself, as well as the
null graph obtained from G by deleting all
the edges of G, is also a sub graph of G.
One can get a subgraph by deleting edges
or vertices or both.
Note that deleting a vertex also requires
deleting any edges incident to the vertex
(since we can’t have an edge with a missing
endpoint).
A simple digraph G= <V,E> is called reflexive, transitive, symmetric , antisymmetric etc
if the relation E is reflexive , transitive ,symmetric, antisymmetric etc.
Converse of digraph : The converse of digraph G=<V,E> to be a digraph Q=<V,~E> in
which the relation ~E is the converse of the relation E. The diagram of Q, is called reversal
or directional dual of a digraph G.
If we reverse all the arrows on a digraph, we obtain in general a different digraph—the
converse of the original. Clearly, a digraph and its converse are co-spectral since their
adjacency matrices are transposes of each other.
The converse of G is also called the reversal or directional dual of a digraph G.
A graph is reflexive if for every node v there is an edge (v,v) (a loop).
Elementary path : p1,p2,p3 (A path in which all the nodes through which it traverses are
distinct)
Simple path: p1,p2,p3,p5 (A path in a diagraph in which the edges are distinct is called a
simple path )
Cycle: c1,c2,c3,c4,c5
Elementary cycle: c1=(<1,1>) c2=(<1,2>,<2,1>) c3=(<1,2>,<2,3>,<3,1>)
c4=(<1,4>,<4,3>,<3,1>) c5=(<1,4>,<4,3>,<3,2>,<2,1>)
Accessible Node/ Reachable Node : A node v of a simple digraph is said to be
reachable/accessible from the node u of the same digraph, if from the node u of
the same digraph, there exists a path from u to v.
Geodesic (Path of Minimum Length) : If a node v is reachable from node u, then a
path of minimum length from u to v is called a geodesic or path of minimum length.
Distance between Two Nodes : The distance from a node u to a node v, if v is
reachable from u, is written as d <u, v> and satisfies the following properties:
d< u, v > ≥ 0
d< u, u > =0
d<u, v> + d<v, w > ≥ d<u, w>
The last inequality is called the Triangle inequality. If v is not reachable from u
then it is customary to write d <u, v> = ∞.
Note also that if v is Reachable from u ,and u is reachable from v, then d <u, v>
is not necessarily equal to d <v, u>.
Theorem 5-1.1 In a simple digraph, the length of any elementary path is less than or equal
n-1, where n is the number of nodes in the graph. Similarly, the length of any cycle does not
exceed n.
Reachability Set of a node: The set of nodes which are reachable from a given node v is said to
be the reachable set of v. The reachable set of v is written as R(v).
Reachable Set of a Set of Nodes : For a digraph G = < V, E> , any subset S ⊆ V, the
reachable set of S is the set of nodes which are reachable from any node of S, and this set is
denoted by R(S).
For the digraph given in below Fig. , some of the reachable sets are as follows:
R (v1) = {v1, v2, v3, v4, v5, v6} = R (v2) = R (v3) = R (v4) = R (v5)
R (v6) = {v6}
R (v7) = {v6, v7}
R (v8) = {v6, v7 , v8}
R (v9) = {v9}
R (v10) = {v10}
R (v5, v8, v9, v10) = V = R (v1, v8, v9, v10)
Node Base : In a digraph G = < V, E>, a subset X ⊆ V
is called a node base if its reachable set is V and if no
proper subset of X has this property.
In the digraph of following Fig. the set {v1, v8, v9, v10}
is a node base and so is the set {v5, v8, v9, v10} .
R (v5, v8, v9, v10) = V = R (v1, v8, v9, v10)
Any node whose indegree is zero must be present in any node base.
Every isolated node/ point of a digraph must be present in a node base.
No node in the node base is reachable from another node in the node base.
From the nodes lying on an elementary cycle any one node could be chosen as
an element of a node base, hence, any node that does not have indegree zero
and that does not lie on a cycle cannot be present in a node base.
In an acyclic graph , a node base consists of only those nodes whose indegree
is zero.
For a given simple digraph, we may have more than one node base; however,
every node base has the same number of nodes.
CONNECTED GRAPHS :STRONGLY, UNILATERALLY, WEAKLY CONNECTED GRAPHS & COMPONENTS
An undirected graph is said to be connected if for any pair of nodes of the graph the two nodes
are reachable from one another.
i.e. An undirected graph is connected if there is a path from every node to every other
node. For example:
Connected Not connected
This definition cannot be applied to directed graphs without some further modification,
because in a directed graph if a node u is reachable from another node v, the node v may not
be reachable from u.
A digraph is called connected (weakly connected) if it is connected as an
undirected graph in which the direction of the edges is neglected.
(a) is strongly connected, (b) is weakly connected but not unilaterally connected,
while (c) is unilaterally connected but not strongly connected.
Connectivity in directed graphs
Definition: A directed graph is said to be strongly connected if for any pair of nodes
there is a path from each one to the other.
Definition: A directed graph is said to be unilaterally connected if for any pair of
nodes at least one of the nodes is reachable from the other i.e. A digraph is
unilaterally connected or unilateral, if either u is reachable from v or v is
reachable from u.
e6
v1
v4
v3
v2 and v3 are adjacent. e2
e1 is incident with v2.
e2 e3
e5
v2
v5
e4
PRESENTATIONS OF GRAPHS
A graph can also be
represented by its n X m
v3
incidence matrix T . e2
v4
T (i ; k ) = T (j ; k ) = 1 e4 e5
v5
iff ek = (vi; vj). v2
e1 e6
For a loop we have to v1
take 2 as two legs of an e1 e2 e3 e4 e5 e6
Incidence matrix
edge are connected with v1 1 0 0 0 0 2
a node. v2 1 1 0 1 0 0
v3 0 1 1 0 1 0
v4 0 0 0 0 0 0
v5 0 0 1 1 1 0
A graph can also be represented by its n X m incidence matrix T .
For an undirected graph T (i ; k ) = T (j ; k ) = 1 iff ek = (vi; vj).
Incidence matrix: The incidence matrix of a digraph with n vertices, m arcs and no self-loops is an n × m matrix
Adjacency matrix
e2 v3
v4
e3
v1 v2 v3 v4 v5
e4 e5
v1 2 1 0 0 0 v5
v2
v2 1 0 1 0 1 e1 e6
v1
v3 0 1 0 0 2
Adjacency matrix : Let G= <V ,E> be a simple digraph
v4 0 0 0 0 0 in which V ={v1 ,v2, ……….} and the nodes are assumed
v5 0 1 2 0 0
to be ordered from v1 to vn. An n X n matrix A whose
elements aij are given by
aij = 1 if<vi ,vj> ∈ E
aij = 0 otherwise
ADJACENCY MATRIX FOR DIRECTED GRAPHS
e2 v3
v4
Adjacency matrix
e3
v1 v2 v3 v4 v5 e4 e5
v5
v2
v1 1 0 0 0 0
e1 e6
v2 1 0 1 0 0 v1
v3 0 0 0 0 1
v4 0 0 0 0 0
v5 0 1 1 0 0
*** Symmetric Matrix. If the transpose of a matrix is equal to itself, that matrix is said to be symmetric e.g.
PG 60
For the Graph in Fig. if we first order the nodes as v1, v2, v3,
and v4 and write its adjacency matrix, we will get matrix A1.
If we reorder the rows as v2, v3, v1, and v4 and write its
adjacency matrix we will get A2.
We want to check,
Is R reflexive? symmetric? transitive?
We'll make use of the digraph for R on the right.
(a) R is reflexive, i.e. there is a loop at each vertex.
(b) R is symmetric, i.e. the arrows joining a pair of different vertices
always appear in a pair with opposite arrow directions.
2 v3
v4
Adjacency matrix
2
v1 v2 v3 v4 v5 5
v1 3 0 0 0 0 3 v5
v2
v2 1 0 2 0 0
1 3
v1
v3 0 0 0 0 2
In the case of a multigraph or a weighted graph, we write aij =
v4 0 0 0 0 0
wij , where wij denotes either the multiplicity or the weight of the
v5 0 3 3 0 0 edge <Vi, Vj>.
If G = <V, E> is a simple digraph whose adjacency matrix is A, then the adjacency
matrix of ~G, the converse of G, is the transpose of A, that is AT.
For simple undirected graphs adjacency matrix is symmetric and for it A= AT
The adjacency matrix for a digraph need not be symmetric. (justify)
Symmetric Diagraph
v1 v2 v3 v4
v1 1 0 0 0
v2 0 1 0 0
v3 0 0 1 0
v4 0 0 0 1
FOR GIVEN MATRIX A= A 1 WE CAN GET A AT, AT A, AND A N FOR N = 2,3,4,.....
0 0 1 1 1 0 1 0
1 0 1 0 A AT = 0 2 1 0
AT =
0 1 0 0 1 1 3 1
0 1 1 0 0 0 1 1
2 1 0 1 0 1 0 0
0 0 1 1 0 0 1 1
AT A = 1 2 0 1 A2 = A1=A =
2 1 0 1 1 1 0 1
0 0 1 1
1 1 1 1 1 0 0 0
1 1 1 2
0 1 0 0
1 2 1 1
2 1 0 1
A3 = A4 = 2 2 2 3
1 2 1 1
3 3 2 3
2 2 1 2
2 1 0 1
0 0 1 1
Lect. 4
28/9
MERITS OF ADJACENCY MATRIX
We can use adjacency matrix, to determine the connection of vertices, it is easy
The degree of a vertex is
For a digraph, the row sum is the outdegree, while the column sum is the indegree
The diagonal entries of AAT simply show the out degree of the nodes.
The element in the ith row and jth column of AT A shows the number of nodes of the graph
which are such that the edges initiation from these nodes terminate in both vi and vj.
Also the diagonal entries show the indgrees of the nodes.
An entry of 1 in the ith row and jth column of A shows the existence of an edge <vi, vj>,
that is, a path of length 1 from vi to vj. Let us denote the element of A2 by aij(2). Then
n
aij(2) = 𝛴 aikakj
k=1
For any fixed k, aikakj = 1 iff both aik and akj equal 1 that is , iff <vi , vk> and <vk , vj>
are the edges of the graph. For each such k we get a contribution of 1 in the sum.
Now <vi, vk> and <vk, vj> imply that there is a path from vi to vj of length 2.
Therefore aij (2) is equal to the number of different paths of exactly length 2 from vi
to vj.
Similarly, the diagonal element aii (2) shows the number of cycle of length 2 at the
node vi for i = 1, 2,.....,n.
In general we can say that
Theorem : Let A be the adjacency matrix of a digraph G. The element in the ith row
and jth column of An is equal to the number of paths of length n from the ith node to
the jth node.
we see that there are two paths of length 2 from v2 to v1 , hence
the entry 2 in the second row and first column of A2. Similarly,
there are two paths of length 4 from v2 to v1, hence the
corresponding entry in A4.
0 0 1 1 1 0 1 0
1 0 1 0 A AT = 0 2 1 0
AT =
0 1 0 0 1 1 3 1 From the adjacency matrix of A we can
0 1 1 0 0 0 1 1 immediately determine whether there exists an
edge from vi to vj in G.
2 1 0 1 0 0 1 1
AT A = Also from the matrix Ar, where r is some positive
1 2 0 1 A2 = 2 1 0 1
integer, we can establish the number of paths of
0 0 1 1 1 1 1 1 length r from vi to vj.
1 1 1 2 0 1 0 0
If we add the matrices A, A2, A3,.........,Ar to get Br
1 2 1 1 Br = A + A2 + ………….. Ar
2 1 0 1
A3 = A4 = 2 2 2 3 Then from the matrix Br we can determine the
1 2 1 1
3 3 2 3 number of paths of length less than or equal to r
2 2 1 2
2 1 0 1 from vi to vj.
0 0 1 1
Element in the ith row and jth column of AT A shows the number of nodes of the graph which are
such that the edges initiation from these nodes terminate in both vi and vj. The diagonal entries
show the indgrees of the nodes.
An entry of 1 in the ith row and jth column of A shows the existence of an edge <vi, vj>, that is, a
path of length 1 from vi to vj. Let us denote the element of A2 by aij(2). Then
n
aij(2) = 𝛴 aikakj
k=1
For any fixed k, aikakj = 1 iff both aik and akj equal 1 that is , iff <vi, vk> and <vk, vj> are the
edges of the graph. For each such k we get a contribution of 1 in the sum.
Now <vi, vk> and <vk, vj> imply that there is a path from vi to vj of length 2. Therefore aij (2) is
equal to the number of different paths of exactly length 2 from vi to vj.
Similarly, the diagonal element aii (2) shows the number of cycle of length 2 at the node vi for i
= 1, 2,.....,n.
By a similarly argument, one can show that the element in the ith row and jth column of A3
gives the number of paths of exactly length 3 from vi to vj. In general, we have the following
theorem.
By a similarly argument, one can show that the element in the ith row and jth column of A3 gives
the number of paths of exactly length 3 from vi to vj. In general, we have the following
theorem.
Theorem 5-1.3 Let A be the adjacency matrix of a digraph G. The element in the ith row and
jth column An is equal to the number of paths of length n from the ith node to the jth node.
In a simple digraph with n nodes, the length of an elementary path or cycle does not exceed n.
One can obtain elementary path between any two nodes by deleting certain parts of the
path which are cycles.
If we are interested in determining whether there exists a path from vi to vj, all we need to
examine are the elementary paths of length less than or equal to n - 1.
In the case of vi = vj and the path is a cycle, we need to examine all possible elementary cycle
of length less than or equal to n.
We can use,
Bn = A + A2 + ………….. +An
The ith row and jth column of Bn shows the number of paths of length n or less
which exist from vi to vj. If this element is nonzero, then it is clear that vj is
reachable from vi.
Path Matrix : Let G = <V , E> be a simple digraph in which | V| = n and the
nodes of G are assumed to be ordered. An n× 𝑛 𝑚𝑎𝑡𝑟𝑖𝑥 P whose element are given
by
A AXA AXAXA
We could move on further and consider all paths of length 3 in the above graph.
A path matrices of n length can be generated by multiplying A X A X A…. n times.(if they exist).
The method of calculating the path matrix P of a graph by calculating first A, A2,..........., An
and then Bn = A + A2 + ………….. +An is cumbersome.
To reduce the amount of calculation involved, Boolean matrix operations are used.
A matrix whose entries are the elements of a two-element Boolean algebra
< B, ∧, ∨,-----, 0, 1 > where entries B = 0, 1 𝑖𝑠 𝑐𝑎𝑙𝑙𝑒𝑑 a Boolean matrix .
The operations ∧ 𝑎𝑛𝑑 ∨ on B are given in table
∧ 0 1 ∨ 0 1
(Boolean product) (Boolean Sum)
A ∧B A∨ B
__________________________________
0 0 0 0 0 1
1 0 1 1 1 1
_________________________________
The adjacency matrix is a Boolean matrix, and so also is the path matrix. Let us write A ∧ A =
A(2), A ∧ A (r-1) = A (r) for any r = 2, 3,..... The only difference between A2 and A(2) is that A(2)
is a Boolean matrix and the entry in the ith row and jth column of A(2) is 1 if there is at least one
path of length 2 from vi to vj, while in A2 the entry in the ith row and jth column shows the
number of paths of length 2 from vi to vj.
the path matrix P is given by n
P = A ∨ A(2) ∨ A(3) ∨……………….∨ A(n) = V A(k)
k=1
p3
p5 0 1 0 1 1
p4
0 1 0 0 0
Fig : Procedure call
Lect. 5
12/10