summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNathann Cohen <nathann.cohen@gmail.com>2015-08-02 23:14:21 +0200
committerNathann Cohen <nathann.cohen@gmail.com>2015-08-03 11:12:10 +0200
commitbcb85c16217374fbced19043be8a8896eac8359f (patch)
treea454659bdd313de0eb9f998e0e7e415e143618e8
parentSmall correction (diff)
trac #18929: Reviewpublic/18929
-rw-r--r--src/sage/graphs/digraph.py58
-rw-r--r--src/sage/graphs/generic_graph.py48
-rw-r--r--src/sage/graphs/graph.py39
3 files changed, 66 insertions, 79 deletions
diff --git a/src/sage/graphs/digraph.py b/src/sage/graphs/digraph.py
index efed0e8..3cac432 100644
--- a/src/sage/graphs/digraph.py
+++ b/src/sage/graphs/digraph.py
@@ -254,9 +254,7 @@ class DiGraph(GenericGraph):
``convert_empty_dict_labels_to_None`` to ``False`` (it is
``True`` by default).
- - ``igraph`` - data must be an igraph directed Graph. In this case,
- vertex and edge labels are also converted, and the graph name is
- deduced, if available, as shown by the following examples.
+ - ``igraph`` - data must be an igraph directed Graph.
- ``boundary`` - a list of boundary vertices, if
empty, digraph is considered as a 'graph without boundary'
@@ -404,31 +402,31 @@ class DiGraph(GenericGraph):
sage: DiGraph(g)
Digraph on 5 vertices
- #. An igraph directed Graph (see also
+ #. An igraph directed Graph (see also
:meth:`~sage.graphs.generic_graph.GenericGraph.igraph_graph`)::
sage: import igraph # optional - python_igraph
sage: g = igraph.Graph([(0,1),(0,2)], directed=True) # optional - python_igraph
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::
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']
-
+
If the igraph Graph has edge attributes, they are used for 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})]
-
+
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)]
@@ -574,14 +572,14 @@ class DiGraph(GenericGraph):
Traceback (most recent call last):
...
ValueError: Unknown input format 'HeyHeyHey'
-
+
Sage DiGraph from igraph undirected graph::
-
+
sage: import igraph # optional - python_igraph
sage: DiGraph(igraph.Graph()) # optional - python_igraph
Traceback (most recent call last):
...
- ValueError: The input is an undirected igraph network, and you are creating a directed Sage network
+ ValueError: A *directed* igraph graph was expected. To build an undirected graph, call the Graph constructor.
"""
msg = ''
GenericGraph.__init__(self)
@@ -905,33 +903,27 @@ class DiGraph(GenericGraph):
self.add_edges((u,v,r(l)) for u,v,l in data.edges_iter(data=True))
elif format == 'igraph':
if not data.is_directed():
- raise ValueError("The input is an undirected igraph network, " +
- "and you are creating a directed Sage network")
+ 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()))
+
+ 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():
vs = data.vs()
- self.add_vertices(vs['name'])
-
- if len(data.edge_attributes()) == 1 and data.edge_attributes()[0] == 'label':
- self.add_edges([(vs[e.source]['name'], vs[e.target]['name'], e['label']) for e in data.es()])
- elif len(data.edge_attributes()) > 0:
- self.add_edges([(vs[e.source]['name'], vs[e.target]['name'], e.attributes()) for e in data.es])
- else:
- self.add_edges([(vs[e.source]['name'], vs[e.target]['name']) for e in data.es])
- else:
- self.add_vertices(range(data.vcount()))
-
- 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())
-
+ self.relabel({v:vs[v]['name'] for v in self})
+
elif format == 'int':
if weighted is None: weighted = False
self.allow_loops(True if loops else False,check=False)
diff --git a/src/sage/graphs/generic_graph.py b/src/sage/graphs/generic_graph.py
index c07aca2..a4b1d91 100644
--- a/src/sage/graphs/generic_graph.py
+++ b/src/sage/graphs/generic_graph.py
@@ -1417,33 +1417,34 @@ class GenericGraph(GenericGraph_pyx):
"""
try:
import igraph
- v_to_int = {v:i for i,v in enumerate(self.vertices())}
- edges = [(v_to_int[v], v_to_int[w]) for v,w in self.edge_iterator(labels=False)]
-
- if vertex_labels:
- vertex_attrs = {'name':[v for i,v in enumerate(self.vertices())]}
- else:
- vertex_attrs = {}
-
- if weight_function:
- edge_attrs = {'weight':[weight_function(e) for e in self.edge_iterator()]}
- else:
- edge_attrs = {}
-
- if edge_labels:
- edge_attrs['label'] = [l for _,_,l in self.edge_iterator()]
-
- return igraph.Graph(n = self.num_verts(),
- edges = edges,
- directed=self.is_directed(),
- graph_attrs = {'name':self.name()},
- vertex_attrs = vertex_attrs,
- edge_attrs = edge_attrs)
except ImportError:
raise ImportError("The package igraph is not available. To " +
"install it, run Sage with option -i " +
"python_igraph.")
+ v_to_int = {v:i for i,v in enumerate(self.vertices())}
+ edges = [(v_to_int[v], v_to_int[w]) for v,w in self.edge_iterator(labels=False)]
+
+ if vertex_labels:
+ vertex_attrs = {'name':[v for i,v in enumerate(self.vertices())]}
+ else:
+ vertex_attrs = {}
+
+ if weight_function:
+ edge_attrs = {'weight':[weight_function(e) for e in self.edge_iterator()]}
+ else:
+ edge_attrs = {}
+
+ if edge_labels:
+ edge_attrs['label'] = [l for _,_,l in self.edge_iterator()]
+
+ return igraph.Graph(n = self.num_verts(),
+ edges = edges,
+ directed=self.is_directed(),
+ graph_attrs = {'name':self.name()},
+ vertex_attrs = vertex_attrs,
+ edge_attrs = edge_attrs)
+
def to_dictionary(self, edge_labels=False, multiple_edges=False):
r"""
Returns the graph as a dictionary.
@@ -18021,7 +18022,8 @@ class GenericGraph(GenericGraph_pyx):
setattr(self, attr, new_attr)
- self._boundary = [perm[v] for v in self._boundary]
+ if hasattr(self,'_boundary'):
+ self._boundary = [perm[v] for v in self._boundary]
if return_map:
return perm
diff --git a/src/sage/graphs/graph.py b/src/sage/graphs/graph.py
index 3c8af9f..94191ce 100644
--- a/src/sage/graphs/graph.py
+++ b/src/sage/graphs/graph.py
@@ -697,9 +697,7 @@ class Graph(GenericGraph):
``convert_empty_dict_labels_to_None`` to ``False`` (it is
``True`` by default).
- - ``igraph`` - data must be an igraph Graph. In this case, vertex and
- edge labels are also converted, and the graph name is deduced, if
- available, as shown by the following examples.
+ - ``igraph`` - data must be an `igraph <http://igraph.org/>`__ graph.
- ``boundary`` - a list of boundary vertices, if
empty, graph is considered as a 'graph without boundary'
@@ -1025,7 +1023,8 @@ class Graph(GenericGraph):
sage: Graph(igraph.Graph(directed=True)) # optional - python_igraph
Traceback (most recent call last):
...
- ValueError: The input is a directed igraph network, and you are creating an undirected Sage network
+ ValueError: An *undirected* igraph graph was expected. To build an directed graph, call the DiGraph constructor.
+
"""
_directed = False
@@ -1437,32 +1436,26 @@ class Graph(GenericGraph):
self.add_edges((u,v,r(l)) for u,v,l in data.edges_iter(data=True))
elif format == 'igraph':
if data.is_directed():
- raise ValueError("The input is a directed igraph network, and" +
- " you are creating an undirected Sage network")
+ raise ValueError("An *undirected* igraph graph was expected. "+
+ "To build an directed graph, call the DiGraph "+
+ "constructor.")
try:
self.name(data['name'])
- except Exception:
+ except KeyError:
pass
- if 'name' in data.vertex_attributes():
- vs = data.vs()
- self.add_vertices(vs['name'])
+ self.add_vertices(range(data.vcount()))
- if len(data.edge_attributes()) == 1 and data.edge_attributes()[0] == 'label':
- self.add_edges([(vs[e.source]['name'], vs[e.target]['name'], e['label']) for e in data.es()])
- elif len(data.edge_attributes()) > 0:
- self.add_edges([(vs[e.source]['name'], vs[e.target]['name'], e.attributes()) for e in data.es])
- else:
- self.add_edges([(vs[e.source]['name'], vs[e.target]['name']) 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_vertices(range(data.vcount()))
+ self.add_edges(data.get_edgelist())
- 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():
+ vs = data.vs()
+ self.relabel({v:vs[v]['name'] for v in self})
elif format == 'rule':
f = data[1]