TransWikia.com

Drawing finite-state automata without using VertexRenderingFunction

Mathematica Asked on April 29, 2021

In the tutorial entitled “Hierarchical Drawing of Directed Graphs” in Mathematica 12, there is an example of how to draw a graph with several properties I want, but it uses an option called VertexRenderingFunction which is superseded by VertexShapeFunction:

LayeredGraphPlot[{1 -> 2, 2 -> 3, 3 -> 4, 4 -> 5, 5 -> 6, 6 -> 7, 
  7 -> 8, 8 -> 1, 1 -> 9, 2 -> 9, 3 -> 10, 4 -> 10, 6 -> 11, 5 -> 11, 
  7 -> 12, 8 -> 12}, Left, 
 VertexRenderingFunction -> ({EdgeForm[Black], Yellow, Disk[#1, 0.2], 
     Black, Text[#2, #1]} &)]

Since I’m trying to draw a finite-state automaton, I want a graph with directed arrows between nodes, where the nodes are circles containing a small number (the state number), as well as labels on the edges. I’m only trying to display acyclic automata at this point.

Trying to get VertexShapeFunction to do these things somehow suppressed the directed arrows; what is the proper way to do this? Ideally there would be the usual notations for the start state and final states as well. Thanks

One Answer

You can use the default vertex shape (Disk) and use the option VertexLabels (that is, no need to use VertexShapeFunction):

edges = {1 -> 2, 2 -> 3, 3 -> 4, 4 -> 5, 5 -> 6, 6 -> 7, 7 -> 8, 8 -> 1, 
    1 -> 9, 2 -> 9, 3 -> 10, 4 -> 10, 6 -> 11, 5 -> 11, 7 -> 12, 8 -> 12};

LayeredGraphPlot[edges, Left, 
  VertexLabels -> Placed["Name", Center], 
  VertexStyle -> Yellow, 
  VertexSize -> Large]

enter image description here

If you need to use VertexShapeFunction you can add the option PerformanceGoal -> "Quality":

LayeredGraphPlot[edges, Left,  
   VertexShapeFunction -> ({EdgeForm[Black], Yellow, Disk[#1, 0.2], Black, Text[#2, #1]}&),
   PerformanceGoal -> "Quality"]

enter image description here

Update: Using DiscreteMarkovProcess with Graph

dmp = DiscreteMarkovProcess[1, Graph[edges]];

Absorbing and transient nodes are automatically styled when dmp is used as the first argument of Graph:

g1 = SimpleGraph @ Graph[dmp, 
  GraphLayout -> {"LayeredDigraphEmbedding", "Orientation" -> Left}]

enter image description here

You can change the style of any node (say, vertex 1) using SetProperty:

SetProperty[g1, VertexStyle -> {1 -> Red}]

enter image description here

Correct answer by kglr on April 29, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP