Ejemplo n.º 1
0
    def breadth_first_search_iterator(self):
        r"""
        Return a breadth first search iterator over the elements of ``self``

        EXAMPLES::

            sage: f = SearchForest([[]],
            ....:                  lambda l: [l+[0], l+[1]] if len(l) < 3 else [])
            doctest:...: DeprecationWarning: This class soon will not be
            available in that way anymore. Use RecursivelyEnumeratedSet
            instead.  See http://trac.sagemath.org/6637 for details.
            sage: list(f.breadth_first_search_iterator())
            [[], [0], [1], [0, 0], [0, 1], [1, 0], [1, 1], [0, 0, 0], [0, 0, 1], [0, 1, 0], [0, 1, 1], [1, 0, 0], [1, 0, 1], [1, 1, 0], [1, 1, 1]]
            sage: S = SearchForest([(0,0)],
            ....: lambda x : [(x[0], x[1]+1)] if x[1] != 0 else [(x[0]+1,0), (x[0],1)],
            ....: post_process = lambda x: x if ((is_prime(x[0]) and is_prime(x[1])) and ((x[0] - x[1]) == 2)) else None)
            doctest:...: DeprecationWarning: This class soon will not be
            available in that way anymore. Use RecursivelyEnumeratedSet
            instead.  See http://trac.sagemath.org/6637 for details.
            sage: p = S.breadth_first_search_iterator()
            sage: [next(p), next(p), next(p), next(p), next(p), next(p), next(p)]
            [(5, 3), (7, 5), (13, 11), (19, 17), (31, 29), (43, 41), (61, 59)]
        """
        iter = search_forest_iterator(self.roots(),
                                      self.children,
                                      algorithm='breadth')
        if hasattr(self, "post_process"):
            iter = imap_and_filter_none(self.post_process, iter)
        return iter
Ejemplo n.º 2
0
    def elements_of_depth_iterator(self, depth=0):
        r"""
        Return an iterator over the elements of ``self`` of given depth.
        An element of depth `n` can be obtained applying `n` times the
        children function from a root.

        EXAMPLES::

            sage: S = SearchForest([(0,0)] ,
            ....:        lambda x : [(x[0], x[1]+1)] if x[1] != 0 else [(x[0]+1,0), (x[0],1)],
            ....:        post_process = lambda x: x if ((is_prime(x[0]) and is_prime(x[1]))
            ....:                                        and ((x[0] - x[1]) == 2)) else None)
            doctest:...: DeprecationWarning: This class soon will not be
            available in that way anymore. Use RecursivelyEnumeratedSet
            instead.  See http://trac.sagemath.org/6637 for details.
            sage: p = S.elements_of_depth_iterator(8)
            sage: next(p)
            (5, 3)
            sage: S = SearchForest(NN, lambda x : [],
            ....:                      lambda x: x^2 if x.is_prime() else None)
            doctest:...: DeprecationWarning: This class soon will not be
            available in that way anymore. Use RecursivelyEnumeratedSet
            instead.  See http://trac.sagemath.org/6637 for details.
            sage: p = S.elements_of_depth_iterator(0)
            sage: [next(p), next(p), next(p), next(p), next(p)]
            [4, 9, 25, 49, 121]
        """
        iter = self._elements_of_depth_iterator_rec(depth)
        if hasattr(self, "post_process"):
            iter = imap_and_filter_none(self.post_process, iter)
        return iter
Ejemplo n.º 3
0
    def elements_of_depth_iterator(self, depth=0):
        r"""
        Returns an iterator over the elements of ``self`` of given depth.
        An element of depth `n` can be obtained applying `n` times the
        children function from a root.

        EXAMPLES::

            sage: S = SearchForest([(0,0)] ,
            ...          lambda x : [(x[0], x[1]+1)] if x[1] != 0 else [(x[0]+1,0), (x[0],1)],
            ...          post_process = lambda x: x if ((is_prime(x[0]) and is_prime(x[1]))
            ...                                          and ((x[0] - x[1]) == 2)) else None)
            sage: p = S.elements_of_depth_iterator(8)
            sage: p.next()
            (5, 3)
            sage: S = SearchForest(NN, lambda x : [],
            ...                        lambda x: x^2 if x.is_prime() else None)
            sage: p = S.elements_of_depth_iterator(0)
            sage: [p.next(), p.next(), p.next(), p.next(), p.next()]
            [4, 9, 25, 49, 121]
        """
        iter = self._elements_of_depth_iterator_rec(depth)
        if hasattr(self, "post_process"):
            iter = imap_and_filter_none(self.post_process, iter)
        return iter
Ejemplo n.º 4
0
    def __iter__(self):
        r"""
        Return an iterator over the elements of ``self``.

        EXAMPLES::

            sage: def children(l):
            ....:      return [l+[0], l+[1]]
            ....:
            sage: C = SearchForest(([],), children)
            doctest:...: DeprecationWarning: This class soon will not be
            available in that way anymore. Use RecursivelyEnumeratedSet
            instead.  See http://trac.sagemath.org/6637 for details.
            sage: f = C.__iter__()
            sage: next(f)
            []
            sage: next(f)
            [0]
            sage: next(f)
            [0, 0]
        """
        iter = search_forest_iterator(self.roots(),
                                      self.children,
                                      algorithm=self._algorithm)
        if hasattr(self, "post_process"):
            iter = imap_and_filter_none(self.post_process, iter)
        return iter
Ejemplo n.º 5
0
    def elements_of_depth_iterator(self, depth=0):
        r"""
        Return an iterator over the elements of ``self`` of given depth.
        An element of depth `n` can be obtained applying `n` times the
        children function from a root.

        EXAMPLES::

            sage: S = SearchForest([(0,0)] ,
            ....:        lambda x : [(x[0], x[1]+1)] if x[1] != 0 else [(x[0]+1,0), (x[0],1)],
            ....:        post_process = lambda x: x if ((is_prime(x[0]) and is_prime(x[1]))
            ....:                                        and ((x[0] - x[1]) == 2)) else None)
            doctest:...: DeprecationWarning: This class soon will not be
            available in that way anymore. Use RecursivelyEnumeratedSet
            instead.  See http://trac.sagemath.org/6637 for details.
            sage: p = S.elements_of_depth_iterator(8)
            sage: next(p)
            (5, 3)
            sage: S = SearchForest(NN, lambda x : [],
            ....:                      lambda x: x^2 if x.is_prime() else None)
            doctest:...: DeprecationWarning: This class soon will not be
            available in that way anymore. Use RecursivelyEnumeratedSet
            instead.  See http://trac.sagemath.org/6637 for details.
            sage: p = S.elements_of_depth_iterator(0)
            sage: [next(p), next(p), next(p), next(p), next(p)]
            [4, 9, 25, 49, 121]
        """
        iter = self._elements_of_depth_iterator_rec(depth)
        if hasattr(self, "post_process"):
            iter = imap_and_filter_none(self.post_process, iter)
        return iter
Ejemplo n.º 6
0
    def elements_of_depth_iterator(self, depth=0):
        r"""
        Returns an iterator over the elements of ``self`` of given depth.
        An element of depth `n` can be obtained applying `n` times the
        children function from a root.

        EXAMPLES::

            sage: S = SearchForest([(0,0)] ,
            ...          lambda x : [(x[0], x[1]+1)] if x[1] != 0 else [(x[0]+1,0), (x[0],1)],
            ...          post_process = lambda x: x if ((is_prime(x[0]) and is_prime(x[1]))
            ...                                          and ((x[0] - x[1]) == 2)) else None)
            sage: p = S.elements_of_depth_iterator(8)
            sage: p.next()
            (5, 3)
            sage: S = SearchForest(NN, lambda x : [],
            ...                        lambda x: x^2 if x.is_prime() else None)
            sage: p = S.elements_of_depth_iterator(0)
            sage: [p.next(), p.next(), p.next(), p.next(), p.next()]
            [4, 9, 25, 49, 121]
        """
        iter = self._elements_of_depth_iterator_rec(depth)
        if hasattr(self, "post_process"):
            iter = imap_and_filter_none(self.post_process, iter)
        return iter
Ejemplo n.º 7
0
    def breadth_first_search_iterator(self):
        r"""
        Return a breadth first search iterator over the elements of ``self``

        EXAMPLES::

            sage: f = SearchForest([[]],
            ....:                  lambda l: [l+[0], l+[1]] if len(l) < 3 else [])
            doctest:...: DeprecationWarning: This class soon will not be
            available in that way anymore. Use RecursivelyEnumeratedSet
            instead.  See http://trac.sagemath.org/6637 for details.
            sage: list(f.breadth_first_search_iterator())
            [[], [0], [1], [0, 0], [0, 1], [1, 0], [1, 1], [0, 0, 0], [0, 0, 1], [0, 1, 0], [0, 1, 1], [1, 0, 0], [1, 0, 1], [1, 1, 0], [1, 1, 1]]
            sage: S = SearchForest([(0,0)],
            ....: lambda x : [(x[0], x[1]+1)] if x[1] != 0 else [(x[0]+1,0), (x[0],1)],
            ....: post_process = lambda x: x if ((is_prime(x[0]) and is_prime(x[1])) and ((x[0] - x[1]) == 2)) else None)
            doctest:...: DeprecationWarning: This class soon will not be
            available in that way anymore. Use RecursivelyEnumeratedSet
            instead.  See http://trac.sagemath.org/6637 for details.
            sage: p = S.breadth_first_search_iterator()
            sage: [next(p), next(p), next(p), next(p), next(p), next(p), next(p)]
            [(5, 3), (7, 5), (13, 11), (19, 17), (31, 29), (43, 41), (61, 59)]
        """
        iter = search_forest_iterator(self.roots(), self.children, algorithm='breadth')
        if hasattr(self, "post_process"):
            iter = imap_and_filter_none(self.post_process, iter)
        return iter
Ejemplo n.º 8
0
    def __iter__(self):
        r"""
        Return an iterator over the elements of ``self``.

        EXAMPLES::

            sage: def children(l):
            ....:      return [l+[0], l+[1]]
            ....:
            sage: C = SearchForest(([],), children)
            doctest:...: DeprecationWarning: This class soon will not be
            available in that way anymore. Use RecursivelyEnumeratedSet
            instead.  See http://trac.sagemath.org/6637 for details.
            sage: f = C.__iter__()
            sage: next(f)
            []
            sage: next(f)
            [0]
            sage: next(f)
            [0, 0]
        """
        iter = search_forest_iterator(self.roots(),
                                      self.children,
                                      algorithm = self._algorithm)
        if hasattr(self, "post_process"):
            iter = imap_and_filter_none(self.post_process, iter)
        return iter
Ejemplo n.º 9
0
    def __iter__(self):
        r"""
        Return an iterator over the elements of ``self``.

        EXAMPLES::

            sage: from sage.combinat.backtrack import SearchForest
            sage: def children(l):
            ....:      return [l+[0], l+[1]]
            ....:
            sage: C = SearchForest(([],), children)
            sage: f = C.__iter__()
            sage: next(f)
            []
            sage: next(f)
            [0]
            sage: next(f)
            [0, 0]
        """
        iter = search_forest_iterator(self.roots(),
                                      self.children,
                                      algorithm = self._algorithm)
        if hasattr(self, "post_process"):
            iter = imap_and_filter_none(self.post_process, iter)
        return iter
Ejemplo n.º 10
0
    def __iter__(self):
        r"""
        Return an iterator over the elements of ``self``.

        EXAMPLES::

            sage: from sage.combinat.backtrack import SearchForest
            sage: def children(l):
            ....:      return [l+[0], l+[1]]
            ....:
            sage: C = SearchForest(([],), children)
            sage: f = C.__iter__()
            sage: next(f)
            []
            sage: next(f)
            [0]
            sage: next(f)
            [0, 0]
        """
        iter = search_forest_iterator(self.roots(),
                                      self.children,
                                      algorithm=self._algorithm)
        if hasattr(self, "post_process"):
            iter = imap_and_filter_none(self.post_process, iter)
        return iter
Ejemplo n.º 11
0
    def breadth_first_search_iterator(self):
        r"""
        Returns a breadth first search iterator over the elements of ``self``

        EXAMPLES::

            sage: f = SearchForest([[]],
            ...                    lambda l: [l+[0], l+[1]] if len(l) < 3 else [])
            sage: list(f.breadth_first_search_iterator())
            [[], [0], [1], [0, 0], [0, 1], [1, 0], [1, 1], [0, 0, 0], [0, 0, 1], [0, 1, 0], [0, 1, 1], [1, 0, 0], [1, 0, 1], [1, 1, 0], [1, 1, 1]]
            sage: S = SearchForest([(0,0)],
            ...   lambda x : [(x[0], x[1]+1)] if x[1] != 0 else [(x[0]+1,0), (x[0],1)],
            ...   post_process = lambda x: x if ((is_prime(x[0]) and is_prime(x[1])) and ((x[0] - x[1]) == 2)) else None)
            sage: p = S.breadth_first_search_iterator()
            sage: [p.next(), p.next(), p.next(), p.next(), p.next(), p.next(), p.next()]
            [(5, 3), (7, 5), (13, 11), (19, 17), (31, 29), (43, 41), (61, 59)]
        """
        iter = search_forest_iterator(self.roots(), self.children, algorithm='breadth')
        if hasattr(self, "post_process"):
            iter = imap_and_filter_none(self.post_process, iter)
        return iter