Part II · Grammar as a Computational Object

Chapter 8

Admissibility Languages and the Product Construction

mixed The automata constructions are classical; the tight cost of constraints (factor |Q| attained; unbounded pruning) is proved here.

Learning objectives
  • Compile a regular expression over move symbols into a minimal deterministic automaton, and know the cost of each step.
  • Resolve a language-constrained query on a structure by searching the product of the structure with the automaton.
  • Prove that a constraint can multiply the cost of search by exactly ∣Q∣|Q| and can also prune it to almost nothing.
  • Explain why “search first, filter afterwards” is incorrect, and why enumerating walks is exponential.
  • Describe what changes when the admissibility language is context-free.

8.1 From expression to automaton

An admissibility language is written as a regular expression over move symbols, such as (a b)* (a | ε) or (L | R)* U. The SDK compiles it by the classical three-stage pipeline:

  1. Thompson’s construction turns the expression into a nondeterministic automaton with ε\varepsilon-moves, with O(r)\Oh(r) states for an expression of length rr.
  2. The subset construction turns it into a deterministic automaton whose states are sets of NFA states; in the worst case this is exponential in rr.
  3. Moore’s partition refinement merges states with the same future, giving the minimal DFA — unique up to renaming (Myhill–Nerode).
Proposition 8.1 (Determinization can be exponential)Established

The language “the kk-th letter from the end is aa”, (a∣b)∗ a (a∣b)k−1(a \mid b)^*\, a\, (a \mid b)^{k-1}, has an expression of length O(k)\Oh(k) and a minimal DFA with 2k2^k states.

Proof.

A DFA must remember the last kk letters: two different suffixes of length kk differ at some position jj, and appending j−1j - 1 further letters separates them (one is accepted, the other not). So at least 2k2^k states are needed, and the automaton tracking the last kk letters achieves it.

∎

The SDK’s tests check the size 88 for k=3k = 3 and check acceptance of every word up to length six against Python’s own regular-expression engine for a set of expressions:

Python · executed by the book's example checker
from cgtsdk import compile_regex

for rx in ["a*", "(a b)* (a | ε)", "(L | R)* U", "(a | b)* a (a | b) (a | b)", "(a | b)* a (a | b) (a | b) (a | b) (a | b)"]:
    d = compile_regex(rx)
    print(f"{rx:44s} minimal DFA states = {d.size:3d}   accepts 'a b a': {d.accepts('a b a'.split())}")
Output (checked)
a*                                           minimal DFA states =   1   accepts 'a b a': False
(a b)* (a | ε)                               minimal DFA states =   2   accepts 'a b a': True
(L | R)* U                                   minimal DFA states =   2   accepts 'a b a': False
(a | b)* a (a | b) (a | b)                   minimal DFA states =   8   accepts 'a b a': True
(a | b)* a (a | b) (a | b) (a | b) (a | b)   minimal DFA states =  32   accepts 'a b a': False

(The automata are partial: a missing transition means the word can no longer be accepted, so no “dead” state is counted.) The size ∣Q∣|Q| of the minimal DFA is the grammar parameter that governs everything below.

Interactive laboratory

Text alternative: type a regular expression over move symbols and a word; the widget compiles the minimal DFA with the same pipeline as the SDK, prints its transition table, and reports whether the word is admissible.

8.2 Searching under a constraint

Let G=(Σ,A,⟦⋅⟧,L)G = (\Sigma, \Str, \sem{\cdot}, \Lang) with L\Lang given by a DFA D=(Q,Σ,δ,q0,F)D = (Q, \Sigma, \delta, q_0, F) with partial δ\delta. The constrained resolution problem is: given xx, compute ⟦L⟧(x)\sem{\Lang}(x), the set of elements reachable from xx by some admissible word. For a labelled graph this is the regular path query of Chapter 6.

The answer is found by searching the product A×QA \times Q: there is a product edge (y,q)→(z,δ(q,a))(y, q) \to (z, \delta(q, a)) for each move (y,z)∈⟦a⟧(y, z) \in \sem{a} with δ(q,a)\delta(q, a) defined. Then y∈⟦L⟧(x)y \in \sem{\Lang}(x) iff some (y,q)(y, q) with q∈Fq \in F is reachable from (x,q0)(x, q_0).

Theorem 8.2 (Product resolution, and the exact cost of a constraint)Proved hereCGT-THM-001

Let m=∑a∣⟦a⟧∣m = \sum_a |\sem{a}|.

  1. (Upper bound; standard.) ⟦L⟧(x)\sem{\Lang}(x) is computable in O(∣Q∣⋅(∣Σ∣n+m))\Oh(|Q| \cdot (|\Sigma| n + m)) time and O(∣Q∣⋅n)\Oh(|Q| \cdot n) space.
  2. (A constraint can cost a factor ∣Q∣|Q| and prune nothing.) For all k≥1k \ge 1 and all nn coprime to kk there are a structure with nn elements and m=nm = n and a kk-state DFA such that unconstrained search from xx visits nn states, product search visits exactly nknk, and ⟦L⟧(x)\sem{\Lang}(x) equals the unconstrained answer.
  3. (A constraint can prune almost everything.) For every nn there are a structure with nn elements and a one-state DFA such that product search visits 11 state, unconstrained search visits nn, and the answers differ.
Proof.

(1) By induction on ∣w∣|w|: y∈⟦w⟧(x)y \in \sem{w}(x) and δ∗(q0,w)=q\delta^*(q_0, w) = q iff (y,q)(y, q) is reachable from (x,q0)(x, q_0) along a product walk spelling ww. The product has at most ∣Q∣n|Q| n vertices; each is expanded once, consulting δ\delta for each letter (∣Q∣∣Σ∣n|Q||\Sigma| n in total) and inspecting each move of its first component once per state (∣Q∣m|Q| m in total). This is the classical construction [Mendelzon & Wood 1995; Barrett et al. 2000].

(2) Take the directed cycle on Zn\Z_n with one label aa, ⟦a⟧={(i,i+1 mod n)}\sem{a} = \{(i, i+1 \bmod n)\}, and L={at:t≡0(modk)}\Lang = \{a^t : t \equiv 0 \pmod k\}, whose DFA is the kk-cycle on Zk\Z_k. The product move is (i,j)↦(i+1 mod n,j+1 mod k)(i, j) \mapsto (i + 1 \bmod n, j + 1 \bmod k), and the orbit of (0,0)(0, 0) is {(t mod n,t mod k):t≥0}\{(t \bmod n, t \bmod k) : t \ge 0\}, of size lcm⁡(n,k)=nk\operatorname{lcm}(n, k) = nk by the Chinese remainder theorem. Unconstrained search visits the nn vertices. For every target ii some tt satisfies t≡i(modn)t \equiv i \pmod n and t≡0(modk)t \equiv 0 \pmod k, so ⟦L⟧(0)=Zn\sem{\Lang}(0) = \Z_n, the unconstrained answer.

(3) Take a star with centre xx and n−1n-1 edges labelled bb to leaves, and L=a∗\Lang = a^*. The product visits only (x,q0)(x, q_0) and answers {x}\{x\}; unconstrained search visits all nn vertices.

∎

Part 1 is textbook material; parts 2 and 3 are elementary and quite possibly folklore, but together they state something that is rarely made explicit: a grammatical constraint is not a speedup. It changes the search space, in either direction, by up to the size of the automaton. Whether it helps depends jointly on the language and on the structure — exactly the claim of hypothesis H-005, which the experiments supported (CGT-H-005).

Python · executed by the book's example checker
from cgtsdk import Cost, compile_regex
from cgtsdk.algorithms import product_reach
from cgtsdk.structures import GraphStructure

n = 101
cycle = GraphStructure(n, [(i, "a", (i + 1) % n) for i in range(n)])
for k in (1, 2, 5, 10):
    rx = " ".join(["a"] * k)                     # words of length divisible by k: (a^k)*
    reach, explored = product_reach(cycle, 0, compile_regex(f"({rx})*"), return_explored=True)
    print(f"k={k:2d}: product states explored = {explored:4d} = n*k = {n*k:4d};  |answer| = {len(reach)}")
Output (checked)
k= 1: product states explored =  101 = n*k =  101;  |answer| = 101
k= 2: product states explored =  202 = n*k =  202;  |answer| = 101
k= 5: product states explored =  505 = n*k =  505;  |answer| = 101
k=10: product states explored = 1010 = n*k = 1010;  |answer| = 101

On random labelled graphs (CGT-EXP-003) the measured ratio of explored product states to unconstrained states ranged from 0.0030.003 (language a∗a^* on a sparse graph) to exactly 5.0005.000 (walk length divisible by 55): both extremes of the theorem occur in practice, not only on constructed inputs.

Interactive laboratory

Text alternative: on an eight-vertex labelled graph, type a regular expression; the widget compiles its DFA, runs breadth-first search over (vertex, state) pairs, highlights the vertices of ⟦L⟧(source)\sem{\Lang}(\text{source}), and compares the number of explored product states with plain reachability — which explores up to ∣Q∣|Q| times fewer states and gives a different answer whenever the constraint matters.

8.3 Two incorrect shortcuts

Two tempting alternatives to the product construction fail.

Search, then filter. Plain reachability forgets which label words lead to a vertex, so no filtering of its answer can recover ⟦L⟧(x)\sem{\Lang}(x). On the random graphs of the experiment, unconstrained search misreported up to about 15,600 of 20,000 vertices for restrictive languages (CGT-OBS-007). The constraint must be carried during the search.

Enumerate walks, then check the word. This is correct if walks up to length ∣Q∣⋅n|Q| \cdot n are enumerated — any longer accepted walk revisits a product state and can be shortened — but the number of walks grows exponentially. On a 12-vertex graph the experiment’s walk enumeration hit its cap of two million walks while the product had at most 5555 states. The product construction is, in effect, the sharing mechanism applied to walks: all walks reaching the same (vertex, state) pair are represented once.

8.4 Context-free admissibility

When L\Lang is context-free — balanced parentheses for matched calls and returns, the Dyck language of Chapter 3 — the product is no longer a finite graph, and constrained reachability becomes CFL-reachability, the formulation behind interprocedural program analysis [Reps 1998; Yannakakis 1990]. It is solved by a dynamic program over pairs of vertices and nonterminals in time cubic in nn (with factors depending on the grammar) [Barrett et al. 2000]. Whether the factor-∣Q∣|Q| phenomenon of Theorem 8.2 has a precise context-free analogue — how the size of the grammar enters, and whether the bound is attained — is open (CGT-OPEN-008).

Exercise 8.1

Compile (L | R)* U by hand: give the Thompson NFA, the subset DFA, and the minimal DFA. Explain in one sentence what language of tree navigations it describes.

Solution to Exercise 8.1

The minimal DFA has two states: a start state q0q_0 looping on LL and RR, and an accepting state q1q_1 reached by UU from q0q_0, with no transitions out of q1q_1. It describes any downward walk followed by exactly one step up — for example, “go to a node, then report its parent”.

Exercise 8.2

In part 2 of Theorem 8.2, what happens when gcd⁡(n,k)=g>1\gcd(n, k) = g > 1? Compute the number of explored product states and the answer set.

Solution to Exercise 8.2

The orbit of (0,0)(0,0) has size lcm⁡(n,k)=nk/g\operatorname{lcm}(n,k) = nk/g, and t≡i(modn)t \equiv i \pmod n, t≡0(modk)t \equiv 0 \pmod k is solvable iff g∣ig \mid i. So nk/gnk/g states are explored and the answer is the n/gn/g vertices divisible by gg.

Exercise 8.3

Prove the shortening argument: if some walk from xx to yy has its label in L\Lang, then some such walk has length less than ∣Q∣⋅n|Q| \cdot n.

Exercise 8.4

Using the SDK, generate a random labelled graph with n=2000n = 2000 and average degree 1.51.5, and compare product-search sizes for a*, (a b)* c and “length divisible by 5”. Reproduce the qualitative pattern of CGT-EXP-003.

Summary
  • Admissibility languages compile to minimal DFAs (Thompson, subset, Moore); determinization can cost 2k2^k states.
  • Constrained resolution is search on the product A×QA \times Q, in O(∣Q∣(∣Σ∣n+m))\Oh(|Q|(|\Sigma| n + m)) time.
  • A constraint can multiply the search space by exactly ∣Q∣|Q| without changing the answer, or prune it to a single state (CGT-THM-001); measured ratios on random graphs span both extremes.
  • Filtering an unconstrained search is incorrect; enumerating walks is exponential; the product shares all walks that reach the same (vertex, state) pair.
  • Context-free admissibility leads to CFL-reachability, cubic in general; its cost-of-constraint theory is open.

References cited in this chapter

  1. Barrett et al. 2000 C. Barrett, R. Jacob, M. Marathe (2000). Formal-language-constrained path problems. SIAM Journal on Computing 30(3):809–837. doi:10.1137/S0097539798337716.
  2. Mendelzon & Wood 1995 A. O. Mendelzon, P. T. Wood (1995). Finding regular simple paths in graph databases. SIAM Journal on Computing 24(6):1235–1258.
  3. Reps 1998 T. Reps (1998). Program analysis via graph reachability. Information and Software Technology 40(11–12):701–726. doi:10.1016/S0950-5849(98)00093-7.
  4. Yannakakis 1990 M. Yannakakis (1990). Graph-theoretic methods in database theory. Proc. 9th ACM PODS, pp. 230–242. doi:10.1145/298514.298576.

Report an erratum or an unclear passage in this chapter