Ticket #8458: trac_8458_independent_graphs.patch

File trac_8458_independent_graphs.patch, 3.5 KB (added by brunellus, 10 years ago)
  • sage/graphs/graph_generators.py

    # HG changeset patch
    # User Lukas Lansky <lansky@kam.mff.cuni.cz>
    # Date 1325631840 0
    # Node ID a72a3005d4ed13c33b82c15a7befacb7759a24ed
    # Parent  05c9f113ccede59fbca9ae26587fa2c4578871c5
    Trac 8458: generated graphs are independent
    
    diff --git a/sage/graphs/graph_generators.py b/sage/graphs/graph_generators.py
    a b  
    325325    - ``sparse`` -- (default: ``True``) ignored if implementation is not
    326326      ``'c_graph'``.
    327327
     328    - ``independent`` -- (default: ``True``) the graph generator can use parts
     329      of already generated graphs to create new ones. That is fast, but any change
     330      of an generated graph, like edge removal or label update, randomly propagates
     331      to the others. Moreover, the generator can crash or produce incorrect results
     332      if the changes to the resulted graphs are made before the end of generation.
     333      Whenever ``independent`` is set to ``True``, the generator make sure that
     334      resulting graphs are separated both from each other and from the inner state
     335      of the generator.
     336
    328337    EXAMPLES:
    329338
    330339    Print graphs on 3 or less vertices::
     
    481490        sage: print 10, len([g for g in graphs(10,deg_seq=[3]*10) if g.is_connected()]) # not tested
    482491        10 19
    483492
     493    Make sure that the graphs are really independent and the generator
     494    survives repeated vertex removal (trac 8458)::
     495
     496        sage: for G in graphs(3):
     497        ...       G.delete_vertex(0)
     498        ...       print(G.order())
     499        2
     500        2
     501        2
     502        2
     503
    484504    REFERENCE:
    485505
    486506    - Brendan D. McKay, Isomorph-Free Exhaustive generation.  *Journal
     
    63846404   
    63856405    def __call__(self, vertices=None, property=lambda x: True, augment='edges',
    63866406        size=None, deg_seq=None, loops=False, implementation='c_graph',
    6387         sparse=True):
     6407        sparse=True, independent=True):
    63886408        """
    63896409        Accesses the generator of isomorphism class representatives.
    63906410        Iterates over distinct, exhaustive representatives. See the docstring
     
    64306450          pages 306-324.
    64316451        """
    64326452        from sage.graphs.all import Graph
     6453        from copy import copy
    64336454        if deg_seq is not None:
    64346455            if vertices is None:
    64356456                raise NotImplementedError
     
    64526473            g = Graph(loops=loops, implementation=implementation, sparse=sparse)
    64536474            for gg in canaug_traverse_vert(g, [], vertices, property, loops=loops, implementation=implementation, sparse=sparse):
    64546475                if extra_property(gg):
    6455                     yield gg
     6476                    yield copy(gg) if independent else gg
    64566477        elif augment == 'edges':
    64576478            if vertices is None:
    64586479                from sage.rings.all import Integer
    64596480                vertices = Integer(0)
    64606481                while True:
    64616482                    for g in self(vertices, loops=loops, implementation=implementation, sparse=sparse):
    6462                         yield g
     6483                        yield copy(g) if independent else g
    64636484                    vertices += 1
    64646485            g = Graph(vertices, loops=loops, implementation=implementation, sparse=sparse)
    64656486            gens = []
     
    64706491                gens.append(gen)
    64716492            for gg in canaug_traverse_edge(g, gens, property, loops=loops, implementation=implementation, sparse=sparse):
    64726493                if extra_property(gg):
    6473                     yield gg
     6494                    yield copy(gg) if independent else gg
    64746495        else:
    64756496            raise NotImplementedError
    64766497