1

I used the following latex code to draw this digraph.

\documentclass[12pt]{article}
\usepackage{pgf, tikz}
\usetikzlibrary{arrows, automata}
\begin{document}
\begin{tikzpicture}
       [> = stealth,
        shorten > = 1pt, 
        auto,
        node distance = 2cm, 
        thick ]

    \tikzstyle{every state}=[
        draw = black,
        thick,
        fill = white,
        minimum size = 2mm]

    \node[state] (v1) {$v_1$};
     \node[state] (v2) [right of=v1] {$v_2$};
       \path[->] (v2) edge node {$1$} (v1);
      \node[state] (v3) [right of=v2] {$v_3$};
        \path[->] (v3) edge node {$1$} (v2);
      \node[state] (v4) [right of=v3] {$v_4$};
       \path[->] (v4) edge node {$1$} (v3);
     \node[state] (v5) [right of=v4] {$v_5$};
       \path[->] (v5) edge node {$1$} (v4);
     \node[state] (v6) [right of=v5] {$v_6$};
       \path[->] (v6) edge node {$1$} (v5);

         \path (v1) edge [loop left] node {$u_1$} (v1);
        \path[->] (v1) edge[bend left=30] node {$u_2$}  (v2);
         \path[->] (v1) edge[bend left=40] node  {$u_3$}  (v3);
          \path[->] (v1) edge[bend left=50] node {$u_4$}  (v4);
           \path[->] (v1) edge[bend left=55] node {$u_5$}  (v5);
            \path[->] (v1) edge[bend left=60] node  {$u_6$}  (v6);

   \end{tikzpicture}
   \end{document}

I would greatly appreciate to improve my latex code such that the output of code be similar to the mentioned digraph. In fact my problems are the size of circles that want to be smaller and the positions of $u_i$’s and the loop for the node v_1.

Thanks for any suggestion.

3

I like to use the quotes library to make edge labels, because it offers the syntax (source) edge["label"] (target) which is shorter than (source) edge node {label} (target). The option to control the position of the label is pos; I think pos=0.8 leads to reasonable output, but you could fiddle with it yourself. I also used a \foreach loop to simplify your first bit of code.

\documentclass[12pt]{article}
\usepackage{pgf, tikz}
\usetikzlibrary{arrows, automata,quotes} % quotes library adds quotes syntax
\begin{document}
\begin{tikzpicture}
       [> = stealth,
        shorten > = 1pt, 
        auto,
        node distance = 2cm, 
        thick , 
        % automatically put nodes in math mode:
        execute at begin node=$,execute at end node=$ 
]

    \tikzset{every state/.style={thick,minimum size = 2mm}}

  \node[state] (v1) {v_1};  
    % Use a loop to make the states. \lastn holds \n minus 1 
  \foreach \n[remember=\n as \lastn (initially 1)] in {2,...,6}{
         \node[state] (v\n) [right of=v\lastn] {v_\n};
         \draw[<-] (v\lastn) edge["1"'] (v\n);
    }


        \draw (v1) edge [loop left,"u_1"]  (v1);
  % Put all the rest of the labels 8/10ths of the way across the edge
    \tikzset{every edge quotes/.style={pos=0.8}}
        \draw[->] (v1) edge[bend left=30,"u_2" ] (v2);
        \draw[->] (v1) edge[bend left=40,"u_3"]  (v3);
        \draw[->] (v1) edge[bend left=50,"u_4"]  (v4);
        \draw[->] (v1) edge[bend left=55,"u_5"]  (v5);
        \draw[->] (v1) edge[bend left=60,"u_6"] (v6);

   \end{tikzpicture}
   \end{document}
1

slightly modified nice Hood Chatham's answer (most changes are off-topic):

\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{arrows, automata, quotes} 

\begin{document}
\begin{tikzpicture}
       [shorten > = 1pt,
        auto,
        node distance = 2cm,
        thick ,
        execute at begin node=$,execute at end node=$,% put nodes in math mode ...
    every state/.style = {thick, minimum size=4mm, inner sep=1mm},  % <--- added
    every  edge/.style = {draw, -stealth},                          % <--- added 
        ]
  \node[state] (v1) {v_1};
  % Use a loop to make the states. \lastn holds \n minus 1
  \foreach \n [remember=\n as \lastn (initially 1)] in {2,...,6}%
  {
    \node[state] (v\n) [right of=v\lastn] {v_\n};
    \draw (v\n) edge["1"] (v\lastn);                                % <--- changed
  }
  \draw (v1) edge [out=135, in=225, looseness=8, "u_1" ']  (v1);    % <--- added
  % bended edges
  \foreach \i [evaluate=\i as \a using 30 + 6*\i] in {2,...,6}      % <--- added
  \draw (v1) edge[inner sep=1pt, pos=0.75, bend left=\a, "u_\i"]    (v\i);  % <--- modified
   \end{tikzpicture}
   \end{document}

enter image description here

addendum:

if i correctly understood your comment, you like change the first solution to something like this:

enter image description here

in mwe below are changes in comparison of above mwe indicated with % <--- new:

\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{arrows, automata, 
                chains,                                             % <--- new
                positioning,                                        % <--- new
                quotes} 

\begin{document}
\begin{tikzpicture}
       [shorten > = 1pt,
        auto,
        node distance = 2cm,
        start chain = grow right,                                   % <--- new
        thick ,
        execute at begin node=$,execute at end node=$,% put nodes in math mode ...
    every state/.style = {thick, minimum size=4mm, inner sep=1mm,
                          on chain},                                % <--- new
    every  edge/.style = {draw, -stealth},                          
        ]
  \foreach \i [count=\j from 1] in {1,2,3,n}                        % <--- changed
    \node (v\j) [state, on chain] {v_\i};                           % <--- changed
  \foreach \i [count=\j from 1] in {2,...,4}                        % <--- new
  {
    \ifnum\i<4                                                      % <--- new
    \draw (v\i) edge["1"] (v\j);                                    % <--- new
    \else                                                           % <--- new
    \draw[-, very thick, dotted,                                    % <--- new
          shorten <=1mm] (v\i) -- (v\j);                            % <--- new
    \fi                                                             % <--- new
  }
  % bended edges
  \foreach \i [count=\j from 2,                                     % <--- new
               evaluate=\j as \a using 30 + 6*\j] in {2,3,n}        % <--- changed
  \draw (v1) edge[inner sep=1pt, pos=0.75, bend left=\a, "u_\i"]    (v\j);  
  \draw (v1) edge [out=135, in=225, looseness=8, "u_1" ']  (v1);    
\end{tikzpicture}
\end{document}
  • Very nice code. I have a question. If I had n nodes v_1, v_2, ..., v_n, How to use the command \cdots between the diagram. Thank you – Amin235 Nov 29 '17 at 19:24
  • 1
    @Amin235, see if added solution (in addendum) is what you looking for. – Zarko Nov 29 '17 at 20:25
  • What a nice and efficient LaTex code you have written. Really Really thanks for your answer. – Amin235 Nov 29 '17 at 22:15

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.