3

I have no background in drawing in LaTeX and I just came across the tkz-berge package. I want to draw a complete graph, a small-world graph and a random connected graph.

The complete graph was easy

\begin{tikzpicture}

  \begin{scope}[xshift=12cm]

    \grComplete[RA=2/sin(60)]{10}

  \end{scope}

 \draw (12,-3) node {A Complete Graph};

\end{tikzpicture}

The small world graph is basically a circle with a few edges added to it that 'cross' the circle. Hence, I started out with

\begin{tikzpicture}

 \begin{scope}[xshift=12cm]

   \grCycle[prefix=a,RA=2/sin(60)]{10}

  \end{scope}

 \draw (12,-3) node {A Small World Graph};

\end{tikzpicture}

But how do I add edges to this graph manually, i.e. how do I refer to nodes that have been automatically generated and ask the package to draw an edge between them.

In order to get a random graph with the same number of nodes and the same placement of nodes as the other two I started out with

\begin{tikzpicture}

 \begin{scope}[xshift=12cm]

   \grEmptyCycle[prefix=a,RA=2/sin(60)]{10}

    \end{scope}

 \draw (12,-3) node {A Random Graph};

\end{tikzpicture}

This worked out well, but now I have the same problem as before, I want to manually add edges to the nodes already drawn. Note that it is not important to me that the edges are randomly generated by LaTeX, I can happily just add edges manually so that it looks like a random graph.

2
  • Are you looking for something like \Edges(a1,a5,a9)? The names are shown inside the nodes.
    – esdd
    Commented Jun 11, 2014 at 13:35
  • Yes! Thank you! But I cannot see the names of the nodes with my example. This is no big deal because I can work out what they are called by specifying different \Edges(...). Is there an easy way to see the names in the graph though? Commented Jun 11, 2014 at 13:50

1 Answer 1

4

Normally the labels are printed inside the nodes, so you can connect them by using \Edges(<name1>,<name2>,...)

enter image description here

Code:

\documentclass[margin=5mm]{standalone}
\usepackage{tkz-berge}

\begin{document}

\begin{tikzpicture}
  \begin{scope}[xshift=12cm]
  \grComplete[RA=2/sin(60)]{10}
  \end{scope}
  \draw (12,-3) node {A Complete Graph};
\end{tikzpicture}

\begin{tikzpicture}
  \begin{scope}[xshift=12cm]
    \grCycle[prefix=a,RA=2/sin(60)]{10}
  \end{scope}
  \Edges(a1,a5,a9)
  \Edges[style={dashed,lightgray}](a7,a2)
  \draw (12,-3) node {A Small World Graph};
\end{tikzpicture}

\begin{tikzpicture}
  \begin{scope}[xshift=12cm]
    \grEmptyCycle[prefix=a,RA=2/sin(60)]{10}
  \end{scope}
  \Edges(a0,a1,a2,a3)
  \draw (12,-3) node {A Random Graph};
\end{tikzpicture}
\end{document}

With \SetVertexNoLabel and \SetVertexLabel you can disable and enable the label printing.

enter image description here

Code:

\documentclass[margin=5mm]{standalone}
\usepackage{tkz-berge}

\begin{document}
\SetVertexNoLabel% disables labels
\begin{tikzpicture}
  \begin{scope}[xshift=12cm]
  \grComplete[RA=2/sin(60)]{10}
  \end{scope}
  \draw (12,-3) node {A Complete Graph};
\end{tikzpicture}

\begin{tikzpicture}
  \begin{scope}[xshift=12cm]
    \grCycle[prefix=a,RA=2/sin(60)]{10}
  \end{scope}
  \Edges(a1,a5,a9)
  \Edges[style={dashed,lightgray}](a7,a2)
  \draw (12,-3) node {A Small World Graph};
\end{tikzpicture}
\SetVertexLabel% enable labels
\begin{tikzpicture}
  \begin{scope}[xshift=12cm]
    \grEmptyCycle[prefix=a,RA=2/sin(60)]{10}
  \end{scope}
  \Edges(a0,a1,a2,a3)
  \draw (12,-3) node {A Random Graph};
\end{tikzpicture}
\end{document}

You must log in to answer this question.

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