I’m trying to take a product of a lot of transpositions, and boil it all the way down to a smaller variety of merchandise. I’ve the next code, and would really like some enter on environment friendly methods to boil these merchandise down. Earlier than I give the code, it is very important acknowledge the next:
Transpositions, a cyclic permutation of two parts, have the next properties:
- The product of similar transpositions cancel. For instance, (1,2)(1,2)(2,3) = (2,3).
- Any product of the shape (x,y)(y,z)(x,y) is the same as (y,z)(x,y)(y,z).
- Any transpositions which share no parts commute. That’s, (u,v)(x,y)=(x,y)(u,v).
The code I’ve is under. It is a process which will get an enter of a listing that comprises numerous 2-tuples which symbolize transpositions. For instance, legitimate enter is [(1,2)(2,3)(1,2)]. I’ve a small rationalization on the backside of the given code.
depend = 1 whereas depend != 0: depend = 0 repeatCount = 0 swappedCount = 0 for i in vary(len(route)-1): if len(route) > 1 and repeatCount == 0: if route[i] == route[i+1]: route.pop(i+1) route.pop(i) depend += 1 repeatCount += 1 for i in vary(len(route)-3): if len(route) > Three and swappedCount == 0: if route[i] == route[i+2] and route[i+1] == route[i+3]: route.pop(i+3) route.pop(i+2) route.insert(i,route[i+1]) route.pop(i+2) depend += 1 swappedCount += 1
The primary for loop searches of cases of property (1) above, and the second for loop seek for cases of property (2). The counters repeatCounter and swappedCounter are my gross try to keep away from an IndexError.
Any approach to accomplish this utterly, i.e. all the time be capable of cut back any product to absolutely the minimal, in a well timed approach can be enormously appreciated, as run time is an issue for the remainder of the entire program.
For clarification, I’ll clarify a number of the context through which this drawback seems. Think about a graph G with labeled vertices. On every vertex lies a pebble, p-i. A transposition within the given product corresponds to choosing an edge in G and swapping the pebbles which can be incident to that edge.
Due to this fact, a product of transpositions corresponds to a strategy of exchanging pebbles of edges, and the tip result’s a permutation of the pebbles on G which is described by the permutation given by the product of transpositions.
Therefore, this strategy of boiling down a product of transpositions is an try to cut back the variety of exchanges wanted to realize any permutation of pebbles on a graph G.