ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 13 Feb 2012 06:50:18 -0600Convert Sage's Graph to NetworkX graphhttps://ask.sagemath.org/question/8715/convert-sages-graph-to-networkx-graph/I want to use a function of networkx. More specifically I want to use the function
double_edge_swap()
provided in the networkx package included in Sage. For more information see [here](http://networkx.lanl.gov/reference/generated/networkx.generators.degree_seq.double_edge_swap.html#networkx.generators.degree_seq.double_edge_swap).
But, I have a Sage graph constructed with:
Graph()
So I want to convert the Sage's Graph() object to a networkx object. How I do that?
Best regards.Mon, 13 Feb 2012 04:03:40 -0600https://ask.sagemath.org/question/8715/convert-sages-graph-to-networkx-graph/Answer by DSM for <p>I want to use a function of networkx. More specifically I want to use the function</p>
<pre><code>double_edge_swap()
</code></pre>
<p>provided in the networkx package included in Sage. For more information see <a href="http://networkx.lanl.gov/reference/generated/networkx.generators.degree_seq.double_edge_swap.html#networkx.generators.degree_seq.double_edge_swap">here</a>.</p>
<p>But, I have a Sage graph constructed with:</p>
<pre><code>Graph()
</code></pre>
<p>So I want to convert the Sage's Graph() object to a networkx object. How I do that?</p>
<p>Best regards.</p>
https://ask.sagemath.org/question/8715/convert-sages-graph-to-networkx-graph/?answer=13271#post-id-13271You can simply call the method .networkx_graph():
sage: import networkx as nx
sage: G = graphs.PetersenGraph()
sage: ng = G.networkx_graph()
sage: ng
<networkx.classes.graph.Graph object at 0xd3c2f8c>
sage: nx.double_edge_swap(ng)
1
Incidentally, I didn't know this five minutes ago, so I should tell you how I found out. Lots of functionality for Sage objects lives inside them, in methods. Typically conversion functions are either called ".targettype" or "._targettype_", so I tried
sage: G.netw[HERE I HIT TAB]
G.networkx_graph
which looked promising. And then typing
sage: G.networkx_graph?
gives the documentation:
Creates a new NetworkX graph from the Sage graph.
INPUT:
* "copy" - if False, and the underlying implementation is a
NetworkX graph, then the actual object itself is returned.
EXAMPLES:
sage: G = graphs.TetrahedralGraph()
sage: N = G.networkx_graph()
sage: type(N)
<class 'networkx.classes.graph.Graph'>
sage: G = graphs.TetrahedralGraph()
sage: G = Graph(G, implementation='networkx')
sage: N = G.networkx_graph()
sage: G._backend._nxg is N
False
sage: G = Graph(graphs.TetrahedralGraph(), implementation='networkx')
sage: N = G.networkx_graph(copy=False)
sage: G._backend._nxg is N
True
PS: Okay, to be perfectly honest, I tried it first and only looked at the documentation when I came to write this. But I would've looked at the documentation if it hadn't worked, I promise! :^)
Mon, 13 Feb 2012 04:18:02 -0600https://ask.sagemath.org/question/8715/convert-sages-graph-to-networkx-graph/?answer=13271#post-id-13271Comment by juanpool for <p>You can simply call the method .networkx_graph():</p>
<pre><code>sage: import networkx as nx
sage: G = graphs.PetersenGraph()
sage: ng = G.networkx_graph()
sage: ng
<networkx.classes.graph.Graph object at 0xd3c2f8c>
sage: nx.double_edge_swap(ng)
1
</code></pre>
<p>Incidentally, I didn't know this five minutes ago, so I should tell you how I found out. Lots of functionality for Sage objects lives inside them, in methods. Typically conversion functions are either called ".targettype" or "._targettype_", so I tried</p>
<pre><code>sage: G.netw[HERE I HIT TAB]
G.networkx_graph
</code></pre>
<p>which looked promising. And then typing</p>
<pre><code>sage: G.networkx_graph?
</code></pre>
<p>gives the documentation:</p>
<pre><code> Creates a new NetworkX graph from the Sage graph.
INPUT:
* "copy" - if False, and the underlying implementation is a
NetworkX graph, then the actual object itself is returned.
EXAMPLES:
sage: G = graphs.TetrahedralGraph()
sage: N = G.networkx_graph()
sage: type(N)
<class 'networkx.classes.graph.Graph'>
sage: G = graphs.TetrahedralGraph()
sage: G = Graph(G, implementation='networkx')
sage: N = G.networkx_graph()
sage: G._backend._nxg is N
False
sage: G = Graph(graphs.TetrahedralGraph(), implementation='networkx')
sage: N = G.networkx_graph(copy=False)
sage: G._backend._nxg is N
True
</code></pre>
<p>PS: Okay, to be perfectly honest, I tried it first and only looked at the documentation when I came to write this. But I would've looked at the documentation if it hadn't worked, I promise! :^)</p>
https://ask.sagemath.org/question/8715/convert-sages-graph-to-networkx-graph/?comment=20300#post-id-20300Thanks very much DSM.
That solves my problem.
Fool of me.
I tried using TAB in sage's notebook, and googled a lot. But I couldn't find it.Mon, 13 Feb 2012 05:31:33 -0600https://ask.sagemath.org/question/8715/convert-sages-graph-to-networkx-graph/?comment=20300#post-id-20300Comment by kcrisman for <p>You can simply call the method .networkx_graph():</p>
<pre><code>sage: import networkx as nx
sage: G = graphs.PetersenGraph()
sage: ng = G.networkx_graph()
sage: ng
<networkx.classes.graph.Graph object at 0xd3c2f8c>
sage: nx.double_edge_swap(ng)
1
</code></pre>
<p>Incidentally, I didn't know this five minutes ago, so I should tell you how I found out. Lots of functionality for Sage objects lives inside them, in methods. Typically conversion functions are either called ".targettype" or "._targettype_", so I tried</p>
<pre><code>sage: G.netw[HERE I HIT TAB]
G.networkx_graph
</code></pre>
<p>which looked promising. And then typing</p>
<pre><code>sage: G.networkx_graph?
</code></pre>
<p>gives the documentation:</p>
<pre><code> Creates a new NetworkX graph from the Sage graph.
INPUT:
* "copy" - if False, and the underlying implementation is a
NetworkX graph, then the actual object itself is returned.
EXAMPLES:
sage: G = graphs.TetrahedralGraph()
sage: N = G.networkx_graph()
sage: type(N)
<class 'networkx.classes.graph.Graph'>
sage: G = graphs.TetrahedralGraph()
sage: G = Graph(G, implementation='networkx')
sage: N = G.networkx_graph()
sage: G._backend._nxg is N
False
sage: G = Graph(graphs.TetrahedralGraph(), implementation='networkx')
sage: N = G.networkx_graph(copy=False)
sage: G._backend._nxg is N
True
</code></pre>
<p>PS: Okay, to be perfectly honest, I tried it first and only looked at the documentation when I came to write this. But I would've looked at the documentation if it hadn't worked, I promise! :^)</p>
https://ask.sagemath.org/question/8715/convert-sages-graph-to-networkx-graph/?comment=20297#post-id-20297To get this, you need to do G.[tab], not G[tab]. In case that's where the confusion came in.Mon, 13 Feb 2012 06:50:18 -0600https://ask.sagemath.org/question/8715/convert-sages-graph-to-networkx-graph/?comment=20297#post-id-20297