Plot directed graph from transition matrix in tikz

graphstikz-arrowstikz-pgf

I would like to plot a directed graph in tikz starting from a transition matrix as the one below. A "1" in position ij indicates state i leads to state j.

[0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0;
 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0;
 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0;
 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0;
 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0;
 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0;
 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0;
 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0;
 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0]

Starting from this (or something similar), I would like to automatically generate a directed graph in tikz. Can you point me in the right direction on how to achieve this?

EDIT: Ideally, I would like the nodes to be located similarly to this https://r-graphics.org/R-Graphics-Cookbook-2e_files/figure-html/FIG-MISCGRAPH-GRAPH-DIRECTED-1.png

enter image description here

Best Answer

Perhaps this could be a start point? I don't know where to put the nodes so I'm not sure. Anyway, this is my idea:

\documentclass[tikz,border=2mm]{standalone}
\tikzset{my node/.style={draw,circle,minimum size=0.75cm}}

\begin{document}
\begin{tikzpicture}
\def\nn{5} % number of nodes
\def\r{3}  % radius
\foreach\i in {1,...,\nn}
  \node[draw,circle] (\i) at ({360/\nn*(\i-1)}:\r) {\i};
\foreach[count=\j]\i in {% Matrix:
1,1,1,0,0,
0,1,0,0,1,
1,1,0,1,1,
0,0,0,1,0,
1,0,0,1,0
}
{
  \ifnum\i=1
    \pgfmathtruncatemacro\row{div(\j-1,\nn)+1}
    \pgfmathtruncatemacro\col{mod(\j-1,\nn)+1}
    \ifnum\row=\col% same initial and final state
      \pgfmathtruncatemacro\a{360/\nn*(\row-1)+30}
      \pgfmathtruncatemacro\b{\a-60}
      \draw[->] (\row.\a) to[out=\a,in=\b,looseness=4] (\row.\b);
    \else
      \draw[->] (\row) -- (\col);
    \fi
  \fi
}
\end{tikzpicture}
\end{document}

And the output:

enter image description here