def __call__(self, vertices, property=lambda x: True, augment='edges', size=None, implementation='c_graph', sparse=True): """ Accesses the generator of isomorphism class representatives. Iterates over distinct, exhaustive representatives. INPUT: - ``vertices`` - natural number - ``property`` - any property to be tested on digraphs before generation. - ``augment`` - choices: - ``'vertices'`` - augments by adding a vertex, and edges incident to that vertex. In this case, all digraphs on up to n=vertices are generated. If for any digraph G satisfying the property, every subgraph, obtained from G by deleting one vertex and only edges incident to that vertex, satisfies the property, then this will generate all digraphs with that property. If this does not hold, then all the digraphs generated will satisfy the property, but there will be some missing. - ``'edges'`` - augments a fixed number of vertices by adding one edge In this case, all digraphs on exactly n=vertices are generated. If for any graph G satisfying the property, every subgraph, obtained from G by deleting one edge but not the vertices incident to that edge, satisfies the property, then this will generate all digraphs with that property. If this does not hold, then all the digraphs generated will satisfy the property, but there will be some missing. - ``implementation`` - which underlying implementation to use (see DiGraph?) - ``sparse`` - ignored if implementation is not ``c_graph`` EXAMPLES: Print digraphs on 2 or less vertices. :: sage: for D in digraphs(2, augment='vertices'): ... print D ... Digraph on 0 vertices Digraph on 1 vertex Digraph on 2 vertices Digraph on 2 vertices Digraph on 2 vertices Print digraphs on 3 vertices. :: sage: for D in digraphs(3): ... print D Digraph on 3 vertices Digraph on 3 vertices ... Digraph on 3 vertices Digraph on 3 vertices For more examples, see the class level documentation, or type :: sage: digraphs? # not tested REFERENCE: - Brendan D. McKay, Isomorph-Free Exhaustive generation. Journal of Algorithms Volume 26, Issue 2, February 1998, pages 306-324. """ if size is not None: extra_property = lambda x: x.size() == size else: extra_property = lambda x: True if augment == 'vertices': from sage.graphs.graph_generators import canaug_traverse_vert g = DiGraph(implementation=implementation, sparse=sparse) for gg in canaug_traverse_vert(g, [], vertices, property, dig=True, implementation=implementation, sparse=sparse): if extra_property(gg): yield gg elif augment == 'edges': from sage.graphs.graph_generators import canaug_traverse_edge g = DiGraph(vertices, implementation=implementation, sparse=sparse) gens = [] for i in range(vertices - 1): gen = range(i) gen.append(i + 1) gen.append(i) gen += range(i + 2, vertices) gens.append(gen) for gg in canaug_traverse_edge(g, gens, property, dig=True, implementation=implementation, sparse=sparse): if extra_property(gg): yield gg else: raise NotImplementedError()
def __call__(self, vertices, property=lambda x: True, augment='edges', size=None, implementation='c_graph', sparse=True): """ Accesses the generator of isomorphism class representatives. Iterates over distinct, exhaustive representatives. INPUT: - ``vertices`` - natural number - ``property`` - any property to be tested on digraphs before generation. - ``augment`` - choices: - ``'vertices'`` - augments by adding a vertex, and edges incident to that vertex. In this case, all digraphs on up to n=vertices are generated. If for any digraph G satisfying the property, every subgraph, obtained from G by deleting one vertex and only edges incident to that vertex, satisfies the property, then this will generate all digraphs with that property. If this does not hold, then all the digraphs generated will satisfy the property, but there will be some missing. - ``'edges'`` - augments a fixed number of vertices by adding one edge In this case, all digraphs on exactly n=vertices are generated. If for any graph G satisfying the property, every subgraph, obtained from G by deleting one edge but not the vertices incident to that edge, satisfies the property, then this will generate all digraphs with that property. If this does not hold, then all the digraphs generated will satisfy the property, but there will be some missing. - ``implementation`` - which underlying implementation to use (see DiGraph?) - ``sparse`` - ignored if implementation is not ``c_graph`` EXAMPLES: Print digraphs on 2 or less vertices. :: sage: for D in digraphs(2, augment='vertices'): ... print D ... Digraph on 0 vertices Digraph on 1 vertex Digraph on 2 vertices Digraph on 2 vertices Digraph on 2 vertices Print digraphs on 3 vertices. :: sage: for D in digraphs(3): ... print D Digraph on 3 vertices Digraph on 3 vertices ... Digraph on 3 vertices Digraph on 3 vertices For more examples, see the class level documentation, or type :: sage: digraphs? # not tested REFERENCE: - Brendan D. McKay, Isomorph-Free Exhaustive generation. Journal of Algorithms Volume 26, Issue 2, February 1998, pages 306-324. """ if size is not None: extra_property = lambda x: x.size() == size else: extra_property = lambda x: True if augment == 'vertices': from sage.graphs.graph_generators import canaug_traverse_vert g = DiGraph(implementation=implementation, sparse=sparse) for gg in canaug_traverse_vert(g, [], vertices, property, dig=True, implementation=implementation, sparse=sparse): if extra_property(gg): yield gg elif augment == 'edges': from sage.graphs.graph_generators import canaug_traverse_edge g = DiGraph(vertices, implementation=implementation, sparse=sparse) gens = [] for i in range(vertices-1): gen = range(i) gen.append(i+1); gen.append(i) gen += range(i+2, vertices) gens.append(gen) for gg in canaug_traverse_edge(g, gens, property, dig=True, implementation=implementation, sparse=sparse): if extra_property(gg): yield gg else: raise NotImplementedError()