Converting python-igraph diagram to networkx

I recently worked with the python-igraph package and all my code is based on plots that I create with igraph. Right now, I need to compute some dimensions for my graph that seem to be implemented in networkx and not in igraph, such as (katz_centrality_numpy, edge_betweenness_centrality, ...). I am wondering if there is a way to convert one graph to another between these two packages and avoid reading from the files again, since my files are huge and have to repeat the same process.

By the way, when I pass the igraph to the networkx function, I get the following error:

TypeError: 'Graph' object is not iterable




source to share

4 answers

You can initiate a networkx diagram with edges:

Graph([(1,2), (3,4)])


See the documentation .


Here's how to use it (thanks for the code):


is a graph igraph

and we create G

which is a graph networkx


import networkx
A = graph.get_edgelist()
G = networkx.DiGraph(A) # In case your graph is directed
G = networkx.Graph(A) # In case you graph is undirected




As I am trying to store the node / edge names on both igraph or nx, this is my single layer version that also carries the node names when passed from an igraph object g

, in nx, G, the result is:

G = nx.from_edgelist([(names[x[0]], names[x[1]])
                      for names in [g.vs['name']] # simply a let
                      for x in g.get_edgelist()], nx.DiGraph())


Also, if you need a way back, see this answer .



Ok so I figured it out myself. Here's what you should do. Assuming your python.igraph object is called graph, we create a networkx graph called G like this:

import networkx as netx

A = [edge.tuple for edge in]
# In case your graph is directed
G = netx.DiGraph(A)
# In case you graph is undirected
G = netx.Graph(A) returns a list of graph edges and then adds all of them to and using matrix A, we create a graph on the netx.

Good luck with your codes :)



I have Python code that uses the igraph library -

    import igraph
    edge =  [(0, 6), (0, 8), (0, 115), (0, 124), (0, 289), (0, 359), (0, 363), (6, 60), (6, 115), (6, 128), (6, 129), (6, 130), (6, 131), (6, 359), (6, 529), (8, 9), (8, 17), (8, 115)]
    G = igraph.Graph(edges=edge, directed=False)
    G.vs['label'] = nodes["weight"] = weights
    dendrogram = G.community_edge_betweenness()
    clusters = dendrogram.as_clustering()
    membership = clusters.membership
    out = pd.Series(membership, index=nodes)


and i need to convert it to networkx library since igraph was recently discontinued.

    import networkx as nx
    G = nx.Graph(edges)
    dendrogram = nx.edge_betweenness_centrality(G)
    clusters = nx.clustering(dendrogram)
    membership = clusters.membership
    out = pd.Series(membership, index=nodes)


However, the dendrogram cannot be clustered in the networkx library. Can anyone help replicate igraph code to networkx clusters?



All Articles