1

I am using the NetworkX python module to work with graphs and I'm happy with it so far.

However, I tried to write a graph to a file in LEDA format (more information here). The NetworkX documentation does not seem mention any function to do it (I only found how to read and parse LEDA formatted graphs). Is there a function to write graphs in LEDA format that I haven't seen?

Knak
  • 496
  • 3
  • 14
  • Based on https://networkx.github.io/documentation/networkx-1.10/reference/readwrite.html my suspicion is that the answer is no. It looks like it shouldn't be hard to create. They may be interested in adding it if you do the legwork. The challenge in writing a general function might be dealing with the fact that it looks to me like LEDA requires node names to be ints while networkx is much more flexible. – Joel Aug 02 '16 at 10:23
  • Yes, it seems that I will have to write it by myself ! – Knak Aug 02 '16 at 13:07
  • The format is pretty similar to Pajek so you might be able to get a good start from there https://github.com/networkx/networkx/blob/master/networkx/readwrite/pajek.py – Aric Aug 02 '16 at 20:19

1 Answers1

0

I have created a code to write graph in leda format :

import networkx as nx
import os
import pandas as pd
df = pd.read_csv(file,header=0,sep="\t")
    G = nx.Graph()
    G = nx.from_pandas_edgelist(df, 'from', 'to')
    path_sub_network_leda=str(file)+ ".gw"
    with open(path_sub_network_leda,"a") as file_graph:
        file_graph.write('LEDA.GRAPH\n')
        file_graph.write('void\n')
        file_graph.write('void\n')
        file_graph.write('-2\n')
        file_graph.write(str(G.order())+str("\n"))
        nodes = list(G)
        nodenumber = dict(zip(nodes, range(1, len(nodes) + 1)))
        for n in nodes:
            node_row=str("|{") + str(n) + str("}|")+str("\n")
            file_graph.write(node_row)
        file_graph.write(str(G.number_of_edges())+str("\n"))
        for u, v, edgedata in G.edges(data=True):
            d = edgedata.copy()
            edge_row=str(nodenumber[u]) +" " + str(nodenumber[v])+ " "+ str(0)+ str(" |{}|") +str("\n")
            file_graph.write(edge_row)