0% found this document useful (0 votes)
67 views

Graph Theory Representing Graphs in Computer Memory: Sequential Representation of G Is by Means of Its Adjacency Matrix A

There are two main ways to represent a graph in computer memory: 1. Use an adjacency matrix for dense graphs 2. Use linked lists of neighbors (adjacency lists) for sparse graphs The adjacency matrix stores whether each pair of vertices is connected in a 2D array, while adjacency lists store each vertex and its connected neighbors. Adjacency lists use less memory for sparse graphs with few edges.

Uploaded by

Arnab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

Graph Theory Representing Graphs in Computer Memory: Sequential Representation of G Is by Means of Its Adjacency Matrix A

There are two main ways to represent a graph in computer memory: 1. Use an adjacency matrix for dense graphs 2. Use linked lists of neighbors (adjacency lists) for sparse graphs The adjacency matrix stores whether each pair of vertices is connected in a 2D array, while adjacency lists store each vertex and its connected neighbors. Adjacency lists use less memory for sparse graphs with few edges.

Uploaded by

Arnab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Graph Theory

Representing Graphs in Computer Memory

There are two standard ways of maintaining a graph G in the memory of a computer. Two main
ways are:
1. Sequential representation of G is by means of its adjacency matrix A.
Matrices are usually used when the graph G is dense.

2. Linked representation or adjacency structure of G, uses linked lists of neighbors.


Linked lists are usually used when G is sparse.

Dense graph is a graph in which the number of edges is close to the maximal number of edges.
The opposite, a graph with only a few edges, is a sparse graph.

(A graph G with m vertices and n edges is said to be dense when m = O(n 2) and sparse when m =
O(n) or even O(n log n).)

Adjacency Matrix Representation

The adjacency matrix of a graph G with n vertices is N x N. It is given by A = [aij].


aij = 1 if ith and jth vertices are adjacent.
= 0 if ith and jth vertices are not adjacent/otherwise.

Figure (b) contains the adjacency matrix of the graph G in Figure (a) where the vertices are
ordered A, B, C,D, E. Observe that each edge {v i, vj} of G is represented twice, by aij = 1 and
aji = 1. Thus, in particular,the adjacency matrix is symmetric.

1
The adjacency matrix A of a graph G does depend on the ordering of the vertices of G, that is, a
different ordering of the vertices yields a different adjacency matrix. However, any two such
adjacency matrices are closely related in that one can be obtained from the other by simply
interchanging row* and columns. On the other hand, the adjacency matrix does not depend on
the order in which the edges (pairs of vertices) are input into the computer.
 
There are variations of the above representation. If G is a multigraph, then we usually let denote
the number of edges {vi vj}. Moreover,if G is a weighted graph, then we may let, a ij denote the
weight of the edge {vi, vj}.

Example

Graph Adjacency matrix

Adjacency Matrix Representation

The incidence matrix of a graph G with N


vertices and E edges is NxE.

mij = 1 if ej is incident on vi.


= 0 otherwise

 In computer’s, even there are many mathematical representations, adjacent matrix and adjacency
lists are only used for representing graph in computers memory.

2
Example 1

Graph Adjacency Matrix Adjacency List Representation in the memory.


Representation

Example 2

Example 3
Find the adjacency matrix A = [aij] of each of the following graph G.

Ans.
0 1 0 1 1 0 0 1
G1= 1 0 1 1 G2 = 0 0 2 1
0 1 0 1 0 2 0 0
1 1 1 0 1 1 0 1
(a) (b)
Example 4
Draw the graph G corresponding to each adjacency matrix.

0 1 0 1 0 1 3 0 0
A= 1 0 0 1 1 A= 3 0 1 1
0 0 0 1 1 0 1 2 2
1 1 1 0 1 0 1 2 0
0 1 1 1 0
(a) (b)

3
Ans.

(a) Since A is a 5-square matrix, G has five vertices, say v1, v2 .. .. v5.
Draw an edge from vi to vj when aij =1. The graph appears in Fig. a.

(b) Since A is a 4-square matrix, G has four vertices, say v1, v2, v3, v4.
Draw n edges from vi to vj when aij = n.
Also, draw n loops at vi when aii = n. The graph appears in Fig. b.

Example 5(p228/8.63)
Find the adjacency matrix A of each graph as bellow:

Example 6(p228/8.64)

Draw the multigraph G corresponding to each of the following adjacency matrices.

0 2 0 1 1 1 1 2
A= 2 1 1 1 A= 1 0 0 0
0 1 0 1 1 0 0 2
1 1 1 0 2 0 2 2
(a) (b)

Linked Representation

Let G be a graph with m vertices. The representation of G in memory by its adjacency matrix A
hasa number of major drawbacks.

4
First of all it may be difficult to insert or delete vertices in G. The reason is that the size of A
may need to be changed and the vertices may need tobe reordered, so there may be many, many
changes in the matrix A.

Furthermore, suppose G is sparse. Then the matrix A will contain many zeros; hence a great deal
of memory space will be wasted. Accordingly, when G is sparse, G is usually represented in
memory by some type of linked representation, also called an adjacency structure.

Consider the graph G in Figure (a). Observe that G may be equivalently defined by the table in
Figure (b) which shows each vertex in G followed by its adjacency list, i.e., its list of adjacent
vertices (neighbors). Here the symbol Ø denotes an empty list. This table may alsobe presented
in the compact form
 
G = [A:B,D;    B:A,C,D;   C:B;   D:A,B;    E: Ø]

where a colon ":" separates a vertex from its list of neighbors, and a semicolon ";" separates the
different lists.

Remark: Observe that each edge of a graph G is represented twice in an adjacency structure; that
is, any edge, say {A, B}, is represented by B in the adjacency list of A, and also by A in the
adjacency list of B. The graph G in Figure (a) has four edges, and so there must be 8 vertices in
the adjacency lists. On the other hand, each vertex in an adjacency listcorresponds to a unique
edge in the graphG.
 

 
The linked representation of a graph G, which maintains G in memory by using its adjacency
lists, would normally contain two files (or sets of records),

One called the Vertex File, and


Other called the Edge File.

You might also like