但这就是我得到的:
这是代码:
\begin{center}
\begin{tikzpicture}[
node distance=1.5cm,
every node/.style={draw, fill, circle, inner sep=1.102pt},
endnode/.style={fill=none, inner sep=5pt},
every edge/.style={draw, ->, shorten >=5pt, shorten <=5pt},
itslabel/.style={draw=none, fill=none, above=0pt, pos=0.5, font=\footnotesize},
]
% position the nodes a..d
\node (a) [draw=none,fill=none] {I};
\node (b) [right=of a] {};
\node (c) [above right=of b] {};
\node (d) [above right=of c] {};
\node (e) [right=of d] {};
\node (f) [right=of e] {};
\node (g) [right=of f] {};
\node (gend) [endnode] at (g) {};
\node (h) [below right=of c] {};
\node (i) [right=of h] {};
\node (j) [right=of i] {};
\node (k) [right=of j] {};
\node (l) [right=of k] {};
\node (m) [right=of l] {};
\node (mend) [endnode] at (m) {};
\node (n) [below right=of b] {};
\node (o) [right=of n] {};
\node (p) [right=of o] {};
\node (q) [right=of p] {};
\node (r) [right=of q] {};
\node (s) [right=of r] {};
\node (send) [endnode] at (s) {};
%[shorten >=1em] final
\path (a.center) edge (b)
(b) edge node [itslabel, pos=0.38, above=5pt] {\(\varepsilon\)} (c)
(c.center) edge node [itslabel, pos = 0.4, above = 0.1] {\(\varepsilon\)} (d)
(d) edge node [itslabel] {\(1\)} (e)
(e) edge node [itslabel] {\(\varepsilon\)} (f)
(f) edge [shorten >=1em] node [itslabel] {\(0\)} (g)
(c) edge node [itslabel, pos = 0.32, below = 0.2] {\(\varepsilon\)} (h)
(h) edge node [itslabel] {\(0\)} (i)
(i) edge [shorten >=1em] node [itslabel] {\(\varepsilon\)} (j)
(j) edge node [itslabel] {\(0\)} (k)
(k) edge node [itslabel] {\(\varepsilon\)} (l)
(l) edge [shorten >=1em] node [itslabel] {\(1\)} (m)
(b) edge node [itslabel, pos = 0.32, below = 0.2] {\(\varepsilon\)} (n)
(n) edge node [itslabel] {\(0\)} (o)
(o) edge node [itslabel] {\(\varepsilon\)} (p)
(p) edge node [itslabel] {\(1\)} (q)
(q) edge node [itslabel] {\(\varepsilon\)} (r)
(r) edge [shorten >=1em] node [itslabel] {\(0\)} (s);
\end{tikzpicture}
\end{center}
有人知道我如何得到第一个图的图形/自动机吗?(图形的边缘最接近)
答案1
yshift
您可以向适当的节点添加:
\node (d) [above right=of c, yshift=-.375cm] {};
...
\node (h) [below right=of c, yshift=.375cm] {};
这是使用 的替代方法forest
,它会自动创建您想要的间距。
您可以通过更改 来调整垂直间距s sep
,通过更改 来调整水平间距l sep
。例如,
for tree={s sep=0cm, l sep=1cm, grow'=0, ...
会使结果更长更平坦。
\documentclass{article}
\usepackage{forest}
\tikzset{lbl/.style={midway, font=\scriptsize, outer sep=-1pt}}
\newcommand{\e}{\varepsilon}
\begin{document}
\begin{forest}
for tree={grow'=0, edge=-stealth, outer sep=3pt, anchor=center, if n children=0{draw, circle, minimum size=4mm}{}}
[I, for descendants={inner sep=0pt, node contents=\textbullet}
[
[,edge label={node[lbl, above left]{$\e$}}
[,edge label={node[lbl, above left]{$\e$}}
[,edge label={node[lbl, above]{1}}
[,edge label={node[lbl, above]{$\e$}}
[,edge label={node[lbl, above]{0}}]]]]
[,edge label={node[lbl, below left]{$\e$}}
[,edge label={node[lbl, above]{0}}
[,edge label={node[lbl, above]{$\e$}}
[,edge label={node[lbl, above]{0}}
[,edge label={node[lbl, above]{$\e$}}
[,edge label={node[lbl, above]{1}}]]]]]]]
[,edge label={node[lbl, below left]{$\e$}}
[,edge label={node[lbl, above]{0}}
[,edge label={node[lbl, above]{$\e$}}
[,edge label={node[lbl, above]{1}}
[,edge label={node[lbl, above]{$\e$}}
[,edge label={node[lbl, above]{0}}]]]]]]]]
\end{forest}
\end{document}
答案2
您可以使用普通的 Tikz 绘图命令在不同位置绘制线条。
这是一个 MWE(请注意包含前言材料,以便其他人更容易运行此代码)。
\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{arrows,
arrows.meta,
calc,
positioning,
decorations.markings,
automata
}
\begin{document}
\begin{tikzpicture}[
node distance=1.5cm,
every node/.style={draw, fill, circle, inner sep=1.102pt},
endnode/.style={fill=none, inner sep=5pt},
every edge/.style={draw, ->, shorten >=5pt, shorten <=5pt},
itslabel/.style={draw=none, fill=none, above=0pt, pos=0.5, font=\footnotesize},
]
% position the nodes a..d
\node (a) [draw=none,fill=none] {I};
\node (b) [right=of a] {};
\node (c) [above right=of b] {};
\node (d) [above right=of c] {};
\node (e) [right=of d] {};
\node (f) [right=of e] {};
\node (g) [right=of f] {};
\node (gend) [endnode] at (g) {};
\node (h) [below right=of c] {};
\node (i) [right=of h] {};
\node (j) [right=of i] {};
\node (k) [right=of j] {};
\node (l) [right=of k] {};
\node (m) [right=of l] {};
\node (mend) [endnode] at (m) {};
\node (n) [below right=of b] {};
\node (o) [right=of n] {};
\node (p) [right=of o] {};
\node (q) [right=of p] {};
\node (r) [right=of q] {};
\node (s) [right=of r] {};
\node (send) [endnode] at (s) {};
%[shorten >=1em] final
\path (a.center) edge (b)
(b) edge node [itslabel, pos=0.38, above=5pt] {\(\varepsilon\)} (c)
(c.center) edge node [itslabel, pos = 0.4, above = 0.1] {\(\varepsilon\)} (d)
(d) edge node [itslabel] {\(1\)} (e)
(e) edge node [itslabel] {\(\varepsilon\)} (f)
(f) edge [shorten >=1em] node [itslabel] {\(0\)} (g)
(c) edge node [itslabel, pos = 0.32, below = 0.2] {\(\varepsilon\)} (h)
(h) edge node [itslabel] {\(0\)} (i)
(i) edge [shorten >=1em] node [itslabel] {\(\varepsilon\)} (j)
(j) edge node [itslabel] {\(0\)} (k)
(k) edge node [itslabel] {\(\varepsilon\)} (l)
(l) edge [shorten >=1em] node [itslabel] {\(1\)} (m)
(b) edge node [itslabel, pos = 0.32, below = 0.2] {\(\varepsilon\)} (n)
(n) edge node [itslabel] {\(0\)} (o)
(o) edge node [itslabel] {\(\varepsilon\)} (p)
(p) edge node [itslabel] {\(1\)} (q)
(q) edge node [itslabel] {\(\varepsilon\)} (r)
(r) edge [shorten >=1em] node [itslabel] {\(0\)} (s);
\draw[ ->, shorten >=5pt, shorten <=5pt] ($(g) + (0,.1cm)$)
-- ++(0,.3cm)
-| node [itslabel, pos=.2] {\(\varepsilon\)} (b);
\draw[ ->, shorten >=5pt, shorten <=5pt] ($(m) + (0,-.1cm)$)
-- ++(0,-2cm)
-| node [itslabel, pos=.2] {\(\varepsilon\)}
($(b) + (-.2cm,0)$);
\draw[ ->, shorten >=5pt, shorten <=5pt] ($(s) + (0,-.1cm)$)
-- ++(0,-.3cm)
-| node [itslabel, pos=.2] {\(\varepsilon\)} (b);
\end{tikzpicture}
\end{document}