6
$\begingroup$

I created a discrete Markov process using p = DiscreteMarkovProcess[{1, 0, 0}, {{0, 1/2, 1/2}, {1/2, 0, 1/2}, {1/2, 1/2, 0}}]; and simulated it 5 times to create a list of lists:

data = Table[RandomFunction[p, {0, 10}][[2]][[1]][[1]], {i, 1, 5, 1}]
(*{{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}}*)

I would like to convert each of these lists into a list of vertices that are directed from the first member of the list to the next and then the next:

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}

How do I do that? I explored EdgeAdd but that doesn't give me the desired result:

Table[EdgeAdd[
  Graph[{data[[1]][[1]] \[DirectedEdge] data[[1]][[2]]}, 
   VertexLabels -> "Name"], 
  data[[1]][[i]] \[DirectedEdge] data[[1]][[i + 1]]],
 {i, 2, 5, 1}]

enter image description here

What function may I use to convert a list to a set of directed vertices for a Graph?

$\endgroup$

3 Answers 3

6
$\begingroup$

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

$\endgroup$
3
  • 1
    $\begingroup$ Beat you to the punch. ;^)) $\endgroup$
    – Mr.Wizard
    Commented Jun 24, 2020 at 18:13
  • $\begingroup$ Oh my... this was simpler than expected. Silly me. Two answers at nearly the same time instance! $\endgroup$
    – dearN
    Commented Jun 24, 2020 at 18:15
  • $\begingroup$ @Mr.Wizard 5 full seconds! :D I'll leave this though because of the Graph[p] bit, which I think many people don't know. (And +1) $\endgroup$
    – Szabolcs
    Commented Jun 24, 2020 at 19:26
6
$\begingroup$
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}}
$\endgroup$
2
  • $\begingroup$ Oh my... this was simpler than expected. Silly me. $\endgroup$
    – dearN
    Commented Jun 24, 2020 at 18:15
  • $\begingroup$ @dearN I think we all have those days; at least I do. :-) mathematica.stackexchange.com/a/71445/121 $\endgroup$
    – Mr.Wizard
    Commented Jun 24, 2020 at 18:16
4
$\begingroup$

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

enter image description here

Row[Graph[#, ImageSize -> 200] & /@ toEdges1 /@ data]

enter image description here

toEdges1 /@ data == 
    toEdges2 /@ data == 
     ( DirectedEdge @@@ Partition[#, 2, 1] & /@ data)
 True
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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