# Adjacency function to create graph - not defined

**URL:** <https://igraph.discourse.group/t/adjacency-function-to-create-graph-not-defined/277>\
**Category:** Usage\
**Tags:** Python\
**Created:** [6 June 2020 20:40 UTC](https://igraph.discourse.group/t/adjacency-function-to-create-graph-not-defined/277 "2020-06-06T20:40:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sid](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/sid/32/451_2.png) [@sid](https://igraph.discourse.group/u/sid)\
**Post date:** [6 June 2020 20:40 UTC](https://igraph.discourse.group/t/adjacency-function-to-create-graph-not-defined/277/1 "2020-06-06T20:40:03Z")

</div>

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.

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

```

Output:

```auto
[[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

```

---

<div class="post-metadata">

**Author:** ![szhorvat](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/szhorvat/32/3_2.png) [@szhorvat](https://igraph.discourse.group/u/szhorvat)\
**Post date:** [8 June 2020 08:01 UTC](https://igraph.discourse.group/t/adjacency-function-to-create-graph-not-defined/277/2 "2020-06-08T08:01:06Z")

</div>

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:

```auto
from igraph import *

Graph.Adjacency(mm.tolist())

```

---

<div class="post-metadata">

**Author:** ![szhorvat](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/szhorvat/32/3_2.png) [@szhorvat](https://igraph.discourse.group/u/szhorvat)\
**Post date:** [8 June 2020 08:03 UTC](https://igraph.discourse.group/t/adjacency-function-to-create-graph-not-defined/277/3 "2020-06-08T08:03:31Z")

</div>

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)?

---

<div class="post-metadata">

**Author:** ![tamas](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/tamas/32/1261_2.png) [@tamas](https://igraph.discourse.group/u/tamas)\
**Post date:** [8 June 2020 12:48 UTC](https://igraph.discourse.group/t/adjacency-function-to-create-graph-not-defined/277/4 "2020-06-08T12:48:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sid](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/sid/32/451_2.png) [@sid](https://igraph.discourse.group/u/sid)\
**Post date:** [8 June 2020 15:09 UTC](https://igraph.discourse.group/t/adjacency-function-to-create-graph-not-defined/277/5 "2020-06-08T15:09:58Z")

</div>

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
