Tutorials › Graph Theory (Undergraduate) › Recovering Degrees And Neighborhoods From Adjacency Matrices

Adjacency and Laplacian Matrices · Tutorial 617 of 799

Recovering Degrees And Neighborhoods From Adjacency Matrices

Use row sums and row supports to recover degrees and neighborhoods, then test regularity directly from an adjacency matrix.

Intermediate 8 min read

What You'll Learn

  • Define adjacency-matrix entries using a fixed ordering of the vertices
  • Recover a vertex's degree by summing its row
  • Recover a vertex's neighborhood from the positions of the nonzero entries
  • Identify isolated vertices from zero rows
  • Test whether a graph is regular by comparing all row sums
  • Check matrix calculations against the graph's edge list

Reading Degrees and Neighborhoods from Rows

An adjacency matrix records which pairs of vertices are joined by edges. Once the vertices have been assigned an order, each row records the connections of one vertex: the positions of its nonzero entries identify its neighbors, and the number of those entries gives its degree. These facts turn basic graph information into direct matrix calculations.

Throughout, \(G=(V,E)\) is a finite simple undirected graph, with no loops or multiple edges. Fix an ordering \(V=\{v_1,\dots,v_n\}\). Its adjacency matrix is the \(n\) by \(n\) matrix \(A=(a_{ij})\) defined by \(a_{ij}=1\) when \(v_i v_j\in E\), and \(a_{ij}=0\) otherwise. Since the graph is undirected, \(a_{ij}=a_{ji}\); since it has no loops, \(a_{ii}=0\).

Definition: The support of row \(i\) is the set of column indices \(j\) for which \(a_{ij}\ne 0\). For a simple graph's adjacency matrix, this row support identifies the neighborhood of \(v_i\): \(N(v_i)=\{v_j:a_{ij}=1\}\).

There are two details to keep in mind. First, a support is a set of column positions, not a set of matrix-entry values: the value \(1\) tells us that a neighbor is present, while the column index tells us which vertex it is. Second, the entries are added as ordinary integers. The degree counts incident edges, so it is the number of \(1\)s in the row.

Proposition: For every vertex \(v_i\) of a finite simple undirected graph, the sum of row \(i\) of its adjacency matrix is \(\deg(v_i)\), and the row support identifies \(N(v_i)\).

Proof. By definition, \(a_{ij}=1\) exactly when \(v_j\) is adjacent to \(v_i\), and is \(0\) otherwise. Thus the indices of the nonzero entries in row \(i\) are exactly the vertices in \(N(v_i)\). Each such entry contributes \(1\) to the row sum, and all other entries contribute \(0\). Therefore the row sum is the number of vertices adjacent to \(v_i\), which is \(\deg(v_i)\) in a simple graph. \(\square\)

Equivalently, if \(\mathbf{1}\) is the column vector of \(n\) ones, then \(A\mathbf{1}\) is the column vector of vertex degrees: its \(i\)th entry is the sum of row \(i\). This gives a compact way to check all degrees at once.

Worked Examples

Worked Example: Recovering Every Degree in a Cycle

Let \(G=(V,E)\) have \(V=\{1,2,3,4\}\) and \(E=\{12,23,34,41\}\). Use the vertex order \(1,2,3,4\). The adjacency matrix is

$$ A= \begin{pmatrix} 0&1&0&1\\ 1&0&1&0\\ 0&1&0&1\\ 1&0&1&0 \end{pmatrix}. $$

Row 1 has sum \(0+1+0+1=2\), with support \(\{2,4\}\), so \(\deg(1)=2\) and \(N(1)=\{2,4\}\). Row 2 has sum \(1+0+1+0=2\), with support \(\{1,3\}\). Row 3 has sum \(0+1+0+1=2\), with support \(\{2,4\}\). Row 4 has sum \(1+0+1+0=2\), with support \(\{1,3\}\). Thus every vertex has degree \(2\).

Check: The four listed edges contribute \(2+2+2+2=8\) total endpoint incidences. The row sums also total \(2+2+2+2=8\), which agrees. Reading the edge list directly gives the same neighborhoods: \(N(1)=\{2,4\}\), \(N(2)=\{1,3\}\), \(N(3)=\{2,4\}\), and \(N(4)=\{1,3\}\).

Worked Example: Detecting Unequal Degrees

Let \(G=(V,E)\) have \(V=\{1,2,3,4\}\) and \(E=\{12,13,23,34\}\), in the order \(1,2,3,4\). Then

$$ A= \begin{pmatrix} 0&1&1&0\\ 1&0&1&0\\ 1&1&0&1\\ 0&0&1&0 \end{pmatrix}. $$

The row calculations give:

VertexRowRow supportDegree
1\((0,1,1,0)\)\(\{2,3\}\)\(0+1+1+0=2\)
2\((1,0,1,0)\)\(\{1,3\}\)\(1+0+1+0=2\)
3\((1,1,0,1)\)\(\{1,2,4\}\)\(1+1+0+1=3\)
4\((0,0,1,0)\)\(\{3\}\)\(0+0+1+0=1\)

For example, the \(1\) in row 3, column 4 means that \(4\) is a neighbor of \(3\), while the zero in row 3, column 3 records the absence of a loop. The degrees \(2,2,3,1\) are not all equal.

Check: Counting directly from the edge list gives degrees \(2\) at vertex 1 (edges \(12,13\)), \(2\) at vertex 2 (edges \(12,23\)), \(3\) at vertex 3 (edges \(13,23,34\)), and \(1\) at vertex 4 (edge \(34\)). Their sum is \(2+2+3+1=8\). There are four edges, each with two endpoints, giving \(2\cdot4=8\) incidences as well.

Worked Example: A Zero Row and an Isolated Vertex

Let \(G=(V,E)\) have \(V=\{1,2,3,4,5\}\) and \(E=\{12,23,34\}\). In the order \(1,2,3,4,5\), its adjacency matrix is

$$ A= \begin{pmatrix} 0&1&0&0&0\\ 1&0&1&0&0\\ 0&1&0&1&0\\ 0&0&1&0&0\\ 0&0&0&0&0 \end{pmatrix}. $$

The row sums are \(1,2,2,1,0\). The corresponding neighborhoods are \(N(1)=\{2\}\), \(N(2)=\{1,3\}\), \(N(3)=\{2,4\}\), \(N(4)=\{3\}\), and \(N(5)=\varnothing\). The fifth row is all zero because vertex \(5\) has no neighbors. A vertex with degree zero is called isolated, so \(5\) is isolated.

Check: From the edge list, vertices \(1\) and \(4\) each occur in one edge, vertices \(2\) and \(3\) each occur in two edges, and vertex \(5\) occurs in none. The degree sum is \(1+2+2+1+0=6\), while the three edges contribute \(2\cdot3=6\) endpoint incidences.

Regularity as a Row-Sum Test

Definition: A graph is regular if all its vertices have the same degree. If every vertex has degree \(r\), the graph is \(r\)-regular.
Theorem: A finite simple undirected graph with adjacency matrix \(A\) is \(r\)-regular if and only if every row sum of \(A\) equals \(r\). Equivalently, \(A\mathbf{1}=r\mathbf{1}\).

Proof. Suppose first that \(G\) is \(r\)-regular. Then every vertex \(v_i\) has degree \(r\), and the row-sum proposition shows that row \(i\) sums to \(r\). This holds for every row, so \(A\mathbf{1}=r\mathbf{1}\). Conversely, suppose every row sums to \(r\). By the same proposition, \(\deg(v_i)=r\) for every vertex \(v_i\). Hence all vertices have the same degree, so \(G\) is \(r\)-regular. The argument also covers the graph with no vertices: it has no rows, and the condition that every row sum equals \(r\) is vacuously true for each \(r\); the equation \(A\mathbf{1}=r\mathbf{1}\) uses empty vectors and holds vacuously. \(\square\)

For a nonempty graph, the row sums give a direct test: compute them all and compare. If even two row sums differ, the graph is not regular. If they all agree, their common value is the degree of every vertex. The first example is \(2\)-regular; the second and third examples are not regular because their row sums are unequal.

Since the graph is undirected, \(A\) is symmetric, so column \(i\) also records the neighbors of \(v_i\). In particular, its column sum equals its row sum. For degree and neighborhood calculations, however, one row per vertex is enough; use the column positions to name the neighbors.

Common Mistakes and a Reliable Check

A frequent mistake is to treat the values in a row as vertex labels. For instance, in the graph \(V=\{1,2,3,4\}\), \(E=\{12,23,34\}\), the row for vertex \(2\) is \((1,0,1,0)\). Taking the set of distinct nonzero values gives \(\{1\}\), which is not the neighborhood. The nonzero entries occur in columns 1 and 3, so the correct result is \(N(2)=\{1,3\}\), and the row sum \(1+0+1+0=2\) gives degree \(2\).

Another error is to infer regularity from one row, or from a few selected rows. Regularity requires that every vertex have the same degree, so every row must be checked. In the second example, three rows have sums \(2,2,3\) before the fourth row, whose sum is \(1\); checking only the first two rows would miss the failure of regularity.

A useful consistency check is to add all row sums and compare with twice the number of edges. Each edge \(v_i v_j\) contributes a \(1\) in row \(i\), column \(j\), and a \(1\) in row \(j\), column \(i\). It therefore contributes exactly \(2\) to the sum of all entries. Thus the total of the degrees must equal \(2|E|\). This check can expose an arithmetic error, but it does not replace checking the individual rows when determining neighborhoods or regularity.

Check Your Understanding

For each question, use the stated vertex order when forming or reading a matrix.

  1. For \(V=\{1,2,3\}\) and \(E=\{12,13\}\), write the adjacency matrix, then give each row support, neighborhood, and degree.
  2. Explain why a zero row in the adjacency matrix of a simple graph corresponds to an isolated vertex, and why the converse also holds.
  3. A graph has adjacency-matrix row sums \(2,2,2,2\). What can you conclude about its degrees and regularity?
  4. For \(V=\{1,2,3,4\}\) and \(E=\{12,23,34,14\}\), verify regularity using row sums, then check the degrees directly from the edge list.
  5. Why does adding all row sums count every edge twice rather than once?