[Networkit] Function nx2nk (Damiano Rossato)
Damiano Rossato
damiano_rossato at alice.it
Wed Mar 4 16:42:02 CET 2015
Hi,
I'm trying to convert a directed graph from NeworkX format to NetworKit, but when I check if the returned Graph is or not Directed with
Graph.isDirected function, it returns always False. Looking at the nxadapter.py file, it seems that the function nx2nk doesn't check if the
input graph is or not Directed, and always return an undirected NetworKit graph. Someone could give me a confirmation about it?!
In the meanwhile I wrote this method that resolve the "issue".
def nxd2nkd(nxG, weightAttr=None, directed=True):
"""
Convert a networkx.Graph to a NetworKit.Graph
:param weightAttr: the edge attribute which should be treated as the edge weight.
"""
# map networkx node ids to consecutive numerical node ids
idmap = dict((id, u) for (id, u) in zip(nxG.nodes(), range(nxG.number_of_nodes())))
z = max(idmap.values()) + 1
# print("z = {0}".format(z))
if directed is not True:
if weightAttr is not None:
nkG = graph.Graph(z, weighted=True)
for (u_, v_) in nxG.edges():
u, v = idmap[u_], idmap[v_]
w = nxG.edge[u_][v_][weightAttr]
nkG.addEdge(u, v, w)
else:
nkG = graph.Graph(z)
for (u_, v_) in nxG.edges():
u, v = idmap[u_], idmap[v_]
# print(u_, v_, u, v)
assert (u < z)
assert (v < z)
nkG.addEdge(u, v)
else:
if weightAttr is not None:
nkG = graph.Graph(z, weighted=True)
for (u_, v_) in nxG.edges():
u, v = idmap[u_], idmap[v_]
w = nxG.edge[u_][v_][weightAttr]
nkG.addEdge(u, v, w)
else:
nkG = graph.Graph(z, directed=True)
for (u_, v_) in nxG.edges():
u, v = idmap[u_], idmap[v_]
# print(u_, v_, u, v)
assert (u < z)
assert (v < z)
nkG.addEdge(u, v)
assert (nkG.numberOfNodes() == nxG.number_of_nodes())
assert (nkG.numberOfEdges() == nxG.number_of_edges())
return nkG
Thank you to everybody
Damiano
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ira.uni-karlsruhe.de/mailman/private/networkit/attachments/20150304/b407a098/attachment.html>
More information about the NetworKit
mailing list