# 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.

• Commented Jun 22, 2012 at 21:33
• Yes, VertexCoordinates will do what you want. Commented Jun 22, 2012 at 21:35
• @verbeia What about the part about the unconnected nodes? Commented Jun 22, 2012 at 21:50
• I was typing up my answer when you commented :) Commented Jun 22, 2012 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. Commented Jun 22, 2012 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]


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.

vcr = Thread[Range[9] -> vc]

{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.

AdjacencyGraph[am, VertexCoordinates -> vc]


And here is the case using GraphPlot.

GraphPlot[am, VertexCoordinateRules -> vcr, SelfLoopStyle -> All]


As you can see, if you specify an adjacency matrix, Mathematica will display the unconnected nodes. You can make the nodes easier to see by using options such as VertexSize in Graph, and PlotStyle or VertexRenderingFunction in GraphPlot. Here is an example with GraphPlot where I have also turned off the self-loop:

GraphPlot[am, VertexCoordinateRules -> vcr,
PlotStyle -> AbsolutePointSize[8]]


The main difference between Graph and GraphPlot is that the former shows nice curved edges to avoid confusion about whether {1,3} is connected to {3,3} or {2,3}. The option names are all different, too. Which you choose is up to you.