6
$\begingroup$

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}} 
$\endgroup$

2 Answers 2

9
$\begingroup$

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] & @ (Labeled[AdjacencyGraph[#, DirectedEdges -> True, 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]) 

enter image description here

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

$\endgroup$
1
  • $\begingroup$ definitely more concise and efficient :) $\endgroup$ Commented Jun 25, 2018 at 22:41
0
$\begingroup$
connections = Subsets[Table[i, {i, 4}]]; MatrixForm /@ Table[ Partition[Table[If[MemberQ[c, i], 1, 0], {i, 4}], {2}], {c, connections}] 

enter image description here

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

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.