Start with an array of integers so that the sum of the values is some positive integer S. The following routine always terminates in the same number of steps with the same results. Why is this?
Start with an array x = [x_0, x_1, ..., x_N-1] such that all x_i‘s are integers. While there is a negative entry, do the following:
-
Choose any index
isuch thatx_i < 0. -
Add
x_i(a negative number) tox_(i-1 % N). -
Add
x_i(a negative number) tox_(i+1 % N). -
Replace
x_iwith-x_i(a positive number).
This process maintains the property that x_0 + x_1 + ... + x_N-1 = S. For any given starting array x, no matter which index is chosen at any step, the number of times one goes through these steps is the same as is the resulting vector. It is not even obvious (to me, at least) that this process terminates in finite time, let alone has this nice invariant property.
EXAMPLE:
Take x = [4 , -1, -2] and flipping x_1 to start, the result is
[4, -1, -2]
[3, 1, -3]
[0, -2, 3]
[-2, 2, 1]
[2, 0, -1]
[1, -1, 1]
[0, 1, 0]
On the other hand, flipping x_2 to start gives
[4, -1, -2]
[2, -3, 2]
[-1, 3, -1]
[1, 2, -2]
[-1, 0, 2]
[1, -1, 1]
[0, 1, 0]
and the final way give this solution with arrays reversed from the third on down if you choose x_2 instead of x_0 to flip at the third array. In all cases, 6 steps lead to [0,1,0].
I have an argument for why this is true, but it seems to me to be overly complicated (it has to do with Coxeter groups). Does anyone have a more direct way to think about why this happens? Even finding a reason why this should terminate would be great.
Bonus points to anyone who finds a way to determine the number of steps for a given array (without going through the process).
I think the easiest way to see why the output vector and the number of steps are the same no matter what index you choose at each step is to look at the problem as a bunch of matrix and vector multiplications.
For the case where
xhas 3 components, think ofxas a 3×1 vector:x = [x_0 x_1 x_2]'(where'is the transpose operation). Each iteration of the loop will choose to flip one ofx_0,x_1,x_2, and the operation it performs onxis identical to multiplication by one of the following matrices:where multiplication by
s_0is the operation performed if the indexi=0,s_1corresponds toi=1, ands_2corresponds toi=2. With this view, you can interpret the algorithm as multiplying the correspondings_imatrix byxat each iteration. So in the first example wherex_1is flipped at the start, the algorithm computes:s_1*s_2*s_0*s_1*s_2*s_1[4 -1 -2]' = [0 1 0]'The fact that the index you choose doesn’t affect the final output vector arises from two interesting properties of the
smatrices. First,s_i*s_(i-1)*s_i = s_(i-1)*s_i*s(i-1), wherei-1is computed modulon, the number of matrices. This property is the only one needed to see why you get the same result in the examples with 3 elements:s_1*s_2*s_0*s_1*s_2*s_1 = s_1*s_2*s_0*(s_1*s_2*s_1) = s_1*s_2*s_0*(s_2*s_1*s_2), which corresponds to choosingx_2at the start, and lastly:s_1*s_2*s_0*s_2*s_1*s_2 = s_1*(s_2*s_0*s_2)*s_1*s_2 = s_1*(s_0*s_2*s_0)*s1*s2, which corresponds to choosing to flipx_2at the start, but then choosing to flipx_0in the third iteration.The second property only applies when
xhas 4 or more elements. It iss_i*s_k = s_k*s_iwheneverk <= i-2wherei-2is again computed modulon. This property is apparent when you consider the form of matrices whenxhas 4 elements:The second property essentially says that you can exchange the order in which non-conflicting flips occur. For example, in a 4 element vector, if you first flipped
x_1and then flippedx_3, this has the same effect as first flippingx_3and then flippingx_1.