A \textit{graph} is a collection of nodes (vertices) and connections between them (edges). If an edge $e$ connects the vertices $v_i$ and $v_j$, then we write $e ={v_i, v_j}$. An example is below.
\begin{center}
\begin{tikzpicture} [scale = .6] \label{pic:1}
\SetGraphUnit{5}
\Vertex{B}
\WE(B){A}
\EA(B){C}
\Edge(B)(A)
\Edge(C)(B)
\tikzset{EdgeStyle/.append style = {bend left = 50}}
\Edge(A)(C)
\Edge(C)(A)
\coordinate [label=above:{$e_1$}] (e1) at (-2.1,.0);
\coordinate [label=above:{$e_2$}] (e2) at (0,2.45);
\coordinate [label=below:{$e_3$}] (e3) at (0,-2.5);
\coordinate [label=above:{$e_4$}] (e4) at (2.1,.0);
\end{tikzpicture}
\end{center}
More formally, a graph is defined by a set of vertices $\{v_1, v_2, ...\}$, and a set of edges $\{\ \{v_1, v_2\}, \{v_1, v_3\}, ...\ \}$.
\medskip
If the order of the vertices in an edge does not matter,
a graph is called {\it undirected}. A graph is called
a {\it directed graph} if the order of the vertices does matter.
For example, the (undirected) graph above
has three vertices, $A$, $B$, and $C$, and four edges,
$e_1=\{A,B\}$, $e_2=\{A,C\}$, $e_3=\{A,C\}$,
and $e_4=\{B,C\}$.
\end{tcolorbox}
Let's represent Cube 1 by a graph. \\
The vertices will be the face colors: Blue, Green, Red, and White, so $V =\{B,G,R,W\}$. \\
Two vertices are be connected by an edge if and only if the corresponding faces are opposing each other on the cube. \\
Cube 1 has the following edges: $e_1=\{B,R\}$, $e_2=\{G,W\}$, and the loop $e_3=\{R,R\}$. To emphasize that all the three edges represent the first cube, let us mark them with the number $1$. \\
\begin{center}\begin{small}
\begin{tikzpicture}
\filldraw [blue] (0,5) -- (1,5) -- (1,6) --
(0,6) -- (0,5);
\draw [line width = 1.5pt] (0,5) --
(1,5) -- (1,6) -- (0,6) -- (0,5);
\filldraw [green] (5,5) -- (6,5) -- (6,6) --
(5,6) -- (5,5);
\draw [line width = 1.5pt] (5,5) --
(6,5) -- (6,6) -- (5,6) -- (5,5);
\filldraw [red] (0,0) -- (1,0) -- (1,1) --
(0,1) -- (0,0);
\draw [line width = 1.5pt] (0,0) --
(1,0) -- (1,1) -- (0,1) -- (0,0);
\draw [line width = 1.5pt] (5,0) --
(6,0) -- (6,1) -- (5,1) -- (5,0);
\draw (-.5,-.5) circle (.3) node {1};
\draw [line width = 1.5pt] (0,.5) ..
controls (- .9,.4) and (-.65,-.2) .. (-.7,-.3);
\draw [line width = 1.5pt] (.5,0) ..
controls (.4,-.9) and (-.2,-.65) .. (-.3,-.7);
\draw (.5,2) circle (.3) node {1};
\draw [line width = 1.5pt] (.5,1) -- (.5,1.7);
\draw [line width = 1.5pt] (.5,2.3) -- (.5,5);
\draw (5.5,4) circle (.3) node {1};
\draw [line width = 1.5pt] (5.5,5) -- (5.5,4.3);
\draw [line width = 1.5pt] (5.5,3.7) -- (5.5,1);
\node[text = white] at (0.5, 5.5) {\textbf{Blue}};
\node[text = white] at (0.5, 0.5) {\textbf{Red}};
\node[text = black] at (5.5, 0.5) {\textbf{Wht}};
\node[text = black] at (5.5, 5.5) {\textbf{Grn}};
\end{tikzpicture}
\end{small}\end{center}
\bigskip
Cube 2 has the following pairs
of opposing faces, $\{B,W\}$, $\{G,R\}$,
and $\{R,W\}$. Let us add them to the graph
as the edges $e_4$, $e_5$, and $e_6$. \\
\begin{center}\begin{small}
\begin{tikzpicture}
\filldraw [blue] (0,5) -- (1,5) -- (1,6) --
(0,6) -- (0,5);
\draw [line width = 1.5pt] (0,5) --
(1,5) -- (1,6) -- (0,6) -- (0,5);
\filldraw [green] (5,5) -- (6,5) -- (6,6) --
(5,6) -- (5,5);
\draw [line width = 1.5pt] (5,5) --
(6,5) -- (6,6) -- (5,6) -- (5,5);
\filldraw [red] (0,0) -- (1,0) -- (1,1) --
(0,1) -- (0,0);
\draw [line width = 1.5pt] (0,0) --
(1,0) -- (1,1) -- (0,1) -- (0,0);
\draw [line width = 1.5pt] (5,0) --
(6,0) -- (6,1) -- (5,1) -- (5,0);
\draw (-.5,-.5) circle (.3) node {1};
\draw [line width = 1.5pt] (0,.5) ..
controls (-.9,.4) and (-.65,-.2) .. (-.7,-.3);
\draw [line width = 1.5pt] (.5,0) ..
controls (.4,-.9) and (-.2,-.65) .. (-.3,-.7);
\draw (.5,2) circle (.3) node {1};
\draw [line width = 1.5pt] (.5,1) -- (.5,1.7);
\draw [line width = 1.5pt] (.5,2.3) -- (.5,5);
\draw (5.5,4) circle (.3) node {1};
\draw [line width = 1.5pt] (5.5,5) -- (5.5,4.3);
\draw [line width = 1.5pt] (5.5,3.7) -- (5.5,1);
\draw (2,4) circle (.3) node {2};
\draw [line width = 1.5pt] (1,5) -- (1.8,4.2);
\draw [line width = 1.5pt] (5,1) -- (2.2,3.8);
\draw (4,4) circle (.3) node {2};
\draw [line width = 1.5pt] (5,5) -- (4.2,4.2);
\draw [line width = 1.5pt] (1,1) -- (3.8,3.8);
\draw (4,.5) circle (.3) node {2};
\draw [line width = 1.5pt] (1,.5) -- (3.7,.5);
\draw [line width = 1.5pt] (5,.5) -- (4.3,.5);
\node[text = white] at (0.5, 5.5) {\textbf{Blue}};
\node[text = white] at (0.5, 0.5) {\textbf{Red}};
\node[text = black] at (5.5, 0.5) {\textbf{Wht}};
\node[text = black] at (5.5, 5.5) {\textbf{Grn}};
\end{tikzpicture}
\end{small}\end{center}
\bigskip
Let us now make the graph
representing all four cubes. \\
\begin{center}\begin{small}
\begin{tikzpicture}\label{pic:II_comfiguration}
\filldraw [blue] (0,5) -- (1,5) -- (1,6) --
(0,6) -- (0,5);
\draw [line width = 1.5pt] (0,5) --
(1,5) -- (1,6) -- (0,6) -- (0,5);
\filldraw [green] (5,5) -- (6,5) -- (6,6) --
(5,6) -- (5,5);
\draw [line width = 1.5pt] (5,5) --
(6,5) -- (6,6) -- (5,6) -- (5,5);
\filldraw [red] (0,0) -- (1,0) -- (1,1) --
(0,1) -- (0,0);
\draw [line width = 1.5pt] (0,0) --
(1,0) -- (1,1) -- (0,1) -- (0,0);
\draw [line width = 1.5pt] (5,0) --
(6,0) -- (6,1) -- (5,1) -- (5,0);
\draw (-.5,-.5) circle (.3) node {1};
\draw [line width = 1.5pt] (0,.5) ..
controls (-.9,.4) and (-.65,-.2) .. (-.7,-.3);
\draw [line width = 1.5pt] (.5,0) ..
controls (.4,-.9) and (-.2,-.65) .. (-.3,-.7);
\draw (.5,2) circle (.3) node {1};
\draw [line width = 1.5pt] (.5,1) -- (.5,1.7);
\draw [line width = 1.5pt] (.5,2.3) -- (.5,5);
\draw (5.5,4) circle (.3) node {1};
\draw [line width = 1.5pt] (5.5,5) -- (5.5,4.3);
\draw [line width = 1.5pt] (5.5,3.7) -- (5.5,1);
\draw (1.5,3.5) circle (.3) node {2};
\draw [line width = 1.5pt] (.8,5) ..
controls (.8,4.6) and (1,4.2) .. (1.3,3.7);
\draw [line width = 1.5pt] (5,.8) ..
controls (2.5,1.5) and (2.1,2.5) .. (1.6,3.22);
\draw (4,4) circle (.3) node {2};
\draw [line width = 1.5pt] (5,5) -- (4.2,4.2);
\draw [line width = 1.5pt] (1,1) -- (3.8,3.8);
\draw (4,.5) circle (.3) node {2};
\draw [line width = 1.5pt] (1,.5) -- (3.7,.5);
\draw [line width = 1.5pt] (5,.5) -- (4.3,.5);
\draw (-1,3) circle (.3) node {3};
\draw [line width = 1.5pt] (0,5) ..
controls (-.7,4.3) and (-.9,3.6) .. (-1,3.3);
\draw [line width = 1.5pt] (0,1) ..
controls (-.7,1.7) and (-.9,2.3) .. (-1,2.7);
\draw (2.5,4) circle (.3) node {3};
\draw [line width = 1.5pt] (1,5.2) ..
controls (1.4,5.2) and (2,4.5) .. (2.3,4.2);
\draw [line width = 1.5pt] (5.2,1) ..
controls (4.7,1.8) and (3.5,3) .. (2.7,3.8);
\draw (7,3) circle (.3) node {3};
\draw [line width = 1.5pt] (6,5) ..
controls (6.7,4.3) and (6.9,3.6) .. (7,3.3);
\draw [line width = 1.5pt] (6,1) ..
controls (6.7,1.7) and (6.9,2.3) .. (7,2.7);
\draw (-.5,6.5) circle (.3) node {4};
\draw [line width = 1.5pt] (0,5.5) ..
controls (-.9,5.4) and (-.65,6.2) .. (-.7,6.26);
\draw [line width = 1.5pt] (-.24,6.65) ..
controls (.1,6.7) and (.45,6.7) .. (.5,6);
\draw (8.5,3) circle (.3) node {4};
\draw [line width = 1.5pt] (6,5.5) ..
controls (7.3,5) and (8.2,4) .. (8.5,3.3);
\draw [line width = 1.5pt] (6,.8) ..
controls (7.3,1) and (8.2,2) .. (8.5,2.7);
\draw (2.3,1.2) circle (.3) node {4};
\draw [line width = 1.5pt] (1,.8) -- (2.04,1.1);
\draw [line width = 1.5pt] (2.6,1.3) ..
controls (4,2) and (5,4) .. (5.2,5);
\node[text = white] at (0.5, 5.5) {\textbf{Blue}};
\node[text = white] at (0.5, 0.5) {\textbf{Red}};
\node[text = black] at (5.5, 0.5) {\textbf{Wht}};
\node[text = black] at (5.5, 5.5) {\textbf{Grn}};
\end{tikzpicture}
\end{small}\end{center}
\problem{}
Check if the above representation
is correct for Cubes 3 and 4.
\vfill
\pagebreak
With the help of the above graph,
solving the puzzle becomes as easy
as a walk in the park, literally.
Imagine that the vertices of the above graph
are the clearings and the edges are the paths.
An edge marked by the number $i$ represents
two opposing faces of the $i$-th cube.
Let us try to find a closed walk, a.k.a.~a cycle,
in the graph that visits each clearing once
and uses the paths marked by the different numbers,
$i=1,2,3,4$. If we order the front and rear sides
of the cubes accordingly,
then the front and rear of the stack
will show all the four different colors
in the order prescribed by our walk. \\
For example, here is such an (oriented) cycle,
represented by the magenta arrows
on the picture below. \\
\begin{center}\begin{small}
\begin{tikzpicture}
\draw [line width = 3pt, color = magenta, <-]
(.5,1) -- (.5,1.7);
\draw [line width = 3pt, color = magenta, <-]
(.5,2.3) -- (.5,5);
\draw [line width = 3pt, color = magenta, <-]
(5,5) -- (4.2,4.2);
\draw [line width = 3pt, color = magenta, ->]
(1,1) -- (3.8,3.8);
\draw [line width = 3pt, color = magenta, ->]
(6,5.5) .. controls (7.3,5) and (8.2,4) .. (8.5,3.3);
\draw [line width = 3pt, color = magenta, <-]
(6,.8) .. controls (7.3,1) and (8.2,2) .. (8.5,2.7);
\draw [line width = 3pt, color = magenta, <-]
(1,5.2) .. controls (1.4,5.2) and (2,4.5) .. (2.3,4.2);
\draw [line width = 3pt, color = magenta, ->]
(5.2,1) .. controls (4.7,1.8) and (3.5,3) .. (2.7,3.8);
\filldraw [blue] (0,5) -- (1,5) -- (1,6) --
(0,6) -- (0,5);
\draw [line width = 1.5pt] (0,5) --
(1,5) -- (1,6) -- (0,6) -- (0,5);
\filldraw [green] (5,5) -- (6,5) -- (6,6) --
(5,6) -- (5,5);
\draw [line width = 1.5pt] (5,5) --
(6,5) -- (6,6) -- (5,6) -- (5,5);
\filldraw [red] (0,0) -- (1,0) -- (1,1) --
(0,1) -- (0,0);
\draw [line width = 1.5pt] (0,0) --
(1,0) -- (1,1) -- (0,1) -- (0,0);
\draw [line width = 1.5pt] (5,0) --
(6,0) -- (6,1) -- (5,1) -- (5,0);
\draw (-.5,-.5) circle (.3) node {1};
\draw [line width = 1.5pt] (0,.5) ..
controls (-.9,.4) and (-.65,-.2) .. (-.7,-.3);
\draw [line width = 1.5pt] (.5,0) ..
controls (.4,-.9) and (-.2,-.65) .. (-.3,-.7);
\draw (.5,2) circle (.3) node {1};
\draw (5.5,4) circle (.3) node {1};
\draw [line width = 1.5pt] (5.5,5) -- (5.5,4.3);
\draw [line width = 1.5pt] (5.5,3.7) -- (5.5,1);
\draw (1.5,3.5) circle (.3) node {2};
\draw [line width = 1.5pt] (.8,5) ..
controls (.8,4.6) and (1,4.2) .. (1.3,3.7);
\draw [line width = 1.5pt] (5,.8) ..
controls (2.5,1.5) and (2.1,2.5) .. (1.6,3.22);
\draw (4,4) circle (.3) node {2};
\draw (4,.5) circle (.3) node {2};
\draw [line width = 1.5pt] (1,.5) -- (3.7,.5);
\draw [line width = 1.5pt] (5,.5) -- (4.3,.5);
\draw (-1,3) circle (.3) node {3};
\draw [line width = 1.5pt] (0,5) ..
controls (-.7,4.3) and (-.9,3.6) .. (-1,3.3);
\draw [line width = 1.5pt] (0,1) ..
controls (-.7,1.7) and (-.9,2.3) .. (-1,2.7);
\draw (2.5,4) circle (.3) node {3};
\draw (7,3) circle (.3) node {3};
\draw [line width = 1.5pt] (6,5) ..
controls (6.7,4.3) and (6.9,3.6) .. (7,3.3);
\draw [line width = 1.5pt] (6,1) ..
controls (6.7,1.7) and (6.9,2.3) .. (7,2.7);
\draw (-.5,6.5) circle (.3) node {4};
\draw [line width = 1.5pt] (0,5.5) ..
controls (-.9,5.4) and (-.65,6.2) .. (-.7,6.26);
\draw [line width = 1.5pt] (-.24,6.65) ..
controls (.1,6.7) and (.45,6.7) .. (.5,6);
\draw (8.5,3) circle (.3) node {4};
\draw (2.3,1.2) circle (.3) node {4};
\draw [line width = 1.5pt] (1,.8) -- (2.04,1.1);
\draw [line width = 1.5pt] (2.6,1.3) ..
controls (4,2) and (5,4) .. (5.2,5);
\node[text = white] at (0.5, 5.5) {\textbf{Blue}};
\node[text = white] at (0.5, 0.5) {\textbf{Red}};
\node[text = black] at (5.5, 0.5) {\textbf{Wht}};
\node[text = black] at (5.5, 5.5) {\textbf{Grn}};
\end{tikzpicture}
\end{small}\end{center}
\bigskip
The first leg of the walk tells us
to take Cube 1 and to make sure
that its blue side is facing forward.
Then the red side, opposite to the blue one,
will face the rear.
\begin{center}
\begin{tikzpicture}
\coordinate [label=center:{Front:}] (f) at (0,.5);