Ticket #13917: trac_13917-doctests.patch

File trac_13917-doctests.patch, 4.6 KB (added by ncohen, 8 years ago)
  • sage/graphs/independent_sets.pyx

    # HG changeset patch
    # User Nathann Cohen <nathann.cohen@gmail.com>
    # Date 1370613268 -7200
    # Node ID ddefd5f9699c0119a14636e30e399043c82a7638
    # Parent  402521047ba097ce7319126f03c544e6c8220c1a
    [mq]: rareharh
    
    diff --git a/sage/graphs/independent_sets.pyx b/sage/graphs/independent_sets.pyx
    a b  
    9898        sage: Im.cardinality()
    9999        2
    100100
    101     One can easily obtain an iterator over all independent sets of given
     101    One can easily count the number of independent sets of each
    102102    cardinality::
    103103
    104104        sage: g = graphs.PetersenGraph()
    105         sage: is4 = (x for x in IndependentSets(g) if len(x) == 4)
    106         sage: list(is4)
    107         [[0, 2, 8, 9], [0, 3, 6, 7], [1, 3, 5, 9], [1, 4, 7, 8], [2, 4, 5, 6]]
    108 
    109     Similarly, one can easily count the number of independent sets of each
    110     cardinality::
    111 
    112105        sage: number_of = [0] * g.order()
    113106        sage: for x in IndependentSets(g):
    114107        ....:     number_of[len(x)] += 1
    115108        sage: print number_of
    116109        [1, 10, 30, 30, 5, 0, 0, 0, 0, 0]
    117110
     111    It is also possible to define an an iterator over all independent sets of a
     112    given cardinality. Note, however, that Sage will generate them *all*, to
     113    return only those that satisfy the cardinality constraints. Getting the list
     114    of independent sets of size 0 in this way can thus take a very long time::
     115
     116        sage: is4 = (x for x in IndependentSets(g) if len(x) == 4)
     117        sage: list(is4)
     118        [[0, 2, 8, 9], [0, 3, 6, 7], [1, 3, 5, 9], [1, 4, 7, 8], [2, 4, 5, 6]]
     119
    118120    Given a subset of the vertices, it is possible to test whether it is an
    119121    independent set::
    120122
     
    130132        sage: [0, 'a', 'b', 'c'] in I
    131133        Traceback (most recent call last):
    132134        ...
    133         ProgrammerError: It should not be a segfault!
     135        ValueError: a is not a vertex of the graph.
    134136    """
    135137    def __init__(self, G, maximal = False, complement = False):
    136138        r"""
     
    170172            sage: for i in range(5):
    171173            ...       check_matching(graphs.RandomGNP(11,.3))
    172174
    173         Check the error for the empty graph::
     175        Empty graph::
    174176
    175             sage: IndependentSets(graphs.empty_graph())
    176             Traceback (most recent call last):
    177             ...
    178             ProgrammerError: It should not be a segfault!
     177            sage: IS0 = IndependentSets(graphs.EmptyGraph())
     178            sage: list(IS0)
     179            [[]]
     180            sage: IS0.cardinality()
     181            1
    179182        """
    180183        cdef int i
    181         if G.order() == 0:
    182             raise ValueError("This class can only handle non-empty graphs")
    183184
    184185        # Map from Vertex to Integer, and from Integer to Vertex
    185186        self.vertices = G.vertices()
     
    214215            sage: iter1.next()
    215216            [0, 2]
    216217        """
     218        if self.n == 0:
     219            yield []
     220            return
     221
    217222        cdef int i = 0
    218223
    219224        cdef bitset_t current_set
     
    251256                        bitset_discard(current_set,i)
    252257
    253258                    # Returning the result if necessary ...
    254 
    255259                    if self.maximal and not ismaximal(self.g,self.n, tmp):
    256260                        continue
    257261
     
    273277            # Not already included in the set
    274278            else:
    275279                if i == 0:
    276                     if not self.maximal:
    277                         count+=1
    278                         if not self.count_only:
    279                             yield []
    280280                    break
    281281
    282282                # Going backward, we explored all we could there !
     
    288288                    if i == -1:
    289289                        break
    290290
     291        if not self.maximal:
     292            count += 1
     293            if not self.count_only:
     294                yield []
    291295
    292296        if self.count_only:
    293297            yield count
     
    299303        r"""
    300304        Frees everything we ever allocated
    301305        """
    302         binary_matrix_free(self.g)
     306        if self.g.rows != NULL:
     307            binary_matrix_free(self.g)
    303308
    304309    @cached_method
    305310    def cardinality(self):
     
    318323            sage: IndependentSets(graphs.PetersenGraph(), maximal = True).cardinality()
    319324            15
    320325        """
     326        if self.n == 0:
     327            return 1
     328
    321329        self.count_only = 1
    322330
    323331        for i in self:
     
    373381            try:
    374382                i = self.vertex_to_int[I]
    375383            except KeyError:
    376                 raise ValueError("An element of the set being tested does not belong to ")
     384                raise ValueError(str(I)+" is not a vertex of the graph.")
    377385
    378386            # Adding the new vertex to s
    379387            bitset_add(s, i)