Post · 10 min read· pp. 55–61

The graph Laplacian

At the heart of of a number of important machine learning algorithms, such as spectral clustering, lies a matrix called the graph Laplacian. In this post, I’ll walk through the intuition behind the graph Laplacian and describe how it represents the discrete analogue to the Laplacian operator on continuous multivariate functions.

Introduction

At the heart of the field of spectral graph theory as well as a number of important machine learning algorithms, such as spectral clustering, lies a matrix called the graph Laplacian. (In fact, the first step in spectral clustering is to compute the Laplacian matrix of the data’s k-nearest neighbors graph… perhaps to be discussed in some future blog post.)

The Laplacian matrix is defined as follows: Given a graph G:=(V,E)G := (V, E) where V:={v1,v2,…,vn}V := \{v_1, v_2, \dots, v_n\} is the set of nodes/vertices and E:={e1,e2,…,em}E := \{e_1, e_2, \dots, e_m\} is the set of edges, with an adjacency matrix A∈{0,1}n×nA \in \{0,1\}^{n \times n}, where

Ai,j:={1,if there is an edge between vi and vj0,otherwiseA_{i,j} := \begin{cases} 1,& \text{if there is an edge between} \ v_i \ \text{and} \ v_j \\ 0, & \text{otherwise}\end{cases}

and degree matrix D∈Zn×nD \in \mathbb{Z}^{n \times n}, where

Di,j:={degree(vi),if i=j0,otherwiseD_{i,j} := \begin{cases} \text{degree}(v_i),& \text{if} \ i = j \\ 0, & \text{otherwise}\end{cases}

the Laplacian matrix is defined as

L:=D−AL := D - A

This definition is super simple, but it describes something quite deep: it’s the discrete analogue to the Laplacian operator on multivariate continuous functions. How does such a simple definition capture such a complex idea? We will demonstrate this in the remainder of this post.

Review of the Laplacian for continuous, multivariate functions

First, let’s review the basics behind the Laplacian for continuous, multivariate functions. Given a multivariate function

f:Rn→Rf: \mathbb{R}^n \rightarrow \mathbb{R}

the Laplacian of ff is defined as

Δf(x):=∇⋅∇f(x)\Delta f(\boldsymbol{x}) := \nabla \cdot \nabla f(\boldsymbol{x})

That is, the Laplacian of ff is the divergence of ff’s gradient.

Okay, let’s break this down. First, let’s discuss the gradient. The gradient of a multivariate function, denoted ∇f\nabla f, returns a vector field where the vector at each point x\boldsymbol{x}, denoted ∇f(x)\nabla f(\boldsymbol{x}), points in the direction of ff’s steepest ascent at x\boldsymbol{x}. Moreover, the magnitude of the gradient vector at point x\boldsymbol{x} is proportional to how much the function changes at x\boldsymbol{x} – i.e., how steep the function is at x\boldsymbol{x}. The steeper the function, the larger the gradient vector. This is depicted in the figure below:

drawing

One important point to keep in mind is that the gradient is an operator that takes as input a multivariate function and produces a vector field.

Now let’s discuss divergence. In contrast to the gradient, the divergence operator takes as input a vector field and produces a multivariate function. That is, given a vector field v(x)\boldsymbol{v}(\boldsymbol{x}), the divergence at point x\boldsymbol{x}, denoted ∇⋅v(x)\nabla \cdot \boldsymbol{v}(\boldsymbol{x}) is a scalar that is computed from the vectors that are nearby x\boldsymbol{x} in the vector field. What does the divergence describe? Intuitively, the divergence at x\boldsymbol{x} describes how much “flow” is coming into and out of x\boldsymbol{x} where the flow is described by the vectors in the vector field nearby x\boldsymbol{x}.

For example, in the following figure we depict three scenarios of a point x\boldsymbol{x} within a vector field. In the left-hand figure, there is more “flow” coming out of the point than into the point and thus, the divergence at this point is positive because flow is “diverging” away from the point. In the center figure, there is more “flow” into the point than going away from the point and thus, the divergence at this point is negative. Finally, in the right-hand figure, there is an equal amount of flow going into and out of the point and thus, the divergence is zero.

drawing

Finally, we come back to the Laplacian. Given a continuous, multivariate function ff, the Laplacian is simply the divergence of ff’s gradient:

drawing

Intuitively, what does the Laplacian describe? Well, following our understanding that ff’s gradient provides vectors that point in ff’s direction of steepest ascent with magnitude proportional to its steepness, we see that the “flow” at any point in this vector field will correspond to how much the steepness of ff is changing. If at some point, x\boldsymbol{x}, the steepness is not changing – that is, x\boldsymbol{x} is located at a steady incline or a steady decline – then the divergence of the gradient at x\boldsymbol{x} will be zero. That is, the Laplacian will be zero. On the other hand, if at x\boldsymbol{x}, the steepness is shrinking or growing – for example, because we’re close to a local maximum/minimum – then the divergence (i.e. Laplacian) will be either large or small (large if we’re approaching a minimum and small if we’re approaching a maximum).

Said succintly, the Laplacian tells us how much the steepness is changing. It’s the analogue to the second derivative for a continuous, single-variate function!

We will save a rigorous definition for the gradient, divergence, and laplacian for another blog post. In the meantime, this high-level intuition will hopefully be sufficient for understanding the core ideas behind the graph Laplacian.

Constructing a Laplacian for graphs

The Laplacian matrix LL for a graph G:=(V,E)G := (V, E) captures the same idea as the Laplacian for continuous, multivariate functions. To see how this is, we will need to construct all of the analogous components for graphs that are required to construct the Laplacian for continuous, multivariate functions. Specifically, we need to define graph analogs for points, functions, gradients, and divergences. For the remainder of this blog post, we’ll use the following graph as an illustration:

drawing

Points

This one is easy. Each vertex v∈Vv \in V in our graph will be analogous to a point in a Euclidean space.

Functions

We can define a function on the graph ff as simply a mapping from each vertex/point to a real number:

f:V→Rf: V \rightarrow \mathbb{R}

For example, in our example graph, such a function can be visualized as follows:

drawing

If we order the vertices in the graph, v1,v2,…,vnv_1, v_2, \dots, v_n, then we can represent the function as a vector:

f:=[f(v1)f(v2)…f(vn)]\boldsymbol{f} := \begin{bmatrix}f(v_1) & f(v_2) & \dots & f(v_n)\end{bmatrix}

Gradient

Intuitively, the gradient of a function tells us how much and in what direction a function is changing at each point. The analog for a graph is simply the set of edges between the vertices. The “magnitude” of a gradient vector then simply corresponds to the difference in the functions values between the two vertices. That is, for edge ek=(vi,vj)∈Ee_k = (v_i, v_j) \in E connecting node viv_i to node vjv_j (where i<ji < j in the vertices’ ordering) its “gradient” can be taken to be

g(ek):=f(vi)−f(vj)g(e_k) := f(v_i) - f(v_j)

These “edge gradients” are depicted below:

drawing

Note, the ordering of vertices tells us which vertex’s function value is subtracted from the other. For the purpose of constructing the Laplacian matrix, this order is arbitrary. Now, if we order all of the edges in the graph similarly to how we ordered all of the vertices, then we can represent the gradient as a vector:

g:=[g(e1)g(e2)…g(em)]\boldsymbol{g} := \begin{bmatrix}g(e_1) & g(e_2) & \dots & g(e_m)\end{bmatrix}

Now, a natural question becomes: given a graph-function f\boldsymbol{f}, how do we construct a matrix operator on f\boldsymbol{f} that returns g\boldsymbol{g} as described above? The answer is the Incidence matrix KK. The columns of KK correspond to the sorted vertices and the rows correspond to the sorted edges. For vertex viv_i and edge ej:=(vk,vh)e_j := (v_k, v_h), the entry Ki,jK_{i,j} is defined as

Ki,j:={1,if i==k and i>h1,if i==h and i>k−1,if i==k and i<h−1,if i==h and i<k0,otherwiseK_{i,j} := \begin{cases} 1,& \text{if} \ i == k \ \text{and} \ i > h \\ 1,& \text{if} \ i == h \ \text{and} \ i > k\\ -1,& \text{if} \ i == k \ \text{and} \ i < h \\ -1,& \text{if} \ i == h \ \text{and} \ i < k \\ 0, & \text{otherwise}\end{cases}

Given our example graph, the incidence matrix would be as follows:

drawing

Then, given a graph function g\boldsymbol{g}, we compute the “gradient” as

drawing

Divergence

Finally, how should we think about a divergence analogue for graphs? Recall that divergence on continuous, multivariate real-valued functions measures the amount of “flow” coming into and out of each point where the flow is determined by the vectors surrounding each point. Since, our analogue for points are vertices, and our analogue for vectors around each point are simply the edges adjacent to each vertex, it follows that we can calculate the divergence by simply summing the edge values around each vertex.

For our example graph, if we have edge assignments given by g(e1),g(e2),g(e3)g(e_1), g(e_2), g(e_3) and g(e4)g(e_4), then the divergence at node A should simply be the sum of the edge values adjacent to A where each edge value is multiplied by −1-1 if the flow is flowing “into” A. In the case of node A, all edges flow outward and thus

divergence(A)=g(e1)+g(e2)+g(e4)\text{divergence}(A) = g(e_1) + g(e_2) + g(e_4)

What matrix will perform this summation? Precisely the transpose of the incidence matrix KTK^T!

drawing

Now, putting it all together, the Laplacian on a graph is, like the Laplacian for real-valued functions, simply the divergence applied to the gradient of ff. Recall from linear algebra that, the composition of two operators is simply the product of the two matrices representing those operators. Applying the divergence to the gradient is carried out by the matrix product KTKK^TK. This is the Laplacian matrix LL. That is,

L:=KTKL := K^TK

If we inspect KTKK^TK more closely, we can show that this matrix is exactly the diagonal matrix minus the adjacency matrix that we introduced at the beginning of this post!

KTK=D−AK^TK = D - A

Why is this case? Recall that for matrix multiplication ABAB, where AA and BB are matrices, we can compute element i,ji,j of the resultant matrix by taking the dot product of the iith row of AA with the jjth column of BB. In our case, AA is the transpose of BB and thus, the iith row of AA is equivalent to the jjth column of BB. Thus, computing element i,ji,j of KTKK^TK is simply taking the dot product between columns ii and jj of KK.

What does each column of KK represent? Well, first, we know that each column of KK corresponds to a vertex. Each row corresponds to an edge. Thus, the value of the ttth entry of the iith column can be understood as an indicator of whether edge ete_t is incident upon vector viv_i. That is, edge ete_t is incident upon viv_i if Kt,i≠0K_{t,i} \neq 0.

With this in mind, let’s look at what the diagonal entries of KTKK^TK would be. That is, what would be element i,ii,i of KTKK^TK? Well, we would simply take the dot product of the iith column of KK with itself:

drawing

Notice that each value of −1-1 is multiplied with another −1-1 and each 11 is multiplied with a 11, which simply ends up summing up the number of edges adjacent to vertex viv_i – that is, the degree of vertex viv_i. Thus, the diagonal elements of KTK^T are simply the degrees of each vertex.

What happens when we compute an off-diagonal entry of KTKK^TK? That is, what would be entry i,ji,j of KTKK^TK (where i≠ji \neq j)? This would be taking the dot product between the iith and jjth columns of KK:

drawing

Because the iith column of KK represents the “edge indicators” of all edges adjacent to viv_i, we see that the dot product filters out all edges that are not adjacent to both viv_i and vjv_j. Thus, if viv_i is adjacent to vjv_j there will be only one non-zero term in the dot product’s summation corresponding to the one edge that connects them! This term is simply −1-1. Why is it always negative? By design of the incidence matrix each row has one value of -1 and one value of 1. When multiplied together, this results in -1.

And there you have it. The Laplacian matrix as the graph analogue to the Laplacian operator on multi-variate, continuous functions!