# How to find an algorithm to shut down all lamps with minimum number of moves?

Is there a way to solve the following optimization problem given in https://www.ohjelmointiputka.net/postit/tehtava.php?tunnus=muslam :

This is the follow up of What would be an algorithm to shut down all lamps?

I have $n$ lamps in a circle, and enumerated by $L_1,\ldots,L_n$. Some of them are switched on and some of them are switched off. I also have been given a positive integer $m$. One every turn I choose one lamp $L_i$ and then the lamps $L_{i−m},\ldots,L_{i+m}$ will change their state, I mean if lamp $L_j$ was turned off then now it is turned on and vice versa. Indexes are modulo $n$ so the lamps $L_{n-1},L_n,L_1,L_2$ are consecutive.

What kind of algorithm will find the minimum number of turns to shut off all lamps and which switches one must press, if the initial states of the lamps are given in a reasonable time?

I found that if I do a simple linear equation system over $\mathbb F_2$, it won't give always an optimal solution. I also saw the discussion on https://ask.sagemath.org/question/39335/how-to-find-a-minimum-number-of-switch-presses-to-shut-down-all-lamps/ but it looks like the code written by dan_fulea has a bug or he uses some other version of Sage that the code won't run on my computer.

The test cases are given as follows:

label,  n.o. lamps, how many lamps        original lamp states
a switch affects
per direction
================================================================================
B       6               1               101101
--------------------------------------------------------------------------------
C       10              2               1011010110
--------------------------------------------------------------------------------
D       20              1               11111011101010111111
--------------------------------------------------------------------------------
E       30              7               011100001010011011100001010011
--------------------------------------------------------------------------------
F       39              6               110100111111101000011000100110111100010
--------------------------------------------------------------------------------
G       53              9               0101100101111100100011100111101001001010
0010000010110
--------------------------------------------------------------------------------
H       120             7               1010110110000100000101011001011111010111
1010011101001100000010001010011010110000
0000100110010100010010110111000000010110
--------------------------------------------------------------------------------
I       220             27              1110111111101000100110011001100110100000
0010100011000111101100111111000001010000
1010110110011100100010011011010111100011
0101101000010000100110111101001001011010
1101001001110110001100011010111101001100
11010111110101010100
--------------------------------------------------------------------------------
J       500             87              1010001101101001110001101001000101010100
0001111111001101011000000011001111111011
1001110011010111111011010100010011011001
1001101110011011100001000111110101011111
1100111100001100110011101110101100001111
1100010010011010001111000000101110101101
1010100001100011111000111001000101101000
1011111111101111000000011111010001000000
1110011110111101010010011000000100010100
0011101011010011010110011110111000010010
0111100100011010010110001000011100101001
1110111010001001011001111011111011010110
10101101111011101110
--------------------------------------------------------------------------------
K       1002            83              0010100100100101000000110101111111101011
1101000101111110001110000110110110010101
1110110011011101100110111001110110010011
1101111010110011110101100001101010100011
1110001100011111110100011110100111111100
0011001011100110101100001101000001110010
0110100000100100100000011010000010111100
1110001110011110101001100111101101010000
0101010000011010011110101001001001000000
0011000100011011011001111010001101111000
0100001011010011001010111001111100110001
0011111110101101001100111101110000000000
1101100100000011000010010100010101001000
1100001000101001100110010100001000001101
1101000100001010011000101001101000100010
0011010001011101010100011101001101101100
0111110100110011001111000000001001001001
1001111001011111000010110000110010101000
1011001100111101000101000110000111010100
0010011011010111001101011001111000001011
1110101010101101111011111110100001100110
1000101100110011010000110000011011110011
0010000010000000111101101000001111101111
0100111110010101100011101001111101010000
1111100010011001110111111000101000000101
01
--------------------------------------------------------------------------------
L       2107            108             0111110100011000011111101110010101100011
1001111011101001001110111110001100011001
1001010100101011101101001000010111111111
1001101010111011110100100101000101100011
1110100010010010101110100000111100101000
0111101011111100010010110000100110100100
0100110101110010110011110010101101100111
1110010011000110110111010110010100101110
0111111101110000111001111100100010010001
1010110011000101100111111001011110101110
0111010110111110110101000101100100011000
1011000011011110001111100110100010100101
1101111100110011001110010010001010101111
1000001001000110011110010011011101110100
1011111100110010011000010110010110101010
0110101000011011110001010000010001000110
1001110101001001110110111111010011010111
1111011001000110111001000011101101110001
0000011111101000010101011111011011000011
1111000000011100010011011001011000110101
1101011111100001100010110010110011000000
0001001111100101110100100011011010011100
0000001111010101000111011000110110100001
1010110011100110111010111110110000010000
1000101001111001000110000101010000010111
1011100001000110001100010000001011101110
1001111110100010010000011000100101010101
1001001001110110101000001001001100001011
0011011100011111100111001110101101110001
0111010000010011110110011011000011101001
1111011010010000101111000010000001100110
1001011101001000010101001001011111111011
1000111000100001101100101110100011111100
1011001111101111110110101111101111011111
1001111100110101110101111110010010101101
1111111111000100100111100011101110110100
0100011011001010110100101101000000110010
0010010001001110110100011111100011111101
0100110111101101010101010100110110011011
0001111111000100000111011010101011000010
0011011110110110110100011001101111001000
1000000011110011100111100000001010010011
1000011101111100000101010101010010100101
1010001011010100011011001110110010100000
1000111101111000010111111101010110110111
0110001111100011001110000100100101001111
0000111111100010011001010000010110111000
1000110110001000001100110000001011000010
1000101101110000101100100010101111100011
1000010010111101000010000110011010000001
0010001100001000001100110111110100100111
1001100110001000100101011111001011001111
110001011111001101010101001
================================================================================

I will post my collection of observations. A bit more work is needed to find the optimal solution (in those cases where several solutions exist).

My solution.

As you pointed out the question is about the set of solutions to a linear system of $n$ equations in $n$ variables ranging over $\Bbb{F}_2$. Let us begin by analyzing the matrix $A$ of coefficients of that system. In order to make a few things a bit simpler I shift the problem in such a way that calling out lamp number $i$ toggles the status of lamps $i,i+1,\ldots,i+2m$. Reverting to the behavior in the problem description is then straight forward.

So let $A$ be the $n\times n$ matrix with the first column beginning with $2m+1$ $1$s followed by $n-(2m+1)$ $0$s. We then get the other columns by always cyclically shifting the preceding column downwards. The resulting matrix is a circulant matrix with the associated polynomial $$f(x)=1+x+x^2+\cdots+x^{2m}.$$ Let us momentarily view $A$ as a complex matrix. The eigenvalues of $A$ are then equal to $\lambda_j=f(\zeta^j)$, $j=0,1,\ldots,n-1$, where $\zeta=e^{2\pi i/n}$ is a primitive $n$th root of unity. By the geometric sum formula $$\lambda_j=f(\zeta^j)= \begin{cases} 2m+1,&\text{if j=0, and}\\ \frac{1-\zeta^{j(2m+1)}}{1-\zeta^j},&\text{otherwise.} \end{cases}$$

Observation 1. $\det A=2m+1$ if $\gcd(2m+1,n)=1$ and $\det A=0$ otherwise.

Proof. The determinant is the product of the eigenvalues, so $$\det A=\prod_{j=0}^{n-1}\lambda_j=(2m+1)\prod_{j=1}^{n-1}\frac{1-\zeta^{(2m+1)j}}{1-\zeta^j}.$$ Assume first that $\gcd(2m+1,n)=1$. Then the collection of $n-1$ numerators in the last form is a permuted version of the denominators. Therefore that complicated looking product $=1$ giving us the claim. In the other case $\gcd(2m+1,n)=d>1$. Here whenever $j$ is a multiple of $n/d$ the numerator $1-\zeta^{j(2m+1)}$ is equal to zero. So we see that zero is an eigenvalue with multiplicity $d-1>0$ and the determinant vanishes.

A key corollary for us is the following

Observation 2. If $\gcd(2m+1,n)=1$ then $det A\equiv1\pmod2$. Consequently the linear system has a unique solution to each initial configuration of lamps. This must then also be the optimal solution.

Proof. In this case $\det A$ is not divisible by two. Hence it is invertible over $\Bbb{F}_2$ as well, and the claim follows from basic linear algebra.

Observation 3. If $\gcd(2m+1,n)>1$ then $\det A=0$. Consequently there exist some initial configurations of lamps that cannot all be switched off. But, also, there are some initial configurations allowing multiple solution, and in those case we have a genuine optimization question of figuring out the minimal set of lamps called out.

Again by linear algebra, two solutions (when more than one exists) differ from each other by a vector in the kernel of $A$, call it $V$. That is, by a a sequence of moves that return all the lamps to their original status.

I conclude (at least for now) with the final observation.

Observation 4. Assume that $d=\gcd(2m+1,n)>1$. A vector $(x_1,x_2,\ldots,x_n)\in\Bbb{F}_2^n$ is in the kernel of $A$ if and only if

• there an even number of $1$ among the $d$ first coordinates $x_1,x_2,\ldots,x_d$, and
• the vector is periodic with period $d$, i.e. $x_{i+d}=x_i$ for all $i=1,2,\ldots,n-d$.

Leaving the proof as an exercise.

The problem of finding a minimal solution (in the cases where there are several) can then be solved as follows.

Assume that $d=\gcd(2m+1,n)>1$. Assume that the initial configuration of lamps $\vec{\ell}=(\ell_1,\ell_2,\ldots,\ell_n)\in\Bbb{F}_2^n$ is such that all the lamps can be swithced by a sequence of described moves. That is, if $\vec{T}(k)=(t_{1,k},t_{2,k},\ldots,t_{n,k})\in \Bbb{F}_2^n$ is the vector such that $t_{i,k}=1$ if $k$ differs from $i$ modulo $n$ by at most $m$, and $t_{i,k}=0$ otherwise, then there exists a subset $S\subseteq\{1,2,\ldots,n\}$ such that $$\vec{\ell}=\sum_{s\in S}\vec{T}(s).$$

We have the following optimality criterion:

Observation 5. Let $j\in\{0,1,2,\ldots,d-1\}$ and let $$n_j(S)=\left|\{s\in S\mid s\equiv j\pmod d\}\right|$$ for all $j$. Let $N=n/d$. Then the solution gotten by using the set $S$ is optimal (i.e. $|S|$ is minimal) if and only if $n_i(S)+n_j(S)\le N$ for all pairs of distinct indices $0\le i<j\le d-1$.

Proof. Let $C$ stand for the collection of subsets $W$ of $\{1,2,\ldots,n\}$ with the property that $$\sum_{s\in W}\vec{T}(s)=\vec{0}.$$ By Observation 4. $W\in C$ if and only if i) $W\cap \{0,1,\ldots,d-1\}$ has an even number of elements, and ii) for all $k$ we have $k\in W$ if and only if $k+d\in W$. For example, if $0\le i<j\le d-1$ then $$W_{i,j}=\{s\in\{1,2,\ldots,n\}\mid s\equiv i\pmod d\ \text{or}\ s\equiv j\pmod d\}$$ is in $C$.

The question is whether the symmetric difference $S'=S\Delta W=(S\setminus W)\cup (W\setminus S)$ is smaller than $S$ for some choice of $W$. It is easy to see that this can be achieved by some $W\in C$ if and only if it can be achieved with some set of the form $W_{i,j}$. The claim follows.

• Editing this became too slow. If I find the time to work on this, I will make an attempt to streamline it. All the key ideas should be there now. Dec 19, 2017 at 13:55