Skip to content
ImagineWorld

Matrices and Graphs

node_idnode-book_mathematics-of-machine-learning_chapter-08-matrices-and-graphs
node_typechapter_node
schema_profileworld.chapter_node.v1
entropyhigh
manifoldworld
domainimagineworld.net
authorityinforms
layercorpus
statusactive
chapter_num8
chapter_titleMatrices and Graphs
governed_byCORPUS
constitutional_citationCONSTITUTION
local graph

Now that we have gotten past the hard part (that is, the singular value decomposition and other matrix factorizations), it’s time to finish our journey through linear algebra with a bang. In my teaching experience, one of students’ most common concerns is the apparent disconnect between practice and theory. Among machine learning practitioners and software engineers, there’s often a reluctance to touch anything that is not immediately valuable in practice.

As a mathematician, I completely get where this dread comes from. We are often taught arcane topics of no practical importance, taking valuable time away from hacking and slashing our way through data.

In this chapter, we’ll look at a subject that is not immediately useful for your machine learning practice, but will pay serious dividends in the future. Considering how beautiful it is, it might be the inspiration for your next genius idea. (No promises, though.)

Let me introduce you to the single most undervalued fact of linear algebra: matrices are graphs, and graphs are matrices. Encoding matrices as graphs is a cheat code, making complex behavior simple to study.

Check out Figure 8.1 below.

PIC

Figure 8.1: A matrix and its directed graph

Can you figure out how it was constructed? Can you guess why it is useful? We’ll answer these questions in the next couple of pages. Specifically, we’ll see:

  • What the relationship between graphs and matrices is
  • How matrix multiplication can be translated to walks on the graph
  • What the connectivity structure of a graph reveals about its corresponding matrix

Let’s get to it!

day-mode Bookmark

8.1 The directed graph of a nonnegative matrix

Section titled “8.1 The directed graph of a nonnegative matrix”

If you look carefully at Figure 8.1, you can probably figure out how to construct a weighted graph from a matrix. Just compare each row and the outgoing edge weights for nodes.

Each row is a node, and each element represents a directed and weighted edge. Edges of zero elements are omitted. The element in the i-th row and j-th column corresponds to an edge going from i to j. The resulting graph is called the directed graph (or digraph) of the matrix.

To unwrap the definition a bit, let’s check out the previous graph of the matrix

 ⌊ ⌋ 0.5 1 0 | | 3×3 A = |⌈0.2 0 2.2|⌉ ∈ ℝ . 1.8 2 0

Here’s the first row, corresponding to the edges coming out from the first node.

PIC

Figure 8.2: The first row corresponds to the edges coming out from the first node

Similarly, the first column corresponds to the edges coming into the first node.

PIC

Figure 8.3: The first column corresponds to the edges coming into the first node

Now, we can put all of this together. Figure 8.4 shows the full picture, with the nodes explicitly labeled.

PIC

Figure 8.4: Constructing the graph of a matrix

It’s time to check the formal definition, which we’ll split into two parts. First, let’s talk about weighted and directed graphs.

Definition 29.

Let V = {v1,v2,…,vn} be an arbitrary finite set. We say that G = (V,E,w) is a weighted and directed graph, if

(a) V represents the set of vertices (also called nodes). (b) E ⊆V ×V represents the set of directed edges. (c) The function w : E →ℝ represents the edge weights.

For example, check out Figure 8.4, which we can formalize as (V,E,w) with V = {v1,v2,v3},

E = {(v1,v1),(v1,v2), (v2,v1),(v2,v3), (v3,v1),(v3,v2)},

and

w(v1,v1) = 0.5, w (v1,v2) = 1, w(v2,v1) = 0.2, w (v2,v2) = 2.2, w(v3,v1) = 1.8, w (v3,v2) = 2.

Now, we are ready to talk about matrices.

Definition 30. (Irreducible and reducible matrices)

Let A ∈ℝn×n be a nonnegative matrix, i.e., a matrix with only nonnegative elements. The directed weighted graph G = (V,E,w) is said to be the directed graph (or digraph for short) of A if:

(a) V = {1,2,…,n}, (b) (i,j) ∈E if, and only if, ai,j>0, (c) w(i,j) = ai,j.

(Sometimes, for illustrative purposes, we’ll just omit the weights and assume all of them to be equal to 1.) Again, why is this useful? Because this way, we can translate algebraic questions into graph-theoretic ones. Thus, we gain access to the vast toolkit of graph theory.

day-mode Bookmark

Let’s talk about the concrete advantages that the graph representation offers. For one, the powers of the matrix correspond to walks in the graph. Say, for any let

. Its square is denoted by

, where the elements

are defined by

 n a(2) = ∑ a a . i,j i,k k,j k=1

(Note that the (2) in the superscript of ai,j(2) is not an exponent; this is just an index indicating that ai,(2) is the element of A2.)

Figure 8.5 shows the elements of the square matrix and its graph: all possible two-step walks are accounted for in the sum defining the elements of A2.

PIC

Figure 8.5: Powers of the matrix describe walks on its directed graph

There is much more to this connection; for instance, it gives us a deep insight into the structure of nonnegative matrices. To see how, let’s talk about the concept of strongly connected components.

Intuitively, we can think of connectivity as the ability to reach every node from the others. To formalize this, we’ll need a couple of definitions. First, the “reach every node from the others” part.

Definition 31. (Walks on a graph)

Let G = (V,E,w) be a weighted and directed graph. The sequence vk1vk2…vkl is a (directed) walk on G if (vki,vki+1) ∈E for all i.

(For consistency, we define walks for weighted and directed graphs, but the definition holds for simple graphs – i.e., graphs without edges and directional edges – as well. The same goes for most of the upcoming concepts.)

In general, we say that the walk vk1vk2…vkl starts at vk1 and ends at vkl.

The term walk is surprisingly descriptive, as it truly describes a walk on the directed edges, going from node to node. However, a graph-theoretic walk is a properly defined mathematical object, not just a vague intuition. Pick up a pen and a paper once again and sketch up a graph, then a couple of its walks, to understand the concept better.

What do walks have to do with connectivity? Simple: If you can reach every node from every other node, the graph is said to be connected. Since we are talking about directed graphs, let’s add a bit of nuance to the discussion and conjure up a formal definition.

Definition 32.

(Strong connectivity)

Let G = (V,E,w) be a weighted and directed graph. We say that G is strongly connected if for every u,v ∈V , there exists a walk that starts at u and ends at v.

In other words, a directed graph is strongly connected if every node can be reached from every other node. If this is not true, the graph is not strongly connected. Figure 8.6 shows you an example of both.

Figure 8.6 also illustrates that strong connectivity does not match the connectivity concept for simple graphs.

It’s not enough to reach u from v; you have to be able to go back as well.

PIC

Figure 8.6: Connected vs. strongly connected

Now, let’s translate what we’ve learned to the language of matrices. Matrices that correspond to strongly connected graphs are called irreducible. All other nonnegative matrices are called reducible. Soon, we’ll see why, but first, here’s the formal definition.

Definition 33.

(Irreducible and reducible matrices)

Let A ∈ℝn×n be a nonnegative matrix.

(a) A is called irreducible if its digraph is strongly connected. (b) A is called reducible if it is not irreducible.

Let’s see an example! Figure 8.7 shows an irreducible matrix.

PIC

Figure 8.7: Strongly connected digraphs and their matrices

Back to the general case! Even though not all digraphs are strongly connected, we can partition the nodes into strongly connected components (as Figure 8.8 illustrates).

PIC

Figure 8.8: Strongly connected components

Let’s label the nodes of this graph and construct the corresponding matrix! If you assume that the weights are simply equal to 1 and translate all the edges into rows and columns as we have learned, you’ll get

 ⌊ ⌋ |0 1 1 0 0 0| ||0 0 1 0 0 0|| | | ||1 0 0 1 0 0|| A = ||0 0 0 0 1 1||. || || |⌈0 0 0 0 0 1|⌉ 0 0 0 1 0 0

That’s just a big block of ones and zeroes, but you shouldn’t be disappointed: there is a pattern! By dividing A into blocks, the matrix of our example graph can be reduced to a simpler form:

 ⌊ | ⌋ 0 1 1 |0 0 0 || | || || 0 0 1 |0 0 0 || ⌊ | ⌋ || 1 0 0 |1 0 0 || A1,1|A1,2 A = ||---------|--------|| = ⌈ -----|-----⌉ , | 0 0 0 |0 1 1 | A2,1|A2,2 || 0 0 0 |0 0 1 || ⌈ | ⌉ 0 0 0 |1 0 0

where

 ⌊ ⌋ ⌊ ⌋ 0 1 1 0 0 0 || || || || A1,1 = ⌈ 0 0 1⌉, A1,2 = ⌈0 0 0⌉ , 1 0 0 1 0 0 ⌊ ⌋ ⌊ ⌋ 0 0 0 0 1 1 A = || ||, A = || || . 2,1 ⌈ 0 0 0⌉ 2,2 ⌈0 0 1⌉ 0 0 0 1 0 0

The diagonal blocks A1,1 and A2,2 represent graphs that are strongly connected (that is, the blocks are irreducible). Furthermore, the block below the diagonal is 0. Is this true for all nonnegative matrices?

You bet. Let’s see!

day-mode Bookmark

In general, the block-matrix structure that we have just seen is called the Frobenius normal form. Here’s the precise definition.

Definition 34.

(Frobenius normal form)

Let A ∈ℝn×n be a nonnegative matrix. A is said to be in Frobenius normal form if it can be written in the block matrix form

L(U,V ) = {f : U → V | f is linear}

where A1,…,Ak are irreducible matrices.

Let’s reverse the question: can we transform an arbitrary nonnegative matrix into the Frobenius normal form? Yes, and with the help of directed graphs, this is much easier to show than purely using algebra. Here is the famous theorem in full form.

Theorem 54. (The existence of the Frobenius normal form)

Let A ∈ℝn×n be a nonnegative matrix. There exists a permutation matrix P ∈ℝn×n such that PT AP is in Frobenius normal form.

Rigorously spelling out the proof of Theorem 54 is quite complicated. However, the ideas behind the proof are simple to show. Thus, we’ll take the less rigorous, more fun route.

So, why is the Frobenius normal form a big deal and what on Earth is a permutation matrix? Let’s dive into it.

Mathematics is often done from concrete to abstract. That’s why we are often start with special cases: what happens if we multiply a 2 x 2 matrix by

 ⌊ ⌋ P1,2 = ⌈0 1⌉ , 1 0

a simple zero-one matrix? With a quick calculation, we can verify that

⌊ ⌋ ⌊ ⌋ ⌊ ⌋ ⌈0 1⌉ ⌈a b⌉ = ⌈c d⌉ , 1 0 c d a b ⌊ ⌋ ⌊ ⌋ ⌊ ⌋ a b 0 1 b a ⌈ ⌉ ⌈ ⌉ = ⌈ ⌉ , c d 1 0 d c

that is,

  1. it switches the rows when multiplied from the left,
  2. and it switches the columns when multiplied from the right.

Multiplying by P from both the left and right compounds the effects: it switches rows and columns, as

⌊ ⌋ ⌊ ⌋ ⌊ ⌋ ⌊ ⌋ 0 1 a b 0 1 d c ⌈ ⌉ ⌈ ⌉ ⌈ ⌉ = ⌈ ⌉ 1 0 c d 1 0 b a

shows. (By the way, this is a similarity transformation, as our special zero-one matrix is its own inverse. This is not an accident; more about it later.)

Why are we looking at this? Because behind the scenes, this transformation doesn’t change the underlying graph structure, just relabels its nodes!

You can easily verify this by hand, but Figure 8.9 illustrates this as well.

PIC

Figure 8.9: Relabeling the nodes

A similar phenomenon is true in the general n×n case. Here, we define the so-called transposition matrices by switching the i-th and j-th rows of the identity matrix, for example:

 ⌊ ⌋ ⌊ ⌋ |0 1 0 0 0| |1 0 0 0 0| ||1 0 0 0 0|| ||0 1 0 0 0|| || || 5×5 || || 5×5 P1,2 = |0 0 1 0 0| ∈ ℝ , P3,5 = |0 0 0 0 1| ∈ ℝ . ||0 0 0 1 0|| ||0 0 0 1 0|| ⌈ ⌉ ⌈ ⌉ 0 0 0 0 1 0 0 1 0 0

The two most important properties of the permutation matrices are Pi,jT = Pi,j and Pi,jT Pi,j = I. That is, their inverse is their transpose.

Multiplication with a transposition matrix has the same effect: it switches rows from the left and columns from the right. To be precise,

  1. Pi,jA switches the i-th and j-th rows of A,
  2. and APi,j switches the i-th and j-th columns of A.

Most importantly, the similarity transformation

Pi,jAPi,j

relabels the i-th and j-th nodes of A’s digraph, leaving the graph structure invariant.

Now, about the aforementioned permutation matrices. A permutation matrix is simply a product of transposition matrices:

P = Pi1,i2Pi3,i4 ...Pi2k−1,i2k.

Permutation matrices inherit some properties from their building blocks. Most importantly,

  1. their inverse is their transpose,
  2. and a similarity transformation with them is just a relabeling of nodes that leave the graph structure invariant.

To see this latter one, consider that

P TAP = (Pi1,i2Pi3,i4 ...Pi2k−1,i2k)TA (Pi1,i2Pi3,i4 ...Pi2k− 1,i2k) = (Pi2k− 1,i2k ...(Pi1,i2APi1,i2)...Pi2k−1,i2k), =

succesively relabeling the nodes. (Recall that transposing a matrix product switches up the order, and transposition matrices are their own transposes.) Conversely, every node relabeling is equivalent to a similarity transformation with a well-constructed permutation matrix.

Why are we talking about this? Because the proper labeling of nodes is key to the Frobenius normal form.

8.3.2 Directed graphs and their strongly connected components

Section titled “8.3.2 Directed graphs and their strongly connected components”

Now, let’s talk about graphs. We’ll see how every digraph decomposes into strongly connected components. Let’s see a concrete example:

PIC

Figure 8.10: A directed graph (that is complex enough for us to study)

This’ll be our textbook example. How many nodes can be reached from a given node? Not necessarily all. Say, for the point highlighted in Figure 8.11, only a portion of the graph is accessible.

PIC

Figure 8.11: Downstream nodes for a single starting point

However, the set of mutually reachable nodes is much smaller: Figure 8.12 shows that in our example, it consists of only two points.

PIC

Figure 8.12: Mutually reachable nodes

Algebraically speaking, “

and

are mutually reachable from each other“ is a special relation that partitions the set of nodes into disjoint subsets such that

  1. two nodes from the same subset are mutually reachable from each other,
  2. and two nodes from different subsets are not mutually reachable.

The subsets of this partition are called the strongly connected components, and we can always decompose a directed graph in this way.

PIC

Figure 8.13: Strongly connected components of our example graph

Now, let’s connect everything together (not in a graph way but, you know, in a wholesome mathematical one)!

8.3.3 Putting graphs and permutation matrices together

Section titled “8.3.3 Putting graphs and permutation matrices together”

We are two steps away from proving that every nonnegative square matrix can be transformed into the Frobenius normal form with a permutation matrix. Here is the plan.

  1. Construct the graph for our nonnegative matrix.
  2. Find the strongly connected components.
  3. Relabel the nodes in a clever way.

And that’s it! Why? Because, as we have seen, relabeling is the same as a similarity transform with a permutation matrix. There’s just one tiny snag: what is the clever way? I’ll show you.

First, we “skeletonize” the graph: merge the components together, as well as any edges between them.

Consider each component as a black box: we don’t care what’s inside, only about their external connections.

PIC

Figure 8.14: Strongly connected components

In this skeleton, we can find components that cannot be entered from other components. These will be our starting points, the zeroth-class components. In our example, we only have one.

PIC

Figure 8.15: Finding the ”zeroth” component

Now, things get a bit tricky. We number each component by the longest path from the farthest zero-class component from which it can be reached.

This is hard to even read, let alone understand. Figure 8.16 illustrates the process.

PIC

Figure 8.16: Numbering the components

The gist is that if you can reach an m-th class from an n-th class, then n <m. In the end, we have something like Figure 8.17.

PIC

Figure 8.17: Numbered components

This defines an ordering on the components (a partial ordering, if you would like to be precise).

Now, we label the nodes inside such that

  1. higher-order classes come first,
  2. and consecutive indices are labeling nodes from the same component if possible.

This is how it goes.

PIC

Figure 8.18: Labeling the nodes

Here is the matrix in this particular example, with zeros and ones for simplicity:

⌊ | | | | ⌋ 0 1 1 |1 0 0 0 |0 0 | 0 |0 0 0 || | | | | || || 0 0 1 |0 0 1 0 |0 0 | 1 |0 0 0 || || 1 0 0 |0 0 0 0 |1 0 | 0 |0 0 0 || | ---------|-----------|----|---|-------| || 0 0 0 |0 1 0 1 |0 0 | 0 |0 0 0 || || 0 0 0 |0 0 1 0 |0 0 | 0 |0 0 0 || || | | | | || || 0 0 0 |1 0 0 0 |0 0 | 0 |0 0 0 || || 0 0 0 |0 1 1 0 |0 0 | 0 |0 0 0 || | ---------|-----------|----|---|-------| || 0 0 0 |0 0 0 0 |0 1 | 1 |0 0 0 || || 0 0 0 |0 0 0 0 |1 0 | 0 |0 0 0 || || ---------|-----------|----|---|-------|| || -0--0--0-|0--0--0--0-|0-0-|-0-|1-0--0-|| || 0 0 0 |0 0 0 0 |0 0 | 0 |0 1 0 || | | | | | | || 0 0 0 |0 0 0 0 |0 0 | 0 |0 0 1 || || 0 0 0 |0 0 0 0 |0 0 | 0 |1 0 0 || ⌈ ⌉

With that, the ideas behind the proof of Theorem 54 are clear! Now, we also finally understand why irreducible matrices are called irreducible: as they describe strongly connected graphs, they cannot be further decomposed into smaller blocks in a meaningful way.

day-mode Bookmark

With the study of the connections between linear algebra and graph theory, our journey through linear algebra is over.

In this and the previous seven chapters, we have learned that vectors and matrices are not merely data structures that store observations and measurements. Vectors and matrices possess a rich and beautiful geometric structure, describing data and their transformations at the same time!

First, we learned that vectors live in so-called vector spaces, the high-dimensional generalizations of the three-dimensional space we are living in (which might be 26-dimensional, according to some string theorists, but let’s stick to the Earth for now). We can measure lengths and distances via norms, most often defined by

 ∑n ∥x ∥ = x2, x ∈ ℝn, i=1 i

or measure angles (among others) via inner products, most often defined by

 ∑n ⟨x, y⟩ = xiyi i=1 = cos(α)∥x ∥∥y∥.

From a mathematical perspective, matrices originate from the linear transformation of vector spaces, i.e., functions of the form f : U →V , satisfying the linearity relation f(ax + by) = af(x) + bf(y). Matrices arise from the algebraic representation of linear transformations by expressing them in the form

 ⌊ ⌋ ⌊ ∑n ⌋ ⌊a a ... a ⌋| x1| | i=1 a1,ixi| | 1,1 1,2 1,m ||| x2|| || ∑n a2,ixi|| ||a2,1 a2,2 ... a2,m ||| .| | i=1. | f(x) = Af x = || .. .. .. .. |||| ..|| = || .. || , ⌈ . . . . ⌉|| x || || ∑n a x || an,1 an,2 ... an,m, ⌈ n⌉ ⌈ i=1 n,i i⌉

allowing us to reason about data transformations from a geometric perspective. This is an extremely powerful tool in machine learning. Think about it: Ax can be a regression model, a layer in a neural network, or various other machine learning building blocks. Ultimately, this is why we want to study vector spaces: the data lives there, and data transformations are described by matrices.

However, building a model doesn’t stop at linear algebra. To capture more complex patterns, we need nonlinearities. For instance, consider the famous Sigmoid function, defined by

σ(x) = ---1---. 1+ e− x

The transformation defined by σ(Ax) (where σ is applied elementwise) is a simple logistic regression model, allowing us to perform binary classification on our multidimensional feature space. Iterating on this idea, the expression

N (x) = σ(B σ(Ax))

defines a two-layer neural network.

So, the next part of our journey is into the domain of calculus, where we’ll learn what functions really are, how we build predictive models from them, and how we fit these models by tuning the parameters with gradient descent.

Let’s go!

day-mode Bookmark

Problem 1. Let G = (V,E) be a directed graph and let u,v ∈V be two of its nodes. Show that if there exists a walk from u to v, then there exists a walk without repeated edges and repeated vertices.

Problem 2. Let G = (V,E) be a strongly connected directed graph. Show that jEj ≥jV j, where jSj denotes the number of elements in the set S. (In other words, show that in order to be strongly connected, G must have at least as many edges as nodes.)

Problem 3. Let A ∈ℝn×n be an irreducible matrix. Is A2 also reducible? (If yes, prove it. If no, show a counterexample.)

Problem 4. Let A ∈ℝ4×4 be the matrix defined by

 ⌊ ⌋ | 0 0 1 0| | 0 0 0 1| A = || || . |⌈ 1 0 0 0|⌉ 0 1 0 0

Find the permutation matrix P that transforms A to a Frobenius normal form!