Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sagemathgh-38064: Steinhaus-Johnson-Trotter algorithm for permutations
<!-- ^ Please provide a concise and informative title. --> <!-- ^ Don't put issue numbers in the title, do this in the PR description below. --> <!-- ^ For example, instead of "Fixes sagemath#12345" use "Introduce new method to calculate 1 + 2". --> <!-- v Describe your changes below in detail. --> <!-- v Why is this change required? What problem does it solve? --> <!-- v If this PR resolves an open issue, please link to it here. For example, "Fixes sagemath#12345". --> I suggest this diff against develop to implement the Steinhaus-Johnson- Trotter algorithm that generates the permutations of a list using only transpositions of two elements of the list. The algorithm can be selected upon initialization with the parameter of the same name, defaults to "lex" which is the current algorithm. Since the `Permutation` class is a bit weird and creates a new list and a new object on every iteration, I had to tweak a bit the parameters in input to the class `__init__` method adding - `algorithm`: this one is fine, it allows to choose which algorithm to use to generate the permutations - `directions`: this one is meant to keep track of the internal state specific to the SJT algorithm. To find the two elements to transpose for the next permutation, we need to find elements according to some conditions regarding each of their direction. Since the `Permutation` class creates a new object at each iteration, I tried to find the best way to pass on the internal state to the new object. Maybe the internal state can be directly computed from the list, I haven't checked. Because of this dependency on the internal state, only the identity permutation can be used when initializing a `Permutation` object with `algorithm='sjt'` (see examples from doc). I am aware that adding an extra optional parameter to the object construction might not be the best option thus I will gladly take your feedback and opinions. I haven't implemented the `prev()` method. I think it should be done if this is accepted. ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. - [ ] I have linked a relevant issue or discussion. - [x] I have created tests covering the changes. - [x] I have updated the documentation and checked the documentation preview. ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on. For example, --> <!-- - sagemath#12345: short description why this is a dependency --> <!-- - sagemath#34567: ... --> URL: sagemath#38064 Reported by: grnx Reviewer(s): grhkm21, grnx
- Loading branch information