summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichele Borassi <michele.borassi@imtlucca.it>2015-08-03 15:14:35 +0200
committerMichele Borassi <michele.borassi@imtlucca.it>2015-08-03 15:14:35 +0200
commitfc693a955362964e4c7bb73c75b74c65e7dbebef (patch)
tree1fc0d09df53173584c730522a0ab6deb9ee37d1a
parenttrac #18929: Review (diff)
Simplified handling of labels
-rw-r--r--src/sage/graphs/digraph.py40
-rw-r--r--src/sage/graphs/generic_graph.py125
-rw-r--r--src/sage/graphs/graph.py41
3 files changed, 87 insertions, 119 deletions
diff --git a/src/sage/graphs/digraph.py b/src/sage/graphs/digraph.py
index 3cac432..2731cc7 100644
--- a/src/sage/graphs/digraph.py
+++ b/src/sage/graphs/digraph.py
@@ -410,26 +410,22 @@ class DiGraph(GenericGraph):
sage: DiGraph(g) # optional - python_igraph
Digraph on 3 vertices
- If the igraph Graph has a vertex attribute ``'name'``, this attribute is
- used as vertex name::
+ If ``vertex_labels`` is ``True``, the names of the vertices are given by
+ the vertex attribute ``'name'``, if available::
- sage: g = igraph.Graph([(0,1),(0,2)], directed=True, vertex_attrs={'name':['a','b','c']}) # optional - python_igraph
- sage: DiGraph(g).vertices() # optional - python_igraph
+ sage: g = igraph.Graph([(0,1),(0,2)], directed=True, vertex_attrs={'name':['a','b','c']}) # optional - python_igraph
+ sage: DiGraph(g).vertices() # optional - python_igraph
['a', 'b', 'c']
+ sage: g = igraph.Graph([(0,1),(0,2)], directed=True, vertex_attrs={'label':['a','b','c']}) # optional - python_igraph
+ sage: DiGraph(g).vertices() # optional - python_igraph
+ [0, 1, 2]
- If the igraph Graph has edge attributes, they are used for edge labels::
+ If the igraph Graph has edge attributes, they are used as edge labels::
- sage: g = igraph.Graph([(0,1),(0,2)], directed=True, edge_attrs={'weight':[1,3]}) # optional - python_igraph
- sage: DiGraph(g).edges() # optional - python_igraph
- [(0, 1, {'weight': 1}), (0, 2, {'weight': 3})]
+ sage: g = igraph.Graph([(0,1),(0,2)], directed=True, edge_attrs={'name':['a','b'], 'weight':[1,3]}) # optional - python_igraph
+ sage: DiGraph(g).edges() # optional - python_igraph
+ [(0, 1, {'name': 'a', 'weight': 1}), (0, 2, {'name': 'b', 'weight': 3})]
- However, if there is only one attribute named ``'label'``, that attribute
- is used as the label (so that, if we make a back and forth conversion,
- labels do not change)::
-
- sage: g = igraph.Graph([(0,1),(0,2)], directed=True, edge_attrs={'label':[1,3]}) # optional - python_igraph
- sage: DiGraph(g).edges() # optional - python_igraph
- [(0, 1, 1), (0, 2, 3)]
TESTS::
@@ -906,21 +902,11 @@ class DiGraph(GenericGraph):
raise ValueError("A *directed* igraph graph was expected. To "+
"build an undirected graph, call the Graph "
"constructor.")
- try:
- self.name(data['name'])
- except Exception:
- pass
self.add_vertices(range(data.vcount()))
+ self.add_edges([(e.source, e.target, e.attributes()) for e in data.es()])
- if len(data.edge_attributes()) == 1 and data.edge_attributes()[0] == 'label':
- self.add_edges([(e.source, e.target, e['label']) for e in data.es()])
- elif len(data.edge_attributes()) > 0:
- self.add_edges([(e.source, e.target, e.attributes()) for e in data.es()])
- else:
- self.add_edges(data.get_edgelist())
-
- if 'name' in data.vertex_attributes():
+ if vertex_labels and 'name' in data.vertex_attributes():
vs = data.vs()
self.relabel({v:vs[v]['name'] for v in self})
diff --git a/src/sage/graphs/generic_graph.py b/src/sage/graphs/generic_graph.py
index a4b1d91..9f45117 100644
--- a/src/sage/graphs/generic_graph.py
+++ b/src/sage/graphs/generic_graph.py
@@ -1311,32 +1311,36 @@ class GenericGraph(GenericGraph_pyx):
N.add_edge(u,v,weight=l)
return N
- def igraph_graph(self, weight_function=None, vertex_labels=False, edge_labels=False):
+ def igraph_graph(self, vertex_attrs={}, edge_attrs={}):
r"""
Converts the graph into an igraph graph.
- If present, the name of this graph is added as a graph attribute
- ``'name'`` to the output graph. Optionally, we can define a weight and
- we can transform vertex and edge labels into igraph vertex and edge
- attributes.
+ Optionally, it is possible to add vertex attributes and edge attributes
+ to the output graph.
For more information on the Python version of igraph, see
http://igraph.org/python/.
INPUT: