[Tex/LaTex] Draw a graph using xymatrix. Is there another way

graphsxy-pic

I am using the package xy to draw simple Graphs or Networks. It allows oriented and non oriented edges and even weights on the edges, but the code is very complicated and it is very difficult to build bigger examples than the ones shown bellow.

Does anybody know another way to do this (a simple way)?

An example of the code I currently use:

\begin{figure}
 \centering
  \begin{minipage}{0.4\textwidth}
    \centering
    \[
    \xymatrix{ 
        \xy*{1}*\cir<6pt>{}\endxy\ar@{->}[r]                            & \xy*{2}*\cir<6pt>{}\endxy\ar@{->}[d]                 \\
        \xy*{4}*\cir<6pt>{}\endxy\ar@{->}[ur]\ar@{->}@/_/[r]\ar@{->}[u] & \xy*{3}*\cir<6pt>{}\endxy\ar@{->}[lu]\ar@{->}@/_/[l]
    }
    \]
  \end{minipage}
  %\hfill
  \begin{minipage}{0.4\textwidth}
    \centering
    \[
    \xymatrix{ 
      \xy*{1}*\cir<6pt>{}\endxy\ar@{-}[r]                      & \xy*{2}*\cir<6pt>{}\endxy\ar@{-}[d]  \\
      \xy*{4}*\cir<6pt>{}\endxy\ar@{-}[ur]\ar@{-}[r]\ar@{-}[u] & \xy*{3}*\cir<6pt>{}\endxy\ar@{-}[lu]
    }
    \]
  \end{minipage}

    \label{fig:graphExample}
\end{figure}

The result is:

http://i.stack.imgur.com/hWeaI.png

Best Answer

Something like this via tikz. First define a style file m for all nodes and allocate the position of node 1 with an (internal) and {external label} and the remaining nodes can be allocated by using the relative position commands right/left/above/below = xx cm of <node 1> in the option. After that use

\draw[m,->] (< node 1 >) -- (< node 2 >);

for curve lines use (a) to [bend right/left, out=<ang1>, in=<ang2>] (b)

enter image description here

Code

\documentclass[varwidth]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,arrows}
\tikzset{
m/.style={circle,draw,fill=gray!40,minimum size=20},outer sep=5pt}
\begin{document}
\begin{tikzpicture}
\node[m] at (0,0)(1){1};
\node[m, right=1cm of 1](2){2};
\node[m, below=1cm of 2](3){3};
\node[m, below=1cm of 1](4){4};
\draw[-] (1)--(2) (2)--(3) (3)--(4) (4)--(1);
\draw[-] (4) -- (2);
\draw[-] (3) -- (1);
\end{tikzpicture}
\par

\begin{tikzpicture}
\node[m] at (0,0)(1){1};
\node[m, right=1cm of 1](2){2};
\node[m, below=1cm of 2](3){3};
\node[m, below=1cm of 1](4){4};
\draw[->] (1) -- (2);
\draw[->] (2) -- (3); 
\draw[->] (4) -- (1);
\draw[->] (3) to[bend right] (4);
\draw[->] (4) to[bend right] (3);
\draw[->] (4) -- (2);
\draw[->] (3) -- (1);
\end{tikzpicture}
\end{document}
Related Question