I am trying to generate all possible transitive boolean matrices of order nxn which satisfies the following definition of transitivity
Let A=(a_ij)∈M_n(B) be a non-zero matrix. If for any i,j,k in I_n, where I_n={1,2,⋯,n}, when a_ij=a_jk=1 ,we have a_ik=1, then A is said to be transitive. and A is transitive if and only if A^2≤A.
The code I am adopting from here to extend it to nxn,
checkTrans[m1_] := Module[{m2}, m2 = m1 . m1; m1 m2 == m2]
To check the validity I am using the following
T1[n_] := n*(n - 1)^3 + 1/6*n*(n - 1)^4*(n - 2) +
1/6*n*(n - 1)*(n - 2)*(4*n - 1)
T2[n_] := 1/2*n*(n - 1)*(n^2 - n + 3)
T3[n_] := n*(2^n - 1)
T1 validity test works for only those transitive boolean matrices which has 3 nonzero elements. For example in 3x3 there are 43 transitive matrices, for 4x4 there are 276 having only 3 nonzero elements.
T2 test works for the number of n × n transitive Boolean matrices with two nonzero elements.
T3 denotes the number of transitive Boolean matrices with 1 non-0 columns in the Boolean matrices of nxn
n = 3; (*martix order*)
ms = IntegerDigits[#, 2, n^2] & /@ Range[0, 2^(n^2) - 1];
ms = ArrayReshape[#, {n, n}] & /@ ms;
validMatrices =
Select[ms, Count[Flatten[#], 1] == 3 && checkTrans[#] &];
validity = Length[validMatrices] == T1[n];
validMatrices2 =
Select[ms, Count[Flatten[#], 1] == 2 && checkTrans[#] &];
validity2 = Length[validMatrices2] == T2[n];
func[x_] :=
Length[Union[SparseArray[x]["NonzeroPositions"][[All, 2]]]] == 1
validMatrices3 = Select[ms, func];
validity3 = Length[Select[validMatrices3, checkTrans]] == T3[n];
{validity, validity2, validity3}
{True, True, True}
(validity3 fails. I am trying to make sure, that the number of all the possible transitive matrices generated by checkTrans are accurate.) @ubpdqn helped to solve this issue.
The remaining issues are
Is this the correct way to find the numbers of transitive Boolean matrices in nxn boolean matrices with 4 non-zero elements in each transitive Boolean matrix?
Select[ms, Total[Flatten[#]] == 4 && checkTrans[#] &];
Finally, this code takes ages for 5x5.
Please have a look!
Thanks