Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
    
Related: mathematica.stackexchange.com/a/7183/8 –  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
add comment

3 Answers 3

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@
    AdjacencyGraph[tds, 
     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]

enter image description here

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

enter image description here

share|improve this answer
add comment

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]

Graph

And here is the case using GraphPlot.

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

enter image description here

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]]

enter image description here

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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