# Is it possible to reduce the number of loops

graphstikz-pgf

Below is a graph I drew using TikZ, which achieves the purpose. However, I feel that my code is still not concise enough because it uses four for-loops. Is there a more concise method?

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=0.8]
% Define the center point
\coordinate (u) at (0,0);
% Draw the vertex u
\node[draw, circle, fill=blue, inner sep=1pt] (u0) at (u) {$u$};
% Draw the first circle with radius 1
%\draw [blue](u) circle (1);
% Draw the second circle with radius 1.5
%\draw (u) circle (1.5);

% Draw vertices on the radius 1 circle and connect them to the center
\foreach \angle/\label in {54/v_0, -18/v_2, -90/v_4, 198/v_6, 126/v_8}
{
\node[draw, circle, fill=white, inner sep=0pt] (w\angle) at ({cos(\angle)}, {sin(\angle)}) {$\label$};
\draw (u0) -- (w\angle);
}

% Draw vertices on the radius 1.5 circle and connect them to the center
\foreach  \angle\label in {18/v_1, 90/v_9, 162/v_7, 234/v_5,-54/v_3}
{
\node[draw, circle, fill=orange, inner sep=0pt] (w\angle) at ({1.5*cos(\angle)}, {1.5*sin(\angle)}) {$\label$};
\draw[blue] (u0) -- (w\angle);
}

% Draw vertices on the radius 1.2 circle and connect them to the center
\foreach \angle [remember=\angle as \lastangle (initially 198)]  in {-90, -18, 54, 126, 198}
{
\draw[blue] (w\lastangle) -- (w\angle);
}

\foreach \angle [remember=\angle as \lastangle (initially 234)] in {-90, -54, -18, 18, 54, 90, 126,162, 198, 234}
{
\draw[thick] (w\lastangle) -- (w\angle);
}

\end{tikzpicture}
\end{document}



Is it possible to reduce the number of loops to make the code more structured?

With two loops and two additional lines:

(you may check if w6 and w8 labels are interchanged ...)

\documentclass[margin=3mm]{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[
C/.style = {circle, draw, fill=#1, inner sep=1pt}
]
\node[C=blue!30] (u0)   {$u$};
% orange vertex
\foreach  \angle/\label in {18/v_1, 90/v_9, 162/v_7, 234/v_5,304/v_3}
{
\node[C=orange] (w\angle) at ({1.2*cos(\angle)}, {1.2*sin(\angle)}) {$\label$};
\draw[blue] (u0) -- (w\angle);
}
\draw[very thick] (w18) -- (w90) -- (w162) -- (w234) -- (w304) -- (w18);
% white vertex
\foreach \angle/\label in {54/v_0, -18/v_2, -90/v_4, 126/v_6, 198/v_8}
{
\node[C=white] (w\angle) at ({cos(\angle)}, {sin(\angle)}) {$\label$};
\draw (u0) -- (w\angle);
}
\draw[blue]   (w54) -- (w-18) -- (w-90) -- (w198) -- (w126) -- (w54);
\end{tikzpicture}
\end{document}


At least above code is quite sorter than yours :-)

with just one loop and \ifodd macro from TeX:

\documentclass[margin=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{backgrounds}

\begin{document}
\begin{tikzpicture}[
C/.style = {circle, draw, fill=#1, inner sep=1pt}
]
\node[C=blue!30] (u0)   {$u$};

\foreach  \i  in {0, 1, ..., 9}
{\ifodd\i
\node[C=orange] (n\i) at (54-\i*36:1.225) {$v_\i$};
\draw[red] (u0) -- (n\i);
\else
\node[C=white] (n\i) at (54-\i*36:1) {$v_\i$};
\draw   (u0) -- (n\i);
\fi
}
\scoped[on background layer]
{
\draw[blue]    (n0) -- (n2) -- (n4) -- (n6) -- (n8) -- (n0);
\draw[thick]   (n1) -- (n3) -- (n5) -- (n7) -- (n9) -- (n1);
}
\end{tikzpicture}
\end{document}


Result is the same as before.