## Friday, April 22, 2016

### Graphs as Matrices - part 1

Graphs are typically represented as adjacency lists. But if they're represented as matrices, you get all the juicy goodness of linear algebra.

What does this mean? Well, for example, let's represent this directed graph as a matrix.

 Digraph from Segewick's "Algorithms".
Note that it has no cycles, so we can represent it like this:

 The same digraph.
We can represent this as an n x n matrix (A) where
Ai,j is 1 when i is linked to j and 0 otherwise. Since there are no self-loops, Ai,i= 0.

In Python, it would look like this:

import numpy as np
from numpy.linalg import eig
from scipy.linalg import lu
import networkx as nx
import sympy

A = np.matrix(  # 0  1  2  3  4  5  6  7  8  9 10 11 12
'0  1  0  0  0  1  1  0  0  0  0  0  0;'  # 0
'0  0  0  0  0  0  0  0  0  0  0  0  0;'  # 1
'1  0  0  1  0  0  0  0  0  0  0  0  0;'  # 2
'0  0  0  0  0  1  0  0  0  0  0  0  0;'  # 3
'0  0  0  0  0  0  0  0  0  0  0  0  0;'  # 4
'0  0  0  0  1  0  0  0  0  0  0  0  0;'  # 5
'0  0  0  0  1  0  0  0  0  1  0  0  0;'  # 6
'0  0  0  0  0  0  1  0  0  0  0  0  0;'  # 7
'0  0  0  0  0  0  0  1  0  0  0  0  0;'  # 8
'0  0  0  0  0  0  0  0  0  0  1  1  1;'  # 9
'0  0  0  0  0  0  0  0  0  0  0  0  0;'  # 10
'0  0  0  0  0  0  0  0  0  0  0  0  1;'  # 11
'0  0  0  0  0  0  0  0  0  0  0  0  0'   # 12
)

It's worth noting that the eigenvalues of this matrix are all 0:

print "eigenvalues = ", eig(A)[0] #  [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]

This is always true of adjacency matrices that are acyclic. I'll explain why.

Firstly, if we re-label the nodes, we can change the order of the rows and columns such that it becomes an upper triangular matrix. Given an order that I calculated elsewhere:

newOrder = [2, 0, 1, 3, 5, 8, 7, 6, 4, 9, 10, 11, 12]
re_arranged_cols = A[newOrder, :]
re_arranged_rows_and_cols = re_arranged_cols[:, newOrder]

we get a matrix that looks like this:

[[0 1 0 1 0 0 0 0 0 0 0 0 0]
[0 0 1 0 1 0 0 1 0 0 0 0 0]
[0 0 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 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 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 0]
[0 0 0 0 0 0 0 0 0 0 1 1 1]
[0 0 0 0 0 0 0 0 0 0 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]]

which also has all zero eigenvalues:

print "eigenvalues for this new matrix: ", eig(re_arranged_rows_and_cols)[0]<-- all="" as="" expected="" font="" zeroes="">

Notice that with this new choice of basis, the matrix has all the connections (that is, the 1 values) above the diagonal and the diagonal values themselves are all zeros (since even with this re-ordering there are still no self-loops).

Triangular Matrices

Now, an interesting thing about upper triangular matrices is that their diagonals are their eigenvalues. The reason for this follows this recipe:

1. The elements in an upper triangular matrix, aij are necessarily 0 if i < j (where i and j are the row and column indexes). That is:

aij = 0 ∀ {i, j | i < j }

2. A formula for calculating the determinants is this one from Leibniz:

 n det(A) = Σ sgn(σ) Π aσ(i),i σ ∈ S n i = 1

where

• n is the order of the square matrix (2 if it's 2x2; 3 if it's 3x3 ...)
• σ is a permutation of n integers in the permutation group Sn.
• sgn(σ) is 1 or -1 depending on the order of σ. Ignore this. It will disappear.
• and σ(i) is the i-th integer in σ

Take a look at the rightmost element of (2). It multiplies n elements of the matrix given by axi . But remember from (1) that the product is 0 is any i is less than x. If you think about it, there is only one permutation for which this is not true, namely [1, 2, 3, ... n]. So, for a triangular matrix,
 n det(A) = Π ai,i i = 1

that is, the determinant is the product of the diagonals.

Given that you calculate the eigenvalues from the characteristic equation:

det(A - λ I) = 0

(where I is the identity matrix) then the eigenvalues are given by:

(λ - a1,1) . (λ - a2,2) . (λ - a3,3) ... (λ - an,n)  = 0

The only way for this equation to hold for all eigenvalues, λ, is that each eigenvalue must equal its corresponding diagonal element. Since the diagonals of a triangular matrix representing an acyclic graph are all 0, all its eigenvalues are 0.

Conclusion

We've shown that just by looking at the adjacency matrix (and not tediously exploring the graph) we can tell whether the graph is cyclic or acyclic. This is done by calculating the eigenvalues which is simple given any decent maths library.