ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 29 Oct 2014 13:10:12 +0100Networkx graph to Sage graph? + Export graph to csv?https://ask.sagemath.org/question/24668/networkx-graph-to-sage-graph-export-graph-to-csv/ Hi everybody,
I want to work with networkx, in particular with algorithms in networkx. I know how to import a graph with data from a csv-file and how to convert a Sage graph into a networkx graph.
Therefor my code:
import csv
f=open(DATA+'example','r')
data=csv.reader(f,delimiter=';')
data=[row for row in data]
f.close()
data=[(column[0:]) for column in data[1:]]
data
Liste=[]
for row in data:
Tupel=()
for item in row:
titem=(int(item),)
Tupel=Tupel+titem
Liste.append(Tupel)
print Liste
G=Graph(Liste)
import networkx as nx
H=G.networkx_graph()
h=nx.minimum_spanning_tree(H)
What do I have to do to convert my so called h-graph into a Sage graph? And how to get the results back into a csv-file?
Thanks in advance!Tue, 28 Oct 2014 14:44:36 +0100https://ask.sagemath.org/question/24668/networkx-graph-to-sage-graph-export-graph-to-csv/Answer by kcrisman for <p>Hi everybody,</p>
<p>I want to work with networkx, in particular with algorithms in networkx. I know how to import a graph with data from a csv-file and how to convert a Sage graph into a networkx graph.
Therefor my code:</p>
<pre><code>import csv
f=open(DATA+'example','r')
data=csv.reader(f,delimiter=';')
data=[row for row in data]
f.close()
data=[(column[0:]) for column in data[1:]]
data
Liste=[]
for row in data:
Tupel=()
for item in row:
titem=(int(item),)
Tupel=Tupel+titem
Liste.append(Tupel)
print Liste
G=Graph(Liste)
import networkx as nx
H=G.networkx_graph()
h=nx.minimum_spanning_tree(H)
</code></pre>
<p>What do I have to do to convert my so called h-graph into a Sage graph? And how to get the results back into a csv-file?</p>
<p>Thanks in advance!</p>
https://ask.sagemath.org/question/24668/networkx-graph-to-sage-graph-export-graph-to-csv/?answer=24669#post-id-24669Does the following not work for getting it as a Sage graph?
sage: Z = Graph(h)
sage: Z.order() == Z.size()+1
True # we hope!
Sage also has a minimum spanning tree method though I think it is an LP one, not the networkx one.
Your question about csv remains unanswered, largely because I didn't know there was a standard way to represent graphs in csv-file format.Tue, 28 Oct 2014 15:16:45 +0100https://ask.sagemath.org/question/24668/networkx-graph-to-sage-graph-export-graph-to-csv/?answer=24669#post-id-24669Answer by fidbc for <p>Hi everybody,</p>
<p>I want to work with networkx, in particular with algorithms in networkx. I know how to import a graph with data from a csv-file and how to convert a Sage graph into a networkx graph.
Therefor my code:</p>
<pre><code>import csv
f=open(DATA+'example','r')
data=csv.reader(f,delimiter=';')
data=[row for row in data]
f.close()
data=[(column[0:]) for column in data[1:]]
data
Liste=[]
for row in data:
Tupel=()
for item in row:
titem=(int(item),)
Tupel=Tupel+titem
Liste.append(Tupel)
print Liste
G=Graph(Liste)
import networkx as nx
H=G.networkx_graph()
h=nx.minimum_spanning_tree(H)
</code></pre>
<p>What do I have to do to convert my so called h-graph into a Sage graph? And how to get the results back into a csv-file?</p>
<p>Thanks in advance!</p>
https://ask.sagemath.org/question/24668/networkx-graph-to-sage-graph-export-graph-to-csv/?answer=24671#post-id-24671### 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](http://www.sagemath.org/doc/reference/graphs/sage/graphs/generic_graph.html#sage.graphs.generic_graph.GenericGraph.min_spanning_tree) 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](http://gephi.github.io/users/supported-graph-formats/csv-format/). 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: Zreader = csv.reader(f,delimiter=';')
sage: Z = Graph([(int(u),int(v)) for u,v in Zreader])
sage: f.close()
Tue, 28 Oct 2014 19:01:09 +0100https://ask.sagemath.org/question/24668/networkx-graph-to-sage-graph-export-graph-to-csv/?answer=24671#post-id-24671Comment by kcrisman for <h3>MST's</h3>
<p>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 <a href="http://www.sagemath.org/doc/reference/graphs/sage/graphs/generic_graph.html#sage.graphs.generic_graph.GenericGraph.min_spanning_tree">here</a> for documentation, you can use the algorithm from networkx too :-). See an example below.</p>
<pre><code>sage: Z=Graph(G.min_spanning_tree(algorithm='Kruskal'))
</code></pre>
<h3>Writing graph to csv</h3>
<p>It seems that the format you are reading from is the edge list format from <a href="http://gephi.github.io/users/supported-graph-formats/csv-format/">here</a>. If this is the case, then you can easily save graph <code>Z</code> with the following commands.</p>
<pre><code>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()
</code></pre>
<h3>Reading graph from csv</h3>
<p>Assuming you are reading from the format mentioned above, it might be possible to read the graph in one line as shown below.</p>
<pre><code>sage: f=open('Z.csv','r')
sage: Zreader = csv.reader(f,delimiter=';')
sage: Z = Graph([(int(u),int(v)) for u,v in Zreader])
sage: f.close()
</code></pre>
https://ask.sagemath.org/question/24668/networkx-graph-to-sage-graph-export-graph-to-csv/?comment=24675#post-id-24675Nice, I like that a lot. You could even get the minimum spanning tree in the same `Z` command, and then write it back to csv in a final command... I wonder what the minimal number of characters needed to do this would be :)Wed, 29 Oct 2014 13:10:12 +0100https://ask.sagemath.org/question/24668/networkx-graph-to-sage-graph-export-graph-to-csv/?comment=24675#post-id-24675