# The Tikz Code of a Digraph

I have the following adjacency matrix:

$A= \left[ \begin {array}{cccccccc} 1&1&0&0&0&0&0&0\\ 0&0&1&0&0&0&0&0\\ 0&0&1&1&0&0&0&0\\ 0&0&0&0&1&0&0&0\\ 0&0&0&0&1&1&0&0\\ 0&0&0&0&0&0&1&0\\ 0&0&0&0&0&0&1&1\\ 1&0&0&0&0&0&0&0 \end {array} \right]$


The digraph that represents this adjacency matrix is:

I obtained the jpg format of this digraph by Maple software.

Is it possible to ask you to help me to write latex code of this digraph with tikz package. One of the my problems is that how to make a loop over vertexes 1, 3 ,5 and 7.

I appreciate you taking the time to help me.

Modifying the code from How to draw a graph in LaTeX?, one can obtain this MWE:

\documentclass{standalone}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
[nodePath/.style={circle,fill=yellow!40}]
\node[nodePath] (n1) at (0,4)  {1} edge [loop above] ();
\node[nodePath] (n2) at (3,3)  {2};
\node[nodePath] (n3) at (4,0)  {3}  edge [loop right] () ;
\node[nodePath] (n4) at (3,-3)  {4};
\node[nodePath] (n5) at (0,-4)  {5}  edge [loop below] ();
\node[nodePath] (n6) at (-3,-3)  {6};
\node[nodePath] (n7) at (-4,0)  {7} edge [loop left] ();
\node[nodePath] (n8) at (-3,3)  {8};

\foreach  \from/\to in {n1/n2,n2/n3,n3/n4,n4/n5,n5/n6,n6/n7,n7/n8,n8/n1}
\draw[->] (\from) -- (\to) node [midway, auto] () {1};
\end{tikzpicture}

\end{document}


This results in:

## Graph with labelled loops

\documentclass{standalone}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
[nodePath/.style={circle,fill=yellow!40}]
\node[nodePath] (n1) at (0,4)  {1} edge [loop above] ();
\node[nodePath] (n2) at (3,3)  {2};
\node[nodePath] (n3) at (4,0)  {3}  edge [loop right] () ;
\node[nodePath] (n4) at (3,-3)  {4};
\node[nodePath] (n5) at (0,-4)  {5}  edge [loop below] ();
\node[nodePath] (n6) at (-3,-3)  {6};
\node[nodePath] (n7) at (-4,0)  {7} edge [loop left] ();
\node[nodePath] (n8) at (-3,3)  {8};

\foreach  \from/\to in {n1/n2,n2/n3,n3/n4,n4/n5,n5/n6,n6/n7,n7/n8,n8/n1}
\draw[->] (\from) -- (\to) node [midway, auto] () {1};

\node[above of= n1] {1};
\node[right of= n3] {1};
\node[below of= n5] {1};
\node[left of= n7] {1};
\end{tikzpicture}

\end{document}


which then results in:

• Thank you so much for your nice code. Can I ask you why you used \documentclass{standalone} instead of \documentclass{article}. I checked the difference of these two mods are in the size of output of image. Am I right. Thanks again. – user0410 Jul 27 '18 at 20:26
• You are right, the standalone class produces only a pdf of the minimum required size. You can then use \includepdf to include it so that the diagram gets calculated only once (which is time-saving). – Tom Jul 27 '18 at 20:29
• I appreciate you taking the time to help me. – user0410 Jul 27 '18 at 20:37
• Again +1. Using an octagon, i.e. a 8-sided polygon, which (also) comes with the shapes.geometric library, may allow you to shorten the code considerably. – marmot Jul 28 '18 at 5:22
• I want to ask you, if it is possible, please help me about make the number 1 over loop edges. Thanks – user0410 Jul 28 '18 at 10:22

In this case, the octagon is based on a regular polygon node. This way, we obtain named anchors in each vertex which can be used to draw labels on them.

As vertex numbers grow counter clockwise, xscale=-1 allows to flip them.

Three foreach sentences draw labels, arrows and loops.

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

\begin{document}
\begin{tikzpicture}[
nodePath/.style={circle, fill=yellow!40}]

\node[minimum size=8cm, regular polygon, regular polygon sides=8,
shape border uses incircle,
shape border rotate=22.5,
xscale=-1] (p) {};

\foreach \i in {1,...,8}
\node[nodePath] (p-\i) at (p.corner \i) {\i};

\foreach \i [remember=\i as \j (initially 8)] in {1,...,8}
\draw[->] (p-\j)--node[auto] {1} (p-\i);

\foreach \i/\j in {1/above,3/right,5/below,7/left}
\path (p-\i) edge [loop \j] ();
\end{tikzpicture}
\end{document}


• I appreciate for your nice code. – user0410 Jul 28 '18 at 8:19

A solution using the TikZ circular graphdrawing library and its simple necklace graph drawing algorithm. Compilation of code using the graph drawing libraries requires lualatex. The graph is updated to also label the loops. Since all the edges are labeled with 1, the tikzset option includes edge label=1, avoiding the need to manually label every edge.

The code:

% !TeX TS-program = lualatex
\RequirePackage{luatex85}
\documentclass[tikz,border=3pt,multi]{standalone}

\usetikzlibrary{graphs,graphdrawing}
\usegdlibrary{circular}

\tikzset{
myedges/.style={
draw=black,
line width=1pt,
->,             % always draw arrow tip
>=stealth,      % style of arrow tip
shorten >=2pt,  % shorten a bit, so that it doesn't quite
shorten <=2pt,  % touch the nodes
font=\scriptsize,
edge label=1
},
mynodes/.style={
node sep=2cm,
anchor=center,
text=black,
inner sep=2pt,
shape=circle,
draw=none,
fill=yellow,
font=\normalfont,
minimum height=10pt
}
}

\begin{document}

\tikz \graph [simple necklace layout,
nodes=mynodes,
edge=myedges]
{  1 -- [loop above]     % loop at 1
1 -- [orient=-22.5]   % Edge from 1 -> 2 at -22.5 degrees
2 --                  % Edge from 2 -> 3
3 -- [loop right]     % Loop at 3
3 --                  % Edge from 3 -> 4
4 --                  % Edge from 4 -> 5
5 -- [loop below]     % Loop at 5
5 --                  % Edge from 5 -> 6
6 --                  % Edge from 6 -> 7
7 -- [loop left]      % Loop at 7
7 --                  % Edge from 7 -> 9
8 --                  % Edge from 8 -> 1
1
};

\end{document}

• I appreciate for your nice code. – user0410 Jul 28 '18 at 10:50