Adjacency Matrix and List
Adjacency Matrix and List
H F X
G
A
R
C E D
Y B
A
Adjacency Matrix and List
Jaenhel Mencidor
Christian De la Torre
Gian Fran Araneta
Peter Francis Espera
Ern Jan Painaga
Erik Agabon
Yosef Komanchi Javier
Graph
A
K
A Graph is a non-linear data structure consisting of nodes and edges. The
nodes are sometimes also referred to as vertices and the edges are lines or arcs
that connect any two nodes in the graph. In this presentation, we will understand
the difference between the ways of representation of the graph.
H
B
C F N
B
A
Y
A
E A
F
B
G
Q
J D
Adjacency Matrix
An adjacency matrix is a way of representing a graph as a matrix of boolean (0’s
and 1’s).
S
Z N
D
L
B
A
K
Example of Directed Graph
R
Direct Graph Adjacency matrix
B
E
Graph Representation of Directed graph to Adjacency Matrix
B
Initially, the entire Matrix is initialized to 0. If there is an edge from F
C
source to destination, we insert 1 for that destination.
G
Y
A X A
F
K
Adjacency Matrix
Adjacency List is an array of separate lists. Each element of array is a list of
corresponding neighbors (or directly connected) vertices. In other words, list of
S
Adjacency List is a list of all those vertices which is directly connected to vertex.
Z N
M So, an array of list will be created of size 3, where each indices represent the
vertices. Now, vertex 0 has two neighbours (i.e, 1 and 2). So, insert vertex 1 and 2
D at indices 0 of array. Similarly, for vertex 1, it has two neighbour (i.e, 2 and 0) So,
insert vertices 2 and 0 at indices 1 of array. Similarly, for vertex 2, insert its
R
K
Vertex 0 has no neighbours since no edges of 0 is pointing to other vertices. For vertex H
1, it has two neighbour (i.e, 0 and 2), so, insert vertices 0 and 2 at indices 1 of array.
Similarly, for vertex 2, insert its neighbours in array of list, which is 0 in the
aforementioned example.
N
M
B
A
A Y
E
F B
F
F
R
Z
F
L
B
D
K
M
N
N
D
B D
B
S
A
R
H
C
J
A
A
K
Y
A
F
B
Z
R
E
N
X
A
D
D
B D L
H K