How do I convert a list to a Graph with directed edges?
data =
{{1, 3, 2, 1, 3, 1, 3, 1, 2, 1, 2}, {1, 2, 1, 2, 1, 2, 3, 2, 3, 2, 3}, {1, 3, 2,
1, 2, 3, 1, 2, 1, 3, 2}, {1, 3, 2, 1, 2, 1, 2, 1, 3, 1, 3}, {1, 2, 3, 1, 3, 1,
3, 2, 1, 2, 1}};
DirectedEdge @@@ Partition[#, 2, 1] & /@ data
{{1 \[DirectedEdge] 3, 3 \[DirectedEdge] 2, 2 \[DirectedEdge] 1, 1 \[DirectedEdge] 3, 3 \[DirectedEdge] 1, 1 \[DirectedEdge] 3, 3 \[DirectedEdge] 1, 1 \[DirectedEdge] 2, 2 \[DirectedEdge] 1, 1 \[DirectedEdge] 2}, {1 \[DirectedEdge] 2, 2 \[DirectedEdge] 1, 1 \[DirectedEdge] 2, 2 \[DirectedEdge] 1, 1 \[DirectedEdge] 2, 2 \[DirectedEdge] 3, 3 \[DirectedEdge] 2, 2 \[DirectedEdge] 3, 3 \[DirectedEdge] 2, 2 \[DirectedEdge] 3}, {1 \[DirectedEdge] 3, 3 \[DirectedEdge] 2, 2 \[DirectedEdge] 1, 1 \[DirectedEdge] 2, 2 \[DirectedEdge] 3, 3 \[DirectedEdge] 1, 1 \[DirectedEdge] 2, 2 \[DirectedEdge] 1, 1 \[DirectedEdge] 3, 3 \[DirectedEdge] 2}, {1 \[DirectedEdge] 3, 3 \[DirectedEdge] 2, 2 \[DirectedEdge] 1, 1 \[DirectedEdge] 2, 2 \[DirectedEdge] 1, 1 \[DirectedEdge] 2, 2 \[DirectedEdge] 1, 1 \[DirectedEdge] 3, 3 \[DirectedEdge] 1, 1 \[DirectedEdge] 3}, {1 \[DirectedEdge] 2, 2 \[DirectedEdge] 3, 3 \[DirectedEdge] 1, 1 \[DirectedEdge] 3, 3 \[DirectedEdge] 1, 1 \[DirectedEdge] 3, 3 \[DirectedEdge] 2, 2 \[DirectedEdge] 1, 1 \[DirectedEdge] 2, 2 \[DirectedEdge] 1}}
If you have a list of vertices representing such a path, you can use
Graph[DirectedEdge @@@ Partition[list, 2, 1]]
If you just want the graph fort his Markov process, use Graph[p]
.
You can also use BlockMap
toEdges1 = BlockMap[Apply @ DirectedEdge, #, 2, 1] &;
or the (undocumented) 6-argument form of Partition
:
toEdges2 = Partition[#, 2, 1, {1, -1}, {}, DirectedEdge] &;
Using it with data
:
toEdges1 /@ data
Row[Graph[#, ImageSize -> 200] & /@ toEdges1 /@ data]
toEdges1 /@ data ==
toEdges2 /@ data ==
( DirectedEdge @@@ Partition[#, 2, 1] & /@ data)
True