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$
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$
  • $\begingroup$ definitely more concise and efficient :) $\endgroup$ – SumNeuron Jun 25 '18 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$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.