# Draw an algorithm graph with Tikz

I would like to draw an algorithm diagram with Tikz. In this diagram, I would like to show which subroutine call which other one. And also that the diagram could be read from the upper left to the down right.

I've looked on the web for a good way to draw this kind of algorithm diagram (as I want) and found this : I glanced at the pgf documentation and performed a few tests :

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{graphs}

\begin{document}

\begin{tikzpicture}[every node/.style = draw]
\graph [grow right sep, left anchor=south, right anchor=west,
grow down=7mm,grow right=7mm]{
root -> {
child 1,
child 2 -> {
grand child 1,
grand child 2
},
child 3 -> {
grand child 3
}
}
};
\end{tikzpicture}

\end{document}


Honestly, I'm not familiar at all with this library and graph options. But I'm sure that they have to be modified to create a diagram as the example I've given (see the link above).

Does anyone have an idea how to do this ? Could you please give some advice about the graph options I need to fix ?

Basic framework: http://www.texample.net/tikz/examples/filesystem-tree/

Have a look to 21. Making trees grow in PGF Man­ual.

The solution is my first try with it, so its not perfect.

You can create nodes and childs. The distance between the levels can defined with level distance=<length>. Connection between node and child it set with edge from parent path={[->] ([xshift=.5em]\tikzparentnode.south west) |- (\tikzchildnode.west)}]. (The xshift is optional). You can also add dashed.

The child position is set with grow via three points={...} have a look to chapter 72.1 Growth Functions in PGF Man­ual. For space/ blank childs you can add child [missing] {}.

Optional you can set the nodes also to dashed and not solid or something else. MWE:

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{trees}
\begin{document}
\tikzstyle{every node}=[draw=black,thick,anchor=west]
\begin{tikzpicture}[%
parent anchor=south west,
level distance=40mm,
grow via three points={one child at (0.1,-1) and
two children at (0.1,-1) and (0.1,-2)},
%edge from parent path={(\tikzparentnode.south) |- (\tikzchildnode.west)}]
edge from parent path={[->] ([xshift=.5em]\tikzparentnode.south west) |- (\tikzchildnode.west)}]
\node { \_start}
child { node {main}
child[%
grow via three points={one child at (0.1,-1) and
two children at (0.1,-1) and (0.1,-2)},
edge from parent path={[->] ([xshift=.5em]\tikzparentnode.south         west) |- (\tikzchildnode.west)}] { node {\_\_function}
child[%
grow via three points={one child at (-0.5,-1) and
two children at (-0.5,-1) and (-1.2,-1)},
edge from parent path={[->] ([xshift=.5em]\tikzparentnode.south west) |- (\tikzchildnode.west)}] { node {\_\_mt\_MasterFunction\_}
child[%
grow=right, edge from parent path={[->,dashed] (\tikzparentnode.east) |- (\tikzchildnode.west)}] { node {\_thread\_start}
child[%
grow via three points={one child at (-0.5,-1) and
two children at (-0.5,-1) and (-0.5,-2)},
edge from parent path={[->,solid] ([xshift=.5em]\tikzparentnode.south west) |- (\tikzchildnode.west)}] { node[solid] {\_\_mt\_SlaveFunction}
child { node {\_\_mt\_WaitForWork}}
child { node {\_\_mt\_run\_my\_job}
child { node {\_\_mt\_runLoop\_int\_}
child { node {loop body function}}
}
}
child [missing] {}
child [missing] {}
}
}
child { node {\_\_mt\_run\_my\_job\_}
child { node {\_\_mt\_runloop\_int\_}
child { node {loop body function}}
}
}
}
child [missing] {}
child [missing] {}
child [missing] {}
}
};
\end{tikzpicture}
\end{document}

• This is a wonderful work which perfectly fits the image I've given in my post. But in my point of view, this code is not enough automated for the general user. Here, he has to define child [missing] {} and specify the children position for each new level via the grow via three points command. Do you think that these specifications could be avoided in the area of the TikZ environment where the algorithm is detailed ? – J. Martin Mar 10 '17 at 10:46
• Possible, but i don't know. The solution was my first try. I'm no expert in doing this. Positive and constructive comments are welcome. – Bobyandbob Mar 10 '17 at 10:56

I've tried the package fast-diagram proposed by rpapa which works well for me and is very easy to use.

In particular, the user has only to define :

• the name of routines and sub-routines
• and the inheritance of each subroutine.

Besides, it permits to define your own boxes and to modify the color/size/length/etc of the existing boxes.

MWE :

\documentclass[border=10pt]{standalone}

\usepackage[french]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{babel}

\usepackage{fast-diagram}

\begin{document}

\renewcommand{\fastLargeurBoite}{11em}
\renewcommand{\fastEspaceColonne}{14em}

\begin{fast}{\_start}
\fastFT{main}{%
\fastFT{function}{%
\fastFT{\_\_mt\_MasterFunction}{%
\fastFT{\_\_mt\_SlaveFunction\_}{%
\fastFT{\_\_mt\_WaitForWork\_}{}
\fastFT{\_\_mt\_run\_my\_job\_}{%
\fastFT{\_\_mt\_runLoop\_int\_}{
\fastFT{\textit{loop body function}}{}}} 