Illustrated · 1 min
Graphs can be represented in various data structures, most commonly by either a list of lists or by an adjacency matrix.
List of Lists
In this representation of a graph, we have a list of vertices. In each vertex, we keep a list of the neighbors of each vertex.

Adjacency Matrix
In this representation of a graph, we keep a matrix whose th entry is if there is an edge from to and otherwise. There are two general observations we can make about all adjacency matrices: (1) they are symmetric, which means or in matrix terms (2) they have s along the main diagonal, i.e., . Note that symmetry is broken if becomes directed, but that is a whole different story.
