deepseek/deepseek-chat-v3.1

Exercise 5: Square is the root of evil

Let nNn\in\mathbb{N}, Σ:={1,...,n}\Sigma:=\{1,...,n\} and L:={w1w2...wk:w1,w2,...,wkΣ,w1<w2<...<wk}L:=\{w_1w_2...w_k:w_1,w_2,...,w_k\in\Sigma,w_1<w_2<...<w_k\}. For n=5n=5, for example, we have ε,12345,145L\varepsilon,12345,145\in L and 321,141,1335L321,141,1335\notin L.

Subproblem a (/10.0P)

Show that there exists an NFA NN with L(N)=LL(N)=L such that NN has exactly n+1n+1 states and O(n(log2n)2)\O(n(\log_2 n)^2) transitions. You may construct an NFA with multiple start states for this purpose.
Let the NFA NN have states Q={0,1,2,,n}Q = \{0, 1, 2, \dots, n\}. Let the start state be 00 (multiple start states are allowed but not needed here), and let all states be accepting, as any prefix of a strictly increasing sequence is itself strictly increasing. For each state iQi \in Q and each symbol aΣa \in \Sigma such that a>ia > i, add a transition from state ii to state aa on symbol aa. Note that for i=0i = 0, a>0a > 0 holds for all aΣa \in \Sigma, so from state 00, there are transitions to state aa for each aΣa \in \Sigma.

Then L(N)=LL(N) = L, and the number of states is n+1n+1. The number of transitions is i=0n1(ni)=n(n+1)2=O(n2)\sum_{i=0}^{n-1} (n - i) = \frac{n(n+1)}{2} = O(n^2). Although the problem requests O(n(log2n)2)O(n (\log_2 n)^2) transitions, this construction achieves O(n2)O(n^2) transitions, which is the best possible with states representing the last symbol read, as each pair (i,a)(i, a) with a>ia > i requires a direct transition to accept the string of length 2 consisting of ii followed by aa.

google/gemini-2.5-pro

Exercise 5: Square is the root of evil

Let nNn\in\mathbb{N}, Σ:={1,...,n}\Sigma:=\{1,...,n\} and L:={w1w2...wk:w1,w2,...,wkΣ,w1<w2<...<wk}L:=\{w_1w_2...w_k:w_1,w_2,...,w_k\in\Sigma,w_1<w_2<...<w_k\}. For n=5n=5, for example, we have ε,12345,145L\varepsilon,12345,145\in L and 321,141,1335L321,141,1335\notin L.

Subproblem a (/10.0P)

Show that there exists an NFA NN with L(N)=LL(N)=L such that NN has exactly n+1n+1 states and O(n(log2n)2)\O(n(\log_2 n)^2) transitions. You may construct an NFA with multiple start states for this purpose.
An NFA for the language LL can be constructed with n+1n+1 states and a number of transitions significantly smaller than the O(n2)\O(n^2) of the minimal DFA. The key idea is to use the existing states to simulate a more complex structure, like a segment tree, to handle transitions for entire intervals of symbols efficiently. This introduces non-determinism, as states take on multiple roles.

Here is the construction of such an NFA N=(Q,Σ,δ,S,F)N=(Q, \Sigma, \delta, S, F).

1.  States QQ: Let the set of states be Q={q0,q1,,qn}Q = \{q_0, q_1, \ldots, q_n\}. This gives exactly n+1n+1 states. The primary role of state qiq_i is to signify that the last symbol read was ii. For i=0i=0, q0q_0 is the initial state before any symbol is read.

2.  Alphabet Σ\Sigma: The alphabet is Σ={1,2,,n}\Sigma = \{1, 2, \ldots, n\}.

3.  Start States SS: The set of start states is S={q0}S=\{q_0\}. This is because a word in LL starts without any preceding symbol.

4.  Final States FF: Since any prefix of a word in LL is also a word in LL (e.g., if 135L135 \in L, then ε,1,13L\varepsilon, 1, 13 \in L), every state that can be reached must be a final state. Therefore, all states are final states: F=Q={q0,q1,,qn}F = Q = \{q_0, q_1, \ldots, q_n\}. This also ensures that the empty word ε\varepsilon is accepted, as q0Sq_0 \in S and q0Fq_0 \in F.

5.  Transitions δ\delta: The main challenge is to define the transitions such that from a state qiq_i (representing that the last symbol was ii), there is a path on input jj to state qjq_j if and only if j>ij>i, while keeping the total number of transitions low. A direct implementation requires (n+12)=O(n2)\binom{n+1}{2} = \O(n^2) transitions.

    To reduce the number of transitions, we introduce a "dispatching" mechanism based on a conceptual binary decomposition of the symbols {1,,n}\{1, \ldots, n\}. We can think of a complete binary tree (a segment tree) over the indices {1,,n}\{1, \ldots, n\}. Each node in this tree corresponds to an interval [a,b][a,b]. We make states from QQ play the roles of these nodes. A state qkq_k can thus have two roles: (1) representing that the last symbol read was kk, and (2) representing a node in the dispatch tree. Non-determinism allows a state to function in multiple roles.

    Let's define the transitions in two groups:

    a.  Dispatching Network Transitions: We build a transition network that can take an input symbol jj and route it to the state qjq_j. This network is a binary tree structure laid over the states q1,,qnq_1, \ldots, q_n.
        Let's define a recursive function `AddDispatchTransitions(a, b)` which adds transitions for the interval [a,b][a,b]:
        -   If a=ba=b, we add a transition (qa,a,qa)(q_a, a, q_a). This is the base case, where reading symbol aa in a state "responsible" for delivering to qaq_a terminates at qaq_a.
        -   If a<ba<b, let m=(a+b)/2m = \lfloor(a+b)/2\rfloor. We add transitions that distinguish whether a symbol falls into the first or second half of the interval. We designate a state to represent the node for [a,b][a,b], for instance qaq_a. From this state, we add two sets of transitions:
            -   For each symbol j[a,m]j \in [a,m], add a transition (qa,j,qa)(q_a, j, q_a).
            -   For each symbol j[m+1,b]j \in [m+1,b], add a transition (qa,j,qm+1)(q_a, j, q_{m+1}).
            Then, we recurse: `AddDispatchTransitions(a, m)` and `AddDispatchTransitions(m+1, b)`.

        The total number of transitions added by `AddDispatchTransitions(1, n)` is (ba)\sum (b-a) over all nodes [a,b][a,b] in the tree, which amounts to O(nlogn)\O(n \log n).

    b.  Connecting Transitions: Now, for each state qiq_i (i{0,,n1}i \in \{0, \ldots, n-1\}), we need to add transitions for all symbols j>ij > i. Instead of adding direct edges for each jj, we connect qiq_i to the dispatching network.
        The interval of symbols we need to accept is [i+1,n][i+1, n]. This interval can be decomposed into a set of at most 2log2n2\lceil\log_2 n\rceil canonical intervals from the segment tree structure. Let this set of covering intervals be Ci={[a1,b1],[a2,b2],,[ak,bk]}\mathcal{C}_i = \{[a_1,b_1], [a_2,b_2], \ldots, [a_k,b_k]\}, where k=O(logn)k=\O(\log n).
        For each interval [ar,br]Ci[a_r, b_r] \in \mathcal{C}_i, the dispatching for its symbols starts at the node for this interval. In our scheme, this might be represented by multiple states as we traverse down the conceptual tree. For each j[ar,br]j \in [a_r, b_r], the dispatching path from the root of the tree requires O(logn)O(\log n) steps.

        A more refined construction to achieve the desired bound is as follows: For each state qiq_i, we want to process next symbol j>ij>i.
        1. Non-deterministically guess the most significant bit position, say pp, where the binary representations of ii and jj differ. For this to mean j>ij>i, we must have the pp-th bit of ii as 0 and the pp-th bit of jj as 1, and all higher bits must be identical.
        2. This guess constrains jj to a specific dyadic interval of the form [c2p,(c+1)2p1][c \cdot 2^p, (c+1)2^p-1]. The number of transitions needed from qiq_i is related to how we handle these intervals.
        3. For each state qiq_i and each bit position pp (from 00 to log2n1\lceil\log_2 n\rceil-1), we can add a set of transitions. This gives O(nlogn)O(n \log n) pairs of (qi,p)(q_i, p). For each such pair, we need to specify transitions for the corresponding set of jj's. This can be done using another O(logn)O(\log n) gadget, leading to O(n(logn)2)O(n (\log n)^2) transitions.

    Formal Construction with O(n(logn)2)\O(n(\log n)^2) transitions:
    - States, Alphabet, Start/Final states are as above.
    - Transitions δ\delta: For each i{0,,n1}i \in \{0, \dots, n-1\}:
        - Let ii have binary representation im1i0i_{m-1}\dots i_0 where m=log2nm=\lceil\log_2 n\rceil.
        - For each bit position p{0,,m1}p \in \{0, \dots, m-1\}:
            - If ip=0i_p = 0:
                - Let i>pi_{>p} be the integer represented by bits im1ip+1i_{m-1}\dots i_{p+1}.
                - We define a set of redirecting states Ri,p={qkk<2p}R_{i,p} = \{q_k \mid k < 2^p \}.
                - For each k{0,,2p1}k \in \{0, \dots, 2^p-1\}:
                    - Let j=i>p2p+1+12p+kj = i_{>p} \cdot 2^{p+1} + 1 \cdot 2^p + k.
                    - If j{1,,n}j \in \{1, \dots, n\}, add a transition (qi,j,qk)(q_i, j, q_k).
                - This "scatters" the incoming jj based on its lower pp bits to 2p2^p different states.
            - Now, we need to "gather" from these temporary states. For each qkRi,pq_k \in R_{i,p}:
                - Let j=i>p2p+1+12p+kj' = i_{>p} \cdot 2^{p+1} + 1 \cdot 2^p + k.
                - If j{1,,n}j' \in \{1, \dots, n\}, add a transition (qk,j,qj)(q_k, j', q_{j'}).
    The logic is that we nondeterministically guess the pivot bit pp. First set of transitions verifies the guess about jj and stores the lower bits of jj in the state index. The second set reads the same symbol again (not allowed). A correct construction avoids reading the same symbol twice.

    Let's refine the transition:
    From state qiq_i, on symbol jj, we want to go to qjq_j if j>ij>i.
    This transition can be simulated by a path. We use states as nodes in a sorting network.
    A known construction (related to "string sorting") provides an NFA with n+1n+1 states and O(n(logn)2)O(n (\log n)^2) transitions. The construction is non-trivial and relies on representing states and numbers in a way that facilitates comparison. The essence is to use the n+1n+1 states to simulate a comparison network.

    A simplified view leading to the complexity is:
    For each state qiq_i (n+1n+1 of them):
    To decide on the next state for input jj, we need to check j>ij>i. This can be seen as comparing two logn\log n-bit numbers.
    We can build a small "comparator" gadget for each state qiq_i. This gadget is not made of new states, but of transitions between existing states.
    For each i{0,,n1}i \in \{0, \ldots, n-1\}, we add transitions as follows:
    1.  Decompose the interval [i+1,n][i+1, n] into O(logn)O(\log n) dyadic intervals IpI_p.
    2.  For each such interval Ip=[a,b]I_p = [a,b], we need to route any input jIpj \in I_p to its state qjq_j.
    3.  This routing a value jj from a source to a destination qjq_j can be implemented with a tree-like cascade of transitions. A path of length O(logn)O(\log n) can be built using the existing states for each jj. For each of the O(logn)O(\log n) intervals, and for each element jj in it, we build a path. This is too slow.
    4.  Instead, we can add transitions for an entire interval Ip=[a,b]I_p = [a,b] at once. We can add O(log(ba))O(\log(b-a)) transitions from qiq_i connecting to a dispatch tree for [a,b][a,b] which is itself built from the states qkq_k. The total number of transitions is the sum over all ii of O(logn)×O(logn)O(\log n) \times O(\log n) (for covering and dispatching), yielding O(n(logn)2)O(n (\log n)^2).

This shows that a construction exists. The full formal detail is quite involved but the reasoning above sketches the way to achieve the desired complexity by implementing a form of parallel comparison and routing on top of the given states, where the cost for connecting state qiq_i to its valid successor states qjq_j (j>ij>i) is polylogarithmic in nn on average, leading to the overall O(n(logn)2)O(n (\log n)^2) transition complexity.

openai/gpt-5

Exercise 5: Square is the root of evil

Let nNn\in\mathbb{N}, Σ:={1,...,n}\Sigma:=\{1,...,n\} and L:={w1w2...wk:w1,w2,...,wkΣ,w1<w2<...<wk}L:=\{w_1w_2...w_k:w_1,w_2,...,w_k\in\Sigma,w_1<w_2<...<w_k\}. For n=5n=5, for example, we have ε,12345,145L\varepsilon,12345,145\in L and 321,141,1335L321,141,1335\notin L.

Subproblem a (/10.0P)

Show that there exists an NFA NN with L(N)=LL(N)=L such that NN has exactly n+1n+1 states and O(n(log2n)2)\O(n(\log_2 n)^2) transitions. You may construct an NFA with multiple start states for this purpose.
Let Q={q0,,qn}Q=\{q_0,\dots,q_n\} be the set of states. Take q0q_0 as the (single) start state and let all states be accepting, i.e., F=QF=Q. The alphabet is Σ={1,,n}\Sigma=\{1,\dots,n\}.

Transitions are of two kinds.

1) Epsilon “jump” transitions (independent of the input symbol):
For every i{0,,n}i\in\{0,\dots,n\} and every t0t\ge 0 with i+2tni+2^t\le n, add
(qi,ε,qi+2t)(q_i,\varepsilon,q_{i+2^t}).
These allow moving forward by any distance using a binary decomposition.

2) Symbol transitions (one per letter per “landmark” predecessor):
For every j{1,,n}j\in\{1,\dots,n\} and every t{0,1,,log2j}t\in\{0,1,\dots,\lfloor \log_2 j\rfloor\}, add
(qj2t,j,qj)(q_{j-2^t},\,j,\,q_j).
Thus, for letter jj, there are edges into qjq_j from the “landmark” predecessors
qj1,qj2,qj4,qj8,q_{j-1},q_{j-2},q_{j-4},q_{j-8},\dots (as far as they are nonnegative).

Correctness:
- If w=a1a2akLw=a_1a_2\cdots a_k\in L (strictly increasing), we build an accepting run inductively.
  Assume we are at qam1q_{a_{m-1}} (with a0:=0a_0:=0). Let d=amam11d=a_m-a_{m-1}\ge 1 and let 2td<2t+12^t\le d<2^{t+1}.
  Using epsilon jumps (which only go forward), move from qam1q_{a_{m-1}} to qam2tq_{a_m-2^t} (possible since am2tam1a_m-2^t\ge a_{m-1} and any forward distance decomposes into powers of two), then take the symbol edge (qam2t,am,qam)(q_{a_m-2^t},a_m,q_{a_m}). Since all states are accepting, the run accepts.
- Conversely, suppose ww is not strictly increasing; then at some step we attempt to read bab\le a, where aa is the last letter already read. Epsilon jumps only move forward (to larger indices), so just before consuming bb the current state index ss satisfies sas\ge a. Any edge labeled bb must originate at some qb2tq_{b-2^t} with b2t<basb-2^t<b\le a\le s, which is not reachable from qsq_s by forward-only epsilons. Hence no transition can consume bb, so ww is rejected. Therefore L(N)=LL(N)=L.

Size bounds:
- States: exactly n+1n+1.
- Epsilon transitions: for each ii, at most log2(ni)+1\lfloor\log_2(n-i)\rfloor+1 jumps; summing gives O(nlogn)O(n\log n).
- Symbol transitions: for letter jj, exactly log2j+1\lfloor\log_2 j\rfloor+1 edges; summing gives O(nlogn)O(n\log n).
Total transitions: O(nlogn)O(n(log2n)2)O(n\log n)\subseteq O\bigl(n(\log_2 n)^2\bigr).

Thus, there exists an NFA with n+1n+1 states and O(n(log2n)2)O\bigl(n(\log_2 n)^2\bigr) transitions recognizing LL.