0

I would like to get this: enter image description here

but this is what i got:

enter image description here

this is the code:

\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}

Does anyone know how I get the graph/automaton of the first figure? (with the edges of the graph closest)

1
  • Does one of the provided answers solve the problem? Apr 24, 2022 at 17:02

2 Answers 2

3

You can add a yshift to the appropriate nodes:

    \node (d) [above right=of c, yshift=-.375cm] {};
...
    \node (h) [below right=of c, yshift=.375cm] {};

enter image description here

Here is an alternative using forest, which automatically creates the spacing you want.

enter image description here

You can adjust the vertical spacing by changing s sep, and horizontal spacing by changing l sep. For example,

for tree={s sep=0cm, l sep=1cm, grow'=0, ...

will make the result longer and flatter.

\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

You can just use normal Tikz drawing commands to draw the lines in different places.

Here's an MWE (note the inclusion of preamble materials to make it easier for others to run this code).

\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}

And the resulting image: enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .