11# -*- coding: utf-8 -*-
22r """
3- Recursively enumerated set
3+ Recursively Enumerated Sets
44
55A set `S` is called recursively enumerable if there is an algorithm that
66enumerates the members of `S`. We consider here the recursively enumerated
@@ -9,11 +9,11 @@ sets that are described by some ``seeds`` and a successor function
99graded, forest) or not. The elements of a set having a symmetric, graded or
1010forest structure can be enumerated uniquely without keeping all of them in
1111memory. Many kinds of iterators are provided in this module: depth first
12- search, breadth first search or elements of given depth.
12+ search, breadth first search and elements of given depth.
1313
1414See :wikipedia:`Recursively_enumerable_set`.
1515
16- See documentation of :func:`RecursivelyEnumeratedSet` below for the
16+ See the documentation of :func:`RecursivelyEnumeratedSet` below for the
1717description of the inputs.
1818
1919AUTHORS:
@@ -22,12 +22,11 @@ AUTHORS:
2222
2323EXAMPLES:
2424
25- No hypothesis on the structure
26- ------------------------------
25+ .. RUBRIC:: No hypothesis on the structure
2726
2827What we mean by "no hypothesis" is that the set is not known
2928to be a forest, symmetric, or graded. However, it may have other
30- structure, like not containing an oriented cycle, that does not
29+ structure, such as not containing an oriented cycle, that does not
3130help with the enumeration.
3231
3332In this example, the seed is 0 and the successor function is either ``+ 2``
@@ -50,8 +49,7 @@ Depth first search::
5049 sage: [next(it) for _ in range(10) ]
5150 [0, 3, 6, 9, 12, 15, 18, 21, 24, 27 ]
5251
53- Symmetric structure
54- -------------------
52+ .. RUBRIC:: Symmetric structure
5553
5654The origin ``( 0, 0) `` as seed and the upper, lower, left and right lattice
5755point as successor function. This function is symmetric since `p` is a
@@ -86,8 +84,7 @@ Levels (elements of given depth)::
8684 sage: sorted( C. graded_component( 2))
8785 [(-2, 0), (-1, -1), (-1, 1), (0, -2), (0, 2), (1, -1), (1, 1), (2, 0) ]
8886
89- Graded structure
90- ----------------
87+ .. RUBRIC:: Graded structure
9188
9289Identity permutation as seed and ``permutohedron_succ`` as successor
9390function::
@@ -137,11 +134,10 @@ Graded components (set of elements of the same depth)::
137134 sage: sorted( R. graded_component( 10))
138135 [[5, 4, 3, 2, 1 ]]
139136
140- Forest structure
141- ----------------
137+ .. RUBRIC:: Forest structure ( Example 1)
142138
143139The set of words over the alphabet `\{ a,b\} ` can be generated from the
144- empty word by appending letter `a` or `b` as a successor function. This set
140+ empty word by appending the letter `a` or `b` as a successor function. This set
145141has a forest structure::
146142
147143 sage: seeds = ['' ]
@@ -162,9 +158,6 @@ Breadth first search iterator::
162158 sage: [next(it) for _ in range(6) ]
163159 ['', 'a', 'b', 'aa', 'ab', 'ba' ]
164160
165- Example: Forest structure
166- -------------------------
167-
168161This example was provided by Florent Hivert.
169162
170163How to define a set using those classes?
@@ -175,41 +168,23 @@ classes being very similar):
175168
176169.. MATH::
177170
178- \b egin{picture}( -300,0) ( 600,0)
179- % R oot
180- \p ut( 0,0) {\c ircle*{7} }
181- \p ut( 0,10) {\m akebox( 0,10) {``\ ''}}
182- % F irst Children
183- \p ut( -150,-60) {\m akebox( 0,10) {``a''}}
184- \p ut( 0,-60) {\m akebox( 0,10) {``b''}}
185- \p ut( 150,-60) {\m akebox( 0,10) {``c''}}
186- \m ultiput( -150,-70) ( 150,0) {3} {\c ircle*{7} }
187- % S econd children
188- \p ut( -200,-130) {\m akebox( 0,10) {``aa''}}
189- \p ut( -150,-130) {\m akebox( 0,10) {``ab''}}
190- \p ut( -100,-130) {\m akebox( 0,10) {``ac''}}
191- \p ut( -50,-130) {\m akebox( 0,10) {``ba''}}
192- \p ut( 0,-130) {\m akebox( 0,10) {``bb''}}
193- \p ut( 50,-130) {\m akebox( 0,10) {``bc''}}
194- \p ut( 100,-130) {\m akebox( 0,10) {``ca''}}
195- \p ut( 150,-130) {\m akebox( 0,10) {``cb''}}
196- \p ut( 200,-130) {\m akebox( 0,10) {``cc''}}
197- \m ultiput( -200,-140) ( 50,0) {9} {\c ircle*{7} }
198- % Le gend
199- \p ut( 100,-5) {\m akebox( 0,10) [l ]{1) An initial element}}
200- \p ut( -250,-5) {\m akebox( 0,10) [l ]{2) A function of an element enumerating}}
201- \p ut( -235,-20) {\m akebox( 0,10) [l ]{its children ( if any) }}
202- % A rrows
203- \t hicklines
204- \p ut( 0,-10) {\v ector( 0,-1) {30} }
205- \p ut( -15,-5) {\v ector( -2,-1) {110} }
206- \p ut( 15,-5) {\v ector( 2,-1) {110} }
207- \m ultiput( -150,-80) ( 150,0) {3} {\v ector( 0,-1) {30} }
208- \m ultiput( -160,-80) ( 150,0) {3} {\v ector( -1,-1) {30} }
209- \m ultiput( -140,-80) ( 150,0) {3} {\v ector( 1,-1) {30} }
210- \p ut( 90,0) {\v ector( -1,0) {70} }
211- \p ut( -215,-30) {\v ector( 1,-1) {40} }
212- \e nd{picture}
171+ \b egin{array}{ccc}
172+ & \e mptyset \\
173+ \h fil\s warrow & \d ownarrow & \s earrow\h fil\\
174+ a & b & c \\
175+ \b egin{array}{ccc}
176+ \s warrow & \d ownarrow & \s earrow \\
177+ aa & ab & ac \\
178+ \e nd{array} &
179+ \b egin{array}{ccc}
180+ \s warrow & \d ownarrow & \s earrow \\
181+ ba & bb & bc \\
182+ \e nd{array} &
183+ \b egin{array}{ccc}
184+ \s warrow & \d ownarrow & \s earrow \\
185+ ca & cb & cc \\
186+ \e nd{array}
187+ \e nd{array}
213188
214189For the previous example, the two necessary pieces of information are:
215190
@@ -219,7 +194,7 @@ For the previous example, the two necessary pieces of information are:
219194
220195 lambda x: [x + letter for letter in ['a', 'b', 'c' ]
221196
222- This would actually describe an ** infinite** set, as such rules describes
197+ This would actually describe an ** infinite** set, as such rules describe
223198"all words" on 3 letters. Hence, it is a good idea to replace the function by::
224199
225200 lambda x: [x + letter for letter in ['a', 'b', 'c' ]] if len( x) < 2 else []
@@ -249,14 +224,13 @@ or::
249224 sage: S. list( )
250225 ['', 'a', 'aa', 'ab', 'ac', 'b', 'ba', 'bb', 'bc', 'c', 'ca', 'cb', 'cc' ]
251226
252- Example: Forest structure 2
253- ---------------------------
227+ .. RUBRIC:: Forest structure ( Example 2)
254228
255229This example was provided by Florent Hivert.
256230
257231Here is a little more involved example. We want to iterate through all
258232permutations of a given set `S`. One solution is to take elements of `S` one
259- by one an insert them at every positions . So a node of the generating tree
233+ by one and insert them at every position . So a node of the generating tree
260234contains two pieces of information:
261235
262236- the list ``lst`` of already inserted element;
@@ -320,7 +294,7 @@ def RecursivelyEnumeratedSet(seeds, successors, structure=None,
320294
321295 A set `S` is called recursively enumerable if there is an algorithm that
322296 enumerates the members of `S`. We consider here the recursively
323- enumerated set that are described by some ``seeds`` and a successor
297+ enumerated sets that are described by some ``seeds`` and a successor
324298 function ``successors``.
325299
326300 Let `U` be a set and ``successors`` `:U \t o 2^ U` be a successor function
@@ -406,7 +380,7 @@ def RecursivelyEnumeratedSet(seeds, successors, structure=None,
406380
407381 .. WARNING::
408382
409- If you do not set the good structure, you might obtain bad results,
383+ If you do not set a good structure, you might obtain bad results,
410384 like elements generated twice::
411385
412386 sage: f = lambda a: [a-1,a+1 ]
@@ -781,8 +755,8 @@ cdef class RecursivelyEnumeratedSet_generic(Parent):
781755 r """
782756 Iterate over the elements of ``self`` of given depth.
783757
784- An element of depth `n` can be obtained applying `n` times the
785- successor function to a seed.
758+ An element of depth `n` can be obtained by applying the
759+ successor function `n` times to a seed.
786760
787761 INPUT:
788762
@@ -847,7 +821,7 @@ cdef class RecursivelyEnumeratedSet_generic(Parent):
847821 r """
848822 Iterate on the elements of ``self`` ( breadth first) .
849823
850- This code remembers every elements generated and uses python
824+ This code remembers every element generated and uses python
851825 queues. It is 3 times slower than the other one.
852826
853827 See :wikipedia:`Breadth-first_search`.
@@ -876,7 +850,7 @@ cdef class RecursivelyEnumeratedSet_generic(Parent):
876850 r """
877851 Iterate on the elements of ``self`` ( in no particular order) .
878852
879- This code remembers every elements generated.
853+ This code remembers every element generated.
880854
881855 TESTS:
882856
@@ -905,7 +879,7 @@ cdef class RecursivelyEnumeratedSet_generic(Parent):
905879 r """
906880 Iterate on the elements of ``self`` ( depth first) .
907881
908- This code remembers every elements generated.
882+ This code remembers every element generated.
909883
910884 The elements are traversed right-to-left, so the last element returned
911885 by the successor function is visited first.
@@ -1552,7 +1526,7 @@ def search_forest_iterator(roots, children, algorithm='depth'):
15521526 [0, 0, 0 ], [0, 0, 1 ], [0, 1, 0 ], [0, 1, 1 ],
15531527 [1, 0, 0 ], [1, 0, 1 ], [1, 1, 0 ], [1, 1, 1 ]]
15541528
1555- This allows for iterating trough trees of infinite depth::
1529+ This allows for iterating through trees of infinite depth::
15561530
15571531 sage: it = search_forest_iterator( [[ ]], lambda l: [l+[0 ], l+ [1 ]], algorithm='breadth')
15581532 sage: [ next(it) for i in range(16) ]
@@ -1574,9 +1548,9 @@ def search_forest_iterator(roots, children, algorithm='depth'):
15741548 [0, 1, 2 ], [0, 2, 1 ], [1, 0, 2 ], [1, 2, 0 ], [2, 0, 1 ], [2, 1, 0 ]]
15751549 """
15761550 # Little trick: the same implementation handles both depth and
1577- # breadth first search. Setting position to -1 makes a depth search
1551+ # breadth first search. Setting position to -1 results in a depth search
15781552 # (you ask the children for the last node you met). Setting
1579- # position on 0 makes a breadth search (enumerate all the
1553+ # position on 0 results in a breadth search (enumerate all the
15801554 # descendants of a node before going on to the next father)
15811555 if algorithm == ' depth' :
15821556 position = - 1
@@ -1920,8 +1894,8 @@ class RecursivelyEnumeratedSet_forest(Parent):
19201894 def _elements_of_depth_iterator_rec (self , depth = 0 ):
19211895 r """
19221896 Return an iterator over the elements of ``self`` of given depth.
1923- An element of depth `n` can be obtained applying `n` times the
1924- children function from a root. This function is not affected
1897+ An element of depth `n` can be obtained by applying the
1898+ children function `n` times from a root. This function is not affected
19251899 by post processing.
19261900
19271901 EXAMPLES::
@@ -1951,8 +1925,8 @@ class RecursivelyEnumeratedSet_forest(Parent):
19511925 def elements_of_depth_iterator (self , depth = 0 ):
19521926 r """
19531927 Return an iterator over the elements of ``self`` of given depth.
1954- An element of depth `n` can be obtained applying `n` times the
1955- children function from a root.
1928+ An element of depth `n` can be obtained by applying the
1929+ children function `n` times from a root.
19561930
19571931 EXAMPLES::
19581932
@@ -2010,7 +1984,7 @@ class RecursivelyEnumeratedSet_forest(Parent):
20101984 depth first search and breadth first search failed. The
20111985 following example enumerates all ordered pairs of nonnegative
20121986 integers, starting from an infinite set of roots, where each
2013- roots has an infinite number of children::
1987+ root has an infinite number of children::
20141988
20151989 sage: from sage. sets. recursively_enumerated_set import RecursivelyEnumeratedSet_forest
20161990 sage: S = RecursivelyEnumeratedSet_forest( Family( NN, lambda x : ( x, 0)) ,
0 commit comments