# Show path with arrows in a matrix

I have a matrix, for example, 5x5 matrix Partition[Range, 5]. And I am given a list, for example:

 {1, 1}, {2, 1}, {3, 1}, {4, 1}, {5, 1}, {5, 2}, {4, 2},
{3, 2}, {2, 2}, {1, 2}, {1, 3}, {2, 3}, {3, 3}, {4, 3},
{5, 3}, {5, 4}, {4, 4}, {4, 5}, {5, 5}


and I need to trace the path from the element in (1,1) to (2, 1), and then from (2, 1) to (3, 1), etc., from (4, 5) to (5, 5) and end there.

How can I draw this graphically with an arrow indicating each of the transition?

• Where does this problem come from? Is it an assignment? The exact same question was asked here: stackoverflow.com/questions/8812371/… Feb 6 '12 at 21:51
• @Szabolcs: no it is not assignment. It is different from that question; here, I need arrows. Feb 6 '12 at 21:58
• Out of curiosity, what are you going to use this for? Feb 6 '12 at 22:01
• just some visualization. :) Feb 6 '12 at 22:26

Mathematica's Graph related functionality is pretty great. You can easily style vertexes, edges and their labels, apply interesting functions. For small increase in code sophistication you gain quite a bit of advantage. Your data:

poli = {{1, 1}, {2, 1}, {3, 1}, {4, 1}, {5, 1}, {5, 2}, {4, 2}, {3,
2}, {2, 2}, {1, 2}, {1, 3}, {2, 3}, {3, 3}, {4, 3}, {5, 3}, {5,
4}, {4, 4}, {4, 5}, {5, 5}};


A simple solution with still quite wide styling options I think would be this

arrow[coord_, e_] := Style[Arrow[coord], Red, Thickness[.01], Arrowheads[0.06]]

pg = PathGraph[Range, VertexCoordinates -> poli,
EdgeShapeFunction -> arrow, ImageSize -> 300, PlotRangePadding -> .2];

gg = GridGraph[{5, 5}, EdgeStyle -> Dashed, VertexLabels -> "Name",
ImageSize -> 300, PlotRangePadding -> .2];

Overlay[{gg, pg}] The rest is some more elaborate exploratory fun with graphs.

We'll use GridGraph again but instead of PathGraph we'll use GraphHighlight option. GridGraph has its own 1D node index system (see indexes above), not 2D indexes as in matrix. So we have to remap the indexes.

vertex = (5 (#[] - 1) + #[]) & /@ poli;

edge = ( #[] \[UndirectedEdge] #[]) & /@ Partition[vertex, 2, 1];


lab = MapThread[Rule[#1, ToString@#2] &, {vertex, poli}];

GridGraph[{5, 5}, EdgeStyle -> Dashed, GraphHighlight -> edge,
VertexLabels -> lab, PlotRangePadding -> .3] If you want arrows there are many ways around, especially depending on how you choose the path. Quick cooking gives something like this:

arr[coord_, e_] :=
Style[Arrow[
If[Positive[(e)[] - (e)[]], Identity[coord],

GridGraph[{5, 5}, EdgeStyle -> Dashed, GraphHighlight -> edge,
VertexLabels -> lab, PlotRangePadding -> .5,
EdgeShapeFunction -> (# -> arr & /@ edge)] • I got an error GridGraph::optx: "Unknown option EdgeStyle->Dashing[{Small,Small}] in ". What version of Mma are you using? Thanks! Feb 6 '12 at 22:26
• @QiangLi I use V8.0.4 - just updated the code. It works for me. Feb 6 '12 at 22:31
• It is very strange here. I can sometimes get the command GridGraph[{5, 5}, VertexSize -> Small, GraphHighlight -> {3 \[UndirectedEdge] 4, 3 \[UndirectedEdge] 2, 2}] executed, but not other times. What is going on here? Feb 6 '12 at 22:47
• Looks like I cannot do GridGraph[{5, 5}, VertexSize -> Small, GraphHighlight -> {1 \[UndirectedEdge] 2, 2 \[UndirectedEdge] 3, 3 \[UndirectedEdge] 4, 4 \[UndirectedEdge] 6}] because the last 6 is larger than the dimension 5. Feb 6 '12 at 22:49
• I see. Because there is no edge between 4 and 6 above. No, I am using V8.0.0. Feb 6 '12 at 22:52

Make a graphics where you put at each position {i,j} the entries of your matrix. The only thing left to do is to transform your path positions from the form {p1,p2,...} into {{p1,p2},{p2,p3},...}, which can easily done with Partition. Then you can map Arrow over this list and have your result

m = RandomInteger[{0, 10}, {5, 5}];
path = {{1, 1}, {2, 1}, {3, 1}, {4, 1}, {5, 1}, {5, 2}, {4, 2},
{3, 2}, {2, 2}, {1, 2}, {1, 3}, {2, 3}, {3, 3}, {4, 3},
{5, 3}, {5, 4}, {4, 4}, {4, 5}, {5, 5}};
Graphics[{Table[Text[Style[m[[j, i]], Blue, 18], {i, -j}],
{i, 5}, {j, 5}], Opacity[0.5], Red,
Arrow /@ Partition[({1, -1}*#1 & ) /@ path, 2, 1]}]


note that you have to plot your matrix entries and the path points upside down to make the entry {1,1} appear at the top. Something like this?

a = Partition[Range@25, 5];

path = {{1, 1}, {2, 1}, {3, 1}, {4, 1}, {5, 1}, {5, 2}, {4, 2}, {3,
2}, {2, 2}, {1, 2}, {1, 3}, {2, 3}, {3, 3}, {4, 3}, {5, 3}, {5,
4}, {4, 4}, {4, 5}, {5, 5}};

Graphics[{
Text[#2, #] & @@@ Most@ArrayRules[a],
Arrow[path]
}] After seeing other answers I suppose you did intend different coordinates:

Graphics[{
Text[#2, {1, -1} Reverse@#] & @@@ Most@ArrayRules[a],
Arrow[{1, -1} # & /@ path]
}] Let's define the matrix and path first:

matrix = Partition[Range, 5];

path = {{1, 1}, {2, 1}, {3, 1}, {4, 1}, {5, 1}, {5, 2}, {4, 2}, {3,
2}, {2, 2}, {1, 2}, {1, 3}, {2, 3}, {3, 3}, {4, 3}, {5, 3}, {5,
4}, {4, 4}, {4, 5}, {5, 5}};


Then this is my preferred solution:

radius = 0.25;
Graphics[
{MapIndexed[{Circle[#2, radius], Text[#, #2]} &, matrix, {2}],
Arrow[#, radius] & /@ Partition[path, 2, 1]},

BaseStyle -> {FontSize -> Scaled[1/20]}
] You can easily style the arrows and Disks or Circles to your liking. The key to making this look good was the second argument of Arrow which makes it possible to easily offset the beginning and endpoints, so they don't overlap the disks.