Template:Short description In graph theory and computer science, 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.
In the special case of a finite simple graph, the adjacency matrix is a (0,1)-matrix with zeros on its diagonal. If the graph is undirected (i.e. all of its edges are bidirectional), the adjacency matrix is symmetric. The relationship between a graph and the eigenvalues and eigenvectors of its adjacency matrix is studied in spectral graph theory.
The adjacency matrix of a graph should be distinguished from its incidence matrix, a different matrix representation whose elements indicate whether vertex–edge pairs are incident or not, and its degree matrix, which contains information about the degree of each vertex.
DefinitionEdit
For a simple graph with vertex set Template:Math, the adjacency matrix is a square Template:Math matrix Template:Mvar such that its element Template:Mvar is 1 when there is an edge from vertex Template:Math to vertex Template:Math, and 0 when there is no edge.<ref>Template:Citation.</ref> The diagonal elements of the matrix are all 0, since edges from a vertex to itself (loops) are not allowed in simple graphs. It is also sometimes useful in algebraic graph theory to replace the nonzero elements with algebraic variables.<ref>Template:Citation.</ref> The same concept can be extended to multigraphs and graphs with loops by storing the number of edges between each two vertices in the corresponding matrix element, and by allowing nonzero diagonal elements. Loops may be counted either once (as a single edge) or twice (as two vertex-edge incidences), as long as a consistent convention is followed. Undirected graphs often use the latter convention of counting loops twice, whereas directed graphs typically use the former convention.
Of a bipartite graphEdit
The adjacency matrix Template:Mvar of a bipartite graph whose two parts have Template:Mvar and Template:Mvar vertices can be written in the form
- <math>A = \begin{pmatrix} 0_{r,r} & B \\ B^\mathsf{T} & 0_{s,s} \end{pmatrix},</math>
where Template:Mvar is an Template:Math matrix, and Template:Math and Template:Math represent the Template:Math and Template:Math zero matrices. In this case, the smaller matrix Template:Mvar uniquely represents the graph, and the remaining parts of Template:Mvar can be discarded as redundant. Template:Mvar is sometimes called the biadjacency matrix.
Formally, let Template:Math be a bipartite graph with parts Template:Math, Template:Math and edges Template:Mvar. The biadjacency matrix is the Template:Math 0–1 matrix Template:Mvar in which Template:Math if and only if Template:Math.
If Template:Mvar is a bipartite multigraph or weighted graph, then the elements Template:Mvar are taken to be the number of edges between the vertices or the weight of the edge Template:Math, respectively.
VariationsEdit
An Template:Nowrap-adjacency matrix Template:Mvar of a simple graph has Template:Math if Template:Math is an edge, Template:Mvar if it is not, and Template:Mvar on the diagonal. The Seidel adjacency matrix is a Template:Math-adjacency matrix. This matrix is used in studying strongly regular graphs and two-graphs.<ref>Template:Cite journal</ref>
The distance matrix has in position Template:Math the distance between vertices Template:Mvar and Template:Mvar. The distance is the length of a shortest path connecting the vertices. Unless lengths of edges are explicitly provided, the length of a path is the number of edges in it. The distance matrix resembles a high power of the adjacency matrix, but instead of telling only whether or not two vertices are connected (i.e., the connection matrix, which contains Boolean values), it gives the exact distance between them.
ExamplesEdit
Undirected graphsEdit
The convention followed here (for undirected graphs) is that each edge adds 1 to the appropriate cell in the matrix, and each loop (an edge from a vertex to itself) adds 2 to the appropriate cell on the diagonal in the matrix.<ref>Template:Cite conference</ref> This allows the degree of a vertex to be easily found by taking the sum of the values in either its respective row or column in the adjacency matrix.
Labeled graph | Adjacency matrix |
---|---|
File:6n-graph2.svg | <math>\begin{pmatrix}
2 & 1 & 0 & 0 & 1 & 0\\
1 & 0 & 1 & 0 & 1 & 0\\
0 & 1 & 0 & 1 & 0 & 0\\
0 & 0 & 1 & 0 & 1 & 1\\
1 & 1 & 0 & 1 & 0 & 0\\
0 & 0 & 0 & 1 & 0 & 0
\end{pmatrix}</math>
|
File:Symmetric group 4; Cayley graph 1,5,21 (Nauru Petersen); numbers.svg | File:Symmetric group 4; Cayley graph 1,5,21 (adjacency matrix).svg
|
Directed graphsEdit
The adjacency matrix of a directed graph can be asymmetric. One can define the adjacency matrix of a directed graph either such that
- a non-zero element Template:Mvar indicates an edge from Template:Mvar to Template:Mvar or
- it indicates an edge from Template:Mvar to Template:Mvar.
The former definition is commonly used in graph theory and social network analysis (e.g., sociology, political science, economics, psychology).<ref> Template:Citation</ref> The latter is more common in other applied sciences (e.g., dynamical systems, physics, network science) where Template:Mvar is sometimes used to describe linear dynamics on graphs.<ref> Template:Citation</ref>
Using the first definition, the in-degrees of a vertex can be computed by summing the entries of the corresponding column and the out-degree of vertex by summing the entries of the corresponding row. When using the second definition, the in-degree of a vertex is given by the corresponding row sum and the out-degree is given by the corresponding column sum.
Labeled graph | Adjacency matrix |
---|---|
File:Symmetric group 4; Cayley graph 4,9; numbers.svg
|
File:Symmetric group 4; Cayley graph 4,9 (adjacency matrix).svg
|
Trivial graphsEdit
The adjacency matrix of a complete graph contains all ones except along the diagonal where there are only zeros. The adjacency matrix of an empty graph is a zero matrix.
PropertiesEdit
SpectrumEdit
The adjacency matrix of an undirected simple graph is symmetric, and therefore has a complete set of real eigenvalues and an orthogonal eigenvector basis. The set of eigenvalues of a graph is the spectrum of the graph.<ref>Template:Harvtxt, Chapter 2 ("The spectrum of a graph"), pp. 7–13.</ref> It is common to denote the eigenvalues by <math>\lambda_1\geq \lambda_2\geq \cdots \geq \lambda_n.</math>
The greatest eigenvalue <math>\lambda_1</math> is bounded above by the maximum degree. This can be seen as result of the Perron–Frobenius theorem, but it can be proved easily. Let Template:Mvar be one eigenvector associated to <math>\lambda_1</math> and Template:Mvar the entry in which Template:Mvar has maximum absolute value. Without loss of generality assume Template:Mvar is positive since otherwise you simply take the eigenvector -Template:Mvar, also associated to <math>\lambda_1</math>. Then
- <math>\lambda_1 v_x = (Av)_x = \sum_{y=1}^n A_{x,y}v_y \leq \sum_{y=1}^n A_{x,y} v_x = v_x \deg(x).</math>
For Template:Mvar-regular graphs, Template:Mvar is the first eigenvalue of Template:Mvar for the vector Template:Math (it is easy to check that it is an eigenvalue and it is the maximum because of the above bound). The multiplicity of this eigenvalue is the number of connected components of Template:Mvar, in particular <math>\lambda_1>\lambda_2</math> for connected graphs. It can be shown that for each eigenvalue <math>\lambda_i</math>, its opposite <math>-\lambda_i = \lambda_{n+1-i}</math> is also an eigenvalue of Template:Mvar if Template:Mvar is a bipartite graph.<ref>Template:Citation</ref> In particular −Template:Mvar is an eigenvalue of any Template:Mvar-regular bipartite graph.
The difference <math>\lambda_1 - \lambda_2</math> is called the spectral gap and it is related to the expansion of Template:Mvar. It is also useful to introduce the spectral radius of <math>A</math> denoted by <math>\lambda(G) = \max_{\left|\lambda_i\right| < d} |\lambda_i|</math>. This number is bounded by <math>\lambda(G) \geq 2\sqrt{d-1} - o(1)</math>. This bound is tight in the Ramanujan graphs.
Isomorphism and invariantsEdit
Suppose two directed or undirected graphs Template:Math and Template:Math with adjacency matrices Template:Math and Template:Math are given. Template:Math and Template:Math are isomorphic if and only if there exists a permutation matrix Template:Mvar such that
- <math>P A_1 P^{-1} = A_2.</math>
In particular, Template:Math and Template:Math are similar and therefore have the same minimal polynomial, characteristic polynomial, eigenvalues, determinant and trace. These can therefore serve as isomorphism invariants of graphs. However, two graphs may possess the same set of eigenvalues but not be isomorphic.<ref>Godsil, Chris; Royle, Gordon Algebraic Graph Theory, Springer (2001), Template:ISBN, p.164</ref> Such linear operators are said to be isospectral.
Matrix powersEdit
If Template:Mvar is the adjacency matrix of the directed or undirected graph Template:Mvar, then the matrix Template:Math (i.e., the matrix product of Template:Mvar copies of Template:Mvar) has an interesting interpretation: the element Template:Math gives the number of (directed or undirected) walks of length Template:Mvar from vertex Template:Mvar to vertex Template:Mvar. If Template:Mvar is the smallest nonnegative integer, such that for some Template:Mvar, Template:Mvar, the element Template:Math of Template:Math is positive, then Template:Mvar is the distance between vertex Template:Mvar and vertex Template:Mvar. A great example of how this is useful is in counting the number of triangles in an undirected graph Template:Mvar, which is exactly the trace of Template:Math divided by 3 or 6 depending on whether the graph is directed or not. We divide by those values to compensate for the overcounting of each triangle. In an undirected graph, each triangle will be counted twice for all three nodes, because the path can be followed clockwise or counterclockwise : ijk or ikj. The adjacency matrix can be used to determine whether or not the graph is connected.
If a directed graph has a nilpotent adjacency matrix (i.e., if there exists Template:Mvar such that Template:Math is the zero matrix), then it is a directed acyclic graph.<ref>Template:Cite journal</ref>
Data structuresEdit
The adjacency matrix may be used as a data structure for the representation of graphs in computer programs for manipulating graphs. The main alternative data structure, also in use for this application, is the adjacency list.<ref>Template:Harvtxt, p. 361: "There are two data structures that people often use to represent graphs, the adjacency list and the adjacency matrix."</ref><ref name="clrs">Template:Citation.</ref>
The space needed to represent an adjacency matrix and the time needed to perform operations on them is dependent on the matrix representation chosen for the underlying matrix. Sparse matrix representations only store non-zero matrix entries and implicitly represent the zero entries. They can, for example, be used to represent sparse graphs without incurring the space overhead from storing the many zero entries in the adjacency matrix of the sparse graph. In the following section the adjacency matrix is assumed to be represented by an array data structure so that zero and non-zero entries are all directly represented in storage.
Because each entry in the adjacency matrix requires only one bit, it can be represented in a very compact way, occupying only Template:Math bytes to represent a directed graph, or (by using a packed triangular format and only storing the lower triangular part of the matrix) approximately Template:Math bytes to represent an undirected graph. Although slightly more succinct representations are possible, this method gets close to the information-theoretic lower bound for the minimum number of bits needed to represent all Template:Mvar-vertex graphs.<ref>Template:Citation.</ref> For storing graphs in text files, fewer bits per byte can be used to ensure that all bytes are text characters, for instance by using a Base64 representation.<ref>Template:Citation.</ref> Besides avoiding wasted space, this compactness encourages locality of reference. However, for a large sparse graph, adjacency lists require less storage space, because they do not waste any space representing edges that are not present.<ref name="clrs"/><ref name="gt"/>
An alternative form of adjacency matrix (which, however, requires a larger amount of space) replaces the numbers in each element of the matrix with pointers to edge objects (when edges are present) or null pointers (when there is no edge).<ref name="gt"/> It is also possible to store edge weights directly in the elements of an adjacency matrix.<ref name="clrs"/>
Besides the space tradeoff, the different data structures also facilitate different operations. Finding all vertices adjacent to a given vertex in an adjacency list is as simple as reading the list, and takes time proportional to the number of neighbors. With an adjacency matrix, an entire row must instead be scanned, which takes a larger amount of time, proportional to the number of vertices in the whole graph. On the other hand, testing whether there is an edge between two given vertices can be determined at once with an adjacency matrix, while requiring time proportional to the minimum degree of the two vertices with the adjacency list.<ref name="clrs"/><ref name="gt">Template:Citation.</ref>
See alsoEdit
ReferencesEdit
External linksEdit
- {{#invoke:Template wrapper|{{#if:|list|wrap}}|_template=cite web
|_exclude=urlname, _debug, id |url = https://mathworld.wolfram.com/{{#if:AdjacencyMatrix%7CAdjacencyMatrix.html}} |title = Adjacency matrix |author = Weisstein, Eric W. |website = MathWorld |access-date = |ref = Template:SfnRef }}
- Fluffschack — an educational Java web start game demonstrating the relationship between adjacency matrices and graphs.
- Open Data Structures - Section 12.1 - AdjacencyMatrix: Representing a Graph by a Matrix, Pat Morin
- Café math : Adjacency Matrices of Graphs : Application of the adjacency matrices to the computation generating series of walks.
Template:Graph representations Template:Matrix classes Template:Authority control