1
$\begingroup$

The problem statement

Since we all rely on mother earth, it’s our duty to save her. Therefore, you’re now asked to save energy by switching lights off.

A friend of yours has the following problem in his job. There’s a grid of size 10x10, where each square has a light bulb and a light switch attached to it. Unfortunately, these lights don’t work as they are supposed to. Whenever a switch is pressed, not only its own bulb is switched, but also the ones left, right, above and under it. Of course if a bulb is on the edge of the grid, there are fewer bulbs switched.

When a light switches it means it’s now on if it was off before and it’s now off if it was on before. Look at the following examples, which show only a small part of the whole grid. They show what happens if the middle switch is pressed. ‘ O ’ stands for a light that’s on, ‘ # ’ stands for a light that’s off.

###          #O#
###    ->    OOO
###          #O#

###          #O#
OOO    ->    ###
###          #O#

your friend loves to save energy and asks you to write a program that finds out if it is possible to turn all the lights off and if possible then how many times he has to press switches in order to turn all the lights off.

The Idea

The idea to solve this problem is to try all possible combination for the first row and then chase down to turn all off. If you find a better solution in the process then store that.

Here chase means, if a light is on at (i - 1, j) switch at (i, j) so as to make sure all the lights on (i - 1)th row will be turned off except for the last row.

By try all possible combination we will able to find the answer. But one thing I don't understand is, what is the correctness of this problem ? How can we guaranteed that this indeed touches the optimal solution. Is there any prove for this ?

For better understanding of the problem try this game.

1 Answers 1

2

We have $100$ switches and $100$ bulbs. Consider the $100\times 100$ matrix $A$ that has a $1$ in position $(i,j)$ if bulb $i$ is flipped by switch $j$ and is $0$ otherwise. Our goal is to find a solution $x$ (in the "switches space") for the linear equation $$Ax=b$$ over the field $\Bbb F_2$ (which is just a fancy way of saying that using the same switch twice has no overall effect), where the vector $b$ (living in the "bulb space") is the original status of all bulbs. More precisely, if there are several solutions, we want a solution of minimum weight (i.e., with as few $1$s as possible). Two cases are possible:

  1. $A$ is invertible. In that case there exists a solution $x$ for every possible $b$. And this solution is unique, which means we need not search for minimal weight.
  2. $A$ is not invertible, say its kernel has dimension $k>0$. In that case, there may not always exist a solution (namely only for $1/2^k$ of all possible $b$). And if there is a solution at all, there are $2^k$ solutions, and we need to test these for minimum weight (at least for small $k$, simply trying all solutions explicitly seems feasible).

Note that the second case does occur if we consider a $2\times 1$ instead of $10\times 10$ arrangement of bulbs and switches: There, both switches do the same, namely flipping both bulbs, and it is impossible to turn off all lights if originally one bulb is on. So we need to ask ourselves: Is $A$ singular for the $10\times 10$ case? This is a non-trivial question.

Now what does your chasing method mean? In your chase phase, you ignore the switches of the first row as well as the bulbs of the last row. This replaces the above $100\times 100$ matrix $A$ with a $90\times 90$ matrix $\tilde A$, obtained by removing $10$ specific rows and $10$ specific columns of $A$. The matrix $\tilde A$ now can easily be shown to be non-singular: With the obvious enumeration of bulbs and switches, it is an upper triangular matrix with an all-ones diagonal because e.g. the 42nd bulb $(4,2)$ is influenced only by the 42nd switch $(5,2)$ and the "earlier" 22nd, 31st, 32nd, and 33rd switches $(3,2)$, $(4,1)$, $(4,2)$, and $(4,3)$. This guarantees that the chasing part is always and uniquely solvable (and the triangular form of the matrix guarantees that the solution can be found one by one precisely by the method you describe). Thus if you try out the $2^{10}$ possible switchings for the first row, then do the chasing, check if the last bulb row is off, you will find all valid switchings and need only look for one of minimal weight among them. Hence the algorithm is indeed able to find the all solutions and of course among these the optimal solution.

For the general problem of an $n\times m$ grid of bulbs with $n\le m$, your algorithm has $O(2^n n(m-1) )$ running time. Can we do better? Maybe. If it should turn out that the rank of $A$ is $90$, I'd say that your method is optimal (unless someone finds a clever way to find the minimum weight vector among $2^n$ candidates without actually computing all of them). So the question of the rank of $A$ still nags us. You could start with the original $100\times 100$ matrix and perform the Gauß-Jordan algorithm on it and bring it into row echelon form. If we read off a rank $>90$ from this, it at least helps us reduce from $1024$ vectors to compare to $512$ or less ...