Count equivalence classes of matrices with row sum and column sum == 4
Method: Iteration by composition
by rplantiko
HTML
<script src="https://rawgit.com/rplantiko/4d5c642e98baddcd11d5aa7242dc7622/raw/db3477d5ffef99a98b121578338f600be9a9d926/BitField.class.js"></script>
There are 67950 6 x 6 matrices which have only 1 and 0 as coefficients, with the property that the number of one's in each row and in each column amounts to 4.
<p>
Consider two of those matrices as equivalent, if they can be transformed into each other by permuting rows and columns. Compute the equivalence classes under this equivalence relation.
<div id="statustext">Progress
<div id="status-frame"><div id="status-bar"></div></div>
(<span id="status-count">0</span> of 67950)</div>
<div id="output">
<span id="browser-message">Code works only with a modern browser!</span>
</div>
<div id="bf"></div>
The total number of these matrices, 67950, is the seventh number of the OEIS sequence <a href="https://oeis.org/A001499">A001499</a>
<div id="button-area">
<button id="stop">Stop worker process</button>
</div>
<h3>Representants of the orbits:</h3>
CSS
body {
font-family:Lucida,Arial;
padding:1em;
}
#status-frame {
border:solid black 1px;
position:relative;
width:15em;
height:1em;
display:inline-block;
}
#button-area {
margin-top:1em;
}
#status-bar {
position:absolute;
left:0px;
background-color:green;
width:0px;
height:100%;
}
#output {
margin:1em 0em;
font-weight:bold;
}
#matrix {
margin:1em 0em;
border-collapse:collapse;
}
#matrix td {
border:solid black 1px;
width:2em;
height:2em;
vertical-align:middle;
text-align:center;
}
#browser-message {
font-style:oblique;
color:orange;
}
JavaScript
"use strict";
(function(){
var output = document.getElementById("output")
var statusCount = document.getElementById("status-count")
var statusBar = document.getElementById("status-bar")
output.textContent = ""
var worker = delegateWorker(work, data=>{
if (data.finished) {
output.insertAdjacentHTML('beforeend',
`Total count: ${data.count}`)
} else if ("status" in data) {
statusCount.textContent = data.status
// Remark: we know the number 67950 from the previous jsfiddle
// https://jsfiddle.net/rplantiko/jzz1e132/
statusBar.style.width = 100 * data.status / 67950 + "%"
} else {
if ("msg" in data) {
output.insertAdjacentHTML('beforeend',
`${data.count}: ${data.msg} (${data.bf})<br>`)
if ("M" in data) {
let matrix = createMatrix()
showMatrix(matrix,data.M)
}
}
}
})
document.getElementById("stop").addEventListener("click",function(){
worker.terminate()
})
// --- End of on DOM ready execution
// Function work() - executed in a "worker" process
function work() {
var M = new Array(6)
var cols = 0
var count = 0
var all = []
// Build M successively from six columns
// When the innermost function is reached, we have a new valid matrix
r(r(r(r(r(r( ()=>{
// Found one matrix, compute all equivalent matrices by permutation
var bf = encodeMatrix(M)
var M_Sym
if (isSymmetric(M)) M_Sym = M
if (!alreadyIndexed(bf,all)) {
var orbit = new Set([ bf ])
try {
// Generate new matrices by permuting rows and columns
permute( [0,1,2,3,4,5], function(p,i) {
permute( [0,1,2,3,4,5], function(q,j) {
if (i===0 && j===0) return // Ignore the identical permutation
var Mn = permutedMatrix(p,q)
if (!M_Sym && isSymmetric(Mn)) M_Sym = Mn
var bfn = encodeMatrix(Mn)
if (!orbit.has(bfn)) {
orbit.add(bfn)
let total = totalCount(orbit)
...