Graph Problem
Graph Problem
, :
]
) E
when there is a connection between vertices :
and :
]
. This connection may mean a road segment, a
friendship, or any other relationship between the two vertices. Edges can be directional (directed graph)
and unidirectional (undirected graph). Figure 1 shows an undirected graph consisting of 6 vertices and 7
edges.
Figure 1: An example of a graph.
Representation of graphs in programs. In a program, a graph is usually represented in any of the two
ways: (i) by an adjacency matrix or (ii) by an adjacency list.
Adjacency matrix. In this representation, a two-dimensional matrix A of size n X n is used to store the
graph. Each element o
]
stores the presence of absence of an edge between vertices :
and :
]
. Whenever
(:
, :
]
) E, o
]
= 1 otherwise o
]
= u. If the graph is undirected, then o
]
= o
]
. An array data structure
can be used to simulate the matrix in programming languages. The following shows the adjacency matrix
representation of graph of Fig. 1.
A =
u 1 u
1 u 1
u 1 u
u 1 u
u 1 u
1 u u
u u 1
1 1 u
u u u
u 1 1
1 u u
1 u u
Adjacency list. In this representation there are n lists: I
1
, I
2
, , I
n
. Each list I