[Networkit] Function nx2nk (Damiano Rossato)
Christian Staudt
christian.staudt at kit.edu
Thu Mar 5 19:13:13 CET 2015
Hi Damiano,
thanks for pointing this out. I’ve added a patch to the Dev branch that implements support for directed graphs. If you have time, please let us know if that solves your problem.
Best,
Chris
On 04 Mar 2015, at 16:42, Damiano Rossato <damiano_rossato at alice.it> wrote:
> 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
> <ATT00001.c>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 495 bytes
Desc: Message signed with OpenPGP using GPGMail
URL: <https://lists.ira.uni-karlsruhe.de/mailman/private/networkit/attachments/20150305/c020fdf9/attachment.sig>
More information about the NetworKit
mailing list