Fact-checked by Grok 6 months ago

Adjacency matrix

In graph theory, the adjacency matrix of a finite simple undirected graph with n vertices is an n × n square matrix where each entry aij is 1 if vertices i and j are adjacent (connected by an edge) and 0 otherwise, with the main diagonal consisting of zeros to reflect the absence of self-loops.[1] This matrix provides a compact algebraic representation of the graph's edge structure, where the labeling of rows and columns corresponds to an arbitrary but fixed ordering of the vertices.[2] For directed graphs, the adjacency matrix is similarly defined but asymmetric, with aij equal to 1 if there is a directed edge from vertex i to j, allowing for one-way connections.[1] In weighted graphs, entries aij represent the weight of the edge between i and j (or 0 if no edge exists), enabling analysis of graphs with varying edge strengths, such as distances or capacities.[1] The matrix for an undirected graph is symmetric (A = AT)[2], reflecting the bidirectional nature of edges. By ordering the vertices such that those in each connected component are contiguous, the matrix can be arranged into a block-diagonal structure, with each block corresponding to the adjacency matrix of a connected component.[1] Key properties of the adjacency matrix include its utility in computing graph invariants: the sum of the i-th row (or column) equals the degree of vertex i in undirected graphs, and powers of the matrix (Ak) have entries that count the number of walks of length k between vertices, facilitating path enumeration.[1] In spectral graph theory, the eigenvalues of the adjacency matrix reveal structural insights, such as connectivity and expansion properties; for regular graphs, the largest eigenvalue relates to the degree, while smaller ones indicate bipartiteness or clustering.[3] The adjacency matrix has broad applications beyond pure mathematics, including efficient storage and manipulation of graphs in computer science algorithms for network analysis, as well as modeling molecular structures in chemistry and communication flows in engineering.[3] Its linear algebraic framework supports randomized algorithms and expander graph constructions in modern computing, underscoring its foundational role in bridging graph theory with applied fields.[3]

Definition and Variations

Basic Definition

In graph theory, a graph GG consists of a set VV of vertices and a set EE of edges, where each edge is an unordered pair of distinct vertices from VV. A simple undirected graph is an unweighted, undirected graph containing no loops (edges connecting a vertex to itself) or multiple edges between the same pair of vertices.[4] The adjacency matrix AA of a simple undirected graph GG with vertex set V={1,2,,n}V = \{1, 2, \dots, n\} is the n×nn \times n square matrix defined by
A=(aij)1i,jn, A = (a_{ij})_{1 \leq i,j \leq n},
where aij=1a_{ij} = 1 if there is an edge between vertices ii and jj, and aij=0a_{ij} = 0 otherwise.[5] Since the graph is undirected, the adjacency matrix AA is symmetric, satisfying aij=ajia_{ij} = a_{ji} for all i,ji, j.[5] Additionally, the absence of loops in simple graphs ensures that the diagonal entries are zero, so aii=0a_{ii} = 0 for all ii.[5] To construct the adjacency matrix, label the vertices in order from 1 to nn, then for each pair of distinct indices ii and jj with i<ji < j, check if an edge exists between vertices ii and jj; if so, set aij=aji=1a_{ij} = a_{ji} = 1, otherwise set aij=aji=0a_{ij} = a_{ji} = 0, and set all diagonal entries aii=0a_{ii} = 0.[6]

Bipartite Graphs

A bipartite graph is one whose vertex set can be partitioned into two disjoint subsets UU and VV such that every edge connects a vertex in UU to a vertex in VV, with no edges within UU or within VV.[7] The adjacency matrix AA of such a graph, assuming vertices are ordered with those in UU first followed by those in VV, is a square (U+V)×(U+V)(|U| + |V|) \times (|U| + |V|) matrix featuring zero blocks on the diagonal corresponding to intra-partition connections and nonzero entries only in the off-diagonal blocks representing inter-partition edges.[8] Since the graph is undirected and simple, AA is symmetric with binary entries (0 or 1), and the upper off-diagonal block is the transpose of the lower one.[8] The submatrix in the upper off-diagonal block, denoted the biadjacency matrix BB, is a U×V|U| \times |V| matrix where the entry Bi,j=1B_{i,j} = 1 if the ii-th vertex in UU is adjacent to the jj-th vertex in VV, and 0 otherwise; for multigraphs, entries count the edges between them.[7] The full adjacency matrix then takes the block form
A=(0U×UBBT0V×V), A = \begin{pmatrix} \mathbf{0}_{|U| \times |U|} & B \\ B^T & \mathbf{0}_{|V| \times |V|} \end{pmatrix},
where 0k×l\mathbf{0}_{k \times l} denotes the k×lk \times l zero matrix.[8] This structure succinctly encodes the bipartition and edge constraints. For the complete bipartite graph Km,nK_{m,n}, where every vertex in the mm-vertex set connects to every vertex in the nn-vertex set, the biadjacency matrix BB is the all-ones matrix Jm,nJ_{m,n}, making the off-diagonal blocks fully populated with 1s.[8] Thus, the adjacency matrix is
A=(0m×mJm,nJn,m0n×n), A = \begin{pmatrix} \mathbf{0}_{m \times m} & J_{m,n} \\ J_{n,m} & \mathbf{0}_{n \times n} \end{pmatrix},
with Jn,m=Jm,nTJ_{n,m} = J_{m,n}^T.[8]

Directed and Weighted Graphs

In directed graphs, the adjacency matrix AA is defined such that its entry AijA_{ij} is 1 if there is a directed edge (arc) from vertex ii to vertex jj, and 0 otherwise.[9] Unlike the adjacency matrix of an undirected graph, which is symmetric (A=ATA = A^T), the matrix for a directed graph is generally not symmetric, reflecting the potential asymmetry in edge directions (AijAjiA_{ij} \neq A_{ji}).[10] This representation captures the orientation of edges, making it suitable for modeling relationships with inherent directionality, such as one-way streets or dependencies in a network. For weighted graphs, the adjacency matrix extends to include edge weights, where Aij=wijA_{ij} = w_{ij} if there is an edge from vertex ii to vertex jj with weight wijw_{ij} (typically a real number indicating strength, cost, or capacity), and 0 otherwise.[10] This formulation applies to both undirected and directed graphs; in the directed case, weights allow AijAjiA_{ij} \neq A_{ji} even if both directions exist, enabling the representation of asymmetric influences or flows. The weighted adjacency matrix thus generalizes the binary version, preserving structural information while incorporating quantitative edge attributes. Variations of the adjacency matrix accommodate specialized graph types. In signed graphs, where edges carry positive or negative signs to model agreement or conflict, Aij=+1A_{ij} = +1 for a positive edge from ii to jj, Aij=1A_{ij} = -1 for a negative edge, and 0 otherwise, with the matrix remaining non-symmetric for directed signed graphs.[11] For multigraphs, which permit multiple edges between the same pair of vertices, AijA_{ij} equals the number of edges from ii to jj (or between them in the undirected case), allowing the matrix to count parallel connections rather than just their presence.[12] These adaptations maintain the core utility of the adjacency matrix while tailoring it to diverse applications in network analysis.

Examples

Undirected Simple Graphs

In undirected simple graphs, the adjacency matrix is a symmetric $ n \times n $ matrix with zeros on the main diagonal (reflecting no self-loops) and ones in the off-diagonal positions corresponding to edges between distinct vertices. The complete graph $ K_n $, where every pair of distinct vertices is connected by an edge, has an adjacency matrix consisting of zeros on the diagonal and ones everywhere else; this can be expressed as $ J - I $, where $ J $ is the all-ones matrix and $ I $ is the identity matrix.[13] For example, the adjacency matrix of $ K_3 $ (a triangle) is
(011101110). \begin{pmatrix} 0 & 1 & 1 \\ 1 & 0 & 1 \\ 1 & 1 & 0 \end{pmatrix}.
The cycle graph $ C_n $, consisting of $ n $ vertices connected in a single cycle, has an adjacency matrix that is circulant, with ones at positions $ (i, i+1) $ and $ (i, i-1) $ for $ i = 1, \dots, n $ (indices modulo $ n $) and zeros elsewhere.[14] For $ n = 4 $, the adjacency matrix of $ C_4 $ (a square) is
(0101101001011010). \begin{pmatrix} 0 & 1 & 0 & 1 \\ 1 & 0 & 1 & 0 \\ 0 & 1 & 0 & 1 \\ 1 & 0 & 1 & 0 \end{pmatrix}.
The path graph $ P_n $, a connected tree with $ n $ vertices and $ n-1 $ edges forming a linear chain, has an adjacency matrix that is tridiagonal, with ones on the subdiagonal and superdiagonal (corresponding to consecutive vertices) and zeros elsewhere.[15] For $ n = 4 $, this yields
(0100101001010010). \begin{pmatrix} 0 & 1 & 0 & 0 \\ 1 & 0 & 1 & 0 \\ 0 & 1 & 0 & 1 \\ 0 & 0 & 1 & 0 \end{pmatrix}.

Directed Graphs

In directed graphs, the adjacency matrix AA is an n×nn \times n matrix where the entry aij=1a_{ij} = 1 if there is a directed edge from vertex ii to vertex jj, and 00 otherwise; unlike the undirected case, this matrix is generally asymmetric, reflecting the orientation of edges.[16] This asymmetry captures the one-way nature of directed edges, allowing the matrix to encode precedence or flow in structures like networks or dependencies.[17] A classic example is the directed cycle graph CnC_n, where vertices are connected in a unidirectional loop. For n=3n=3 (a directed triangle), the adjacency matrix has 1s on the superdiagonal and in the bottom-left corner:
(010001100) \begin{pmatrix} 0 & 1 & 0 \\ 0 & 0 & 1 \\ 1 & 0 & 0 \end{pmatrix}
This matrix represents edges 121 \to 2, 232 \to 3, and 313 \to 1, forming a cyclic structure. For general nn, the matrix is a circulant permutation matrix with 1s shifted along the superdiagonal and an1=1a_{n1} = 1.[18] Another illustrative case is the tournament graph, a complete directed graph on nn vertices where exactly one directed edge exists between every pair of distinct vertices. The adjacency matrix of a 3-vertex tournament is thus a 3x3 matrix with zeros on the diagonal and exactly one 1 in each off-diagonal pair (i,j)(i,j) and (j,i)(j,i).[19] For the cyclic tournament (isomorphic to the directed triangle above), the matrix is
(010001100), \begin{pmatrix} 0 & 1 & 0 \\ 0 & 0 & 1 \\ 1 & 0 & 0 \end{pmatrix},
featuring a single directed 3-cycle. In contrast, an asymmetric (transitive) tournament, where one vertex dominates the others in a linear hierarchy, has the matrix
(011001000), \begin{pmatrix} 0 & 1 & 1 \\ 0 & 0 & 1 \\ 0 & 0 & 0 \end{pmatrix},
with edges 121 \to 2, 131 \to 3, and 232 \to 3, containing no cycles.[19] The transpose ATA^T of a directed graph's adjacency matrix represents the graph with all edge directions reversed, swapping rows and columns to invert the orientations while preserving the underlying structure.[16] This property is useful for analyzing reversals in flow or dependency graphs.

Trivial and Special Cases

The trivial graph, consisting of a single isolated vertex with no edges, has an adjacency matrix that is the 1×1 zero matrix [0][0]. This follows directly from the standard definition, where the absence of any edges results in no off-diagonal entries, and the diagonal is zero since simple graphs prohibit self-loops. For the empty graph, or edgeless graph, with nn isolated vertices and no edges connecting any pair, the adjacency matrix is the n×nn \times n zero matrix, where every entry aij=0a_{ij} = 0 for all i,ji, j. This structure reflects the complete lack of adjacencies, making the matrix entirely sparse with no 1s anywhere.[20] In disconnected graphs, which consist of multiple connected components, the adjacency matrix can be arranged—by appropriately ordering the vertices—to take a block-diagonal form. Specifically, if the graph has pp components with sizes n1,n2,,npn_1, n_2, \dots, n_p, the matrix AA is a block-diagonal matrix with pp square blocks along the diagonal, each block being the adjacency matrix of one component, and all off-block entries zero. This permutation highlights the independence of the components.[1] Although simple undirected graphs typically exclude self-loops, allowing loops in more general graph models modifies the adjacency matrix such that the diagonal entry aii=1a_{ii} = 1 (or the loop's weight) if vertex ii has a self-loop, deviating from the zero diagonal of loop-free graphs. This convention enables representation of reflexive edges in applications like multigraphs or directed graphs with possible feedback.[5]

Mathematical Properties

Spectral Properties

The spectrum of a graph is defined as the multiset of eigenvalues of its adjacency matrix AA. For an undirected simple graph without loops, AA is a real symmetric matrix, so its eigenvalues are real numbers and it admits an orthogonal basis of eigenvectors.[21] The eigenvalues λ1,λ2,,λn\lambda_1, \lambda_2, \dots, \lambda_n (ordered such that λ1λ2λn\lambda_1 \geq \lambda_2 \geq \dots \geq \lambda_n) satisfy the characteristic equation det(AλI)=0\det(A - \lambda I) = 0, where II is the identity matrix. Since AA has zero diagonal entries (no loops), its trace is zero, implying that the sum of the eigenvalues equals zero: i=1nλi=0\sum_{i=1}^n \lambda_i = 0.[21] The largest eigenvalue λ1\lambda_1, known as the spectral radius ρ(A)\rho(A), provides key insights into graph structure. For any graph, ρ(A)Δ\rho(A) \leq \Delta, where Δ\Delta is the maximum vertex degree, with equality if and only if the graph is regular.[22] For a connected graph, the Perron–Frobenius theorem applies to the nonnegative irreducible matrix AA, ensuring that ρ(A)\rho(A) is a simple eigenvalue with a positive eigenvector (the Perron vector), and ρ(A)>λi\rho(A) > |\lambda_i| for all other eigenvalues λi\lambda_i.[23] In a kk-regular graph, λ1=k\lambda_1 = k with multiplicity equal to the number of connected components.[21] A representative example is the complete graph KnK_n on nn vertices, whose adjacency matrix has eigenvalues n1n-1 (with multiplicity 1, corresponding to the all-ones eigenvector) and 1-1 (with multiplicity n1n-1).[21] This spectrum reflects the high connectivity, with the spectral radius n1n-1 matching the regular degree.

Isomorphism and Graph Invariants

The adjacency matrix provides a fundamental tool for determining graph isomorphism. Two graphs GG and HH with adjacency matrices AA and BB, respectively, are isomorphic if and only if there exists a permutation matrix PP such that B=P1APB = P^{-1} A P.[24] For undirected graphs, where AA and BB are symmetric, this relation simplifies to B=PTAPB = P^T A P, reflecting the orthogonal nature of permutation matrices.[24] This permutation similarity captures the structural equivalence of the graphs under vertex relabeling, as the matrix entries correspond directly to edge incidences. However, the adjacency matrix itself is not a complete graph invariant due to its dependence on vertex labeling. Isomorphic graphs share the same adjacency matrix up to such a similarity transformation, but distinct labelings of the same graph yield different matrices without applying the permutation.[5] Consequently, direct matrix equality does not imply isomorphism; instead, one must verify the existence of the transforming permutation, which underpins the computational challenge of the graph isomorphism problem.[25] Several properties of the adjacency matrix serve as graph invariants. The total number of 1's in AA equals twice the number of edges in an undirected simple graph without loops, providing a basic measure of graph density.[24] The row sums of AA correspond to the degrees of the vertices, yielding the degree sequence as an invariant multiset.[24] More sophisticated invariants include the permanent of AA, which counts the number of permutation matrices subordinate to AA and remains unchanged under permutation similarity, useful for enumerating perfect matchings in bipartite cases.[26] Immanants, generalizations of the permanent and determinant via irreducible characters of the symmetric group, offer further invariants that capture cycle index symmetries and have applications in distinguishing non-isomorphic graphs.[26] The spectrum of AA—its multiset of eigenvalues—is another key invariant preserved under similarity.[24]

Matrix Powers and Walk Counting

One of the key applications of the adjacency matrix in graph theory lies in its powers, which provide a means to count walks between vertices. Specifically, if AA is the adjacency matrix of a graph GG, then the (i,j)(i,j)-entry of AkA^k, denoted (Ak)ij(A^k)_{ij}, equals the number of walks of length kk from vertex ii to vertex jj in GG. This result follows from the structure of matrix multiplication, where each term in the product corresponds to extending a walk by one edge at a time. In undirected graphs, these walks may revisit vertices and edges, and the count (Ak)ii(A^k)_{ii} for i=ji = j gives the number of closed walks of length kk based at vertex ii. Such closed walks offer insights into the graph's local connectivity and periodic structures around ii. Moreover, the existence of a positive entry in some power AkA^k (for kn1k \leq n-1, where nn is the number of vertices) indicates the presence of a walk between ii and jj, which in connected undirected graphs implies overall connectivity via the union of such powers. For directed graphs, the theorem extends analogously, but the walks must respect edge directions, enabling the enumeration of directed paths and circuits. The adjacency matrix for digraphs is typically not symmetric, yet the power AkA^k still captures the number of directed walks of length kk from ii to jj. A concrete illustration occurs for k=2k=2: the diagonal entries of A2A^2 equal the degrees of the vertices in undirected graphs, since (A2)ii=lAilAli=li1=deg(i)(A^2)_{ii} = \sum_{l} A_{il} A_{li} = \sum_{l \sim i} 1 = \deg(i).[27] More generally, the trace tr(Ak)=i(Ak)ii\operatorname{tr}(A^k) = \sum_{i} (A^k)_{ii} equals the total number of closed walks of length kk across all vertices, a quantity that relates to cycle enumeration in broader combinatorial contexts by summing contributions from all possible closed structures. To see this in action, consider the path graph P3P_3 on vertices {1,2,3}\{1,2,3\} with edges 121-2 and 232-3. Its adjacency matrix is
A=(010101010). A = \begin{pmatrix} 0 & 1 & 0 \\ 1 & 0 & 1 \\ 0 & 1 & 0 \end{pmatrix}.
The second power is
A2=(101020101), A^2 = \begin{pmatrix} 1 & 0 & 1 \\ 0 & 2 & 0 \\ 1 & 0 & 1 \end{pmatrix},
where (A2)13=1(A^2)_{13} = 1 counts the single walk 1231 \to 2 \to 3 of length 2, and (A2)22=2(A^2)_{22} = 2 counts the closed walks 2122 \to 1 \to 2 and 2322 \to 3 \to 2. The off-diagonal entries like (A2)11=1(A^2)_{11} = 1 and (A2)13=1(A^2)_{13} = 1 highlight connections to neighbors of neighbors, illustrating how A2A^2 reveals the graph's two-step reachability.[27]

Computational Aspects

Storage and Data Structures

The adjacency matrix is typically stored in a dense representation as a full n×nn \times n array, where nn is the number of vertices, requiring O(n2)O(n^2) space regardless of the number of edges.[28] This approach is suitable for dense graphs, where the edge count approaches n2n^2, as it avoids wasting space on absent edges and enables constant-time access to any potential edge via direct indexing.[28] For sparse graphs, where the number of edges is significantly less than n2n^2, compressed formats like the compressed sparse row (CSR) or compressed sparse column (CSC) are used to store only the non-zero entries along with their row and column indices.[29] In CSR, for instance, the non-zero values are kept in a single array, with auxiliary arrays tracking the starting positions of each row and the column indices of the non-zeros, achieving space proportional to the number of edges plus vertices.[29] Similarly, CSC transposes this structure for column-major access.[29] In unweighted graphs, the adjacency matrix can be implemented as a bit matrix, packing each entry into a single bit to reduce space to approximately O(n2/8)O(n^2 / 8) bytes, assuming byte-aligned storage.[30] This bit-packing is efficient for binary presence/absence indicators but requires bitwise operations for access, which may introduce minor overhead compared to full integer arrays.[30] These storage choices involve trade-offs in access time and flexibility: dense representations offer O(1)O(1) edge queries but fixed high space usage, while sparse formats like CSR or CSC provide better space efficiency for low-edge-density graphs at the cost of slower insertions, which may require reallocating index arrays.[28] For weighted graphs, entries must store floating-point or integer weights rather than bits or simple indicators, increasing per-entry space needs to 4 or 8 bytes typically.[31]

Algorithms and Operations

The multiplication of two adjacency matrices AA and BB, where AA is the adjacency matrix of graph G1G_1 and BB of graph G2G_2, yields the adjacency matrix of the matrix product graph, a construction that combines the structures of G1G_1 and G2G_2 by connecting vertices based on paths through intermediate nodes.[32] This operation, defined over the reals or integers, counts the number of walks of length 2 between vertices in the product graph, with applications in graph factorization and relational composition.[32] In the context of reachability, matrix multiplication under the boolean semiring (replacing addition with logical OR and multiplication with AND) computes transitive closures, enabling detection of paths between nodes.[33] The standard algorithm for n×nn \times n matrix multiplication has O(n3)O(n^3) time complexity, but Strassen's algorithm reduces this to O(nlog27)O(n2.807)O(n^{\log_2 7}) \approx O(n^{2.807}) by recursively partitioning matrices into 2x2 blocks and performing seven multiplications instead of eight. Addition of adjacency matrices corresponds to the direct sum for the disjoint union of graphs, forming a block-diagonal matrix where the blocks are the individual adjacency matrices, preserving the disconnected components without inter-graph edges.[34] This operation maintains the sparsity and structure of the components, with the resulting matrix size equal to the sum of the original dimensions.[35] For directed graphs, the transpose of the adjacency matrix ATA^T reverses all edge directions, transforming outgoing edges into incoming ones and vice versa, which is equivalent to the adjacency matrix of the transpose graph.[36] This reversal is computationally efficient at O(n2)O(n^2) time for dense matrices but can leverage sparsity for faster execution.[36] Key algorithms utilizing adjacency matrices include the Floyd-Warshall algorithm for all-pairs shortest paths, which iteratively updates a distance matrix initialized from the adjacency matrix (or infinity where no edges exist) using dynamic programming to consider intermediate vertices, achieving O(n3)O(n^3) time. Gaussian elimination applied to the adjacency matrix solves linear systems Ax=bA \mathbf{x} = \mathbf{b} over fields like the reals or GF(2), useful in graph algorithms such as computing matchings or analyzing connectivity, with complexity O(n3)O(n^3) in the dense case but reducible via sparsity patterns.[37] Matrix-vector multiplication AvA \mathbf{v}, fundamental for iterative methods like power iteration on graphs, has O(n2)O(n^2) complexity for dense adjacency matrices but improves to O(E)O(|E|) when exploiting sparsity, as only non-zero entries contribute to the result.[38]

References

Table of Contents