5

I'm trying to draw control flow graph of a program using Tikz and want to render a output like the image below But I'm unable to draw the curved edges in the given figure.

enter image description here

The Code used for the same(but for different program)

\begin{figure}    
\begin{tikzpicture}
[%
    ->,
    shorten >=2pt,
    >=stealth,
    node distance=1cm,
    noname/.style={%
      rounded corners=0.5em,
      minimum width=5em,
      minimum height=3em,
      draw,
    }
  ]
    \node[noname] (1)                                             {$Start : P$};
    \node[noname] (2) [below=of 1]                                {$c=3$};
    \node[noname] (3) [node distance=1cm and 3mm,below left=of 2] {$b = a*b$};
    \node[noname] (4) [node distance=1cm and 3mm,below right=of 2]{print $a*c$};
    \node[noname] (5) [node distance=3cm,below=of 4]              {$a=b*c$};
    \node[noname] (6) [below=of 3]                                {print $a*b$};
    \node[noname] (7) [below=of 6]                                {$Call : P$};
    \node[noname] (8) [node distance=1cm,below=of 7]              {$Return : P$};
    \node[noname] (9) [below right=of 8]                          {$Exits : P$};
    \node[noname] (10) [node distance=6cm,right =of 1]            {$Start : main$};
    \node[noname] (11) [below=of 10]                              {$a = 3$};    
    \node[noname] (12) [below=of 11]                              {$b = a*b$};    
    \node[noname] (13) [below=of 12]                              {$Call : P$};    
    \node[noname] (14) [below=of 13]                              {$Return : P$};    
    \node[noname] (15) [below=of 14]                              {$c = a*b$};    
    \node[noname] (16) [below=of 15]                              {$Exit : main$};    
    \path (1) edge                   node {} (2)
          (2) edge                   node {} (3)
          (2) edge                   node {} (4)
          (3) edge                   node {} (6)
          (4) edge                   node {} (5)
          (6) edge                   node {} (7)
          (7) edge                   node {} (8)
          (8) edge                   node {} (9)
          (5) edge [bend left = 2em]                  node {} (9)
          (10) edge                   node {} (11)
          (11) edge                   node {} (12)
          (12) edge                   node {} (13)
          (13) edge                   node {} (14)
          (14) edge                   node {} (15)
          (15) edge                   node {} (16)
          (13) edge [bend right = 1.4em]                   node {} (1)
          (9) edge  [bend left = 2.1em]                   node {} (14)
\end{tikzpicture}
\caption{Supergraph for given program} 
\label{fig:1}
\end{figure}

But it outputs image like this:

This is a cfg for a different program

How can I change the edges from node Call : P to node Start : P and node Exit : P to node Return : P to the one drawn in the desired figure above

2
  • 1
    Hi, welcome. Are you just referring to the line style (add dotted to edge option), or about modifying the direction the arrows enter/leave the nodes? May 22, 2020 at 12:02
  • How to modify the direction of the arrows entering/leaving the nodes ?
    – mehul jain
    May 22, 2020 at 12:06

2 Answers 2

4

You can use the in and out options for the edge, which lets you define the angles at which the path hits the nodes. For example out=45 means the path goes up to the right from the start node, while in=45 means the path comes in from above right of the end node.

dotted will make the line dotted.

Hence, if you change the two last edges in your diagram to

      (13) edge [out=230,in=45,dotted]                   node {} (1)
      (9) edge  [out=-30,in=180,dotted]                   node {} (14);

you get:

enter image description here

Complete code below. (You don't need to add empty nodes for all the edges of course, so I removed the node{}.)

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
[%
    ->,
    shorten >=2pt,
    >=stealth,
    node distance=1cm,
    noname/.style={%
      rounded corners=0.5em,
      minimum width=5em,
      minimum height=3em,
      draw,
    }
  ]
    \node[noname] (1)                                             {$Start : P$};
    \node[noname] (2) [below=of 1]                                {$c=3$};
    \node[noname] (3) [node distance=1cm and 3mm,below left=of 2] {$b = a*b$};
    \node[noname] (4) [node distance=1cm and 3mm,below right=of 2]{print $a*c$};
    \node[noname] (5) [node distance=3cm,below=of 4]              {$a=b*c$};
    \node[noname] (6) [below=of 3]                                {print $a*b$};
    \node[noname] (7) [below=of 6]                                {$Call : P$};
    \node[noname] (8) [node distance=1cm,below=of 7]              {$Return : P$};
    \node[noname] (9) [below right=of 8]                          {$Exits : P$};
    \node[noname] (10) [node distance=6cm,right =of 1]            {$Start : main$};
    \node[noname] (11) [below=of 10]                              {$a = 3$};    
    \node[noname] (12) [below=of 11]                              {$b = a*b$};    
    \node[noname] (13) [below=of 12]                              {$Call : P$};    
    \node[noname] (14) [below=of 13]                              {$Return : P$};    
    \node[noname] (15) [below=of 14]                              {$c = a*b$};    
    \node[noname] (16) [below=of 15]                              {$Exit : main$};    
    \path (1) edge                   (2)
          (2) edge                   (3)
          (2) edge                   (4)
          (3) edge                   (6)
          (4) edge                   (5)
          (6) edge                   (7)
          (7) edge                   (8)
          (8) edge                   (9)
          (5) edge [bend left = 2em]                  (9)
          (10) edge                   (11)
          (11) edge                   (12)
          (12) edge                   (13)
          (13) edge                   (14)
          (14) edge                   (15)
          (15) edge                   (16)
          (13) edge [out=230,in=45,dotted]                   (1)
          (9) edge  [out=-30,in=180,dotted]                   (14);
\end{tikzpicture}
\end{document}
1
  • 1
    Thanks a lot !!
    – mehul jain
    May 22, 2020 at 12:19
2

Use .. controls <first control point> and <second control point> .. <end point>

\begin{tikzpicture}
[%
    ->,
    shorten >=2pt,
    >=stealth,
    node distance=1cm,
    noname/.style={%
      rounded corners=0.5em,
      minimum width=5em,
      minimum height=3em,
      draw,
    }
  ]
    \node[noname] (1)                                             {$Start : P$};
    \node[noname] (2) [below=of 1]                                {$c=3$};
    \node[noname] (3) [node distance=1cm and 3mm,below left=of 2] {$b = a*b$};
    \node[noname] (4) [node distance=1cm and 3mm,below right=of 2]{print $a*c$};
    \node[noname] (5) [node distance=3cm,below=of 4]              {$a=b*c$};
    \node[noname] (6) [below=of 3]                                {print $a*b$};
    \node[noname] (7) [below=of 6]                                {$Call : P$};
    \node[noname] (8) [node distance=1cm,below=of 7]              {$Return : P$};
    \node[noname] (9) [below right=of 8]                          {$Exits : P$};
    \node[noname] (10) [node distance=6cm,right =of 1]            {$Start : main$};
    \node[noname] (11) [below=of 10]                              {$a = 3$};    
    \node[noname] (12) [below=of 11]                              {$b = a*b$};    
    \node[noname] (13) [below=of 12]                              {$Call : P$};    
    \node[noname] (14) [below=of 13]                              {$Return : P$};    
    \node[noname] (15) [below=of 14]                              {$c = a*b$};    
    \node[noname] (16) [below=of 15]                              {$Exit : main$};    
    \draw (1) edge (2)
          (2) edge (3)
          (2) edge (4)
          (3) edge (6)
          (4) edge (5)
          (6) edge (7)
          (7) edge (8)
          (8) edge (9)
          (5) edge [bend left = 2em] (9)
          (10) edge (11)
          (11) edge (12)
          (12) edge (13)
          (13) edge (14)
          (14) edge (15)
          (15) edge (16);
    \draw[->] (13) .. controls +(left:4cm) and +(right:4cm) .. (1);
    \draw[->] (9)[anchor=east] .. controls +(right:3cm) and +(left:5cm) .. (14);
\end{tikzpicture}
1
  • Thanks a lot ..!!
    – mehul jain
    May 22, 2020 at 12:55

You must log in to answer this question.

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