0

What is a good Python program to calculate the composition( from right to left) of cycle permutations? I know how to calculate the answer, but I don't know the algorithm for a Python program.
For example; '(1,6,5,3)(1,4,2,3)' has the solution '(1,4,2)(3,6,5)'. Because 1 - 4 - 4, 4 - 2 - 2, 2 - 3 - 1 and 3 - 1 - 6, 6 - 6 - 5, 5 - 5 - 3
On the internet I couldn't find where to begin or what to do. Can someone please help me?

twister
  • 3
  • 2
  • Why are you using a $? –  Jun 10 '21 at 13:06
  • My misstake, I have changed it! – twister Jun 10 '21 at 13:08
  • 2
    I cannot understand what you want. Please elaborate on it a little. –  Jun 10 '21 at 13:09
  • I want to have a Python program. For example def cycle(input): ...(here the desired program) Where if the input is '(1,6,5,3)(1,4,2,3)' i get '(1,4,2)(3,6,5). This because of composition of cycle permutations – twister Jun 10 '21 at 13:16
  • Can you provide which algorithm you are following for the permutations –  Jun 10 '21 at 13:17
  • You have to start with the right tuple, in this example '(1,4,2,3)'. You take the first element, so that's 1. Then you look at the right element, that's 4. If 4 is in the tuple left next to it ('(1,6,5,3)'), it wil take the number right next to 4. In this example that is not the case so the element is going to stay 4. Then you look at 2 (the same case). Then you look at the right element of 2, that's 3, and this one is in the left tuple. Here the right element of 3 is 1, so it gets 1 and the cycle is complete ('(1,4,2)'), because the first element was 1. Now you look at 3 and do the same. – twister Jun 10 '21 at 13:28

1 Answers1

0

The Sympy package handles cycle permutations nicely. Your method of writing permutations is called "Disjoint Cycle Notation". Here's an example using your cycles:

from sympy.combinatorics.permutations import Permutation

a = Permutation([[1, 6, 5, 3]])  
b = Permutation([[1, 4, 2, 3]])

new_perm = b * a

This gives output (142)(365) for new_perm.

For any of these cycles, you can call them like a function. For example, we can input 1 to new_perm and would expect 4 as an output:

> new_perm(1)
4

Edit

The Sympy permutations can be used as the building blocks for a function which composes cycle permutations together. The original question asked for a string input and output. Here is one example (you may have to modify based on your string input):

import re
import functools

def compose_cycles(input_string):
    # Split the cycles by regex
    cycles = re.findall("\(([\d,]+)\)", input_string)

    # Break each cycle into a list of integers
    cycles = [list(map(int, x.split(","))) for x in cycles]

    # Make each cycle into a Sympy Permutation
    cycles = [Permutation([x]) for x in cycles]

    composition = functools.reduce(lambda x, y: y * x, cycles)

    return str(composition)

compose_cycles('(1,6,5,3)(1,4,2,3)')

The last line of the functions calls str which returns the string representation (not the original permutation). Our output is '(1 4 2)(3 6 5)'

SNygard
  • 916
  • 1
  • 9
  • 21
  • Thankyou for you answer! Do you maybe know how you can get the string '(1,4,2)(3,6,5)' instead of (142)(365). And do you maybe know a definition program(instead of the sympy package). I would like to have my program like: def cycle(input): ....(here the program)...... return output. With input the string '(1,6,5,3)(1,4,2,3)' and output '(1,4,2)(3,6,5)' @SNygard – twister Jun 10 '21 at 17:33
  • The basic problem is the same, with some string handling at the beginning to build the initial Permutation objects. I've updated the answer to show one example. – SNygard Jun 10 '21 at 18:56
  • This helps a lot! Is there also a Program without using the sympy permutation? @SNygard – twister Jun 10 '21 at 22:20