Iv'e seen the following graph at Introduction to Algorithms by CLRS third edition (P. 596) and I wanted to create it with latex code instead of copying it (the main purpose is the option to edit it and use it for other graphs). I know how to create the nodes, labels of nodes and the numbers inside nodes.

What I don't know is how to add the label (a) at left side and the items at right side (Q and the square contains s with 0 beneath it [I know how to create square node with label and character inside]).

This is what I want to achieve:

This is the code which I use to create the graph (I'm using lyx and the preamble is configured correctly):

\begin{tikzpicture}
[node distance=2.5cm,main node/.style={minimum size=.7cm,circle,fill=white!20,draw},
scale=0.6, every node/.style={scale=0.6}]
\node[main node] [label={270:$v$}] (A) {$\infty$};
\node[main node] (B) [above of=A,label=$r$] {$\infty$};
\node[minimum size=.7cm,circle,fill=lightgray,draw] (C) [right of=B,label=$s$]{$0$};
\node[main node] (D) [below of=C,label={270:$w$}] {$\infty$};
\node[main node] (E) [right of=D,label={270:$x$}] {$\infty$};
\node[main node] (F) [right of=E,label={270:$y$}] {$\infty$};
\node[main node] (G) [above of=F,label=$u$] {$\infty$};
\node[main node] (H) [left of=G,label=$t$] {$\infty$};
\draw (A) -> (B) -> (C) -> (D) -> (E) -> (F) -> (G) -> (H);
\draw (D) -> (H) -> (E) -> (G);
\end{tikzpicture}


I know that my code might be pretty ugly and could be better, I'm still a learner and any advice will be appreciated.

• It's not clear what you mean by a label. You can add text to any side of a node by adding another node (text only). You can create a multiline text entry inside a node by using \parbox, [text width=...], \tabuilar, minipage, \savebox, etc. Commented Mar 9, 2016 at 23:17
• Where is the operation -> in the pgfmanual ? Commented Mar 18, 2016 at 13:48

There are several methods you could use to position nodes at those locations. Below I've placed them relative to the current bounding box node, and also showed another way (commented) using the two outermost nodes that you've defined already.

Note that to draw a line between two coordinates one should use -- not ->, e.g. (a) -- (b).

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
[node distance=2.5cm,main node/.style={minimum size=.7cm,circle,fill=white!20,draw},
scale=0.6, every node/.style={scale=0.6}]
\node[main node] [label={270:$v$}] (A) {$\infty$};
\node[main node] (B) [above of=A,label=$r$] {$\infty$};
\node[minimum size=.7cm,circle,fill=lightgray,draw] (C) [right of=B,label=$s$]{$0$};
\node[main node] (D) [below of=C,label={270:$w$}] {$\infty$};
\node[main node] (E) [right of=D,label={270:$x$}] {$\infty$};
\node[main node] (F) [right of=E,label={270:$y$}] {$\infty$};
\node[main node] (G) [above of=F,label=$u$] {$\infty$};
\node[main node] (H) [left of=G,label=$t$] {$\infty$};
\draw (A) -- (B) -- (C) -- (D) -- (E) -- (F) -- (G) -- (H);
\draw (D) -- (H) -- (E) -- (G);

\node at ([xshift=-0.6cm]current bounding box.west) {(a)};
% alternative method:
%    \path (A) -- node[left=0.5cm] {(a)} (B);

\node at ([xshift=1.5cm]current bounding box.east) [fill=lightgray,draw=black,inner sep=7pt,label=left:$Q$,label=below:$0$] {$s$};

\end{tikzpicture}
\end{document}

• This is the first time I see -> . Where is this option in the pgfmanual ? Commented Mar 18, 2016 at 13:46
• @AlainMatthes It's from the OPs code, I didn't notice it. I've never seen it either, will edit to --. Commented Mar 18, 2016 at 13:47
• thanks. It's used in a graph but it's not the case here ... strange because this operation is accepted Commented Mar 18, 2016 at 13:50
• @AlainMatthes Yes, I was a bit surprised that it worked myself. Commented Mar 18, 2016 at 13:51
• @AlainMatthes It's a "feature" of TikZ parser: you may use -<, -a or any character after - except | and TikZ interprets this sequence as --... Commented Mar 18, 2016 at 14:36