binary (generalized) post correspondence problem is in p

46
Vesa Halava | ˇ Stˇ ep´ an Holub Binary (Generalized) Post Correspondence Problem is in P TUCS Technical Report No 785, September 2006

Upload: others

Post on 27-Mar-2022

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Binary (Generalized) Post Correspondence Problem is in P

Vesa Halava | Stepan Holub

Binary (Generalized) Post CorrespondenceProblem is in P

TUCS Technical ReportNo 785, September 2006

Page 2: Binary (Generalized) Post Correspondence Problem is in P
Page 3: Binary (Generalized) Post Correspondence Problem is in P

Binary (Generalized) Post CorrespondenceProblem is in P

Vesa HalavaDepartment of Mathematics and

TUCS - Turku Centre for Computer Science

University of Turku FIN-20014 Turku, Finland.

Supported by the Academy of Finland under grant 208414.

[email protected]

Stepan HolubDepartment of Algebra

Faculty of Mathematics and Physics

Charles University in Prague

Sokolovska 83

186 75 Praha 8

Czech Republic

[email protected]

TUCS Technical Report

No 785, September 2006

Page 4: Binary (Generalized) Post Correspondence Problem is in P

Abstract

We show that the binary (generalized) Post Correspondence Problem is decid-able in polynomial time. We also give a characterization of morphisms withstable suffix complexity.

Keywords: binary Post Correspondence Problem; decidability; polynomialtime complexity; morphism; suffix complexity

TUCS Laboratory

Discrete Mathematics for Information Technology Laboratory

Page 5: Binary (Generalized) Post Correspondence Problem is in P

1 Introduction

The Post Correspondence Problem, PCP for short, is one of the most importantundecidable problems in the theory of computation. The PCP has been veryuseful in proving undecidability results in automata and formal language theory,in matrix theory etc.

The PCP was proved undecidable by E. Post in 1946, see [13], in the followingform: given two lists of words {u1, . . . , un} and {v1, . . . , vn}, the task is todetermine whether or not there exists a nonempty sequence i1, . . . ik, whereij ∈ {1, . . . , n} for 1 ≤ j ≤ k, such that

ui1ui2 · · ·uik= vi1vi2 · · · vik

.

Such a sequence i1, . . . , ik is called a solution of the problem instance.

In the theory of computability it is interesting to study the borderline be-tween the decidability and undecidability by relaxing and/or tightening require-ments of a problem. It is known, taking PCP as an example, that if the lists ofwords have cardinality at most two, i.e. n ≤ 2, then the problem is decidable(see [3] and [5]). On the other hand, the PCP is known to be undecidable forn ≥ 7 (see [11]). It is a difficult open question where exactly in the interval2,. . . ,7 the border between decidability and undecidability lies.

In this paper we shall consider the binary version of the PCP, i.e. the casen = 2. As already mentioned, in this case the PCP is known to be decidable.Historically, we like to mention that the decidability of the binary PCP wasstated as an exercise – without an asterisk to indicate that the problem isdifficult – in Hopcroft and Ullman [10] as well as in Harrison [7]. In both ofthese books it was expected that the problem has a positive answer, that is,that the binary PCP is decidable. In [10] this was stated explicitly and in [7]more implicitly. To confuse the issue more, Brainerd and Landweber [1] had thesame exercise, mentioned it to be open, but expected an undecidability result.

The binary PCP was originally proved decidable by Ehrenfeucht, Karhum-ki and Rozenberg in [3] and by Pavlenko in [12]. A somewhat simpler proofwas found by Halava, Harju and Hirvensalo in [5]. In their proof the basicidea is the same as in [3], that is, one uses the transformation of an (non-periodic) instance into an instance of marked binary generalized PCP. In [3],after the tranformation, the decision is done by case analysis, whereas in [5] thedecidability of the marked generalized PCP for any size of the alphabet is used(see [6]). Both of these approaches yield an algorithm, where the worst casecomplexity seems to be exponential.

In this paper we shall give a new algorithm, which solves the binary PCPin polynomial time. Our proof is based on [5], while the complexity is pusheddown with help of some ideas from the paper of Holub [8] on binary equality sets.In our algorithm we did not make any effort to achieve the lowest complexitypossible. We only wanted to exclude the exponential threat. Also we tried tomake the proof as simple as possible often choosing solutions, which are easier toexplain while not very efficient. This is in particular true of the length estimates,which are surely too generous.

After some preliminary definitions and lemmas we present in Section 2 basicproperties of nonperiodic binary PCP. The discussion of periodic cases, whichare relatively easy to deal with, is postponed to Section 6.

Section 3 describes the idea of reducing the instance to its successor, andshows that the reduction does not endanger the polynomial complexity.

1

Page 6: Binary (Generalized) Post Correspondence Problem is in P

Some special cases have to be treated separately, which is done in rathertechnical Section 4. In that section we among other things provide a character-ization of morphisms with stable suffix complexity.

Section 5 yields a detailed description of the decision algorithm for nonperi-odic instances which exploits the previous theory.

2 Preliminaries

2.1 Words

We shall first introduce some basic definitions and notations. As a generalreference to combinatorics on words, we give Choffrut and Karhumaki [2].

Let A be a finite set of symbols, called an alphabet. A word over A is a finitesequence of symbols in A. We denote by A∗ the set of all words over A. Notethat the empty word, denoted by ε, is in A∗.

The length of the word u is denoted by |u|, and |u|a is the number of occur-rences of a letter a in u.

A word u ∈ A∗ is said to be a prefix of a word v ∈ A∗ if there exists w ∈ A∗

such that v = uw. This will be denoted by u ≤ v.The prefix relation ≤ is the natural ordering on words. Therefore if we speak

about a minimal word, we refer to the prefix ordering.A prefix of length k of v is denoted by prefk(v) whenever k ≤ |v|. Similarly

we define suffk(v).We say that u is a prefix of a set L iff u ≤ v for at least one v ∈ L.Also, if u 6= ε and w 6= ε in v = uw, then u is a proper prefix of v, and this

is denoted by u < v. We say that u and v are comparable if u ≤ v or v ≤ u,and denote it by u ./ v. The maximal common prefix of two words, u and v, isdenoted by u ∧ v.

A word u ∈ A∗ is said to be a suffix of a word v ∈ A∗ if there exists w ∈ A∗

such that v = wu. This will be denoted by u 4 v and, if u 6= ε and w 6= ε, theword u is called a proper suffix of v, denoted by u ≺ v. If either u 4 v or v 4 u,then we say that u and v are suffix comparable

To simplify our exposition we shall sometimes consider the extension of thefree monoid A∗ to the free group generated by A. For example, if u = vw thenwe can write v = uw−1, w = v−1u but also v−1 = wu−1. By u ∈ (A−1)∗ weshall denote the fact that u is an element of the free monoid generated by lettersa−1 and b−1.

If v = wuz, then u is called a factor of v. Note that also prefixes and suffixesare factors.

We shall also work with the monoid A∗ × A∗ with the product defined by(u, v)(u′, v′) = (uu′, vv′). Although this monoid is not free, the notions of prefixand suffix are well defined also here. For example the prefix ordering on pairsof words is given by (u, v) ≤ (u′, v′) if and only if u ≤ u′ and v ≤ v′.

Let u = `1 . . . `d, with d = |u|. Then the mirror image of the word u, denotedby u , is obtained by inverting the order of the letters, i.e.

u = `d`d−1 . . . `1 .

Let A and B be two alphabets. A mapping h from A∗ into B∗, that is,h : A∗ → B∗, is called a morphism if, for all u, v ∈ A∗,

h(uv) = h(u)h(v).

2

Page 7: Binary (Generalized) Post Correspondence Problem is in P

It follows that a morphism becomes defined by h[A], i.e. by the images of theletters in the domain alphabet A.

If h(a) 6= ε for all a ∈ A, then h is said to be nonerasing.

A morphism h : A∗ → B∗ is called periodic if there exists a word w ∈ B∗

such that h(u) ∈ w∗ for all u ∈ A∗.

A morphism h is marked if it is nonerasing and the images of the lettersbegin with different letters, i.e., if h(a) ∧ h(b) = ε for all a, b ∈ A with a 6= b.

The equality set of the morphisms g, h is

E(g, h) = {h(w) = g(w) | w ∈ A∗} .

In [8] and [9] it was proved that in the binary case the equality set of two non-periodic morphisms is either generated by just one word, or it is of the form{bai, aib}.

Let g be an arbitrary morphism. The mirror image of g is the morphismdenoted by g and defined by

g (x) = g(x) ,

for each x ∈ A. Note that in general g (u) does not equal to g( u ) nor to g(u) .Instead

g ( u ) = g(u) .

We recall some basic facts from combinatorics on words describing commut-ing words and conjugate words.

Lemma 1. If the words u and v satisfy a nontrivial relation, even an infiniteone, then they are powers of the same word.

Lemma 2. Let u and v be two words. Then the following conditions are equiv-alent:

1. There is a word z such that zu = vz.

2. There are words x and y such that u = xy and v = yx.

3. There is a word t1, and a nonempty word t2 such that t1t2 is primitive,and u = (t1t2)

j , v = (t2t1)j , for some j ∈ N.

Moreover, if t1, t2, z are like above, then z = (t2t1)it2 for some i ≥ 0.

Words u and v satisfying conditions of the above lemma are called conjugate.

Lemma 3. If the words uω and vω have a common factor of length |uv|, thentheir primitive roots are conjugate.

A word u ∈ A∗ such that

w ≤ h(u) and h(u′) < w, for all u′ < u,

is called a minimal h-cover of w.

3

Page 8: Binary (Generalized) Post Correspondence Problem is in P

2.2 Generalized PCP

The PCP can be redefined using morphisms. Let h, g : A∗ → B∗ be morphisms,where A = {a1, . . . , an} is an alphabet of n letters, and

g(ai) = ui and h(ai) = vi.

The original form of the PCP now transforms to the question, whether or notthere exists a nonempty word w ∈ A+ such that

g(w) = h(w). (1)

The pair (g, h) is called an instance of the PCP, and a word w satisfying (1)is a solution of the instance. Note that the set of solutions of the instanceI = (g, h) is the equality set E(I) = E(g, h), when including the empty word.Therefore, the PCP becomes defined also by asking to determine whether ornot E(I) = {ε}.

Manipulation with PCP leads naturally to a modification of the problemcalled generalized Post Correspondence Problem, GPCP for short. It consists oftwo morphisms g, h : A∗ → B∗ and four words p1, p2, s1, s2 ∈ B∗. The problemis to determine whether or not there exists a nonempty word w ∈ A+ such that

p1g(w)s1 = p2h(w)s2.

The word w is again called a solution of the instance I = (p1, p2, g, h, s1, s2) ofthe GPCP. Note that neither in the generalized PCP we admit the empty wordas a solution, although it is not always true that ε satisfies the requirement. Itis nevertheless easy to check whether p1s1 = p2s2.

The GPCP is undecidable in general, since instances of the PCP are alsoinstances of the GPCP.

Finally, we mention that in the marked PCP and in the marked GPCP, weassume that the morphisms are marked.

Denote by I = ( s1 , s2 , g , h , p1 , p2 ) the reverse instance of I . Next lemmais obvious.

Lemma 4. A word w is a solution of I if and only if w is a solution of I .

3 Binary case

In the binary GPCP the domain alphabet A is of size 2. We shall assumethat A = {a, b}. It will be convenient to suppose that the image alphabet isalso A. This assumption does not harm generality, since we can encode anyalphabet injectively to the binary one, with no influence on polynomiality ofthe algorithm.

Instances of GPCP split in two rather different types. The first case, the lesscomplicated one, is when at least one of the morphisms is periodic. We shallspeak about periodic instances. Otherwise, the instance is nonperiodic.

In this section we introduce essential properties of nonperiodic instances,namely the existence of the critical overflow and the theory of blocks.

Let h : {a, b}∗ → {a, b}∗ be a morphism that is not periodic. In particular,h is nonerasing. By Lemma 1, if uv = vu then the words u and v are powers ofthe same word. Since h is nonperiodic, we have h(ab) 6= h(ba), and we denote

zh = h(ab) ∧ h(ba).

Nonperiodic binary morphisms satisfy the following important lemma.

4

Page 9: Binary (Generalized) Post Correspondence Problem is in P

Lemma 5. Let h be a nonperiodic morphism over A = {a, b}, and let au, bv bewords such that |h(au)| > |zh|, and |h(bv)| > |zh|. Then

h(au) ∧ h(bv) = zh.

The proof can be found in [3]. The lemma says that the (zh + 1)-th letter ofany enough long word h(w) determines the first letter of w.

Let (g, h) be a pair of morphisms. If zg and zh are suffix comparable thenthe word

c(g, h) = zhz−1g

is called the critical overflow of the morphisms g, h. Note that c(g, h) ∈ (A−1)∗

if zh ≺ zg . By symmetry, however, we can suppose c(g, h) ∈ A∗.For a morphism h we define its marked version hm by

hm(a) = z−1h h(a)zh, hm(b) = z−1

h h(b)zh.

Lemma 5 implies that hm(a) is well defined, and

hm(w) = z−1h h(w)zh

for all w ∈ A∗.Note that if both morphisms are marked, the critical overflow always exists,

namely it is the empty word.Lemma 5 has the following consequence.

Lemma 6. Let h be a nonperiodic morphism over A = {a, b}, and p, q, v ∈{a, b}∗. If rv is a the prefix of h(v) of length |zh|+ 1 and

qrv ≤ p

then pref1(v) is given uniquely, and it can be determined in polynomial time.

Proof. Denote

x(h, a) = pref1(z−1h h(ab)), x(h, b) = pref1(z

−1h h(ba)).

By Lemma 5,

x(h, a) 6= x(h, b). (2)

Lemma 5 implies, moreover, that if qrv ≤ p then the first letter of v is givenuniquely by

x(h, pref1(v)) = pref1((qzh)−1p) .

Polynomiality is obvious. The proof is ilustrated by the following picture:

p

q zh x

rv

Previous lemma implies that given two comparable words p and q, the pos-sible continuation by means of morphisms g and h is unique, until the criticaloverflow occurs. This justifies the word “critical”. The fact is formulated in thefollowing

5

Page 10: Binary (Generalized) Post Correspondence Problem is in P

Lemma 7. Let morphisms g and h, and two words p and q be given. Thenthere is at most one minimal pair of words (u, v) ∈ A∗ ×A∗ such that

pg(u)zg = qh(v)zh.

If such words exist, the morphisms g and h have the critical overflow, and

pg(u) = qh(v)c(g, h).

Proof. Suppose that pg(ui) = qh(vi)c(g, h) holds for two pairs of words (ui, vi),i = 1, 2, such that v1 and v2 are not comparable. Put t = v1 ∧ v2, v′1 = t−1v1,and v′2 = t−1v2. By assumption, both v′

1 and v′2 are nonempty.For an enough long word w, let ri, i = 1, 2, be the prefix of h(v′

iw) of length|zh|+ 1. We have

qh(t)ri < pg(u)zg

for both i = 1, 2 and Lemma 6 implies pref1(v′1) = pref1(v

′2), a contradiction.

Therefore v1 and v2 are comparable.Similarly we show u1 ./ u2. The length argument yields that either (u1, v1) ≤

(u2, v2) or vice versa. Therefore there is at most one minimal pair satisfying theequality.

The rest follows from the definition of the critical overflow.

The difficulties come from the cases, in which the critical overflow makesitself felt, since after the critical overflow two different continuations are possible.Basic description of this situation is obtained in the following lemma. It is adirect consequence of Lemma 7.

Lemma 8. There are at most two minimal pairs of words (e, f) and (e′, f ′)such that ef and e′f ′ are nonempty and

c(g, h)g(e) = h(f)c(g, h) c(g, h)g(e′) = h(f ′)c(g, h) (3)

holds. Moreover pref1(e) 6= pref1(e′) and pref1(f) 6= pref1(f

′).

Proof. It is obvious from a length argument that all words e, f , e′, f ′ have tobe nonempty.

Put p = c(g, h)g(a), q = ε. Lemma 7 implies that there is at most oneminimal pair of words (e, f) satisfying assumptions with pref1(e) = a. Similarly,there is at most one pair of words (e′, f ′) such that pref1(e

′) = b.Analogous considerations for p = c(g, h), q = h(c), c ∈ A, implies that

pref1(f) 6= pref1(f′).

The pairs (e, f) and (e′, f ′) described in Lemma 8 shall be called letter blocksof morphisms g and h. More precisely we say that (e, f) is the d-block, whered = pref1(e).

Let us formulate one more consequence of Lemma 6.

Lemma 9. Let (e, f) be a letter block of morphisms g and h. Let w1 = r−1erbe a conjugate of e and w2 = q−1fq a conjugate of f . Then for each wordo ∈ {a, b}∗ the pairs (u, v) and (u′, v′), such that, (u, v), (u′, v′) < (w1, w2), and

c(g, h)g(ru) = h(qv) o,

c(g, h)g(ru′) o = h(qv′),

are unique, if they exist.

6

Page 11: Binary (Generalized) Post Correspondence Problem is in P

Proof. We shall prove only uniqueness of the pair (u, v), the other part is similar.Assume that there are two distinct pairs (u1, v1) and (u2, v2) such that, for

i = 1, 2,

c(g, h)g(rui) = h(qvi) o. (4)

From (ui, vi) < (w1, w2), i = 1, 2 we deduce that we can suppose u1 ≤ u2, andv1 ./ v2. A length argument then implies that (u1, v1) < (u2, v2).

By Lemma 7, there is a unique minimal pair (t, s) such that

c(g, h)g(ruit) = h(qvis)c(g, h). (5)

Note that the pair (t, s) is the same for both i = 1, 2, since we can choosethe pair (ε, o) as the starting words in Lemma 7.

Lemma 8 implies that for both i = 1, 2 the pair (ruit, qvis) is a product ofblocks (e, f) and (e′, f ′). From the minimality of (t, s) we deduce that neither(e, f) nor (e′, f ′) is a suffix of (t, s).

Therefore, since rui ≤ rw1 ≤ eω and qvi ≤ qw2 ≤ fω, necessarily ruit = eji

and qvis = f ji for some j1, j2 ∈ N. Assumption (u1, v1) < (u2, v2) impliesj1 < j2, which yields that

|u2| − |u1| = (j2 − j1)|e| and |v2| − |v1| = (j2 − j1)|f |.

This is a contradiction with (ui, vi) < (w1, w2) and the fact that |w1| = |e| and|w2| = |f |.

If both letter blocks exist, we say that morphisms g and h have a successor.It is then natural to define the successor morphisms g1, h1 : A∗ → A∗ of g andh by

g1(a) = e, g1(b) = e′,

h1(a) = f, h1(b) = f ′.

By Lemma 8, successor morphisms are given uniquely up to renaming ofletters. They have some important properties.

Lemma 10. Let g1 and h1 be successor morphisms of morphisms g and h.Then

(i) the morphisms g1 and h1 are marked;

(ii) the morphisms g and h also have successors g′1 and h′

1 such that (up torenaming of letters)

g′1(a) = rer−1 , g′1(b) = re′r−1 ,

h′1(a) = qfq−1 , h′

1(b) = qf ′q−1 ,

where r (q resp.) is the maximal common suffix of ee′ and e′e (ff ′ andf ′f resp.).

(iii)

g(r) = c( g , h ) h(q)c(g, h). (6)

7

Page 12: Binary (Generalized) Post Correspondence Problem is in P

Proof. (i) Directly from Lemma 8.(ii) By (i) , the words e and e′ do not commute. Therefore r is a proper suffix

of both ee′ and e′e. Similarly for q. It should be also clear that the morphismsg′1 and h′

1 are well defined. In fact, r = z g1 , q = zh1and g′1(x) = z−1

g1g1 (x)z g1

and h′1(x) = z−1

h1h1 (x)zh1

for x ∈ {a, b}. Therefore, g′1 is the marked version

of g1 and h′1 the marked version of h1 .

We have to show that g′1 and h′

1 are the successors of g and h . Assumethat |zh | ≥ |z g |. We prove that

c( g , h ) g ( r−1 e r ) = h ( q−1 f q )c( g , h ), (7)

c( g , h ) g ( r−1 e′ r ) = h ( q−1 f ′ q )c( g , h ), (8)

and that these words are minimal.Consider morphisms g and h . Since g ( e ) c(g, h) = c(g, h) h ( f ), using

Lemma 7 (for p = ε, q = c(g, h) ), and the definition of r and q, necessarily

g ( r ) = c(g, h) h ( q )c( g , h ). (9)

Indeed, for the words g ( r ) and c(g, h) h ( q ), there are two continuations, andthis implies the existence of the critical factor.

Using (9) we obtain

g ( e r ) = g ( e ) c(g, h) h ( q )c( g , h )

= c(g, h) h ( f q )c( g , h ),

and

g ( r−1 e r ) = c( g , h )−1 h ( q−1 f q )c( g , h ).

Therefore

c( g , h )g( r−1 e r ) = h( q−1 f q )c( g , h ).

The minimality of the pair ( r−1 e r , q−1 f q ) follows from Lemma 9, by con-

sidering the morphisms g and h, the block (e, f) and o = c( g , h ) .The proof for (8) is similar.For the case |z g | > |zh | we give only the sketch. In this case (7) is replaced

byg ( r−1 e r )c( g , h ) = c( g , h ) h ( q−1 f q ) (10)

and (9) is replaced by

g ( r )c( g , h ) = c(g, h) h ( q ). (11)

Nowc(g, h) h ( f q ) = g ( e r )c( g , h )

and using (11) we get,

c( g , h )−1 g ( r−1 e r )c( g , h ) = h ( q−1 f q ),

which yields (10).(iii) Follows directly from (9) and (11) after taking the inverses of both

sides.

8

Page 13: Binary (Generalized) Post Correspondence Problem is in P

We also have the following necessary condition for the existence of successormorphisms.

Lemma 11. Let g and h have a successor morphisms. Let s (t resp.) be themaximal common suffix of g(ab) and g(ba) (h(ab) and h(ba) resp.) Then s andt are comparable.

Proof. By Lemma 10, also morphisms g and h have successor morphisms.Therefore they have a critical overflow, which implies that t and s are suffixcomparable. The claim follows.

We say that the instance I = (p1, p2, g, h, s1, s2) of the binary GPCP has asuccessor if morphisms g and h have a successor and, moreover, there are wordsu1, u2, v1 and v2 ∈ A∗ such that u1 ./ u2, v1 ./ v2 , and

p1g(u1) = p2h(u2)c(g, h),

c(g, h)g(v1)s1 = h(v2)s2.

The successor instance of I is then defined by I ′ = (u1, u2, g1, h1, v1, v2).The pair (u1, u2) is called the begin block, if it is minimal, and (v1, v2) is

called the end block. The begin block is unique, by Lemma 7.There can be more than one successor instance, since the end block is not

given uniquely.Let I = (p1, p2, g, h, s1, s2) be an instance of the binary GPCP, and w its

solution. We say that w is decomposable if there is a sequence

(u0, v0), . . . , (uk, vk) (12)

such that

w = u0 · · ·uk = v0 · · · vk

(u0, v0) is the begin block of I , (ui, vi), i = 1, . . . , k − 1 are blocks of g and h,and (uk, vk) is an end block of I .

The sequence (12) is called block decomposition of w. Solutions which containno letter block are called simple. Therefore simple solutions either are notdecomposable or they consist exactly of the begin block and an end block.

Because blocks are minimal solutions to c(g, h)g(ui) = h(vi)c(g, h), it is easyto see that each decomposable solution has unique block decomposition.

4 Reduction tree

The following lemma represents a crucial tool in the study of decidability of(G)PCP: if an instance has no simple solution, it can be reduced to its successors.

Lemma 12. Let I = (p1, p2, g, h, s1, s2) be an instance of the binary GPCP,and assume that it has a successor. Then I has a solution which is not simple,if and only if one of its successors has a solution.

Moreover, each non-simple solution w of I can be written as

w = p′1g1(w′)s′1 = p′2h1(w

′)s′2,

where w′ is a solution of an instance I1 = (p′1, p′2, g1, h1, s

′1, s

′2), which is one of

the successor instances of I.

9

Page 14: Binary (Generalized) Post Correspondence Problem is in P

Proof. Follows directly from the definition of simple solution, and of the succes-sor instance.

The overall strategy of the decision algorithm can be now divided in threesteps:

1. Check whether I has a simple solution.

2. Construct all successor instances I1, . . . , Im.

3. Call the algorithm recursively for all instances Ij , j = 1, . . . , m.

We obtain an oriented tree of instances where the edge goes always from aninstance to its successor, and the tree is rooted in the original instance. We callthis tree the reduction tree of I .

It is obvious that the main problem of this outline is the size and the formof the tree of successors. The main part of the decidability proof in [5] consistsin showing that it is sufficient to study only finite subtree of the successors treein order to decide whether or not an instance has a solution. This is due tothe facts that the successor morphisms eventually appear cyclically, and that aminimal solutions can be detected among the end blocks of bounded length.

Accordingly, in this paper we have to prove that

• the tree is (almost) linear, i.e. it does not branch (Section 4.1);

• the tree is not too deep (Section 4.2 and Section 5.2); and

• the size of instances in the tree is limited (Section 4.3).

4.1 End blocks theory

The crucial problem of GPCP is the fact that an instance can have more thanone successor. This implies that the reduction tree can be very wide, and thenumber of the instances threatens to grow exponentially with the recursive callof the algorithm. The source of problems is the endblock, which is not givenuniquely, while blocks and the begin block are unique.

The present section is therefore the core of our proof. We describe thestructure of endblocks, and show that all successors of an instance, if there aremore of them, can be replaced by just one “common” successor.

Recall that end block of an instance I is a suffix-minimal pair of words (u, v),which satisfies

c(g, h)g(u)s1 = h(v)s2,

and, moreover, (u, v) are suffix-comparable. For the successor instance only thevalue of vu−1 is relevant, which is either an element of A∗ or (A−1)∗ according towhether u is a suffix of v or vice versa. In the first case (s′1, s

′2) of the successor

is equal to (ε, vu−1) in the latter (s′1, s′2) = ((vu−1)−1, ε) = (uv−1, ε).

Therefore we denote

e(u, v) =

{

(ε, vu−1) if vu−1 ∈ A∗

(uv−1, ε) if vu−1 ∈ (A−1)+

Note thate(uz, vz) = e(uz′, vz′)

for any z, z′, which is a natural way how to say, that the two endblocks (uz, vz)and (uz′, vz′) are essentially the same.

10

Page 15: Binary (Generalized) Post Correspondence Problem is in P

Lemma 13. Let (e, f), (e′, f ′) be letter blocks, and

r = e ∧ e′ q = f ∧ f ′ .

Then either r is a proper suffix of both e and e′, or q is a proper suffix of bothf and f ′.

Proof. Suppose the opposite. Then e and e′ are suffix comparable, as well asf and f ′. Since g(e) = h(f) and g(e′) = h(f ′), a length argument implies thateither (e, f) is a suffix of (e′, f ′), or vice versa. This contradicts minimality ofletter blocks.

Theorem 14. Let I be an instance of binary GPCP, which has a successor.Then I has at most max{2|ee′|, 2|ff ′|} end blocks.

If (u, v) is an end block of I, then

ru = g′1(z) b1, qv = h′1(z) b2 (13)

where ( b1 , b2 ) is the begin block of I , and z is a minimal word, such that

r ≤ g′1(z) b1, q ≤ h′1(z) b2. (14)

Proof. Let (u, v) be an endblock. Denote by r (q resp.) the maximal commonsuffix of ee′ and e′e (ff ′ and f ′f resp.) as in Lemma 10 and Lemma 13. Thenby Lemma 10(iii),

g(ru)s1 = c( g , h ) h(qv)s2

and therefore

ru = g′1(z) b1, qv = h′1(z) b2,

for some z. This can be also written as

u = r−1 g′1(z) b1, v = q−1 h′1(z) b2.

Suffix-minimality of the end block implies that z is a minimal word satisfying(14).

If r is a suffix of both e and e′, and q is a suffix of both f and f ′, then z iseither empty or z ∈ {a, b}. In a general case e and e′, or f and f ′, may be suffixcomparable. Let k be the smallest integer such that r is a suffix of ek, and q offk. Similarly, let m be the smallest integer such that r is a suffix of (e′)m, andq of (f ′)m. Note that z satisfies (14) as soon as it contains both letters a and b.It is now not difficult to see that

z ∈ {aib | i = 0, . . . k − 1} ∪ {bja | j = 0, . . .m− 1} ∪ {ak, bm}. (15)

Therefore the number of endblocks is limited by m + k ≤ max{2q, 2r}. Thiscompletes the proof.

Theorem 15. Let I have at least two endblocks. Then

(i) There is a word τ such that (τ, τ) is a suffix of each endblock, and

g(τ)s1 = c( g , h ) h(τ)s2. (16)

11

Page 16: Binary (Generalized) Post Correspondence Problem is in P

Let, moreover, q ./ r and define the instance

J = ((p′1, p′2), g1, h1, e(ε, q−1r)),

where (p′1, p′2) is the begin block of I. Then

(ii) a) If I has a solution w satisfying

|w| > |p′1τ |, |w| > |p′2τ | (17)

then J has a solution.

b) If J has a solution then I has a solution.

Proof. (i) Let (ui, vi), i = 1, . . . , k be the list of all end blocks. By Theorem14,

rui = g′1(zi) b1, qvi = h′1(zi) b2

for some words zi. Let z be the longest common prefix of all zi, i =1, . . . , k, and suppose, without loss of generality, that z = z1∧z2. If z1 andz2 were comparable then also (u1, v1) and (u2, v2) would be comparable,which would contradict the minimality of the end block. Therefore, z1 =zzd, z2 = zzd′ , with d = pref1(zd), d′ = pref1(zd′), d 6= d′.

Recall that the end block has to be suffix comparable, i.e. u1 ./ v1 ,u2 ./ v2 . Since g′1, h′

1 are marked, one deduces

τ = b1 g′1(z) = b2 h′1(z). (18)

Equality (16) follows from the fact, that g′1 and h′

1 are successors of g andh .

(ii) a) By assuptions, there are words w1, w2 ∈ A+, such that

w = p′1w1τ = p′2w2τ, (19)

and

c(g, h)g(w1) c( g , h ) = h(w2).

Equality (6) implies

c(g, h)g(w1r) = h(w2q)c(g, h).

Thus there is a word w′, which satisfies

g1(w′) = w1r, h1(w

′) = w2q.

From (19) we have

p′1g1(w′)r−1τ = p′2h1(w

′)q−1τ,

and w′ is a solution of J .

b) Let w′ ∈ A+ be a solution of J , i.e.

p′1g1(w′) = p′2h1(w

′)q−1r.

12

Page 17: Binary (Generalized) Post Correspondence Problem is in P

By Lemma 13, the word

p′1g1(w′)r−1 = p′2h1(w

′)q−1

is in A+. From

p1g(p′1g1(w′)) = p2h(p′2h1(w

′))c(g, h),

and equalities (16) and (6) we deduce

p1g(p′1g1(w′)r−1τ)s1 = p2h(p′2h1(w

′)q−1τ)s2.

Hencew = p′1g1(w

′)r−1τ = p′2h1(w′)q−1τ

is a solution of I .

The following lemma shows that Theorem 15 applies as soon as g1, h1 havethemselves a successor.

Lemma 16. If g1 and h1 have successors g2, h2 then q ./ r.

Proof. Directly from Lemma 11.

Example 1. Consider an instance with marked morphisms

g(a) = a g(b) = b

h(a) = a h(b) = baa,

and (s1, s2) = (aaa, a).Then (e, f) = (a, a), (e′, f ′) = (baa, b), c(g, h) = ε, c( g , h ) = aa, r = aa

and q = ε. Therefore

g′1(a) = a g′1(b) = baa

h′1(a) = a h′

1(b) = b.

The begin block ( b 1, b 2) of I is empty, since (s1, s2) has already the criticaloverflow.

a a a

a

s1

c

s2

Therefore τ = ε. Consider the following three end blocks:

(u1, v1) = (ε, aa) = (r−1 g′1(aa) , q−1 h′1(aa) )

a a a

a a a

s1

s2h(a)h(a)

13

Page 18: Binary (Generalized) Post Correspondence Problem is in P

(u2, v2) = (ba, ba) = (r−1 g′1(ab) , q−1 h′1(ab) )

b a a a a

b a a a a

s1g(a)g(b)

s2h(a)h(b)

(u3, v3) = (b, b) = (r−1 g′1(b) , q−1 h′1(b) )

b a a a

b a a a

g(b) s1

s2h(b)

Note thate(u2, v2) = e(u3, v3) = (ε, ε).

4.2 The suffix complexity

To show that the reduction sequence is not too deep (or even infinite), it isessential to guarantee that each element of it is in some sense simpler thanits predecessor. A most suitable measure for this task is the so called suffixcomplexity, first introduced in [3].

The suffix complexity σ(g) of a morphism g is defined as the number ofdifferent non-empty words occurring as suffixes of an image of g. Formally,

σ(g) = Card{x | x 6= ε; x ∈ suff(g(a)) ∪ suff(g(b))}.

In this section we will suppose, for sake of simplicity, that the conside-red morphisms are marked. This assumption is justified by the fact that allsuccessor morphisms are marked, see Lemma 10(i), and the number of successorsof the first, possibly not marked, instance is limited. Moreover, slightly modifiedconsiderations hold even in the general case. The assumption allows to makeuse of the properties described in Lemma 10, which simplify the exposition.

Let therefore (g, h) be a pair of marked morphisms, and (g1, h1) be its suc-cessor. As usually, we denote the blocks by (e, f) and (e′, f ′), that is

g1(a) = e, g1(b) = e′,

h1(a) = f, h1(b) = f ′.

Let Og be the set of all suffixes of g(a) and g(b), which occur as overflowsin at least one block. More precisely, u ∈ Og if and only if u is a nonemptysuffix of g(a) or g(b), and there are (possibly empty) words e1, f1, such that(e1, f1) ≤ (e, f) or (e1, f1) ≤ (e′, f ′), and

g(e1) = h(f1)u.

Similarly we define Oh.

14

Page 19: Binary (Generalized) Post Correspondence Problem is in P

Corollary 7 implies that each u ∈ Og determines uniquely a word f2 ∈suff(f)∪ suff(f ′) such that ug(e2) = h(f2) for some e2. That defines a mapping

πg : Og → suff(f) ∪ suff(f ′).

It is not difficult to see that πg is surjective. For each non-empty word vfrom suff(f) ∪ suff(f ′) there is a corresponding overflow, which is in Og .

Symmetrically, we define a mapping

πh : Oh → suff(e) ∪ suff(e′).

The existence of the surjective mappings πg and πh immediatelly implies

|σ(h1)| ≤ |Og | ≤ |σ(g)|,

|σ(g1)| ≤ |Oh| ≤ |σ(h)|.(20)

Therefore

σ(g1) + σ(h1) ≤ σ(g) + σ(h). (21)

Moreover, the equality holds if and only if

1. each suffix of g(a) and g(b) is in Og, and each suffix of h(a) and h(b) is inOh, and

2. the mappings are injective.

Remark 17. Later we shall use only the first of the two above conditions. In-jectivity, however, is not guaranteed, as the following example shows:

g(a) = abb g(b) = b

h(a) = a h(b) = bb.

Here both overflows b and bb from suff(g) map to b ∈ suff(h1).

The fact that the sum of suffix complexities does not increase is used in [5]to show that successors has to cycle after finitely (at most exponentially) manysteps. In Section 5.2 we shall show that the suffix complexity (in most cases)decreases strictly (exceptions are treated separately). By (21) and (22), thismeans that the depth of the reduction tree is linear in the length of I .

4.3 The size of successors

Now we are going to show that the instances in the reduction tree are not toolarge. Again we suppose that the morphisms are marked.

By length of a morphism g we mean the integer

λ(g) = |g(a)|+ |g(b)|.

Consider the relation between λ(g) and σ(g). Surely

σ(g) ≥ max{|g(a)|, |g(b)|}.

Therefore

1

2λ(g) ≤ σ(g) ≤ λ(g). (22)

15

Page 20: Binary (Generalized) Post Correspondence Problem is in P

(In fact, the first inequality is strict, since otherwise g(a) = g(b).) The inequality(22) guarantees that the length of any pair of morphisms in the sequence isbounded by 2(σ(g) + σ(h)).

We have to find a polynomial bound also for begin blocks and end blocks.Let us define the begin complexity of I by

σp(I) =Card{x | x 6= ε; x ∈ suff(g(a)) ∪ suff(g(b)) ∪ suff(p1)}+

+Card{x | x 6= ε; x ∈ suff(h(a)) ∪ suff(h(b)) ∪ suff(p2)}.

The begin complexity measures the number of overflows that can occur in thebegin block.

Each overflow determines uniquely a suffix of the begin block as above, andwe can construct a mapping π to show that

σp(I1) ≤ σp(I).

This implies that the size of the begin block of any member of the reductiontree is bounded.

We now turn to the end block. Theorem 14 shows that the size of an endblockis bounded by the begin block of the mirror instance plus the size of the successormorphisms. We can therefore combine the bounds already obtained to see thatthe size of any endblock has a polynomial bound as well.

5 Special cases

In this section we deal with two particular types of instances, which have to betreated separately: instances, which are unbalanced, and instances which havestable suffix complexity.

5.1 Unbalanced instances

First we show how to calculate the bound for the length of minimal solutions,if the instance is unbalanced and has letter blocks.

Recall that by |w|l, with l ∈ {a, b}, we denote the number of occurrences ofletter l in w.

We say that morphisms g and h are balanced if

ρ(g(a)) ≤ ρ(h(a)), ρ(g(b)) ≥ ρ(h(b))

or

ρ(g(a)) ≥ ρ(h(a)), ρ(g(b)) ≤ ρ(h(b))

for each ρ(.) ∈ {|.|, |.|a, |.|b}. Otherwise we say that the instance is unbalanced.Consider an unbalanced instance. Without loss of generality we shall suppose

ρ(g(a)) < ρ(h(a)), ρ(g(b)) ≤ ρ(h(b)). (23)

Let w be a minimal solution of I . Then the equality

ρ(p1g(w)s1) = ρ(p2h(w)s2)

and the first inequality in (23) imply

|w|a <ρ(p1s1)− ρ(p2s2)

ρ(g(a))− ρ(h(a))(=: ma).

16

Page 21: Binary (Generalized) Post Correspondence Problem is in P

Therefore, we have a (polynomial) upper bound ma for the number of letters ain the solution.

For the upper bound for |w|b we have two cases according to the lengths ofg(b) and h(b). Suppose first that |h(b)| 6= |g(b)|. From

|p1g(w)s1| = |p2h(w)s2|

we deduce

|p1s1|+ |w|a · |g(a)|+ |w|b · |g(b)| = |p2s2|+ |w|a · |h(a)|+ |w|b · |h(b)|,

and

|w|b =|p1s1| − |p2s2|

|h(b)| − |g(b)|+ |w|a ·

|g(a)| − |h(a)|

|h(b)| − |g(b)|.

Therefore

|w|b ≤

|p1s1| − |p2s2|

|h(b)| − |g(b)|

+ ma ·

|g(a)| − |h(a)|

|h(b)| − |g(b)|

. (24)

For the second case, suppose |h(b)| = |g(b)|. We give a polynomial upper boundfor the number of consecutive bs in a minimal solution. Consider a solutionw = w1b

i+1w2. If

||p1g(w1bi)| − |p2h(w1b

i)|| ≤ |g(bi)|, (25)

then w1biw2 is a solution as well. That follows from the fact that, for all ` > i,

the overflow of the presolution w1b`, namely,

(p2h(w1b`))−1p1g(w1b

`) (or (p1g(w1b`))−1p2h(w1b

`)),

is equal to (p2h(w1bi))−1p1g(w1b

i) (or (p1g(w1bi))−1p2h(w1b

i), resp.), as theyhave the same length, and are suffixes of g(b)i+1 (or h(b)i+1, resp.).

Therefore, if w is minimal, from (25) we deduce

i <||p1| − |p2|+ |w1|a(|g(a)| − |h(a)|)|

|g(b)|

≤||p1| − |p2||+ ma||g(a)| − |h(a)||

|g(b)|(=: cb).

We conclude that in this case a minimal solution w satisfies

|w|b < (ma + 1)cb. (26)

Now from inequalitites (24) and (26) we get a polynomial upper bound mb forthe number of bs in a minimal solution according to above cases, and, therefore,there exist a polynomial upper bound ma + mb for the length of a minimalsolution. Note that the bounds ma and mb can be deduced in polynomial timefrom the input I .

5.2 Stable suffix complexity

In this section we shall study the morphisms, which have the same suffix com-plexity as their successors. Our classification is a refinement of results in [3].

We shall study only balanced pairs, since the unbalanced cases cases weretreated in Section 5.1.

Given morphisms g and h, let ga denote the word g(x), x ∈ {a, b}, suchthat a = pref1(ga). Similarly define also gb, ha and hb. By symmetry, we maysuppose ga = g(a) and gb = g(b). For {x, y} = {a, b} we may have hx = h(x) orhx = h(y).

17

Page 22: Binary (Generalized) Post Correspondence Problem is in P

Lemma 18. Let (g, h) be a pair of balanced morphisms, which has a successorwith the same suffix complexity. Then there is a positive integer j such that (atleast) one of the following cases takes place (up to the renaming of letters andmorphisms):

(Case 1)

ga, ha ∈ a+, gb, hb ∈ b+,

(Case 2)

ga, ha < (ab)ω, gb, hb < (ba)ω ,

(Case 3)

ga, ha ∈ (abj)+a, gb, hb = b.

(Case 4)

ga, ha ∈ ab∗, gb, hb ∈ b+,

(Case 5)

ga ∈ aa+b+, ha = a, gb, hb ∈ b+.

(Case 6)

ga = a, hb = b.

Proof. The main tool of the proof of Lemma 18 will be the following observation,which follows from the condition 1 on page 15, namely that each suffix of g(a)and g(b) is in Og , and each suffix of h(a) and h(b) is in Oh.

Claim 19. Let u be a suffix of ga or gb. If x is the first letter of u, then u iscomparable with hx.

The claim holds also if we exchange g and h. A typical example of anapplication of the previous claim is the observation that if bb ≤ hb then ba isnot a factor of ga nor of gb.

We will also need the following auxiliary lemma, which again has a variantwith g and h exchanged. It uses the condition 1 on page 15.

Lemma 20. Let (g, h) have stable suffix complexity, and {x, y} = {a, b}. If

suff1ha = suff1hb = x (27)

then gx ∈ x+ and gy ∈ yx∗.

Proof. Let k be the maximal exponent such that xk is a suffix of {ga, gb}. Sincexk ∈ Og , there is a (possibly empty) word v such that vxk ∈ {ga, gb} and v issuffix-comparable with ha or hb. If v is nonempty, the assumption (27) yieldsthat x is its suffix, a contradiction with the maximality of k. Hence v is empty,and gx ∈ x+.

Suppose that gy = yu and u /∈ x∗. Since u ∈ Og and it is not a suffix of gx,the letter y has to be suffix-comparable with ha or hb. It is a contradiction with(27).

18

Page 23: Binary (Generalized) Post Correspondence Problem is in P

Consider the number of words in the set {ga, gb, ha, hb}, which are longerthan one.

If |ga| = |gb| = |ha| = |hb| = 1, we have Case 1.If exactly three of the words in the set {ga, gb, ha, hb} are of length one, the

morphisms are not balanced.In the following we shall assume that at least two words have the length at

least two.Suppose first that bb ≤ gb. Claim 19 implies that hb ∈ b+, and ha ∈ a+b∗.

1. If |hb| ≥ 2 then bb ≤ hb, and as above gb ∈ b+ and ga ∈ a+b∗. Supposethat Case 4 does not takes place. Therefore at least one of the words ga,ha starts with aa. Let, by symmetry, aa ≤ ga. Then ha ∈ a+. If alsoaa ≤ ha then Case 1 takes place. If ha = a, we have Case 5.

2. Let now hb = b. Since the morphisms are balanced, |ha| ≥ 2. If ga = a,we have Case 6. Suppose therefore that also |ga| ≥ 2. If aa is a prefix ofga or ha then we have a situation similar to 1. above. Let, on the otherhand, ab be a prefix of both ga and ha. The assumption bb ≤ gb impliesha ∈ ab+, by Claim 19. Thus Case 4 takes place, by Lemma 20.

We have up to now discussed all cases with bb < gb. By symmetry, it remainsto consider morphisms such that bb is not a prefix of {gb, hb}, and aa is not aprefix of {ga, ha}.

1. If all words are longer than two then, by Claim 19, they do not containfactor aa nor bb, and Case 2 occurs.

2. Suppose that there is just one word of length 1, say h(b) = b. Thereforeab ≤ ga, ba ≤ gb, and ha is a prefix of (ab)ω, by Claim 19. Since themorphisms are balanced, we conclude that |ha| ≥ 3, and therefore aba ≤ha. Hence, using Claim 19, we deduce that ga, gb do not contain abb, andtherefore neither bb. Again Case 2 occurs.

3. It remains to consider cases, in which exactly two words are of lengthone. Suppose that Case 6 does not takes place, and, by symmetry, assumegb = hb = b.

2.3.1. If b is the last letter of either ga or ha, then we have Case 4, byLemma 20.

2.3.2. Let now

ga = abk1abk2 . . . abkma, and ha = ab`1ab`2 . . . ab`m′a.

Claim 19 implies that ki = `1 for each i = 1, . . . , m, and `j = k1 foreach j = 1, . . . , m′. Therefore we have Case 3.

This completes the proof of Lemma 18.

We now have a list of morphisms which are suspicious of having stable suffixcomplexity. They require a special attention. If (g, h) has the same suffixcomplexity as its successor (g1, h1), it does not necessarily mean that (g1, h1)has itself a successor nor that it has stable suffix complexity. We may thereforerepeat the construction of the successors hoping that the suffix complexity willfinally decrease. These considerations lead to the following theorem, which liststhe cases in which even repeated construction of successors may fail to simplifythe instance.

19

Page 24: Binary (Generalized) Post Correspondence Problem is in P

Theorem 21. Let (g, h), (g′, h′) be pairs of balanced morphisms, such that(g′, h′) is the successor of (g, h). Let moreover also (g′, h′) have a successor,and let the suffix complexity of both (g, h) and (g′, h′) be stable. Then one of thefollowing cases occurs (up to the renaming of letters):

(Case 0)

g(a) = h(a) = a, g(b) = h(b) = b.

(Case 1’)

g(a) = aj , g(b) = bk,

h(a) = bl, h(b) = am,

where j, k, l, m are positive integers satisfying

gcd(j, m) = gcd(k, l) = gcd(m, k) = gcd(l, j) = 1. (28)

(Case 2’)

ga, ha ∈ (ab)∗a, gb, hb ∈ (ba)∗b,

and gcd(|g(ab)|, |h(ab)|) = 2.

Proof. By Lemma 18, the pair (g, h) has one of the forms described by Cases1–6. We have to eliminate Cases 3–5, and to specify Cases 1–2 and 6.

Note that if g = h, then we have Case 0, since (g, h) has stable suffix com-plexity. Further we shall suppose g 6= h.

a) We first show that Case 6 comes under one of Cases 1, 2, 4 or 5. Supposetherefore that ga = a, hb = b.

If Case 1 does not take place, we can assume, by symmetry, that ha

contains the letter b. Since g and h are balanced, we have |gb| ≥ 2.

1. If suff1(ha) = b, then gb ∈ b+, by Claim 20. Lemma 19 now impliesthat ha ∈ a+b+, and we have Case 4 or a case symmetric with Case5.

2. If, on the other hand, suff1(ha) = a then ha contains a factor ba,which implies, by Lemma 19, that ba ≤ gb, and bb is not a factor ofha.

From Claim 20 we deduce that gb does not end by a, which impliesthat it contains ab, and ab ≤ ha. Therefore, by Lemma 19, the wordaa is not a factor of gb.

We conclude that aba ≤ ha and bab ≤ gb, which implies, by Lemma19, that g and h satisfy Case 2.

b) Let (g, h) conform with Case 3. Since g and h are balanced, and g 6= h,we conclude g(b) = h(a) = b. Let

g(a) = (abj)ka, g(b) = b,

h(a) = b, h(b) = (abj)`a.

20

Page 25: Binary (Generalized) Post Correspondence Problem is in P

We assume j > 1 and k + ` > 0, otherwise we have Case 1 or 2, whichwill be studied later. We can also suppose k 6= `, since (g, h) have stablesuffix complexity. One can directly verify that

g′(a) = (abj)k′

a, g′(b) = b,

h′(a) = (baj)`′b, h′(b) = a

for some k′ 6= `′. Therefore (g′, h′) is not balanced, a contradiction.

c) Suppose that (g, h) satisfies Case 4, and let

ga = abγ , gb = bα,

ha = abδ, hb = bβ .

We can also suppose γ + δ > 0 (otherwise Case 1 occurs). With {x, y} ={a, b} we obtain

g′(a) = abγ′

, g′(b) = bα′

,

h′x = xyδ′

, h′y = yβ′

,

where

αα′ = ββ′ = lcm(α, β), (29)

and (γ′, δ′) 6= (0, 0) is the smallest non-negative solution of the equation

γ′α + γ = δ′β + δ. (30)

The assumption x = b, y = a implies that (g′, h′) is not balanced in |.|aor |.|b. Therefore

h′(a) = abδ′

, h′(b) = bβ′

,

If α = β then also γ = δ, since (g, h) is balanced, and thus the pair hasnot stable suffix complexity, a contradiction.

We may therefore suppose α < β, and γ > δ.

From (29) we deduce α′ > β′, and since g′ and h′ are balanced, we havealso γ′ < δ′. Inequalities α < β and γ′ < δ′, and the equality (30) imply

γ′α + γ = δ′β + δ ≥ (γ′ + 1)(α + 1) + δ,

and thus γ > α. Therefore σ(g) = γ + 1.

Similarlyγ′α + γ ≥ δ′(α + 1) + δ > γ′α + δ′

implies δ′ < γ. Together with β′ ≤ α, which follows from (29), we obtain

σ(h′) = max{β′ + 1, δ′ + 1} < γ + 1 = σ(g).

Recall that, by (20), |σ(h′)| ≤ |σ(g)| and |σ(g′)| ≤ |σ(h)|. The aboveinequality therefore implies that σ(g′, h′) is strictly smaller than σ(g, h),a contradiction.

21

Page 26: Binary (Generalized) Post Correspondence Problem is in P

d) Suppose that Case 5 is satisfied by (g, h).

If ha = h(a) and hb = h(b) then (g, h) is not balanced in |.|a.

Let therefore

g(a) = aγbδ, g(b) = bα,

h(a) = bβ, h(b) = a.

Then

g′(a) = abε, g′(b) = bα′

,

h′(a) = bγaδ′

, h′(b) = aβ′

,

with δ′ > 0, and this time (g′, b′) is not balanced in |.|a.

e) Let (g, h) have the form of Case 2.

Suppose that, contrary to the claim, at least one of the four images iselement of (ab)+ or (ba)+. By symmetry, let ga ∈ (ab)+.

1. If gb ∈ (ba)∗b then, by Lemma 20, we have ha ∈ {a, ab} and hb = b.Therefore either the morphisms are not balanced, or ga = ha = aband gb = hb, and (g, h) has not stable suffix complexity.

2. Let now gb ∈ (ba)+. The previous argument allows to suppose thateither ha ∈ (ab)+, hb ∈ (ba)+, or ha ∈ (ab)∗a, hb ∈ (ba)∗b.

2.1. Let first ha ∈ (ab)+ and hb ∈ (ba)+. It is easy to see that thesuffix complexity of (g, h) is not stable.

2.2. Let then ha ∈ (ab)∗a and hb ∈ (ba)∗b. A direct inspection ofblocks yields that g′

a ∈ a+, g′b ∈ b+, h′a ∈ (ab)+, and h′

b ∈(ba)+. Clearly, the pair (g′, h′) either is not balanced, or it hasno successor.

It remains to show that m = gcd(|g(ab)|, |h(ab)|) is equal to 2. Bothnumbers are even, hence m ≥ 2.

It is easy to see that the pair (g′, h′) again conforms to Case 2. Since italso has stable suffix complexity, we have just showed

g′a, h′a ∈ (ab)∗a, g′b, h

′b ∈ (ba)∗b.

We have σ(g) = |g(ab)|, σ(h) = |h(ab)|, σ(g′) = |g′(ab)|, and σ(h′) =|h′(ab)|.

Let h(x) = ha and h(y) = hb.

Consider the shortest word z ∈ (ab)+, which is an image of both g and h.From the definition of blocks and from their forms we deduce

z = g(g′ag′b) = h(h′

xh′y) ∈ (ab)+.

On the other hand, it is not difficult to see that

g(ab)k = h(ab)l ∈ (ab)+

22

Page 27: Binary (Generalized) Post Correspondence Problem is in P

with

k =|h(ab)|

m

l =|g(ab)|

m.

The minimality of z implies

2|h(ab)|

m≥ |g′ag′b|,

and2σ(h) ≥ m σ(g′).

Since (g, h) has stable suffix complexity, we have σ(h) = σ(g′) from (20),and the claim follows.

f) Let finally Case 1 be satisfied by (g, h). If h(a) ∈ a+ and h(b) ∈ b+ thenthe morphisms are not balanced in |.|a or |.|b.

Let therefore j, k, l, m be positive integers such that

g(a) = aj g(b) = bk

h(a) = bl h(b) = am.

It is straightforward to see that the successors (g′, h′) of (g, h) satisfy

g′(a) = aj′ g′(b) = bk′

h′(a) = bl′ h′(b) = am′

,

with

j′ =m

gcd(j, m)k′ =

l

gcd(k, l)

l′ =j

gcd(j, m)m′ =

k

gcd(k, l).

Clearly σ(g, h) = j + k + l + m, and σ(g′, h′) = j′ + k′ + l′ + m′. Stablesuffix complexity implies gcd(j, m) = gcd(k, l) = 1, and j ′ = m, k′ = l,l′ = j, m′ = k.

Since also (g′, h′) has stable suffix complexity, we similarly obtain gcd(m, k) =gcd(l, j) = 1.

Henceforth we shall say that the pair (g, h) has steadfast suffix complexity ifand only if it satisfies one of the cases of Theorem 21. The notion of steadfastsuffix complexity is stronger than that of stable suffix complexity. It points outinfinite chains of successors with the same suffix complexity. Note, however,that the steadfast suffix complexity does not guarantee that the successors willbe balanced.

5.3 Bounds for instances with steadfast suffix complexity

We are going to show that the instances with steadfast suffix complexity canbe decided in the polynomial time. The overall conclusion will be that in allthree cases we can obtain a polynomial upper bound for the length of a minimalsolution.

23

Page 28: Binary (Generalized) Post Correspondence Problem is in P

5.3.1 The simplest case

We start with the simplest possible case |g(a)| = |g(b)| = |h(a)| = |h(b)| = 1.Let µ be the morphism rewriting letters, i.e. µ(a) = b and µ(b) = a.

We shall consider two cases. Either

g(a) = h(a) = a and g(b) = h(b) = b

or

g(a) = h(b) = a and g(b) = h(a) = b.

Note that I can have a solution only if |p1s1| = |p2s2|. Assume that |p1| ≥ |p2|,which implies that |s2| ≥ |s1| and let p = p−1

2 p1 and s = s2s−11 .

1. In the first case a solution w satisfies pw = pg(w) = h(w)s = ws. There-fore a solution exists if and only if the words p and s are conjugates. Theminimal solution is surely shorter than |p|.

2. Consider the second case. We have

pw = µ(w)s.

Let w = l1 . . . l|w|. It is easy to see that li = µ(li+p), i = 1, . . . , |w| − p.Since µ(p) is a prefix of w, the word w is a prefix of (µ(p)p)+. A solutionexists if and only if s is a factor of pµ(p)p. The length of the shortestsolution is less than 3|p|.

5.3.2 a+, b+

Consider Case 1’ of Theorem 21. We may also suppose that jklm > 1.A solution w satisfies

|p1|a + j · |w|a + |s1|a = |p2|a + m · |w|b + |s2|a

|p1|b + k · |w|b + |s1|b = |p2|b + l · |w|a + |s2|b,

whencej · |w|a −m · |w|b = |p2s2|a − |p1s1|a−l · |w|a + k · |w|b = |p2s2|b − |p1s1|b,

(31)

Equality (28) implies that (j,−m) and (−l, k) are linearly independent, andthere is unique solution for wa and wb. The size of the solution is certainlypolynomial with respect to the coefficients (for example by Cramer’s rule), andthe solution can be easily checked.

5.3.3 Segments

Now study an instance I with a pair (g, h), which satisfies Case 2’. Supposep2 ≤ p1, and put p = (p2)

−1p1.We say that u1, . . . , uj is the decomposition of the word u = u1 . . . uj into

segments iff each ui is a nonempty factor of (ab)+, and suff1(ui) = pref1(ui+1),u = 1, . . . , j − 1. Such a decomposition is clearly unique.

If w1, . . . , wk is the decomposition of a solution w into segments, then alsog(w1), . . . , g(wk) is the decomposition of g(w) into segments, and similarlyh(w1), . . . , h(wk) is such a decomposition of h(w).

24

Page 29: Binary (Generalized) Post Correspondence Problem is in P

1. First consider a solution w being a factor of (ab)+, which means that itsdecomposition into segments is trivial. By Theorem 21,

gcd(|g(ab)|, |h(ab)|) = 2,

which implies, in particular, that |g(ab)| 6= |h(ab)|. Therefore if w = (ab)k orw = (ba)k, the integer k is uniquely given by

|p1s1|+ k |g(ab)| = |p2s2|+ k |h(ab)|.

Similarly for w = (ab)ka, and w = (ba)kb.2. Let now w1, . . . , wk be the decomposition of a solution w into segments

with k ≥ 2. Let also r1, . . . , rm, t1, . . . , tl, t′1, . . . , t′l′ be the decomposition into

segments of words p, s2, s1 respectively.The decomposition into segments of pg(w) is either

r1, . . . , rm, g(w1), . . . , g(wk)

orr1, . . . , rmg(w1), . . . , g(wk) (32)

depending on the last letter of p and the first letter of w. To avoid complicatedtechnical case analysis we shall suppose that if suff1(p) = pref1(g(w1)) then p =r1 . . . rm−1, and rm is empty. Therefore (32) will be always the decompositionof pg(w) into segments. Using the same convention for t′1 we may assume that

r1, . . . , rmg(w1), g(w2), . . . , g(wk)t′1, t′2, . . . , t

′l′

is the decomposition of the word z = pg(w)s1 into segments. The decompositionof z may be given also with help of h as

h(w1), . . . , h(wk)t1, t2, . . . , tl.

The main tool of the decision in this setting is the fact that the two decompo-sition are identical.

For sake of simplicity we are not going to give an explicit formula for thelength bound of the solution, but we show how to obtain an upper bound for k,and an upper bound for the length of each segment.

Suppose that m = 1, i.e., p = r1. Then pg(w1) = h(w1), g(wk)s1 = h(wk)s2

and w1wk is a solution. As we have seen above in 1, it is not difficult to decidewhether there exist words w1 and wk .

Let now m ≥ 2. Then

h(wi) = ri (33)

for i = 1, . . . , m− 1,

h(wm) = rmg(w1), (34)

h(wi+m−1) = g(wi) (35)

for i = 2, . . . , k −m, and

h(wk)t1 = g(wk−m+1). (36)

Also l − l′ = m and we may suppose l′ = 0, l = m, t1 . . . tm = s = s2s−11 .

Then

g(wk−m+i) = ti (37)

25

Page 30: Binary (Generalized) Post Correspondence Problem is in P

for i = 2, . . . , m.The equalities (33)–(37) yield a recurrent formula for the sequence w1, w2,

. . . , wk . We shall study its behavior.Suppose

g(a) = (ab)αa g(b) = (ba)βb

h(a) = (ab)γa h(b) = (ba)δb.

We shall say that

di =

|wi|

2

is the size of the segment wi. For example, the size of (ab)da is d.Clearly di ≥ 0 for each i = 1, . . . , k, and the size of wi, i = 1, . . . , m− 1, is

bounded, because of the equality

h(wi) = ri.

The equality (34) imposes a polynomial upper bound also to the size of wm.Similarly we deduce that there is a polynomial upper bound for the numbersdk−m+1, . . . , dk induced by the length of tis.

A segment wi may have four different types: it can be an element of (ab)+,(ba)+, (ab)+a or (ba)+b.

It is straightforward to verify, using the reccurence formula, the importantfact that the type of wi is the same as the type of wi+m−1, for each i = 2, . . . , k−m.

Consider the relation between di and di+m−1. Direct computation yieldsthat, for example, if wi = (ab)dia, 2 ≤ i ≤ k −m, then

g(wi) = (ab)di(α+β+1)+αa = h((ab)di(α+β+1)+α−γ

γ+δ+1 a) = h(wi+m−1),

which means that

di+m−1 = κ · di + λ, (38)

with κ > 0. Let i = j mod (m − 1), with 2 ≤ j ≤ m. It is an easy exercise toshow that the sequence

dj , dj+m−1, dj+2(m−1), . . . , dj+c(m−1), (39)

with k −m + 1 ≤ j + c(m− 1) ≤ k − 1 is either constant or strictly monotone.If it is not constant, we have a polynomial upper bound for c derived from thebounds for dk−m+1, . . . , dk. Therefore we have also a polynomial upper boundfor the length of the solution.

It is not difficult to verify that the relation between di and di+m−1 is given bya recurrence relation similar to (38) for all types of segments. The upper boundfor the length of the solution is obtained as soon as some of the correspondingsequences is not constant.

Suppose that k > 3m − 2, and the sequences (39) are constant for all j =2, . . . , m. Then g(w2 . . . wm), which is the overflow of the presolution w1 . . . wm,is equal to g(wm+1 . . . w2m−1), which is the overflow of the word w1 . . . w2m−1.Consequently, w is not the shortest solution, because

w1 . . . wmw2m . . . wk

26

Page 31: Binary (Generalized) Post Correspondence Problem is in P

is a solution as well.It remains to consider the possibility h(a) = hb and h(b) = ha. In this case

the period after which the types of segments start to repeat, is 2(m− 1), ratherthan m − 1. The relationship between di and di+2(m−1) is more complicated,but again of the form (38). The rest is similar as in the former case.

6 Pseudocode

In this section we want to give a complete description of our algorithm for non-periodic instances. The algorithm is written in a rather detailed pseudocodeform, so that an interested reader can check all details. From this section aloneit should be clear that the algorithm does what it is supposed to do, and that itruns in polynomial time, provided that the theoretical claims given in the paperare true.

The algorithm outputs “yes” if and only if a solution exists. For sake ofsimplicity we do not provide the solution although the algorithm is constructive,and a solution is found, if it exists.

It should be also remembered that the our primary task is not to make thealgorithm as efficient as possible. We sometimes choose an approach, which isa bit generous from the complexity point of view, in order to keep the codesimple.

The main body of the algorithm is the following.

Non-Periodic Binary GPCP(I = {p1, p2, g, h, s1, s2})1 if at least one block is missing2 then return Direct(I)3 else if (g, h) is unbalanced or has steadfast suffix complexity4 then calculate the upper bound k5 return Length(ε, k, I)6 else return Successor(I)

Whether the instance has a successor or not makes the crucial difference. Ifthe successor does not exist, the decision is done by a direct attempt to constructthe solution.

If a successor exists, we first check whether the instance can be decidedby a length argument. If the length argument does not help, the procedureSuccessor is applied, which represents the core of our algorithm, since thesuccessors are the main threat to the polynomial complexity.

6.1 Constructing the solution

The basic idea of the algorithm is extremely simple: the existence of the solutionis tested by an attempt to construct it. The words obtained during the processof the construction are called presolutions. More precisely, presolution is anypair of words (w1, w2), such that

p1g(w1) ./ p2h(w2), (40)

and

w1 ./ w2. (41)

27

Page 32: Binary (Generalized) Post Correspondence Problem is in P

Note that we call presolution even a pair, which will eventually not lead to asolution.

In this subsection we present tools used in the construction. The basic stepis the procedure Continue. We have two words p, and q, and want to findan ordered pair (x1, x2), where xi is either a letter or the empty word, so thatpg(x1) is comparable with qh(x2). Lemma 7 implies that if q−1p is not thecritical overflow then either x1 or x2 is given uniquely by the letter x(f, c), withf ∈ {g, h}, c ∈ {a, b} (see proof of Lemma 6).

Therefore the algorithm returns either (c, ε) or (ε, c), or, if the overflow iscritical, (ε, ε). The procedure fails (returns Nil), if the words p, q do not admita continuation.

Continue(p, q, g, h)1 if pzg ./ qzh

2 then switch

3 case qzh < pzg :4 fix c such that pref1((qzh)−1pzg) = x(g, c)5 if p ./ qh(c)6 then return (ε, c)7 else return nil8 case pzg < qzh :9 fix c such that pref1((pzg)

−1qzh) = x(h, c)10 if pg(c) ./ q11 then return (c, ε)12 else return nil13 case pzg = qzh :14 return (ε, ε)15 else return nil

The possible solution is constructed in the following way. We have twowords w1 and w2, which satisfy (40) and (41). One of them is extended in eachstep by one letter determined by Continue. If the overflow is critical thenthe continuation still can be obtained by comparison of w1 and w2. This iswhat Extend adds to Continue. Only if w1 = w2 and the overflow is critical,the output is (ε, ε). If the extension of (w1, w2) is not possible, the procedurereturns Nil.

Extend (I, w1, w2)1 if not w1 ./ w2

2 then return nil3 else (c1, c2)← Continue(p1g(w1), p2h(w2))4 switch

5 case (c1, c2) = nil : return nil6 case (c1, c2) = (ε, ε) :7 switch

8 case w1 = w2 : return (ε, ε)9 case w1 < w2 : return (pref1(w

−11 w2), ε)

10 case w2 < w1 : return (ε, pref1(w−12 w1))

11 case else : return (c1, c2)

Given words z1, z2, x, y, the auxiliar procedure Overflow returns the over-flow of z1g(x) and z2g(y), i.e., a pair of words ν = (o1, o2), one of them empty,such that z1g(x)o1 = z2h(y)o2.

28

Page 33: Binary (Generalized) Post Correspondence Problem is in P

The aim of this subsection is acomplished by the procedure Construct,which starts with a presolution pair (x, y) (at the beginning x = y = ε), andreturns

• (w1, w2),Nil, where (w1, w2) is a presolution, after which there is no con-tinuation;

• or (w1, w2),(u, v), such that (w1, w2) and (w1u, w2v) are presolutions whichyield the same overflow;

• or (w1, w2), (ε, ε), where w1 = w2, and the presolution (w1, w2) yields thecritical overflow.

In all cases (x, y) ≤ (w1, w2).

Construct(I, x, y)1 i← 02 (x0, y0)← (ε, ε)3 ν0 ← Overflow(p1, p2, x, y)4 S ← ∅5 (c1, c2)← Extend(x, y)6 while (c1, c2) 6= nil & (c1, c2) 6= (ε, ε) & νi /∈ S7 do (xi+1, yi+1)← (xic1, yic2)8 i← i + 19 (c1, c2)← Extend(xxi, yyi)

10 νi ← Overflow(p1, p2, xxi, yyi)11 S ← S ∪ {νi}12 if νj = νi

13 then return (xxj , yyj), (x−1j xi, y

−1j yj)

14 else return (xui, yvi), (c1, c2)

The algorithm Construct is polynomial, since the number of possible over-flows is limited by the length of the input. The procedures Continue and Ex-tend work in a way, which keeps overflows as short as possible. More precisely,if (z, ε) is an overflow in Construct, then z is either a suffix of p1g(x), or asuffix of tg(a) or tg(b), where t < zh. Similarly for overflows (ε, z).

6.2 Some missing blocks

The procedures of the previous subsection will be used for several purposes. Inthis part we explain the first line of the main algorithm, namely the test of theexistence of blocks.

Consider an instance I = (p1, p2, g, h, s1, s2). By Lemma 7, the begin blockis given uniquely, if it exists. The following procedure constructs the begin block(x, y), or returns Nil, if it does not exist.

BeginBlock(p1, p2, g, h)1 (x, y)← (ε, ε)2 ν ← (p1, p2)3 S ← ∅4 (c1, c2)← (ε, ε)5 while (c1, c2) 6= nil & ν 6= (c(g, h), ε) & ν /∈ S6 do (x, y)← (xc1, yc2)7 ν ← Overflow(p1, p2, x, y)8 (c1, c2)← Continue(ν, g, h)

29

Page 34: Binary (Generalized) Post Correspondence Problem is in P

9 S ← S ∪ {ν}10 if ν = (c(g, h), ε)11 then return (x, y)12 else return nil

The procedure is similar to Construct. Since the continuation is uniqueunless the critical overflow occurs, it is enough to construct presolutions by Con-tinue. In polynomial time either a critical overflow is obtained and the beginblock is completed, or the continuation is no more possible, or a non-criticaloverflow is obtained twice, which indicates an infinite repetition of already seenoverflows. In the two latter cases, therefore, the begin block does not exist.

The d-block of morphisms g, h, d ∈ {a, b}, is a minimal pair (e, f), withd ≤ e, satisfying c(g, h)g(e) = h(f)c(g, h). The algorithm searching for a d-block is quite analogical to BeginBlock. It starts with the critical overflowand first checks whether the presolution (d, ε) is possible.

Block(d, g, h)1 (x, y)← (ε, ε)2 o← (c(g, h), ε)3 S ← ∅4 if c(g, h) ./ h(d)5 then (c1, c2)← (ε, d)6 else (c1, c2)← nil7 while (c1, c2) 6= nil & o 6= (c(g, h), ε) & o /∈ S8 do (x, y)← (xc1, yc2)9 o← Overflow(o1, o2, c1, c2)

10 (c1, c2)← Continue(o1, o2, g, h)11 S ← S ∪ {o}12 if o = (c(g, h), ε)13 then return (x, y)14 else return nil

If g, h has both blocks, then also the mirror morphisms g , h have blocks. Wealso check whether the reverse instance has a begin block. Since the instanceswithout a begin block are easier to decide, we switch to the reverse instance, ifit has no begin block.

The condition on the first line of the main algorithm can be therefore definedin the following way:

BeginBlock(p1, p2, g, h) = Nil or BeginBlock(s1, s2, g, h) = Nil

or Block(a, g, h) = Nil or Block(b, g, h) = Nil.

6.3 Direct solution

Suppose now that one of the begin blocks does not exist, or, if both exist, atleast one of the letter blocks does not exist. (If I has the begin block, and Ihas not, we consider I instead of I .)

In this case the existence of a solution can be tested by a direct attempt toconstruct it. Let (w1, w2) be a presolution. The procedure Continue yields acontinuation under the assumption that the image of the continuation will havelength at least z = max{|zg|, |zh|}+ 1. Therefore, given a presolution (w1, w2),we have also to check whether wt is a solution for some w ≤ max{w1, w2}, andsome word t such that |g(t)| < |z| or |h(t)| < |z|. The set of such words t will

30

Page 35: Binary (Generalized) Post Correspondence Problem is in P

be denoted by F . The definition of zg and zh implies that t ∈ a+ or t ∈ b+, andtherefore the cardinality of F is at most 2z.

The following natural concept arises. We say that a pair of words (w1, w2) isa sufficient presolution for an instance I if and only if the following implicationholds:

• If (w1, w2) ≤ (w, w) for a solution w

• then there is a prefix w′ ≤ max{w1, w2} such that w′t is a solution forsome t ∈ F .

Clearly, if a sufficient presolution is obtained, it is useless to continue with theconstruction: either there is no solution, or there is a shorter one.

Since the instance has no successor, it is possible to find one or more sufficientpresolutions, which are not too long. Then it is enough to check whether a prefixof a sufficient presolution leads to a solution. The work is done by the followingprocedure.

Presolution(I, w1, w2)1 w ← max{w1, w2}2 for all w′ ≤ w and all t ∈ F3 do if p1g(w′t)s1 = p2h(w′t)s2 & w′t 6= ε4 then return yes5 halt

6 return nil

The sufficient presolution is constructed by the procedure Sufficient. It isbased on the procedure Construct, which has several possible outputs. Letus discuss respective cases.

(A) If the output is (x, y), Nil, then there is no continuation, and (x, y) is theunique sufficient presolution.

(B) If the output is (w, w), (ε, ε), then

p1g(w) = p2h(w)c(g, h). (42)

This is the only case, in which the continuation is not given uniquely byExtend. It is then necessary to branch the algorithm and check bothpossible continuations.

Suppose that this special case occurs for the second time, i.e.,

p1g(w) = p2h(w)c(g, h) and p1g(ww′) = p2h(ww′)c(g, h).

Then each solution ww′z can be shortened to wz. Therefore it is enoughto pay attention only to the first occurrence of the situation.

(C) Suppose that the output is (x, y), (u, v), where uv is a nonempty word.Then (x, y) and (xu, yv) have the same overflow.

(a) Let moreover xuω = yvω. Then the construction of presolution cancontinue infinitely. We are, however, able to find an integer k suchthat (xuk, yvk) is a sufficient presolution in the following way.

By Lemma 3, the primitive roots of u and v are conjugate. Thatimplies, together with |g(u)| = |h(v)|, that |g(v)| = |h(v)| (and|g(u)| = |h(u)|) if and only if |u| = |v|.

31

Page 36: Binary (Generalized) Post Correspondence Problem is in P

Suppose first |u| = |v|, therefore also |g(v)| = |h(v)|. Let ` be anexponent, such that

|g(yv`v′)| > |g(x)|,

|h(yv`v′)| > |h(y)|.

Then for each v′ < v, and k > ` we have

h(yv`v′)−1g(yv`v′) = h(yvkv′)−1g(yvkv′),

since both overflows have the same length and are suffixes of the sameword. Therefore if yvkv′t with t ∈ F is a solution then also yv`v′t isa solution and (xu`+1, yv`+1) is a sufficent presolution.

Let then |u| 6= |v| and |g(v)| 6= |h(v)|. The exponent k in a solutionyvkv′t is now bounded by the equality

|p1g(yvkv′t)s1| = |p2h(yvkv′t)s2|.

(b) Assume, on the other hand, that xuω 6= yvω. Then we continue theconstruction by a recursive call. The fact of a recursive call naturallyraises doubts about the complexity. To analyse the possible progressof the algorithm we once more distinguish two cases according towhether (u, v) can or cannot be factorized into (u′u′′, v′v′′) so that(u′′u′, v′′v′) is a letter block.

(i) Suppose first that such a factorization does not exist. Then thefurther continuation is unique, by absence of the critical overflow.Moreover the length of the continuation is limited by |xuω∧yvω |,which is less than

max{|x|, |y|}+ 2 ·max{|u|, |v|},

by Lemma 3.

(ii) Let now the indicated factorization of (u, v) exist. Repeated ap-plication of Extend will as before eventually lead to a longestprefix (xuku1, yvkv1) of (xuω, yvω) that is a presolution. Furtherextension towards (xuω , yvω) would violate requirement that thewords in a presolution have to be comparable. In this case, how-ever, it can also happen that the overflow of (xuku1, yvkv1) iscritical, if (u1, v1) = (u′, v′). Then there may be a continuation(xuku′α, yvkv′β), which is no more a prefix of (xuω, yvω).It is crucial to observe here that the critical overflow cannot oc-cur again in the continuation (xuku′α, yvkv′β), since this wouldimply the existence of the second letter block, which is excludedby assumptions of the present case.The construction of (α, β) can therefore lead again to one of thecases (A), (C)(a), or (C)(b)(i), but not to the just discussed case(C)(b)(ii).

We can conclude that the length of the constructed presolution is polynomiallybounded in all cases.

The pseudocode is as follows.

Sufficient(x, y, I)

32

Page 37: Binary (Generalized) Post Correspondence Problem is in P

1 (x, y), (u, v)← Construct(x, y, I)2 if (u, v) 6= nil & (u, v) 6= (ε, ε)3 then switch

4 case xuω = yvω :5 find sufficient k6 (x, y)← (xuk, yvk)7 case xuω 6= yvω :8 Sufficient(xu, yv, I)9 return (x, y)

When a sufficient presolution is given, we check whether it has a prefix(w, w) satisfying (42). If such a prefix exists, it is necessary to check bothpossible continuations. Note that it can happen not only in case (B), but alsoin case (C).

Direct(I)1 if I has the begin block but I has no begin block2 then I ← I3 (x, y)← Sufficient(p1, p2, g, h)4 if p1g(w) = p2h(w)c(g, h) for some (w, w) ≤ (x, y)5 then (for minimal such w)6 (xa, ya)← Sufficient(w, wa, I)7 Presolution(xa, ya)8 (xb, yb)← Sufficient(w, wb, I)9 Presolution(xb, yb)

10 else Presolution(x, y)

6.4 Length arguments

We now suppose that the instance cannot be decided by Direct, which meansthat both begin blocks, and both letter blocks exist.

In such case we first check whether we can obtain an upper bound for thelength of minimal solutions. This happens in two separated cases:

• the morphisms are unbalanced (see Section 5.1); or

• the morphisms have steadfast suffix complexity (see Section 5.3).

The corresponding sections describe how to calculate the bound for the lengthof a minimal solution.

The procedure Length, after calculating the bound, just constructs pres-olutions of sufficient length. Recall that the continuation is unique even if theblocks exist with the only exception of presolution (w, w) having the criticaloverflow. Such situation can moreover in a minimal solution occur at mostonce. Therefore the condition on line 10 does not continue the construction ifthe situation was already present on the input.

At the beginning w = ε.

Length(w, k, I)1 (x, y)← (w, w)2 (c1, c2)← Extend(x, y)3 while (c1, c2) 6= nil & (c1, c2) 6= (ε, ε) & min{|x|, |y|} < k

33

Page 38: Binary (Generalized) Post Correspondence Problem is in P

4 do (x, y)← (xc1, yc2)5 (c1, c2)← Extend(x, y)6 switch

7 case (c1, c2) = nil or min{|x|, |y|} = k :8 return Presolution(x, y)9 case (c1, c2) = (ε, ε) :

10 if p1g(w) = p2h(w)c(g, h)11 then Presolution(x, y)12 else Length(xa, k, I)13 Length(xb, k, I)

6.5 Successors

We shall now deal with the instances, which have four blocks: the begin block(p′1, p

′2), two letter blocks (e, f), (e′, f ′), and the begin block ( b1 , b2 ) of the

reverse instance. Moreover the length argument does not apply, and the mor-phisms have not steadfast suffix complexity.

Recall that (g1, h1) are successor morphisms of (g, h), and (g′1, h

′1) are suc-

cessor morphisms of ( g , h ).

Successors(I = {p1, p2, g, h, s1, s2})1 E ← EndBlock(b1, b2, g

′1, h

′1, ε)

2 if τ 6= nil & r ./ q3 then

4 k ← max{|p′1τ |, |p′2τ |}+ 1

5 Length(ε, k, I)6 I ′ ← {p′1, p

′2, g1, h1, e(ε, q−1r)}

7 NonPeriodicBinaryGPCP(I ′)8 else k ← max{|p′1s

′1|, |p

′2s

′2|} | (s

′1, s

′2) ∈ E}

9 Length(ε, k, I)10 for (s′1, s

′2) ∈ E

11 do I ′ ← {p′1, p′2, g1, h1, s

′1, s

′2}

12 NonPeriodicBinaryGPCP(I)

We first check whether the instance has steadfast suffix complexity. Suchinstances cannot be reduced to simpler ones, and have to be treated separately(see section 6.4 above). Fortunately, there are just few of them, and they aredescribed in section 5.2.

If the suffix complexity is not steadfast then we construct one or more suc-cessors, and ask whether they have a solution. There are three possibilities:

a) there is just one end block, and hence just one successor;

b) there are more successors then one, but none of them has itself a successor(by Lemma 16);

c) there are more than one successor, and they have possibly further succes-sors.

Cases a) and b) are friendly, since no uncontrolled branching can occur. Thesituation, in which there are more successors than one to check (correspondingto lines 8-12 of the algorithm Successors), occurs at most once at the end ofthe whole algorithm. The number of successors is limited by Theorem 14.

34

Page 39: Binary (Generalized) Post Correspondence Problem is in P

The most tricky problem of the whole algorithm is the case c), because apossible exponential branching is threatening to destroy the polynomial com-plexity. The crucial Theorem 15 allows to find a unique successor, which is in away common to all successors.

There are two conditions to be verified in order to construct the commonsuccessor: q ./ r, and there is a word τ , shorter than all end blocks, such that

g(τ)s1 = c( g , h ) h(τ)s2.

By (18), the word τ is of the form

τ = g′1(z) b1 = h′1(z) b2.

The following algorithm constructs the set E of all endblocks using Theorem14. We start the procedure with z = ε, and set E = ∅. Procedure calls itselfin case of branching. Polynomial complexity is guaranteed by the upper boundgiven in Theorem 14.

The algorithm also finds the word τ , if it exists.

EndBlock(b1, b2, g′1, h

′1, z)

1 if r ≤ g′1(z) b1 & q ≤ h′

1(z) b2

2 then E ← E ∪ {e(r−1 g′1(z) b1, q−1 h′

1(z) b2)}3 else if b1 g′1(z) = b2 h′

1(z)4 then if τ = nil5 then τ ← z6 EndBlock(b1, b2, g

′1, h

′1, za)

7 EndBlock(b1, b2, g′1, h

′1, zb)

8 else if there is c ∈ A such that b1 g′1(zc) ./ b2 h′1(zc)

9 then EndBlock(b1, b2, g′1, h

′1, zc)

The idea of the successor instances is based on the fact that the instance Ihas a solution, which is not simple, if and only if a successor I ′ has a solution(see Theorem 12). A similar fact for the common successor is stated in Theorem15(ii).

Before turning to the successor it is therefore necessary to check, whetherthere is a simple solution. This is done by the procedure Length, since thelength of a simple solution is bounded by the length of blocks.

7 Periodic GPCP

In this section we study instances in which at least one of the morphisms isperiodic, that is h({a, b}) ⊆ v∗ for some word v.

We start with the following lemma for coverings, mainly for complexity rea-sons.

Lemma 22. Let v be a nonempty word and h a binary nonperiodic morphisms.

(i) There exists at most 2|zh| minimal h-covers of v, and they can be listed inpolynomial time.

(ii) The word u such that h(u) = v is unique, if it exists, and it can be foundin polynomial time.

35

Page 40: Binary (Generalized) Post Correspondence Problem is in P

Proof. Suppose we have a word ui, such that h(ui) ≤ v and look for a continu-ation. Like in the procedure Continue based on the proof of Lemma 6, thereis a unique letter x, such that h(uix) is comparable with v, unless h(ui)

−1v isa prefix of zh.

Suppose therefore that h(ui)−1v ≤ zh. The question now is how many

different covers of the word h(ui)−1v there may be. Since |h(ab)| > |zh|, there

are only following possibilities:

• aib, 1 ≤ i < |zh||h(a)| ,

• ai, i =⌈

|zh||h(a)|

,

• bia, 1 ≤ i < |zh||h(b)| ,

• bi, i =⌈

|zh||h(b)|

.

Clearly the number of words is at most 2|zh| and they can be all checkedand all covers can be found in polynomial time.

Therefore it can be also found if for some cover the equality holds. Theuniqueness follows from Lemma 1.

It is well known that the periodic PCP and GPCP are decidable, but we haveto show that they are decidable in polynomial time. The decidability could beproved using the traditional tools like one-counter languages and their decidableemptiness problem, see for example [4]. Here we shall give a constructive proofbased on combinatorial properties.

Note that the proofs restricted to the periodic GPCP are much simpler thantheir nonperiodic counterpart. The procedure Periodic is listed at the end ofthis section, see page 39.

Theorem 23. The binary GPCP is decidable in polynomial time in the casewhere both of the morphisms are periodic.

Proof. Let I = ((p1, p2), g, h, (s1, s2)) be an instance of the binary GPCP, whereboth h and g are periodic. Assume that u and v are primitive words, and that

h(a) = vi, h(b) = vj

g(a) = ui′ , g(b) = uj′(43)

It is important to note that in this case the order of letters in a solution wis irrelevant. Only numbers k = |w|a, and ` = |w|b are important.

We distinguish two main cases: 1) p1uω 6= p2v

ω, 2) p1uω = p2v

ω.It is easy to decide, using Lemma 2 and Lemma 3, which case takes place:

If we suppose, by symmetry, p2 ≤ p1 then the equality holds if and only ifp−12 p1u = vp−1

2 p1.Let u1 = p1u

ω ∧ p2vω be a finite word. In fact, by Lemma 3,

|u1| < max{|p1|, |p2|}+ max{|u2|, |v2|}(:= d),

since otherwise the words uω and vω have enough long overlap, they are conju-gated, and p1u

ω = p2vω. Put

t = d + max{|s2|, |s1|}.

36

Page 41: Binary (Generalized) Post Correspondence Problem is in P

The above considerations imply that a solution w satisfies p1g(w) < t andp2h(w) < t. We obtain upper bounds for k and ` from

k · |g(a)|+ ` · |g(b)| ≤ t,

k · |h(a)|+ ` · |h(b)| ≤ t.(44)

If g(a) = h(a) = ε, then it is sufficient to consider only solutions of the form bk,with k ≤ t. Similarly for g(b) = h(b) = ε. If all four image words are empty,the solution exists if and only if p1s1 = p2s2. In all remaining cases (44) yieldsthat k and ` are smaller than t.

Suppose now p1uω = p2v

ω, and let, by symmetry, p2 ≤ p1. Then u and vare conjugates, say u = xy, v = yx, and p−1

2 p1 = (xy)nx for some n. Moreover,a solution exists only if, for some m ∈ N, either s2s

−11 = x(yx)m or s1s

−12 =

y(xy)m, depending on whether s2 4 s1 or s1 4 s2 respectively.

The decision is now based on a length argument. The instance has a solutionif and only if there exists numbers k and ` such that

p−12 p1h(akb`) = g(akb`)s2s

−11

or

p−12 p1h(akb`)s1s

−12 = g(akb`).

Comparing length of both sides we conclude that either

n + ki + `j + t + 1 = ki′ + `j′, (45)

or

n + ki + `j = t + ki′ + `j′. (46)

If i = i′ or j = j′, we simply solve the equation. Otherwise, it is easy to deducethat positive integers ` and k satisfying (45) exist if and only if

`(j′ − j) ≡ n + t + 1 mod |i− i′|

for some `, 0 ≤ ` < |i− i′|.Similarly (46) has a solution for k and ` if and only if

`(j′ − j) ≡ n− t mod |i− i′|

for some `.

Obviously, the modular equation can be solved in polynomial time.

Corollary 24. The unary PCP is decidable in polynomial time.

Proof. Claim follows from Theorem 23, if h, g : {a}∗ → {a, b}∗, then we can setg(b) = ε = h(b), and both morphisms are periodic.

Next we study the case where only one of the morphisms is periodic.

Theorem 25. The binary periodic GPCP is decidable in polynomial time ifonly one of the morphisms is periodic.

37

Page 42: Binary (Generalized) Post Correspondence Problem is in P

Proof. Let I = (p1, p2, g, h, s1, s2) be an instance of the binary GPCP and as-sume that h is periodic, h({a, b}) ∈ v∗, where v ∈ {a, b}+ is a primitive word.Suppose also that g is nonperiodic.

Let w be a solution. We factorize w into w1w2, where w1 is the maximalprefix of w such that p1w1 ≤ p2v

ω .Consider first the word w2. Define words x and v′, and an integer ` ≥ 0 such

that x is a suffix of v or a suffix of p2, v′ < v, and

p1g(w1)xv`v′ = p1g(w) ∧ p2vω.

Let r = s2 ∧ vω, and define a word s by

s =

{

r−1s2s−11 if this word is in A∗,

ε otherwise.

It should be clear that the definitions are chosen to guarantee that

g(w2) = xv`v′s. (47)

The maximality of w1, and Lemma 22, imply that |xv`v′| < |g(ab)|, and

` ≤|g(ab)|

|v|.

We conclude that there is a polynomially bounded number of possible wordsg(w2), given by (47). By Lemma 22, it is not diffult to find in polynomial timea set F of all possible words w2.

Let us now study the word w1. We have two cases according to whetherthere is an infinite word u such that p1g(u) = p2v

ω or not.Let first u exist. By Lemma 1, such a word is unique. Since the number of

suffixes of v is finite, there are two prefixes u′, u′′ of u such that

(p1g(u′))−1p2vω = (p1g(u′′))−1p2v

ω = xvω ,

where x is a suffix of v. Therefore u is ultimately periodic, i.e. u = u1uω2 . The

words can be found in polynomial time by the procedure PeriodicConstruct,similar to Construct.

Note that w1 < u1uk2 . We have to give an upper bound for the exponent k

in a minimal solution w1w2.If |g(u2)| 6= |h(u2)|, the bound is given by a length argument from

|p1g(w1w2)s1| = |p2h(w1w2)s2|.

Suppose that |g(u2)| = |h(u2)|. Let w = u1uk2u′t be a solution such that

u′ ≤ u and t ∈ F . Let, moreover,

|g(u1)| ≤ |p2h(u1uk−12 u′)| (48)

|p2| ≤ |p1g(u1uk−12 u′)|. (49)

Then the overflows of u1uk−12 u′ and u1u

k2u

′ are equal, since they have the samelength and are suffixes of the same word g(uk−1u′) or h(u1u

k−12 u′) ∈ v+.

Therefore u1uk−12 u′t is a solution as well, which shows that in a minimal

solution w1 ≤ u1uk2 .

Suppose now that u does not exist. Then we denote by u1 the maximal wordsuch that p1g(u1)zg < p2v

ω. By Lemma 6, the word u1 is given uniquely alsoin this case, and is again determined by PeriodicConstruct. The length ofthe word is bounded by |p2|+ |v|, since otherwise an overflow occurs twice.

Now each solution is of the form w1w2, with w1 < u1, and w2 ∈ F .

38

Page 43: Binary (Generalized) Post Correspondence Problem is in P

Next we shall state the whole procedure Periodic for deciding periodicinstances of the GPCP. The algorithm uses the notation of previous proofs.

PeriodicConstruct(p1, p2, g, v)1 i← 02 x0 ← ε3 ν0 ← PeriodicOverflow(x0)4 S ← ∅5 c← letter such that p1g(c) < p2v

ω

6 while c 6= nil & νi /∈ S7 do xi+1 ← xic8 i← i + 19 c← letter such that p1g(xiczg) < p2v

ω

10 νi ← PeriodicOverflow(xi)11 S ← S ∪ {νi}12 if νj = νi

13 then return xj , x−1j xi

14 else return xi,nil

The procedure PeriodicOverflow(x) returns the shortest word v′ suchthat p1g(x)v′ ∈ p2v

∗. Parameters p1, p2, g and v are considered as global in thecode.

Periodic(I = {(p1, p2), h, g, (s1, s2)})1 switch

2 case both g, h periodic :3 if there is a solution of (45) (of (46) respectively)4 then return akb`

5 else k, `← count an upper bound6 for all w ∈

{

aibj | i ≤ k, j ≤ `}

7 do if p1g(w)s1 = p2h(w)s2

8 then return w9 halt

10 case h is periodic, h(a, b) ∈ v∗ :11 F ← ∅12 for all (x 4 p2 or x ≺ v) & v′ < v & ` ≤ |ab|

|v|

13 do if there is a w2 such that g(w2) = xv`v′

14 then F ← F ∪ {w2}15 (u1, u2)← PeriodicConstruct16 switch

17 case u2 6= nil :18 k ← calculate bound such that (48) and (49) hold19 for all w1 ≤ u1u

k2 and all w2 ∈ F

20 do if p1g(w1w2)s1 = p2h(w1w2)s2

21 then return w1w2

22 halt

23 case u2 = nil :24 for all w1 ≤ u1 and all w2 ∈ F25 do if p1g(w1w2)s1 = p2h(w1w2)s2

26 then return w1w2

27 halt

39

Page 44: Binary (Generalized) Post Correspondence Problem is in P

8 Conclusion

Previous section concludes the proof of our main result:

Theorem 26. The binary GPCP is decidable in polynomial time.

In section 4.2. we obtained a characterization of balanced morphisms, whichhave infinitely many successors with the same suffix complexity, a result worthon its own.

As we have noted, the algorithm is constructive, and therefore it allowsto output the solution which caused a “yes” answer. It is important to note,nevertheless, that the length of such a solution can be exponential, therefore insuch case it could not be written explicitly in the polynomial time. Althoughwe cannot exclude such a situation, we do not know whether it ever occurs.

It is reasonable to suppose that a modification of our algorithm could list allsolutions of the instance in a suitable way. This task is, however, beyond thescope of the present paper.

It is also not surprising that in an “average” case the decision is almosttrivial. It is open to investigation whether there are some “hard” instancesat all. In particular, one may ask whether there are some interesting successortrees, or, to put it differently, whether there exist sequences of successors leadingto the above mentioned solution of exponential length.

A similar remark is appropriate also for end blocks, the nightmare of thePCP. The estimate of the number of end blocks in Theorem 14 seems far toogenerous. In fact, we are not sure whether more than two non-equivalent endblocks can exist (see Example 1).

References

[1] W. S. Brainerd and L. H. Landweber. Theory of Computation. John Wiley& Sons, 1974.

[2] C. Choffrut and J. Karhumaki. Combinatorics of Words. In G. Rozenbergand A. Salomaa, editors, Handbook of Formal Languages, volume 1. pp.329–438, Springer-Verlag, 1997.

[3] A. Ehrenfeucht, J. Karhumaki, and G. Rozenberg. The (generalized) PostCorrespondence Problem with lists consisting of two words is decidable.Theoret. Comput. Sci., 21:119–144, 1982.

[4] V. Halava. The Post Correspondence Problem for Marked Morphisms. PhDthesis, Department of Math., Univ. of Turku. TUCS Dissertations no. 37,2002.

[5] V. Halava, T. Harju, and M. Hirvensalo. Binary (generalized) Post Corre-spondence Problem. Theoret. Comput. Sci., 276:183–204, 2002.

[6] V. Halava, T. Harju, and M. Hirvensalo. Generalized Post correspondenceproblem for marked morphisms. Internat. J. Algebra Comput., 10(6):757–772, 2000.

[7] M. A. Harrison. Introduction to Formal Language Theory. Addison-Wesley,1978.

40

Page 45: Binary (Generalized) Post Correspondence Problem is in P

[8] S. Holub, Binary equality sets are generated by two words, J. Algebra,259(1): 1–42, 2003.

[9] S. Holub, A unique structure of two-generated binary equality sets. InM. Ito and M. Toyama, editors, Developments in Language Theory, vol-ume 2450 of Lecture Notes in Computer Science, pp. 245–257. Springer,2002.

[10] J. E. Hopcroft and J. D. Ullman. Formal Languages and Their Relation toAutomata. Addison-Wesley, 1969.

[11] Y. Matiyasevich and G. Senizergues. Decision problems for semi-Thuesystems with a few rules. Theor. Comput. Sci. 330(1):145-169, 2005.

[12] V. A. Pavlenko. Post combinatorial problem with two pairs of words. Dokl.Akad. Nauk. Ukr. SSR 9–11, 1981.

[13] E. Post. A variant of a recursively unsolvable problem. Bull. of Amer.Math. Soc., 52:264–268, 1946.

41

Page 46: Binary (Generalized) Post Correspondence Problem is in P

Lemminkaisenkatu 14 A, 20520 Turku, Finland | www.tucs.fi

University of Turku

• Department of Information Technology

• Department of Mathematics

Abo Akademi University

• Department of Computer Science

• Institute for Advanced Management Systems Research

Turku School of Economics and Business Administration

• Institute of Information Systems Sciences

ISBN 952-12-1779-0

ISSN 1239-1891