Debian Codegraph @

texmf-dist/doc/generic/pgf/pgfmanual-en-library-automata.tex

raw
1 % Copyright 2019 by Till Tantau
2 %
3 % This file may be distributed and/or modified
4 %
5 % 1. under the LaTeX Project Public License and/or
6 % 2. under the GNU Free Documentation License.
7 %
8 % See the file doc/generic/pgf/licenses/LICENSE for more details.
9
10
11 \section{Automata Drawing Library}
12
13 \begin{tikzlibrary}{automata}
14 This packages provides shapes and styles for drawing finite state automata
15 and Turing machines.
16 \end{tikzlibrary}
17
18
19 \subsection{Drawing Automata}
20
21 The |automata| (drawing) library is intended to make it easy to draw finite
22 automata and Turing machines. It does not cover every situation imaginable, but
23 most finite automata and Turing machines found in text books can be drawn in a
24 nice and convenient fashion using this library.
25
26 To draw an automaton, proceed as follows:
27 %
28 \begin{enumerate}
29 \item For each state of the automaton, there should be one node with the
30 option |state|.
31 \item To place the states, you can either use absolute positions or
32 relative positions, using options like |above| or |right|.
33 \item Give a unique name to each state node.
34 \item Accepting and initial states are indicated by adding the options
35 |accepting| and |initial|, respectively, to the state nodes.
36 \item Once the states are fixed, the edges can be added. For this, the
37 |edge| operation is most useful. It is, however, also possible to add
38 edges after each node has been placed.
39 \item For loops, use the |edge [loop]| operation.
40 \end{enumerate}
41
42 Let us now see how this works for a real example. Let us consider a
43 nondeterministic four state automaton that checks whether an input contains the
44 sequence $0^*1$ or the sequence $1^*0$.
45 %
46 \begin{codeexample}[preamble={\usetikzlibrary{automata,positioning}}]
47 \begin{tikzpicture}[shorten >=1pt,node distance=2cm,on grid,auto]
48 \draw[help lines] (0,0) grid (3,2);
49
50 \node[state,initial] (q_0) {$q_0$};
51 \node[state] (q_1) [above right=of q_0] {$q_1$};
52 \node[state] (q_2) [below right=of q_0] {$q_2$};
53 \node[state,accepting](q_3) [below right=of q_1] {$q_3$};
54
55 \path[->] (q_0) edge node {0} (q_1)
56 edge node [swap] {1} (q_2)
57 (q_1) edge node {1} (q_3)
58 edge [loop above] node {0} ()
59 (q_2) edge node [swap] {0} (q_3)
60 edge [loop below] node {1} ();
61 \end{tikzpicture}
62 \end{codeexample}
63
64
65 \subsection{States With and Without Output}
66
67 The |state| style actually just ``selects'' a default underlying style. Thus,
68 you can define multiple new complicated state style and then simply set the
69 |state| style to your given style to get the desired kind of styles.
70
71 By default, the following state styles are defined:
72 %
73 \begin{stylekey}{/tikz/state without output}
74 This node style causes nodes to be drawn as circles. Also, this style calls
75 |every state|.
76 \end{stylekey}
77
78 \begin{stylekey}{/tikz/state with output}
79 This node style causes nodes to be drawn as split circles, that is, using
80 the |circle split| shape. In the upper part of the shape you have the name
81 of the style, in the lower part the output is placed. To specify the
82 output, use the command |\nodepart{lower}| inside the node. This style also
83 calls |every state|.
84 %
85 \begin{codeexample}[preamble={\usetikzlibrary{automata}}]
86 \begin{tikzpicture}
87 \draw[help lines] (0,0) grid (3,2);
88
89 \node[state without output] {$q_0$};
90
91 \node[state with output] at (2,0) {$q_1$ \nodepart{lower} $00$};
92 \end{tikzpicture}
93 \end{codeexample}
94 %
95 \end{stylekey}
96
97 \begin{stylekey}{/tikz/state (initially state without output)}
98 You should redefine it to something else, if you wish to use states of a
99 different nature.
100 %
101 \begin{codeexample}[preamble={\usetikzlibrary{automata}}]
102 \begin{tikzpicture}[state/.style=state with output]
103 \node[state] {$q_0$ \nodepart{lower} $11$};
104 \node[state] at (2,0) {$q_1$ \nodepart{lower} $00$};
105 \end{tikzpicture}
106 \end{codeexample}
107 %
108 \end{stylekey}
109
110 \begin{stylekey}{/tikz/every state (initially \normalfont empty)}
111 This style is used by |state with output| and also by
112 |state without output|. By default, it does nothing, but you can use it to
113 make your state look more fancy:
114 %
115 \begin{codeexample}[preamble={\usetikzlibrary{arrows.meta,automata,positioning}}]
116 \begin{tikzpicture}[shorten >=1pt,node distance=2cm,on grid,>={Stealth[round]},
117 every state/.style={draw=blue!50,very thick,fill=blue!20}]
118
119 \node[state,initial] (q_0) {$q_0$};
120 \node[state] (q_1) [above right=of q_0] {$q_1$};
121 \node[state] (q_2) [below right=of q_0] {$q_2$};
122
123 \path[->] (q_0) edge node [above left] {0} (q_1)
124 edge node [below left] {1} (q_2)
125 (q_1) edge [loop above] node {0} ()
126 (q_2) edge [loop below] node {1} ();
127 \end{tikzpicture}
128 \end{codeexample}
129 %
130 \end{stylekey}
131
132
133 \subsection{Initial and Accepting States}
134
135 The styles |initial| and |accepting| are similar to the |state| style as they
136 also just select an ``underlying'' style, which installs the actual settings
137 for initial and accepting states.
138
139 Let us start with the initial states.
140 %
141 \begin{stylekey}{/tikz/initial (initially initial by arrow)}
142 This style is used to draw initial states.
143 \end{stylekey}
144
145 \begin{stylekey}{/tikz/initial by arrow}
146 This style causes an arrow and, possibly, some text to be added to the
147 node. The arrow points from the text to the node. The node text and the
148 direction and the distance can be set using the following key:
149 %
150 \begin{key}{/tikz/initial text=\meta{text} (initially start)}
151 This key sets the text to be used. Use an empty text to suppress all
152 text.
153 \end{key}
154 %
155 \begin{key}{/tikz/initial where=\meta{direction} (initially left)}
156 Set the place where the text should be shown. Allowed values are
157 |above|, |below|, |left|, and |right|.
158 \end{key}
159 %
160 \begin{key}{/tikz/initial distance=\meta{distance} (initially 3ex)}
161 Sets the length of the arrow leading from the text to the state node.
162 \end{key}
163 %
164 \begin{stylekey}{/tikz/every initial by arrow (initially \normalfont empty)}
165 This style is executed at the beginning of every path that contains the
166 arrow and the text. You can use it to, say, make the text red or
167 whatever.
168 \end{stylekey}
169 %
170 \begin{codeexample}[preamble={\usetikzlibrary{automata}}]
171 \begin{tikzpicture}[every initial by arrow/.style={text=red,->>}]
172 \node[state,initial,initial distance=2cm] {$q_0$};
173 \end{tikzpicture}
174 \end{codeexample}
175 %
176 \end{stylekey}
177
178 \begin{stylekey}{/tikz/initial above}
179 This is a shorthand for |initial by arrow,initial where=above|.
180 \end{stylekey}
181
182 \begin{stylekey}{/tikz/initial below}
183 Works similarly to the previous option.
184 \end{stylekey}
185
186 \begin{stylekey}{/tikz/initial left}
187 Works similarly to the previous option.
188 \end{stylekey}
189
190 \begin{stylekey}{/tikz/initial right}
191 Works similarly to the previous option.
192 \end{stylekey}
193
194 \begin{stylekey}{/tikz/initial by diamond}
195 This style uses a diamond to indicate an initial node.
196 \end{stylekey}
197
198 For the accepting states, the situation is similar: There is also an
199 |accepting| style that selects the way accepting states are rendered. There are
200 now two options: First, |accepting by arrow|, which works the same way as
201 |initial by arrow|, only with the direction of arrow reversed, and
202 |accepting by double|, where accepting states get a double line around them.
203
204 \begin{stylekey}{/tikz/accepting (initially accepting by double)}
205 This style is used to draw accepting states. You can replace this by the
206 style |accepting by arrow| to get accepting states with an arrow leaving
207 them.
208 \end{stylekey}
209
210 \begin{stylekey}{/tikz/accepting by double}
211 This style causes a double line to be drawn around a state.
212 \end{stylekey}
213
214 \begin{stylekey}{/tikz/accepting by arrow}
215 This style causes an arrow and, possibly, some text to be added to the
216 node. The arrow points to the text from the node.
217
218 The same options as for initial states can be used, only with |initial|
219 replaced by |accepting|:
220 %
221 \begin{key}{/tikz/accepting text=\meta{text} (initially \normalfont empty)}
222 This key sets the text to be used.
223 \end{key}
224 %
225 \begin{key}{/tikz/accepting where=\meta{direction} (initially right)}
226 Set the place where the text should be shown. Allowed values are
227 |above|, |below|, |left|, and |right|.
228 \end{key}
229 %
230 \begin{key}{/tikz/initial distance=\meta{distance} (initially 3ex)}
231 Sets the length of the arrow leading from the text to the state
232 node.
233 \end{key}
234 %
235 \begin{stylekey}{/tikz/every accepting by arrow (initially \normalfont empty)}
236 Executed at the beginning of every path that contains the arrow and the
237 text.
238 \end{stylekey}
239 %
240 \begin{codeexample}[preamble={\usetikzlibrary{arrows.meta,automata,positioning}}]
241 \begin{tikzpicture}
242 [shorten >=1pt,node distance=2cm,on grid,>={Stealth[round]},initial text=,
243 every state/.style={draw=blue!50,very thick,fill=blue!20},
244 accepting/.style=accepting by arrow]
245
246 \node[state,initial] (q_0) {$q_0$};
247 \node[state] (q_1) [above right=of q_0] {$q_1$};
248 \node[state] (q_2) [below right=of q_0] {$q_2$};
249 \node[state,accepting](q_3) [below right=of q_1] {$q_3$};
250
251 \path[->] (q_0) edge node [above left] {0} (q_1)
252 edge node [below left] {1} (q_2)
253 (q_1) edge node [above right] {1} (q_3)
254 edge [loop above] node {0} ()
255 (q_2) edge node [below right] {0} (q_3)
256 edge [loop below] node {1} ();
257 \end{tikzpicture}
258 \end{codeexample}
259 %
260 \end{stylekey}
261
262 \begin{stylekey}{/tikz/accepting above}
263 This is a shorthand for |accepting by arrow,accepting where=above|.
264 \end{stylekey}
265
266 \begin{stylekey}{/tikz/accepting below}
267 Works similarly to the previous option.
268 \end{stylekey}
269
270 \begin{stylekey}{/tikz/accepting left}
271 Works similarly to the previous option.
272 \end{stylekey}
273
274 \begin{stylekey}{/tikz/accepting right}
275 Works similarly to the previous option.
276 \end{stylekey}
277
278
279 \subsection{Examples}
280
281 In the following example, we once more typeset the automaton presented in the
282 previous sections. This time, we use the following rule for accepting/initial
283 state: Initial states are red, accepting states are green, and normal states
284 are orange. Then, we must find a path from a red state to a green state.
285 %
286 \begin{codeexample}[preamble={\usetikzlibrary{arrows.meta,automata,positioning,shadows}}]
287 \begin{tikzpicture}
288 [shorten >=1pt,node distance=2cm,on grid,>={Stealth[round]},thick,
289 every state/.style={fill,draw=none,orange,text=white,circular drop shadow},
290 accepting/.style ={green!50!black,text=white},
291 initial/.style ={red,text=white}]
292
293 \node[state,initial] (q_0) {$q_0$};
294 \node[state] (q_1) [above right=of q_0] {$q_1$};
295 \node[state] (q_2) [below right=of q_0] {$q_2$};
296 \node[state,accepting](q_3) [below right=of q_1] {$q_3$};
297
298 \path[->] (q_0) edge node [above left] {0} (q_1)
299 edge node [below left] {1} (q_2)
300 (q_1) edge node [above right] {1} (q_3)
301 edge [loop above] node {0} ()
302 (q_2) edge node [below right] {0} (q_3)
303 edge [loop below] node {1} ();
304 \end{tikzpicture}
305 \end{codeexample}
306
307 The next example is the current candidate for the five-state busiest beaver:
308 %
309 \begin{codeexample}[preamble={\usetikzlibrary{arrows.meta,automata,positioning}}]
310 \begin{tikzpicture}[->,>={Stealth[round]},shorten >=1pt,
311 auto,node distance=2cm,on grid,semithick,
312 inner sep=2pt,bend angle=45]
313 \node[initial,state] (A) {$q_a$};
314 \node[state] (B) [above right=of A] {$q_b$};
315 \node[state] (D) [below right=of A] {$q_d$};
316 \node[state] (C) [below right=of B] {$q_c$};
317 \node[state] (E) [below=of D] {$q_e$};
318
319 \path [every node/.style={font=\footnotesize}]
320 (A) edge node {0,1,L} (B)
321 edge node {1,1,R} (C)
322 (B) edge [loop above] node {1,1,L} (B)
323 edge node {0,1,L} (C)
324 (C) edge node {0,1,L} (D)
325 edge [bend left] node {1,0,R} (E)
326 (D) edge [loop below] node {1,1,R} (D)
327 edge node {0,1,R} (A)
328 (E) edge [bend left] node {1,0,R} (A);
329 \end{tikzpicture}
330 \end{codeexample}
331
332
333 %%% Local Variables:
334 %%% mode: latex
335 %%% TeX-master: "pgfmanual-pdftex-version"
336 %%% End: