# Dynamically sized $n\times n$ checkboxes corresponding to a matrix

I was wondering if it was possible to do to following:

1. have an input field for an integer where you input the $n$

2. have an $n\times n$ matrix of checkboxes that would correspond to an $n\times n$ matrix which has 0's where there boxes are not checked and 1's where they are. This would allow me to input the adjacency matrix of a graph into Mathematica in a fairly easy way without having to type it up every time.

EDIT: Something like this code modified for my purposes

Manipulate[
ArrayPlot[Take[data, n, n]],
{{data, RandomInteger[{0, 1}, {20, 20}]}, ControlType -> None},
{{n, 5}, 1, 20, 1},
Dynamic[
Panel[Grid[Outer[Checkbox[Dynamic[data[[#1, #2]]], {0, 1}] &, Range[n], Range[n]]]]]]


EDIT: I have the graphing code working properly as shown below but I want to change/add two things. 1) instead of n being a slider I want it to be an input box. 2) I want to implement FindshortestPath function on the graph that is generated with two input boxes for which two vertices you are finding the path between

Manipulate[ GraphPlot[Take[data, n, n], VertexLabeling -> True, SelfLoopStyle -> All], {{data, RandomInteger[{0, 0}, {20, 20}]}, ControlType -> None}, {{n, 5}, 1, 10, 1}, Dynamic[Panel[ Grid[Outer[Checkbox[Dynamic[data[[#1, #2]]], {0, 1}] &, Range[n], Range[n]]]]]]

A bit simple minded:

DynamicModule[{n = 3, bs},
Panel[Column[{Slider[Dynamic[n, {(n = #) &,
(bs = PadRight[bs, {n, n}]) &}],
{2, 100, 1}],
Row[{Dynamic[Grid[Array[Checkbox[Dynamic[bs[[##]]], {0, 1}] &,
{n, n}]]],
Spacer[10], Dynamic[ArrayPlot[bs]]}]}]],
Initialization :> {bs = ConstantArray[0, {n, n}]}]


You can modify it to use an InputField[] instead for changing the array's size.

• I tried changing your code like this in order for it to treat the matrix as the adjacency matrix for a graph as well as inputfield but ran into some issues. DynamicModule[{n = 3, bs}, Panel[Column[{InputField[ Dynamic[n, {(n = #) &, (bs = PadRight[bs, {n, n}]) &}], {2, 20, 1}], Row[{Dynamic[ Grid[Array[Checkbox[Dynamic[bs[[##]]], {0, 1}] &, {n, n}]]], Spacer[10], Dynamic[GraphPlot[bs]]}]}]], Initialization :> {bs = ConstantArray[0, {n, n}]}] Commented May 9, 2016 at 13:44
Manipulate[Row[{ArrayPlot[mat[[;; k, ;; k]], ImageSize -> 300],
AdjacencyGraph[mat[[;; k, ;; k]],  ImageSize -> {300, 300}]}],
{{mat, ConstantArray[0, {50, 50}]}, None}, {k, 4, None},
Dynamic[Column[{InputField[Dynamic[k, (k = Clip[IntegerPart@#, {2, 20}]) &], Number,
FieldSize -> {8, 1}],
Panel[Grid[Outer[Checkbox[Dynamic[mat[[#, #2]]], {0, 1}] &, Range@k, Range@k]]]}]]]


Update:

I want to implement FindShortestPath function on the graph that is generated with two input boxes for which two vertices you are finding the path between

Manipulate[Row[{ArrayPlot[mat[[;; k, ;; k]], ImageSize -> 300],
With[{ag = AdjacencyGraph[mat[[;; k, ;; k]], ImageSize -> {300, 300},
VertexLabels -> "Name", ImagePadding -> 5]},
HighlightGraph[ag, PathGraph[FindShortestPath[ag, s, t],
DirectedEdges -> True]]]}],
{{mat, ConstantArray[0, {50, 50}]}, None},
{k, 6, None}, {s, 1, None}, {t, 2, None},
Dynamic[Row[{Column[{Style["size", 14, "Panel"],
InputField[Dynamic[k, (k = Clip[IntegerPart@#, {2, 20}]) &],
Number, FieldSize -> {8, 1}],
Style["source/target", 14, "Panel"],
InputField[Dynamic[s, (s = Clip[IntegerPart@#, {2, 20}]) &],
Number, FieldSize -> {8, 1}],
InputField[Dynamic[t, (t = Clip[IntegerPart@#, {2, 20}]) &],
Number, FieldSize -> {8, 1}]}, Alignment -> Top],
Panel[Grid[Outer[Checkbox[Dynamic[mat[[#, #2]]], {0, 1}] &, Range@k,
Range@k]]]}, Spacer[5]]]]