# Graph adjacency matrix with unconnected nodes

I am trying to display a series of 9x9 adjacency matrices which represent directed graphs on a 3x3 grid. The issue I am running up against is this (compounded by that fact that I don't really know how to do anything in Mathematica): only the connected nodes appear, and they do so with a Mathematica-decided geometry. I'd like every point in the adjacency matrix to refer to the same fixed coordinate on the 3x3 grid every time. It seemed that VertexCoordinates might work, but it only refers to the vertices that have degree > 0, right?

Insight on how to accomplish this? Any help would be much appreciated.

-
– Verbeia Jun 22 '12 at 21:33
Yes, VertexCoordinates will do what you want. – Verbeia Jun 22 '12 at 21:35
@verbeia What about the part about the unconnected nodes? – Sjoerd C. de Vries Jun 22 '12 at 21:50
I was typing up my answer when you commented :) – Verbeia Jun 22 '12 at 22:11
MathTeam, welcome to Mathematica.SE! If you registered your account then any reputation points you get from future questions and answers will be added to those you are getting from this question. – Verbeia Jun 22 '12 at 22:14

Answers here use table to specify VertexCoordinates. Another way is to use GridGraph and extract VertexCoordinates from it. It may seem excessive, but it actually requires a bit less code.

AbsoluteOptions[GridGraph[{3, 3}], VertexCoordinates]

VertexCoordinates -> {{1., 1.}, {1., 2.}, {1., 3.}, {2., 1.}, {2., 2.}, {2., 3.}, {3., 1.}, {3., 2.}, {3., 3.}}

You have giant space of 2^81 possible 9x9 matrices. If you need to deal with most of them a nice interactive setup may help. I happen to have some app written already, where you set AdjacencyMatrix by mouse clicks. it may come handy, especially when you want to see the relation between AdjacencyMatrix and the graph. To use click on cells in the grid.

Manipulate[
x = ConstantArray[0, 9 {1, 1}];
Row[{
EventHandler[
Dynamic[tds = Reverse[Transpose[x]];
MatrixPlot[tds, PlotRangePadding -> 0, Mesh -> All,
ImageSize -> {300, 300}, ColorRules -> {1 -> Black, 0 -> None}]
],
{"MouseClicked" :> (pos = Ceiling[
MousePosition["Graphics"]];
x = ReplacePart[x, pos -> 1 - x[[Sequence @@ pos]]];)}],

Dynamic@
AbsoluteOptions[GridGraph[{3, 3}], VertexCoordinates],
ImageSize -> {300, 300}, PlotRange -> {{.5, 3.5}, {.5, 3.5}},
VertexStyle -> Directive[Red, Opacity[.5]], VertexSize -> .2,
EdgeStyle -> Thick, DirectedEdges -> True]
}],
{pos, ControlType -> None},
{x, ControlType -> None},
{tds, ControlType -> None},
AppearanceElements -> None, FrameMargins -> 0]

-
+1 Very nice approach – Dr. belisarius Jun 22 '12 at 23:31
Graph[Range@9,
UndirectedEdge @@@ Partition[Range@9, 2],
VertexCoordinates -> Flatten[Table[{i, j}, {i, 3}, {j, 3}], 1],
VertexLabels -> "Name", ImagePadding -> 20]

-

Here is an adjacency graph with at least one unconnected node:

am = RandomVariate[BernoulliDistribution[0.1], {9, 9}]

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

Here are the coordinates if you want to lay those nodes out on a grid.

vc = Flatten[Table[{i, j}, {i, 3}, {j, 3}], 1]

{{1, 1}, {1, 2}, {1, 3}, {2, 1}, {2, 2},
{2, 3}, {3, 1}, {3, 2}, {3, 3}}

If you use GraphPlot rather than Graph, the coordinates need to be expressed like this.

{1 -> {1, 1}, 2 -> {1, 2}, 3 -> {1, 3}, 4 -> {2, 1}, 5 -> {2, 2},
6 -> {2, 3}, 7 -> {3, 1}, 8 -> {3, 2}, 9 -> {3, 3}}

Here is the case using a Graph construct.