10

I tried many times but failed to draw the graph below using the tikz graphdrawing library. I am quite confused, especially by its "layouts" concepts.

Problem: How to draw the graph below using graphdrawing.

mst-example

Code with graphdrawing stuff commented:

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{positioning}
% \usetikzlibrary{graphs, graphdrawing}
% \usegdlibrary{layered, force}

\begin{document}
\begin{tikzpicture}[node distance = {1.0cm and 1.5cm}, v/.style = {draw, circle}]
  % \graph [spring layout, nodes = {}, horizontal = A to C]
  % {
  %   A -- C -- E,
  %   B -- {D -- F}
  % };

  \node (a) [v] {A};
  \node (c) [v, right = of a] {C};
  \node (e) [v, right = of c] {E};

  \draw (a) to node[above] {1} (c);
  \draw (c) to node[above] {3} (e);

  \node (b) [v, below = of a] {B};
  \node (d) [v, right = of b] {D};
  \node (f) [v, right = of d] {F};

  \draw (b) to node[below] {1} (d);
  \draw (d) to node[below] {4} (f);

  \draw (a) to node[left] {2} (b);
  \draw (c) to node[below] {2} (b);
  \draw (c) to node[right] {2} (d);
  \draw (e) to node[below] {3} (d);
  \draw (e) to node[right] {1} (f);
\end{tikzpicture}
\end{document}
  • Waht is the problem? Your code run well. – ferahfeza Dec 9 '16 at 14:18
  • @ferahfeza I want to know how to draw it using graphdrawing library. – hengxin Dec 9 '16 at 14:24
  • Have you identified a suitable layout? If not, you'd have to invent one. That is, if you want it to look like that and not say, merely be structurally similar. – cfr Dec 10 '16 at 1:22
  • @cfr I don't know how to invent a suitable layout for this graph. I have to read the pgfmanual again. Thanks. – hengxin Dec 10 '16 at 9:04
  • 1
    Certainly don't do that for one diagram. It would be worth it only if you need many of these and do not want to figure out what the results should look like yourself. The point of the layouts is they layout the diagram for you: you just specify the relationships and the rules for the layout. If you want things in particular places, it won't usually make sense to have Lua figure out where to put them. – cfr Dec 10 '16 at 16:49
9

I think you're trying to use graphdrawing when you actually should be using the normal graph instead. The graphdrawing library is for when you have stuff that follows a structural logic but the graph itself is not of a fixed nature. Quoting Till Tantau:

You do not specify where, exactly, the nodes and edges should be. This is something you leave to a graph drawing algorithm. The algorithm gets your description of the graph as an input and then decides where the nodes should go on the page.

The graph you're proposing, to me, looks like you want to specify where the nodes are, but you want to do it in a concise and powerful way. Quoting Till again:

There is nothing in the graph library that you cannot do using the normal \node and the edge commands. Rather, its purpose is to offer a concise and powerful way of specifying which nodes are present and how they are connected.

Here it is how you draw your graph using the graph library (no need for Lua):

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{graphs, graphs.standard, quotes}% quotes library is for the [""] edges

\begin{document}
\begin{tikzpicture}[node distance = {1.0cm and 1.5cm}, v/.style = {draw, circle}]
  \graph[nodes={circle, draw}, grow right=2.25cm, branch down=1.75cm]{
    A -- ["1"] C -- ["3"] E,
    B -- ["1",swap] D -- ["4",swap] F,
    B -- ["2"] A,
    C -- ["2"] {D,B},
    D -- ["3",swap] E -- ["1"] F
  };
\end{tikzpicture}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.