# All adjacency matrices of size n

What would be a concise way to get all adjacency matrices of size $n$, e.g. for $n=2$, these $(2^2)^2$ matrices:

{{0,0},
{0,0}}

{{1,0},
{0,0}}

{{0,1},
{0,0}}

{{0,0},
{1,0}}

{{0,0},
{0,1}}

{{1,1},
{0,0}}

{{1,0},
{1,0}}

{{1,0},
{0,1}}

{{0,1},
{1,0}}

{{0,1},
{0,1}}

{{0,0},
{1,1}}

{{1,1},
{1,0}}

{{1,1},
{0,1}}

{{0,1},
{1,1}}

{{1,0},
{1,1}}

{{1,1},
{1,1}}


### Tuples

Tuples[{0, 1}, {2, 2}]

TeXForm @ Grid[Partition[MatrixForm /@ %, 8]]


$\small\begin{array}{cccccccc} \left( \begin{array}{cc} 0 & 0 \\ 0 & 0 \\ \end{array} \right) & \left( \begin{array}{cc} 0 & 0 \\ 0 & 1 \\ \end{array} \right) & \left( \begin{array}{cc} 0 & 0 \\ 1 & 0 \\ \end{array} \right) & \left( \begin{array}{cc} 0 & 0 \\ 1 & 1 \\ \end{array} \right) & \left( \begin{array}{cc} 0 & 1 \\ 0 & 0 \\ \end{array} \right) & \left( \begin{array}{cc} 0 & 1 \\ 0 & 1 \\ \end{array} \right) & \left( \begin{array}{cc} 0 & 1 \\ 1 & 0 \\ \end{array} \right) & \left( \begin{array}{cc} 0 & 1 \\ 1 & 1 \\ \end{array} \right) \\ \left( \begin{array}{cc} 1 & 0 \\ 0 & 0 \\ \end{array} \right) & \left( \begin{array}{cc} 1 & 0 \\ 0 & 1 \\ \end{array} \right) & \left( \begin{array}{cc} 1 & 0 \\ 1 & 0 \\ \end{array} \right) & \left( \begin{array}{cc} 1 & 0 \\ 1 & 1 \\ \end{array} \right) & \left( \begin{array}{cc} 1 & 1 \\ 0 & 0 \\ \end{array} \right) & \left( \begin{array}{cc} 1 & 1 \\ 0 & 1 \\ \end{array} \right) & \left( \begin{array}{cc} 1 & 1 \\ 1 & 0 \\ \end{array} \right) & \left( \begin{array}{cc} 1 & 1 \\ 1 & 1 \\ \end{array} \right) \\ \end{array}$

Grid[Partition[#, 4], Dividers -> All] & @
VertexLabels -> "Name", ImageSize -> {120, 60},
VertexSize -> Tiny, VertexCoordinates -> {{1/3, 0}, {2/3, 0}}],
Pane[MatrixForm[#], ImageMargins -> 10], Top] & /@ Tuples[{0, 1}, {2, 2}], 4])


### IntegerDigits

You can use PadLeft with IntegerDigits and Partition the results as follows:

n = 2;
Partition[#, n] & /@ PadLeft[IntegerDigits[Range[0, 2^( n^2) - 1], 2]]
% == Tuples[{0, 1}, {n, n}]


True

• definitely more concise and efficient :) – SumNeuron Jun 25 '18 at 22:41
connections = Subsets[Table[i, {i, 4}]];
MatrixForm /@ Table[ Partition[Table[If[MemberQ[c, i], 1, 0], {i, 4}], {2}], {c, connections}]


replace 4 with $n$ and $2$ with $\sqrt{n}$