<p>@MikhailRudoy was the first to show NP-hardness, but <a href="http://louis.jachiet.com/" rel="nofollow noreferrer">Louis</a> and I had a different idea, which I thought I could outline here since it works somewhat differently. 
We reduce directly from CNF-SAT, the
<a href="https://en.wikipedia.org/wiki/Boolean_satisfiability_problem" rel="nofollow noreferrer">Boolean satisfiability
problem</a> for
<a href="https://en.wikipedia.org/wiki/Conjunctive_normal_form" rel="nofollow noreferrer">CNFs</a>. In exchange for this, the regular language $L$ that we use is more complicated.</p>

<p>The key to show hardness is to design a language $L'$ that allows us to guess a word and repeat it multiple times. Specifically, for any number $k$ of variables and number
$m$ of clauses, we will build intervals that ensure that all words $w$ of $L'$
that we can form must start with an arbitrary word $u$ of length $k$ on alphabet
$\{0, 1\}$ (intuitively encoding a guess of the valuation of variables), and
then this word $u$ is repeated $m$ times (which we will later use to test that
each clause is satisfied by the guessed valuation).</p>

<p>To achieve this, we will fix the alphabet $A = \{0, 1, \#, 0', 1'\}$ and the
language: $L' := (0|1)^* (\# (00'|11')^*)^* \# (0|1)^*$. The formal claim
is a bit more complicated:</p>

<p><strong><em>Claim:</strong> For any numbers $k, m \in \mathbb{N}$, we can build in PTIME a set of
intervals such that the words in $L'$ that can be formed with these intervals are
precisely:</em></p>

<p>$$\left\{ u (\# (\tilde{u} \bowtie \tilde{u}') \# (u \bowtie u'))^m \# \tilde{u} \mid u
\in \{0, 1\}^k\right\}$$</p>

<p><em>where $\tilde{u}$ denotes the result of reversing the order of $u$ and swapping
$0$'s and $1$'s, where $u'$ denotes the result of adding a prime to all letters in
$u$, and where $x \bowtie y$ for two words $x$ of $y$ of length $p$ is the word
of length $2p$ formed by taking alternatively one letter from $x$ and one letter
from $y$.</em></p>

<p>Here's an intuitive explanation of the construction that we use to prove this.
We start with intervals that encode the initial guess of $u$. Here is the gadget
for $n = 4$ (left), and a possible solution (right):</p>

<p><a href="https://a3nm.net/share/triangle_855967.svg" rel="nofollow noreferrer"><img src="https://a3nm.net/share/triangle_855967.svg" alt="choice gadget"></a></p>

<p>It's easy to show the following observation (ignoring $L'$ for now): the possible
words that we can form with these intervals are exactly $u \# \tilde{u}$ for $u
\in \{0, 1\}^k$. This is shown essentially like the Lemma in @MikhailRudoy's
answer, by induction from the shortest intervals to the longest ones: the center
position must contain $\#$, the two neighboring positions must contain one $0$ and
one $1$, etc.</p>

<p>We have seen how to make a guess, now let's see how to duplicate it. For this, we
will rely on $L'$, and add more intervals. Here's an illustration for $k = 3$:</p>

<p><a href="https://a3nm.net/share/sync_665667.svg" rel="nofollow noreferrer"><img src="https://a3nm.net/share/sync_665667.svg" alt="duplication gadget"></a></p>

<p>For now take $L := (0|1)^* (\# (00'|11')^*)^* \# (0' | 1')^*$. Observe
how, past the first $\#$, we must enumerate alternatively an unprimed and a primed
letter. So, on the un-dashed triangle of
intervals, our observation above still stands: even though it seems like these
intervals have more space to the right of the first $\#$, only one position out of
two can be used. The same claim holds for the dashed intervals. Now, $L$ further enforces that, when we enumerate an unprimed letter, the primed letter that follows must be the same. So it is easy to see that the possible words are exactly: $u \# (\tilde{u}
\bowtie \tilde{u}') \# u'$ for $u \in \{0, 1\}^k$.</p>

<p>Now, to show the claim, we simply repeat this construction $m$ times. Here's an
example for $k=3$ and $m=2$, using now the real definition of $L'$ above the
statement of the claim:</p>

<p><a href="https://a3nm.net/share/sync3_863991.svg" rel="nofollow noreferrer"><img src="https://a3nm.net/share/sync3_863991.svg" alt="duplication gadget, repeated"></a></p>

<p>As before, we could show (by induction on $m$) that the possible words are
exactly the following: $u (\# \tilde{u} \bowtie \tilde{u}'
\# u \bowtie u')^2 \# \tilde{u}$ for $u \in \{0, 1\}^k$. So this construction
achieves what was promised by the claim.</p>

<p>Thanks to the claim we know that we can encode a guess of a valuation for the
variables, and repeat the valuation multiple times. The only missing thing is to
explain how to check that the valuation satisfies the formula. We will do this
by checking one clause per occurrence of $u$. To do this, we observe that
without loss of generality we can assume that each letter of the word is
annotated by some symbol provided as input. (More formally: we could assume that
in the problem we also provide as input a word $w$ of length $n$, and we ask
whether the intervals can form a word $u$ such that $w \bowtie u$ is in $L$.)
The reason why we can assume this is because we can double the size of each
interval, and add unit intervals (at the bottom of the picture) at odd positions
to carry the annotation of the corresponding even position:</p>

<p><a href="https://a3nm.net/share/annotate_275617.svg" rel="nofollow noreferrer"><img src="https://a3nm.net/share/annotate_275617.svg" alt="unit annotations"></a></p>

<p>Thanks to this observation, to check clauses, we will define our regular
language $L$ to be the intersection of two languages. The first language
enforces that the sub-word on even positions is a word in $L'$, i.e., if we
ignore the annotations then the word must be in $L'$, so we can just use the construction of the claim and add some annotations. The second language $L''$ will
check that the clauses are satisfied. To do this, we will add three letters in
our alphabet, to be used as annotations: $+$, $-$, and $\epsilon$. At clause $1
\leq i \leq m$, we add unit intervals to annotate by $+$ the positions in the
$i$-th repetition of $u$ corresponding to variables occurring positively in
clause $i$, and annotate by~$-$ the positions corresponding to negatively
occurring variables. We annotate everything else by~$\epsilon$. It is now clear
that $L''$ can check that the guessed valuation satisfies the formula, by
verifying that, between each pair of consecutive $\#$ symbols that contain an
occurrence of $u$ (i.e., one pair out of two), there is some literal that
satisfies the clause, i.e., there must be one occurrence of the subword $+1$ or of the subword $-0$.</p>

<p>This concludes the reduction from CNF-SAT and shows NP-hardness of the letter
scheduling problem for the language $L$.</p>

CC BY-SA 3.0
2018-03-04T21:07:54.417Z
2018-03-04T21:07:54.417Z
Antoine Amarilli 'a3nm'
<p>The problem is NP-hard for $L = A^*$ where $A$ is the finite language containing the following words:</p>

<ul>
<li>$x111$, $x000$,</li>
<li>$y100$, $y010$, $y001$,</li>
<li>$00c11$, $01c10$, $10c01$, and $11c00$</li>
</ul>

<p>The reduction is from the problem Graph Orientation, which is known to be NP-hard (see <a href="https://link.springer.com/article/10.1007/s00454-017-9884-9" rel="noreferrer">https://link.springer.com/article/10.1007/s00454-017-9884-9</a>). In this problem, we are given a 3-regular undirected graph in which every vertex is labeled either "$\{1\}$" or "$\{0,3\}$". The goal is to direct the edges of the graph so that the outdegree of every vertex is in the set labeling that vertex.</p>

<p>The reduction needs to take as input a Graph Orientation instance and produce a list of triples as output. In this reduction, the triples we output will always satisfy certain constraints. These constraints are listed below, and we will refer to a list of triples as valid if and only if they satisfy these constraints:</p>

<ul>
<li>The characters $x$, $y$, and $c$ are only given intervals containing exactly one index. In other words, whenever these characters are placed, they are placed in specific locations. </li>
<li>For every triple $(i, l, r)$ present in the instance with $i \in \{0,1\}$, the triple $(1-i, l, r)$ is also present.</li>
<li>If $(\alpha, l, r)$ and $(\alpha',l',r')$ are both triples present in the instance then either $l &lt; l' \le r' &lt; r$, or $l' &lt; l \le r &lt; r'$, or $\{\alpha,\alpha'\} = \{0,1\}$ with $l = l' &lt; r = r'$.</li>
<li>If $(\alpha, l, r)$ is a triple then the number of triples $(\alpha', l', r')$ with $l \le l' \le r' \le r$ is exactly $r-l+1$.</li>
</ul>

<p>Note the following lemma, proved at the end of this post.</p>

<p>Lemma: for a valid list of triples, the characters $x$, $y$, and $c$ must be placed exactly as indicated by the triples, and for any pair of triples $(0, l, r)$ and $(1, l, r)$, the two characters for that triple must be placed at indices $l$ and $r$.</p>

<p>Then the idea of the reduction is the following. </p>

<p>We use pairs of triples $(0, l, r)$, and $(1, l, r)$ to represent edges. The edge goes between endpoints at index $l$ and at index $r$. Assuming we produce a valid list of triples, the characters from these two triples must be placed at $l$ and $r$, so we can treat the order in which they are placed as indicating the direction of the edge. Here $1$ is the "head" of the edge and $0$ is the "tail". In other words, if the $1$ is placed at $r$ then the edge points from $l$ to $r$ and if the $1$ is placed at $l$ then the edge points from $r$ to $l$. </p>

<p>To represent vertices, we place an $x$ or $y$ character at an index and use the next three characters as the endpoints of the three edges which touch the vertex. Note that if we place an $x$, all three edges at the vertex must point in the same direction (all into the vertex or all out of the vertex) simply due to the strings that are in finite language $A$. Such vertices have outdegree $0$ or $3$, so we place an $x$ exactly for the vertices labeled $\{0,3\}$. If we place a $y$, exactly one of the three edges at the vertex must point in the same direction due to the strings in $A$. Such vertices have outdegree $1$, so we place a $y$ exactly for the vertices labeled $\{1\}$. </p>

<p>In some sense, we are done. In particular, the correspondence between solving this instance and solving the Graph Orientation instance should be clear. Unfortunately, the list of triples we produce may not be valid, and so the "edges" described may not work as intended. In particular, the list of triples might not be valid because the condition that the intervals from the triples must always contain each other might not hold: the intervals from two edges may overlap without one containing the other. </p>

<p>To combat this, we add some more infrastructure. In particular, we add "crossover vertices". A crossover vertex is a vertex of degree $4$ whose edges are paired such that within each pair one edge must point into the crossover vertex and one out. In other words, a crossover vertex will behave the same as just two "crossing" edges. We represent a crossover vertex by placing the character $c$ at some index $i$. Then note that the language $A$ constrains the characters at $i-1$ and $i+2$ to be opposite (one $0$ and one $1$) and the characters at $i-2$ and $i+1$ to be opposite. Thus, if we use these indices as the endpoints for the four edges at the crossover vertex, the behavior is exactly as described: the four edges are in pairs and among every pair one points in and one points out.</p>

<p>How do we actually place these crossovers? Well suppose we have two intervals $(l, r)$ and $(l', r')$ which overlap. WLOG, $l &lt; l' &lt; r &lt; r'$. We add the crossover character into the middle (between $l'$ and $r$). (Let's say that all along we spaced everything out so far that there's always enough space, and at the end we will remove any unused space.) Let the index of the crossover character be $i$. Then we replace the four triples $(0, l, r)$, $(1, l, r)$, $(0, l', r')$, and $(1, l', r')$ with eight triples with two each (one with character $0$ and one with character $1$) for the following four intervals $(l, i-1)$, $(i+2, r)$, $(l', i-2)$, $(i+1, r')$. Notice that the intervals don't overlap in the bad way anymore! (After this change, if two intervals overlap, one is strictly inside the other.) Furthermore, the edge from $l$ to $r$ is replaced by an edge from $l$ to the crossover vertex followed by the edge from there to $r$; these two edges are paired at the crossover vertex in such a way that one is pointed in and one is pointed out; in other words, the two edges together behave just like the one edge they are replacing.</p>

<p>In some sense, putting in this crossover vertex "uncrossed" two edges (whose intervals were overlapping). It is easy to see that adding the crossover vertex can't cause any additional edges to become crossed. Thus, we can uncross every pair of crossing edges by inserting enough crossover vertices. The end result still corresponds to the Graph Orientation instance, but now the list of triples is valid (the properties are all easy to verify now that we have "uncrossed" any crossing edges), so the lemma applies, the edges must behave as described, and the correspondence is actually an equivalence. In other words, this reduction is correct.</p>

<hr>

<h2>proof of lemma</h2>

<p>Lemma: for a valid list of triples, the characters $x$, $y$, and $c$ must be placed exactly as indicated by the triples, and for any pair of triples $(0, l, r)$ and $(1, l, r)$, the two characters for that triple must be placed at indices $l$ and $r$.</p>

<p>proof: </p>

<p>We proceed by induction on the triples by interval length. In particular, our statement is the following: for any $k$ if some triple has interval length $k$ then the character in that triple must be placed as described in the lemma.</p>

<p>Base case: for $k = 0$, the triple must be placing a character $x$, $y$, or $c$ at the single index inside the interval. This is exactly as described in the lemma.</p>

<p>Inductive case: assume the statement holds for any $k$ less than some $k'$. Now consider some triple with interval length $k'$. Then that triple must be of the form $(i, l, r)$ with $r = l+k'-1$ and $i \in \{0,1\}$. The triple $(1-i, l, r)$ must also be present. The number of triples $(\alpha',l',r')$ with $l \le l'\le r' \le r$ is exactly $r-l+1 = k'$. These triples include triples $(0, l, r)$ and $(1, l, r)$ but also $k'-2$ other triples of the form $(\alpha',l',r')$ with $l &lt; l' \le r' &lt; r$. These other triples all have interval length smaller than $k'$, so they all must place their characters as specified in the lemma. The only way for this to occur is if these triples place characters in every index starting at index $l+1$ and ending at index $r+1$. Thus, our two triples $(0, l, r)$ and $(1, l, r)$ must place their characters at indices $l$ and $r$, as described in the lemma, concluding the inductive case.</p>

<p>By induction, the lemma is correct.</p>

CC BY-SA 3.0
2018-03-04T19:20:12.527Z
Thanks a lot for this elaborate proof, and with a very simple language! I think it is correct, the only thing I'm not sure about is the claim that "adding the crossover vertex can't cause any additional edges to become crossed". Couldn't it be the case that the interval $(l, r)$ included some other interval $(l'', r'')$ with $l \leq l'' \leq r'' \leq r$, and now one of $(l, i-1)$ and $(i+2, r)$ crosses it? It seems like the process still has to converge because the intervals get smaller, but that's not completely clear either because of the insertion of crossover vertices. How should I see it?
Antoine Amarilli 'a3nm'
CC BY-SA 3.0
2018-03-05T00:08:59.133Z
If $l <l' <r < r'$, then you can insert the new indices for the new crossover vertex immediately to the right of $l'$. This causes the new indices ($i\pm$ a bit) to be in exactly those intervals that used to contain $l'$. It should be easy to see that adding a crossover vertex can add a new crossing with some other interval only if the new indices fall in the other interval. If $l' < l'' < r'' < r'$ then the new indices do not fall into the interval $(l'', r'')$. If $l < l'' < r'' < r$ then the new indices might fall into the interval $(l'', r'')$, but only if $l'$ already fell into that
Mikhail Rudoy
CC BY-SA 3.0
2018-03-05T00:12:53.777Z
(continued) interval. In this case, you aren't actually creating a new crossing, just turning an old crossing with the old interval $(l,r)$ into a new crossing with the interval $(i+\text{something}, r)$
Mikhail Rudoy
CC BY-SA 3.0
2018-03-05T16:23:29.127Z
I guess in your second message you meant "with the old interval $(l', r')$" rather than "$(l, r)$"? But OK, I see it: when you add the crossing vertex, the only bad case would be an interval $I$ that overlap with a new interval without overlapping with the corresponding interval. This cannot happen for supersets of $(l, r)$ or of $(l', r')$: if they overlap with a new interval then they overlapped with the old one. Likewise for subsets of $(l, r)$ or $(l', r')$ for the reason that you explain. So I agree that this proof looks correct to me. Thanks again!
Antoine Amarilli 'a3nm'
CC BY-SA 3.0
2018-03-03T22:18:10.100Z
2018-03-03T22:18:10.100Z
Mikhail Rudoy
<p>I fix a <a href="https://en.wikipedia.org/wiki/Regular_language" rel="nofollow noreferrer">regular language</a> $L$ on an alphabet $\Sigma$, and I consider the following problem that I call <em>letter scheduling</em> for $L$. Informally, the input gives me $n$ letters and an interval for each letter (i.e., a minimal and maximal position), and my goal is to place each letter in its interval such that no two letters are mapped to the same position and so that the resulting $n$-letter word is in $L$. Formally:</p>

<ul>
<li>Input: $n$ triples $(a_i, l_i, r_i)$ where $a_i \in \Sigma$ and $1 \leq l_i \leq r_i \leq n$ are integers</li>
<li>Output: is there a bijection $f: \{1, \ldots, n\} \to \{1, \ldots, n\}$ such that $l_i \leq f(i) \leq r_i$ for all $i$, and $a_{f^{-1}(1)} \cdots a_{f^{-1}(n)} \in L$.</li>
</ul>

<p>Obviously this problem is in NP, by guessing a bijection $f$ and checking membership to $L$ in PTIME. My question: <strong>Is there a regular language $L$ such that the letter scheduling problem for $L$ is NP-hard?</strong></p>

<p>Some initial observations:</p>

<ul>
<li>It seems that similar problems have been studied in scheduling: we could see the problem as scheduling unit-cost tasks on a single machine while respecting starting and finishing dates. However, this latter problem is obviously in PTIME with a greedy approach, and I don't see anything in the scheduling literature for the case where tasks are labeled and we would want to achieve a word in a target regular language.</li>
<li>One other way to see the problem is as a special case of a <a href="https://en.wikipedia.org/wiki/Matching_(graph_theory)#In_unweighted_bipartite_graphs" rel="nofollow noreferrer">bipartite maximum matching problem</a> (between letters and positions), but again it's hard to express the constraint that we must fall in $L$.</li>
<li>In the specific case where $L$ is a language of the form $u^*$ for some fixed word $u$ (e.g., $(ab)^*$), then the letter scheduling problem for $L$ is in PTIME with an easy greedy algorithm: build the word from left to right, and put at each position one of the available letters which is correct relative to $L$ and has the smallest time $r_i$. (If there are no available letters which are correct, fail.) However, this does not generalize to arbitrary regular languages $L$ because for such languages we may have the choice of which kind of letter to use.</li>
<li>It looks like a dynamic algorithm should work, but in fact it's not so simple: it seems that you would need to memorize which set of letters that you have taken so far. Indeed, when building a word from left to right, when you have reached a position $i$, your state depends on which letters you have consumed so far. You cannot memorize the entire set because then there would be exponentially many states. But it's not so easy to "summarize" it (e.g., by how many copies of each letter were used), because to know which copies you used, it seems that you need to remember when you have consumed them (the later you have consumed them, the more letters were available). Even with a language like $(ab|ba)^*$, there may already be complicated constraints about when you should choose to take $ab$ and when you should choose to take $ba$ depending on which letters you will need later and when the letters become available.</li>
<li>However, as the regular language $L$ is fixed and cannot memorize so much information, I'm having a hard time finding an NP-hard problem from which I could reduce.</li>
</ul>

CC BY-SA 3.0
2018-03-02T04:26:36.370Z
Can you get NP-completeness for some L in PTIME?
Lance Fortnow
CC BY-SA 3.0
2018-03-02T06:33:19.240Z
@LanceFortnow Sure. You can pad a 3CNF so that every variable occurs in even number of literals, and every two consecutive occurrences are negated. Encode $x_i$ into $0^i$ or $1^i$, then in the letter scheduling instance the symbols $(,),\wedge,\vee$ are fixed while the rest are half $0$'s and half $1$'s. In polynomial time one can check if the string encodes a padded 3CNF which evaluates to true.
Wei Zhan
CC BY-SA 3.0
2018-03-02T07:54:27.607Z
You can also generalize the problem to "arbitrary positions" (not limited to 1..n). Perhaps it's easier to prove hardness (if it's hard).
Marzio De Biasi
CC BY-SA 3.0
2018-03-02T08:45:55.307Z
@MarzioDeBiasi: I'm not sure I understand, do you mean that the position of letters could be any arbitrary subset rather than an interval? I don't know if this is hard (it starts resembling a bit the [exact perfect matching problem](https://eccc.weizmann.ac.il/report/2011/148/download/)), but the version with intervals allows for a greedy algorithm when $L= u^*$ so I have some hope that it could be easier.
Antoine Amarilli 'a3nm'
CC BY-SA 3.0
2018-03-02T09:02:06.913Z
@a3nm: no I mean that you could generalize dropping the constraint $r_i \leq n$;  you ask for a word in L in which there is at least one letter $a_i$ in the range $[l_i .. r_i]$; in other words you don't "build" the full word of length $n$, but ask for a word of arbitrary length that contains the given letters in the allowed ranges. I don't know if this changes the complexity of the problem, but in this case you must face "indexes" that possibly are not polynomially bounded by the length of the input.
Marzio De Biasi
CC BY-SA 3.0
2018-03-02T10:00:27.770Z
@Marzio: I don't think that this changes the problem. Suppose that $r_i-l_i\ge n$ for some $i$. Then you can omit $i$, solve the problem for the remaining $n-1$ letters and reinsert $i$ to an empty place. If this reduction doesn't work, $r_i-l_i< n$ for all $i$. Also, there cannot be an $I$ such that $r_i<l_j$ for all $i\in I, j\notin I$, as we could again reduce the problem. Combining these two observations, we get that all $r_i<n^2$, and probably a more careful analysis would even prove $r_i\le n$.
domotorp
<cc.complexity-theory><np-hardness><automata-theory><regular-language><scheduling>
Testing whether letters can be scheduled to achieve a word in a regular language
Antoine Amarilli 'a3nm'
# Testing whether letters can be scheduled to achieve a word in a regular language

(Asked by: username_0 on 2018-03-01)

I fix a [regular language](https://en.wikipedia.org/wiki/Regular_language) $L$ on an alphabet $\Sigma$, and I consider the following problem that I call *letter scheduling* for $L$. Informally, the input gives me $n$ letters and an interval for each letter (i.e., a minimal and maximal position), and my goal is to place each letter in its interval such that no two letters are mapped to the same position and so that the resulting $n$-letter word is in $L$. Formally:

* Input: $n$ triples $(a\_i, l\_i, r\_i)$ where $a\_i \in \Sigma$ and $1 \leq l\_i \leq r\_i \leq n$ are integers
* Output: is there a bijection $f: \{1, \ldots, n\} \to \{1, \ldots, n\}$ such that $l\_i \leq f(i) \leq r\_i$ for all $i$, and $a\_{f^{-1}(1)} \cdots a\_{f^{-1}(n)} \in L$.

Obviously this problem is in NP, by guessing a bijection $f$ and checking membership to $L$ in PTIME. My question: **Is there a regular language $L$ such that the letter scheduling problem for $L$ is NP-hard?**

Some initial observations:

* It seems that similar problems have been studied in scheduling: we could see the problem as scheduling unit-cost tasks on a single machine while respecting starting and finishing dates. However, this latter problem is obviously in PTIME with a greedy approach, and I don't see anything in the scheduling literature for the case where tasks are labeled and we would want to achieve a word in a target regular language.
* One other way to see the problem is as a special case of a [bipartite maximum matching problem](https://en.wikipedia.org/wiki/Matching_(graph_theory)#In_unweighted_bipartite_graphs) (between letters and positions), but again it's hard to express the constraint that we must fall in $L$.
* In the specific case where $L$ is a language of the form $u^\*$ for some fixed word $u$ (e.g., $(ab)^\*$), then the letter scheduling problem for $L$ is in PTIME with an easy greedy algorithm: build the word from left to right, and put at each position one of the available letters which is correct relative to $L$ and has the smallest time $r\_i$. (If there are no available letters which are correct, fail.) However, this does not generalize to arbitrary regular languages $L$ because for such languages we may have the choice of which kind of letter to use.
* It looks like a dynamic algorithm should work, but in fact it's not so simple: it seems that you would need to memorize which set of letters that you have taken so far. Indeed, when building a word from left to right, when you have reached a position $i$, your state depends on which letters you have consumed so far. You cannot memorize the entire set because then there would be exponentially many states. But it's not so easy to "summarize" it (e.g., by how many copies of each letter were used), because to know which copies you used, it seems that you need to remember when you have consumed them (the later you have consumed them, the more letters were available). Even with a language like $(ab|ba)^\*$, there may already be complicated constraints about when you should choose to take $ab$ and when you should choose to take $ba$ depending on which letters you will need later and when the letters become available.
* However, as the regular language $L$ is fixed and cannot memorize so much information, I'm having a hard time finding an NP-hard problem from which I could reduce.

**Tags:** cc.complexity-theory, np-hardness, automata-theory, regular-language, scheduling
> **Comments:**
> **username_1**: Can you get NP-completeness for some L in PTIME?  
> **username_2**: @LanceFortnow Sure. You can pad a 3CNF so that every variable occurs in even number of literals, and every two consecutive occurrences are negated. Encode $x_i$ into $0^i$ or $1^i$, then in the letter scheduling instance the symbols $(,),\wedge,\vee$ are fixed while the rest are half $0$'s and half $1$'s. In polynomial time one can check if the string encodes a padded 3CNF which evaluates to true.  
> **username_3**: You can also generalize the problem to "arbitrary positions" (not limited to 1..n). Perhaps it's easier to prove hardness (if it's hard).  
> **username_0**: @MarzioDeBiasi: I'm not sure I understand, do you mean that the position of letters could be any arbitrary subset rather than an interval? I don't know if this is hard (it starts resembling a bit the [exact perfect matching problem](https://eccc.weizmann.ac.il/report/2011/148/download/)), but the version with intervals allows for a greedy algorithm when $L= u^*$ so I have some hope that it could be easier.  
> **username_3**: @a3nm: no I mean that you could generalize dropping the constraint $r_i \leq n$;  you ask for a word in L in which there is at least one letter $a_i$ in the range $[l_i .. r_i]$; in other words you don't "build" the full word of length $n$, but ask for a word of arbitrary length that contains the given letters in the allowed ranges. I don't know if this changes the complexity of the problem, but in this case you must face "indexes" that possibly are not polynomially bounded by the length of the input.  
> **username_4**: @Marzio: I don't think that this changes the problem. Suppose that $r_i-l_i\ge n$ for some $i$. Then you can omit $i$, solve the problem for the remaining $n-1$ letters and reinsert $i$ to an empty place. If this reduction doesn't work, $r_i-l_i< n$ for all $i$. Also, there cannot be an $I$ such that $r_i<l_j$ for all $i\in I, j\notin I$, as we could again reduce the problem. Combining these two observations, we get that all $r_i<n^2$, and probably a more careful analysis would even prove $r_i\le n$.  

---

## 2 answers

### Answer by username_5 on 2018-03-03. Score: 7 | Accepted answer

The problem is NP-hard for $L = A^\*$ where $A$ is the finite language containing the following words:

* $x111$, $x000$,
* $y100$, $y010$, $y001$,
* $00c11$, $01c10$, $10c01$, and $11c00$

The reduction is from the problem Graph Orientation, which is known to be NP-hard (see <https://link.springer.com/article/10.1007/s00454-017-9884-9>). In this problem, we are given a 3-regular undirected graph in which every vertex is labeled either "$\{1\}$" or "$\{0,3\}$". The goal is to direct the edges of the graph so that the outdegree of every vertex is in the set labeling that vertex.

The reduction needs to take as input a Graph Orientation instance and produce a list of triples as output. In this reduction, the triples we output will always satisfy certain constraints. These constraints are listed below, and we will refer to a list of triples as valid if and only if they satisfy these constraints:

* The characters $x$, $y$, and $c$ are only given intervals containing exactly one index. In other words, whenever these characters are placed, they are placed in specific locations.
* For every triple $(i, l, r)$ present in the instance with $i \in \{0,1\}$, the triple $(1-i, l, r)$ is also present.
* If $(\alpha, l, r)$ and $(\alpha',l',r')$ are both triples present in the instance then either $l < l' \le r' < r$, or $l' < l \le r < r'$, or $\{\alpha,\alpha'\} = \{0,1\}$ with $l = l' < r = r'$.
* If $(\alpha, l, r)$ is a triple then the number of triples $(\alpha', l', r')$ with $l \le l' \le r' \le r$ is exactly $r-l+1$.

Note the following lemma, proved at the end of this post.

Lemma: for a valid list of triples, the characters $x$, $y$, and $c$ must be placed exactly as indicated by the triples, and for any pair of triples $(0, l, r)$ and $(1, l, r)$, the two characters for that triple must be placed at indices $l$ and $r$.

Then the idea of the reduction is the following.

We use pairs of triples $(0, l, r)$, and $(1, l, r)$ to represent edges. The edge goes between endpoints at index $l$ and at index $r$. Assuming we produce a valid list of triples, the characters from these two triples must be placed at $l$ and $r$, so we can treat the order in which they are placed as indicating the direction of the edge. Here $1$ is the "head" of the edge and $0$ is the "tail". In other words, if the $1$ is placed at $r$ then the edge points from $l$ to $r$ and if the $1$ is placed at $l$ then the edge points from $r$ to $l$.

To represent vertices, we place an $x$ or $y$ character at an index and use the next three characters as the endpoints of the three edges which touch the vertex. Note that if we place an $x$, all three edges at the vertex must point in the same direction (all into the vertex or all out of the vertex) simply due to the strings that are in finite language $A$. Such vertices have outdegree $0$ or $3$, so we place an $x$ exactly for the vertices labeled $\{0,3\}$. If we place a $y$, exactly one of the three edges at the vertex must point in the same direction due to the strings in $A$. Such vertices have outdegree $1$, so we place a $y$ exactly for the vertices labeled $\{1\}$.

In some sense, we are done. In particular, the correspondence between solving this instance and solving the Graph Orientation instance should be clear. Unfortunately, the list of triples we produce may not be valid, and so the "edges" described may not work as intended. In particular, the list of triples might not be valid because the condition that the intervals from the triples must always contain each other might not hold: the intervals from two edges may overlap without one containing the other.

To combat this, we add some more infrastructure. In particular, we add "crossover vertices". A crossover vertex is a vertex of degree $4$ whose edges are paired such that within each pair one edge must point into the crossover vertex and one out. In other words, a crossover vertex will behave the same as just two "crossing" edges. We represent a crossover vertex by placing the character $c$ at some index $i$. Then note that the language $A$ constrains the characters at $i-1$ and $i+2$ to be opposite (one $0$ and one $1$) and the characters at $i-2$ and $i+1$ to be opposite. Thus, if we use these indices as the endpoints for the four edges at the crossover vertex, the behavior is exactly as described: the four edges are in pairs and among every pair one points in and one points out.

How do we actually place these crossovers? Well suppose we have two intervals $(l, r)$ and $(l', r')$ which overlap. WLOG, $l < l' < r < r'$. We add the crossover character into the middle (between $l'$ and $r$). (Let's say that all along we spaced everything out so far that there's always enough space, and at the end we will remove any unused space.) Let the index of the crossover character be $i$. Then we replace the four triples $(0, l, r)$, $(1, l, r)$, $(0, l', r')$, and $(1, l', r')$ with eight triples with two each (one with character $0$ and one with character $1$) for the following four intervals $(l, i-1)$, $(i+2, r)$, $(l', i-2)$, $(i+1, r')$. Notice that the intervals don't overlap in the bad way anymore! (After this change, if two intervals overlap, one is strictly inside the other.) Furthermore, the edge from $l$ to $r$ is replaced by an edge from $l$ to the crossover vertex followed by the edge from there to $r$; these two edges are paired at the crossover vertex in such a way that one is pointed in and one is pointed out; in other words, the two edges together behave just like the one edge they are replacing.

In some sense, putting in this crossover vertex "uncrossed" two edges (whose intervals were overlapping). It is easy to see that adding the crossover vertex can't cause any additional edges to become crossed. Thus, we can uncross every pair of crossing edges by inserting enough crossover vertices. The end result still corresponds to the Graph Orientation instance, but now the list of triples is valid (the properties are all easy to verify now that we have "uncrossed" any crossing edges), so the lemma applies, the edges must behave as described, and the correspondence is actually an equivalence. In other words, this reduction is correct.

---

## proof of lemma

Lemma: for a valid list of triples, the characters $x$, $y$, and $c$ must be placed exactly as indicated by the triples, and for any pair of triples $(0, l, r)$ and $(1, l, r)$, the two characters for that triple must be placed at indices $l$ and $r$.

proof:

We proceed by induction on the triples by interval length. In particular, our statement is the following: for any $k$ if some triple has interval length $k$ then the character in that triple must be placed as described in the lemma.

Base case: for $k = 0$, the triple must be placing a character $x$, $y$, or $c$ at the single index inside the interval. This is exactly as described in the lemma.

Inductive case: assume the statement holds for any $k$ less than some $k'$. Now consider some triple with interval length $k'$. Then that triple must be of the form $(i, l, r)$ with $r = l+k'-1$ and $i \in \{0,1\}$. The triple $(1-i, l, r)$ must also be present. The number of triples $(\alpha',l',r')$ with $l \le l'\le r' \le r$ is exactly $r-l+1 = k'$. These triples include triples $(0, l, r)$ and $(1, l, r)$ but also $k'-2$ other triples of the form $(\alpha',l',r')$ with $l < l' \le r' < r$. These other triples all have interval length smaller than $k'$, so they all must place their characters as specified in the lemma. The only way for this to occur is if these triples place characters in every index starting at index $l+1$ and ending at index $r+1$. Thus, our two triples $(0, l, r)$ and $(1, l, r)$ must place their characters at indices $l$ and $r$, as described in the lemma, concluding the inductive case.

By induction, the lemma is correct.

> **Comments:**
> **username_0**: Thanks a lot for this elaborate proof, and with a very simple language! I think it is correct, the only thing I'm not sure about is the claim that "adding the crossover vertex can't cause any additional edges to become crossed". Couldn't it be the case that the interval $(l, r)$ included some other interval $(l'', r'')$ with $l \leq l'' \leq r'' \leq r$, and now one of $(l, i-1)$ and $(i+2, r)$ crosses it? It seems like the process still has to converge because the intervals get smaller, but that's not completely clear either because of the insertion of crossover vertices. How should I see it?  
> **username_5**: If $l <l' <r < r'$, then you can insert the new indices for the new crossover vertex immediately to the right of $l'$. This causes the new indices ($i\pm$ a bit) to be in exactly those intervals that used to contain $l'$. It should be easy to see that adding a crossover vertex can add a new crossing with some other interval only if the new indices fall in the other interval. If $l' < l'' < r'' < r'$ then the new indices do not fall into the interval $(l'', r'')$. If $l < l'' < r'' < r$ then the new indices might fall into the interval $(l'', r'')$, but only if $l'$ already fell into that  
> **username_5**: (continued) interval. In this case, you aren't actually creating a new crossing, just turning an old crossing with the old interval $(l,r)$ into a new crossing with the interval $(i+\text{something}, r)$  
> **username_0**: I guess in your second message you meant "with the old interval $(l', r')$" rather than "$(l, r)$"? But OK, I see it: when you add the crossing vertex, the only bad case would be an interval $I$ that overlap with a new interval without overlapping with the corresponding interval. This cannot happen for supersets of $(l, r)$ or of $(l', r')$: if they overlap with a new interval then they overlapped with the old one. Likewise for subsets of $(l, r)$ or $(l', r')$ for the reason that you explain. So I agree that this proof looks correct to me. Thanks again!  

---

### Answer by username_0 on 2018-03-04. Score: 2

@MikhailRudoy was the first to show NP-hardness, but [Louis](http://louis.jachiet.com/) and I had a different idea, which I thought I could outline here since it works somewhat differently.
We reduce directly from CNF-SAT, the
[Boolean satisfiability
problem](https://en.wikipedia.org/wiki/Boolean_satisfiability_problem) for
[CNFs](https://en.wikipedia.org/wiki/Conjunctive_normal_form). In exchange for this, the regular language $L$ that we use is more complicated.

The key to show hardness is to design a language $L'$ that allows us to guess a word and repeat it multiple times. Specifically, for any number $k$ of variables and number
$m$ of clauses, we will build intervals that ensure that all words $w$ of $L'$
that we can form must start with an arbitrary word $u$ of length $k$ on alphabet
$\{0, 1\}$ (intuitively encoding a guess of the valuation of variables), and
then this word $u$ is repeated $m$ times (which we will later use to test that
each clause is satisfied by the guessed valuation).

To achieve this, we will fix the alphabet $A = \{0, 1, \#, 0', 1'\}$ and the
language: $L' := (0|1)^\* (\# (00'|11')^\*)^\* \# (0|1)^\*$. The formal claim
is a bit more complicated:

***Claim:*** For any numbers $k, m \in \mathbb{N}$, we can build in PTIME a set of
intervals such that the words in $L'$ that can be formed with these intervals are
precisely:

$$\left\{ u (\# (\tilde{u} \bowtie \tilde{u}') \# (u \bowtie u'))^m \# \tilde{u} \mid u
\in \{0, 1\}^k\right\}$$

*where $\tilde{u}$ denotes the result of reversing the order of $u$ and swapping
$0$'s and $1$'s, where $u'$ denotes the result of adding a prime to all letters in
$u$, and where $x \bowtie y$ for two words $x$ of $y$ of length $p$ is the word
of length $2p$ formed by taking alternatively one letter from $x$ and one letter
from $y$.*

Here's an intuitive explanation of the construction that we use to prove this.
We start with intervals that encode the initial guess of $u$. Here is the gadget
for $n = 4$ (left), and a possible solution (right):

[![choice gadget](https://a3nm.net/share/triangle_855967.svg)](https://a3nm.net/share/triangle_855967.svg)

It's easy to show the following observation (ignoring $L'$ for now): the possible
words that we can form with these intervals are exactly $u \# \tilde{u}$ for $u
\in \{0, 1\}^k$. This is shown essentially like the Lemma in @MikhailRudoy's
answer, by induction from the shortest intervals to the longest ones: the center
position must contain $\#$, the two neighboring positions must contain one $0$ and
one $1$, etc.

We have seen how to make a guess, now let's see how to duplicate it. For this, we
will rely on $L'$, and add more intervals. Here's an illustration for $k = 3$:

[![duplication gadget](https://a3nm.net/share/sync_665667.svg)](https://a3nm.net/share/sync_665667.svg)

For now take $L := (0|1)^\* (\# (00'|11')^\*)^\* \# (0' | 1')^\*$. Observe
how, past the first $\#$, we must enumerate alternatively an unprimed and a primed
letter. So, on the un-dashed triangle of
intervals, our observation above still stands: even though it seems like these
intervals have more space to the right of the first $\#$, only one position out of
two can be used. The same claim holds for the dashed intervals. Now, $L$ further enforces that, when we enumerate an unprimed letter, the primed letter that follows must be the same. So it is easy to see that the possible words are exactly: $u \# (\tilde{u}
\bowtie \tilde{u}') \# u'$ for $u \in \{0, 1\}^k$.

Now, to show the claim, we simply repeat this construction $m$ times. Here's an
example for $k=3$ and $m=2$, using now the real definition of $L'$ above the
statement of the claim:

[![duplication gadget, repeated](https://a3nm.net/share/sync3_863991.svg)](https://a3nm.net/share/sync3_863991.svg)

As before, we could show (by induction on $m$) that the possible words are
exactly the following: $u (\# \tilde{u} \bowtie \tilde{u}'
\# u \bowtie u')^2 \# \tilde{u}$ for $u \in \{0, 1\}^k$. So this construction
achieves what was promised by the claim.

Thanks to the claim we know that we can encode a guess of a valuation for the
variables, and repeat the valuation multiple times. The only missing thing is to
explain how to check that the valuation satisfies the formula. We will do this
by checking one clause per occurrence of $u$. To do this, we observe that
without loss of generality we can assume that each letter of the word is
annotated by some symbol provided as input. (More formally: we could assume that
in the problem we also provide as input a word $w$ of length $n$, and we ask
whether the intervals can form a word $u$ such that $w \bowtie u$ is in $L$.)
The reason why we can assume this is because we can double the size of each
interval, and add unit intervals (at the bottom of the picture) at odd positions
to carry the annotation of the corresponding even position:

[![unit annotations](https://a3nm.net/share/annotate_275617.svg)](https://a3nm.net/share/annotate_275617.svg)

Thanks to this observation, to check clauses, we will define our regular
language $L$ to be the intersection of two languages. The first language
enforces that the sub-word on even positions is a word in $L'$, i.e., if we
ignore the annotations then the word must be in $L'$, so we can just use the construction of the claim and add some annotations. The second language $L''$ will
check that the clauses are satisfied. To do this, we will add three letters in
our alphabet, to be used as annotations: $+$, $-$, and $\epsilon$. At clause $1
\leq i \leq m$, we add unit intervals to annotate by $+$ the positions in the
$i$-th repetition of $u$ corresponding to variables occurring positively in
clause $i$, and annotate by~$-$ the positions corresponding to negatively
occurring variables. We annotate everything else by~$\epsilon$. It is now clear
that $L''$ can check that the guessed valuation satisfies the formula, by
verifying that, between each pair of consecutive $\#$ symbols that contain an
occurrence of $u$ (i.e., one pair out of two), there is some literal that
satisfies the clause, i.e., there must be one occurrence of the subword $+1$ or of the subword $-0$.

This concludes the reduction from CNF-SAT and shows NP-hardness of the letter
scheduling problem for the language $L$.


---

