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.Wed, 17 Jul 2019 15:55:38 -0500Sage graph backendhttps://ask.sagemath.org/question/47191/sage-graph-backend/A Sage graph `G` has a backend graph (say `GG`) accessible with the private `_backend` attribute. The later graph refers to two "C-graphs" (say `G1` and `G2`) accessibles via the `c-graph` attribute.
Some code with a weighted graph `G` to illustrate my question :
import string
ALPHA = string.ascii_uppercase
n = 10
wedges = [(0, 1, 7.0), (0, 3, 1.0), (0, 9, 9.0), (1, 7, 6.0), (2, 8, 7.0),
(2, 9, 2.0), (3, 6, 7.0), (4, 9, 5.0), (5, 9, 6.0), (6, 9, 9.0),
(6, 7, 1.0), (7, 8, 9.0), (7, 9, 5.0), (8, 9, 3.0)]
wedges = [(ALPHA[i], ALPHA[j], 10 * w) for (i, j, w) in wedges]
G = Graph()
G.add_edges(wedges)
G.weighted(True)
for v in G:
print v, ''.join(G[v])
print
print "G type:", type(G)
print "-----------------------------"
GG = G._backend
print "GG type:", type(GG)
print list(GG.iterator_verts())
print "-----------------------------"
G1, G2 = GG.c_graph()
print "G1 type:", type(G1)
print
for i in range(n):
print i, ''.join(map(str, G1.out_neighbors(i)))
outputting:
A BDJ
C JI
B AH
E J
D AG
G DJH
F J
I JHC
H BJIG
J AHCIGEF
G type: <class 'sage.graphs.graph.Graph'>
-----------------------------
GG type: <type 'sage.graphs.base.sparse_graph.SparseGraphBackend'>
['A', 'C', 'B', 'E', 'D', 'G', 'F', 'I', 'H', 'J']
-----------------------------
G1 type: <type 'sage.graphs.base.sparse_graph.SparseGraph'>
0 123
1 04
2 07
3 0456789
4 1367
5 36
6 345
7 234
8 3
9 3
The graphs above `G`, `GG` and `G1` have `n=10` vertices. The difference is that G1's vertices are labelled from 0 to `n-1`. As you can imagine, the two graph `G` and `G1` are isomorphic.
So my question is simple: does anybody know how to access the mapping between the vertice sets?
The documentation explains that two dictionaries `vertex_ints` and `vertex_labels` are available to make translation from vertices id to integers and vice-versa, unfortunately, `GG.vertex_ints` and `GG.vertex_labels` cause an attribute error.
As you may see, and contrary to what I was expecting, the correpondance is not $i\mapsto G[i]$.Tue, 16 Jul 2019 14:53:34 -0500https://ask.sagemath.org/question/47191/sage-graph-backend/Answer by vdelecroix for <p>A Sage graph <code>G</code> has a backend graph (say <code>GG</code>) accessible with the private <code>_backend</code> attribute. The later graph refers to two "C-graphs" (say <code>G1</code> and <code>G2</code>) accessibles via the <code>c-graph</code> attribute.
Some code with a weighted graph <code>G</code> to illustrate my question :</p>
<pre><code>import string
ALPHA = string.ascii_uppercase
n = 10
wedges = [(0, 1, 7.0), (0, 3, 1.0), (0, 9, 9.0), (1, 7, 6.0), (2, 8, 7.0),
(2, 9, 2.0), (3, 6, 7.0), (4, 9, 5.0), (5, 9, 6.0), (6, 9, 9.0),
(6, 7, 1.0), (7, 8, 9.0), (7, 9, 5.0), (8, 9, 3.0)]
wedges = [(ALPHA[i], ALPHA[j], 10 * w) for (i, j, w) in wedges]
G = Graph()
G.add_edges(wedges)
G.weighted(True)
for v in G:
print v, ''.join(G[v])
print
print "G type:", type(G)
print "-----------------------------"
GG = G._backend
print "GG type:", type(GG)
print list(GG.iterator_verts())
print "-----------------------------"
G1, G2 = GG.c_graph()
print "G1 type:", type(G1)
print
for i in range(n):
print i, ''.join(map(str, G1.out_neighbors(i)))
</code></pre>
<p>outputting:</p>
<pre><code>A BDJ
C JI
B AH
E J
D AG
G DJH
F J
I JHC
H BJIG
J AHCIGEF
G type: <class 'sage.graphs.graph.Graph'>
-----------------------------
GG type: <type 'sage.graphs.base.sparse_graph.SparseGraphBackend'>
['A', 'C', 'B', 'E', 'D', 'G', 'F', 'I', 'H', 'J']
-----------------------------
G1 type: <type 'sage.graphs.base.sparse_graph.SparseGraph'>
0 123
1 04
2 07
3 0456789
4 1367
5 36
6 345
7 234
8 3
9 3
</code></pre>
<p>The graphs above <code>G</code>, <code>GG</code> and <code>G1</code> have <code>n=10</code> vertices. The difference is that G1's vertices are labelled from 0 to <code>n-1</code>. As you can imagine, the two graph <code>G</code> and <code>G1</code> are isomorphic. </p>
<p>So my question is simple: does anybody know how to access the mapping between the vertice sets?</p>
<p>The documentation explains that two dictionaries <code>vertex_ints</code> and <code>vertex_labels</code> are available to make translation from vertices id to integers and vice-versa, unfortunately, <code>GG.vertex_ints</code> and <code>GG.vertex_labels</code> cause an attribute error.</p>
<p>As you may see, and contrary to what I was expecting, the correpondance is not $i\mapsto G[i]$.</p>
https://ask.sagemath.org/question/47191/sage-graph-backend/?answer=47198#post-id-47198The attributes `vertex_ints` and `vertex_labels` are Cython attributes. They are declared in [c_graph.pxd](https://github.com/sagemath/sage/blob/master/src/sage/graphs/base/c_graph.pxd#L11-L15). In particular, they can not be accessed at Python level.
It is possible to provide access to them, but this requires modifications to the SageMath source code... which you are welcome to provide on [the trac server](https://trac.sagemath.org/).Wed, 17 Jul 2019 14:36:42 -0500https://ask.sagemath.org/question/47191/sage-graph-backend/?answer=47198#post-id-47198Comment by elastica for <p>The attributes <code>vertex_ints</code> and <code>vertex_labels</code> are Cython attributes. They are declared in <a href="https://github.com/sagemath/sage/blob/master/src/sage/graphs/base/c_graph.pxd#L11-L15">c_graph.pxd</a>. In particular, they can not be accessed at Python level.</p>
<p>It is possible to provide access to them, but this requires modifications to the SageMath source code... which you are welcome to provide on <a href="https://trac.sagemath.org/">the trac server</a>.</p>
https://ask.sagemath.org/question/47191/sage-graph-backend/?comment=47201#post-id-47201That's unfortunate. Thanks for the answer.Wed, 17 Jul 2019 15:55:38 -0500https://ask.sagemath.org/question/47191/sage-graph-backend/?comment=47201#post-id-47201