From 44e925fa54823f18f3c4d39fba05ed2bb98874f4 Mon Sep 17 00:00:00 2001 From: Travis Scrimshaw Date: Sun, 18 Sep 2016 10:56:39 -0500 Subject: [PATCH] Implementing iterator over all orientations of a graph. --- src/sage/graphs/graph.py | 87 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/src/sage/graphs/graph.py b/src/sage/graphs/graph.py index c320d149543..0ff23971078 100644 --- a/src/sage/graphs/graph.py +++ b/src/sage/graphs/graph.py @@ -3403,6 +3403,93 @@ def bounded_outdegree_orientation(self, bound): return D + @doc_index("Connectivity, orientations, trees") + def orientations(self, implementation='c_graph', data_structure=None, sparse=None): + r""" + Return an iterator over orientations of ``self``. + + An *orientation* of an undirected graph is a directed + graph such that every edge is assigned a direction. + Hence there are `2^s` oriented digraphs for a graph + with `s` edges. + + INPUT: + + - ``data_structure`` -- one of ``"sparse"``, ``"static_sparse"``, or + ``"dense"``; see the documentation of :class:`Graph` or + :class:`DiGraph` + + - ``sparse`` -- (optional) boolean; ``sparse=True`` is an alias for + ``data_structure="sparse"``, and ``sparse=False`` is an alias for + ``data_structure="dense"`` + + EXAMPLES:: + + sage: G = Graph([[1,2,3], [(1, 2, 'a'), (1, 3, 'b')]], format='vertices_and_edges') + sage: it = G.orientations() + sage: D = it.next() + sage: D.edges() + [(1, 2, 'a'), (1, 3, 'b')] + sage: D = it.next() + sage: D.edges() + [(1, 2, 'a'), (3, 1, 'b')] + + TESTS:: + + sage: G = Graph() + sage: D = [g for g in G.orientations()] + sage: len(D) + 1 + sage: D[0] + Digraph on 0 vertices + + sage: G = Graph(5) + sage: it = G.orientations() + sage: D = next(it) + sage: D.size() + 0 + """ + if sparse is not None: + if data_structure is not None: + raise ValueError("cannot specify both 'sparse' and 'data_structure'") + data_structure = "sparse" if sparse else "dense" + if data_structure is None: + from sage.graphs.base.dense_graph import DenseGraphBackend + from sage.graphs.base.sparse_graph import SparseGraphBackend + if isinstance(self._backend, DenseGraphBackend): + data_structure = "dense" + elif isinstance(self._backend, SparseGraphBackend): + data_structure = "sparse" + else: + data_structure = "static_sparse" + + if self.num_edges() == 0: + D = DiGraph(name=self.name(), + pos=self._pos, + multiedges=self.allows_multiple_edges(), + loops=self.allows_loops(), + implementation=implementation, + data_structure=data_structure) + if hasattr(self, '_embedding'): + D._embedding = copy(self._embedding) + yield D + return + + from itertools import product + E = [[(u,v,label), (v,u,label)] for u,v,label in self.edges()] + verts = self.vertices() + for edges in product(*E): + D = DiGraph(data=[verts, edges], + format='vertices_and_edges', + name=self.name(), + pos=self._pos, + multiedges=self.allows_multiple_edges(), + loops=self.allows_loops(), + implementation=implementation, + data_structure=data_structure) + if hasattr(self, '_embedding'): + D._embedding = copy(self._embedding) + yield D ### Coloring