Binary relations and closures

I have made a little tool. Here you can specify some binary relation over a finite set. Then you can generate a closure of it.

How to use it

Click on checkbox with coordinates (x, y) to make it the element of relation. The closure is marked with black squares behind checkboxes. You can specify some criteria for closure (reflexivity, symmetry, transitivity).

Code

I decided to use recursive attitude to make a closure. At the beginning, add or not add a diagonal, according to reflexivity checkbox. Then I call CloseCell(x, y) on each "cell" (pair) defined by user. It marks it as "done" (bool matrix "rdone"), it sets a relation value (bool matrix "r") and recursively cals CloseCell on other cells, when transitivity or symmetry was checked.

function CloseCell(i, j)
{
   if(rdone[i][j]) return;
   r[i][j] = rdone[i][j] = true;

   // if symmetric
   if(c.sym) CloseCell(j, i);

   // if transitive	
   if(c.tra) for(var k=0; k<n; k++) if(r[j][k]) CloseCell(i, k);
}

Correctness

As you see, I didn't use a classic Floyd-Warshall-like attitude to make transitive closure. It seems that it works, but I am not sure with it's correctness (I don't know why it works). If you find some proof of correctness, or you find a relation, when closure is incorrect, please let me know :) .

Old comments (closed because of spam)