igraph
官网:https://igraph.org/python/#startpy
值得注意的是,安装不是所见即所得
的pip install igraph
,而是:
pip install python-igraph
如题,本篇文章中将介绍的内容为networkx
和igraph
之间的转换。
networkx
转igraph
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)
igraph
转networkx
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']