1 Introduction

Graph colouring is a well-studied topic in Computer Science and Discrete Mathematics, both for theoretical and practical reasons. The classical variant is to give each vertex of a graph a colour in such a way that two adjacent vertices are not coloured alike while using as few colours as possible. Formally, a colouring of a graph \(G=(V,E)\) is a mapping \(c:V\rightarrow \{1,2,\ldots \}\) such that \(c(u)\ne c(v)\) for each pair of vertices u, v with \(uv\in E\). If \(c(V)\subseteq \{1,\ldots ,k\}\), then c is also called a k-colouring. The integer c(u) is the colour of u, and the set of all vertices of the same colour i is a colour class of c. The problem Colouring is to decide if a given graph G has a k-colouring for some given integer k. It is well-known that Colouring is NP-complete even if \(k=3\).

We prove new results for a well-studied variant of graph colouring. We denote the disjoint union two graphs F and G by \(F+G=(V(F)\cup V(G),E(F)\cup E(G))\); the disjoint union of s copies of G by sG and the n-vertex path by \(P_n\). A colouring is injective if the union of any two colour classes of c induces an \(sP_1+tP_2\) for some integers \(s\ge 0\) and \(t\ge 0\). Injective colourings are also known as L(1, 1)-labellings or distance-2 colourings. An equivalent formulation is to say that a colouring of a graph G is injective if all the neighbours of every vertex of G are coloured differently. The problem Injective Colouring is to decide whether a given graph has an injective k-colouring for some given integer k. In the literature (see, for example, [6, 8, 10]) it is sometimes allowed that injective colourings give adjacent vertices the same colour. This is in contrast to our paper: we emphasize that all colourings in our paper are proper (as can be seen from their definitions).

It is known that Injective Colouring is NP-complete for split graphs [1], unit disk graphs [15] and planar graphs [13], respectively. Moreover, Injective Colouring is NP-complete for line graphs of bipartite graphs of girth at least g for any fixed integer \(g\ge 3\) [14] and cubic graphs [7] even when \(k=4\). On the positive side, Injective Colouring is polynomial-time solvable for graphs of bounded treewidth [16]. Injective colourings, viewed as L(1, 1)-labellings, belong to the distance constrained labelling framework; we refer to the L(hk)-labelling survey of Calamoneri [3] for more algorithmic and structural results.

In the above results of [1, 7, 13,14,15,16] the input is restricted to some special graph class. Restricting the input is also the focus in our paper. Our goal is to obtain complexity dichotomies, which we compare with similar dichotomies for Colouring. In particular we consider classes of H-free graphs, that is, graphs that do not contain some fixed graph H as an induced subgraph, or equivalently, cannot be modified into H by a sequence of vertex deletions. On a side note, the difference between the chromatic number \(\chi \) and injective chromatic number \(\chi _i\) of a graph can be arbitrarily large as illustrated by the \((n+1)\)-vertex star \(K_{1,n}\), which has chromatic number 2 but injective chromatic number \(n+1\).

1.1 Dichotomies for Colouring

Král’ et al. [12] completely classified the complexity of Colouring for H-free graphs. We write \(H_1\subseteq _iH_2\) to say that \(H_1\) is an induced subgraph of \(H_2\).

Theorem 1

([12]). Let H be a graph. Then Colouring for H-free graphs is polynomial-time solvable if \(H\subseteq _iP_1+P_3\) or \(H\subseteq _iP_4\) and NP-complete otherwise.

The complexity of k -Colouring, the variant of Colouring where k is fixed (in other words, not part of the input) has not been classified yet; in particular there are infinite families of open cases when H is a linear forest (disjoint union of paths); for example, the complexities of 3-Colouring for \(P_t\)-free graphs for \(t\ge 8\) and k-Colouring for \(sP_3\)-free graphs for \(s\ge 2\) and \(k\ge 4\) are still open; see also [4, 5, 11].

A set of vertices in a graph G is independent if each pair of vertices is non-adjacent. The independence number \(\alpha (G)\) of G is the size of a largest independent set of G. The class of \(sP_1\)-free graphs coincides with the class of graphs with \(\alpha \le s-1\). Hence, Theorem 1 immediately implies the following dichotomy.

Theorem 2

Let \(s\ge 1\) be an integer. Then Colouring for graphs with \(\alpha \le s\) is polynomial-time solvable if \(s\le 2\) but NP-complete if \(s\ge 3\).

The complement of a graph G is the graph \(\overline{G}\) with vertex set V(G) and an edge between two distinct vertices u and v if and only if \(uv\notin E(G)\). A k-colouring of G is a partition of V(G) into at most k independent sets. Hence, a (k-)colouring of G corresponds to a (k-)clique-covering of \(\overline{G}\), which is a partition of \(V(\overline{G})=V(G)\) into (at most k) cliques. The clique covering number \(\overline{\chi }(G)\) of G is the smallest number of cliques in a clique-covering of G. Note that \(\overline{\chi }(G)=\chi (\overline{G})\). The following theorem follows from a known result of [9] and a standard trick (see Sect. 2).

Theorem 3

Let \(s\ge 1\) be an integer. Then Colouring for graphs with \(\overline{\chi }\le s\) is polynomial-time solvable if \(s\le 2\) but NP-complete if \(s\ge 3\).

Note that Theorems 2 and 3 are incomparable: for \(p\ge 2\), the class of graphs with \(\alpha \le p\) is a proper superclass of the class of graphs with \(\overline{\chi }\le p\). For example, as the class of \(K_3\)-free graphs properly contains the class of bipartite graphs, the class of \(3P_1\)-free graphs, that is, graphs with \(\alpha \le 2\), properly contains the class of co-bipartite graphs, that is, graphs with \(\overline{\chi }\le 2\). Hence, the polynomial part of Theorem 2 is stronger than the polynomial part of Theorem 3, and the reverse statement holds for the hardness parts of these two dichotomies.

1.2 Dichotomies for Injective Colouring

In contrast to the situation for k -Colouring, the variant of Injective Colouring where the number k of colours is fixed has been completely settled: in [2] we proved the following dichotomy for Injective k -Colouring; note that Injective 3-Colouring is polynomial-time solvable, as the only yes-instances are graphs of maximum degree 2 (disjoint unions of cycles and paths).

Theorem 4

Let H be a graph and \(k\ge 4\) be an integer. Then Injective k -Colouring for H-free graphs is polynomial-time solvable if H is a linear forest and NP-complete otherwise.

Our first result, proven in Sect. 2, is a similar dichotomy as Theorem 2.

Theorem 5

Let \(s\ge 1\) be an integer. Then Injective Colouring for graphs with \(\alpha \le s\) is polynomial-time solvable if \(s\le 3\) but NP-complete if \(s\ge 4\).

Comparing Theorem 2 with Theorem 5, we see that the jump in complexity for Colouring happens when \(s=3\) instead of \(s=4\). This has the following consequence. It is well known and easy to see that an injective colouring of a graph G is a colouring of the square \(G^2\) of G. The latter graph is obtained from G by adding an edge between every pair of non-adjacent vertices in G that have a common neighbour in G. If G has \(\alpha \le 3\), then \(G^2\) has \(\alpha \le 3\) as well. However, Colouring for graphs with \(\alpha \le 3\) is NP-complete by Theorem 2. Hence, we do not have any polynomial-time algorithm for Colouring that we can use. Instead of this, we will develop a direct approach to obtain polynomial-time solvability of Injective Colouring for \(4P_1\)-free graphs (on a side note, it can be observed that the hard instances with \(\alpha =3\) of Colouring are not squares of graphs).

The hardness part of Theorem 5 follows from the hardness part of the following analogue of Theorem 3 for Injective Colouring, which we prove in Sect. 2.

Theorem 6

Let \(s\ge 1\) be an integer. Then Injective Colouring for graphs with \(\overline{\chi }\le s\) is polynomial-time solvable if \(s\le 3\) but NP-complete if \(s\ge 4\).

In [2] we combined known results from [1, 14] with some new results to obtain a partial dichotomy for Injective Colouring where we left open ten cases of linear forests H, including the cases \(H=4P_1\) and \(H=5P_1\), which we now have solved (these casess correspond to graphs with \(\alpha \le 3\) and \(\alpha \le 4\), respectively, so follow from Theorem 5). In this paper we solve a total of nine open cases. In Sect. 3 we use our result for \(4P_1\)-free graphs from Theorem 5 to obtain new polynomial-time algorithms for two superclasses of \(4P_1\)-free graphs, namely when \(H=2P_1+P_3\) and \(H=3P_1+P_2\). In the same section we also prove polynomial-time solvability if \(H=P_1+P_4\). As a consequence of our results we can update the partial dichotomy of [2] as follows, leaving only one missing case.

Theorem 7

Let \(H\ne 2P_1+P_4\) be a graph. Then Injective Colouring for H-free graphs is polynomial-time solvable if \(H\subseteq _iP_1+P_4\) or \(H\subseteq _i2P_1+P_3\) or \(H\subseteq _i3P_1+P_2\) and NP-complete otherwise.

Compared to Theorem 1 we notice that Injective Colouring is polynomial-time solvable for larger classes of H-free graphs than Colouring. We prove Theorem 7 in Sect. 3 as well and state some relevant open problems in Sect. 4.

2 The Proofs of Theorems 3, 5 and 6

An injective colouring c of a graph G is optimal if G has no injective colouring using fewer colours than c. An injective colouring c is \(\ell \) -injective if every colour class of c has size at most \(\ell \). An \(\ell \)-injective colouring c of a graph G is \(\ell \) -optimal if G has no \(\ell \)-injective colouring that uses fewer colours than c. We start with a useful lemma for the case where \(\ell =2\) that we will also use in the next section.

Lemma 1

An optimal 2-injective colouring of a graph G can be found in polynomial time.

Proof

Let c be a 2-injective colouring of G. Then each colour class of size 2 in G corresponds to a dominating edge of \(\overline{G}\) (an edge uv of a graph is dominating if every other vertex in the graph is adjacent to at least one of uv). Hence, the end-vertices of every non-dominating edge in \(\overline{G}\) have different colours in G. Algorithmically, this means we may delete every non-dominating edge of \(\overline{G}\) from \(\overline{G}\); note that we do not delete the end-vertices of such an edge.

Let \(\mu ^*\) be the size of a maximum matching in the graph obtained from \(\overline{G}\) after deleting all non-dominating edges of \(\overline{G}\). The edges in such a matching will form exactly the colour classes of size 2 of an optimal 2-injective colouring of G. Hence, the injective chromatic number of G is equal to \(\mu ^*+(|V(G)|-2\mu ^*)\). It remains to observe that we can find a maximum matching in a graph in polynomial time by using a standard algorithm.    \(\square \)

We are now ready to prove the following result.

Lemma 2

Injective Colouring is polynomial-time solvable for \(4P_1\)-free graphs, or equivalently, graphs with \(\alpha \le 3\), and thus for graphs with \(\overline{\chi }\le 3\).

Proof

Let \(G=(V,E)\) be a \(4P_1\)-free graph on n vertices. We first analyze the structure of injective colourings of G. Let c be an optimal injective colouring of G. As G is \(4P_1\)-free, every colour class of c has size at most 3. From all optimal injective colourings, we choose c such that the number of size-3 colour classes is as small as possible. We say that c is class-3-optimal.

Suppose c contains a colour class of size 3, say colour 1 appears on three distinct vertices \(u_1\), \(u_2\) and \(u_3\) of G. As G is \(4P_1\)-free, \(\{u_1,u_2,u_3\}\) dominates G. As c is injective, this means that every vertex in \(G-\{u_1,u_2,u_3\}\) is adjacent to exactly one vertex of \(\{u_1,u_2,u_3\}\). Hence, we can partition \(V\setminus \{u_1,u_2,u_3\}\) into three sets \(T_1\), \(T_2\) and \(T_3\), such that for \(i\in \{1,2,3\}\), every vertex of \(T_i\) is adjacent to \(u_i\) and not to any other vertex of \(\{u_1,u_2,u_3\}\). If two vertices \(t,t'\) in the same \(T_i\), say \(T_1\), are non-adjacent, then \(\{t,t',u_2,u_3\}\) induces a \(4P_1\), a contradiction. Hence, we partitioned V into three cliques \(T_i\cup \{u_i\}\). We call the cliques \(T_1\), \(T_2\), \(T_3\), the T-cliques of the triple \(\{u_1,u_2,u_3\}\).

Let \(t\in T_i\) for some \(i\in \{1,2,3\}\). For \(i\in \{0,1,2\}\) we say that t is i-clique-adjacent if t has a neighbour in zero, one or two cliques of \(\{T_1,T_2,T_3\}\setminus T_i\), respectively. By the definition of an injective colouring and the fact that every \(T_i\) is a clique, a 1-clique-adjacent vertex of \(T_1\cup T_2\cup T_3\) belongs to a colour class of size at most 2, and a 2-clique-adjacent vertex of \(T_1\cup T_2\cup T_3\) belongs to a colour class of size 1. Hence, all the vertices that belong to a colour class of size 3 are 0-clique-adjacent. The partition of V(G) is illustrated in Fig. 1.

Fig. 1.
figure 1

The partition of V(G) from Lemma 2. The squares inside each \(T_i\), \(i \in \{1,2,3\}\), represent the sets of 0-clique-adjacent, 1-clique-adjacent and 2-clique-adjacent vertices in \(T_i\), respectively.

We now use the fact that c is class-3-optimal. Let \(t\in V\setminus \{u_1,u_2,u_3\}\), say \(t\in T_1\), be i-clique-adjacent for \(i=0\) or \(i=1\). Then we may assume without loss of generality that t has no neighbours in \(T_2\). If t belongs to a colour class of size 1, then we can set \(c(u_2):=c(t)\) to obtain an optimal injective colouring with fewer size-3 colour classes, contradicting our choice of c.

We now consider the 0-clique-adjacent vertices again. Recall that these are the only vertices, other than \(u_1\), \(u_2\) and \(u_3\), that may belong to a colour class of size 3. As every \(T_i\) is a clique, every colour class of size 3 (other than \(\{u_1,u_2,u_3\}\)) has exactly one vertex of each \(T_i\). Let \(\{w_1,w_2,w_3\}\) be another colour class of size 3 with \(w_i\in T_i\) for every \(i\in \{1,2,3\}\). Let \(x\in T_1\setminus \{w_1\}\) be another 0-clique-adjacent vertex. Then swapping the colours of \(w_1\) and x yields another class-3-optimal injective colouring of G. Hence, we derived the following claim, which summarizes the discussion above and where statement (iv) follows from (i)–(iii).

Claim

Let c be a class-3-optimal injective colouring of G with \(c(u_1)=c(u_2)=c(u_3)\) for three distinct vertices \(u_1,u_2,u_3\) and with \(p\ge 0\) other colour classes of size 3. Then the following four statements hold:

  1. (i)

    All 0-clique-adjacent and 1-clique-adjacent vertices belong to a colour class of size at least 2.

  2. (ii)

    Let \(S=\{y_1,\ldots ,y_s\}\) be the set of 2-clique-adjacent vertices. Then \(\{y_1\},\ldots , \{y_s\}\) are exactly the size-1 colour classes.

  3. (iii)

    For \(i\in \{1,2,3\}\), let \(x_1^i,\ldots ,x_{q_i}^i\) be the 0-clique-adjacent vertices of \(T_i\) and assume without loss of generality that \(q_1\le q_2\le q_3\). Then \(p\le q_1\) and if \(p\ge 1\), we may assume without loss of generality that the size-3 classes, other than \(\{u_1,u_2,u_3\}\), are \(\{x_1^1,x_1^2,x_1^3\},\ldots , \{x_p^1,x_p^2,x_p^3\}\).

  4. (iv)

    The number of colours used by c, or equivalently, the number of colour classes of c is equal to \(1+s+p+\frac{1}{2}(n-s-3(p+1))=\frac{1}{2}n+\frac{1}{2}s-\frac{1}{2}p-\frac{1}{2}\).

We are now ready to present our algorithm. We first find, in polynomial time, an optimal 2-injective colouring of G by Lemma 1. We remember the number of colours used. Recall that the colour classes of every injective colouring of G have size at most 3. So, it remains to compute an optimal injective colouring for which at least one colour class has size 3.

We consider each triple \(u_1,u_2,u_3\) of vertices of G and check if \(\{u_1,u_2,u_3\}\) can be a colour class. That is, we check if \(\{u_1,u_2,u_3\}\) is an independent set and has corresponding T-cliques \(T_1\), \(T_2\), \(T_3\). This takes polynomial time. If not, then we discard \(\{u_1,u_2,u_3\}\). Otherwise we continue as follows. Let \(S=\{y_1,\ldots ,y_s\}\) be the set of 2-clique adjacent vertices in \(T_1\cup T_2\cup T_3\). Exactly the vertices of S will form the size-1 colour classes by Claim (ii). For \(i\in \{1,2,3\}\), let \(x_1^i,\ldots ,x_{q_i}^i\) be the 0-clique-adjacent vertices of \(T_i\), where we assume without loss of generality that \(q_1\le q_2\le q_3\). By Claim (iii), any injective colouring of G which has \(\{u_1,u_2,u_3\}\) as one of its colour classes has at most \(q_1\) other colour classes of size 3 besides \(\{u_1,u_2,u_3\}\). As can be seen from Claim (iv), the value \(\frac{1}{2}n+\frac{1}{2}s-\frac{1}{2}p-\frac{1}{2}\) is minimized if the number p of size-3 colour classes is maximum.

From the above we can now do as follows. For \(p=q_1,\ldots ,1\), we check if G has an injective colouring with exactly p colour classes of size 3. We stop as soon as we find a yes-answer or if p is set to 0. We first set \(\{x_1^1,x_1^2,x_1^3\},\ldots , \{x_p^1,x_p^2,x_p^3\}\) as the colour classes of size 3 by Claim (iii). Let Z be the set of remaining 0-clique-adjacent and 1-clique-adjacent vertices. We use Lemma 1 to check in polynomial time if the subgraph of G induced by \(S\cup Z\) has an injective colouring that uses \(s+\frac{1}{2}(n-s-3(p+1))\) colours (which is the minimum number of colours possible). If so, then we stop and note that after adding the size-3 colour classes we obtained an injective colouring of G that uses \(\frac{1}{2}n+\frac{1}{2}s-\frac{1}{2}p-\frac{1}{2}\) colours, which we remember. Otherwise we repeat this step after first setting \(p:=p-1\).

As the above procedure for a triple \(u_1,u_2,u_3\) takes polynomial time and the number of triples we must check is \(O(n^3)\), our algorithm runs in polynomial time. We take the 3-injective colouring that uses the smallest number of colours and compare it with the number of colours used by the optimal 2-injective colouring that we computed at the start. Our algorithm then returns a colouring with the smallest of these two values as its output.    \(\square \)

We now show two hardness results.

Lemma 3

Colouring is NP-complete for graphs with \(\overline{\chi }\le 3\).

Proof

The List Colouring problem takes as input a graph G and a list assignment L that assigns each vertex \(u\in V(G)\) a list \(L(u)\subseteq \{1,2,\ldots \}\). The question is whether G admits a colouring c with \(c(u)\in L(u)\) for every \(u\in V(G)\). Jansen [9] proved that List Colouring is NP-complete for unions of two complete graphs. This is the problem we reduce from.

Let G be a graph with a list assignment L and assume that V(G) can be split into two (not necessarily disjoint) cliques K and \(K'\). We set \(A_1:=K\) and \(A_2:=K\setminus K'\). As both \(A_1\) and \(A_2\) are cliques, we have that \(\overline{\chi }(G)\le 2\). We may assume without loss of generality that the union of all the lists L(u) is \(\{1,\ldots ,k\}\) for some integer k. We now extend G by adding a clique \(A_3\) of k new vertices \(v_1,\ldots ,v_k\) and by adding an edge between a vertex \(x_\ell \) and a vertex \(u\in V(G)\) if and only if \(\ell \notin L(u)\). This yields a new graph \(G'\) with \(\overline{\chi }(G')\le 3\). It is readily seen that G has a colouring c with \(c(u)\in L(u)\) for every \(u\in V(G)\) if and only if \(G'\) has a k-colouring.    \(\square \)

We use Lemma 3 to prove the next lemma, which uses the same arguments as the proof of our NP-hardness result for \(6P_1\)-free graphs [2]. The only difference is that we now reduce from Colouring for graphs with \(\overline{\chi }\le 3\) instead of Colouring for graphs with \(\overline{\chi }\le 4\) as we did in [2].

Lemma 4

Injective Colouring is NP-complete for graphs with \(\overline{\chi }\le 4\), and thus for \(5P_1\)-free graphs, or equivalently, graphs with \(\alpha \le 4\).

We are now ready to prove three theorems.

Proofs of Theorems 35 and 6. Theorem 3 follows from combining Theorem 1, after observing that graphs with \(\overline{\chi }\le 2\) are \(3P_1\)-free and thus \((P_1+P_3)\)-free, with Lemma 3, whereas Theorems 5 and 6 follow from combining Lemmas 2 and 4.

3 The Proof of Theorem 7

In this section we prove our final theorem. We start by showing three new polynomial results. We shall use Lemma 2, on \(4P_1\)-free graphs, in the proofs of Lemmas 6 and 7.

Lemma 5

Injective Colouring is polynomial-time solvable for \((P_1+P_4\))-free graphs.

Proof

Let G be a \((P_1+P_4)\)-free graph. Since connected \(P_4\)-free graphs have diameter at most 2, no two vertices can be coloured alike in an injective colouring. Hence, the injective chromatic number of a \(P_4\)-free graph is equal to the number of its vertices. Consequently, Injective Colouring is polynomial-time solvable for \(P_4\)-free graphs. From now on, we assume that G is not \(P_4\)-free.

We first show that any colour class in any injective colouring of G has size at most 2. For contradiction, assume that c is an injective colouring of G such that there exists some colour, say colour 1, that has a colour class of size at least 3. Let \(P=x_1x_2x_3x_4\) be some induced \(P_4\) of G.

We first consider the case where colour 1 appears at least twice on P. As no vertex has two neighbours coloured with the same colour, the only way in which this can happen is when \(c(x_1)=c(x_4)=1\). By our assumption, \(G-P\) contains a vertex u with \(c(u)=1\). As G is \((P_1+P_4)\)-free, u has a neighbour on P. As every colour class is an independent set, this means that u must be adjacent to at least one of \(x_2\) and \(x_3\). Consequently, either \(x_2\) or \(x_3\) has two neighbours with colour 1, a contradiction.

Now we consider the case where colour 1 appears exactly once on P, say \(c(x_h)=1\) for some \(h\in \{1,2,3,4\}\). Then, by our assumption, \(G-P\) contains two vertices \(u_1\) and \(u_2\) with colour 1. As G is \((P_1+P_4)\)-free, both \(u_1\) and \(u_2\) must be adjacent to at least one vertex of P, say \(u_1\) is adjacent to \(x_i\) and \(u_2\) is adjacent to \(x_j\). Then \(x_i\ne x_j\), as otherwise G has a vertex with two neighbours coloured 1. As every colour class is an independent set, we have that \(x_h\notin \{x_i,x_j\}\), and hence, \(x_h\), \(x_i\), \(x_j\) are distinct vertices. Moreover, \(x_h\) is not a neighbour of \(x_i\) or \(x_j\), as otherwise \(x_i\) or \(x_j\) has two neighbours coloured 1. Hence, we may assume without loss of generality that \(h=1\), \(i=3\) and \(j=4\). As every colour class is an independent set, \(u_1\) and \(u_2\) are non-adjacent. However, now \(\{x_1,u_1,x_3,x_4,u_2\}\) induces a \(P_1+P_4\), a contradiction.

Finally, we consider the case where colour 1 does not appear on P. Let \(u_1\), \(u_2\), \(u_3\) be three vertices of \(G-P\) coloured 1. As before, \(\{u_1,u_2,u_3\}\) is an independent set and each \(u_i\) has a different neighbour on P. We first consider the case where \(x_1\) or \(x_4\), say \(x_4\) is not adjacent to any \(u_i\). Then we may assume without loss of generality that \(u_1x_1\) and \(u_2x_2\) are edges. However, now \(\{x_4,u_1,x_1,x_2,u_2\}\) induces a \(P_1+P_4\), which is not possible. Hence, we may assume without loss of generality that \(u_1x_1\), \(u_2x_2\) and \(u_4x_4\) are edges of G. Again we find that \(\{x_4,u_1,x_1,x_2,u_2\}\) induces a \(P_1+P_4\), a contradiction.

From the above, we find that each colour class in an injective colouring of G has size at most 2. This means we can use Lemma 1.    \(\square \)

Lemma 6

Injective Colouring is polynomial-time solvable for \((2P_1+P_3\))-free graphs.

Proof

Let \(G=(V,E)\) be a \((2P_1+P_3)\)-free graph. We may assume without loss of generality that G is connected and by Lemma 2 that G has an induced \(4P_1\). We first show that any colour class in any injective colouring of G has size at most 2. For contradiction, assume that c is an injective colouring of G such that there exists some colour, say colour 1, that has a colour class of size at least 3. Let \(U=\{u_1,\ldots ,u_p\}\) for some \(p\ge 3\) be the set of vertices of G with \(c(u_i)=1\) for \(i\in \{1,\ldots ,p\}\).

As c is injective, every vertex in \(G-U\) has at most one neighbour in U. Hence, we can partition \(G-U\) into (possibly empty) sets \(T_0,\ldots ,T_p\), where \(T_0\) is the set of vertices with no neighbour in U and for \(i\in \{1,\ldots ,p\}\), \(T_i\) is the set of vertices of \(G-U\) adjacent to \(u_i\).

We first claim that \(T_0\) is empty. For contradiction, assume \(v\in T_0\). As G is connected, we may assume without loss of generality that v is adjacent to some vertex \(t\in T_1\). Then \(\{u_2,u_3,u_1,t,v\}\) induces a \(2P_1+P_3\), a contradiction. Hence, \(T_0=\emptyset \).

We now prove that every \(T_i\) is a clique. For contradiction, assume that t and \(t'\) are non-adjacent vertices of \(T_1\). Then \(\{u_2,u_3.t,u_1,t'\}\) induces a \(2P_1+P_3\), a contradiction. Hence, every \(T_i\) and thus every \(T_i\cup \{u_i\}\) is a clique.

We now claim that \(p=3\). For contradiction, assume that \(p\ge 4\). As G is connected and U is an independent set, we may assume without of generality that there exist vertices \(t_1\in T_1\) and \(t_2\in T_2\) with \(t_1t_2\in E\). Then \(\{u_3,u_4,u_1,t_1,t_2\}\) induces a \(2P_1+P_3\), a contradiction. Hence, \(p=3\).

Now we know that V can be partitioned into three cliques \(T_1\cup \{u_1\}\), \(T_2\cup \{u_2\}\) and \(T_3\cup \{u_3\}\). However, then G is \(4P_1\)-free, a contradiction. We conclude that every colour class of every injective colouring of G has size at most 2. This means we can use Lemma 1.    \(\square \)

Lemma 7

Injective Colouring is polynomial-time solvable for \((3P_1+P_2\))-free graphs.

Proof

Let G be a \((3P_1+P_2)\)-free graph on n vertices. We may assume without loss of generality that G is connected and by Lemma 2 that G has an induced \(4P_1\). As before, we will first analyze the structure of injective colourings of G. We will then exploit the properties found algorithmically.

Let c be an injective colouring of G that has a colour class U of size at least 3. So let \(U=\{u_1,\ldots ,u_p\}\) for some \(p\ge 3\) be the set of vertices of G with, say colour 1. As c is injective, every vertex in \(G-U\) has at most one neighbour in U. Hence, we can partition \(G-U\) into (possibly empty) sets \(T_0,\ldots ,T_p\), where \(T_0\) is the set of vertices with no neighbour in U and for \(i\in \{1,\ldots ,p\}\), \(T_i\) is the set of vertices of \(G-U\) adjacent to \(u_i\).

Assume that \(p\ge 4\). As G is connected, there exists a vertex \(v\notin U\) but that has a neighbour in U, say \(v\in T_1\). Then \(\{u_2,u_3,u_4,u_1,v\}\) induces a \(3P_1+P_2\), a contradiction. Hence, we have shown the following claim.

Claim 1

Every injective colouring of G is \(\ell \)-injective for some \(\ell \in \{1,2,3\}\).

We continue as follows. As \(p=3\) by Claim 1, we have \(V(G)=U\cup T_0\cup T_1\cup T_2 \cup T_3\). Suppose \(T_0\) contains two adjacent vertices x and y. Then \(\{u_1,u_2,u_3,x,y\}\) induces a \(3P_1+P_2\), a contradiction. Hence, \(T_0\) is an independent set. As G is connected, this means each vertex in \(T_0\) has a neighbour in \(T_1\cup T_2\cup T_3\).

Suppose \(T_0\) contains two vertices x and y with the same colour, say \(c(x)=c(y)=2\). Let \(v\in T_1\cup T_2\cup T_3\), say \(v\in T_1\) be a neighbour of x. Then, as \(c(x)=c(y)\) and c is injective, v is not adjacent to y. As \(T_0\) is independent, x and y are not adjacent. However, now \(\{u_2,u_3,y,x,v\}\) induces a \(3P_1+P_2\), a contradiction. Hence, every vertex in \(T_0\) has a unique colour. Suppose \(T_0\) contains a vertex x and \(T_1\cup T_2\cup T_3\) contains a vertex v such that \(c(x)=c(v)\). We may assume without loss of generality that \(v\in T_1\). Then \(\{u_2,u_3,x,v,u_1\}\) induces a \(3P_1+P_2\), a contradiction.

Finally, suppose that \(T_1\cup T_2\cup T_3\) contain two distinct vertices v and \(v'\) with \(c(v)=c(v')\). Let \(x\in T_0\). Then x is not adjacent to at least one of v, \(v'\), say \(xv\notin E\) and also assume that \(v\in T_1\). Then \(\{u_2,u_3,x,v,u_1\}\) induces a \(3P_1+P_2\). Hence, we have shown the following claim.

Claim 2

If c is 3-injective and U is a size-3 colour class such that G has a vertex not adjacent to any vertex of U, then all colour classes not equal to U have size 1.

We note that the injective colouring c in Claim 2 uses \(n-2\) distinct colours.

We continue as follows. From now on we assume that \(T_0=\emptyset \). Every \(T_i\) is \((P_1+P_2)\)-free, as otherwise, if say \(T_1\) contains an induced \(P_1+P_2\), then this \(P_1+P_2\), together with \(u_2\) and \(u_3\), forms an induced \(3P_1+P_2\), which is not possible. Hence, each \(T_i\) induces a complete \(r_i\)-partite graph for some integer \(r_i\) (that is, the complement of a disjoint union of \(r_i\) complete graphs). Hence, we can partition each \(T_i\) into \(r_i\) independent sets \(T_i^1,\dots ,T_i^{r_i}\) such that there exists an edge between every vertex in \(T_i^a\) and every vertex in \(T_i^b\) if \(a\ne b\). See also Fig. 2.

Suppose G contains another colour class of size 3, say \(v_1\), \(v_2\) and \(v_3\) are three distinct vertices coloured 2. If two of these vertices, say \(v_1\) and \(v_2\), belong to the same \(T_i\), say \(T_1\), then \(u_1\) has two neighbours with the same colour. This is not possible, as c is injective. Hence, we may assume without loss of generality that \(v_i\in T_i^1\) for \(i\in \{1,2,3\}\).

Suppose that \(T_1^2\) contains two vertices s and t. Then, as s and t are adjacent to \(v_1\), both of them are not adjacent to \(v_2\) (recall that \(c(v_1)=c(v_2)\) and c is injective). Hence, \(\{s,t,u_3,v_2,u_2\}\) induces a \(3P_1+P_2\) (see Fig. 2). We conclude that for every \(i\in \{1,2,3\}\), the sets \(T_i^2,\ldots ,T_i^{r_i}\) have size 1.

Fig. 2.
figure 2

The situation in Lemma 7 where \(T_1^2\) contains two vertices s and t. We show that this situation cannot happen, as it would lead to a forbidden induced \(3P_1+P_2\). Note that each \(u_i\) is adjacent to all vertices of \(T_i\) and not to any vertices of \(T_j\) for \(j\ne i\). There may exist edges between vertices of different sets, but these are not drawn.

We will now make use of the fact that G contains an induced \(4P_1\). We note that each \(T_i\cup \{u_i\}\) is a clique, unless \(|T_i^1|\ge 2\). As \(V(G)=T_1\cup T_2\cup T_3\cup \{u_1,u_2,u_3\}\) and G contains an induced \(4P_1\), we may assume without loss of generality that \(T_1^1\) has size at least 2. Recall that \(v_1\in T_1^1\). Let \(z\ne v_1\) be some further vertex of \(T_1^1\). If z is not adjacent to \(v_2\), then \(\{z,v_1,u_3,v_2,u_2\}\) induces a \(3P_1+P_2\), which is not possible. Hence, z is adjacent to \(v_2\). For the same reason, z is adjacent to \(v_3\). This is not possible, as c is injective and \(v_2\) and \(v_3\) both have colour 2. Hence, we have proven the following claim.

Claim 3

If c is 3-injective and U is a size-3 colour class such that each vertex of \(G-U\) is adjacent to a vertex of U, then c has no other colour class of size 3.

We are now ready to present our polynomial-time algorithm. We first use Lemma 1 to find in polynomial time an optimal 2-injective colouring of G. We remember the number of colours it uses.

By Claim 1, it remains to find an optimal 3-injective colouring with at least one colour class of size 3. We now consider each set \(\{u_1,u_2,u_3\}\) of three vertices. We discard our choice if \(u_1,u_2,u_3\) do not form an independent set or if \(V(G)\setminus \{u_1,u_2,u_3\}\) cannot be partitioned into sets \(T_0,\ldots , T_4\) as described above. Suppose we have not discarded our choice of vertices \(u_1\), \(u_2\), \(u_3\). We continue as follows.

If \(T_0\ne \emptyset \), then by Claim 2 the only 3-injective colouring of G (subject to colour permutation) with colour class \(\{u_1,u_2,u_3\}\) is the colouring that gives each \(u_i\) the same colour and a unique colour to all the other vertices of G. This colouring uses \(n-2\) colours and we remember this number of colours.

Now suppose \(T_0=\emptyset \). By Claim 3, we find that \(\{u_1,u_2,u_3\}\) is the only colour class of size 3. Recall that no vertex in \(G-\{u_1,u_2,u_3\}=T_1\cup T_2\cup T_3\) is adjacent to more than one vertex of \(\{u_1,u_2,u_3\}\). Hence, we can apply Lemma 1 on \(G-\{u_1,u_2,u_3\}\). This yields an optimal 2-injective colouring of \(G-\{u_1,u_2,u_3\}\). We colour \(u_1\), \(u_2\), \(u_3\) with the same colour and choose a colour that is not used in the colouring of \(G-\{u_1,u_2,u_3\}\). This yields a 3-injective colouring of G that is optimal over all 3-injective colourings with colour class \(\{u_1,u_2,u_3\}\). We remember the number of colours.

As the above procedure takes polynomial time and there are \(O(n^3)\) triples to consider, we find in polynomial time an optimal 3-injective colouring of G that has at least one colour class of size 3 (should it exist). We compare the number of colours used with the number of colours of the optimal 2-injective colouring of G that we found earlier. Our algorithm returns the minimum of the two values as the output. Since both colourings are found in polynomial time, we conclude that our algorithm runs in polynomial time.   \(\square \)

We are now ready to prove Theorem 7.

Theorem 7

(restated). Let \(H\ne 2P_1+P_4\) be a graph. Then Injective Colouring for H-free graphs is polynomial-time solvable if \(H\subseteq _iP_1+P_4\) or \(H\subseteq _i2P_1+P_3\) or \(H\subseteq _i3P_1+P_2\) and NP-complete otherwise.

Proof

The problem is readily seen to be in NP. Let \(C_n\) denote the n-vertex cycle. In [2], we proved that Injective 4-Colouring is NP-complete for \(C_3\)-free graphs (in fact, the proof of [2] shows NP-completeness for bipartite graphs). Hence, if \(C_3\subseteq _iH\), then Injective Colouring is NP-complete for H-free graphs. If \(C_p\subseteq _iH\) for some \(p\ge 4\) or \(K_{1,3}\subseteq _iH\), then we use a result of Mahdian [14], namely that for every \(g\ge 4\) and \(k\ge 4\), Injective k -Colouring is NP-complete for line graphs of bipartite graphs of girth at least g: by setting \(g=p+1\) we obtain a class of \((C_p,K_{1,3})\)-free graphs.

In the remaining case H is a linear forest. If \(5P_1\subseteq _iH\), then we use Lemma 4. If \(2P_2\subseteq _iH\), then we obtain a superclass of \((2P_2,C_4,C_5)\)-free graphs (split graphs) for which Bodlaender et al. [1] proved NP-completeness. In all other cases, \(H\subseteq _iP_1+P_4\), \(H\subseteq _i2P_1+P_3\) or \(H\subseteq _i3P_1+P_2\), and we use Lemma 56 or 7, respectively.    \(\square \)

4 Conclusions

We extended the partial classification of Injective Colouring for H-free graphs in [2] to a new classification that leaves open only the case where \(H=2P_1+P_4\). Note that each of the graphs \(P_1+P_4\), \(2P_1+P_3\) and \(3P_1+P_2\) is an induced subgraph of \(2P_1+P_4\). Our strategy for solving the cases where \(H=3P_1+P_2\) or \(H=2P_1+P_3\) is based on the presence of an induced \(4P_1\); as otherwise we can use our algorithm for \(4P_1\)-free graphs from Lemma 2. We then argue in each of the two cases that the number of size-3 colour classes is small. However, such an approach no longer works for the unknown case where \(H=2P_1+P_4\), as there exist connected \((2P_1+P_4)\)-free graphs with an induced \(4P_1\) that have an arbitrarily large number of size-3 colour classes. On the other hand, the algorithms for the cases where \(H\in \{P_1+P_4,2P_1+P_3,3P_1+P_2\}\) might be useful for a polynomial-time algorithm for the case \(H=2P_1+P_4\) (should such an algorithm exist). However, we first need to understand the structure of these graphs better and leave the case where \(H=2P_1+P_4\) for future work.

Open Problem 1

Determine the complexity of Injective Colouring for \((2P_1+P_4)\)-free graphs.

We recall that Injective 3-Colouring is polynomial-time solvable for general graphs and that in [2] we proved that, for every \(k\ge 4\), Injective k -Colouring is NP-complete for bipartite graphs and thus for graphs of girth at least 4. Mahdian [14] proved that for every \(g\ge 4\) and \(k\ge 4\), Injective Edge k -Colouring is NP-complete for bipartite graphs of girth at least g. For the vertex variant, such a result does not seem to be known (except when \(g=4\)). Hence, we pose the following challenging but highly interesting open problem (see also [2]).

Open Problem 2

For every \(g\ge 5\), determine the complexity of Injective Colouring and Injective k -Colouring (\(k\ge 4\)) for graphs of girth at least g.