0

I am trying to integrate a nice looking control flow graph for an algorithm in one of my papers:

static void PrintEven(int x)
{
    int c = 0;
    while (c <= x)
    {
        if (c % 2 == 0)
            Console.WriteLine(c);
        c++;
    }
    Console.WriteLine("Done.");
}

I first did create the cfg with http://www.webgraphviz.com/ which looked horrible:

enter image description here

I was told to create the graph with tikz instead to make it look better. I was told this is the go to way to implement CFGs in a paper. By nice looking I mean a nice vertical structure, the entry node to be at the top and the exit node (The Console.WriteLine("Done")) at the bottom. Also if the arrows would not be rounded but instead look somewhat like in this picture that would be nice:

enter image description here

Can someone tell me how I can create a CFG for my algorithm with tikz?

1

2 Answers 2

3

This is not necessarily an answer but an attempt to steer the above discussion into a different direction. Flowchart type diagrams are abundant on this site. A nice example is given in this answer, a slight variation of which I add here. (Note that I am not a programmer, so it is quite possible that I use the wrong styles for the elements of that flow chart. This is an additional reason to make this a community wiki.)

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{positioning}
\usetikzlibrary{shapes.geometric}
\begin{document}
\begin{tikzpicture}[node distance=1cm,font=\sffamily,
    startstop/.style={rectangle, rounded corners, minimum width=3cm, minimum height=1cm,text centered, draw=black, fill=red!30},
    process/.style={rectangle, minimum width=3cm, minimum height=1cm, text centered, draw=black, fill=orange!30},
    io/.style={trapezium, trapezium left angle=70, trapezium right angle=110, minimum width=3cm, minimum height=1cm, text centered, draw=black, fill=blue!30},
    decision/.style={diamond, minimum width=3cm, minimum height=1cm, text centered, draw=black, fill=green!30},
    arr/.style={very thick,-latex}
    ]
    \node (int) [startstop]                             {\texttt{int c=0;}};
    \node (while) [process,below=of int]  {\texttt{while (c <= x)}};
    \draw[arr] (int) -- (while);
    \node (if) [decision,below=of while]        {\texttt{if (c \% 2 == 0)}};       
    \draw[arr] (while) -- (if);
    \node (write) [process,right=of if] {\texttt{Console.WriteLine(c);}};
    \draw[arr] (if) -- (write) node[midway,above]{yes};
    \node (pp) [process,below=of if]  {\texttt{c++;}};
    \draw[arr] (if) -- (pp) node[midway,left]{no};
    \draw[arr] (write) |- (pp);
    \draw[arr] (pp.west) -- ++ (-1,0) |- (while) node[pos=0.25,left]{$c\le x$};
\end{tikzpicture}
\end{document}

enter image description here

Here I also have "an alternative"... (... it seems that "alternative" means one copies from another post without mentioning it, keeps 90% and makes some small changes here and there. OK, this is an "alternative", then, ;-)

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{positioning,shapes.geometric}
\begin{document}
\begin{tikzpicture}[node distance = 9mm and 14mm,
    nodes= {draw, minimum width=8em, minimum height=2em,
                    font=\ttfamily},
startstop/.style = {fill=red!30, rounded corners},
  process/.style = {fill=orange!30},
       io/.style = {fill=blue!30,
                    trapezium, trapezium stretches body,
                    trapezium left angle=70, trapezium right angle=110},
 decision/.style = {fill=green!30, diamond, aspect=1.5},
 exp/.style={draw=none,font=\sffamily,minimum width=1em},
      arr/.style = {very thick,-latex}
    ]
\node (int) [io]    {int c=0;};
\node (while) [process,below=of int]    {while (c <= x)};
\draw[arr] (int) -- (while);
\node (if) [decision,below=of while]    {if (c \% 2 == 0)};
\draw[arr] (while) --  node[exp,right]  {$c\le x$} (if);
\node (write)   [process,below=of if]   {Console.WriteLine(c);};
\draw[arr] (if) --  (write) node[exp,pos=0.5,left] {yes};
\node (pp)      [process,below=of write]    {c++;};
\draw[arr] (write) -- (pp);
\draw[arr] (if.east)  --  ++ (1,0) node[exp,pos=0.5,above] {no} |- (pp) ;
\node (done)   [startstop,below right=of write]   {Console.WriteLine("Done");};
\draw[arr] (while) -| (done)   node[exp,pos=0.25,above] {$c> x$};
\draw[arr] (pp.west) -- ++ (-1,0) |- (while);
\end{tikzpicture}
\end{document}

enter image description here

2
  • Why should this be a Community Wiki?
    – CarLaTeX
    Jun 21, 2019 at 4:02
  • @CarLaTeX Because I was hoping that others would make some additions (instead of adding so-called "alternatives"), and because the main purpose was to give a hook to those who wanted to contribute if the question gets closed.
    – user121799
    Jun 21, 2019 at 5:08
3

An alternative solution based on my one old answer to the similar question. Compare to it the MWE below try to replicate your first image in your question:

enter image description here

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{positioning,
                shapes.geometric}

\begin{document}
    \begin{tikzpicture}[
   node distance = 8mm and 12mm,
     base/.style = {draw, fill=#1,
                    minimum width=28mm, minimum height=7mm,
                    font=\ttfamily},
startstop/.style = {base=red!30, rounded corners},
  process/.style = {base=orange!30},
       io/.style = {base=blue!30,
                    trapezium, trapezium stretches body,
                    trapezium left angle=70, trapezium right angle=110},
 decision/.style = {base=green!30, diamond, aspect=1.5},
      arr/.style = {semithick,-latex}
    ]
\node (int) [io]    {int c=0;};
\node (while) [process,below=of int]    {while (c <= x)};
    \draw[arr] (int) -- (while);
\node (if) [decision,below=of while]    {if (c \% 2 == 0)};
    \draw[arr] (while) --  node[right]  {$c\le x$} (if);
\node (write)   [process,below=of if]   {Console.WriteLine(c);};
    \draw[arr] (if) -- node[left] {yes} (write);
\node (pp)      [process,below=of write]    {c++;};
    \draw[arr] (write) -- (pp);
    \draw[arr] (if.east)  -- node[above] {no} ++ (1,0) |- (pp);
\node (done)   [startstop,
                below right=of write]   {Console.WriteLine("Done");};
    \draw[arr] (while) -| (done)   node[pos=0.25,above] {$c> x$};
    \draw[arr] (pp.west) -- ++ (-1,0) |- (while);
\end{tikzpicture}
\end{document}
3
  • There's a check box called "Community wiki" after the text box, but I think you can check it at the moment you posted the answer, not after. (P.S. = why this should be a Community Wiki?)
    – CarLaTeX
    Jun 21, 2019 at 4:00
  • "Your" old answer was also essentially copied from A Ellet's answer, and this one is to a very large extent copied from mine (which is of course based on A Ellet's answer, as I say). Wouldn't it be better if you told other users where you get "your" code from?
    – user121799
    Jun 21, 2019 at 14:53
  • @marmot, please cold down.
    – Zarko
    Jun 21, 2019 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.