Is there a way to determine if by permutation of rows and columns a matrix can be transformed into a block-diagonal matrix (EDIT: with more than one block)? For example the following matrix
\begin{bmatrix} 0 &0 &7 &0 &0 \\ 0 &0 &0 &0 &3 \\ 5 &0 &0 &1 &0\\ 0 &0 &2 &0 &0 \\ 0 &1 &0 &0 &0 \end{bmatrix} EDIT: set to 0 element in 2nd row that was =2.
By permuting first row with last row and first column with last column can be transformed into the following block-diagonal matrix. \begin{bmatrix} 0 &1 &0 &0 &0 \\ 3 &0 &0 &0 &0 \\ 0 &0 &0 &1 &5\\ 0 &0 &2 &0 &0 \\ 0 &0 &7 &0 &0 \end{bmatrix} Is there an algorithm to find out if it is possible and do it, or to determine the permutation matrix?
My problem is about representing points scored by players against each other in. I need to determine the relative strength of players by comparing their scores. Imagine the matrix is the score of a player against another player. Each row is a player, and each column is a player. The diagonal is empty, or zero. If nobody in a group of player has played against anybody in the other group of players, then I cannot rank one group against the other group, because I do not know their relative strength. So I'm trying to eventually decompose the matrix into several if they exist. In the example above they would be the two following ones \begin{bmatrix} 0 &1 &0 &0 &0 \\ 3 &0 &0 &0 &0 \\ 0 &0 &0 &0 &0\\ 0 &0 &0 &0 &0 \\ 0 &0 &0 &0 &0\ \end{bmatrix} \begin{bmatrix} 0 &0 &0 &0 &0 \\ 0 &0 &0 &0 &0 \\ 0 &0 &0 &1 &5\\ 0 &0 &2 &0 &0 \\ 0 &0 &7 &0 &0\end{bmatrix}
Problem
Given an $n\times n$ matrix $A=(a_{ij})$, determine if it is possible to find a permutation matrix $P=(p_{ij})$ and square matrices $B$ and $C$ such that $$ PAP^{\intercal}=\begin{pmatrix}B & 0\\ 0 & C \end{pmatrix}. $$
Note: By grouping blocks, any matrix with three or more diagonal blocks can also be considered as a matrix with two diagonal blocks. For example,
$$ \begin{pmatrix}B\\ & C\\ & & D \end{pmatrix}=\begin{pmatrix}B\\ & \begin{pmatrix}C\\ & D \end{pmatrix} \end{pmatrix} $$
Solution
Let $G=(V,E)$ denote the undirected adjacency graph of $A$. That is, $V$ and $E$ are defined by $$ V=\{1,\ldots,n\}\qquad\text{and}\qquad E=\left\{ (i,j)\in V\times V\colon a_{ij}\neq0\text{ or }a_{ji}\neq0\right\}. $$ Now, our original problem is equivalent to determining whether $G$ is a graph with two or more disjoint components.
This is accomplished with a breadth-first search (or depth-first search) started at an arbitrary vertex, call it $v_{1}$. Let $V^{\prime}=\{v_{1},\ldots,v_{k}\}$ be the set of vertices visited by the search. Then, the answer to our original problem is in the affirmitive if and only if $V^{\prime}$ is a proper subset of $V$.
Example
Let's apply the algorithm to the matrix $$ A=\begin{pmatrix}0 & 0 & 7 & 0 & 0\\ 0 & 0 & 0 & 0 & 3\\ 5 & 0 & 0 & 1 & 0\\ 0 & 0 & 2 & 0 & 0\\ 0 & 1 & 0 & 0 & 0 \end{pmatrix}. $$
Let's run the search algorithm:
The final vertex set is $V^{\prime}=\{v_1,v_2,v_3\}=\{1,3,4\}$. Next, make any permutation matrix that satisfies $p_{1v_{1}}=p_{2v_{2}}=p_{3v_{3}}=1$. For example, we could take $$ P=\begin{pmatrix}1 & 0 & 0 & 0 & 0\\ 0 & 0 & 1 & 0 & 0\\ 0 & 0 & 0 & 1 & 0\\ 0 & 1 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 1 \end{pmatrix}. $$ You can check that $PAP^{\intercal}$ has two diagonal blocks: $$ PAP^{\intercal}=\begin{pmatrix}0 & 7 & 0 & 0 & 0\\ 5 & 0 & 1 & 0 & 0\\ 0 & 2 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 3\\ 0 & 0 & 0 & 1 & 0 \end{pmatrix}. $$
MATLAB implementation
The code below can be used to make the matrix
Pdescribed above from an input matrixA. Just callperm_mat_to_make_block_diag(A).Note: I don't have access to MATLAB and GNU Octave has not implemented the breadth-first search function
bfsearch, so I was unable to test the code below. If someone could test it for me, that would be great.