1 Introduction and Preliminaries

All graphs considered in this paper are simple finite graphs. We use \(V(G)\) for the vertex set and \(E(G)\) for the edge set of a graph \(G\). The neighborhood \(N(x)\) (or more precisely \(N_{G}(x)\), when needed) of a vertex \(x\) is the set of vertices adjacent to \(x\), and the degree \(d(x)\) of \( x\) is \(|N(x)|\), i.e. the size of the neighborhood of \(x\). By \(C_{n}\) we denote a cycle on \(n\) vertices.

Distance magic labeling  (also called sigma labeling) of a graph \(G=(V(G),E(G))\) of order \(n\) is a bijection \(\ell :V\rightarrow \{1,\ldots ,n\}\) with the property that there is a positive integer \(k\) (called magic constant) such that \(w(x)=\sum _{y\in N_{G}(x)}\ell (y)=k \) for every \(x\in V(G)\), where \(w(x)\) is the weight of \(x \). If a graph \(G\) admits a distance magic labeling, then we say that \(G\) is a distance magic graph.

The concept of distance magic labeling of a graph has been motivated by the construction of magic squares. However, finding an \(r\)-regular distance magic labeling is equivalent to finding equalized incomplete tournament \(\mathrm {EIT}(n, r)\) [5]. In an equalized incomplete tournament \(\mathrm {EIT}(n, r)\) of \(n\) teams with \(r\) rounds, every team plays with exactly \(r\) other teams and the total strength of the opponents of each team is \(k\). For a survey, we refer the reader to [1].

The following observations were proved independently:

Observation 1.1

([7, 9, 11, 12]) Let \(G\) be an \(r\)-regular distance magic graph on \(n\) vertices. Then \(k=\frac{r(n+1)}{2}\).

Observation 1.2

([7, 9, 11, 12]) No \(r\)-regular graph with an odd \(r\) can be a distance magic graph.

We recall three out of four standard graph products (see [6]). Let \(G\) and \(H\) be two graphs. All three, the Cartesian product \(G\square H\), the lexicographic product \(G\circ H\), and the direct product \( G\times H\) are graphs with vertex set \(V(G)\times V(H)\). Two vertices \((g,h)\) and \((g^{\prime },h^{\prime })\) are adjacent in:

  • \(G\square H\) if and only if either \(g=g^{\prime }\) and \(h\) is adjacent with \(h^{\prime }\) in \(H\), or \(h=h^{\prime }\) and \(g\) is adjacent with \( g^{\prime }\) in \(G\);

  • \(G\circ H\) if and only if either \(g\) is adjacent with \(g^{\prime }\) in \(G\) or \(g=g^{\prime }\) and \(h\) is adjacent with \(h^{\prime }\) in \(H\);

  • \(G\times H\) if and only if \(g\) is adjacent with \(g^{\prime }\) in \(G\) and \(h\) is adjacent with \(h^{\prime }\) in \(H\).

For a fixed vertex \(g\) of \(G\), the subgraph of any of the above products induced by the set \(\{(g,h)\,:\,h \in V(H)\}\) is called an \(H\) -layer and is denoted \(^g\!H\). Similarly, if \(h \in H\) is fixed, then \(G^h\), the subgraph induced by \(\{(g,h)\,:\, g \in V(G)\}\), is a \(G\) -layer.

The main topic of this paper is the direct product (that is known also by many other names, see [6]). It is the most natural graph product in the sense that each edge of \(G\times H\) projects to an edge in both factors \( G\) and \(H\). This is also the reason that many times this product is the most difficult to handle among (standard) products. Even the distance formula is very complicated with respect to other products (see [8]), and \( G\times H \) does not need to be connected, even if both factors are. More precisely, \(G\times H\) is connected if and only if both \(G\) and \(H\) are connected and at least one of them is non-bipartite [13].

The direct product is commutative, associative, and has attracted a lot of attention in the research community in last 50 years. Probably the biggest challenge (among all products) is the famous Hedetniemi’s conjecture:

$$\begin{aligned} \chi (G\times H)=\min \{\chi (G),\chi (H)\}. \end{aligned}$$

This conjecture suggests that the chromatic number of the direct product depends only on the properties of one factor and not both. This is not so rare and also in this work we show that it is enough for one factor to be a distance magic graph with one additional property and then the product with any regular graph will result in a distance magic graph. More details about the direct product and products in general can be found in the book [6].

Some graphs which are distance magic among (some) products can be seen in [2, 3, 9, 10]. The following product cycle and product related results were proved by Miller, Rodger, and Simanjuntak.

Theorem 1.3

[9] The cycle \(C_n\) of length \(n\) is a distance magic graph if and only if \(n = 4 \).

Theorem 1.4

[9] Let \(G\) be an \(r\)-regular graph and \(C_{n}\) the cycle of length \(n\) for \(r\ge 1\) and \(n\ge 3\). The lexicographic product \(G\circ C_{n}\) admits a distance magic labeling if and only if \(n=4\).

In particular we have:

Observation 1.5

The lexicographic product \(C_n \circ C_m\), \(n, m\ge 3\) is a distance magic graph if and only if \(m =4\).

Rao, Singh and Parameswaran characterized distance magic graphs among Cartesian products of cycles.

Theorem 1.6

[10] The Cartesian product \(C_{n}\square C_{m}\), \(n,m\ge 3\), is a distance magic graph if and only if \(n=m\equiv 2\;(\mathop {\mathrm{mod}}\nolimits 4)\).

In the next section we introduce a natural subclass of distance magic graphs. For this class of graphs we were able to generalize Theorem 1.4 and show that it is closed for the direct product with regular graphs. In the last section we characterize distance magic graphs among direct products of cycles. In particular, we prove that a graph \(C_{m}\times C_{n}\) is distance magic if and only if \(n=4\) or \(m=4\) or \(m,n\equiv 0 \;(\mathop {\mathrm{mod}}\nolimits 4)) \).

2 Balanced Distance Magic Graphs

In order to obtain a large class of graphs for which their direct product is distance magic we introduce a natural subclass of distance magic graphs.

A distance magic graph \(G\) with an even number of vertices is called balanced if there exists a bijection \(\ell :V(G)\rightarrow \{1,\ldots ,|V(G)|\}\) such that for every \(w\in V(G)\) the following holds: if \(u\in N(w) \) with \(\ell (u)=i\), then there exists \(v\in N(w)\), \(v\ne u\), with \(\ell (v)=|V(G)|+1-i\) . We call \(u\) the twin vertex of \(v\) and vice versa (we will also say that \(u\) and \(v\) are twin vertices, or shortly twins) and \(\ell \) is called a balanced distance labeling. Hence a distance magic graph \(G\) is balanced if for any \(w\in V(G)\) either both or none of vertices \(u\) and \(v\) with labels \( \ell (u)=i\) and \(\ell (v)=|V(G)|+1-i\) are in the neighborhood of \(w\). It also follows from the definition that twin vertices of a balanced distance magic graph cannot be adjacent and that \(N_{G}(u)=N_{G}(v)\).

It is somewhat surprising that the condition \(N_{G}(u)=N_{G}(v)\) plays an important role in finding the factorization of the direct product, see Chapter 8 of [6]. In particular, if a non-bipartite connected graph has no pairs of vertices with the property \(N_G(u)=N_G(v)\), then it is easier to find the prime factor decomposition. Similarly, such pairs generate very simple automorphisms of \(G\) and have been called unworthy in [14]. However in both above mentioned cases not all vertices need to have a twin vertex as in our case.

It is easy to see that a balanced distance magic graph is \(r\)-regular for some even \(r\). Recall that the magic constant is \(\frac{r}{2} (|V(G)|+1)\) by Observation 1.1. Trivial examples of balanced distance magic graphs are empty graphs on even number of vertices. Not all distance magic graphs are balanced distance magic graphs. The smallest example is \(P_{3}\). More examples (regular graphs with an even number of vertices) will be presented in the next section.

The graph \(K_{2n,2n}\), \(n\ge 1\), is balanced distance magic. To verify this let \(V(K_{2n,2n})=\{v_{1},\ldots ,v_{4n}\}\). Assume that the vertices are enumerated in such a way that the sets \(U=\{v_{i}:i(\mathrm{mod} 4)\in \{0,1\}\}\) and \(W=V(K_{2n,2n})-U\) form the bipartition of \(V(K_{2n,2n})\). It is easy to see that the labeling

$$\begin{aligned} \ell (v_{i})=i \quad \text { for } i\in \{1,\ldots ,4n\} \end{aligned}$$

is the desired balanced distance magic labeling for \(n\ge 2\). In particular, for \(n=1\) note that \(K_{2,2}\) is isomorphic to \(C_{4}\) and consecutive vertices receive labels \(1,2,4,3\).

Also \(K_{2n}-M\) is balanced distance magic if \(M\) is a perfect matching of \(K_{2n}\). Indeed, if \(u\) and \(v\) form the \(i\)-th edge of \(M\), \( i\in \{1,\ldots ,n\}\), then we set \(\ell (u)=i\) and \(\ell (v)=2n+1-i\) which is a balanced distance magic labeling.

The distance magic graphs \(G\circ C_{4}\) described in Theorem 1.4 are also balanced distance magic graphs. Let \(V(G)=\{g_{1},\ldots ,g_{p}\}\) be the vertex set of a regular graph \(G\) and \(V(C_{4})= \{h_{1},h_{2},h_{3},h_{4}\}\) where indices of vertices in \(V(C_{4})\) correspond to labels of a distance magic labeling of \(C_{4}\). It is not hard to verify that the labeling

$$\begin{aligned} \ell ((g_{i},h_{j}))=\left\{ \begin{array}{lcl} (j-1)p+i, &{}\quad \text {if} &{} j\in \{1,2\}, \\ jp-i+1, &{}\quad \text {if} &{} j\in \{3,4\}, \end{array} \right. \end{aligned}$$

is a balanced distance magic labeling of \(G\circ C_{4}\). Using similar labeling we obtain a larger family of balanced distance magic graphs.

Next we give another description of balanced distance magic graphs, which was implicitly used in above examples and will help in forthcoming proofs.

Proposition 2.1

Graph \(G\) is balanced distance magic if and only if \(G\) is regular, and \(V(G)\) can be partitioned in pairs \((u_i,v_i)\), \(i\in \{1,\ldots ,|V(G)|/2\}\), such that \(N(u_i)=N(v_i)\) for all \(i\).

Proof

Let \(G\) be a balanced distance magic graph. Twins generate a desired partition of \(V(G)\) for which \(N(u_i)=N(v_i)\) holds, since every vertex \(w\in V(G)\) has either both or none of vertices \(u_i\) and \(v_i\) in its neighborhood. Also \(G\) is regular since every vertex must have the same amount of twins in its neighborhood to obtain the same weight equal to the magic constant.

Conversely let \(G\) be regular, and \(V(G)\) can be partitioned in pairs \((u_i,v_i)\), \(i\in \{1,\ldots ,|V(G)|/2\}\), such that \(N(u_i)=N(v_i)\) for all \(i\). By setting \(\ell (u_i)=i\) and \(\ell (v_i)=|V(G)|-i+1\) we see that \(u_i\) and \(v_i\) are twins for every \(i\). Moreover, \(w(x)=\sum _{v\in N(x)}\ell (v)=\frac{d(x)}{2}(i+|V(G)|-i+1)=\frac{d(x)(|V(G)|+1)}{2}\) for all \(x\in V(G)\). Hence \(G\) is balanced distance magic graph. \(\square \)

Theorem 2.2

Let \(G\) be a regular graph and \(H\) be a graph not isomorphic to \(\overline{K_{n}}\) where \(n\) is odd. Then \(G\circ H\) is a balanced distance magic graph if and only if \(H\) is a balanced distance magic graph.

Proof

Let \(G\) be an \(r_G\)-regular graph and \(H\) be a graph not isomorphic to \(\overline{K_{n}}\) for an odd \(n\). Let first \(H\) be a balanced distance magic graph with the vertex set \(V(H)=\{h_{1},\ldots ,h_{t}\}\) and let \(\varphi \) defined by \(\varphi (h_{j})=j\) be a balanced distance magic labeling of \(H\) (we can always enumerate the vertices in an appropriate way). Hence \((h_j,h_{|V(H)|-j+1})\) form a partition of \(V(H)\) from Proposition 2.1. Clearly \(((g_i,h_j),(g_i,h_{|V(H)|-j+1}))\), \(i\in \{1,\ldots ,|V(G)|\}\) and \(j\in \{1,\ldots ,|V(H)|/2\}\), form a partition of \(V(G\circ H)\) with

$$\begin{aligned} N_{G\circ H}(g_i,h_j)=N_{G\circ H}(g_i,h_{|V(H)|-j+1}). \end{aligned}$$

By Proposition 2.1 \(G\circ H\) is balanced distance magic graph.

Conversely, let \(G\circ H\) be a balanced distance magic (and hence regular) graph. If \(H\) is an empty graph on even number of vertices, then it is balanced distance magic graph.

If \(H\) is not empty, then for each pair of twins \((g_1,h_1)\) and \((g_2,h_2)\) we have

$$\begin{aligned} N(g_1,h_1)\cap {^{g_1}\!H} = N(g_2,h_2)\cap {^{g_1}\!H} \end{aligned}$$

and

$$\begin{aligned} N(g_1,h_1)\cap {^{g_2}\!H} = N(g_2,h_2)\cap {^{g_2}\!H}, \end{aligned}$$

what means that \(g_1=g_2\), i.e. \((g_1,h_1)\) and \((g_2,h_2)\) lie in the same \(H\)-layer \(^{g_1}\!H\). In consequence

$$\begin{aligned} N_H(h_1)=N_H(h_2) \end{aligned}$$

and by the Proposition 2.1 \(H\) is balanced distance magic. \(\square \)

Note that in order to prove the equivalence in the above theorem we needed to exclude \(H\) as an empty graph with odd number of vertices. Namely, it is not hard to see that for positive integer \(k\), \(C_4\circ \overline{ K_{2k-1}}\) is a balanced distance magic graph, but \(\overline{K_{2k-1}}\) is not (recall that by the definition an empty graph is balanced distance magic if it has an even order). As an example see the labeling of \(C_4\circ \overline{K_{3}}\) in the table below, where rows and columns represent labeling of vertices in \(C_{4}\)-layers and \(\overline{K_{3}}\)-layers, respectively (the latter ones refer to consecutive vertices of \(C_4\)).

3

6

10

7

2

5

11

8

1

4

12

9

The situation is even more challenging when we turn to the direct product. If one factor, say \(H\), is an empty graph, also the product \(G\times H\) is an empty graph. Hence for any graph \(G\) on even number of vertices \(G\times \overline{K_{2k-1}}\) is a balanced distance magic graph, while \(\overline{ K_{2k-1}}\) is not. However, we can still obtain a result similar to Theorem 2.2. For this we need the following observations.

Lemma 2.3

Let \(G\times H\) be a balanced distance magic graph and let \((g,h)\) and \((g',h')\) with \(g\ne g'\) and \(h\ne h'\) be twin vertices for some balanced distance magic labeling. Then the labeling in which we exchange the labels of \((g',h')\) and \((g',h)\) is also balanced distance magic labeling with \((g,h)\) and \((g',h)\) being twin vertices.

Proof

Let \(\ell : V(G\times H)\rightarrow \{1,\ldots ,|V(G)||V(H)|\}\) be a balanced distance magic labeling where \((g,h)\) and \((g',h')\) are twin vertices with \(g\ne g'\) and \(h\ne h'\). Recall that \(N_{G \times H}(a,b)=N_G(a)\times N_H(b)\) for every \((a,b)\in V(G \times H)\) and that twin vertices have the same neighborhood. Thus we derive

$$\begin{aligned} N_{G\times H}(g,h)=N_{G\times H}(g',h')=N_{G\times H}(g',h)=N_{G\times H}(g,h') \end{aligned}$$

and we can exchange labelings of \((g',h')\) and \((g',h)\). By Proposition 2.1, \(\ell \) is still a balanced distance magic labeling with twin vertices \((g,h)\) and \((g',h)\). \(\square \)

This lemma has clearly a symmetric version if we exchange the labels of \((g',h')\) and \((g,h')\).

Lemma 2.4

Let \(G\times H\) be a balanced distance magic graph, and let \((g,h)\) and \((g',h)\) be twin vertices as well as \((g,h_1)\) and \((g,h_2)\) for some balanced distance magic labeling. The labeling in which we exchange the labels of \((g,h_2)\) and \((g',h_1)\) is balanced distance magic labeling with twins \((g,h_1)\) and \((g',h_1)\).

Proof

Let \(\ell : V(G\times H)\rightarrow \{1,\ldots ,|V(G)||V(H)|\}\) be a balanced distance magic labeling of \(G\times H\) where \(\{(g,h),(g',h)\}\) and \(\{(g,h_1),(g,h_2)\}\) are pairs of twin vertices. Hence \(N_{G\times H}(g,h)=N_{G\times H}(g',h)\) and therefore \(N_G(g)=N_G(g')\). Now as in the proof of Lemma 2.3 we have

$$\begin{aligned} N_{G\times H}(g,h_1)=N_{G\times H}(g,h_2)=N_{G\times H}(g',h_1)=N_{G\times H}(g',h_2) \end{aligned}$$

and we can exchange labels of \((g,h_2)\) and \((g',h_1)\). By Proposition 2.1, \(\ell \) is still a balanced distance magic labeling with twin vertices \((g,h_1)\) and \((g',h_1)\). \(\square \)

Lemma 2.5

Let \(G\times H\) be a balanced distance magic graph, and let \((g,h)\) and \((g',h)\) be twin vertices as well as \((g,h')\) and \((g'',h')\), \(g''\ne g'\), for some balanced distance magic labeling. The labeling in which we exchange the labels of \((g',h')\) and \((g'',h')\) is a balanced distance magic labeling where \((g,h')\) and \((g',h')\) are twin vertices.

Proof

Let \(\ell : V(G\times H)\rightarrow \{1,\ldots ,|V(G)||V(H)|\}\) be a balanced distance magic labeling of \(G\times H\) where \(\{(g,h),(g',h)\}\) and \(\{(g,h'),(g'',h')\}\) are pairs of twin vertices for \(g''\ne g'\). Hence \(N_{G\times H}(g,h)=N_{G\times H}(g',h)\) and \(N_{G\times H}(g,h')=N_{G\times H}(g'',h')\) and therefore \(N_G(g)=N_G(g')=N_G(g'')\). One can observe that

$$\begin{aligned} N_{G\times H}(g,h')=N_{G\times H}(g',h')=N_{G\times H}(g'',h') \end{aligned}$$

and we can exchange labels of \((g',h')\) and \((g'',h')\). By Proposition 2.1, \(\ell \) is still a balanced distance magic labeling with twin vertices \((g,h')\) and \((g',h')\). \(\square \)

Theorem 2.6

The direct product \(G\times H\) is balanced distance magic if and only if one of the graphs \(G\) and \(H\) is balanced distance magic and the other one is regular.

Proof

Assume first, without loss of generality, that G is regular and \(H\) is a balanced distance magic graph with \(V(H)=\{h_{1},\ldots ,h_{p}\}\), where the suffix indicates the label of a balanced distance magic labeling of \(H\). Thus for \( j\le \frac{p}{2}\), \(h_{p+1-j}\) is the twin vertex of \(h_{j}\). Recall that \(H\) is \(r_H\)-regular for an even \(r_{H}\). Let \(V(G)=\{g_{1},\ldots ,g_{t}\}\). Pairs \(((g_i,h_j),(g_i,h_{|V(H)|-j+1}))\), \(i\in \{1,\ldots ,t\}\) and \(j\in \{1,\ldots ,p/2\}\), form a partition of \(V(G\times H)\) with \(N_{G\times H}(g_i,h_j))=N_{G\times H}(g_i,h_{p-j+1})\). By Proposition 2.1 \(G\times H\) is a balanced distance magic graph.

Conversely, let \(G\times H\) be a balanced distance magic graph (this implies that \(G\times H\) is a regular graph and hence also both \(G\) and \(H\) are regular). There exists a balanced distance magic labeling \(\ell : V(G\times H) \rightarrow \{1,\ldots ,|V(G)||V(H)|\}\). First we show the following.

Claim There exists a balanced distance labeling of \(G\times H\) such that one of the following is true:

  1. 1.

    There exists an \(H\) -layer \(^{g}\!H\), such that the twin vertex of any \((g,h)\in {^{g}\!H}\) lies in \(^{g}\!H\).

  2. 2.

    There exists a \(G\) -layer \(G^{h}\), such that the twin vertex of any \((g,h)\in {G^{h}}\) lies in \(G^{h}\).

If there exists an \(H\)-layer or a \(G\)-layer such that the twin vertex of any vertex in this layer also lies within this layer, then we are done. Hence assume that this is not the case, i.e. for every \(H\)-layer \(^{g}\!H\) there exists a vertex \((g,h)\) such that its twin vertex \((g',h')\) has the property \(g'\ne g\) and for every \(G\)-layer \(G^h\) there exists a vertex \((g,h)\) such that its twin vertex \((g',h')\) has the property \(h'\ne h\).

We use Algorithm 1 to rearrange the labels of vertices in \(V(G\times H)\) in such a way that we either obtain an \(H\)-layer closed for twin vertices or we couple all \(H\)-layers, i.e. we find pairs of \(H\)-layers \(\{^{g}\!H,{^{g'}\!H}\}\) with the property that the twin vertex of a vertex \((g,h)\in ^g\!H\) lies in \({^{g'}\!H}\) and is of the form \((g',h)\). The latter case implies that all \(G\)-layers (and in particular one of them, say \(G^h\)) are closed for twins, and the claim is proved.

figure a

Observe that after step \(5\), all the vertices in \({^{g}\!H}\cup G^h\) have their twins in \({^{g}\!H}\cup G^h\). Step \(6\) guarantees that each application of the Lemma 2.5 in step \(7\) in fact exchanges the twins of a vertex in \(^{g}\!H\) and a vertex in \(G^h\).

Assume that some \(H\)-layer, say \(^{g}\!H\), is closed for twins. In particular for every pair of twins \((g,h_1),(g,h_2)\) we have

$$\begin{aligned} N_{G\times H}(g,h_1)=N_{G\times H}(g,h_2), \end{aligned}$$

and in consequence

$$\begin{aligned} N_H(h_1)=N_H(h_2), \end{aligned}$$

thus by the Proposition 2.1, \(H\) is balanced distance magic.

In the case when some \(G\)-layer \(G^{h}\) is closed for twins, we can prove in an analogous way that \(G\) is a balanced distance magic graph. \(\square \)

3 Distance Magic Graphs \(C_{m}\times C_{n}\)

Let \(V(C_{m}\times C_{n})=\{v_{i,j}:0\le i\le m-1,0\le j\le n-1\}\), where \(N(v_{i,j})=\{v_{i-1,j-1},v_{i-1,j+1},v_{i+1,j-1},v_{i+1,j+1}\}\) and operation on the first suffix is taken modulo \(m\) and on the second suffix modulo \(n\). We also refer to the set of all vertices \(v_{i,j}\) with fixed \(i\) as \(i\)-th row and with fixed \(j\) as \(j\)-th column.

We start with direct products of cycles that are not distance magic.

Theorem 3.1

If (\(m\not \equiv 0\;(\mathop {\mathrm{mod}}\nolimits 4)\) and \(n\ne 4\)) or (\(n\not \equiv 0\;(\mathop {\mathrm{mod}}\nolimits 4)\) and \(m\ne 4\)), then \(C_{m}\times C_{n}\) is not distance magic.

Proof

By commutativity of the direct product we can assume that \(m\not \equiv 0\;(\mathop {\mathrm{mod}}\nolimits 4)\) and \(n\ne 4\). Assume that \(C_{m}\times C_{n}\) is distance magic with some magic constant \(k\), which means there is a distance magic labeling \(\ell \). Let us consider sum of the labels of the neighbors of \(v_{i+1,j+1}\) and \(v_{i+3,j+1}\) for any \(i\in \{0,\dots ,m-1\}\) and \(j\in \{0,\dots ,n-1\}\), operations on indices taken modulo \(m\) and \(n\), respectively:

$$\begin{aligned} w(v_{i+1,j+1})=\ell (v_{i,j})+\ell (v_{i,j+2})+\ell (v_{i+2,j})+\ell (v_{i+2,j+2})=k, \end{aligned}$$
$$\begin{aligned} w(v_{i+3,j+1})=\ell (v_{i+2,j})+\ell (v_{i+2,j+2})+\ell (v_{i+4,j})+\ell (v_{i+4,j+2})=k. \end{aligned}$$

It implies that

$$\begin{aligned} \ell (v_{i,j})+\ell (v_{i,j+2})=\ell (v_{i+4,j})+\ell (v_{i+4,j+2}). \end{aligned}$$

Repeating that procedure we obtain that

$$\begin{aligned} \ell (v_{i,j})+\ell (v_{i,j+2})=\ell (v_{i+4\alpha ,j})+\ell (v_{i+4\alpha ,j+2}) \end{aligned}$$

for any natural number \(\alpha \).

It is well known that if \(a,b\in {{\mathbb {Z}}}_m\) and \( {\mathrm{gcd}}(a,m)={\mathrm{gcd}}(b,m)\), then \(a\) and \(b\) generate the same subgroup of \({{\mathbb {Z}}}_m\), that is, \( \langle a\rangle =\langle b\rangle \).

Since \(m\not \equiv 0\;(\mathop {\mathrm{mod}}\nolimits 4)\) we have \({\mathrm{gcd}} (2,m)={\mathrm{gcd}}(4,m)\) and \( 2\in \langle 4\rangle \), which implies that there exists \(\alpha ^{\prime }\) such that \(4\alpha ^{\prime }\equiv 2\;(\mathop {\mathrm{mod}}\nolimits m)\). We deduce that

$$\begin{aligned} \ell (v_{i,j})+\ell (v_{i,j+2})=\ell (v_{i+2,j})+\ell (v_{i+2,j+2})=\frac{k}{2}. \end{aligned}$$

Substituting \(j\) with \(j+2\) we obtain

$$\begin{aligned} \ell (v_{i,j+2})+\ell (v_{i,j+4})=\frac{k}{2}. \end{aligned}$$

Thus for every \(i, j\) we have

$$\begin{aligned} \ell (v_{i,j})=\ell (v_{i,j+4}), \end{aligned}$$

which leads to a contradiction, since \(n\ne 4\) and \(\ell \) is not a bijection. \(\square \)

Next we show that some of direct products of cycles are distance magic but not balanced distance magic. Used constructions are similar to those by Cichacz and Froncek in [4].

Theorem 3.2

If \(m,n\equiv 0\;(\mathop {\mathrm{mod}}\nolimits 4)\) and \(m,n>4\), then \(C_{m}\times C_{n}\) is distance magic but not balanced distance magic graph.

Proof

First we show that \(C_{m}\times C_{n}\) is distance magic. We define the labeling \(\ell \) by starting conditions (every second vertex of the row zero) followed by recursive rules that cover all the remaining vertices.

$$\begin{aligned} \ell (v_{0,4j+t})=\left\{ \begin{array}{lclcl} 2j+1, &{} \text {if} &{} \quad 0\le j \le \lceil \frac{n}{8}\rceil -1 &{} \text {and} &{}\quad t=0, \\ \frac{n}{2}-2j, &{} \text {if} &{}\quad \lceil \frac{n}{8}\rceil \le j \le \frac{n}{4} -1 &{} \text {and} &{} \quad t=0, \\ mn-2j-1, &{} \text {if} &{} \quad 0\le j \le \lfloor \frac{n}{8}\rfloor -1 &{} \text {and} &{} \quad t=2, \\ mn-\frac{n}{2}+2j+2, &{} \text {if} &{}\quad \lfloor \frac{n}{8}\rfloor \le j \le \frac{n}{4}-1 &{} \text {and} &{} \quad t=2. \end{array} \right. \end{aligned}$$

Note that we have used every label between \(1\) and \(\frac{n}{4}\) as well as between \(mn-\frac{n}{4}+1\) and \(mn\) exactly once for the starting conditions.

In the first recursive step we label every second vertex of row two in the order that is in a sense reverse to the one of row zero:

$$\begin{aligned} \ell (v_{2,j})=\left\{ \begin{array}{lcl} \ell (v_{0,n-2-j})+\frac{n}{4}, &{} \quad \text {if} &{} \quad \ell (v_{0,n-2-j})\le \frac{mn }{2}, \\ \ell (v_{0,n-2-j})-\frac{n}{4}, &{}\quad \text {if} &{} \quad \ell (v_{0,n-2-j})>\frac{mn}{2} , \end{array} \right. \end{aligned}$$

for \(j\in \{0,2,\ldots ,n-2\}\). Clearly we use in this step every label between \(\frac{n}{4}+1\) and \(\frac{n}{2}\) and between \(mn-\frac{n}{2}+1\) and \(mn-\frac{n}{4}\) exactly once.

We continue with every second vertex in every even row. Hence for \(2\le i\le \frac{m}{2}-1\) and for \(j\in \{0,2,\ldots ,n-2\}\) let

$$\begin{aligned} \ell (v_{2i,j})=\left\{ \begin{array}{lcl} \ell (v_{2i-4,j})+\frac{n}{2}, &{}\quad \text {if} &{}\quad \ell (v_{2i-4,j})\le \frac{mn}{ 2}, \\ \ell (v_{2i-4,j})-\frac{n}{2}, &{}\quad \text {if} &{} \quad \ell (v_{2i-4,j})>\frac{mn}{2}. \end{array} \right. \end{aligned}$$

Again all the labels here are used exactly once and are between \(\frac{n}{2}+1\) and \(\frac{mn}{8}\) and between \(mn-\frac{mn}{8}+1=\frac{7mn}{8}+1\) and \(mn- \frac{n}{2}\).

Next we label every second vertex of every odd row and complete with this all even columns. For \(0\le i\le \frac{m}{2}-1\) and for \(j\in \{0,2,\ldots ,n-2\}\) we set:

$$\begin{aligned} \ell (v_{2i+1,j})=\left\{ \begin{array}{lcl} \ell (v_{2i,j})+\frac{mn}{8} &{}\quad \text {if} &{} \quad \ell (v_{2i,j})\le \frac{mn}{2}, \\ \ell (v_{2i,j})-\frac{mn}{8} &{} \quad \text {if} &{} \quad \ell (v_{2i,j})>\frac{mn}{2}. \end{array} \right. \end{aligned}$$

Labels used here are between \(\frac{mn}{8}+1\) and \(\frac{mn}{4}\) and between \(\frac{3mn}{4}+1\) and \(\frac{7mn}{8}\).

Finally we use all the remaining labels between \(\frac{mn}{4}+1\) and \(\frac{ 3mn}{4}\) for all the vertices in every odd column. Thus for \(0\le i\le m-1\) and \(j\in \{1,3,\ldots ,n-1\}\) let:

$$\begin{aligned} \ell (v_{i,2j+1})=\left\{ \begin{array}{lcl} \ell (v_{i,2j})+\frac{mn}{4}, &{}\quad \text {if} &{} \ell (v_{i,2j})\le \frac{mn}{2}, \\ \ell (v_{i,2j})-\frac{mn}{4}, &{}\quad \text {if} &{}\quad \ell (v_{i,2j})>\frac{mn}{2}. \end{array} \right. \end{aligned}$$

Obviously the labeling \(\ell \) is a bijection from \(V(C_{m}\times C_{n})\) to \(\{1,\ldots ,mn\}\). It is also straightforward to see that \(k=2mn+2\) is the magic constant. Hence \(\ell \) is distance magic labeling.

However, \(\ell \) is not balanced distance magic, as none of the cycles \(C_m, C_n\) is (see Theorem 1.3) and thus their product cannot be balanced distance magic due to Theorem 2.6. \(\square \)

The example of distance magic labeling of \(C_{16}\times C_{16}\) is shown below, where \(v_{0,0}\) starts in lower left corner and the first index is for the row and the second for the column:

$$\begin{aligned} \begin{array}{cccccccccccccccc} 196 &{} 132 &{} 62 &{} 126 &{} 194 &{} 130 &{} 64 &{} 128 &{} 193 &{} 129 &{} 63 &{} 127 &{} 195 &{} 131 &{} 61 &{} 125 \\ 228 &{} 164 &{} 30 &{} 94 &{} 226 &{} 162 &{} 32 &{} 96 &{} 225 &{} 161 &{} 31 &{} 95 &{} 227 &{} 163 &{} 29 &{} 93 \\ 57 &{} 121 &{} 199 &{} 135 &{} 59 &{} 123 &{} 197 &{} 133 &{} 60 &{} 124 &{} 198 &{} 134 &{} 58 &{} 122 &{} 200 &{} 136 \\ 25 &{} 89 &{} 231 &{} 167 &{} 27 &{} 91 &{} 229 &{} 165 &{} 28 &{} 92 &{} 230 &{} 166 &{} 26 &{} 90 &{} 232 &{} 168 \\ 204 &{} 140 &{} 54 &{} 118 &{} 202 &{} 138 &{} 56 &{} 120 &{} 201 &{} 137 &{} 55 &{} 119 &{} 203 &{} 139 &{} 53 &{} 117 \\ 236 &{} 172 &{} 22 &{} 86 &{} 234 &{} 170 &{} 24 &{} 88 &{} 233 &{} 169 &{} 23 &{} 87 &{} 235 &{} 171 &{} 21 &{} 85 \\ 49 &{} 113 &{} 207 &{} 143 &{} 51 &{} 115 &{} 205 &{} 141 &{} 52 &{} 116 &{} 206 &{} 142 &{} 50 &{} 114 &{} 208 &{} 144 \\ 17 &{} 81 &{} 239 &{} 175 &{} 19 &{} 83 &{} 237 &{} 173 &{} 20 &{} 84 &{} 238 &{} 174 &{} 18 &{} 82 &{} 240 &{} 176 \\ 212 &{} 148 &{} 46 &{} 110 &{} 210 &{} 146 &{} 48 &{} 112 &{} 209 &{} 145 &{} 47 &{} 111 &{} 211 &{} 147 &{} 45 &{} 109 \\ 244 &{} 180 &{} 14 &{} 78 &{} 242 &{} 178 &{} 16 &{} 80 &{} 241 &{} 177 &{} 15 &{} 79 &{} 243 &{} 179 &{} 13 &{} 77 \\ 41 &{} 105 &{} 215 &{} 151 &{} 43 &{} 107 &{} 213 &{} 149 &{} 44 &{} 108 &{} 214 &{} 150 &{} 42 &{} 106 &{} 216 &{} 152 \\ 9 &{} 73 &{} 247 &{} 183 &{} 11 &{} 75 &{} 245 &{} 181 &{} 12 &{} 76 &{} 246 &{} 182 &{} 10 &{} 74 &{} 248 &{} 184 \\ 220 &{} 156 &{} 38 &{} 102 &{} 218 &{} 154 &{} 40 &{} 104 &{} 217 &{} 153 &{} 39 &{} 103 &{} 219 &{} 155 &{} 37 &{} 101 \\ 252 &{} 188 &{} 6 &{} 70 &{} 250 &{} 186 &{} 8 &{} 72 &{} 249 &{} 185 &{} 7 &{} 71 &{} 251 &{} 187 &{} 5 &{} 69 \\ 33 &{} 97 &{} 223 &{} 159 &{} 35 &{} 99 &{} 221 &{} 157 &{} 36 &{} 100 &{} 222 &{} 158 &{} 34 &{} 98 &{} 224 &{} 160 \\ 1 &{} 65 &{} 255 &{} 191 &{} 3 &{} 67 &{} 253 &{} 189 &{} 4 &{} 68 &{} 254 &{} 190 &{} 2 &{} 66 &{} 256 &{} 192 \end{array} \end{aligned}$$

Next theorem that completely describes distance magic graphs among direct product of cycles follows immediately by Theorems 3.1, 3.2, and 2.6.

Theorem 3.3

A graph \(C_{m}\times C_{n}\) is distance magic if and only if \(n=4\) or \(m=4\), or \(m,n\equiv 0\;(\mathop {\mathrm{mod}}\nolimits 4)\). Moreover, \(C_{m}\times C_{n}\) is balanced distance magic if and only if \(n=4\) or \(m=4\). \(\square \)