Adjacency function to create graph - not defined

I would appreciate any comments.
There is a function called Adjacency(matrix, mode=ADJ_DIRECTED) which supposedly generates a graph from its adjacency matrix.
I am not sure what I am doing wrong in the following code.

mm=np.matrix('3,0;0,0')
print(mm)
g1=Adjacency(mm)

Output:

[[3 0]
 [0 0]]
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-108-9ff2d95466cc> in <module>()
      1 mm=np.matrix('3,0;0,0')
      2 print(mm)
----> 3 g1=Adjacency(mm)

NameError: name 'Adjacency' is not defined

The Adjacency function is a method of the Graph class. Also, it does not take a numpy matrix as input.

Thus, we can do this as follows:

from igraph import *

Graph.Adjacency(mm.tolist())

I have a question of my own, since I am not terribly comfortable with either igraph’s Python interface or Python itself:

@tamas Is there a way to avoid the conversion from an efficient numpy array to an inefficient list of lists? Is there some sort of “unified” matrix interface (or de-facto standard) in Python that could be used to make this work with any matrix type? How about sparse arrays (adjacency matrices are best stored as sparse arrays)?

There is no unified matrix interface as far as I know.

In the C layer, we are calling igraph_adjacency(), which needs an igraph_matrix_t. Internally, igraph_matrix_t is simply a continuous chunk of memory with the values of the matrix cells, in either row major or column major order (can’t remember which, I’d have check the source code). In theory, we could get hold of the internal memory buffer of the NumPy matrix and create an igraph_matrix_view() out of it, assuming that the NumPy matrix is also continuous - which might not always be the case as NumPy multi-dimensional arrays can be sliced in a way that the underlying memory buffer remains the same and NumPy only adjusts the “strides” one has to take along a particular axis in order to get to the next cell along that axis. So, one way or another, we need to convert the NumPy representation to something that igraph_matrix_view() can then digest.

I thank you all. It may be worth changing the document on Adjacency as well as Weighted-Adjacency since both ask for matrix as the input. As you all point out, instead one needs to give a list of lists as input.

Sid
Siddhartha Dalal, Professor
Columbia University

1 Like