I was recently asked to solve a problem in a programming interview involving word squares, and on further reflection I realized it could be recast as a linear algebra question. Since my solution has a worst-case time complexity of $O(n!)$ if $n$ is the width of $A$, out of personal curiosity I'm trying to find a better solution, as measured by worst-case time complexity.
Given an $n$ by $n$ square matrix $A$ over the reals with $n\geq 2$, is there an $n$ by $n$ permutation matrix $X$ satisfying $$XA=(XA)^T?$$
Since $(XA)^T=A^TX^T$ and $X$ is a permutation matrix, we have $X^T=X^{-1}$, hence this is equivalent to asking if there are any solutions to $$XAX=A^T$$ which are permutation matrices. Naively, I was hoping there would be a unique solution, but numeric techniques strongly indicate a wide variety of solutions, even when a satisfactory permutation exists.
Unfortunately, it doesn't seem that solutions form a linear subspace. For example, if $X$ is a solution, then $(aX)A(aX)=a^2A^T$ for any real $a$, so most multiples of $X$ are not solutions for most matrices $A$. Similar problems exist for sums of solutions. With this in mind, I'm not quite sure which method one might use to enumerate solutions. If they don't form a linear subspace, we can't just enumerate a basis.
Any partial solutions, theorems, pointers in the right direction, or related problems would be helpful. I'm not familiar with what seems to be a nonlinear matrix equation (if that's the right terminology).
Update This seems to be a special case of something called the algebraic Riccati equation, as indicated in another question. The techniques I have found so far emphasize one of two approaches:
- Algorithms for finding any solution from a given starting guess.
- Algorithms for finding the unique stabilizing solution, should one exist.
Given the control theoretic background for those approaches, they typically assume that $A^T$ is symmetric. In my case, if $A^T$ were symmetric I could simply take $X$ to be the identity and be done, and that assumption oversimplifies the problem. Additionally, those solutions do not seem to have a clean way to enumerate all solutions, inhibiting one from checking if any happen to be permutation matrices. Does anyone have more tips from a Riccati equation standpoint?
I would like to add that strictly speaking one can answer the question in finite time by simply enumerating all permutation matrices of the appropriate size and checking if they satisfy $XA=(XA)^T$. For some reason I'm having trouble describing, I don't think this captures the spirit of the question.
Update @GCab points out that $A$ would need a very peculiar structure for $X$ to be a permutation matrix. This is indeed the case, and $A$ would need to be symmetric on some row or column permutation. This does yield some insights. If $X$ is any permutation matrix, then $$(XAX)_{ij}=A_{ji}=(A^T)_{ij}$$ for any $i$, $j$ so that $X_{ij}=1$, and this does not hold when $X_{ij}=0$. In other words, the elements of $A$ come in pairs except for possibly the main diagonal of $XA$. Using this, one can rule out many possible permutations, since any element appearing only once in $A^T$ must give a location of a $1$ in $X$.
There are many other such combinatorial tricks that can greatly improve the average runtime. One can similarly note that for a permutation $X$ to exist as a solution, there must exist a bijection $f$ between the rows and columns of $A$ so that if $r$ is a row of $A$ then the count of each element of $r$ is the same as its count in $f(r)$. Such a heuristic has the potential to easily classify a choice of $A$ as not having a satisfactory solution, but it could still take an enormous amount of time to confirm that a given $A$ does have a satisfactory $X$.