Are these braids equal? If you are not familiar with Braid-Theory I recommend that you read this first.  This question assumes that you are at least familiar with the concepts at hand and assumes you are well familiar with group-theory


Let us define σn to be the braid in which the nth strand (One indexed) from the top crosses over the n+1th strand, and σn- to be the inverse of σn (That is the n+1th strand crosses the nth strand).

The braid group Bn is then generated by &lt;σ1,σ2,σ3, . . . , σn-1>.  Thus every braid on Bn can be written as the product of the σ-braids.1



Determining if two braids on a group are equal is not a simple task.  It may be pretty obvious that σ1σ3 = σ3σ1, but it is a little less obvious that for example σ2σ1σ2 = σ1σ2σ1.2

So the question is "How can we determine if two braids are the same?".  Well the two examples above each represent a bit of this.  In general the following relations, called Artin's relations, are true:


σiσj = σjσi; i - j > 1
σiσi+1σi = σi+1σiσi+1


We can use these two relations in conjunction with the group axioms to prove that any equal braids are equal.  Thus two braids are equal iff the repeated application of these relations and the group axioms can demonstrate so.

Task

You will write a program or function to take two braids and determine whether or not they are equal.  You may also optionally take a positive integer representing the order of the group.

This is a code-golf question so answers will be scored in bytes, with less bytes being better.

Input and Output

You should represent a Braid as an ordered list of generators, (or any equivalent structure, e.g. vector).  You may represent the generators in any reasonable form (e.g. an integer, a two tuple of a positive integer and a boolean).

On par with standard descision-problem rules you should output one of two distinct values, an accept an reject.

Test Cases

[],       []              -&gt; True
[1,-1],   []              -&gt; True
[1,2,1],  [2,1,2]         -&gt; True
[1,3],    [3,1]           -&gt; True
[1,3,2,1],[3,2,1,2]       -&gt; True
[1,4,-4,3,2,1], [3,2,1,2] -&gt; True
[2,2,1],  [2,1,2]         -&gt; False
[1,2,-1], [-1,2,1]        -&gt; False
[1,1,1,2],[1,1,2]         -&gt; False




1:  Note that while Bn satisfies all the properties of a group the operation on our braid group is not commutative, and thus our group is not abelian.

2: If you would like to verify this for yourself I suggest applying σ1- to both sides, If you draw the two out on paper, or model them with actual strings it should become apparent why this is the case.
Haskell, 190 bytes



i!j|j&lt;0=reverse$map(0-)$i!(-j)|i==j=[i,i+1,-i]|i+1==j=[i]|i+j==0=[j+1]|i+j==1=[-j,-i,j]
_!j=[j]
j%(k:a)|j+k==0=a
j%a=j:a
i&amp;a=foldr(%)[]$foldr((=&lt;&lt;).(!))[i]a
a?n=map(&amp;a)[1..n]
(a#b)n=a?n==b?n


Try it online!

How it works

Let Fn be the free group on n generators x1, …, xn.  One of the first results in braid theory (Emil Artin, Theorie der Zöpfe, 1925) is that we have an injective homomorphism f : Bn → Aut(Fn) where the action fσi of σi is defined by

fσi(xi) = xixi + 1xi−1,
fσi(xi + 1) = xi,
fσi(xj) = xj for j ∉ {i, i + 1}.

The inverse fσi−1 is given by

fσi−1(xi) = xi + 1,
fσi−1(xi + 1) = xi + 1−1xixi + 1,
fσi−1(xj) = xj for j ∉ {i, i + 1}

and of course composition is given by fab = fa ∘ fb.

To test whether a = b ∈ Bn, it suffices to test that fa(xi) = fb(xi) for all i = 1, …, n.  This is a much simpler problem in Fn, where we only need to know how to cancel xi with xi−1.

In the code:


i!j computes fσi(xj) (where either i or j may be negative, representing an inverse),
foldr(%)[] performs reduction in the free group,
i&amp;a computes fa(xi),
a?n computes [fa(x1), …, fa(xn)],
and (a#b)n is the equality test for a = b in Bn.