1 Introduction

In the literature one can find some variants of vertex coloring which model the frequency assigment problem (Hale 1980), e.g. the backbone coloring (Broersma 2003) or the \(L(p,q)\)-labeling (Griggs and Yeh 1992). These variants impose similar requirements on the colors assigned to adjacent or close enough vertices: their distance have to be greater than or equal to a given number. For instance, in the backbone coloring problem we are given a graph \(G\), its spanning subgraph \(H\) (backbone) and the following condition: the distance of the colors assigned to vertices \(u,\, v\) have to be at least 2 if they are adjacent in \(H\) or at least \(1\) if they are adjacent in \(G\). In this paper we introduce a similar concept which we call \(\xi \)-colorings.

Definition 1

Let \(G=(V,E)\) be a nonempty graph and \(\xi :E\rightarrow \mathbb {N}\) be a function. A function \(c:V\rightarrow \mathbb {Z}\) is  a  \(\xi \)-coloring of \(G\) if and only if \(|c(u)-c(v)|\ge \xi (uv)\) for each edge \(uv\in E\).

Clearly, \(\xi \)-colorings generalize backbone colorings. They are also related to \(L(p,q)\)-colorings since these are just \(\xi \)-colorings of \(G^2\) for function \(\xi \) defined as follows:

$$\begin{aligned} \xi (uv)= {\left\{ \begin{array}{ll} p, &{} \text {if } \, u,\, v \text { are adjacent in }\,G,\\ q, &{} \text {if } \, u,\, v \text { are of distance }\, 2 \hbox { in }G. \end{array}\right. } \end{aligned}$$

In most works related to the \(L(p,q)\)-labeling or the backbone coloring (see e.g. Calamoneri (2006); Yeh (2006) for a survey of results) the authors study the problem of finding colorings with minimal span, i.e. the difference between the largest and the smallest color used. For instance, it was shown that the \(L(p,q)\)-labeling problem is NP-complete even for bipartite planar graphs with degree \({\varDelta }\le 4\) (Janczewski et al. 2009) and polynomially solvable for trees in the case \(q=1\) (Yeh 2006) and for many simple graph classes: paths, cycles etc. It was also shown that the \(L(2,1)\)-labeling problem is polynomially solvable for cacti (Jonas 1993) and \(p\)-almost trees (Fiala and Kratochvil 2001) for every fixed \(p\).

Herein we focus on similar minimization criterion: the edge span.

Definition 2

Let \(c:V\rightarrow \mathbb {Z}\) be a \(\xi \)-coloring of a nonempty graph \(G=(V,E)\). The number \(\mathop {\mathrm {esp}}(c):=\max \{|c(u)-c(v)|:uv\in E\}\) is the edge span of \(c\).

Definition 3

Let \(G=(V,E)\) be a nonempty graph and \(\xi :E\rightarrow \mathbb {N}\) be a function. The number \(\mathop {\mathrm {esp}}(G,\xi ):=\min \{\mathop {\mathrm {esp}}(c):c\,\,\mathrm{is}\,\,\text {a}\,\,\xi \)-\({ coloring}\,\,\mathrm{of}\,\,G\}\) is the edge \(\xi \) -span of \(G\).

The edge span was studied in the context of the \(L(2,1)\)-labeling (Yeh 2000) and other coloring variants (Chang et al. 1999). It is important since it is used as a local optimization criterion in the frequency assigment problem.

The remainder of the paper is organized as follows. We begin by showing that it is NP-hard to compute the edge \(\xi \)-span of subcubic outerplanar graphs. Next, we show that the edge \(\xi \)-span and optimal \(\xi \)-colorings, i.e. \(\xi \)-colorings with minimal possible edge span, of a bipartite graph \(G\) may be found in \(O(n+m)\) time, where \(n\) is the number of vertices and \(m\) is the number of edges of \(G\). Section 4 deals with odd cycles. We present a formula for the edge \(\xi \)-span and an \(O(n\log n)\) algorithm that produces optimal \(\xi \)-colorings of \(C_{2n+1}\). The last section shows how these results can be used to obtain an \(O(n\log n)\) algorithm that produces optimal \(\xi \)-colorings of cacti.

2 Subcubic outerplanar graphs

It appears that computing the edge \(\xi \)-span is NP-hard even for relatively simple graphs.

Lemma 1

Let \(a<b\) be positive integers. If \(d_1\), \(d_2\) and \(d_3\) are integers such that \(|d_1|=b\), \(a\le |d_2|\le b\), \(b-a\le |d_3|\le b\) and \(d_1+d_2+d_3=0\) then \(|d_2|=a\) and \(|d_3|=b-a\).

Proof

Observe that \(|d_2+d_3|=|-d_1|=b\). There are two cases to consider.

  1. (a)

    \(|d_2+d_3|=|d_2|-|d_3|\) or \(|d_2+d_3|=|d_3|-|d_2|\). Then \(|d_2+d_3|<\max \{|d_2|,|d_3|\}\le b\)—a contradiction.

  2. (b)

    \(|d_2+d_3|=|d_2|+|d_3|\). Then \(b=a+b-a\le |d_2|+|d_3|=|d_2+d_3|=b\), which yields \(|d_2|=a\) and \(|d_3|=b-a\).

\(\square \)

Theorem 1

The following problem is NP-complete:

Instance::

A nonempty subcubic outerplanar graph \(G=(V,E)\), a function \(\xi :E\rightarrow \mathbb {N}\) and an integer \(k\).

Question::

Does \(\mathop {\mathrm {esp}}(G,\xi )\le k\)?

Proof

The problem is clearly in NP. To complete the proof we will show that there is a polynomial-time reduction from the well-known partition problem to our problem. Recall that the partition problem is NP-complete (Karp 1972) even in the following version:

Instance::

A sequence of positive integers \(a_1,\, a_2, \ldots , a_{2s}\).

Question::

Is there a sequence \(\varepsilon _1,\, \varepsilon _2,\ldots , \varepsilon _{2s}\) such that \(|\varepsilon _i|=1\) for all \(i\) and \(\sum _{i=1}^{2s}\varepsilon _ia_i=0\)?

Given a sequence \(a_1,\, a_2, \ldots , a_{2s}\), we construct an instance of our problem in the following way. Let \(H_i\) (\(1\le i\le 2s\)) be a complete graph with vertex set \(\{u_i,v_i,w_i\}\) and \(H\) be a graph whose connected components are \(H_1,\, H_2, \ldots , H_{2s}\). \(G\) arises from \(H\) by adding edges \(v_1u_2\), \(v_2u_3\), ..., \(v_{2s-1}u_{2s}\) and \(v_{2s}u_1\) (see Fig. 1 for an example). We set \(k=1+\sum _{i=1}^{2s}a_i\) and

$$\begin{aligned} \xi (e)= {\left\{ \begin{array}{ll} a_i, &{} \quad \text {if }\, e=u_iv_i, \\ k-a_i, &{} \quad \text {if }\, e=u_iw_i, \\ k, &{} \quad \text {otherwise.} \end{array}\right. } \end{aligned}$$

It is easy to see that \(G\) is subcubic, outerplanar and the above construction takes \(O(s)\) time. To complete the proof it suffices to show that \(\mathop {\mathrm {esp}}(G,\xi )\le k\) if and only if there is a sequence \(\varepsilon _1,\, \varepsilon _2, \ldots , \varepsilon _{2s}\) such that \(|\varepsilon _i|=1\) for all \(i\) and \(\sum _{i=1}^{2s}\varepsilon _ia_i=0\).

Fig. 1
figure 1

An example of the reduction for \(s=2\)

\((\Rightarrow )\) Let \(c\) be an optimal \(\xi \)-coloring of \(G\). Since \(\mathop {\mathrm {esp}}(G,\xi )\le k\), we have \(|c(u)-c(v)|=k\) for all edges satisfying \(\xi (uv)=k\). Combining this with Lemma 1, we get \(\xi (uv)=|c(u)-c(v)|\) for all edges of \(G\) since \(|c(v_i)-c(w_i)|=k,\, k\ge |c(u_i)-c(v_i)|\ge \xi (u_iv_i)=a_i\) and \(k\ge |c(u_i)-c(w_i)|\ge \xi (u_iw_i)=k-a_i\) for all \(i\). Obviously

$$\begin{aligned} \sum _{i=1}^{2s}\left( (c(v_i)-c(u_i))+(c(u_{1+(i \,\,\hbox {mod}\,\,2s)})-c(v_i))\right) =0. \end{aligned}$$

This sum is of form \(\sum _{i=1}^{2s}\varepsilon _ia_i+Ak\). Since \(k>\sum _{i=1}^{2s}a_i\) and the sum is 0, this gives \(A=0\) and \(\sum _{i=1}^{2s}\varepsilon _ia_i=0\).

\((\Leftarrow )\) Let \(c:V\rightarrow \mathbb {Z}\) be function given by

$$\begin{aligned} c(u)= {\left\{ \begin{array}{ll} \sum _{i=1}^{j-1}\varepsilon _ia_i, &{} \quad \text {if } \,u=u_j \text { and } j \,\text { is odd,} \\ k+\sum _{i=1}^{j-1}\varepsilon _ia_i, &{} \quad \text {if } \,u=u_j \text { and } j \text { is even,} \\ \sum _{i=1}^{j}\varepsilon _ia_i, &{} \quad \text {if } \,u=v_j \text { and } j \text { is odd,} \\ k+\sum _{i=1}^{j}\varepsilon _ia_i, &{} \quad \text {if } \,u=v_j \text { and } j \text { is even,} \\ c(u_i)+k-a_i, &{} \quad \text {if } u=w_i. \\ \end{array}\right. } \end{aligned}$$

It is easy to verify that \(c\) is a \(\xi \)-coloring of \(G\) and \(\mathop {\mathrm {esp}}(c)=k\). \(\square \)

3 Bipartite graphs

The following proposition implies that to obtain an optimal \(\xi \)-coloring of a bipartite graph \(G\) it suffices to find its partitions and color the vertices of the first one with \(0\) and the second with \(\max \xi (E)\). All these steps can be done in \(O(n+m)\) time.

Proposition 1

Let \(G\) be a nonempty graph and \(\xi :E\rightarrow \mathbb {N}\) be a function. Then

$$\begin{aligned} \max \xi (E)\le \mathop {\mathrm {esp}}(G,\xi )\le \max \xi (E)(\chi -1), \end{aligned}$$

where \(\chi \) is the chromatic number of \(G\).

Proof

The left-hand side inequality follows directly from the definition of the edge span and the \(\xi \)-coloring. The right-hand side is an easy consequence of the fact that if \(c\) is a coloring of \(G\) that uses colors 0, 1, ..., \(\chi -1\) then \(\max \xi (E)\cdot c\) is a \(\xi \)-coloring of \(G\) with edge span that equals \(\max \xi (E)(\chi -1)\). \(\square \)

4 Odd cycles

In this section, we consider an odd cycle \(C_{2n+1}\) and a function \(\xi :E(C_{2n+1})\rightarrow \mathbb {N}\). Let \(v_1\), \(v_2\), ..., \(v_{2n+1}\) be the vertices of \(C_{2n+1}\), numbered in such a way that \(v_i\) is adjacent to \(v_{1+i\,\,\hbox {mod}\,\,(2n+1)}\) for \(i=1, 2, \ldots , 2n+1\). Let \(\pi \) be a permutation of \(\{1, 2, \ldots , 2n+1\}\) such that the sequence

$$\begin{aligned} \xi _i:=\xi (v_{\pi (i)}v_{1+\pi (i)\,\,\hbox {mod}\,\,(2n+1)}) \end{aligned}$$

is nondecreasing.

Definition 4

A sequence \(r=(r_1, r_2, \ldots , r_{2n+1})\) is a sequence of cyclic differences if and only if

  1. (1)

    \(|r_i|\ge \xi _i\) for \(1\le i\le 2n+1\);

  2. (2)

    \(\sum _{i=1}^{2n+1}r_i=0\).

The number \(\Vert r\Vert :=\max _{1\le i\le 2n+1}|r_i|\) will be called the norm of \(r\).

Theorem 2

\(\mathop {\mathrm {esp}}(C_{2n+1},\xi )=\min \{\Vert r\Vert :r\text { is a sequence of cyclic differences}\}\).

Proof

It suffices to show that for every positive integer \(s\) the existence of a sequence of cyclic differences of norm \(s\) is equivalent to the existence of a \(\xi \)-coloring of \(C_{2n+1}\) with edge span \(s\).

\((\Rightarrow )\) Let \(r\) be a sequence of cyclic differences with norm \(s\). The formula \(c(v_i)=\sum _{j:\pi (j)<i}r_j\) defines the required \(\xi \)-coloring. Indeed,

$$\begin{aligned} \big |c(v_{1+i\,\,\hbox {mod}\,\,(2n+1)})-c(v_i)\big |=\big |r_{\pi ^{-1}(i)}\big | \end{aligned}$$

which gives \(|c(v_{1+i\,\,\hbox {mod}\,\,(2n+1)})-c(v_i)|\ge \xi _{\pi ^{-1}(i)}=\xi (v_{i}v_{1+i\,\,\hbox {mod}\,\,(2n+1)})\) and \(\mathop {\mathrm {esp}}(c)=s\).

\((\Leftarrow )\) Let \(c\) be a \(\xi \)-coloring of \(C_{2n+1}\) with edge span \(s\). It is easy to verify that the formula \(r_i=c(v_{1+\pi (i)\,\,\hbox {mod}\,\,(2n+1)})-c(v_{\pi (i)})\) defines the required sequence of cyclic differences. \(\square \)

The proof of the above theorem shows that finding an optimal \(\xi \)-coloring of \(C_{2n+1}\) reduces to finding of a sequence of cyclic differences with minimal possible norm. The transition from one problem to the other requires two steps: we must sort the multiset \(\xi (E)\), compute the permutation \(\pi \) and its inverse \(\pi ^{-1}\). All these steps can be done in \(O(n\log n)\) time.

Lemma 2

Let \(r\) be a sequence of cyclic differences.

  1. (1)

    \(-r\) is a sequence of cyclic differences.

  2. (2)

    The sequence \(r'\) that arises as a result of sorting \(r\) in order of nondecreasing absolute values is a sequence of cyclic differences.

Proof

  1. (1)

    Obvious.

  2. (2)

    It suffices to show that swapping \(r_i\) with \(r_j\), where \(i<j\) and \(|r_i|>|r_j|\), results in a sequence of cyclic differences. It holds since swapping does not change the sum of the sequence, \(|r_i|>|r_j|\ge \xi _j\) and \(|r_j|\ge \xi _j\ge \xi _i\).

\(\square \)

Lemma 3

If there exists an integer \(k\) such that \(n+1\le k\le 2n\) and \(\sum _{i=1}^k\xi _i\le (2n+1-k)\xi _{2n+1}\) then there exist an integer \(s\) such that \(0\le s\le k-2\) and the sequence \(r\) given by

$$\begin{aligned} r_i= {\left\{ \begin{array}{ll} -\xi _{2n+1}, &{} \quad \mathrm{{if }} \; 1\le i\le s, \\ \sum _{j=s+2}^k\xi _j-(2n+1-k-s)\xi _{2n+1}, &{} \quad \mathrm{{if }} \; i=s+1, \\ -\xi _i, &{} \quad \mathrm{{if }}\; s+2\le i\le k, \\ \xi _{2n+1}, &{} \quad \mathrm{{if }} \; k<i\le 2n+1 \end{array}\right. } \end{aligned}$$

is a sequence of cyclic differences with norm \(\xi _{2n+1}\).

Proof

Let us notice that for all possible values of \(s\) we have \(\xi _i\le |r_i|\le \xi _{2n+1}\) for \(i\ne s+1\) and \(\sum _{i=1}^{2n+1}r_i=-s\xi _{2n+1}+\sum _{i=s+2}^k\xi _i-(2n+1-k-s)\xi _{2n+1}-\sum _{i=s+2}^k\xi _i+(2n+1-k)\xi _{2n+1}=0\). To complete the proof it suffices to show that there is \(s\) such that \(\xi _{s+1}\le |r_{s+1}|\le \xi _{2n+1}\).

Let \(\delta :\{0,1,\ldots ,k-1\}\rightarrow \mathbb {Z}\) be the function given by \(\delta (j)=(2n+1-k-j)\xi _{2n+1}- \sum _{i=j+1}^k\xi _i\). We know that \(\delta (0)=\xi _{2n+1}(2n+1-k)-\sum _{i=1}^k\xi _i\ge 0\). Moreover \(\delta (k-1)=2(n+1-k)\xi _{2n+1}-\xi _k\le -\xi _k<0\) and \(\delta (j+1)-\delta (j)=\xi _{j+1}-\xi _{2n+1}\le 0\), so there must be \(s\) such that \(0\le s\le k-2\), \(\delta (s)\ge 0\) and \(\delta (s+1)<0\). To complete the proof it suffices to note that \(|r_{s+1}|=\xi _{s+1}+\delta (s)\) and \(\xi _{s+1}\le \xi _{s+1}+\delta (s)<\xi _{s+1}+\delta (s)-\delta (s+1)=\xi _{2n+1}\). \(\square \)

Theorem 3

The following conditions are equivalent:

  1. (1)

    there exists an integer \(k\) such that \(n+1\le k\le 2n\) and \(\sum _{i=1}^k\xi _i\le (2n+1-k)\xi _{2n+1}\);

  2. (2)

    there exists a sequence of cyclic differences with norm \(\xi _{2n+1}\).

Proof

\((\Rightarrow )\) Follows immediately from Lemma 3.

\((\Leftarrow )\) For every sequence of cyclic differences \(r\) with norm \(\xi _{2n+1}\) we define a parameter \(\zeta (r)=\sum _{i:r_i>0}|\xi _{2n+1}-r_i|\). It is easy to see that \(\zeta (r)\ge 0\) and \(\zeta (r)=0\) if and only if \(r_i=\xi _{2n+1}\) for each \(r_i>0\).

Let \(r\) be a sequence of cyclic differences with norm \(\xi _{2n+1}\) such that \(\zeta (r)\) is minimal. Suppose that \(\zeta (r)>0\). Then there is \(j\) such that \(0<r_j<\xi _{2n+1}\). By Lemma 2, \(-r\) is a sequence of cyclic differences. Since \(\Vert {-r}\Vert =\Vert r\Vert \), we have \(\zeta (-r)>0\) and there must be \(l\) such that \(0<-r_l<\xi _{2n+1}\). But then the sequence \(r'\) given by

$$\begin{aligned} r'_i= {\left\{ \begin{array}{ll} r_j+1, &{} \quad \text {if }\, i=j, \\ r_l-1, &{} \quad \text {if }\, i=l, \\ r_i, &{} \quad \text {otherwise,} \end{array}\right. } \end{aligned}$$

would be a sequence of cyclic differences with \(\Vert r'\Vert =\xi _{2n+1}\) and \(\zeta (r')=\zeta (r)-1\)—a contradiction. Hence \(\zeta (r)=0\) and \(r_i=\xi _{2n+1}\) for all \(r_i>0\). Let \(k=|\{i:r_i<0\}|\). By Lemma 2, the sequence \(r''\) resulting from \(r\) by sorting it in order of nondecreasing absolute values is a sequence of cyclic differences. Since only negative elements of \(r\) may have absolute value less than \(\xi _{2n+1}\), we may assume that \(r''_i<0\) if and only if \(i\le k\). Hence

$$\begin{aligned} (2n+1-k)\xi _{2n+1}=\sum _{i=k+1}^{2n+1}r''_i=-\sum _{i=1}^{k}r''_i=\sum _{i=1}^{k}|r''_i|, \end{aligned}$$

which, combined with \(\sum _{i=1}^{k}\xi _i\le \sum _{i=1}^{k}|r''_i|\le k\xi _{2n+1}\), gives immediately \(k\ge n+1\) and \(\sum _{i=1}^k\xi _i\le (2n+1-k)\xi _{2n+1}\). This completes the proof since \(k\le 2n\) is obvious. \(\square \)

The above theorem combined with Lemma 3 leads to a linear algorithm that can verify if there is a sequence of cyclic differences of norm \(\xi _{2n+1}\). The algorithm, if the answer is yes, will give us a formula describing the required sequence. Indeed, it suffices to verify for \(k=n+1, n+2, \ldots , 2n\) whether \(\sum _{i=1}^k\xi _i\le (2n+1-k)\xi _{2n+1}\), and, if such \(k\) was found, use the formula of Lemma 3.

Lemma 4

If there exists an integer \(k\) such that \(1\le k\le 2n\) and \(\sum _{i=1}^k\xi _i>(2n+1-k)\xi _{2n+1}\) then the sequence \(r\) given by

$$\begin{aligned} r_i= {\left\{ \begin{array}{ll} -\xi _i, &{} \mathrm{{if }} \; i\le k, \\ q, &{} \mathrm{{if }} \; k+1\le i\le 2n+1-s, \\ q+1, &{} \mathrm{{if }} \; i>2n+1-s, \end{array}\right. } \end{aligned}$$

where \(q\) is the quotient and \(s\) is the remainder from the division of \(\sum _{i=1}^k\xi _i\) by \(2n+1-k\), is a sequence of cyclic differences with norm \(\lceil \sum _{i=1}^k\xi _i/(2n+1-k)\rceil \).

Proof

Since \(\sum _{i=1}^k\xi _i>(2n+1-k)\xi _{2n+1}\), we have \(q\ge \xi _{2n+1}\). To complete the proof, it suffices to observe that \(\sum _{i=1}^{2n+1}r_i=-\sum _{i=1}^k\xi _i+q(2n+1-s-k)+(q+1)s=-\sum _{i=1}^k\xi _i+q(2n+1-k)+s=0\). \(\square \)

Theorem 4

Suppose that every sequence of cyclic differences is of norm greater than \(\xi _{2n+1}\). If \(r\) is a sequence of cyclic differences with the minimal possible norm then there is an integer \(k\) such that \(1\le k\le 2n\), \(\sum _{i=1}^k\xi _i>(2n+1-k)\xi _{2n+1}\) and

$$\begin{aligned} \Vert r\Vert \ge \left\lceil \sum _{i=1}^k\frac{\xi _i}{2n+1-k}\right\rceil . \end{aligned}$$

Proof

For a given sequence of cyclic differences \(s\) we define a parameter

$$\begin{aligned} \zeta _1(s)=\min \left\{ \sum _{i:s_i<0}|s_i+\xi _i|,\sum _{i:s_i>0}|s_i-\xi _i|\right\} . \end{aligned}$$

It is easy to see that \(\zeta _1(s)\ge 0\) and \(\zeta _1(s)=0\) if and only if holds at least one of the following conditions:

  • (a) for all \(i\) such that \(s_i<0\) we have \(s_i=-\xi _i\);

  • (b) for all \(i\) such that \(s_i>0\) we have \(s_i=\xi _i\).

Let \(s\) be a sequence of cyclic differences such that \(\Vert s\Vert =\Vert r\Vert \) and the value of \(\zeta _1(s)\) is minimal. If \(\zeta _1(s)>0\) then there would exist \(l\) and \(j\) such that \(s_l<-\xi _l,\, s_j>\xi _j\) and the function given by

$$\begin{aligned} s'_i= {\left\{ \begin{array}{ll} s_i, &{} \quad \text {if } \; i\ne j \hbox { and } i\ne l, \\ s_l+1, &{} \quad \text {if } \; i=l, \\ s_j-1, &{} \quad \text {if } \; i=j, \end{array}\right. } \end{aligned}$$

would be a sequence of cyclic differences satisfying \(\Vert s'\Vert =\Vert r\Vert \) and \(\zeta _1(s')=\zeta _1(s)-1\)—a contradiction. Hence \(\zeta _1(s)=0\). Since \(\zeta _1(-s)=\zeta _1(s)\) and \(\Vert s\Vert =\Vert {-s}\Vert \), we may assume without loss of generality that \(s\) satisfies condition \((a)\).

For all sequences of cyclic differences \(s\) that satisfy \((a)\) and the equality \(\Vert s\Vert =\Vert r\Vert \) we define a parameter

$$\begin{aligned} \zeta _2(s)=\min \left\{ \sum _{i:s_i>0}\max \{s_i-\xi _{2n+1},0\},\sum _{i:s_i>0}\max \{\xi _{2n+1}-s_i,0\} \right\} . \end{aligned}$$

Obviously, \(\zeta _2(s)\ge 0\) and \(\zeta _2(s)=0\) if and only if at least one of the following conditions hold

  • (c) for all \(i\) such that \(s_i>0\) we have \(s_i\le \xi _{2n+1}\);

  • (d) for all \(i\) such that \(s_i>0\) we have \(s_i\ge \xi _{2n+1}\).

Let \(s\) be a sequence of cyclic differences that satisfies \((a)\), the equality \(\Vert s\Vert =\Vert r\Vert \) and has the minimal value of \(\zeta _2(s)\). If \(\zeta _2(s)>0\) then there would exist \(l\) and \(j\) such that \(0<s_l<\xi _{2n+1},\, s_j>\xi _{2n+1}\) and the function given by

$$\begin{aligned} s'_i= {\left\{ \begin{array}{ll} s_i, &{} \quad \text {if } \; i\ne j \hbox { and } i\ne l, \\ s_l+1, &{} \quad \text {if } \; i=l, \\ s_j-1, &{} \quad \text {if } \; i=j, \end{array}\right. } \end{aligned}$$

would be a sequence of cyclic differences satisfying \((a)\), the equality \(\Vert s'\Vert =\Vert r\Vert \) and \(\zeta _2(s')=\zeta _2(s)-1\)—a contradiction. Hence \(\zeta _2(s)=0\), which means that \(s\) satisfies \((d)\), otherwise \((c)\) would be satisfied and \(\Vert s\Vert =\xi _{2n+1}<\Vert r\Vert \). Let \(k=|\{i:s_i<0\}|\). By Lemma 2 the sequence \(s''\) resulting from sorting \(s\) in order of nondecreasing absolute values is a sequence of cyclic differences. Since only the negative elements of \(s\) may have absolute value less than \(\xi _{2n+1}\), we may assume that \(s''_i<0\) if and only if \(i\le k\). Hence

$$\begin{aligned} \sum _{i=1}^k\xi _i=-\sum _{i=1}^ks''_i=\sum _{i=k+1}^{2n+1}s''_i, \end{aligned}$$

which along with \(\sum _{i=k+1}^{2n+1}s''_i>(2n+1-k)\xi _{2n+1}\) (at least one is greater than \(\xi _{2n+1}\)) and \(\Vert r\Vert =\Vert s''\Vert \ge \max \{s''_i:s''_i>0\}\ge \lceil \sum _{i=k+1}^{2n+1}s''_i/(2n+1-k)\rceil = \lceil \sum _{i=1}^k\xi _i/(2n+1-k)\rceil \)) completes the proof. \(\square \)

Observe that from Lemma 4 it follows that the inequality \(\Vert r\Vert \ge \lceil \sum _{i=1}^k\xi _i/(2n+1-k)\rceil \) must be equality for some \(k\). This means that this theorem along with Lemma 4 leads to another linear algorithm which this time finds a sequence of cyclic differences with minimal norm under the assumption that there is no sequence of cyclic differences with norm \(\xi _{2n+1}\). Combining these algorithms we obtain a linear algorithm that finds a sequence of cyclic differences with minimal norm. We also obtain the following formula:

  1. (1)

    if there is \(k\) such that \(n+1\le k\le 2n\) and \(\sum _{i=1}^k\xi _i\le (2n+1-k)\xi _{2n+1}\) then \(\mathop {\mathrm {esp}}(G,\xi )=\xi _{2n+1}\);

  2. (2)

    otherwise \(\mathop {\mathrm {esp}}(G,\xi )=\min \{\lceil \sum _{i=1}^k\frac{\xi _i}{2n+1-k}\rceil :1\le k\le 2n \wedge \sum _{i=1}^k\xi _i>(2n+1-k)\xi _{2n+1}\}\).

5 Cacti

A connected graph \(G\) is a cactus if and only if every edge belongs to at most one cycle. It is known that \(G\) is a cactus if and only if there exists a sequence of graphs \(G_1\), \(G_2\), ..., \(G_k\), called a decomposition of \(G\), such that:

  1. (1)

    \(G_i\) is a cycle or a path \(P_2\) for \(i=1,2, \ldots , k\);

  2. (2)

    \(G_i\) has exactly one vertex in common with \(G_1\cup G_2\cup \ldots \cup G_{i-1}\) for \(i=2,3, \ldots , k\);

  3. (3)

    \(G=G_1\cup G_2\cup \ldots \cup G_k\).

Verification whether \(G\) is a cactus and, if the answer is yes, obtaining a decomposition is executable in a linear time. Now we are ready to formulate and prove our main result.

Theorem 5

Let \(G\) be a nonempty cactus, \(G_1, G_2, \ldots , G_k\) be its decomposition and \(\xi :E\rightarrow \mathbb {N}\) be a function. Then

$$\begin{aligned} \mathop {\mathrm {esp}}(G,\xi )=\max _{1\le i\le k}\mathop {\mathrm {esp}}(G_i,\xi _i), \end{aligned}$$

where \(\xi _i:=\xi |_{E(G_i)}\).

Proof

The inequality \(\mathop {\mathrm {esp}}(G,\xi )\ge \max _{1\le i\le k}\mathop {\mathrm {esp}}(G_i,\xi _i)\) follows from the fact that \(G_i\) is a subgraph of \(G\) and \(\xi _i=\xi |_{E(G_i)}\). To complete the proof we use induction on \(j\) to show that \(\mathop {\mathrm {esp}}(G'_j,\xi '_j)\le \max _{1\le i\le j}\mathop {\mathrm {esp}}(G_i,\xi _i)\), where \(G'_j:=G_1\cup G_2\cup \ldots \cup G_j\) and \(\xi '_j:=\xi |_{E(G'_j)}\).

This is obvious for \(j=1\) since \(G'_1=G_1\) and \(\xi '_1=\xi _1\). Assume that the inequality holds for \(j-1\), i.e. \(\mathop {\mathrm {esp}}(G'_{j-1},\xi '_{j-1})\le \max _{1\le i\le j-1}\mathop {\mathrm {esp}}(G_i,\xi _i)\). Let \(c'\) be an optimal \(\xi '_{j-1}\)-coloring of \(G'_{j-1}\) and \(c_j\) be an optimal \(\xi _j\)-coloring of \(G_j\). Let \(v\) be the only common vertex of \(G'_{j-1}\) and \(G_j\). It is easy to see that the function \(c:V(G'_j)\rightarrow \mathbb {Z}\) given by

$$\begin{aligned} c(u)= {\left\{ \begin{array}{ll} c'(u), &{} \quad \text {if } \, u\in V(G'_{j-1}), \\ c_j(u)+c'(v)-c_j(v), &{} \quad \text {if } \, u\in V(G_j), \end{array}\right. } \end{aligned}$$

is a well-defined \(\xi '_j\)-coloring of \(G'_j\). Moreover \(\mathop {\mathrm {esp}}(c)=\max \{\mathop {\mathrm {esp}}(c'),\mathop {\mathrm {esp}}(c_j)\}\), so \(\mathop {\mathrm {esp}}(G'_j,\xi '_j) \le \mathop {\mathrm {esp}}(c)=\max \{\mathop {\mathrm {esp}}(G'_{j-1},\xi '_{j-1}),\mathop {\mathrm {esp}}(G_j,\xi _j)\}\le \max _{1\le i\le j}\mathop {\mathrm {esp}}(G'_i,\xi '_i)\). \(\square \)

The above proof shows how to construct an optimal \(\xi \)-coloring of a cactus \(G\) provided that we have its decomposition \(G_1,\, G_2, \ldots , G_k\) and optimal \(\xi _i\)-colorings. The former can be done in \(O(n)\), the latter was proved to be executable in \(O(n\log n)\) time since the elements of the decomposition are paths and cycles. The construction presented in the proof is clearly linear, thus the overall complexity of this construction is \(O(n\log n)\).