1

I am writing an article on graph coloring and trying to make this type of labeled tree in TikZ. (See the attached Figure)

The problem is that I have to use same color label (English letter) for many vertices. Then, labeling many vertices with same English letter, how can I recognize the vertices, position these vertices and draw edges between them ?

A sample coloring of a tree

I want to draw this type of images. Please give suggestions.

  • Welcome to TeX.SE! We're always happy to help you, but you've got to show us what you've tried - we will not do your work for you. Basic drawing commands for TikZ can be found in many places on the internet with a quick search. If you're completely new to it, I would suggest starting here. – Jānis Lazovskis Apr 21 '16 at 13:47
  • It kind of seems to me that you're confusing the text of a node (called a label in the TikZ manual) and the name of a node. The latter is what you use as a coordinate when drawing lines between nodes, but it is completely separate from the text that is shown, so you can have nodes with the same label/text, but different names. This is also demonstrated in answers below. – Torbjørn T. Apr 21 '16 at 16:54
  • @jlv Thank you for the manual. I had tried with basics but had problem when I was giving same label to vertices. So, I did not put my code. – Sushil Verma Apr 21 '16 at 18:06
0

Using the matrix library of tikz as jlv say it :

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
\begin{tikzpicture}
  \matrix[matrix of nodes, nodes={shape=circle, draw=black}, row sep=1cm, column sep=1cm] (m)
  {
    Y & P & P & P \\
    R & B & G & Y \\
    P & Y & R & R \\
  };
  \foreach \n in {1,...,4}
  {
    \draw (m-1-\n) -- (m-2-\n);    
    \draw (m-2-\n) -- (m-3-\n);
  }
  \foreach \y/\z in {1/2,2/3,3/4}
  {
    \draw (m-2-\y) -- (m-2-\z);
  }
\end{tikzpicture}

\end{document}

enter image description here

  • Can we draw the edges of second row of m, with for loop? – Sushil Verma Apr 21 '16 at 19:22
  • see the modifications – flav Apr 22 '16 at 5:32
1

more coloreful ...

\documentclass[tikz]{standalone}
\usetikzlibrary[backgrounds,chains,positioning]

    \begin{document}
\begin{tikzpicture}[
    node distance = 12mm and 13mm,
      start chain = going right,
      vrtx/.style = {circle, draw=#1, fill=#1!50, outer sep=0pt,
                     on chain}
                    ]
\foreach \i/\j [count=\k from 1] in {yellow/Y, purple/P, purple/P, purple/P}
    \node (v1\k) [vrtx=\i] {\j};
\foreach \i/\j [count=\k from 1] in {red/R,    blue/B,   green/G, yellow/Y}
    \node (v2\k) [vrtx=\i,below=of v1\k] {\j};
\foreach \i/\j [count=\k from 1] in {purple/P, yellow/Y, red/R,   red/R}
    \node (v3\k) [vrtx=\i,below=of v2\k] {\j};
\scoped[on background layer]
    \draw   (v11) -- (v31)    (v12) -- (v32)
            (v13) -- (v33)    (v14) -- (v34)
            (v21) -- (v24);
\end{tikzpicture}
    \end{document}

enter image description here

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.