A binary relation on a set \$X\$ is simply a subset \$S \subseteq X \times X\$; in other words, a relation is a collection of pairs \$(x,y)\$ such that both \$x\$ and \$y\$ are in \$X\$. The number of different relations grows quickly with the size of the set: if \$X\$ contains \$n\$ elements, there are \$2^{n^2}\$ binary relations on \$X\$.
This challenge will have you computing the number of binary relations subject to certain constraints, listed here:
A binary relation is called "reflexive" if \$(x,x) \in S\$ for all \$x \in X\$.
A binary relation is called "irreflexive" if \$(x,x) \not\in S\$ for all \$x \in X\$.
A binary relation is called "symmetric" if whenever \$(x,y) \in S\$, then \$(y,x) \in S\$.
A binary relation is called "asymmetric" if whenever \$(x,y) \in S\$, then \$(y,x) \not\in S\$.
A binary relation is called "transitive" if whenever \$(x,y) \in S\$ and \$(y,z) \in S\$ then \$(x,z) \in S\$.
A binary relation is called "antitransitive" if whenever \$(x,y) \in S\$ and \$(y,z) \in S\$ then \$(x,z) \not\in S\$.
Challenge
The goal of this code-golf challenge is to write a function that takes in a nonnegative integer \$n\$, and some subset of the six conditions above in any reasonable format*, and returns the number of binary relations on the set \$\{1,2,\dots,n\}\$ satisfying all of the conditions in the aforementioned subset.
Brute-force strategies are okay, but your code should be able to handle all \$n \leq 4\$ on TIO.
Test Data
n | conditions | number of binary relations
--+------------------------------------+-------------------------
0 | {reflexive, antitransitive} | 1
3 | {reflexive, antitransitive} | 0
3 | {} | 512
3 | {antitransitive} | 39
4 | {antitransitive} | 921
4 | {reflexive, irreflexive} | 0
4 | {symmetric, asymmetric} | 1
4 | {transitive, antitransitive} | 87
4 | {reflexive, symmetric, transitive} | 15
4 | {symmetric, transitive} | 52
4 | {asymmetric, antitransitive} | 317
Example
For \$n = 3\$, there are \$39\$ antitransitive relations, as shown by the illustration below. (Strictly speaking, the illustration shows unlabeled relations.)
- There is \$1\$ empty relation.
- There are \$6\$ relations consisting of just one pair.
- There are \$3 + 3 + 6 + 3\$ relations consisting of two pairs.
- There are \$6 + 6 + 2\$ relations consisting of three pairs.
- There are \$3\$ relations consisting of four pairs.
* For example, you could take the conditions as a list like [False, False, True, False, False, True]
, with each position referring to the particular condition. As another example, you could take a set of strings like {"transitive", "asymmetric"}
.