1 Introduction

The chromatic number of a graph G is denoted by \(\chi (G)\) (the minimum number of colors needed in a proper vertex coloring). An edge coloring of a graph G is proper if incident edges of G must receive different colors. The chromatic index, q(G), is the minimum number of colors needed for a proper coloring of G (usually this quantity is denoted by \(\chi '(G)\) in the literature but we will shortly need a further index B, explaining our choice). Edge colorings of graphs where every 4-cycle is rainbow, i.e. it is colored with four different colors, have been treated earlier in [5, 6]. Recently the authors studied these colorings [7] with the additional property that the colorings should be proper. We called these colorings B-colorings and defined \(q_B(G)\) as the smallest number of colors needed for a B-coloring of graph G. Observe that \(q(G)\le q_B(G)\) for every graph G. The motivation to study \(q_B(G)\) comes from its relation to the famous (7, 4)-problem. We asked in [7] whether for any graph G with n vertices and with \(q_B(G)=cn\), G has \(o(n^2)\) edges. We showed that a positive answer to this question would imply a positive answer to the (7, 4)-problem as well: any triple system on n points with no 4 triples on 7 vertices has \(o(n^2)\) triples [1].

Here we study \(q_B(G)\) for cartesian products of paths and cycles. Our main result (Theorem 1.3) shows that \(q_B(G)\) is equal to the chromatic index of G for Cartesian products of paths (apart from a few exceptions). This extends the result in [5] where G is the d-dimensional cube. Our main tool is a lemma (Lemma 1.1) form [5] which gives \(q_B(G\square H)\le q_B(G)+q_B(H)\) if \(\chi (G)\le q_B(H), \chi (H)\le q_B(G)\), where \(\chi\) is the chromatic number.

It is worth noting that B-colorings are similar to the well studied concept of star-edge colorings: proper edge colorings where the union of any two color classes does not contain paths or cycles with four edges. These colorings were defined in [3] (appeared also in [4, 7]). These are also related to the (7,4)-problem; it was shown in [7] that (similarly to B-colorings) the following statement would give a positive answer to the (7, 4)-problem. Any graph G with n vertices and with star-edge colorings with cn colors has \(o(n^2)\) edges. Star-edge colorings of Cartesian products are well-studied ([2, 9], more results are in the survey [8]).

1.1 B-Colorings of Cartesian Products of Graphs

The Cartesian product of two graphs G and H, denoted by \(G\square H\), is a graph with vertex set \(V(G)\times V (H)\), and \(((a,x),(b,y))\in E(G\times H)\) if either \((a,b)\in E(G)\) and \(x = y\), or \((x,y)\in E(H)\) and \(a = b\). Extending the definition for d factors, the d-dimensional grid is the Cartesian product of d paths \(P_{n_1}\square P_{n_2}\square \ldots \square P_{n_d}\). A special case is the d-dimensional hypercube \(Q^d\) where all factors are equal to \(P_2\). If the d factors in the Cartesian product are all equal to a graph G, we denote their cartesian product by \(G^d\).

We will frequently use the well-known fact that the chromatic number of the Cartesian product is the maximum of the chromatic numbers of the factors [10]:

$$\begin{aligned} \chi (G \square H) \le \max \left\{ \chi (G), \chi (H)\right\} . \end{aligned}$$
(1)

In particular, the Cartesian product of bipartite graphs is bipartite. Our main tool will be the following lemma claiming that

$$\begin{aligned} q_B\left( G_1 \square G_2\right) \le q_B\left( G_1\right) + q_B\left( G_2\right) , \end{aligned}$$
(2)

when \(G_1\) and \(G_2\) satisfy an additional condition.

Lemma 1.1

Assume that graph \(G_1\) has a B-coloring with \(p_1\) colors, and graph \(G_2\) has a B-coloring with \(p_2\) colors. Furthermore, assume that \(G_1\) has a proper vertex \(q_1\)-coloring, and \(G_2\) has a proper vertex \(q_2\)-coloring satisfying thecross inequalities"

$$\begin{aligned} q_1 \le p_2\; \; \; and \; \; \; q_2\le p_1. \end{aligned}$$
(3)

Then \(q_B(G_1 \square G_2) \le p_1 + p_2\).

Lemma 1.1 is implicit in [5] (Lemma 1 in [5]), where it was stated for “\(C_4\)-rainbow” colorings: the requirement was only that all 4-cycles must be colored with four distinct colors without requiring that they are proper colorings. However, the color sets used in the proof for the projections of \(G_1 \square G_2\) to \(G_1\) and to \(G_2\), respectively, were disjoint. Thus if we start out with proper colorings of \(G_1\) and \(G_2\), the result will also be a proper coloring of \(G_1 \square G_2\).

We also adopt another lemma from [5] (Lemma 2 in [5]) claiming that in certain special cases we can improve the upper bound in (2) by one. For the sake of completeness we present the proof.

Lemma 1.2

If \(G_1=P_2\square P_2\square P_2=Q^3\) and \(G_2\) is a connected bipartite graph with at least two edges, then

$$\begin{aligned} q_B\left( G_1\square G_2\right) \le q_B\left( G_2\right) +3. \end{aligned}$$

Proof

Set

$$\begin{aligned} S_1=\{1,2,3,4\}, p_2=q_B(G_2), S_2=\{5,6,\dots p_2+4\}. \end{aligned}$$

Fix a B-coloring of \(G_2\) with the \(p_2\) (\(\ge 2\)) colors in \(S_2\) and a proper vertex coloring c of both \(G_1\) and \(G_2\) with two colors. Let CD denote copies of \(G_1=P_2\square P_2\square P_2 =Q^3\) with total B-colorings by the 4 colors in \(S_1\) as shown in Fig. 1.

For each vertex \(x\in V(G_2)\), let \(G_1(x)\) be the copy of \(G_1\) in \(G_1\square G_2\) corresponding to x. Let \(G_1(x)\) be equipped with the total coloring defined by C or D depending on the color c(x). For each vertex \(y\in V(G_1)\), let \(G_2(y)\) be the copy of \(G_2\) in \(G_1\square G_2\) corresponding to y. Let \(G_2(y)\) be equipped with either the original B-coloring or this B-coloring shifted by one (mod \(p_2\)), depending on the color c(y). This is a B-coloring of \(G_1\square G_2\) by \(p_2+4\) colors.

To eliminate one color, say color 5, we use the following property of copies C and D: the corresponding vertices are labelled with the same vertex color, i.e. they miss the same edge color. Then for an edge of color 5 we can recolor it with the common vertex color from \(\{1,2,3,4\}\) of the two endpoints. For example, if in Fig. 1 the two top leftmost corners were connected by a color 5 edge, then we could recolor this edge to color 1. The resulting coloring is still a B-coloring, but we are using one fewer color, as desired. \(\square\)

Fig. 1
figure 1

The colorings C and D of \(Q^3\)

Using Lemmas 1.1 and 1.2 we can determine \(q_B(G)\) for the d-dimensional grid. (In fact, it is equal to its chromatic index, apart from some special cases.) Theorem 1.3 incorporates \(q_B(Q^d)=d, d\ge 4, d\not = 5\), a result from [5] (Theorem 1 in [5]).

Theorem 1.3

Let \(n_i \ge 2\) for \(1\le i \le d\) be arbitrary integers and set \(d'=|\{n_i: n_i=2\}|\). Then

$$\begin{aligned} q_B\left( P_{n_1}\square P_{n_2}\square \ldots \square P_{n_d}\right) = 2d-d', \end{aligned}$$

apart from the following exceptional cases:

$$\begin{aligned} q_B\left( P_2\square P_n\right) =q_B\left( P_2\square P_2\square P_2\right) =4, q_B\left( P_2\square P_2\square P_2 \square P_2 \square P_2\right) =6. \end{aligned}$$

Next we consider the product of even cycles. A sequence of cycle lengths \(2n_1\le 2n_2\le \dots \le 2n_d\) with \(n_i\ge 2\) is exceptional if \(n_1=2\) and for all \(1<i\le d\), \(2n_i \equiv 2 \pmod 4\). The next result shows that \(q_B(G)=q(G)\) again, when G is the product of even cycles with non-exceptional cycle lengths.

Theorem 1.4

Assume that \(2n_1\le 2n_2\le \dots \le 2n_d\) is a non-exceptional sequence. Then

$$\begin{aligned} q_B\left( C_{2n_1}\square C_{2n_2}\square \ldots \square C_{2n_d}\right) = 2d. \end{aligned}$$

For the exceptional case we have the following result.

Theorem 1.5

\(q_B(C_4)=4, q_B(C_4\square C_{2n_2})=5\) if \(n_2\) is odd. In general, for odd \(n_2,\dots ,n_d\), we have

$$\begin{aligned} 2d\le q_B\left( C_4\square C_{2n_2}\square \ldots \square C_{2n_d}\right) \le 2d+1. \end{aligned}$$

Theorem 1.6

Let \(n_i\) for \(1 \le i \le d\) be arbitrary integers. Then

$$\begin{aligned} 2d+1\le q_B\left( C_{2n_1+1}\square C_{2n_2+1}\square \ldots \square C_{2n_d+1}\right) \le 3d. \end{aligned}$$
(4)

The lower bound \(2d+1\) is probably the right value of the expression in (4). We can prove this in some special cases.

Theorem 1.7

Let \(n_i \ge 1, 1\le i \le d\) be arbitrary integers. Then

$$\begin{aligned} q_B\left( C_{2n_1+1}\square C_{2n_2+1}\square \ldots \square C_{2n_d+1}\right) =2d+1, \end{aligned}$$

if \(2d+1|2n_i+1\) for every \(1\le i \le d\).

Theorem 1.6 can be slightly improved for product of triangles.

Theorem 1.8

$$\begin{aligned} 2d+1\le q_B(C_3^d) \le \left\{ \begin{array}{ll} {5d\over 2} &{}\text {if}\ d\ \text {is even}, \\ {5d+1\over 2} &{} \text {if}\ d\ \text {is odd}. \end{array}\right. \end{aligned}$$

It seems interesting to decide whether \(q_B(C_3^d)\) is asymptotic to 2d.

2 Proofs

Proof of Theorem 1.3

Set \(G=P_{n_1}\square P_{n_2}\square \cdots \square P_{n_d}\) and note that G is bipartite by (1) and \(q_B(G)\ge 2d-d'\) is obvious since the right hand side is equal to \(\Delta (G)\). Thus we have to construct a B-coloring of G with \(2d-d'\) colors. The case when \(n_1=n_2=\dots n_d=2\) (including the exceptional cases) is settled in [5]. Thus we may assume that \(n_i\ge 3\) for at least one i. Furthermore, clearly it is enough to construct B-colorings where for all \(n_i,n_j \ge 3\) we have \(n_i=n_j\).

The case \(d'=0\) follows immediately from Lemma 1.1. Similarly the cases \(d'\ge 4, d'\not = 5\) follow from Lemma 1.1 and again from the fact that \(q_B(Q^d)=d, d\ge 4\) and \(d\not = 5\).

For \(d'=1\), \(q_B(P_2\square P_n)=4\) is obvious. Indeed, we start with a proper 2-coloring of \(P_n\) with colors 1 and 2. Then in the other \(P_n\) we switch the colors and finally we color the \(P_2\)-s alternately by colors 3 and 4. Clearly every 4-cycle is rainbow. For the general case it is enough to show a B-coloring of \(P_2\square P_{n}\square P_{n}\), then we can use induction and Lemma 1.1 again.

Represent the vertices of \(G=P_{n}\square P_{n}\) as the grid

$$\begin{aligned} \left\{ (i,j)\; | \; 0\le i \le n-1, 0\le j\le n-1\right\} . \end{aligned}$$

We define two B-colorings with 5 colors, \(\pi _1,\pi _2\) on G as follows.

$$\begin{aligned} \pi _1\left( (i,j),(i+1,j)\right) =i+j-2, \pi _2\left( (i,j),(i+1,j)\right) =i+j, \end{aligned}$$

with \(\pmod 5\) arithmetic, for \(i\in [0,n-2], j\in [0,n-1]\).

$$\begin{aligned} \pi _1\left( (i,j),(i,j+1)\right) =i+j, \pi _2\left( (i,j),(i,j+1)\right) =i+j-2, \end{aligned}$$

with \(\pmod 5\) arithmetic, for \(i\in [0,n-1], j\in [0,n-2]\).

Then we take two vertex disjoint copies of G, say C and D and the corresponding vertices C(ij), D(ij) are joined with an edge of color \(i+j+1 \pmod 5\). Color copy C with \(\pi _1\), copy D with \(\pi _2\). It is easy to check that we get a B-coloring of \(P_2\square P_{n}\square P_{n}\), see Fig. 2 (showing only the corners of C and D, it is continued in a similar fashion).

Indeed, every 4-cycle inside the copies of G is one of the four generated by the neighbors of a vertex of C or a vertex of D not on the border. These are colored by four distinct colors because \(\pi _1,\pi _2\) are B-colorings. The 4-cycles containing an edge e between vertices C(ij) and D(ij) are colored with four distinct colors because the color of e is different from the four colors appearing on the neighbors of C(ij) in C and from the colors on the neighbors of D(ij) in D. This is equivalent to the property that assigning the color \(i+j+1 \pmod 5\) to C(ij) and to D(ij) we get total colorings on CD (a total coloring is a coloring of the edges and vertices (elements) of a graph, such that both edge and vertex colorings are proper and two elements of the same color are not incident). We leave it to the reader to check this, see Fig. 2.

For \(d'=2\), it is enough to prove the statement for \(d=3\) because then we can finish by using Lemma 1.1 again. A B-coloring of \(P_2\square P_2\square P_n=Q^2\square P_n\) is shown in Fig. 2. For each vertex \(x\in V(P_n)\), let \(Q^2(x)\) be the copy of \(Q^2\) in \(Q^2\square P_n\) corresponding to x. For the first vertex \(v_1\) of \(P_n\) we start with a 4-coloring of \(Q^2\), then in each subsequent copy we shift the colors by one (mod 4). Then, as shown in Fig. 2, for a “horizontal" edge (i.e. edges of \(P_n(y)\) for \(y\in Q^2\)) we can color the edge with the color from \(\{1,2,3,4\}\) that is missing on the 4 vertical edges incident to the edge (one of the colors is repeated, thus one of the 4 colors is missing). For example in Fig. 3 the edge e is colored with color 2. It is not hard to check that we have a B-coloring with 4 colors, as desired.

The case \(d'=3\) follows from Lemma 1.2. Finally, for \(d'=5\), we use Lemma 1.1 and the fact that for \(d'=2\) the statement is true, finishing the proof. \(\square\)

Fig. 2
figure 2

C with coloring \(\pi _1\) and D with coloring \(\pi _2\)

Fig. 3
figure 3

A B-coloring of \(Q^2\square P_n\) with 4 colors; the pattern repeats after this

Proof of Theorem 1.4

Note that \(C_{2n_1}\square \dots \square C_{2n_d}\) is a 2d-regular graph, thus 2d is a lower bound in the theorem. Furthermore, by (1) this graph is bipartite. For the upper bound, consider a non-exceptional sequence \(2n_1\le 2n_2\le \dots \le 2n_d\). If \(n_1\ge 3\), then no factor \(C_{2n_i}\) is a \(C_4\), thus Lemma 1.1 implies the theorem.

Assume that \(n_1=2\) and \(n_2>2\). Then, since the sequence of cycle lengths is non-exceptional, there exists \(k>1\) such that \(2n_k\equiv 0 \pmod 4\) and we color \(G=C_4\square C_{2n_k}\) by 4 colors as follows. Represent the vertices of G as

$$\begin{aligned} \left\{ (i,j)\; | \; 0\le i \le 2n_k-1, 0\le j\le 3\right\} . \end{aligned}$$

For all \(0\le i \le 2n_k-2\) and \(0\le j\le 3\), let the color of the edge between (ij) and \((i+1,j)\) be \(i+j+3 \pmod 4\) and let the color of the edge between \((2n_k-1,j)\) and (0, j) be \((2n_1-1)+j+3 \pmod 4\). For all \(0\le i \le 2n_k-1\) and \(0\le j\le 2\), let the color of the edge between (ij) and \((i,j+1)\) be \(i+j+1\pmod 4\) and let the color of the edge between (i, 3) and (i, 0) be \(i+3+1\equiv i \pmod 4\). This is clearly a B-coloring of G. Then we can apply Lemma 1.1 for G and for the product of the remaining factors.

We are left with the case when the sequence of cycle lengths starts with \(t\ge 2\) four-cycles. Let G be the product of these four-cycles and H is the product of the remaining factors. Since G is the product of an even number (at least four) of \(P_2\)-s, \(q_B(G)=2t\) by Theorem 1.3. Then we can finish the proof by applying Lemma 1.1 for G and H (if H is empty we are done by Theorem 1.3). \(\square\)

Proof of Theorem 1.5

Note that \(q_B(C_4)=4\) is trivial. Next we show that \(q_B(C_4\square C_{2n_2})\le 5\) when \(n_2\ge 3\) is odd. Represent the vertices of \(G=C_4\square C_{2n_2}\) again as the grid

$$\begin{aligned} \left\{ (i,j)\; | \; 0\le i \le 2n_2-1, 0\le j\le 3\right\} . \end{aligned}$$

Color the edges of the cycle in row j with the alternating colors j and 5 so that rows 0 and 2 start with colors 0 and 2, respectively, while rows 1 and 3 start with color 5. The 4-cycles (i, 0), (i, 1), (i, 2), (i, 3), (i, 0) in column i are colored with 3, 0, 1, 2 (in this order) for even i and with 2, 3, 0, 1 for odd i. One can easily check that this is a B-coloring of G.

Suppose that G has a B-coloring with colors 1, 2, 3, 4. We claim that any four consecutive edges of the cycles in the rows of G must be colored with four different colors. This leads to contradiction since such a coloring is impossible if \(2n_2\equiv 2 \pmod 4\). Assume w.l.o.g. that the path (0, 0), (1, 0), (2, 0), (3, 0), (4, 0) has two edges, ef with color 1. These edges cannot intersect (since a B-coloring is a proper coloring). There are two cases (apart from symmetry).

  • \(e=\{(0,0), (1,0)\}, f=\{(2,0),(3,0)\}\). Assume w.l.o.g. that the edge \(g=\{(1,0),(2,0)\}\) has color 2. Then w.l.o.g. the edges \(\{(1,0),(1,1)\}\), \(\{(2,0),(2,3)\}\) have color 3 and the edges \(\{(1,0),(1,3)\}\), \(\{(2,0),(2,1)\}\) have color 4. This implies that the none of the three edges \(\{(1,1),(1,2)\}\), \(\{(1,1),(2,1)\}\), \(\{(2,1),(2,2)\}\) can be colored with 3 or 4. This is a contradiction because these edges are on a 4-cycle.

  • \(e=\{(0,0), (1,0)\}, f=\{(3,0), (4,0)\}\). Assume w.l.o.g. that the edge \(\{(1,0),(2,0)\}\) has color 2, the edge \(\{(2,0),(3,0)\}\) has color 3. Then w.l.o.g. the edge \(\{(2,0),(2,1)\}\) has color 1 and the edge \(\{(2,0),(2,3)\}\) has color 4. This implies that the edges \(\{(1,0),(1,1)\}\) and \(\{(3,0),(3,1)\}\) have color 4. Next we observe that the edge \(\{(1,1),(2,1)\}\) has color 3 and the edge \(\{(2,1),(3,1)\}\) has color 2. Thus the edge \(\{(2,1),(2,2)\}\) has color 4, so the 4-cycle in the third column has two edges of color 4, a contradiction.

Using that \(q_B(C_4\square C_{2n_2})=5\), we can use Lemma 1.1 to get the upper bound \(q_B(C_4\square C_{2n_2}\square \ldots \square C_{2n_d})\le 2d+1\). \(\square\)

Proofs of theorems 1.6, 1.7, 1.8

The upper bound of Theorem 1.6 follows from Lemma 1.1 (here \(C_{2n_i+1}\) has a proper 3-coloring and a B-coloring with 3 colors as well and we use (1)). The lower bound of Theorems 1.6 and 1.8 follows from the well-known fact that regular graphs with odd number of vertices are class 2 graphs (i.e. have chromatic index \(\Delta +1\)).

For Theorem 1.7, set \(G=C_{2n_1+1}\square C_{2n_2+1}\square \ldots \square C_{2n_d+1}\), where \(2d+1|2n_j+1\) for every \(1\le j \le d\). We give a B-coloring of G with \(2d+1\) colors. Represent G by the d-dimensional vectors \(x=(x_1,\dots ,x_d)\) with \(1\le x_j\le 2n_j+1\). Let \(e_1,\dots ,e_d\) be unit vectors, so \(e_i\) has 1 at the i-th position and has 0 otherwise. Then, for \(1\le i \le d\), we color the edge \(\{x,x+e_i\}\) of G with

$$\begin{aligned} \left( \sum _{j=1}^d x_j\right) + 2i-1 \pmod {(2d+1)}. \end{aligned}$$

This coloring is proper, since the edges of G incident to x are colored with different colors, the missing color at vertex x is \(\left( \sum _{j=1}^d x_j\right) + 2d \pmod {(2d+1)}.\) Any 4-cycle in G incident to vertex x is determined by two different unit vectors \(e_p,e_q\) with edges

$$\begin{aligned} \{x,x+e_p\},\{x,x+e_q\},\{x+e_p,x+e_p+e_q\}, \{x+e_q,x+e_q+e_p\}. \end{aligned}$$

The colors on these edges are

$$\begin{aligned} \left( \sum _{j=1}^d x_j\right) + 2p-1, \left( \sum _{j=1}^d x_j\right) + 2q-1, \left( \sum _{j=1}^d x_j\right) + 2q, \left( \sum _{j=1}^d x_j\right) + 2p, \end{aligned}$$

clearly all different \(\pmod {(2d+1)}\), as desired.

The upper bound for Theorem 1.8 follows by induction on d, using Lemma 1.1 starting with \(d=1,2\). The B-coloring of \(C_3\) with three colors is obvious. The B-coloring of \(C_3^2\) with 5 colors is shown in Fig. 4. \(\square\)

Fig. 4
figure 4

A B-coloring of \(C_3\square C_3\) with 5 colors