Adjacency matrices are a popular representation for graphs in algorithmic problems, where for a graph with N
nodes, it can be stored as an array int adj[N][N]
. Each value adj[a][b]
indicates whether the graph contains an edge from nodes a
to b
. For an unweighted graph, it contains a Boolean value. For a weighted graph, it contains the edge weight.
Implications
Matrix representations are ideal for dense graphs, i.e., if is close to . Main drawback of adjacency matrices is that the matrix contains elements, and if the graph isn’t dense, then most of them are zero.