How to draw the following automaton

How does one draw the elliptical blue background and the nodes? I am quite lost here.

• My suggestion is that you se the "dot language" to create the automaton and then use the well known script dot2tex to export (indeed you can also convert from dot to pdf). Take a look at martin-thoma.com/how-to-draw-a-finite-state-machine Mar 4 '16 at 19:31
• I can see that's an advantage if you already know the language @boumol, but otherwise, that seems to introduce a quite unnecessary level of complexity.
– cfr
Mar 4 '16 at 22:00

You can use children for the parent nodes. The labels can be appended when connecting the nodes. Something to get you started:

\documentclass{standalone}
\usepackage{tikz}

\usetikzlibrary{shapes,backgrounds,fit}

\begin{document}
\tikzset{every node/.style={fill=yellow,draw=black,circle},
myedgestyle/.style={edge from parent/.style={thick,draw,->}}
}
\begin{tikzpicture}[level 1/.style={sibling distance=4cm},
level 2/.style={sibling distance=2cm}]
\node (parent) {1}
child[myedgestyle] {node (child1) {2}
child {node (child2) {4} edge from parent node[left,draw=none,fill=none]{c}}
child {node (child3) {5} edge from parent node[right,draw=none,fill=none]{d}}
edge from parent node[left,draw=none,fill=none]{a}}
child[myedgestyle] {node (child4) {3}
child {node (child5) {6} edge from parent node[left,draw=none,fill=none]{e}}
child {node (child6) {7} edge from parent node[right,draw=none,fill=none]{f}}
edge from parent node[right,draw=none,fill=none]{b}};

% For the cyan-coloured circles and ellipses behind the nodes and children
\begin{pgfonlayer}{background}
\filldraw[cyan] (parent.center) circle (5mm);
\filldraw[cyan] (child1.center) circle (5mm);
\filldraw[cyan] (child4.center) circle (5mm);
\node[shape=ellipse,cyan,inner sep=0pt,fit={(child2) (child3)}] {};
\node[shape=ellipse,cyan,inner sep=0pt,fit={(child5) (child6)}] {};
\end{pgfonlayer}
\end{tikzpicture}
\end{document}


The edge from parent node option has to be provided for every child. I couldn't find a way to predefine it in \tikzset. For a parent node, edge for parent node has to be written after the edge labels have been defined for its children.

I still haven't found a way to implement the background blue colour. It might have to do something with the backgrounds tikzlibrary.

EDIT: So using this link, we can call the fit library to get the ellipses for the child nodes. The backgrounds library makes sure we get the shapes after creating the nodes.

• I added a class so your code can be compiled. If you intended a different one, you should edit it. Also, I removed a stray ; and added an image of the output. Feel free to roll-back if you object at all.
– cfr
Mar 4 '16 at 21:56
• @smike This seems really helpful. However, I made a change. \node[shape=ellipse,cyan,inner sep=0pt,fit={(child2) (child3)}] {}; Mar 5 '16 at 3:06
• Suppose I want two edges out of the same node, say, an edge labelled "x" from node 1 to node 2. How do I do that? Mar 5 '16 at 3:07
• @cfr Thank you for the edit! I had to include the result graph, but wasn't sure exactly how (this is my first time answering on SE!). @GermanShepherd From the PGF manual, on page 325, it shows how to define custom edge from parent macros. I think that if there we change the path to \tikzchildnode\tikzchildanchor -- #2 \tikzparentnode\tikzparentanchor, then we can get a new edge from node 1 to node 2. In addition to that, we just need to make another edge from parent node label. Mar 5 '16 at 14:12
• @GermanShepherd I have so far not been able to define the macro correctly. I am not sure how to include it. Using edge from parent macro=mymacro in \tikzset didn't help. Mar 5 '16 at 14:13