# Creating combinations of a graph using loops

In the blue graph at the left there are 5 connections. From this graph one can create sub-graphs using for instance 3 of the connections. There would be a total of 10 combinations.

Is there a smart way to create and draw these combinations? Probably by using \foreach.

\documentclass[border=5pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}

\usepackage{tikz}
\usetikzlibrary{positioning,matrix,shapes,arrows,calc}

\begin{document}

\begin{tikzpicture}[>=stealth',auto,node distance=2cm,
thick,main node/.style={circle,draw,font=\sffamily\Large\bfseries},dashnode/.style={circle,draw,dashed,font=\sffamily\Large\bfseries}]

\node[main node,blue] at (0, 0)   (1) {1};
\node[main node,blue] at (1, 1)  (2) {2};
\node[main node,blue] at (1, -1)  (3) {3};
\node[main node,blue] at (2, 0)  (4) {4};

\draw [blue] (1) -- (2);
\draw [blue] (1) -- (3);
\draw [blue] (2) -- (4);
\draw [blue] (2) -- (3);
\draw [blue] (3) -- (4);

\node[main node] at (3, 0)   (1) {1};
\node[main node] at (4, 1)  (2) {2};
\node[main node] at (4, -1)   (3) {3};
\node[main node] at (5, 0)   (4) {4};

\foreach \k in {(1) -- (2),(2) -- (3),(2) -- (4)} {
\draw \k ;
}

\node[main node] at (6, 0)   (1) {1};
\node[main node] at (7, 1)  (2) {2};
\node[main node] at (7, -1)   (3) {3};
\node[main node] at (8, 0)   (4) {4};

\foreach \k in {(1) -- (3),(2) -- (3),(3) -- (4)} {
\draw \k ;
}
\end{tikzpicture}

\end{document}



EDIT

After the very helpful comments from @Excelsior the problem has been codes as:

\documentclass[border=5pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}

\usepackage{tikz}
\usetikzlibrary{positioning,matrix,shapes,arrows,calc}

\begin{document}

% \begin{tikzpicture}[>=stealth',auto,node distance=2cm,
%   thick,main node/.style={circle,draw,font=\sffamily\Large\bfseries},dashnode/.style={circle,draw,dashed,font=\sffamily\Large\bfseries}]

%     \node[main node,blue] at (0, 0)   (1) {1};
%     \node[main node,blue] at (1, 1)  (2) {2};
%     \node[main node,blue] at (1, -1)  (3) {3};
%     \node[main node,blue] at (2, 0)  (4) {4};

%     \draw [blue] (1) -- (2);
%     \draw [blue] (1) -- (3);
%     \draw [blue] (2) -- (4);
%     \draw [blue] (2) -- (3);
%     \draw [blue] (3) -- (4);

%     \node[main node] at (3, 0)   (1) {1};
%     \node[main node] at (4, 1)  (2) {2};
%     \node[main node] at (4, -1)   (3) {3};
%     \node[main node] at (5, 0)   (4) {4};

%     \foreach \k in {(1) -- (2),(2) -- (3),(2) -- (4)} {
%       \draw \k ;
%     }

%     \node[main node] at (6, 0)   (1) {1};
%     \node[main node] at (7, 1)  (2) {2};
%     \node[main node] at (7, -1)   (3) {3};
%     \node[main node] at (8, 0)   (4) {4};

%     \foreach \k in {(1) -- (3),(2) -- (3),(3) -- (4)} {
%       \draw \k ;
%     }

% \end{tikzpicture}

\begin{tikzpicture}[
>=stealth', auto, node distance=2cm, thick,
main node/.style={circle, draw, font=\sffamily\Large\bfseries}, dashnode/.style={circle,draw,dashed,font=\sffamily\Large\bfseries}]

\node[main node,blue] at (0, 0) (1-0) {1};
\node[main node,blue] at (1, 1) (2-0) {2};
\node[main node,blue] at (1,-1) (3-0) {3};
\node[main node,blue] at (2, 0) (4-0) {4};

\draw [blue] (1-0) -- (2-0);
\draw [blue] (1-0) -- (3-0);
\draw [blue] (2-0) -- (4-0);
\draw [blue] (2-0) -- (3-0);
\draw [blue] (3-0) -- (4-0);

\foreach [evaluate=\i as \x using {3*(\i)}] \i in {1,...,11} {
\foreach \n in {1,...,4}{
\node[main node] at ($(\n-0) + (\x,0)$) (\n-\i) {\n};
}
}

% node system 1

\foreach \k in {(1-1) -- (2-1),
(2-1) -- (3-1),
(2-1) -- (4-1)} {
\draw \k ;
}

% node system 2
\draw (1-2) -- (2-2);
\draw (2-2) -- (3-2);
\draw (3-2) -- (4-2);
\end{tikzpicture}

\end{document}



With the following result:

I tried to draw 10 additional node systems and labeled them according to their number. You may know best the possible combinations, so I leave them empty.

\documentclass[border=5pt]{standalone}

\usepackage{tikz}
\usetikzlibrary{positioning,matrix,shapes,arrows,calc}

\begin{document}

\begin{tikzpicture}[
>=stealth', auto, node distance=2cm, thick,
main node/.style={circle, draw, font=\sffamily\Large\bfseries}, dashnode/.style={circle,draw,dashed,font=\sffamily\Large\bfseries}]

\node[main node,blue] at (0, 0) (1-0) {1};
\node[main node,blue] at (1, 1) (2-0) {2};
\node[main node,blue] at (1,-1) (3-0) {3};
\node[main node,blue] at (2, 0) (4-0) {4};

\draw [blue] (1-0) -- (2-0);
\draw [blue] (1-0) -- (3-0);
\draw [blue] (2-0) -- (4-0);
\draw [blue] (2-0) -- (3-0);
\draw [blue] (3-0) -- (4-0);

\foreach [evaluate=\i as \x using {3*(\i)}] \i in {1,...,11} {
\foreach \n in {1,...,4}{
\node[main node] at ($(\n-0) + (\x,0)$) (\n-\i) {\n};
}
}

% node system 1
\draw (1-1) -- (2-1);
\draw (2-1) -- (3-1);
\draw (2-1) -- (4-1);

% node system 2
\draw (1-2) -- (2-2);
\draw (2-2) -- (3-2);
\draw (3-2) -- (4-2);
\end{tikzpicture}
\end{document}


If you need more node systems, you just have to increase the maximum of \i.

EDIT

The possible combination were added:

\documentclass[border=5pt]{standalone}

\usepackage{tikz}
\usetikzlibrary{positioning,matrix,shapes,arrows,calc}

\begin{document}

\begin{tikzpicture}[
>=stealth', auto, node distance=2cm, thick,
main node/.style={circle, draw, font=\sffamily\Large\bfseries}, dashnode/.style={circle,draw,dashed,font=\sffamily\Large\bfseries}]

\node[main node,blue] at (0, 0) (1-0) {1};
\node[main node,blue] at (1, 1) (2-0) {2};
\node[main node,blue] at (1,-1) (3-0) {3};
\node[main node,blue] at (2, 0) (4-0) {4};

\draw [blue] (1-0) -- (2-0);
\draw [blue] (1-0) -- (3-0);
\draw [blue] (2-0) -- (4-0);
\draw [blue] (2-0) -- (3-0);
\draw [blue] (3-0) -- (4-0);

\foreach [evaluate=\i as \x using {3*(\i)}] \i in {1,...,10} {
\foreach \n in {1,...,4}{
\node[main node] at ($(\n-0) + (\x,0)$) (\n-\i) {\n};
}
}

% node system 1
\draw (1-1) -- (2-1);
\draw (2-1) -- (4-1);
\draw (2-1) -- (3-1);

% node system 2
\draw (1-2) -- (2-2);
\draw (2-2) -- (4-2);
\draw (1-2) -- (3-2);

% node system 3
\draw (1-3) -- (2-3);
\draw (2-3) -- (4-3);
\draw (3-3) -- (4-3);

% node system 4
\draw (1-4) -- (2-4);
\draw (2-4) -- (3-4);
\draw (1-4) -- (3-4);

% node system 5
\draw (1-5) -- (2-5);
\draw (2-5) -- (3-5);
\draw (3-5) -- (4-5);

% node system 6
\draw (1-6) -- (2-6);
\draw (1-6) -- (3-6);
\draw (3-6) -- (4-6);

% node system 7
\draw (1-7) -- (3-7);
\draw (2-7) -- (3-7);
\draw (3-7) -- (4-7);

% node system 8
\draw (2-8) -- (4-8);
\draw (2-8) -- (3-8);
\draw (3-8) -- (4-8);

% node system 9
\draw (2-9) -- (4-9);
\draw (2-9) -- (3-9);
\draw (1-9) -- (3-9);

% node system 10
\draw (2-10) -- (4-10);
\draw (3-10) -- (4-10);
\draw (1-10) -- (3-10);
\end{tikzpicture}
\end{document}

• Great, Thanks! Now I only have to figure out how all possible combinations. Commented May 6, 2021 at 18:50
• @JohnW.deBurgh I added the 10 combination Commented May 6, 2021 at 19:24
• That is excellent! Thank you so much. Commented May 6, 2021 at 20:58