[Tex/LaTex] Generating adjacency matrices from isomorphic graphs

diagramsgraphsmatricestikz-pic

Here are some isomorphic graphs and their corresponding adjacency matrices. I can draw the graphs with tikz. But I'm not sure the best way to draw the matrices. Is it possible to generate one from the other? What's the right way to approach it?

enter image description here

Here's an example of the code to generate a graph:

 \documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\tikzset{Bullet/.style={circle,draw,fill=black,scale=0.75}}
\node[Bullet,label=left :{$e_1$}] (E1) at (0,2) {} ;
\node[Bullet,label=above:{$e_2$}] (E2) at (1,3) {} ;
\node[Bullet,label=right:{$e_3$}] (E3) at (2,2) {} ;
\node[Bullet,label=right:{$e_4$}] (E4) at (2,0) {} ;
\node[Bullet,label=left :{$e_5$}] (E5) at (0,0) {} ;
\draw[thick] (E1)--(E2)--(E3)--(E4)--(E5)--(E1) {} ;
\end{tikzpicture}
\end{document}

Best Answer

This is in case you change your mind and use the adjacency matrices to draw the graphs. TikZ allows you to define arrays, see p. 999 of the pgfmanual. And these arrays can be converted to tables using this nice answer. And these matrices/arrays can also be used to define the graphs.

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{etoolbox}
\usetikzlibrary{matrix,positioning}
% building the table in a foreach loop from https://tex.stackexchange.com/a/60400/121799
\begin{document}
\begin{tikzpicture}[Bullet/.style={circle,draw,fill=black,inner sep=1.5pt},
adjacency matrix/.style={ampersand replacement=\&,matrix of math nodes,
 row 1/.append style={nodes={font=\boldmath}},
 column 1/.append style={nodes={font=\boldmath}},nodes in empty cells,
nodes={draw,minimum width=1.5em,text height=1.8ex},column sep=-\pgflinewidth,row
sep=-\pgflinewidth}]
% first matrix
\def\adjancymatrix{%
{{0,0,1,1,0},%
{0,0,0,1,1},%
{1,0,0,0,1},%
{1,1,0,0,0},%
{0,1,1,0,0}}} 
 \let\mymatrixcontent\empty
 \def\mymatrixcontent{|[draw=none]|\& 1 \& 2 \& 3 \& 4  \& 5\\}
 \begin{scope}[local bounding box=left]
  \foreach \X in {1,...,5}
   {\node[Bullet,label=90+72-\X*72:{$e_\X$}] (E\X) at (90+72-\X*72:2) {} ;}
  \foreach \X in {1,...,5}
  {\begingroup\edef\x{\endgroup
         \noexpand\gappto\noexpand\mymatrixcontent{\X }}\x
  \foreach \Y in {1,...,5}
   {\pgfmathtruncatemacro{\itest}{\adjancymatrix[\X-1][\Y-1]}
   \ifnum\itest=1
    \draw (E\X) -- (E\Y);
    \begingroup\edef\x{\endgroup
         \noexpand\gappto\noexpand\mymatrixcontent{\& 1 }}\x
   \else
    \begingroup\edef\x{\endgroup
         \noexpand\gappto\noexpand\mymatrixcontent{ \&}}\x
   \fi
   }
   \gappto\mymatrixcontent{\\}
   }
 \end{scope} 
 \matrix (leftmat) [below=of left,adjacency matrix]{
    \mymatrixcontent
  };
 %
% second matrix
\def\adjancymatrix{%
{{0,1,0,0,1},%
{1,0,1,0,0},%
{0,1,0,1,0},%
{0,0,1,0,1},%
{1,0,0,1,0}}} 
 \let\mymatrixcontent\empty
 \def\mymatrixcontent{|[draw=none]|\& 1 \& 2 \& 3 \& 4  \& 5\\}
 \begin{scope}[local bounding box=middle,xshift=5cm]
  \foreach \X in {1,...,5}
   {\node[Bullet,label=90+72-\X*72:{$e_\X$}] (E\X) at (90+72-\X*72:2) {} ;}
  \foreach \X in {1,...,5}
  {\begingroup\edef\x{\endgroup
         \noexpand\gappto\noexpand\mymatrixcontent{\X }}\x
  \foreach \Y in {1,...,5}
   {\pgfmathtruncatemacro{\itest}{\adjancymatrix[\X-1][\Y-1]}
   \ifnum\itest=1
    \draw (E\X) -- (E\Y);
    \begingroup\edef\x{\endgroup
         \noexpand\gappto\noexpand\mymatrixcontent{\& 1 }}\x
   \else
    \begingroup\edef\x{\endgroup
         \noexpand\gappto\noexpand\mymatrixcontent{ \&}}\x
   \fi
   }
   \gappto\mymatrixcontent{\\}
   }
 \end{scope} 
 \matrix (midmat) [below=of middle,adjacency matrix]{
    \mymatrixcontent
  };
% third matrix
\def\adjancymatrix{%
{{0,1,0,1,0},%
{1,0,0,0,1},%
{0,0,0,1,1},%
{1,0,1,0,0},%
{0,1,1,0,0}}} 
 \let\mymatrixcontent\empty
 \def\mymatrixcontent{|[draw=none]|\& 1 \& 2 \& 3 \& 4  \& 5\\}
 \begin{scope}[local bounding box=right,xshift=10cm]
  \foreach \X in {1,...,3}
   {\node[Bullet,label=90+72-\X*72:{$e_\X$}] (E\X) at (90+72-\X*72:2) {} ;}
  \node[Bullet,label=90+72-4*72:{$e_5$}] (E5) at (90+72-4*72:2) {} ; 
  \node[Bullet,label=90+72-5*72:{$e_4$}] (E4) at (90+72-5*72:2) {} ; 
  \foreach \X in {1,...,5}
  {\begingroup\edef\x{\endgroup
         \noexpand\gappto\noexpand\mymatrixcontent{\X }}\x
  \foreach \Y in {1,...,5}
   {\pgfmathtruncatemacro{\itest}{\adjancymatrix[\X-1][\Y-1]}
   \ifnum\itest=1
    \draw (E\X) -- (E\Y);
    \begingroup\edef\x{\endgroup
         \noexpand\gappto\noexpand\mymatrixcontent{\& 1 }}\x
   \else
    \begingroup\edef\x{\endgroup
         \noexpand\gappto\noexpand\mymatrixcontent{ \&}}\x
   \fi
   }
   \gappto\mymatrixcontent{\\}
   }
 \end{scope} 
 \matrix (rightmat) [below=of right,adjacency matrix]{
    \mymatrixcontent
  };

\end{tikzpicture}
\end{document}

enter image description here