Recurrence relations - finding optimal way to solve it

56 Views Asked by At

I've found a very interesting algorithmic problem that looks like this.

Given the first row of a $(m \times n)$ $0$-indexed matrix ($m$ rows, $n$ columns), find the last row of the matrix, using this recurrence relations :

$$a_{i,j} = a_{i-1,j}\oplus a_{i-1,j+1 (\forall j : 0 \leq j \leq n-2)}$$ $$ a_{i, n-1} = a_{i-1, n-1} \oplus a_{i-1,0}$$

Of course, an $O(n^2)$ algorithm that simply generates row after row until the final one is obtained is out of the question. I'm basically looking for a $O(n)$ solution that finds the last row by simply processing the first row through employing some mathematical tricks or noticing and patterns I can't see right now.

Through inspection, I found out that the constituents of $a_{i,j}$ become cyclical after a while, but combining this into a program gives a really nasty run-time.

As an example, given $6,7,1,3$ is the first row of a $(2 \times 4)$ matrix, the last row would be $1,6,2,5$.

Any ideas? I'm quite sure this has a neat solution that I'm anxious to see but my efforts in expanding terms and noticing patterns hasn't paid off yet.

1

There are 1 best solutions below

6
On BEST ANSWER

Notice that for any $n$ we have $a_{n+2^k,j}=a_{n,j}\oplus a_{n,(j+2^k\mod m)}$.

We show it by induction on $k$. $k=0$ is direct. Assume that for any $n$ we have $a_{n+2^k,j}=a_{n,j}\oplus a_{n,(j+2^k\mod m)}$. Let $n'$ be an integer we have: $$a_{n'+2^{k+1},j}=a_{(n'+2^{k})+2^k,j}$$ hence by induction hypothesis $$a_{n'+2^{k+1},j}=a_{(n'+2^{k}),j}\oplus a_{(n'+2^{k}),(j+2^k\mod m)}$$hence by induction hypothesis $$a_{n'+2^{k+1},j}=a_{n',j}\oplus a_{n',(j+2^k\mod m)}\oplus a_{n',(j+2^k\mod m)}\oplus a_{n',(j+2^k+2^k\mod m)}$$ $$ = a_{n',j}\oplus a_{n',(j+2^{k+1}\mod m)}$$

This should improve your algorithm.