Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I need to create some match graphs for my masters thesis report, something like the following: enter image description here

I have found something called PG 3.0 that can make the graphs, but i do not know how to make the dotted lines or the name of the tree (see T1 and T2 in the picture above)

\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{graphdrawing}
\usetikzlibrary{graphs}
\usegdlibrary{trees}
\begin{document}
\begin{tikzpicture}[>=stealth, every node/.style={circle, draw, minimum size=0.75cm}]
\graph [tree layout, grow=down, fresh nodes, level distance=0.5in, sibling distance=0.5in]
    {
        R -> { 
          a,
          b,
          c
        },
        R -> {
            a, 
            q,
            c
        }
    };
\end{tikzpicture}
\end{document}
share|improve this question

2 Answers 2

up vote 4 down vote accepted

Below I present two options:

  1. Here's an option using "pure" TikZ and two \graphs; naming them allows the nodes to be used to draw the lines:

    \documentclass[tikz,border=10pt]{standalone}
    \usetikzlibrary{graphdrawing}
    \usetikzlibrary{graphs}
    \usegdlibrary{trees}
    
    \begin{document}
    
    \begin{tikzpicture}[>=stealth, every node/.style={circle, draw, minimum size=0.75cm}]
    % tree to the left
    \graph[tree layout, grow=down, fresh nodes, level distance=0.5in, sibling distance=0.5in]
        {[name=left]
            R -> { 
              a -> {
                  b, 
                  c
                  },
              d  
            },
        };
    % tree to the right
    \begin{scope}[xshift=4cm]    
    \graph [tree layout, grow=down, fresh nodes, level distance=0.5in, sibling distance=0.5in]
        {[name=right]
            R -> { 
              a' -> {
                  b, 
                  {[name=m] b}
                  },
              d -> {
                  e, 
                  f
                  }  
            },
        };
    \end{scope}
    
    % naming the trees
    \node[draw=none,label={above left:$T_{1}$}] at (left R) {};    
    \node[draw=none,label={above left:$T_{2}$}] at (right R) {};    
    
    % connecting the trees
    \draw[dashed]
          (left R) to[out=30,in=150] (right R);
    \draw[dashed]
          (left a) to[out=40,in=140] (right a');
    \draw[dashed]
          (left d) to[out=40,in=140] (right d);
    \draw[dashed]
          (left b) to[out=-40,in=-140] (right b);
    \draw[dashed]
          (left b) to[out=-40,in=-140] (right m b);
    \end{tikzpicture}
    
    \end{document}
    

    enter image description here

  2. Here's now another option using this time the forest package and just one tree (again, the key is to have a naming mechanism available):

    \documentclass[varwidth,border=10pt]{standalone}
    \usepackage{forest}
    
    \begin{document}
    \begin{forest}
    for tree={
      draw,
      circle,
      minimum size=20pt
    }
    [,phantom,s sep=1cm
    [R,name=rl,
      [a,name=al
        [b,name=bl]
        [b]
      ]
      [d,name=dl
      ]
    ]
    [R,name=rr
      [a',name=ar
        [b,name=bri]
        [b,name=brii]
      ]
      [d,name=dr
        [e]
        [f]
      ]
    ]
    ]
    % naming the trees
    \node[inner sep=7pt,label={above left:$T_{1}$}] at (rl) {};
    \node[inner sep=7pt,label={above left:$T_{2}$}] at (rr) {};
    % connecting the trees
    \draw[dashed]
          (rl) to[out=30,in=150] (rr);
    \draw[dashed]
          (al) to[out=40,in=140] (ar);
    \draw[dashed]
          (dl) to[out=40,in=140] (dr);
    \draw[dashed]
          (bl) to[out=-40,in=-140] (bri);
    \draw[dashed]
          (bl) to[out=-40,in=-140] (brii);
    \end{forest}
    
    \end{document}
    

    enter image description here

share|improve this answer
    
Thank you very much –  DoomStone Jul 16 at 20:44
    
@DoomStone You're welcome. In the meantime I improved the code in my answer and also I added a new option using the forest package. –  Gonzalo Medina Jul 16 at 21:51

Here is another suggestion using one \graph with two subgraphs inside:

\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{graphs,graphdrawing}
\usegdlibrary{trees}
\begin{document}
\begin{tikzpicture}[
    >=stealth,
    subgraph text top=text centered,
  ]
\graph[
    tree layout,
    nodes={circle, draw, minimum size=0.75cm},
    level distance=0.5in,
    sibling distance=0.5in,
    component sep=3em % sep between the two subgraphs/trees
  ]
  {
    $T_{1}$//%first subgraph
      {[name=left]R->{a->{b,c},d}};
    $T_{2}$[xshift=-1em]//% second subgraph
      {[name=right]R->{a/a'->{bl/b,br/b},d->{e,f}}};
  };
  \foreach \n in {R,a,d}
    \draw[dashed](left \n)to[out=30,in=150](right \n);
  \foreach \n in {l,r}
    \draw[dashed](left b)to[out=330,in=210](right b\n);
\end{tikzpicture}
\end{document}

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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