[Tex/LaTex] Draw an algorithm graph with Tikz

algorithmstikz-graphstikz-pgf

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 :

descriptive figure

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 ?

Best Answer

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.

enter image description here

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\_EndOfTaskBarrier\_}}
            }
            }
                child { node {\_\_mt\_run\_my\_job\_}
                    child { node {\_\_mt\_runloop\_int\_}
                        child { node {loop body function}}                      
                    }   
                }
            }
            child [missing] {}
            child [missing] {}
            child [missing] {}  
            child { node {\_\_mt\_EndOfTaskBarrier\_}}
        }
    };  
\end{tikzpicture}
\end{document}
Related Question