Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

kcrisman's answer works for converting the networkx graph to a sage graph. As he points out Sage has its own implementations of mst algorithms (see here for documentation, you can use the algorithm from networkx too :-). See an example below.

sage: Z=Graph(G.min_spanning_tree(algorithm='Kruskal'))

It seems that the format you are reading from is the edge list format from here. If this is the case, then you can easily save graph Z with the following commands.

sage: f=open('Z.csv','w')
sage: f.write('\n') # We write the blank line at the top
sage: Zwriter = csv.writer(f,delimiter=';')
sage: Zwriter.writerows(Z.edges(labels=None))
sage: f.close()

MST's

kcrisman's answer works for converting the networkx graph to a sage graph. As he points out Sage has its own implementations of mst algorithms (see here for documentation, you can use the algorithm from networkx too :-). See an example below.

sage: Z=Graph(G.min_spanning_tree(algorithm='Kruskal'))

Writing graph to csv

It seems that the format you are reading from is the edge list format from here. If this is the case, then you can easily save graph Z with the following commands.

sage: f=open('Z.csv','w')
sage: f.write('\n') # We write the blank line at the top
sage: Zwriter = csv.writer(f,delimiter=';')
sage: Zwriter.writerows(Z.edges(labels=None))
sage: f.close()

Reading graph from csv

Assuming you are reading from the format mentioned above, it might be possible to read the graph in one line as shown below.

sage: f=open('Z.csv','r')
sage: Hreader = csv.reader(f,delimiter=';')
sage: H = Graph([(int(u),int(v))  for u,v in Hreader])
sage: f.close()

MST's

kcrisman's answer works for converting the networkx graph to a sage graph. As he points out Sage has its own implementations of mst algorithms (see here for documentation, you can use the algorithm from networkx too :-). See an example below.

sage: Z=Graph(G.min_spanning_tree(algorithm='Kruskal'))

Writing graph to csv

It seems that the format you are reading from is the edge list format from here. If this is the case, then you can easily save graph Z with the following commands.

sage: f=open('Z.csv','w')
sage: f.write('\n') # We write the blank line at the top
sage: Zwriter = csv.writer(f,delimiter=';')
sage: Zwriter.writerows(Z.edges(labels=None))
sage: f.close()

Reading graph from csv

Assuming you are reading from the format mentioned above, it might be possible to read the graph in one line as shown below.

sage: f=open('Z.csv','r')
sage: Hreader Zreader = csv.reader(f,delimiter=';')
sage: H Z = Graph([(int(u),int(v))  for u,v in Hreader])
Zreader])
sage: f.close()