igraph与netwrokx的转换

igraph官网:https://igraph.org/python/#startpy
值得注意的是,安装不是所见即所得pip install igraph,而是:

pip install python-igraph

如题,本篇文章中将介绍的内容为networkxigraph之间的转换。

1. networkxigraph

import networkx as nx
import igraph as ig

g = nx.karate_club_graph()
d = nx.to_pandas_edgelist(g).values
g = ig.Graph(d)
for v in g.vs:
    print(v)

2. igraphnetworkx

import networkx as nx
import igraph as ig

g = nx.karate_club_graph()
d = nx.to_pandas_edgelist(g).values
g = ig.Graph(d)

t = [str(e[0])+" "+str(e[1]) for e in g.get_edgelist()]
f = nx.parse_adjlist(t)
print(f.nodes)
['0', '1', '2', '3', '4', '5', '6', '7', '8', '10', '11', '12', '13', '17', '19', '21', '31', '30', '9', '27', '28', '32', '16', '33', '14', '15', '18', '20', '22', '23', '25', '29', '24', '26']

猜你喜欢

转载自blog.csdn.net/qq_26460841/article/details/114640346