How to create a graph from an adjacency matrix in C?

Hello!

Is there a way how to create a igraph_t graph directly from a binary adjacency matrix in C igraph? I know it is possible in Python, but I need to use the develop version of C igraph because of the latest change in label propagation function. I tried to covert the adjacency matrix to .lgl in like this:

import sys
import igraph as ig

G = ig.Graph.Read_Adjacency(sys.argv[1], sep=',',mode='undirected')
path = sys.argv[1][0:-3]+"lgl"
file = open(path,"w")

G.write_lgl(path, names=None, weights=None)
file.close()

Then, I used the function igraph_read_graph_lgl(&g, input, 0, IGRAPH_ADD_WEIGHTS_NO, 0); with to create the graph in C but I have a problem that the order of nodes is changed. I need the order of nodes to be the same as in the adjacency matrix (node 0 = first row of the matrix). The graph is unweighted and undirected. Do you know how to do it?

Thank you,
Kateřina

Yes, you can use igraph_adjacency().

I’ll get back to you about this later.

Thank you! But I can’t pass .csv file to igraph_adjacency(), right? I’m sorry, I forgot to mention this in the original question but I have data in .csv. I want to process more matrices, so I don’t want to hard code it to the C code.

If Python is more convenient for you then perhaps it would be best if you compiled the latest 0.9.x version of python-igraph. This will be relatively easy on Linux or macOS. Instruction are here:

You will need to clone the git repository and use the master branch. This already includes the fix to label propagation that you contributed.

The easiest way to install the version from the master branch is pip install git+https://github.com/igraph/python-igraph. This automatically fetches the repository, builds the package and installs it.

We should perhaps also include this in the documentation you reference @szhorvat.

1 Like

Well, I didn’t know that, so please do include it :slight_smile:

1 Like

Thank you very much, pip install git+https://github.com/igraph/python-igraph works for me.

1 Like

Great, good to hear!

I’ve updated the instructions to also include this (CC @szhorvat).