1 Introduction

The notion of well quasi-order (wqo) is a well-established tool in mathematics and in many areas of theoretical computer science that was rediscovered by many authors (see [10] by Kruskal). A comprehensive overview of the applications of the notion in the theory of formal languages and combinatorics on words can be found in the book [4] by de Luca and Varricchio or in the survey paper [3] by D’Alessandro and Varricchio.

A quasi-order on a set X is a wqo if it has no infinite antichains and no decreasing infinite chains (the latter property is often called well-foundedness). The important property which makes the notion of wqo a useful tool in formal language theory is that every subset of \(A^*\) upper closed with respect to a monotone wqo is a regular language (see [5] by Ehrenfeucht, Haussler, and Rozenberg).

The first example of a wqo in the area of formal languages was given by Higman [7]. We recall the simplest instance of the general statement: the embedding relation \(\unlhd \) on \(A^*\) is a wqo. The embedding relation \(\unlhd \) is often called subword ordering because a word u embeds in a word v if u is a scattered subword of v, i.e., \(u\unlhd v\) if there are factorizations of the same length \(u=a_1\dots a_k\) and \(v=v_1\dots v_k\) such that, for all \(i\in \{1,\dots , k\}\), we have \(a_i\in A\), \(v_i\in A^+\), and \(a_i\) appears in \(v_i\).

The considered notion of embedding relation can be modified by requiring different conditions on the factorizations. For example, if the alphabet A is quasi-ordered by \(\preceq \), then we may replace the condition that \(a_i\) appears in \(v_i\) by the condition that there is a letter \(b_i\in A\) such that \(a_i\preceq b_i\) and \(b_i\) appears in \(v_i\). In this way, we obtain the quasi-order \(\unlhd \) considered by Higman when he extended his result to infinite alphabets: the resulting relation \(\unlhd \) is a wqo if and only if \(\preceq \) is a wqo. Another variant is the following gap embedding considered by Schütte and Simpson in [17] for an alphabet equipped with a linear order \(\sqsubseteq \): the defining condition that \(a_i\) appears in \(v_i\) is replaced by the condition that the letter \(a_i\) is the last letter in \(v_i\) and it is the least letter in \(v_i\) with respect to \(\sqsubseteq \).

Our paper concentrates on a study of wqos motivated by the work of Bucher, Ehrenfeucht, and Haussler [2], which leads to a purely algebraic question in the realm of ordered semigroups. Notice that the original paper [2] does not use terminology of ordered semigroups which was fixed in the algebraic theory of formal languages later, see, e.g., [14] by Pin, a fundamental survey in that theory. The reformulation of the question in [2] using the notion of ordered semigroups is also mentioned in the recent survey paper [15] by Pin (see Paragraph 3.5). Recall that by an ordered semigroup \((S,\cdot , \le )\) we mean a semigroup \((S,\cdot )\) equipped with a monotone partial order \(\le \), i.e., with the antisymmetric quasi-order \(\le \) compatible with the associative multiplication \(\cdot \) on S. In this setting, any semigroup may be also viewed as an ordered semigroup which is ordered by the equality relation.

The starting point in the study of well quasi-orders in [2] was a research in [5] concerning specific rewriting systems preserving regularity, where a well quasi-order plays a role of a sufficient condition guaranteeing the required property of the rewriting system. In that research, particular attention is paid to finite rewriting systems R with rules of the form \(a\rightarrow u\), with a being a letter and u being a word over the same alphabet. For such a rewriting system, several conditions equivalent to the fact that the derivation relation \(\overset{*}{\Rightarrow }_{R}\) is a well quasi-order are stated in [2]. For example, one of the equivalent conditions is that the set \(L=\{aua \,\vert \,a\in A, u\in A^*, a \overset{*}{\Rightarrow }_{R} aua \}\) is unavoidable over A meaning that every infinite word over the alphabet A contains a finite factor in the language L. They also showed that every derivation relation that is a wqo is defined by a rewriting system induced by a semigroup homomorphism \(\sigma : A^+ \rightarrow S\) onto a finite ordered semigroup \((S,\cdot ,\le )\) in the sense that \(\overset{*}{\Rightarrow }_{R}\) equals \({\overset{*}{{\Rightarrow }_{R_{\sigma }}}}\), where \(R_\sigma = \{ a\rightarrow u \,\vert \,a\in A, u\in A^+, \sigma (a)\le \sigma (u)\}\). The open question is to characterize homomorphisms \(\sigma \) such that \({\overset{*}{{\Rightarrow }_{R_\sigma }}}\) is a wqo. Another research goal is a characterization of finite ordered semigroups S such that the relation \({\overset{*}{{\Rightarrow }_R{_\sigma }}}\) is a wqo for every alphabet A and every homomorphism \(\sigma : A^+ \rightarrow S\). For the purpose of this paper, we call these ordered semigroups congenial. Notice that all examples of embeddings mentioned above are of the form \({\overset{*}{{\Rightarrow }_R{_\sigma }}}\) for an appropriate semigroup homomorphisms onto a finite ordered semigroup (see Examples 2.2 and 2.3).

Up to our knowledge, and also according to the survey paper [15], there is just one significant contribution to the mentioned open questions. Namely, in the paper [11] by Kunc, the questions are solved for the semigroups ordered by the equality relation. It is stated in [11] (implicitly contained in the proof of [11, Theorem 10]) that the property depends only on the semigroup S, not on the actual homomorphism \(\sigma \). In that paper, the congenial semigroups ordered by the equality relation are then characterized as finite chains of finite simple semigroups.

We show that the problem of whether a homomorphism generates a well quasi-order is decidable in polynomial time. The proof is an application of the above mentioned characterization (in [2]). We show that the side result of [11] holds in the full generality: the property is indeed a property of an ordered semigroup and does not depend on the homomorphism. Unfortunately, our characterization is not as transparent as Kunc’s result, however, we explain that the borderline between congenial and non-congenial semigroups is so delicate, that one cannot expect a structural characterization of congenial ordered semigroups.

The paper is a full version of our conference contribution [9]. However, it is not an extended version in the usual sense, since we remove all tentative observations that are unnecessary to obtain the main results. On the other hand, the necessary ones are elaborated in more detail (e.g., Sections 3.2 and 4.2). Section 4 contains some new statements, including Theorem 4.16 stating the polynomial complexity of congeniality testing.

2 Well Quasi-Orders Generated by Finite Ordered Semigroups

The aim of this preliminary section is to formalize the studied construction of a quasi-order generated by a finite ordered semigroup, and give basic observations. We start by recalling necessary notions of the semigroup theory and order theory.

2.1 Semigroups, Finite Semigroups and Order

By a quasi-order on a set X we mean a reflexive and transitive binary relation \(\le \) on the set X. If a quasi-order is also antisymmetric, it is called a partial order. If any two elements are comparable in a partial order, we talk about linear order. Every quasi-order \(\le \) on a set X defines a partial order on \(X/\!\!\cong \), where the relation \(\cong \) is an equivalence defined as \(\le \cap \, (\le )^{-1}\). A subset Y of X is said to be upper closed when, for all \(y\in Y\) and \(x\in X\), if \(y\le x\) then \(x\in Y\).

As mentioned in Introduction, a quasi-order \(\le \) on a set X is a well quasi-order (wqo) if it has no infinite antichains and no decreasing infinite chains. Alternatively, the quasi-order \(\le \) is a wqo if for an infinite sequence \((x_n)_{n\in \mathbb {N}}\) of elements of X there exist indices \(m,n\in \mathbb {N}\) such that \(m<n\) and \(x_m\le x_n\). Other equivalent conditions are widely used (see, e.g., [4, Theorem 6.1.1]).

As stated above, in an ordered semigroup \((S,\cdot ,\le )\), the partial order \(\le \) is monotone (or stable), i.e., compatible with the multiplication \(\cdot \) in the sense that, for arbitrary \(x,y,s\in S\), the inequality \(x\le y\) implies both \(s\cdot x\le s\cdot y\) and \(x\cdot s \le y\cdot s\). In what follows, when we talk about (ordered) semigroup, we write simply S instead of formal notation \((S,\cdot )\), resp. \((S,\cdot ,\le )\). Notice that in the theory of ordered semigroups, the homomorphisms respect the multiplication as well as the partial order, i.e., they are isotone.

Throughout the paper we work with finite semigroups with the exception of the free monoid \(A^*\) (the free semigroup \(A^+\)) formed by (non-empty) words over an alphabet A. We use the symbol \(\varepsilon \) for the empty word. Recall, that the freeness means that whenever we have a mapping \(\alpha :A \rightarrow S\) from the alphabet A to a semigroup S, then there is a unique homomorphism extension \(\alpha '\) from \(A^+\) to S, taking \(\alpha '(a_1 \dots a_n)=\alpha (a_1)\cdot \alpha (a_2) \cdots \alpha (a_n)\), where \(a_i\)’s are letters. If S is an ordered semigroup, then such mapping is homomorphism of ordered semigroups as we consider \(A^+\) ordered by equality. In such a case, we simplify the terminology and talk about a homomorphism from \(A^+\) to an ordered semigroup S.

Suppose now that the alphabet A is linearly ordered. By the shortlex order on \(A^*\) (denoted by \(\le _{slex}\)) we mean that the words are sorted by length first and then lexicographically.

For a finite semigroup S, by \(S^1\) we mean the monoid \(S\cup \{1\}\) with a new neutral element 1 added if S is not a monoid and \(S^1=S\) if S is a monoid. As for the alphabet A, we also denote \(S^+\) and \(S^*\) the free semigroup, respectively monoid, over the alphabet S. We denote \({\text {eval}}_S:S^+\rightarrow S\) the evaluation homomorphism from the free semigroup over S defined by the rule \({\text {eval}}_S(s)=s\) for all \(s\in S\). Here, an element \(w\in S^+\) is a word \(w=s_1s_2\dots s_k\), where \(s_i\in S\) for \(i\in \{1,\dots ,k\}\), and for such w we have \({\text {eval}}_S(w)={\text {eval}}_S(s_1s_2\dots s_k)=s_1\cdot s_2\cdots s_k\).

An element e in a semigroup S is called idempotent if \(e\cdot e=e\). If S is a finite semigroup, for every \(s\in S\), the set \(\{ s^n \,\vert \,n \in \mathbb {N}\}\) contains exactly one idempotent, which is denoted \(s^{\omega }\). We put \(s^{\omega +1}=s^\omega \cdot s\) which equals (by definition) to \(s\cdot s^\omega \). Another useful property is that \(s\cdot (t\cdot s)^{\omega }= (s\cdot t)^{\omega }\cdot s\), for every \(s,t\in S\). Recall that a finite semigroup S is completely regular if it is a union of groups. An equivalent expression is that every element \(s\in S\) satisfies \(s^{\omega +1}=s\).

2.2 Quasi-order on \(A^*\) Generated by a Homomorphism

The next definition is implicitly contained in [2]. Our formalism and notation is adopted from survey chapter [12] by Kunc and Okhotin, and it is a formalism originally used in [11] in the case of semigroups ordered by equality.

Definition 2.1

Let \(\sigma :A^+\rightarrow S\) be a homomorphism onto a finite ordered semigroup. We denote \(\le _{\sigma }\) a quasi-order on \(A^*\) defined by setting \(u \le _{\sigma }v\) if there exist factorizations \(u=a_1\dots a_n\) and \(v=v_1\dots v_n\), such that for all \(i\in \{1,\dots , n\}\) we have \(a_i\in A, v_i\in A^+\) and \(\sigma (a_i)\le \sigma (v_i)\).

Note that \(u\le _{\sigma }v\) implies \(\sigma (u)\le \sigma (v)\) and either \(u=v=\varepsilon \) or \(u,v\in A^+\). It is also clear that the relation \(\le _{\sigma }\) is a stable quasi-order on \(A^*\).

We show that the examples of wqos mentioned in Introduction fit to the introduced pattern of relations generated by a homomorphism onto a finite ordered semigroup.

Example 2.2

For an alphabet A we consider an ordered semigroup \((P(A),\cup ,\subseteq )\) consisting of non-empty subsets of A equipped with the operation of union, and ordered by the inclusion relation. Then the homomorphism \(\sigma : A^+ \rightarrow P(A)\), defined by the rule \(\sigma (a)=\{a\}\) for \(a\in A\), maps every word u to the set of all letters that occur in u. By Definition 2.1, the relation \(\le _{\sigma }\) coincides with the embedding relation \(\unlhd \).

For a variant of Higman’s Lemma where an infinite alphabet A is equipped with the quasi-order \(\preceq \), we take the same semigroup P(A) however we use the homomorphism \(\tau : A^+ \rightarrow P(A)\) given by \(\tau (a)=\{b\in A \,\vert \,b\preceq a\}\). Then, for a word \(u\in A^+\), we have \(\tau (u)=\{ b\in A \,\vert \,\exists a \in \sigma (u), b\preceq a\}\), which is a downward closed subset with respect to the quasi-order \(\preceq \), that is the subset \(\tau (u)\) has the property that \(b\preceq a \in \tau (u)\) implies \(b\in \tau (u)\). Now, the relation \(\unlhd \) considered by Higman is the quasi-order \(\le _\tau \). Since the alphabet A is not finite, the homomorphism \(\tau \) is not onto. If we want to work with an onto homomorphism, we may restrict the semigroup P(A) to its subsemigroup of all downward closed subsets of A generated by finite set of its maximal elements.

Notice that, for an infinite alphabet, the ordered semigroups considered in Example 2.2 are not finite and therefore they are not of our focus. This example is a unique exception where we consider infinite alphabets. We emphasize that an alphabet is meant to be non-empty and finite in the paper.

The next example deals with the gap embedding of [17].

Example 2.3

Let \(\sqsubseteq \) be a linear order on A. We consider the finite semigroup \(S=A\times A\) (ordered by equality), where the multiplication is defined by the rule \((a,b)\cdot (c,d)=(min(a,c),d)\), where min is taken with respect to \(\sqsubseteq \). Then the homomorphism \(\sigma :A^+\rightarrow S\) is the extension of the diagonal mapping, i.e., \(\sigma (a)=(a,a)\). By Definition 2.1, the relation \(\le _{\sigma }\) coincides with the gap embedding mentioned in Introduction.

We also give two straightforward examples of semigroup homomorphism \(\sigma \) such that \(\le _{\sigma }\) is not a wqo.

Example 2.4

We consider a two-element semigroup \(S=\{s,0\}\), where the product of any two elements equals 0, and the semigroup is ordered by equality. Let A be an arbitrary alphabet, and consider \(\sigma : A^+ \rightarrow S\) given by \(\sigma (a)=s\) for all \(a\in A\). Hence, words of length one are mapped to s, and words of length at least 2 are mapped to 0. Now, we have \(\sigma (a)\le \sigma (u)\) if and only if \(u\in A\). This means that, for a pair of words \(u,v\in A^+\), the inequality \(u \le _{\sigma }v\) holds exactly when the words u and v have the same length. Thus, for every \(m\in \mathbb N\) we have one class \(A^m\) of \(\le _{\sigma }\)-equivalent words, and an element of the class \(A^m\) is incomparable with an element of a class \(A^n\) with respect to \(\le _{\sigma }\) whenever \(n\ne m, n\in \mathbb {N}\). In particular, the relation \(\le _{\sigma }\) is not a wqo.

Example 2.5

Let A be an arbitrary alphabet. We consider the finite ordered semigroup \(S=A\cup \{0\}\), ordered by equality, similar to that in Example 2.4, namely, we put the product of any two elements equals 0. Then the considered homomorphism \(\sigma :A^+\rightarrow S\) is the extension of the identity mapping on A. In this case, for \(a\in A\), the inequality \(\sigma (a)\le \sigma (u)\) holds if and only if \(a=u\). Whenever this happens for the homomorphism \(\sigma \), the relation \(\le _{\sigma }\) is the equality relation on \(A^*\), which is not a wqo.

2.3 Recognizing Languages by Ordered Semigroups

The finite ordered semigroups are used to recognize regular languages similarly to unordered semigroups – see, e.g., the fundamental surveys on the (algebraic) theory of regular languages [14, 16] by Pin. The modification is natural, let us explain it informally: The syntactic semigroup of a regular language is implicitly ordered in the following way. In the syntactic congruence of the regular language, words are related if they have the same set of contexts. Then one may also compare these sets of contexts by the inclusion relation; this comparison gives the syntactic quasi-order and consequently the partial order on the syntactic semigroup of the considered regular language (see [16]). Let us note that in the literature the syntactic quasi-order is not always defined in this way, as sometimes the dual quasi-order is considered instead, e.g., in [14].

For our purpose, we recall formally only the notion of recognizability. We say that a homomorphism \(\sigma :A^+\rightarrow S\) onto a finite ordered semigroup recognizes a language \(L\subseteq A^+\) if there exists an upward closed subset \(F\subseteq S\) such that \(L=\sigma ^{-1}(F)\). Such a language is regular, since it is recognized by the semigroup S in the usual setting.

Now, using this theory, we may add some observations concerning relations \(\le _{\sigma }\). For a given homomorphism \(\sigma :A^+\rightarrow S\), the set of words above a given letter \(a\in A\), denoted \(L^a=\{u\in A^* \,\vert \,a\le _{\sigma }u\}\), is recognizable by the homomorphism \(\sigma \). Then, for a given word \(u=a_1\dots a_n\), the language \(L^u=\{ v\in A^+ \,\vert \,u\le _{\sigma }v\}\) is a product of the languages \(L^{a_i}\) and therefore regular. Next, suppose that \(\le _{\sigma }\) is a wqo. Then an arbitrary upward closed subset of \(A^*\) is a regular language as it is a finite union of languages of the form \(L^u\). On the contrary, in Example 2.4 where the quasi-order \(\le _{\sigma }\) is not a wqo, we see that an arbitrary language is an upward closed subset.

We may also notice that the class of all upward closed languages with respect to a wqo \(\le _{\sigma }\) is closed under taking finite unions, finite intersections and quotients, thus it is a quotienting algebra of languages following the terminology of [6] by Gehrke, Grigorieff, and Pin. We leave this terminology without formal definitions as we do not use it onward.

2.4 Congenial Semigroups

We say that an ordered semigroup S is congenial if for every alphabet A and homomorphism \(\sigma :A^+\rightarrow S\) the corresponding relation \(\le _{\sigma }\) is a well quasi-order.

We finish this section with a basic observation that it is enough to consider the case of the homomorphism \({\text {eval}}_S\) when a congeniality of S is tested. We establish the following auxiliary lemma and subsequent statement, the both observed in unordered case by Kunc. The idea of the proof remains essentially the same (see [11, Theorem 10, (iii)\(\Longrightarrow \)(i)]).

Lemma 2.6

Let \(\sigma :A^+ \rightarrow S\) be a homomorphism to an ordered semigroup S such that \(\le _\sigma \) is a wqo. Let B be an alphabet, \(\alpha : B^+ \rightarrow A^+\) be a homomorphism of free semigroups such that \(\alpha (B)\subseteq A\), and \(\varphi =\sigma \circ \alpha \). Then the quasi-order \(\le _\varphi \) is a wqo.

Proof

Let \((w_i)_{i\in \mathbb N}\) be a sequence of words over B. Then \((\alpha (w_i))_{i\in \mathbb N}\) is a sequence of words over A. Since the relation \(\le _\sigma \) is a wqo, there exist k and \(\ell \) such that \(k<\ell \) and \(\alpha (w_k)\le _\sigma \alpha (w_\ell )\). This means that \(\alpha (w_k)=a_1\dots a_m\) and \(\alpha (w_\ell )=u_1\dots u_m\) such that \(a_i \le _\sigma u_i\), \(a_i\in A\), and \(u_i\in A^+\) for every \(i\in \{1,\dots , m\}\). Since the homomorphism \(\alpha \) maps letters to letters, the considered factorizations correspond to the factorizations \(w_k=b_1 \dots b_m\) and \(w_\ell =v_1 \dots v_m\) such that \(b_i\in B\), \(v_i\in B^+\), \(\alpha (b_i)=a_i\), \(\alpha (v_i)=u_i\). Now, the inequality \(a_i \le _\sigma u_i\) means \(\sigma (a_i)\le \sigma (u_i)\), which can be written as \(\varphi (b_i)\le \varphi (v_i)\), that is \(b_i \le _\varphi v_i\). Composing these inequalities for \(i\in \{1,\dots , m\}\), we deduce \(w_k\le _\varphi w_\ell \). \(\square \)

Proposition 2.7

An ordered semigroup S is congenial if and only if \(\le _{{\text {eval}}_S}\) is a wqo.

Proof

The implication from left to right is trivial. For the other implication, let \(\varphi : B^+ \rightarrow S\) be a homomorphism to a finite ordered semigroup S such that \(\le _{{\text {eval}}_S}\) is a wqo. Now, we consider the mapping \(\varphi ':B\rightarrow S^+\) to the semigroup \(S^+\), which has the same images as the mapping \(\varphi \). Since \(B^+\) is a free semigroup, we may consider the semigroup homomorphism \(\alpha :B^+\rightarrow S^+\) that is the extension of the mapping \(\varphi '\). Then we may write \(\varphi ={\text {eval}}_S\circ \alpha \), where \(\alpha (B)=\varphi '(B)=\varphi (B)\subseteq S\). By Lemma 2.6, we deduce that \(\le _\varphi \) is a wqo. \(\square \)

3 Unavoidable Languages for Derivation Relations

As we mentioned in Introduction, the results in [2] are essential for our study of congenial semigroups. The quasi-orders \(\le _\sigma \) occurred in [2] as derivation relation of specific rewriting systems called 0S schemes. We recall all necessary concepts and results in this section.

Let A be an alphabet and \(w\in A^+\) be a word. We denote by \(\vert w\vert \) the length of the word w. For words \(x,y,z\in A^*\) such that \(w=xyz\), we say that x is a prefix, y is a factor and z is a suffix of the word w.

For a language \(L\subseteq A^*\), we say that it is unavoidable over A if there are only finitely many words which do not have a factor in L. In other words, L is unavoidable over A if and only if \(A^* \setminus A^*LA^*\) is finite. Alternatively, that definition may be described using infinite words. For this purpose, by an infinite word u we mean a sequence of letters indexed by the set \(\mathbb N\), i.e, \(u=a_1a_2 \dots a_n \dots \), where all \(a_i\)’s are letters in A. In this case, by a factor of u we mean a finite word \(a_i\dots a_j\), for some indices \(i\le j\). Then L is unavoidable over A if and only if every infinite word u over A has a factor v such that \(v\in L\). By \(A^\infty \) we mean the set of all infinite words over A. Finally, for a word \(u\in A^+\), we denote the so-called periodic infinite word \(uu \dots \) \(\in A^\infty \) by \(u^\infty \).

3.1 Derivation Relations

By an 0S scheme is meant a pair (AR) where A is an alphabet and R is a finite set of rules of the form \(a \rightarrow u\), where \(a\in A\) and \(u\in A^*\). Moreover, an 0S scheme is called propagating (also called length-increasing) if \(u\in A^+\) for all rules in R. For such a set of rules, a derivation relation on \(A^*\) is usually defined in two steps: we write \(xay \Rightarrow _R xuy\) whenever \(x,y\in A^*\), and \(a\rightarrow u \in R\); then the derivation relation for R, denoted by \(\overset{*}{\Rightarrow }_{R}\), is the reflexive and transitive closure of \(\Rightarrow _R\). Clearly, the derivation relation is a stable quasi-order, and the basic question studied in [2] is whether it is a wqo. Notice that by [2, Theorem 1.12], the derivation relation of a length-increasing 0S scheme is a wqo if and only if it is a total regulator, the notion which is the main object of that paper.

Now, we may recall a key result of [2].

Proposition 3.1

([2, Theorem 2.3.]) Let (AR) be an 0S scheme. Then the following conditions are equivalent:

  1. (i)

    The relation \(\overset{*}{\Rightarrow }_{R}\) is a well quasi-order on \(A^*\).

  2. (ii)

    The language \(\{ awa \,\vert \,a\in A,w\in A^*, a \overset{*}{\Rightarrow }_{R} awa \}\) is unavoidable over A.

Section 2 in [2] is devoted to the proof of this result. In Section 3 of that paper, it is shown that under an additional assumption on a given 0S scheme (AR), there exists a homomorphism \(\sigma :A^+\rightarrow S\) onto a finite ordered semigroup, such that \(\overset{*}{\Rightarrow }_{R}\ =\ \le _{\sigma }\). The additional necessary condition is that the set \(R^a =\{u \,\vert \,a \overset{*}{\Rightarrow }_{R}u \}\) is regular for every letter a. In particular, the assumption is satisfied for R for which the derivation relation \(\overset{*}{\Rightarrow }_{R}\) is a wqo. We would find it useful to have a characterization for quasi-orders \(\le _{\sigma }\) analogue to Proposition 3.1. If we can construct, for an arbitrary given homomorphism \(\sigma \), an equivalent 0S scheme R, then we obtain such a characterization as a direct consequence of Proposition 3.1. Unfortunately, we cannot take for such R the relation \(R_\sigma \) mentioned in Introduction as R has to be finite. In fact, there is no general construction of such finite relation R and the existence of an equivalent 0S scheme seems to be a property of the considered homomorphism \(\sigma \). We introduce the following terminology in order to apply Proposition 3.1.

Definition 3.2

Let \(\sigma :A^+\rightarrow S\) be a homomorphism onto a finite ordered semigroup. The relation \(\le _{\sigma }\) is representable by 0S scheme if there is an 0S scheme (AR) such that \(\overset{*}{\Rightarrow }_{R}\ =\ \le _{\sigma }\).

The direct consequence of Proposition 3.1 is the following statement.

Corollary 3.3

Let \(\sigma :A^+\rightarrow S\) be a homomorphism onto a finite ordered semigroup such that \(\le _{\sigma }\) is representable by 0S scheme. Then \(\le _{\sigma }\) is a wqo on \(A^*\) if and only if the language \(L_{\sigma } = \{awa \,\vert \,a\in A,w\in A^*, a\le _{\sigma }awa\}\) is unavoidable over A.

3.2 Characterization of Representable Quasi-Orders

Our goal is to characterize homomorphisms \(\sigma \) which have the quasi-order \(\le _{\sigma }\) representable by 0S scheme. Such characterization is implicitly contained in [2, Lemma 3.4], where one may find a construction of an appropriate 0S scheme, which we employ. Nevertheless, we reprove the result here using a different formalism, since the formalism used in [2, Lemma 3.4] does not fit our purpose, namely in the case when more letters are mapped onto one element of the semigroup. This case is not solved in [2].Footnote 1

Let \(\sigma :A^+\rightarrow S\) be a given homomorphism onto a finite ordered semigroup S with the partial order denoted by \(\le \) as usual. For technical reasons, and just for this part of the paper, assume that A is linearly ordered, and consider the corresponding shortlex order \(\le _{slex}\) on \(A^*\). Furthermore, we denote by \(\simeq _\sigma \) the equivalence relation corresponding to the quasi-order \(\le _{\sigma }\), i.e., we write \(u\simeq _\sigma v\) if \(u\le _{\sigma }v\) and \(v\le _{\sigma }u\). In particular, we call letters \(a,b\in A\) equivalent, if \(a\simeq _\sigma b\), which means that \(\sigma (a)=\sigma (b)\), since the relation \(\le \) is a partial order on S. Observe that the classes of the equivalence relation \(\simeq _\sigma \) consist of words of the same length which can be obtained from each other by replacing some occurrences of letters by equivalent letters. In particular, all equivalence classes of \(\simeq _\sigma \) are finite. We say that \(u\in A^*\) is basic if it is the minimal word in the shortlex order in its \(\simeq _\sigma \)-class.

Now, we consider the covering relation of the quasi-order \(\le _{\sigma }\), which we prefer to introduce formally since the relation \(\le _{\sigma }\) is not a partial order.

Definition 3.4

Let \(\sigma :A^+\rightarrow (S,\le )\) be a homomorphism onto a finite ordered semigroup. We define a covering relation \(\lessdot _\sigma \) of the relation \(\le _{\sigma }\) on \(A^*\) by setting, for \(u,v\in A^*\), \(u\lessdot _\sigma v\) if the following three conditions are satisfied:

  • \(u\le _{\sigma }v\),

  • \(u\not \simeq _\sigma v\),

  • for every \(w\in A^*\) such that \(u \le _{\sigma }w \le _{\sigma }v\), we have \(u \simeq _\sigma w\) or \(w\simeq _\sigma v\).

For a letter \(a\in A\), we denote \(P_a^\sigma = \{ u\in A^+ \,\vert \,a\lessdot _\sigma u, u \text { is basic}\}\).

The following lemma states an expected property of the quasi-order \(\le _{\sigma }\) and the covering relation \(\lessdot _\sigma \).

Lemma 3.5

Let \(\sigma :A^+\rightarrow (S,\le )\) be a homomorphism onto a finite ordered semigroup. The quasi-order \(\le _{\sigma }\) is the transitive closure of the relation \(\lessdot _\sigma \ \cup \ \simeq _\sigma \).

Proof

Since both relations \(\lessdot _\sigma \) and \(\simeq _\sigma \) are contained in \(\le _{\sigma }\), the considered transitive closure of the relation \(\lessdot _\sigma \cup \simeq _\sigma \) is contained in \(\le _{\sigma }\) as well. It remains to show the reverse inclusion. First, we introduce one more notation. For \(s,t\in A^*\), we write \(s<_\sigma t\) if \(s\le _{\sigma }t\) and \(s\not \simeq _\sigma t\).

Now, let \(u,v\in A^*\) be words such that \(u \le _{\sigma }v\). If \(u\simeq _\sigma v\) or \(u \lessdot _\sigma v\), then we are done. Thus, we assume that there exists \(w\in A^*\) such that \(u<_\sigma w <_\sigma v\). Then, for the pair \(u<_\sigma w\), there are two possibilities: \(u \lessdot _\sigma w\) or there exists \(w'\) such that \(u<_\sigma w' <_\sigma w\). Then we may continue in the same way with pairs \(u <_\sigma w'\), \(w' <_\sigma w\), and also with \(w <_\sigma v\). In this way we may obtain expanding sequences of the form

$$\begin{aligned} u= w_1<_\sigma w_2<_\sigma \dots <_\sigma w_n =v. \end{aligned}$$
(1)

For every pair of words \(s,t\in A^*\), the assumption \(s<_\sigma t\) implies \(\vert s\vert \le \vert t\vert \). Therefore, for every \(w_i\), a member of the sequence (1), we have \(\vert u\vert \le \vert w_i\vert \le \vert v \vert \). Moreover, if we have \(w_i \simeq _\sigma w_j\) in (1) for some indexes \(1\le i<j \le n\), then we get

$$w_i \le _{\sigma }w_{i+1} \le _{\sigma }\dots \le _{\sigma }w_j \simeq _\sigma w_i ,$$

and hence

$$w_i \simeq _\sigma w_{i+1} \simeq _\sigma \dots \le _{\sigma }w_j \simeq _\sigma w_i$$

which is a contradiction with (1). We get that \(w_i \not \simeq _\sigma w_j\) for \(i\not = j\). In particular, the words \(w_i\)’s are pairwise different. Since \(\vert u\vert \le \vert w_i\vert \le \vert v \vert \), there are only finitely many sequences of the form (1). Taking the longest possible such sequence we get \(w_i \lessdot _\sigma w_{i+1}\) for every \(i\in \{1,\dots ,n-1\}\). This yields that a pair (uv) belongs to the transitive closure of the relation \(\lessdot _\sigma \ \cup \ \simeq _\sigma \). \(\square \)

We describe the relation \(\lessdot _\sigma \) using the sets \(P_a^\sigma \).

Lemma 3.6

Let \(u=a_1\dots a_n\) be a word, where \(a_i\in A\) are letters, and \(v\in A^*\) be a word such that \(u\lessdot _\sigma v\). Then there exists an index \(i\in \{1,\dots ,n\}\), a word \(p\in P_{a_i}^\sigma \), and a factorization \(v=v_1\dots v_n\), where \(v_1,\dots ,v_n \in A^+\) are such that \(v_i\simeq _\sigma p\) and \(v_j \simeq _\sigma a_j\) for every \(j\not =i\).

Proof

Let \(v=v_1\dots v_n\) be a factorization such that \(a_i \le _{\sigma }v_i\) for every \(i=1,\dots , n\). For each i we denote \(w_i=a_1\dots a_{i-1}v_ia_{i+1}\dots a_n\). Since \(u\le _{\sigma }w_i \le _{\sigma }v\), we have \(u \simeq _\sigma w_i\) or \(w_i \simeq _\sigma v\) for each i. The first condition means that \(a_i\simeq _\sigma v_i\). If this property holds for every i, then \(v=v_1\dots v_n \simeq _\sigma u\), which is a contradiction with the assumption \(u\lessdot _\sigma v\). Therefore, there is an index i such that \(w_i\simeq _\sigma v\). This condition implies \(\vert w_i\vert = \vert v \vert \) which gives \(v_j \in A\) for every \(j\not = i\). In fact, we get \(v_j \simeq _\sigma a_j\) for \(j\not = i\). Now, we denote p the basic word such that \(p\simeq _\sigma v_i\). We know that \(a_i \le _{\sigma }p\) and \(a_i \not \simeq _\sigma p\). To conclude the proof, it is enough to check that \(a_i \lessdot _\sigma p\), which means \(p\in P_{a_i}^\sigma \). So, let \(p'\in A^+\) be a word such that \(a_i \le _{\sigma }p' \le _{\sigma }p\). Then \(u \le _{\sigma }a_1\dots a_{i-1}p'a_{i+1}\dots a_n \le _{\sigma }a_1\dots a_{i-1} p a_{i+1}\dots a_n \simeq _\sigma w_i \simeq _\sigma v\). Since \(u\lessdot _\sigma v\), we get \(a_i \simeq _\sigma p'\) or \(p' \simeq _\sigma p\). Therefore, we indeed get \(a_i \lessdot _\sigma p\). \(\square \)

We formulate one more technical observation concerning the sets \(P_a^\sigma \).

Lemma 3.7

The set \(P_a^\sigma \) is an antichain with respect to the quasi-order \(\le _{\sigma }\).

Proof

Assume that there are \(u,v\in P_a^\sigma \), such that \(u\not = v\), \(u\le _{\sigma }v\). Then \(a\le _{\sigma }u \le _{\sigma }v\). This implies that \(a\simeq _\sigma u\) or \(u\simeq _\sigma v\). However, the first case is a contradiction with the assumption \(a\lessdot _\sigma u\), and the second case is a contradiction with the assumption that u and v are basic and distinct. \(\square \)

We are now ready to formulate the announced characterization.

Proposition 3.8

Let \(\sigma :A^+\rightarrow S\) be a homomorphism onto a finite ordered semigroup. Then the quasi-order \(\le _{\sigma }\) is representable by 0S scheme if and only if, for every \(a\in A\), the set \(P_a^\sigma \) is finite.

Proof

Assume first that R is an 0S scheme such that \(\overset{*}{\Rightarrow }_{R}\, = \, \le _{\sigma }\). For each \(p\in P_a^\sigma \), we have \(a \le _{\sigma }p\) and there exist words \(u_1,u_2,\dots ,u_n\) such that

$$a=u_1 \Rightarrow _R u_2 \Rightarrow _R \dots \Rightarrow _R u_n=p .$$

Since \(a\lessdot _\sigma p\), there is an index i such that

$$u_1 \simeq _\sigma \dots \simeq _\sigma u_i \lessdot _\sigma u_{i+1} \simeq _\sigma \dots \simeq _\sigma u_n .$$

Then \(u_i=b\in A\) is a letter as \(a\simeq _\sigma u_i\) holds. This means that \(u_i\Rightarrow _R u_{i+1}\) is, in fact, the rule \(b \rightarrow u\) in R, where \(u\simeq _\sigma p\). By Lemma 3.7 we see that for different \(p\in P_a^\sigma \) we get different rules. Since R is finite, we deduce that the set \(P_a^\sigma \) has to be finite.

Now assume that all sets \(P_a^\sigma \) are finite. Then we may consider the 0S scheme (AR), where R is the following finite set:

$$R=\{a\rightarrow b \,\vert \,a,b\in A, a\simeq _\sigma b\}\cup \{a \rightarrow p \,\vert \,a\in A, p \in P_a^\sigma \} .$$

We claim that \(\overset{*}{\Rightarrow }_{R}\, =\, \le _{\sigma }\). Indeed, since R is a subset of \(\le _{\sigma }\) we have \(\overset{*}{\Rightarrow }_{R}\ \subseteq \ \le _{\sigma }\). By Lemma 3.6 we know that \(\overset{*}{\Rightarrow }_{R}\) contains the relation \(\lessdot _\sigma \). Since \(\overset{*}{\Rightarrow }_{R}\) contains also \(\simeq _\sigma \), the transitivity of \(\overset{*}{\Rightarrow }_{R}\) gives that \(\overset{*}{\Rightarrow }_{R}\) contains the transitive closure of \(\lessdot _\sigma \ \cup \ \simeq _\sigma \). This means that the relation \(\overset{*}{\Rightarrow }_{R}\) contains the quasi-order \(\le _{\sigma }\) by Lemma 3.5. Thus the claim \(\overset{*}{\Rightarrow }_{R}\, =\, \le _{\sigma }\) is proved. \(\square \)

For the purpose of this paper, there are two important classes of homomorphisms which have the relation \(\le _{\sigma }\) representable by an 0S scheme. We apply Proposition 3.8 to them.

Corollary 3.9

Let \(\sigma :A^+\rightarrow S\) be an arbitrary homomorphism onto a finite ordered semigroup such that the relation \(\le _{\sigma }\) is a well quasi-order on \(A^*\). Then the language \(L_{\sigma } = \{awa \,\vert \,a\in A,w\in A^*, a\le _{\sigma }awa\}\) is unavoidable over A.

Proof

It was stated above in Lemma 3.7 that every \(P_a^\sigma \) forms an antichain in the quasi-order \(\le _{\sigma }\). Since the quasi-order \(\le _{\sigma }\) is a well quasi-order on \(A^*\), this antichain has to be finite. Thus, the relation \(\le _{\sigma }\) is representable by Proposition 3.8 and Corollary 3.3 can be applied. \(\square \)

In the following corollary, the semigroup S is also viewed as an alphabet.

Corollary 3.10

Let S be a finite ordered semigroup and \({\text {eval}}_S:S^+\rightarrow S\) the evaluation homomorphism. Then the following conditions are equivalent.

  1. (i)

    The semigroups S is congenial.

  2. (ii)

    The relation \(\le _{{\text {eval}}_S}\) is a well quasi-order on the free monoid \(S^*\).

  3. (iii)

    The language \(L_{{\text {eval}}_S}=\{sws \,\vert \,s\in S,w\in S^*, s\le _{{\text {eval}}_S} sws\}\) is unavoidable over the alphabet S.

Proof

The equivalence “(i) \(\iff \) (ii)" is stated in Proposition 2.7, thus we need to show only the equivalence “(ii) \(\iff \) (iii)". The implication “(ii) \(\Longrightarrow \) (iii)" follows by Corollary 3.9. Thus, with respect to Proposition 3.8 and Corollary 3.3, it is enough to show that \(P_s^{{\text {eval}}_S}\) is finite for every letter \(s\in S\). Let \(w=s_1s_2\dots s_n\in S^+\) be a word of length \(n> 2\) such that \(s\le _{{\text {eval}}_S} w\). For \(t=s_1\cdot s_2\) in S, we may write \(s\le _{{\text {eval}}_S} ts_3\dots s_n\le _{{\text {eval}}_S} w\). This means that if \(s\lessdot _{{\text {eval}}_S} w\) then \(\vert w\vert \le 2\). Thus all sets \(P_s^{{\text {eval}}_S}\) are finite. \(\square \)

3.3 Testing Unavoidability

The purpose of this subsection is to show that condition (iii) of Corollary 3.10 gives an algorithm testing whether a given semigroup S is congenial.

In the conference version of the paper [9], such an algorithm is presented in detail. However, that algorithm can be seen as a special case of a well-known algorithm testing the unavoidability of a given regular language which we recall next. To apply this general algorithm for a given homomorphism \(\sigma :A^+\rightarrow ~S\), we check that the language \(L_{\sigma } = \{awa \,\vert \,a\in A,w\in A^*, a\le _{\sigma }awa\}\) is regular: the set \(L_\sigma \) can be viewed as a finite union \(\bigcup _{a\in A}L_\sigma ^a\) of languages \(L_\sigma ^a=\{awa \,\vert \,w\in A^*, a\le _{\sigma }awa\}=L^a \cap aA^*a\), where the regular language \(L^a\) was introduced in Section 2.3.

Alternatively, we may construct a deterministic automaton \((Q,A,\delta ,i,F)\) recognizing the language \(L_\sigma \) as follows. We want to hold three values for an input word, namely the first letter, the last letter, and the evaluation of the word in the homomorphism \(\sigma \). Thus, we take the set \(Q=A\times A\times S \cup \{i\}\). The action by a letter \(a\in A\) is defined by the rules \(\delta (i,a)=(a,a,\sigma (a))\) and \(\delta ( (b,c,s),a) = (b,a,s\cdot \sigma (a))\) for \(b,c\in A\), \(s\in S\). Finally, the set of terminal states is given by \(F=\{(a,a,s) \,\vert \,a \in A, s\in S, \sigma (a)\le s\}\). Notice that to apply Corollary 3.10 the described automaton can be constructed just for \(A=S\) and \(\sigma ={\text {eval}}_S\). Nevertheless, the construction is useful also in the general case for an arbitrary homomorphism \(\sigma : A^+ \rightarrow S\).

For an arbitrary regular language \(K\subseteq A^*\) (given by a deterministic finite automaton), one can decide whether the language K is unavoidable over A, i.e., whether the complement \(\overline{K}=A^* \setminus A^*KA^*\) is finite, in the following way. Since \(\overline{K}\) is regular, we may look at a trim automaton \(\mathcal A_K\) for that language. The question whether \(\overline{K}\) is infinite is equivalent to the question whether there is a cycle in the automaton \(\mathcal A_K\). Since the automaton \(\mathcal A_K\) can be computed from a given deterministic finite automaton recognizing the language K, we get the appropriate algorithm testing whether K is unavoidable over A. In particular, we state the following result.

Proposition 3.11

Let \(\sigma :A^+\rightarrow S\) be a homomorphism onto a finite ordered semigroup S. Then it is decidable whether the set \(L_\sigma =\{awa \,\vert \,a\in A, w\in A^*,a\le _\sigma awa\}\) is unavoidable over A.

We are interested in one particular detail of the general algorithm for a regular language K. If there is a cycle in the automaton \(\mathcal A_K\), then there is a word u such that \(u^\infty \) avoids K. We state this side result of checking the unavoidability of \(L_\sigma \), since it plays a crucial role in the next section.

Corollary 3.12

Let \(\sigma : A^+ \rightarrow S\) be a homomorphism onto a finite ordered semigroup S. Then there is an infinite word avoiding \(L_\sigma \) if and only if there is a periodic infinite word \(u^\infty \) with that property.

We may finish this subsection with a brief discussion concerning the complexity aspects of the presented algorithm that decides whether a given ordered semigroup is congenial or, in a more general setting, whether \(L_\sigma \) is unavoidable over A. First of all, the deterministic automaton for \(L_\sigma \) has \(\vert A\vert ^2\cdot \vert S \vert +1\) states and the automaton can be computed in the same time. If we add non-deterministic actions by every letter from i to itself, and we change terminal states to \(\overline{F}=\{ \delta (p, w) \,\vert \,p\in F, w\in A^*\}\), then we construct a non-deterministic automaton for the language \(A^*L_\sigma A^*\). Unfortunately, we need to use the power set construction if we want to compute an automaton for the complement \(\overline{L_\sigma }=A^* \setminus A^*L_\sigma A^*\). Therefore, our algorithm is exponential with respect to the input triple \((A,S,\sigma )\). We will not analyze the time complexity of the algorithm in more detail as we give a more efficient algorithm in the next section.

4 Polynomial Characterization of Congenial Ordered Semigroups

The main purpose of this section is to obtain a better characterization of congeniality than that of Corollary 3.10. Such a characterization should be more transparent and testable in polynomial time, which is not the case of the algorithm of the previous subsection.

4.1 Characterization of Congenial Semigroups

We show that an ordered semigroup S is congenial whenever we have an onto homomorphism \(\sigma : A^+ \rightarrow S\) determining the wqo \(\le _\sigma \). To prove the following main result, we combine the results of the previous sections.

Theorem 4.1

Let S be a finite ordered semigroup. Then the following conditions are equivalent:

  1. (i)

    The ordered semigroup S is congenial.

  2. (ii)

    For every alphabet B and a homomorphism \(\varphi :B^+ \rightarrow S\) the relation \(\le _\varphi \) is a well quasi-order.

  3. (iii)

    There exists an alphabet A and an onto homomorphism \(\sigma :A^+ \rightarrow S\) such that \(\le _\sigma \) is a well quasi-order.

  4. (iv)

    There exists an alphabet A and an onto homomorphism \(\sigma :A^+ \rightarrow S\) such that, for every \(n\in \mathbb N\) and \(a_1, \dots , a_n\in A\), there exists \(i\in \{1,\dots ,n\}\) and \(p\in \mathbb N\) such that \(a_i \le _\sigma a_i (a_{i+1}\dots a_n a_1\dots a_i)^p\).

  5. (v)

    There exists an alphabet A and an onto homomorphism \(\sigma :A^+ \rightarrow S\) such that, for every \(n\in \mathbb N\) and \(u_1, \dots , u_n \in A^+\), there exists \(i\in \{1,\dots ,n\}\) and \(p\in \mathbb N\) such that \(u_i \le _\sigma u_i (u_{i+1}\dots u_n u_1\dots u_i)^p\).

  6. (vi)

    There exists an alphabet A and an onto homomorphism \(\sigma :A^+ \rightarrow S\) such that, for every \(n\in \mathbb N\) and \(u_1, \dots , u_n\in A^+\), there exists \(i\in \{1,\dots ,n\}\) and \(p\in \mathbb N\) such that \(\sigma (u_i) \le \sigma \left( u_i (u_{i+1}\dots u_n u_1\dots u_i)^p\right) \).

  7. (vii)

    For every \(n\in \mathbb N\) and \(s_1, \dots , s_n\in S\), there exists \(i\in \{1,\dots ,n\}\) and \(p\in \mathbb N\) such that \(s_i \le s_i\cdot (s_{i+1}\cdots s_n\cdot s_1\cdots s_i)^p\).

  8. (viii)

    The relation \(\le _{{\text {eval}}_S}\) is a well quasi-order on \(S^*\).

Proof

We show the implications from top to bottom.

“(i)\(\implies \)(ii)": This holds by definition of a congenial semigroup.

The implication “(ii)\(\implies \)(iii)" is straightforward.

Notice that in conditions (iii) – (vi) the same pair \((A,\sigma )\) is employed.

“(iii)\(\implies \)(iv)": Let \(a_1, \dots ,a_n\in A\) be an arbitrary sequence of letters. We consider a new alphabet \(B=\{b_1,\dots , b_n\}\) of size n and a homomorphism \(\alpha ~:~B^+~\rightarrow ~A^+\) such that \(\alpha (b_i)=a_i\) for all \(i\in \{1,\dots ,n\}\). We denote the composition \(\sigma \circ \alpha \) by \(\varphi \). The relation \(\le _\varphi \) is a wqo by Lemma 2.6. By Corollary 3.9, we see that \(L_\varphi =\{bwb \,\vert \,b\in B,w\in B^*, b\le _\varphi bwb \}\) is unavoidable over B. In particular, the infinite word \((b_1b_2\dots b_n)^\infty \) has a factor in \(L_\varphi \). Therefore, there exists \(i\in \{1,\dots ,n\}\) and \(p\in \mathbb N\) such that \(b_i\le _\varphi b_i (b_{i+1}\dots b_nb_1\dots b_i)^p\). Finally, we deduce

$$\sigma (a_i)=\varphi (b_i) \le \varphi (b_i (b_{i+1}\dots b_nb_1\dots b_i)^p)= \sigma (a_i(a_{i+1}\dots a_na_1\dots a_i)^p).$$

“(iv)\(\implies \)(v)": We apply condition (iv) on the word \(u=u_1u_2\dots u_n\) which we see as a concatenation of individual letters. Thus, there are \(i\in \{1,\dots ,n\}\), \(p\in \mathbb N\), \(a\in A\) and \(u_i',u_i''\in A^*\) such that \(u_i=u'_iau''_i\) and \(a\le _\sigma a(u''_i u_{i+1} \dots u_n u_1 \dots u_{i-1} u'_i a)^p\). If we multiply this inequality by the word \(u'_i\) on left and by the word \(u''_i\) on right, we get \(u_i\le _\sigma u_i(u_{i+1} \dots u_n u_1 \dots u_{i-1} u_i)^p\).

The implications “(v)\(\implies \)(vi)" and “(vi)\(\implies \)(vii)" are trivial.

“(vii)\(\implies \)(viii)": Assume that the relation \(\le _{{\text {eval}}_S}\) is not a well quasi-order on \(S^*\). By Corollary 3.10, there is an infinite word avoiding the set \(L_{{\text {eval}}_S}\). By Corollary 3.12, there is a finite word \(u\in S^+\) such that \(u^\infty \) avoids \(L_{{\text {eval}}_S}\). Let \(u=s_1\dots s_n\), where \(s_1,\dots ,s_n\in S\). By condition (vii), we have \(s_i \le s_i (s_{i+1}\cdots s_ns_1\cdots s_i)^p\) for some index i and \(p\in \mathbb N\). Therefore, \(s_i \le _{{\text {eval}}_S} s_i (s_{i+1}\dots s_ns_1\dots s_i)^p\) for that i and \(p\in \mathbb N\). This contradicts the assumption that \(u^\infty \) avoids \(L_{{\text {eval}}_S}\).

“(viii)\(\implies \)(i)": It is contained in Proposition 2.7. \(\square \)

The equivalence of the conditions (i) and (iii) in Theorem 4.1 gives the following result saying that whether the induced quasi-order \(\le _{\sigma }\) is a wqo does not depend on the homomorphism \(\sigma \) and it is indeed a property of the ordered semigroup.

Corollary 4.2

Let \(\sigma : A^+ \rightarrow S\) be a homomorphism onto a finite ordered semigroup S. Then \(\le _\sigma \) is a wqo if and only if the semigroup S is congenial.

We may also obtain an alternative characterization of congeniality using the operation \((~~)^\omega \).

Theorem 4.3

Let S be an arbitrary finite ordered semigroup. Then S is congenial if and only if for every \(n\in \mathbb N\) and \(s_1, \dots , s_n\in S\), there exists \(i\in \{1,\dots ,n\}\) such that \(s_i \le s_i \cdot (s_{i+1}\cdots s_n \cdot s_1\cdots s_i)^{\omega }\).

Proof

With respect to condition (vii) in Theorem 4.1, it is enough to show that the following two conditions are equivalent for a given pair of elements st in S:

$$\text { (a) }\ \ \text {there exists } p \in \mathbb N \text { such that } s\le s \cdot (t\cdot s)^p;\qquad \text { (b) }\ \ s\le s \cdot (t\cdot s)^\omega .$$

If condition (a) holds, then we may iterate the inequality and obtain

$$s\le s \cdot (t\cdot s)^p \le s \cdot (t\cdot s)^p \cdot (t\cdot s)^p \le \dots \le s \cdot (t\cdot s)^{pk}$$

for an arbitrary \(k\in \mathbb N\). Since there is an integer \(k\in \mathbb N\) such that \((t\cdot s)^k=(t\cdot s)^\omega \), we have also \((t\cdot s)^{pk}=(t\cdot s)^\omega \) for this k. Thus we get condition (b).

The existence of k satisfying \((t\cdot s)^k=(t\cdot s)^\omega \) gives directly the proof of the implication “(b)\(\implies \)(a)". \(\square \)

4.2 Obstacles for a Structural Characterization

Unfortunately, it is not possible to bound n in Theorem 4.3. Indeed, we introduce in the following example a sequence of ordered semigroups \(S_m\) such that every \(S_m\) satisfies the condition in Theorem 4.3 if \(n<m\) and does not satisfy the condition for \(n=m\). The same holds for conditions (iv)–(vii) in Theorem 4.1.

We will show in Section 4.5 that such a bound for n exists if the size of the semigroup is fixed.

Example 4.4

For every integer \(m\ge 2\), we construct a finite ordered semigroup \(S_m\) as follows. The semigroup is given by the presentation over the m-letter alphabet \(A_m=\{a_1,\dots , a_m\}\):

$$ S_m=\langle \, a_1, \dots , a_m \,\vert \,a_ia_j=0\ (\text {for } i,j \in \{1,\dots , m\} \text { such that } j-i\not \in \{1, 1-m\}), $$
$$ \qquad \qquad \qquad \qquad (a_i\dots a_ma_1\dots a_{i-1})^2=a_i\dots a_ma_1\dots a_{i-1} \ (\text {for } i\in \{1,\dots , m\} ) \rangle . $$

Recall that a presentation \(\langle A \,\vert \,R\, \rangle \) defines the semigroup \(S=A^*\!/\!\rho _R\), where A is a generating set and \(\rho _R\) is the smallest congruence generated by the relation R. We explain the meaning of the semigroup presentation of \(S_m\) informally. We denote \(u=a_1\dots a_m\). The non-zero elements of \(S_m\) are words of length at most \(2m-1\) which are factors of \(u^\infty \). Every word which is not a factor of \(u^\infty \) is identified with 0 by the rule \(a_ia_j=0\) for appropriate indices. Furthermore, any word which is a factor of \(u^\infty \) of length at least 2m contains a factor w of length 2m of the form \(a_i\dots a_ma_1\dots a_ma_1\dots a_{i-1}\). Each such factor w may be shortened by the rule \((a_i\dots a_ma_1\dots a_{i-1})^2=a_i\dots a_ma_1\dots a_{i-1}\). This procedure may be repeated until the resulting word has length smaller than 2m. The important property of the previous reduction is that the prefixes and suffixes of length m are kept.

The previous description implicitly explains both the natural homomorphism \(\sigma _m : A_m^+ \rightarrow S_m\) and the multiplication on \(S_m\). Indeed, for the image of a word u in the homomorphism, we reduce the word u to the unique word of length smaller than 2m (or to 0), and for two elements \(s,t \in S_m\) represented by words \(u_s\) and \(u_t\), we concatenate \(u_s\) and \(u_t\), and reduce the word to the unique word (or to 0), which represents the product \(s\cdot t\).

Now, we introduce the order on \(S_m\). Firstly, we put 0 to be the top element. Then for two factors xy of \(u^\infty \) of length at most \(2m-1\), we have

$$ x\le y\quad \text {if} \quad \left\{ \begin{array}{l} x=y \text { or}\\ 1<\vert x\vert < m \le \vert y\vert \ \text { and } x \text { is both the prefix and the suffix of } y . \end{array}\right. $$

One may check that the relation \(\le \) is a stable order on the semigroup \(S_m\). We point out that \(a_i\le y\) only for \(y\in \{a_i,0\}\).

In Theorem 4.3, we consider the following condition for a given integer n:

$$\begin{aligned} (\forall s_1, \dots , s_n\in S) (\exists i\in \{1,\dots ,n\}) : s_i \le s_i\cdot (s_{i+1}\cdots s_n \cdot s_1\cdots s_i)^{\omega }. \end{aligned}$$
(2)

To test whether \(S_m\) satisfies condition (2) for a given n, we distinguish two cases depending on whether m divides n or not.

First assume that m divides n. For every \(i\in \{1,\dots , n\}\) we denote [im] the remainder after dividing i by m and put \(s_i=a_{[i,m]}\). Then \(s_1\cdot s_2\cdots s_n=(a_1\cdots a_m)^{\frac{n}{m}}=a_1\dots a_m\) and we see that \(S_m\) does not satisfy condition (2).

If we assume that m does not divide n, then \((s_{i+1}\cdots s_n\cdot s_1\cdots s_i)^{\omega }\not =0\) only if \(s_1 s_2\dots s_n\) is a conjugate to some power of \(a_1\dots a_m\). This is not possible when each \(s_i\) is an element in \(A_m\). Thus there is an index i such that \(s_i\not \in A_m\), and condition (2) is valid for this index.

We may conclude the example with the statement that \(S_m\) satisfies condition (2) if and only if m does not divide n. In particular, \(S_m\) satisfies condition (2) for all \(n<m\) and does not satisfy the condition for \(n=m\).

Remark 4.5

We add one more observation concerning the previous example. Every semigroup \(S_m\) may be modified to a congenial semigroup, namely it is enough to extend the order (of the same semigroup) by adding the inequality \(s_m\le s_m s_1 \dots s_m\). This observation suggests that there is a subtle borderline between congenial and non-congenial semigroups. In particular, we may not expect any transparent structural characterization of congeniality as in the case of unordered semigroups.

4.3 Consequences of the Characterization

Now we give some basic consequences of Theorem 4.3. A straighforward combinatorial proof of the following corollary was given in [9].

Corollary 4.6

If S is a congenial ordered semigroup, then for every element \(s\in S\) we have \(s\le s^{\omega +1}\).

Proof

We simply take \(n=1\) in Theorem 4.3. \(\square \)

As a special case of Corollary 4.6, we obtain that every unordered congenial semigroup satisfies \(s=s^{\omega +1}\), which means that the semigroup S is completely regular. This may be viewed as a reason why the congeniality has a transparent structural characterization for unordered semigroups, however, we get a more complicated property for ordered semigroups in Theorem 4.3.

We also mention the direct consequence of Theorem 4.3 if we take \(n=2\).

Corollary 4.7

Let S be a congenial ordered semigroup. Then, for every \(s,t\in S\), we have

$$ s\le (s\cdot t)^\omega \cdot s \quad \text { or } \quad t\le t\cdot (s\cdot t)^\omega .\quad $$

This statement may be used to reprove the result mentioned in the introduction, namely that unordered congenial semigroups are precisely chains of simple semigroups. The result is proved in [11], however we include an alternative proof here to show, how the characterization of unordered case is related to the condition in Theorem 4.3.

Nevertheless, to prove the result, we need to use Green’s relations, the fundamental notion of structural theory of (finite) semigroups. The reader may find more details in any book about semigroup theory, e.g., in [8] by Howie. The reader may wish to look into [11], where chains of simple semigroups are elaborated (see in particular [11, Lemma 2]). Notice that the rest of this subsection has no impact to other parts of the paper, so the reader may also wish to skip it.

Let xy be two elements of a semigroup S. We write \(x\le _{\mathscr {L}} y\) if \(S^1x\subseteq S^1y\). Notice that \(S^1x=\{sx \,\vert \,s\in S \}\cup \{x\}\) is the principal left ideal of S generated by the element x. Thus the fact \(S^1x\subseteq S^1y\) is equivalent with a condition \(x\in S^1y\). Hence the pair of elements (xy) is in the defined relation \(\le _{\mathscr {L}}\) if and only if there is \(s\in S^1\) such that \(x=sy\). We put \(x\mathscr {L} y\) if \(x\le _{\mathscr {L}} y\) and \(y\le _{\mathscr {L}} x\), i.e., the elements x and y are \(\mathscr {L}\)-related if x and y generate the same left ideal. The basic observation is that the relation \(\le _\mathscr {L}\) is a quasi-order on S and the relation \(\mathscr {L}\) is the corresponding equivalence relation. Analogically, we define the quasi-orders \(\le _{\mathscr {R}}\) and \(\le _{\mathscr {J}}\) and the equivalence relations \(\mathscr {R}\) and \(\mathscr {J}\), if we consider right ideals and two-sided ideals instead of left ideals. We summarize it in the following way:

$$\begin{aligned} x&\le _{\mathscr {R}} y&\quad&\text {if} \quad&xS^1&\subseteq yS^1,{} & {} \quad \quad{} & {} x\, \mathscr {R}\ y&\quad&\text {if} \quad&S^1&= yS^1,\\ x\,&\le _{\mathscr {J}}\, y&\quad&\text {if} \quad&S^1 xS^1&\subseteq S^1yS^1,{} & {} \quad \quad{} & {} x \mathscr {J}\, y&\quad&\text {if} \quad&S^1xS^1&=S^1yS^1. \end{aligned}$$

By the definitions we immediately get \(\mathscr {R}\subseteq \mathscr {J}\) and \(\mathscr {L}\subseteq \mathscr {J}\).

The following well-known lemma is often stated only implicitly in the basic literature, e.g., in [8]. An explicit formulation can be found for example in [13] by Pin, one of the first books on the algebraic theory of regular languages. To assume the finiteness of the semigroup is essential.

Lemma 4.8

([13, Prop 1.4]) Let ab be two elements of a finite semigroup S. Then

$$(a \le _{\mathscr {R}} b \text { and } a \mathscr {J} b) \Longrightarrow a\mathscr {R} b.$$

We say that a finite semigroup S is a chain of simple semigroups if it is a completely regular semigroup and all pairs of elements are comparable by the relation \(\le _\mathscr {J}\), i.e.,

$$\begin{aligned} \forall s,t\in S : s\le _\mathscr {J} t \ \text { or } \ t\le _\mathscr {J} s . \end{aligned}$$
(3)

For the purpose of the proof of the mentioned result, we need alternative characterizations of chains of simple semigroups.

Lemma 4.9

For a finite semigroup S the following conditions are equivalent.

  1. (i)

    S is a chain of simple semigroups,

  2. (ii)

    \(\forall s,t\in S : s\cdot t\ \mathscr {R}\ s\) or \(s\cdot t\ \mathscr {L}\ t\),

  3. (iii)

    \(\forall s,t\in S : s\cdot t\ \mathscr {J}\ s \) or \(s\cdot t\ \mathscr {J}\ t\),

  4. (iv)

    \(\forall s,t\in S : (s\cdot t)^\omega \cdot s=s \) or \(t\cdot (s\cdot t)^\omega =t\).

Proof

“(i)\(\implies \)(ii)": Since \( s\le _\mathscr {J} t\) implies \(s=x\cdot t\cdot y\) for some \(x,y\in S^1\), and since S is completely regular, we have also \(s=x \cdot (t\cdot y)^{\omega +1}= s \cdot (t\cdot y)^\omega \). In particular, we deduce \(s\cdot t\ \mathscr {R}\ s\). Similarly, we obtain \(t\le _\mathscr {J} s \implies s\cdot t\ \mathscr {L}\ t\).

“(ii)\(\implies \)(i)": If we put \(s=t\) in (ii), we get \(s^2\mathscr {J}s\) for every \(s\in S\). By Lemma 4.8 we get \(s^2\mathscr {R} s\), i.e, \(s^2 x=s\) for some \(x\in S^1\). Since S is finite, there are integers \(k>\ell \) such that \(s^k=s^\ell \). Multiplying that equality by x, we may reduce the exponents by 1 if \(\ell >1\). Thus there is k such that \(s^k=s\), and the semigroup S is completely regular. The implications \(s\cdot t\ \mathscr {R}\ s \implies s\le _\mathscr {J} t\) and \(s\cdot t\ \mathscr {L}\ t \implies \ t\le _\mathscr {J} s\) are trivial.

“(ii)\(\implies \)(iii)": It is enough to recall that \(\mathscr {R}\subseteq \mathscr {J}\) and \(\mathscr {L}\subseteq \mathscr {J}\).

“(iii)\(\implies \)(ii)": It follows by Lemma 4.8.

“(ii)\(\iff \)(iv)": The both conditions (if we put \(s=t\)) ensure that the semigroup S is completely regular. Moreover, under the assumption that S is completely regular, the condition \(s\cdot t\ \mathscr {R}\ s\) is equivalent to \((s\cdot t)^\omega \cdot s=s\): the implication from left to right follows from the fact that every idempotent is a left neutral element of its \(\mathscr {R}\)-class; the implication \((s\cdot t)^\omega \cdot s=s \implies s\cdot t\ \mathscr {R}\ s\) is clear. Analogically, for a completely regular semigroup S, we have \(s\cdot t\ \mathscr {L}\ t \iff t\cdot (s\cdot t)^\omega =t\).

Notice that from the formal point of view, our definition of a chain of simple semigroups differs from that in [11], where also the assumption that \(\mathscr {J}\) is congruence is used. Nevertheless, condition (iii) of Lemma 4.9 occurs in [11, Lemma 2], where alternative characterizations are presented. Thus our formalism is equivalent to that in [11] in the case of finite semigroups.

Now we are prepared to show the mentioned result describing congenial semigroups ordered by equality.

Theorem 4.10

([11]) Let S be a finite semigroup ordered by equality. Then S is congenial if and only if S is a chain of simple semigroups.

Proof

Let S be a congenial semigroup ordered by equality. By Corollary 4.7 we deduce that S satisfies condition (iv) of Lemma 4.9. Therefore, S is a chain of simple semigroups.

Let S be a chain of simple semigroups, that is S satisfies all conditions of Lemma 4.9. Assume that \(s_1,\dots , s_n\in S\) is an arbitrary sequence of elements. For \(n=1\), the equality \(s_1=s_1^{\omega +1}\) holds as S is a completely regular semigroup. Assume next that \(n\ge 2\). By condition (iii) in Lemma 4.9, one can show inductively the following statement: for every \(s_1,\dots , s_n\) there exists an index i such that \(s_1\dots s_n \mathscr {J} s_i\). Since S is a completely regular semigroup, we deduce \((s_1\dots s_n)^\omega \mathscr {J} s_i\). Then we also have \((s_i \cdots s_n \cdot s_1 \cdots s_{i+1})^\omega \ \mathscr {J}\ s_i\). Therefore \((s_i \cdots s_n \cdot s_1 \cdots s_{i+1})^\omega \ \mathscr {R}\ s_i\) by Lemma 4.8, and we deduce \((s_i \cdots s_n \cdot s_1 \cdots s_{i+1})^\omega s_i=s_i\). This means that S satisfies the condition of Theorem 4.3, and therefore the semigroup S is congenial. \(\square \)

4.4 Examples of Congenial Ordered Semigroups

A stronger condition than that of Corollary 4.7 gives the following sufficient condition for congeniality.

Proposition 4.11

Let S be a finite ordered semigroup satisfying, for every \(s,t\in S\), the inequality \(s \le s \cdot (t\cdot s )^\omega \). Then S is congenial.

Proof

Under the assumption, we show that S satisfies the condition of Theorem 4.3. Let \(n\in \mathbb N\) and \(s_1, \dots , s_n\in S\) be arbitrary. If \(n=1\), we put \(s=t=s_1\) and deduce \(s_1\le s_1^\omega s_1\). If \(n\ge 2\), we put \(s=s_1\) and \(t=s_2 \cdots s_n\). By the assumption, we obtain \(s_1 \le s_1\cdot (s_2 \cdots s_n \cdot s_1)^\omega \). Thus the semigroup S is congenial. \(\square \)

Notice that the conference version [9] contains an alternative elementary argument for the previous result, which does not use Theorem 4.3, i.e., without the application of Proposition 3.1.

The following two classes of examples are instances of Proposition 4.11. Both classes are well studied in semigroup theory, see [8] for more details. A semigroup S is 0-simple if (i) it has a zero element 0, (ii) it has exactly two non-empty ideals \(\{0\}\) and S, and (iii) \(S^2\not =\{0\}\). In this classical definition, the most important condition is the second one, which implies that all non-zero elements of S form one \(\mathscr {J}\)-class. The role of the third condition is to exclude the two-element semigroup case.

Example 4.12

Let S be a 0-simple semigroup. The compatible partial order \(\le \) is given by putting 0 to be the top element that covers the antichain of all non-zero elements. We claim that this ordered semigroup satisfies the condition of Proposition 4.11. Indeed, for \(s,t\in S\), the element \((t\cdot s)^\omega \) is either equal the zero element 0 or it is an idempotent \(\mathscr {J}\)-related to s. In the first case, we get \(s\le 0=s \cdot (t\cdot s)^\omega \). In the second case, we have \(s \mathscr {L} (t\cdot s)^\omega =e\) by dual version of Lemma 4.8 which gives \(s=ye\) for some \(y\in S^1\) and \(s\cdot e= ye\cdot e=ye=s\) follows.

Let S be an arbitrary semigroup. An element \(t\in S\) is an inverse of an element \(s\in S\) if \(sts=s\) and \(tst=t\). The semigroup S is an inverse semigroup, if every element \(s\in S\) has a unique inverse in S. Inverse semigroups admit a natural stable partial order in the following way: for elements \(a,b\in S\), we define \(a\le b\) if there exists an idempotent \(e\in S\) such that \(a=eb\). For details see [8].

Example 4.13

Let S be a finite inverse semigroup with the natural partial order \(\le \). Then for every \(s,t\in S\) we get \((s\cdot t)^\omega \cdot s \le s\) if we take \(a=(s\cdot t)^\omega \cdot s\), \(b=s\) and \(e=(s\cdot t)^\omega \) in the definition of \(\le \) above. Then considering the semigroup S ordered by dual order \(\le ^{-1}\), it is a congenial semigroup by Proposition 4.11.

4.5 A Polynomial Test for Congeniality

Now we show how to use the condition of Theorem 4.3 to decide congeniality in polynomial time. The idea of the proof is a fast searching for a potential counterexample, that is a sequence of elements \(s_1,\dots , s_n\in S\) such that

$$s_i \not \le s_i \cdot (s_{i+1} \cdots s_n \cdot s_1\cdots s_i)^{\omega }\quad \text { for all } \quad i\in \{1,\dots ,n\} . $$

For testing the inequality of this form, for a given \(s_i\), we need to compute the values up to the position \(s_i\) and after that position, i.e., products \(s_1\cdots s_{i}\) and \(s_{i+1}\cdots s_n\). For that purpose we consider pairs \((s,t)\in S^1\times S^1\) which we imagine as \((s_1\cdots s_{i}, s_{i+1}\cdots s_n)\) where the exact factorization into elements of S is not known. More precisely, we consider a directed labeled multigraph \(\mathcal G_S=(V,E)\) with the set of vertices \(V=S^1\times S^1\), where an edge \((s,t) \longrightarrow (s',t')\) is labeled by an element u in S if it is possible to transfer u from the beginning of t and put it to the end of s, where \((s',t')\) is the resulting pair in \(V=S_1\times S_1\) after that transfer of u. Moreover, we are interested only in those transfers, which contradict the inequality in Theorem 4.3. In more formal setting: we put \((s,t) \overset{u}{\longrightarrow } (s',t')\) into E if \(t=u\cdot t'\), \(s\cdot u =s'\), and \(u\not \le u \cdot (t'\cdot s')^\omega \). Notice that if there is an edge \((s,t) \overset{u}{\longrightarrow } (s',t')\) in \(\mathcal {G}_S\), then \(s\cdot t= s\cdot u\cdot t'=s'\cdot t'\). This means that the directed labeled multigraph \(\mathcal G_S\) (further referred to as graph) is a finite union of subgraphs with the fixed products. More precisely, for every \(r\in S^1\), we may denote \(V^r=\{(s,t)\in V \,\vert \,s\cdot t=r\}\), and \(E^r= E \cap (V^r \times V^r)\). Then for edges we have \(E=\bigcup _{r\in S^1} E^r\). The following lemma shows that we may reconstruct a counterexample by an appropriate directed walk in the graph \(\mathcal G_S\).

Lemma 4.14

Let \(\mathcal G_S=(V,E)\) be a graph constructed as above for a given finite ordered semigroup S. Then the following two conditions are equivalent:

  1. (i)

    There exist \(n\in \mathbb N\) and a sequence \(s_1, \dots , s_n\) of elements of S such that \(s_i \not \le s_i \cdot (s_{i+1} \cdots s_n \cdot s_1\cdots s_i)^{\omega }\) for all \(i\in \{1,\dots ,n\}\).

  2. (ii)

    There exist \(s\in S\) and a directed walk in \(\mathcal G_S\) starting in (1, s) and ending in (s, 1).

Proof

“(i)\(\implies \)(ii)": If there is such a sequence \(s_1, \dots , s_n\) of elements of S, then we may put \(s=s_1 \cdots s_n\) and consider the following directed walk

$$(1,s) \overset{s_1}{\longrightarrow } (s_1, s_2\cdots s_n) \overset{s_2}{\longrightarrow } (s_1s_2, s_3\cdots s_n) \overset{s_3}{\longrightarrow } \dots \overset{s_n}{\longrightarrow } (s,1) .$$

“(ii)\(\implies \)(i)": Let

$$(1,s)=(s_0,t_0) \overset{u_1}{\longrightarrow } (s_1, t_1) \overset{u_2}{\longrightarrow } (s_2, t_2) \overset{u_3}{\longrightarrow } \dots \overset{u_n}{\longrightarrow } (s_n,t_n)=(s,1)$$

be a directed walk in \(\mathcal G_S\). Then \(s_{i}=s_{i-1}\cdot u_{i}\) and \(t_{i-1}=u_{i}\cdot t_{i}\) for every \(i\in \{1,\dots , n\}\). By these equalities, we may deduce, for every \(i\in \{1, \dots , n\}\), that

$$s_{i}=s_{i-1}\cdot u_{i}= s_{i-2} \cdot u_{i-1} \cdot u_{i} = \dots = u_1 \cdot u_2 \cdots u_{i}$$

since \(s_0=1\). In particular, we have \(s=s_n= u_1\cdots u_n\). Analogically, we deduce

$$t_{i}=u_{i+1} \cdot t_{i+1} =u_{i+1} \cdot u_{i+2} \cdot t_{i+2} = \dots =u_{i+1} \cdot u_{i+2} \cdots u_n$$

for every \(i\in \{1,\dots , n\}\), as the product for \(i=n\) is empty, and we have \(t_n=1\). Moreover, we have the inequalities \(u_i \not \le u_i \cdot (t_i\cdot s_i)^\omega \) for all \(i\in \{1,\dots ,n\}\). Since the last inequality may be rewritten as \(u_i \not \le u_i \cdot (u_{i+1} \cdots u_n \cdot u_1\cdots u_i)^{\omega }\), we deduce that \(u_1,u_2, \dots , u_n\) is a sequence which existence is requested in condition (i). \(\square \)

Remark 4.15

Notice that s in condition (ii) in Lemma 4.14 cannot be equal to 1, since there is no edge starting in (1, 1). Indeed, assume that \((1,1) \overset{u}{\longrightarrow } (s,t)\), where \(u\in S\) is such that \(u\not \le u \cdot (t\cdot s)^\omega \). Then \(s=u\) and \(u\cdot t=1\). This gives

$$u\cdot (t\cdot s)^{\omega }=u\cdot (t\cdot u)^{\omega }=(u\cdot t)^{\omega }\cdot u=1\cdot u=u,$$

which contradicts the inequality \(u\not \le u \cdot (t\cdot s)^\omega \).

Since the graph \(\mathcal G_S\) has at most \((\vert S\vert +1)^2\) vertices, we deduce the aforementioned result.

Theorem 4.16

Let S be a finite ordered semigroup. It is decidable in polynomial time whether S is a congenial semigroup.

Proof

For a given ordered semigroup we may construct \(\mathcal G_S\) in polynomial time, and we may test the existence of a directed walk by condition (ii) in Lemma 4.14 in polynomial time as well. Of course, when we look for such a directed walk, we may assume that all vertices in that walk are pairwise distinct. By Remark 4.15, the walk we are interested in cannot start and end in the same vertex. \(\square \)

Taking into account the size of the graph \(\mathcal G_S\), we may reformulate Theorem 4.3 in the following way. Nevertheless, this statement does not yield any advantage for effective testing of congeniality of a given ordered semigroup.

Corollary 4.17

Let S be a finite ordered semigroup. Then S is congenial if and only if for every \(n\le (\vert S\vert +1)^2\) and \(s_1, \dots , s_n\in S\), there exists \(i\in \{1,\dots ,n\}\) such that \(s_i \le s_i \cdot (s_{i+1}\cdots s_n \cdot s_1\cdots s_i)^{\omega }\).

5 Conclusion

We answered a basic question when a homomorphism \(\sigma :A^+ \rightarrow S\) onto a finite ordered semigroup S generates a well quasi-order \(\le _{\sigma }\) on \(A^*\). Namely, we proved that the question does not depend on \(\sigma \), but it is a property of the given ordered semigroup. Then we obtained a purely algebraic characterization of the property. Unfortunately, this characterization lacks a transparent structural description, unlike the case of the unordered semigroups presented in [11]. However, we explained in Section 4.2, that such a structural property cannot be expected. In addition, we gave a polynomial algorithm for testing this rather complex technical property in Section 4.5.

We conclude with a brief discussion of the applications of our results. The basic motivation to study a wqo \(\le _{\sigma }\) in [11] was to establish regularity of maximal solutions of very general language equations and inequalities (see [12] or [15]). Our results extend the class of languages for which the mentioned theory is applicable, namely to the class of languages with congenial ordered syntactic monoids.

On the other hand, our results do not impact on the theory developed in [2]. The basic problem with a potential application lies in the necessary condition for construction of a finite order semigroup representing a given 0S scheme. We already mentioned that the necessary condition is the property of regularity of languages generated by each letter using the rules of 0S scheme. Since the regularity of the language generated by a context free grammar is an undecidable problem, no straightforward application is possible.

Although the main questions concerning wqos generated by homomorphisms to a finite ordered semigroup are answered in this paper, we may mention at least two basic directions of future research. The first direction is suggested in Section 4.4. It would be interesting to have some other sufficient conditions which ensure the congeniality of semigroups. For example, one may try to obtain more transparent characterization of congeniality under some additional assumption on the semigroup S.

The second research direction is more sophisticated. At the end of Subsection 2.3, we mention that the languages which are upward closed with respect to \(\le _{\sigma }\) form a quotienting algebra of languages. Following the theory in [6], this algebra induces a quasi-order (denoted \(\preceq _\sigma \)) on the free profinite semigroup \(\widehat{A^*}\). The semigroup \(\widehat{A^*}\) is an extension of the free semigroup \(A^*\) and plays a central role in finite semigroup theory, see, e.g., [1] by Almeida. Notice that the semigroup \(\widehat{A^*}\) can be constructed in many natural ways: as the set of all implicit operations, as the colimit of finite semigroups, as the completion of \(A^*\) from topological point of view, or as the dual Stone space of Boolean algebra of all regular languages over A via Stone duality.

Therefore, for a given homomorphism \(\sigma : A^+ \rightarrow S\) onto an ordered semigroup S, we consider the described quasi-order \(\preceq _\sigma \) on \(\widehat{A^*}\), the extension of \(\le _{\sigma }\) on \(A^*\), and we propose to identify homomorphisms \(\sigma \) such that \(\preceq _\sigma \) is a wqo. Since a restriction of a well quasi-order on a set X always forms a well quasi-order on an arbitrary subset of X, we deal only with the case when \(\le _{\sigma }\) is a wqo on \(A^*\). We mention, as it is related to this research program, that the extension of Higman’s embedding relation \(\unlhd \) to \(\widehat{A^*}\) is a well quasi-order (see [1, Theorem 8.2.11]). Hence, one may guess that the relation \(\preceq _\sigma \) is a wqo whenever S is congenial. The main question is then to decide whether there is a homomorphism \(\sigma \) onto a congenial semigroup such that \(\preceq _\sigma \) is not a well quasi-order on \(\widehat{A^*}\). If there is such a homomorphism, then we may ask, for example, whether the property is independent of \(\sigma \) as in the case of the relation \(\le _{\sigma }\) on \(A^*\) proved in our paper.