# Linear algebra

Graphs.jl provides numerous matrix operations on both directed and undirected graphs, as part of the LinAlg submodule.

## Full docs

Graphs.LinAlg.AdjacencyType
Adjacency{T}

The core Adjacency matrix structure. Keeps the vertex degrees around. Subtypes are used to represent the different normalizations of the adjacency matrix. Laplacian and its subtypes are used for the different Laplacian matrices.

Adjacency(lapl::Laplacian) provides a generic function for getting the adjacency matrix of a Laplacian matrix. If your subtype of Laplacian does not provide a field A for the Adjacency instance, then attach another method to this function to provide an Adjacency{T} representation of the Laplacian. The Adjacency matrix here is the final subtype that corresponds to this type of Laplacian.

source
Graphs.LinAlg.NoopType
Noop

A type that represents no action.

Implementation Notes

• The purpose of Noop is to help write more general code for the

different scaled GraphMatrix types.

source
Graphs.LinAlg.NormalizedAdjacencyType
NormalizedAdjacency{T}

The normalized adjacency matrix is $\hat{A} = D^{-1/2} A D^{-1/2}$. If A is symmetric, then the normalized adjacency is also symmetric with real eigenvalues bounded by [-1, 1].

source
Graphs.LinAlg.NormalizedLaplacianType
NormalizedLaplacian{T}

The normalized Laplacian is $\hat{L} = I - D^{-1/2} A D^{-1/2}$. If A is symmetric, then the normalized Laplacian is also symmetric with positive eigenvalues bounded by 2.

source
Graphs.LinAlg.symmetrizeFunction
symmetrize(A::SparseMatrix, which=:or)

Return a symmetric version of graph (represented by sparse matrix A) as a sparse matrix. which may be one of :triu, :tril, :sum, or :or. Use :sum for weighted graphs.

source
Graphs.LinAlg.symmetrizeFunction
symmetrize(adjmat, which=:or)

Return a symmetric version of graph (represented by CombinatorialAdjacency adjmat) as a CombinatorialAdjacency. which may be one of :triu, :tril, :sum, or :or. Use :sum for weighted graphs.

Implementation Notes

Only works on Adjacency because the normalizations don't commute with symmetrization.

source
Graphs.LinAlg.NonbacktrackingType
Nonbacktracking{G}

A compact representation of the nonbacktracking operator.

The Nonbacktracking operator can be used for community detection. This representation is compact in that it uses only ne(g) additional storage and provides an implicit representation of the matrix B_g defined below.

Given two arcs $A_{i j} and A_{k l} in g, the non-backtraking matrix$B is defined as

$B_{A_{i j}, A_{k l}} = δ_{j k} * (1 - δ_{i l})$

This type is in the style of GraphMatrices.jl and supports the necessary operations for computed eigenvectors and conducting linear solves.

Additionally the contract!(vertexspace, nbt, edgespace) method takes vectors represented in the domain of $B$ and represents them in the domain of the adjacency matrix of g.

source
Graphs.LinAlg.contract!Method
contract!(vertexspace, nbt, edgespace)

The mutating version of contract(nbt, edgespace). Modifies vertexspace.

source
Graphs.LinAlg.non_backtracking_matrixMethod
non_backtracking_matrix(g)

Return a non-backtracking matrix B and an edgemap storing the oriented edges' positions in B.

Given two arcs $A_{i j} and A_{k l} in g, the non-backtracking matrix$B is defined as

$B_{A_{i j}, A_{k l}} = δ_{j k} * (1 - δ_{i l})$

source
Graphs.LinAlg.adjacency_matrixFunction
adjacency_matrix(g[, T=Int; dir=:out])

Return a sparse adjacency matrix for a graph, indexed by [u, v] vertices. Non-zero values indicate an edge from u to v. Users may override the default data type (Int) and specify an optional direction.

Optional Arguments

dir=:out: :in, :out, or :both are currently supported.

Implementation Notes

This function is optimized for speed and directly manipulates CSC sparse matrix fields.

source
Graphs.LinAlg.adjacency_spectrumFunction
adjacency_spectrum(g[, T=Int; dir=:unspec])

Return the eigenvalues of the adjacency matrix for a graph g, indexed by vertex. Default values for T are the same as those in adjacency_matrix.

Optional Arguments

dir=:unspec: Options for dir are the same as those in laplacian_matrix.

Performance

Converts the matrix to dense with $nv^2$ memory usage.

Implementation Notes

Use eigvals(Matrix(adjacency_matrix(g, args...))) to compute some of the eigenvalues/eigenvectors.

source
Graphs.LinAlg.incidence_matrixFunction
incidence_matrix(g[, T=Int; oriented=false])

Return a sparse node-arc incidence matrix for a graph, indexed by [v, i], where i is in 1:ne(g), indexing an edge e. For directed graphs, a value of -1 indicates that src(e) == v, while a value of 1 indicates that dst(e) == v. Otherwise, the value is 0. For undirected graphs, both entries are 1 by default (this behavior can be overridden by the oriented optional argument).

If oriented (default false) is true, for an undirected graph g, the matrix will contain arbitrary non-zero values representing connectivity between v and i.

source
Graphs.LinAlg.laplacian_matrixMethod
laplacian_matrix(g[, T=Int; dir=:unspec])

Return a sparse Laplacian matrix for a graph g, indexed by [u, v] vertices. T defaults to Int for both graph types.

Optional Arguments

dir=:unspec: :unspec, :both, :in, and:outare currently supported. For undirected graphs,dirdefaults to:out; for directed graphs,dirdefaults to:both.

source
Graphs.LinAlg.laplacian_spectrumFunction
laplacian_spectrum(g[, T=Int; dir=:unspec])

Return the eigenvalues of the Laplacian matrix for a graph g, indexed by vertex. Default values for T are the same as those in laplacian_matrix.

Optional Arguments

dir=:unspec: Options for dir are the same as those in laplacian_matrix.

Performance

Converts the matrix to dense with $nv^2$ memory usage.

Implementation Notes

Use eigvals(Matrix(laplacian_matrix(g, args...))) to compute some of the eigenvalues/eigenvectors.

source
Graphs.LinAlg.spectral_distanceFunction
spectral_distance(G₁, G₂ [, k])

Compute the spectral distance between undirected n-vertex graphs G₁ and G₂ using the top k greatest eigenvalues. If k` is ommitted, uses full spectrum.

References

• JOVANOVIC, I.; STANIC, Z., 2014. Spectral Distances of Graphs Based on their Different Matrix Representations
source