Adjacency Matrix is a way of representing a graph using a 2D array. It is particularly useful for dense graphs where the number of edges is close to the maximum number of edges.
Going back to a previous undirected graph example, we can represent this as a matrix:
An adjacency matrix is a square matrix used to represent a finite graph. The elements of the matrix indicate whether pairs of vertices are adjacent or not in the graph.
They are defined as follows:
Let G = (V, E) with |V| = n and vertices indexed v_1, …, v_n. The adjacency matrix A ∈ ℝ^{n×n} is defined by:
-
Directed, unweighted graphs:
-
Undirected, unweighted graphs:
For simple graphs (no self-loops), A_{ii} = 0.
-
Weighted graphs:
For undirected weighted graphs, A is symmetric. In multigraphs, A_{ij} may store the number of parallel edges (or the sum of their weights).