Representations of a graph
Representations of a graph:
• Adjacency Matrix
• Adjacency List
• Incidence Matrix
Graph Theory A Senthil Thilak
Representations of a graph
I ADJACENCY MATRIX OF A GRAPH G [A(G )]:
Graph Theory A Senthil Thilak
Representations of a graph
I ADJACENCY MATRIX OF A GRAPH G [A(G )]:
General definition: Let G be a graph on n vertices labelled by
V (G ) = {u1 , u2 , . . . un }. For each i, j ∈ {1, 2, . . . n}. Then the
adjacency matrix of G , denoted by A(G ) is defined as
A(G ) = (aij )n×n where
aij = No. of edges (or arcs) from ui to uj
If G = (V, E, φ) is undirected,
aij = No. of edges between ui and uj
= |{e ∈ E (G ) : φ(e) = (ui , uj )}|
If G = (V, E, φ) is directed,
aij = No. of arcs from ui to uj
= |{e ∈ E (G ) : e is a directed edge from ui to uj }|
Graph Theory A Senthil Thilak
Representations of a graph
Example (1)
0 1 2 0 0
1 0 1 0 0
2
A(G ) = 1 0 1 0
0 0 1 1 0
0 0 0 0 0
Graph Representations 31
Example (2)
.6 Graph Representations 31
2 13 24 3
5 4 5
(b) (b) 1
e2e2 01 0 1 01 1
1 1 1
(a)(a) 11 2 2
1 0
e1
e1 2 1 0 1 0 0
e4 e3 2 1 0 1 0 0
e6
5
e7 e6 e4 e3 3 0 0 0 1 0
5 3 0 0 0 1 0
e7 e5
3 4 0 0 0 0 0
4
e5
3 5 04 0 01 01 0
0 0 0
4
e8
5 0 0 1 1 0
(c)e8
Graph Theory
1 3 5 4 \ A Senthil Thilak
Representations of a graph
Properties of an adjacency matrix:
Graph Theory A Senthil Thilak
Representations of a graph
Properties of an adjacency matrix:
• The adjacency matrix is symmetric for an undirected graph and is
asymmetric for a directed graph.
Graph Theory A Senthil Thilak
Representations of a graph
Properties of an adjacency matrix:
• The adjacency matrix is symmetric for an undirected graph and is
asymmetric for a directed graph.
• An adjacency matrix requires O(n2 ) space, for a graph of order n.
Graph Theory A Senthil Thilak
Representations of a graph
Properties of an adjacency matrix:
• The adjacency matrix is symmetric for an undirected graph and is
asymmetric for a directed graph.
• An adjacency matrix requires O(n2 ) space, for a graph of order n.
• If G is simple, then A(G ) is a symmetric binary matrix - that is,
symmetric where aij ∈ {0, 1}, for all i and j. In addition, aii = 0, for
all i, since G has no loops.
Graph Theory A Senthil Thilak
Representations of a graph
Properties of an adjacency matrix:
• The adjacency matrix is symmetric for an undirected graph and is
asymmetric for a directed graph.
• An adjacency matrix requires O(n2 ) space, for a graph of order n.
• If G is simple, then A(G ) is a symmetric binary matrix - that is,
symmetric where aij ∈ {0, 1}, for all i and j. In addition, aii = 0, for
all i, since G has no loops.
• Each entry aij in A(G ) counts how many edges are there between ui
and uj . Hence,P P
|E (G )| = i≥j aij = i≤j aij .
Graph Theory A Senthil Thilak
Representations of a graph
Properties of an adjacency matrix:
• The adjacency matrix is symmetric for an undirected graph and is
asymmetric for a directed graph.
• An adjacency matrix requires O(n2 ) space, for a graph of order n.
• If G is simple, then A(G ) is a symmetric binary matrix - that is,
symmetric where aij ∈ {0, 1}, for all i and j. In addition, aii = 0, for
all i, since G has no loops.
• Each entry aij in A(G ) counts how many edges are there between ui
and uj . Hence,P P
|E (G )| = i≥j aij = i≤j aij .
• For each i, the degree dG (ui ) is the sum of all entries in i th row (or
column) of A(G ), where we count the entry aii on the diagonal
twice. That is,P
n Pn
dG (ui ) = j=1 aij + aii = j=1 aji + aii
Graph Theory A Senthil Thilak
Representations of a graph
Properties of an adjacency matrix:
• The adjacency matrix of G depends on the labelling of the vertices
in G .
Graph Theory A Senthil Thilak
Representations of a graph
Properties of an adjacency matrix:
• The adjacency matrix of G depends on the labelling of the vertices
in G . For any two distinct labellings of V (G ), say {u1 , u2 . . . un }
and {u10 , u20 . . . un0 }, it is clear that ui0 = uσ(i) for some fixed
permutation, σ : {1, . . . n} → {1, . . . n}.
Graph Theory A Senthil Thilak
Representations of a graph
Properties of an adjacency matrix:
• The adjacency matrix of G depends on the labelling of the vertices
in G . For any two distinct labellings of V (G ), say {u1 , u2 . . . un }
and {u10 , u20 . . . un0 }, it is clear that ui0 = uσ(i) for some fixed
permutation, σ : {1, . . . n} → {1, . . . n}.
Therefore, if A(G )0 = [aij0 ] is the adjacency matrix of G with respect
to the labelling {u10 , u20 . . . un0 } of V (G ), then
aij0 = aσ(i)σ(j) .
Graph Theory A Senthil Thilak
Representations of a graph
Properties of an adjacency matrix:
• The adjacency matrix of G depends on the labelling of the vertices
in G . For any two distinct labellings of V (G ), say {u1 , u2 . . . un }
and {u10 , u20 . . . un0 }, it is clear that ui0 = uσ(i) for some fixed
permutation, σ : {1, . . . n} → {1, . . . n}.
Therefore, if A(G )0 = [aij0 ] is the adjacency matrix of G with respect
to the labelling {u10 , u20 . . . un0 } of V (G ), then
aij0 = aσ(i)σ(j) .
• The following statements are equivalent:
1 The graphs G and H are isomorphic.
2 A(G ) and A(G ) are orthogonally equivalent with the respect to
any labelling of their vertices.
Graph Theory A Senthil Thilak
Representations of a graph
Properties of an adjacency matrix:
• The adjacency matrix of G depends on the labelling of the vertices
in G . For any two distinct labellings of V (G ), say {u1 , u2 . . . un }
and {u10 , u20 . . . un0 }, it is clear that ui0 = uσ(i) for some fixed
permutation, σ : {1, . . . n} → {1, . . . n}.
Therefore, if A(G )0 = [aij0 ] is the adjacency matrix of G with respect
to the labelling {u10 , u20 . . . un0 } of V (G ), then
aij0 = aσ(i)σ(j) .
• The following statements are equivalent:
1 The graphs G and H are isomorphic.
2 A(G ) and A(G ) are orthogonally equivalent with the respect to
any labelling of their vertices.
• The (i, j)th entry aij(k) in A(G )k gives the number of distinct
walks of length k from ui to uj in G . (Try as an
exercise!!!)
Graph Theory A Senthil Thilak
Representations of a graph
II ADJACENCY LIST OF A GRAPH G :
Graph Theory A Senthil Thilak
Representations of a graph
II ADJACENCY LIST OF A GRAPH G :
General definition: The adjacency list of a graph (or a digraph)
is an array of lists with each list representing a vertex and its (out)
neighbours in a linked list.
Graph Theory A Senthil Thilak
Representations of a graph
2.6 Graph Representations 31
II ADJACENCY LIST OF A GRAPH G : 2 3 4 5
1
(b)
General definition: The adjacency
(a) e2 list of a 1graph
0 0
(or
1
a1 digraph)
1
1 2
e1
is an array of lists with each list representing a 2vertex 1 0 and
1 0 its0 (out)
e6 e4 e3
neighbours in a linked list. The
5
e7end of the list is 3 marked
0 0 0 by
1 a0
e5
6 GraphNULL pointer.
Representations 4 3 4 0 0 0 0
31
0
5 0 0 1 1 0
e8
Example (c)1
2 3 4 5
(b)
(a) e2 1 1
0 0 3
1 1 5
1 4 \
1 2
e1
2 21 0 1 1 03 \0
e6 e4 e3
5 3
e7 30 0 4 0\ 1 0
e5
3 4 0 0 0 0 0
4 4 \
5 0 0 1 1 0
e8 5 4 3 \
(c) Fig. 2.16 a A digraph, b Its adjacency matrix, c Its adjacency list. The end of the list entries are
marked by a backslash
1 3 5 4 \
is preferred due to the space dependence on the number of vertices and edges. For
Graph Theory A Senthil Thilak
Representations of a graph
Properties of an adjacency list:
• An adjacency list requires O(n + m) space for a graph of order
n and size m.
Graph Theory A Senthil Thilak
Representations of a graph
Properties of an adjacency list:
• An adjacency list requires O(n + m) space for a graph of order
n and size m.
• For sparse graphs, adjacency list is preferred due to the space
dependence on the number of vertices and edges.
Graph Theory A Senthil Thilak
Representations of a graph
Properties of an adjacency list:
• An adjacency list requires O(n + m) space for a graph of order
n and size m.
• For sparse graphs, adjacency list is preferred due to the space
dependence on the number of vertices and edges.
• For dense graphs, adjacency matrix is commonly used as
searching the existence of an edge in this matrix can be done
in constant time.
Graph Theory A Senthil Thilak
Representations of a graph
Properties of an adjacency list:
• An adjacency list requires O(n + m) space for a graph of order
n and size m.
• For sparse graphs, adjacency list is preferred due to the space
dependence on the number of vertices and edges.
• For dense graphs, adjacency matrix is commonly used as
searching the existence of an edge in this matrix can be done
in constant time.
• With the adjacency list of a graph, the time required for the
search operation in O(n).
Graph Theory A Senthil Thilak
Representations of a graph
III INCIDENCE MATRIX OF A GRAPH G :
Graph Theory A Senthil Thilak
Representations of a graph
III INCIDENCE MATRIX OF A GRAPH G :
Incidence matrix of a graph: The incidence matrix of an
undirected graph G of order n and size m is defined as
(
1; if edge ej is incident with vertex vi
B(G ) = (bij )n×m , where bij =
0; otherwise
When G is a digraph, then incidence matrix of G is defined as
−1; if arc ej ends at vertex ui
B(G ) = (bij )n×m , where bij = 1; if arc ej starts at vertex ui
0; otherwise
Graph Theory A Senthil Thilak
v Pv Sv
Representations of a graph
1 {2} {3, 4, 5}
2 {Ø} {1, 3}
3 {1, 2, 5} {4}
III INCIDENCE 4MATRIX OF A GRAPH
{1, 3, 5}
G: {Ø}
Incidence matrix of5 a graph: The incidence
{1} matrix of {3,
an4}
undirected graph G of order n and size m is defined as
(
1; if edge ej is incident with vertex vi
, where
B(G ) = (bij )n×m 2.6.3 bij = Matrix
Incidence
0; otherwise
An incident
When G is a digraph, then matrix of a simple
B[n, m] matrix
incidence ofgraph
G ishasdefined bi j = 1 if edge j is
elementsas
incident to vertex iand bi j = 0 otherwise. The incidence matrix for a digraph is
defined differently as −1; if arc ej ends at vertex ui
below.
B(G ) = (bij )n×m , where bij = 1; ⎧ if arc
⎨ −1 eje starts
if arc at vertex
ends at vertex v ui
bve = 1 if arc e starts at vertex v
0; ⎩ otherwise
2.6 Graph Representations
0 31
otherwise
Example (b)
The incidence
1 2 3matrix
4 of
5 the graph of Fig. 2.16a is as below:
(a) e2 1 0 0 1 1 1
1 2 ⎛ ⎞
e1 1 −1 0 1 0 0 0 0
2 1 0 1 0 0
e4 e3 ⎜ 0 1 1 0 0 0 0 0 ⎟
5 e6
3 0 0 0 1
⎜ ⎟
e7 B0 = ⎜
⎜ 0 0 −1 −1 1 0 0 −1 ⎟
⎟
e5 ⎝ 0
4 3 4 0 0 0 0 0 0 0 0 1 1 1 0 ⎠
5 0 0 1 1 0 −1 0 0 0 0 0 1 1
e8
(c)
In the edge list representation of a graph, all of its edges are included in the list.
1 3 5 4 \
Graph Theory A Senthil Thilak