CS 6241: Numerics for Data Science

Matrices associated with graphs

Author

David Bindel

Published

April 8, 2025

Graphs and linear algebra

Formally, an unweighted graph is G=(V,E), where EV×V. Informally, V consists of things we want to model and E represents the relations between them. It is a very flexible representation: we use graphs to represent friendships between people, wires between routers, citations between papers, links between objects in a data structure, and many other things. When the bare topology of the relationships does not provide enough modeling power, we might also consider including functions on V or E corresponding to different attributes. The most common case is a scalar weight function assigned to each edge that corresponds to the importance of the relation: in a social network, for example, maybe a close and active friendship has more weight than a casual acquaintance. We use relationship information encoded in graphs to reason about logical groupings (whether we call them communities or clusters), about power relations and influence, and about dynamic processes like the spread of rumors or disease.

Matrix methods for network analysis rely on a sort of pun: we encode the network as a matrix, translate the question into linear algebraic terms , and commence to compute. There are many possible matrices associated with a graph, and we use them to reason about different things. We consider three interpretations of these network matrices:

  • A matrix may represent a linear map between different spaces, typically mapping vertex properties to edge properties, or vice-versa. Examples include the discrete gradient operator and the edge sum operator.

  • A matrix may represent an operator mapping the space of functions over the vertices (or over edges) to itself. Examples include transition matrices for random walks defined on the graph.

  • A matrix may represent a quadratic form mapping functions on the vertices (or edges) into scalars. Often the quadratic form has an easy to interpret meaning for special inputs; for example, the quadratic form for the combinatorial Laplacian counts cut edges in graph partitioning.

Adjacency and degrees

If we identify vertices in the graph with indices 1,,n, then the (unweighted) adjacency matrix ARn×n has entries aij={1,(i,j)E0,otherwise. For graphs in which edges have positive weights, we sometimes use the weighted adjacency matrix, with aij giving the edge weight for (i,j)E.

The degree di of a node is the sum of the weights on the incident edges. When the graph is directed, the in-degree and out-degree may differ; for the moment, we will stick with the directed case. We let dRn denote the vector of weighted node degrees, and let D denote the matrix in which the weighted node degrees appear on the diagonal.

The adjacency matrix and the degree matrices are building blocks for several other matrices, but they are also useful on their own. First, as a linear operator, the adjacency matrix accumulates values from neighbors; that is, (Ax)i=jNixj where Ni={j:(i,j)E} is the neighborhood of i. If xj is the number of paths of length k leading from starting point to node j, then (Ax)i is the number of paths of length k to all neighbors of node i — that is, the total number of paths of length k+1 to node j. Therefore, [Ak]ij=number of paths of length k from i to j. We use this formula in many ways; for example, it lets us write number of triangles in an undirected graph (closed cycles of length three) as number of triangles=13i[A3]ii=13tr(A3) where we divide by three because each triangle is counted once for each of its vertices. We also know, for example, how to approximate the number of long paths between i and j in terms of the dominant eigenvector (and associated eigenvalue) of A.

The matrix A also defines a quadratic form that is useful for counting edges. Let x{0,1}n be the indicator for a subset of vertices SV. Then xTAx=i,jaijxixj=(i,j)S×Saij, i.e. xTAx is the total (directed) edge weight between nodes in S, or twice the total undirected edge weight. If x is an indicator then xTx=|S|, and so xTAxxTx=mean degree within S. If S is a clique, the mean degree within S is |S|1; therefore |S|1=xTAxxTx=ρA(x)λmax(A), since λmax(A) is the largest possible value for the Rayleigh quotient ρA(x). Hence, the maximum clique size k(G) has the bound k(G)1+λmax(A). This is an example of a result in spectral graph theory, i.e. the study of graphs in terms of eigenvalues and eigenvectors of associated matrices. In fact, another continuous optimization problem due to Motzkin and Straus gives the clique number exactly: 11/k(G)=maxxΔnxTAx, where Δn{xRn:x0,eTx=1}. The optimization is now carried out over the simplex Δn rather than over the Euclidean unit ball used in the spectral bound.

If x is an indicator for a set S, we can also use the quadratic form xTDx=iSdi=edges incident on S. Therefore, xTDxxTAx=xT(DA)x=edges between S and SC. We will see more of the combinatorial Laplacian matrix L=DA shortly.

Random walks and normalized adjacency

Now consider a random walk on a G, i.e. a Markov process where the walker location Xt+1 at time t+1 is chosen randomly from among the neighbors of the previous location Xt with probability determined by the edge weights. Using the properties of conditional probability, P{Xt+1=i}=jP{Xt+1=i|Xt=j}P{Xt=j}, and the rule for randomly choosing a neighbor gives P{Xt+1=i|Xt=j}=aijdj. Letting πt+1Rn be the column vector whose entries represent the probability that Xt+1=i, and similarly with πt, we write the equation for conditional probability concisely as πt+1=(AD1)πt. The matrix T=AD1 is the transition matrix for the random walk Markov chain. Powers of T have an interpretation similar to that of powers of A, but rather than counting length k paths, Tk computes probabilities: [Tk]ij=P{Xk=i|X0=j}. Assuming the graph is connected and and aperiodic, the matrix T has a unique eigenvalue at 1, and all other eigenvalues are inside the unit circle. In this case, limkTk=T=(π)eT where π is a probability vector representing the stationary distribution for the Markov chain. In the undirected case, the stationary distribution is rather simple: πi=di/(2m). Things are more interesting for directed graphs.

While the eigenvalue at 1 and the associated stationary distribution are particularly interesting, the other eigenvalues and vectors are also interesting. In particular, suppose T=VΛV1, and consider TkT=VΛ¯kV1κ(V)|λ2|k, where κ(V)=VV1 is the condition number of the eigenvector matrix, Λ¯ is the diagonal matrix of eigenvalues with the eigenvalue at one replaced by zero, and |λ2| is the maximum modulus of all eigenvalues other than the eigenvalue at one. Therefore, the asymptotic rate of convergence of the Markov chain, also known as the mixing rate is determined by the second-largest eigenvalue modulus of T.

To understand the mixing rate in more detail (in the undirected case), it is helpful to consider the closely related normalized adjacency matrix A¯=D1/2AD1/2. Note that A¯=D1/2TD1/2, so (v,λ) an eigenpair of A¯(D1/2v,λ) an eigenpair of T. The eigenvalues of A¯ are critical points of ρA¯(x)=xTD1/2AD1/2xxTx; substituting x=D1/2y, we have ρA¯(D1/2y)=ρ(A,D)(y)=yTAyyTDy. If y is an indicator for a set S, this last expression represents the fraction of edges incident on S that are to other nodes in S. If z=2ye is +1 on S and 1 on Sc, then zTDz=2m and zTAz is 2m minus twice the total weight |C(S)| of edges from S to SC; hence, zTAzzTDz=1|C(S)|m If we restrict to the case where the same number of edges are incident on S and SC, then z is D-orthogonal to the all one vector, and so ρ(A,D)(z) is a lower bound on the eigenvalue closest to one. Thus, spectral analysis lets us bound the mixing rate in terms of the normalized cut size |C(S)|/m.

Discrete gradients and the Laplacian

For an unweighted graph, the discrete gradient GRm×n is a matrix in which each row represents an edge (i,j)E by (eiej)T. If x is an indicator for a set S, then Gx is nonzero (±1) only on edges between S and Sc; hence, Gx2=edges between S and SC. We can rewrite this as xTGTGx=xTLx where L=(i,j)E(eiej)(eiej)T. Each term in this sum contributes one to the lii and ljj entries through the eieiT and ejejT products; the cross terms fill in lij=lji=1. Putting everything together, we have L=DA. The matrix L is known as the combinatorial Laplacian, or sometimes simply as the Laplacian. The same construction holds in the weighted case, where it corresponds to L=(i,j)Eaij(eiej)(eiej)T where aij is the weight of the (i,j) edge. In either case, the smallest eigenvalue of L is zero, corresponding to an eigenvector of all ones. The multiplicity of the zero eigenvalue is equal to the number of connected components in the graph; assuming there is only one connected component, the second largest eigenvalue λ2 is a lower bound on xTLx for any xTe=0; if we choose x to be a ±1 vector indicating the split between equal size sets S and Sc, then xTLx also gives four times the number of edges cut by the partitioning. Hence, λ2/4 is a lower bound on the minimal bisector size.

The combinatorial Laplacian also can be interpreted as a linear operator, and in this guise it plays a role as the generator for a continuous-time random walk involving a random walk in which the time elapsed between each consecutive pair of steps is given by an independent exponential random variable with mean one. In this case, we have exp(sL)ij=P{X(s)=i|X(0)=j}. The matrix exp(sL) is known as the heat kernel on the graph because it can also describe continuous-time diffusion of heat on a graph.

The normalized Laplacian is L¯=D1/2LD1/2=IA¯; the eigenvalues of the normalized Laplacian can also be expressed as critical points of the generalized Rayleigh quotient ρ(L,D)(x)=xTLxxTDx. Thus twice the Rayleigh quotient gives the fraction of all edges that go between S and SC if x is a vector which is +1 on the set S and 1 on Sc.

Discrete sums and the signless Laplacian

The discrete sum operator is G+Rm×n where each row corresponds to an edge (i,j)E and has the form (ei+ej)T. The signless Laplacian is L+=D+A=(G+)T(G+)=(i,j)E(ei+ej)(ei+ej)T. The signless Laplacian is positive semi-definite; but unlike the combinatorial Laplacian, it may or may not have a null vector. If there is a null vector x for the signless Laplacian, then we must have xi=xj whenever (i,j)E; this implies that x indicates bipartite structure where the set S with positive elements can have edges to a set S with negative elements, but neither S nor S may have any other edges. There has been some work on the spectral theory for the signless Laplacian, but it is generally much less used than the combinatorial Laplacian.

Modularity matrix

Suppose we want to find a tight cluster in our graph. One approach is to look for sets of nodes that make xTAx large; but large relative to what? For an undirected graph, we use the quadratic form xTAx to count internal edges in a set. But a set may have many internal edges purely as an accident of having many high-degree nodes: high-degree nodes are highly likely to connect to each other simply because they are highly likely to connect to anyone! Hence, we would like a reference model so that we can see whether the edge density in a subgraph is “unusually” high relative to that reference.

The simplest reference model that takes into account the effect of degree distribution is sometimes called the configuration model. In the configuration model, we prescribe the vector d of expected node degrees. We then add m edges, each of which is (i,j) with probability didj/(2m)2; self-loops and repeating edges are allowed. With this construction, the expected adjacency is A¯=ddT2m, which has the correct expected degree distribution. The modularity matrix is defined to be B=AA¯, and if x is an indicator for S, the quadratic form xTBx indicates the number of “excess edges” within S compared to what we would predict in the configuration model.

And many more

The list of graph matrices that we have discussed is by no means exhaustive. We will see a few more examples this week, including the heat kernel matrix and the PageRank matrix. But there are many more besides; for example, recent work by Leskovec and Benson used a motif adjacency matrix M=(AA)A for which mij represents the number of triangles in the graph involving the edge (i,j). And one can define ever more exotic matrix representations. However, the adjacency, Laplacian, and their close neighbors suffice for many applications.

Footnotes

  1. “Mathematicians are like Frenchmen: whatever you say to them they translate into their own language and forthwith it is something entirely different.” – Goethe↩︎

  2. We use the convention that probability densities are column vectors, and that aij represents a transition from j to i. If G is directed, we also denote by D the out-degree of the nodes. This is consistent with the conventions in numerical linear algebra; in other areas, probability densities are typically rows.↩︎

  3. The graph is aperiodic if there is some k such that there is a length k path between any nodes i and j.↩︎