Instantiating graph with adjacency matrix

Thanks for this awesome package! I was just hoping to clarify fairly minor observation regarding the documentation of gt.Graph: Under option 5 it states that gt.Graph can be instantiated with

A sparse adjacency matrix of type scipy.sparse.sparray or scipy.sparse.spmatrix

For a given graph g The adjacency matrix of a graph can be obtained via graph_tool.spectral.adjacency where the notion a adjacency matrix is also nicely and clearly defined.

To me surprisingly it seems that although both sections in the documentation use the term “adjacency matrix” the former is actually the transpose of the latter:

import graph_tool.all as gt
import scipy.sparse
import numpy as np

a = np.array([[0, 2, 1], [3, 1, 2], [0, 1, 0]])
g = gt.Graph(scipy.sparse.coo_array(a), directed=True)
adj = gt.adjacency(g, weight=g.ep.weight).toarray()

np.allclose(a, adj.T)  # this is true

I have naively expected that gt.Graph uses the same definition of adjacency matrix as graph_tool.spectral.adjacency. Would it be possible to include a definition of “adjacency matrix” under option 5 of the documentation gt.Graph to make clear that it is the transpose of the definition given in graph_tool.spectral.adjacency?

This is indeed an inconsistency. Please open an issue at https://git.skewed.de/count0/graph-tool/-/issues, including the minimum working example you show, and it will be fixed.

1 Like

Thank you @tiago! I’ll open an issue as soon as possible. I currently am still unable to log in at git.skewed.de saying “Your account is pending approval…”. (I’ve used my GitHub account to log in.)

Your account has now been approved. (The spam filter does this sometimes).

In the meantime, I have fixed the issue here: Graph.__init__(): transpose sparse matrix initialization (19856107) · Commits · Tiago Peixoto / graph-tool · GitLab

1 Like