Generate all possible adjacency matrices:
n = 4;
adjmat = Table[Partition[IntegerDigits[i, 2, n^2], 4], {i, 0, 2^(n^2) - 1}];
Remove ones containing self-loops:
adjmat = Select[adjmat, Diagonal[#] == ConstantArray[0, n] &];
Make graph list:
glist = AdjacencyGraph[#, DirectedEdges -> True] & /@ adjmat;
Remove isomorphic duplicates:
glist = DeleteDuplicates[glist, IsomorphicGraphQ];
Length[glist]
(* 218 *)
Looks good, there are supposed to be 218 different ones on 4 vertices.
WARNING: We could have done this differently. Instead of removing adjacency matrices with non-zero diagonals, we could have applied SimpleGraph
to the graph list. But in that case not all duplicates will be removed because of a long-standing bug in IsomorphicGraphQ
!
We could also have not generated adjacency matrices with non-zero diagonals but I was too lazy to do that.
Now all you need to do is filter glist
down based on your conditions, i.e.
Select[glist, condition]
For example,
Select[glist, ConnectedGraphQ]
will take only strongly connected ones.
Note: there are some programs out there that can generate non-isomorphic graphs quickly, e.g. geng
from the nauty package.