0

I am trying to draw the graph in the attached picture, but I am not sure how to connect the line between the other nodes. enter image description here

My output: enter image description here

\begin{tikzpicture}[>=stealth]
% Leader Node
\node (leader) [circle, draw=black!80, fill=red!40, very thick, minimum size=7mm] {\textbf{0}};
%
\node (Agent1) [circle, above right= 0.5cm and 1.5cm of leader, draw=black!80, fill=yellow!40, very thick, minimum size=7mm] {\textbf{1}};
  \draw[very thick,->, right] (leader) edge (Agent1);
%
\node (Agent2) [circle, below right= 0.5cm and 1.5cm of leader, draw=black!80, fill=yellow!40, very thick, minimum size=7mm] {\textbf{2}};
  \draw[very thick,->, right] (leader) edge (Agent2);
%
\node (Agent3) [circle, right= 1.5cm of Agent2, draw=black!80, fill=yellow!40, very thick, minimum size=7mm] {\textbf{3}};

%
\node (Agent4) [circle, right= 1.5cm of Agent1, draw=black!80, fill=yellow!40, very thick, minimum size=7mm] {\textbf{4}};

\end{tikzpicture}
1
  • 1
    Just add four more \draw macros at the end: \draw[very thick, ->] (Agent1) edge (Agent4); \draw[very thick, ->] (Agent4) edge (Agent2); \draw[very thick, ->] (Agent2) edge (Agent3); \draw[very thick, ->] (Agent3) edge (Agent4);. Oct 14, 2022 at 15:12

3 Answers 3

2

It is very useful to define styles with the repeated characteristics of the elements. (nodes and arrows in this example). Save typing and errors; and makes the code easier to follow.

\documentclass[12pt,a4paper]{article}

\usepackage{tikz}
\usetikzlibrary{positioning}% <<<<<<<<<<<

\begin{document}
    \begin{tikzpicture}[>=stealth, 
        font= \bfseries,
         ynode/.style={circle, draw=black!80, fill=yellow!40, very thick, minimum size=7mm},
         barrow/.style={very thick,->}
         ]
        % Leader Node
        \node (leader) [ynode, fill=red!40] {0};
        \node (Agent1) [ynode, above right= 0.5cm and 1.5cm of leader] {1};
        \node (Agent2) [ynode, below right= 0.5cm and 1.5cm of leader] {2};
        \node (Agent3) [ynode, right= 1.5cm of Agent2] {3};
        \node (Agent4) [ynode, right= 1.5cm of Agent1] {4};
        % arrows x 6
        \draw[barrow] (leader) edge (Agent1);
        \draw[barrow] (leader) edge (Agent2);
        \draw[barrow] (Agent1) edge (Agent4);
        \draw[barrow] (Agent2) edge (Agent3);
        \draw[barrow] (Agent4) edge (Agent2);
        \draw[barrow] (Agent3) edge (Agent4);
    
    \end{tikzpicture}
\end{document}

a

1
  • Thank you @Simon
    – Tee
    Oct 22, 2022 at 6:08
1

Just as Jasper Habicht commented:

Just add four more \draw macros at the end: \draw[very thick, ->] (Agent1) edge (Agent4); \draw[very thick, ->] (Agent4) edge (Agent2); \draw[very thick, ->] (Agent2) edge (Agent3); \draw[very thick, ->] (Agent3) edge (Agent4);.

And I am pretty sure the arrow type is '-latex' so I changed it.

\documentclass[american]{article}    
\begin{tikzpicture}[>=latex]
    % Leader Node
    \node (leader) [circle, draw=black!80, fill=red!40, very thick, minimum size=7mm] {\textbf{0}};
    %
    \node (Agent1) [circle, above right= 0.5cm and 1.5cm of leader, draw=black!80, fill=yellow!40, very thick, minimum size=7mm] {\textbf{1}};
      \draw[very thick,->, right] (leader) edge (Agent1);
    %
    \node (Agent2) [circle, below right= 0.5cm and 1.5cm of leader, draw=black!80, fill=yellow!40, very thick, minimum size=7mm] {\textbf{2}};
      \draw[very thick,->, right] (leader) edge (Agent2);
    %
    \node (Agent3) [circle, right= 1.5cm of Agent2, draw=black!80, fill=yellow!40, very thick, minimum size=7mm] {\textbf{3}};
    
    %
    \node (Agent4) [circle, right= 1.5cm of Agent1, draw=black!80, fill=yellow!40, very thick, minimum size=7mm] {\textbf{4}};
    \draw[very thick, ->] (Agent1) edge (Agent4); 
    \draw[very thick, ->] (Agent4) edge (Agent2); 
    \draw[very thick, ->] (Agent2) edge (Agent3); 
    \draw[very thick, ->] (Agent3) edge (Agent4);
    \end{tikzpicture}
\end{document}

enter image description here

1

One more possible solution:

  • drawn are all arrows between nodes
  • for nodes and and edges are defined styles in picture preamble
  • distances between nodes are defined by node distance
  • by using aforementioned code for your image is concise and simpler.
\documentclass[tikz, margin=3.14159mm]{standalone}
\usetikzlibrary{arrows.meta,
                positioning}

\begin{document}
    \begin{tikzpicture}[
node distance = 5mm and 15mm,   % <---
     C/.style = {circle, draw=black!80, very thick,
                 fill=#1, minimum size=7mm, font=\bfseries}, % <---
     C/.default = yellow!40,    % <---
every edge/.append style = {draw, semithick, -Stealth} % <---
                        ]
% Leader Node
\node (leader) [C=red!40] {0};
\node (Agent1) [C, above right=of leader] {1};
\node (Agent2) [C, below right=of leader] {2};
\node (Agent3) [C, right=of Agent2] {3};
\node (Agent4) [C, right=of Agent1] {4};
%
\draw   (leader) edge (Agent1)
        (leader) edge (Agent2)
        (Agent1) edge (Agent4)
        (Agent2) edge (Agent3)
        (Agent4) edge (Agent2)
        (Agent4) edge (Agent3);
\end{tikzpicture}
\end{document}

enter image description here

5
  • 1
    Just curious about the 'margin=3.14159mm'. May I know the reason? Thank you.
    – MattD626
    Oct 15, 2022 at 2:00
  • @MattD626, that is just size if borders, no special meaning. Only for training of my memorising :-)
    – Zarko
    Oct 15, 2022 at 6:07
  • You certainly decoded it as \pi mm ;-)
    – MS-SPO
    Oct 15, 2022 at 7:45
  • 1
    @MS-SPO, yes, first five decimals :-)
    – Zarko
    Oct 15, 2022 at 8:35
  • 1
    Sorry, we have to stop here (see rules)
    – MS-SPO
    Oct 15, 2022 at 9:58

You must log in to answer this question.

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