summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichele Borassi <michele.borassi@imtlucca.it>2015-07-28 19:20:37 +0200
committerMichele Borassi <michele.borassi@imtlucca.it>2015-07-28 19:20:37 +0200
commitb5c421f1902bb9c41a01d1359d1fc0c2407f4e5f (patch)
tree14bdc488c798b37eea6e3578b30dbaf82b74ee3f
parentConversion of graph name, edge labels, vertex name, and edge weight (diff)
Removed trailing whitespaces
-rw-r--r--src/sage/graphs/generic_graph.py52
-rw-r--r--src/sage/graphs/graph.py26
2 files changed, 39 insertions, 39 deletions
diff --git a/src/sage/graphs/generic_graph.py b/src/sage/graphs/generic_graph.py
index 3367657..34e8fb2 100644
--- a/src/sage/graphs/generic_graph.py
+++ b/src/sage/graphs/generic_graph.py
@@ -1319,27 +1319,27 @@ class GenericGraph(GenericGraph_pyx):
``'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.
-
+
For more information on the Python version of igraph, see
http://igraph.org/python/.
-
+
INPUT:
-
+
- ``weight_function`` (function) - a function that inputs an edge
``(u, v, l)`` and outputs its weight. If provided, the weight obtained
is stored as an attribute of the edges of the igraph network, named
``'weight'``. As a consequence, igraph will consider this graph
weighted.
-
+
- ``vertex_labels`` (boolean) - if True, we add a vertex attribute
``'name'`` to the output graph, which is equal to the vertex name in
Sage. If False, the igraph network will have no vertex attribute.
-
+
- ``edge_labels`` (boolean) - if True, the Sage edge labels are stored
- inside an igraph edge attribute named ``'label'``.
+ inside an igraph edge attribute named ``'label'``.
EXAMPLES:
-
+
Standard conversion::
sage: G = graphs.TetrahedralGraph() # optional - python_igraph
@@ -1350,33 +1350,33 @@ class GenericGraph(GenericGraph_pyx):
sage: H = G.igraph_graph() # optional - python_igraph
sage: H.summary() # optional - python_igraph
'IGRAPH D--- 3 2 -- Path\n+ attr: name (g)'
-
+
Adding a weight::
-
+
sage: G = Graph([(1,2,{'w':1}),(2,3,{'w':2})]) # optional - python_igraph
sage: H = G.igraph_graph(weight_function=lambda e:e[2]['w']) # optional - python_igraph
sage: H.is_weighted() # optional - python_igraph
True
sage: H.es['weight'] # optional - python_igraph
[1, 2]
-
+
Edge labeled graphs::
-
+
sage: H = G.igraph_graph(edge_labels = True) # optional - python_igraph
sage: H.es['label'] # optional - python_igraph
[{'w': 1}, {'w': 2}]
-
+
Vertex labeled graphs::
-
+
sage: G = graphs.GridGraph([2,2]) # optional - python_igraph
sage: H = G.igraph_graph(vertex_labels=True) # optional - python_igraph
sage: H.vs()['name'] # optional - python_igraph
[(0, 0), (0, 1), (1, 0), (1, 1)]
-
+
TESTS:
-
+
Converting a DiGraph back and forth::
-
+
sage: G = DiGraph([('a','b',{'w':1}),('b','c',{'w':2})]) # optional - python_igraph
sage: H = DiGraph(G.igraph_graph(vertex_labels=True, edge_labels=True)) # optional - python_igraph
sage: G == H # optional - python_igraph
@@ -1386,17 +1386,17 @@ class GenericGraph(GenericGraph_pyx):
sage: H = DiGraph(G.igraph_graph(edge_labels=True)) # optional - python_igraph
sage: G == H # optional - python_igraph
False
-
+
When checking for equality, edge labels are not taken into account::
-
+
sage: H = DiGraph(G.igraph_graph(vertex_labels=True)) # optional - python_igraph
sage: G == H # optional - python_igraph
True
sage: G.edges() == H.edges() # optional - python_igraph
False
-
+
Converting a Graph back and forth::
-
+
sage: G = Graph([('a','b',{'w':1}),('b','c',{'w':2})]) # optional - python_igraph
sage: H = Graph(G.igraph_graph(vertex_labels=True, edge_labels=True)) # optional - python_igraph
sage: G == H # optional - python_igraph
@@ -1406,25 +1406,25 @@ class GenericGraph(GenericGraph_pyx):
sage: H = Graph(G.igraph_graph(edge_labels=True)) # optional - python_igraph
sage: G == H # optional - python_igraph
False
-
+
When checking for equality, edge labels are not taken into account::
-
+
sage: H = Graph(G.igraph_graph(vertex_labels=True)) # optional - python_igraph
sage: G == H # optional - python_igraph
True
sage: G.edges() == H.edges() # optional - python_igraph
- False
+ False
"""
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:
@@ -1432,7 +1432,7 @@ class GenericGraph(GenericGraph_pyx):
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(),
diff --git a/src/sage/graphs/graph.py b/src/sage/graphs/graph.py
index a06a58c..2299184 100644
--- a/src/sage/graphs/graph.py
+++ b/src/sage/graphs/graph.py
@@ -339,7 +339,7 @@ examples are covered here.
Graph on 5 vertices
- an igraph Graph::
-
+
sage: import igraph # optional - python_igraph
sage: g = Graph(igraph.Graph([(1,3),(3,2),(0,2)])) # optional - python_igraph
sage: g # optional - python_igraph
@@ -966,31 +966,31 @@ class Graph(GenericGraph):
sage: DiGraph(g)
Digraph on 5 vertices
- #. An igraph Graph (see also
+ #. An igraph 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)]) # optional - python_igraph
sage: Graph(g) # optional - python_igraph
Graph 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)], vertex_attrs={'name':['a','b','c']}) # optional - python_igraph
sage: Graph(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)], edge_attrs={'weight':[1,3]}) # optional - python_igraph
sage: Graph(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)], edge_attrs={'label':[1,3]}) # optional - python_igraph
sage: Graph(g).edges() # optional - python_igraph
[(0, 1, 1), (0, 2, 3)]
@@ -1021,7 +1021,7 @@ class Graph(GenericGraph):
Traceback (most recent call last):
...
ValueError: Unknown input format 'HeyHeyHey'
-
+
sage: Graph(igraph.Graph(directed=True)) # optional - python_igraph
Traceback (most recent call last):
...
@@ -1438,16 +1438,16 @@ class Graph(GenericGraph):
elif format == 'igraph':
if data.is_directed():
raise ValueError("The input is a directed igraph network, and" +
- " you are creating an undirected Sage network")
+ " you are creating an undirected Sage network")
try:
self.name(data['name'])
except Exception:
pass
-
+
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:
@@ -1456,14 +1456,14 @@ class Graph(GenericGraph):
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())
-
+
elif format == 'rule':
f = data[1]
verts = data[0]