Course Test · University: upper level

Theory of computation: RatMath answers

The same twenty exercises, answered with RatMath Core and the Shelf libraries only. The code below was run when this page was built; its output is shown as printed, and compared with the independent answer.

Agrees 18 Can't yet 2

Libraries used here: core, discrete, logic. In this code each library is in scope under its own name (linalg.det, stats.tTest), and Core's names (R, Rational, …) are in scope directly.

  1. 1.Deterministic automataAgrees
    Design a DFA over {0,1}\{0, 1\} accepting words with an even number of 1s. Does it accept 1011? 1001?
    Independent answer
    Two states, even (start, accepting) and odd, swapped by each 1. 1011 has three 1s: rejected. 1001 has two: accepted.
    RatMath code
    const M = logic.automaton({ states: ["e", "o"], alphabet: "01", start: "e", accept: ["e"],
      delta: { e: { 0: "e", 1: "o" }, o: { 0: "o", 1: "e" } } });
    [M.accepts("1011"), M.accepts("1001")]
    Output
    [false, true]
  2. 2.Minimal DFAAgrees
    How many states does the minimal DFA for binary words ending in 01 have?
    Independent answer
    It must remember whether the word ends in nothing useful, in 0, or in 01: three states, which are pairwise distinguishable (by the suffixes 01, 1, and ε\varepsilon).
    RatMath code
    logic.minimizeAutomaton(logic.toDFA(logic.regexToNFA("(0|1)*01"))).states.length
    Output
    3
  3. 3.Subset constructionAgrees
    Convert (a∣b)∗abb(a|b)^*abb to a DFA and minimise it. How many states? Does it accept aababbaababb and abababab?
    Independent answer
    The minimal DFA tracks the longest suffix that is a prefix of abbabb: ε,a,ab,abb\varepsilon, a, ab, abb: 4 states. aababbaababb ends in abbabb: yes; abababab: no.
    RatMath code
    const D = logic.minimizeAutomaton(logic.toDFA(logic.regexToNFA("(a|b)*abb")));
    [D.states.length, D.accepts("aababb"), D.accepts("abab")]
    Output
    [4, true, false]
  4. 4.Equivalent regular expressionsAgrees
    Are (a∣b)∗(a|b)^* and (a∗b∗)∗(a^*b^*)^* equivalent?
    Independent answer
    Yes: both denote all words over {a,b}\{a, b\} (every letter is an a∗b∗a^*b^* block).
    RatMath code
    logic.equivalentAutomata(logic.regexToNFA("(a|b)*"), logic.regexToNFA("(a*b*)*")).equivalent
    Output
    true
  5. 5.Product constructionAgrees
    Build an automaton for words over {a,b}\{a, b\} with an even number of aas and an odd number of bbs. Test aabaab and abab.
    Independent answer
    Pair the two 2-state parity automata: 4 states, accepting (even, odd). aabaab: 2 aas, 1 bb: yes. abab: no.
    RatMath code
    const par = (letter, acc) => logic.automaton({ states: ["e", "o"], alphabet: "ab", start: "e", accept: [acc],
      delta: { e: { a: letter === "a" ? "o" : "e", b: letter === "b" ? "o" : "e" }, o: { a: letter === "a" ? "e" : "o", b: letter === "b" ? "e" : "o" } } });
    const P = logic.product(par("a", "e"), par("b", "o"));
    [P.states.length, P.accepts("aab"), P.accepts("ab")]
    Output
    [4, true, false]
  6. 6.From DFA to regular expressionAgrees
    Give a regular expression for binary words with an even number of 1s.
    Independent answer
    0∗(10∗10∗)∗0^*(10^*10^*)^*, or equivalently 0∗∣0∗1(0∣10∗1)∗10∗0^* \mid 0^*1(0 \mid 10^*1)^*10^*.
    RatMath code
    const M = logic.automaton({ states: ["e", "o"], alphabet: "01", start: "e", accept: ["e"],
      delta: { e: { 0: "e", 1: "o" }, o: { 0: "o", 1: "e" } } });
    const r = String(logic.toRegex(M));
    [r, logic.equivalentAutomata(logic.regexToNFA(r), logic.regexToNFA("0*(10*10*)*")).equivalent]
    Output
    ["0*|0*1((0|10*1)*)10*", true]
  7. 7.Context-free grammarsAgrees
    Give a grammar for {anbn:n≥0}\{a^nb^n : n \ge 0\}. Does it generate aabbaabb? aabaab? List its words of length at most 6.
    Independent answer
    S→aSb∣εS \to aSb \mid \varepsilon. aabbaabb: yes; aabaab: no. Words: ε,ab,aabb,aaabbb\varepsilon, ab, aabb, aaabbb.
    RatMath code
    const g = logic.grammar("S -> a S b | ε");
    [g.accepts("aabb"), g.accepts("aab"), g.words(6).words]
    Output
    [true, false, ["", "ab", "aabb", "aaabbb"]]
  8. 8.Chomsky normal formAgrees
    Convert S→aSb∣εS \to aSb \mid \varepsilon to Chomsky normal form.
    Independent answer
    New start S0→ε∣SS_0 \to \varepsilon \mid S; remove ε\varepsilon: S→aSb∣abS \to aSb \mid ab; terminals A→aA \to a, B→bB \to b; split: S→AX∣ABS \to AX \mid AB, X→SBX \to SB, and S0→ε∣AX∣ABS_0 \to \varepsilon \mid AX \mid AB.
    RatMath code
    String(logic.grammar("S -> a S b | ε").toCNF())
    Output
    S → T0 X0 | T0 T1
    X0 → S T1
    S0 → ε | T0 X0 | T0 T1
    T0 → a
    T1 → b
    The rules print one per line (line breaks shown here as spaces).
  9. 9.Pushdown automataAgrees
    Run a PDA for anbna^nb^n (push on aa, pop on bb) on aabbaabb and aabaab.
    Independent answer
    aabbaabb: push, push, pop, pop, the stack is back to its bottom: accept. aabaab: one AA is left: reject.
    RatMath code
    const P = logic.pda({ start: "q0", accept: ["q2"], moves: [
      ["q0", "a", "ε", "q0", "A"], ["q0", "ε", "ε", "q1", "ε"],
      ["q1", "b", "A", "q1", "ε"], ["q1", "ε", "Z", "q2", "Z"]] });
    [P.accepts("aabb"), P.accepts("aab")]
    Output
    [true, false]
  10. 10.Pumping lemmaCan't yet
    Prove that {anbn}\{a^nb^n\} is not regular.
    Independent answer
    If it were, with pumping length pp, write apbp=xyza^pb^p = xyz with ∣xy∣≤p|xy| \le p, ∣y∣≥1|y| \ge 1. Then y=aky = a^k, and xy2z=ap+kbpxy^2z = a^{p+k}b^p is not in the language. Contradiction.
    RatMath code
    // No proof tool: a finite check only. The minimal DFA for words a^n b^n with n ≤ N needs more states as N grows.
    [2, 4, 6].map((N) => logic.minimizeAutomaton(logic.toDFA(logic.regexToNFA(Array.from({ length: N + 1 }, (_, n) => "a".repeat(n) + "b".repeat(n) || "ε").join("|")))).states.length)
    Output
    [6, 10, 14]
    Independent answer, in RatMath's notation
    not regular (a proof)
    Proofs about all words are out of reach; the code only shows the minimal DFA for the finite pieces n≤Nn \le N growing with NN, which is evidence, not the pumping argument.
  11. 11.Turing machinesAgrees
    Run a Turing machine that adds one to a binary number on the input 1011.
    Independent answer
    Move to the right end, turn trailing 1s into 0s moving left, then turn the first 0 into 1: 1011+1=11001011 + 1 = 1100.
    RatMath code
    const r = logic.binaryIncrement().run("1011");
    [r.status, r.tape]
    Output
    ["accepted", "1100"]
  12. 12.Primitive recursionAgrees
    Using only primitive recursive building blocks, compute 3+43 + 4, 2102^{10} and 7!7!.
    Independent answer
    77, 10241024, 50405040.
    RatMath code
    const { add, power, factorial } = logic.recursive.examples;
    [add.at(3, 4), power.at(2, 10), factorial.at(7)]
    Output
    [7n, 1024n, 5040n]
  13. 13.Composition of recursive functionsAgrees
    Define d(x)=x+xd(x) = x + x by composing addition with projections, and evaluate d(21)d(21).
    Independent answer
    d=add⁡∘(π11,π11)d = \operatorname{add}\circ(\pi^1_1, \pi^1_1), d(21)=42d(21) = 42.
    RatMath code
    const { compose, proj, examples } = logic.recursive;
    compose(examples.add, [proj(1, 1), proj(1, 1)]).at(21)
    Output
    42n
  14. 14.MinimisationAgrees
    Use the μ\mu operator to compute the integer square root of 30: the least yy with (y+1)2>30(y + 1)^2 > 30.
    Independent answer
    52=25≤30<36=625^2 = 25 \le 30 < 36 = 6^2: ⌊30⌋=5\lfloor\sqrt{30}\rfloor = 5.
    RatMath code
    const { mu } = logic.recursive;
    // mu(f) finds the least y with f(y, x) = 0 (the searched argument comes first); here f = 0 exactly when (y + 1)^2 > x
    const f = { arity: 2, name: "gt", at: (y, x) => ((y + 1n) * (y + 1n) > x ? 0n : 1n) };
    mu(f).at(30)
    Output
    5n
    The test function is written in JavaScript rather than built from the primitive recursive pieces.
  15. 15.The halting problemCan't yet
    Is there an algorithm that decides, for every program and input, whether the program halts?
    Independent answer
    No (Turing). If HH decided halting, the program DD that runs HH on its own code and does the opposite would halt exactly when it does not.
    RatMath code
    // A run with a budget can only report "running" for a machine that has not stopped yet.
    const loop = logic.turing({ start: "s", accept: "y", reject: "n", delta: { "s,_": ["s", "_", "R"] } });
    loop.run("", { budget: 1000 }).status
    Output
    running
    Independent answer, in RatMath's notation
    undecidable (a proof)
    No computation can stand in for the diagonal argument; run with a budget reports running, as the documentation says it must.
  16. 16.CircuitsAgrees
    A full adder adds bits a,ba, b and a carry cinc_{in}. What are the sum and carry for 1+1+11 + 1 + 1?
    Independent answer
    1+1+1=3=1121 + 1 + 1 = 3 = 11_2: sum 1, carry 1.
    RatMath code
    logic.adders.full().evaluate({ a: 1, b: 1, cin: 1 })
    Output
    { sum: 1, cout: 1 }
  17. 17.Ripple-carry adderAgrees
    How many gates and what depth does a ripple-carry adder for 4-bit numbers have, built from full adders of 5 gates and depth 3?
    Independent answer
    Four full adders: about 20 gates; the carry passes through each stage, so the depth grows linearly (roughly 2n+1=92n + 1 = 9 for the carry chain).
    RatMath code
    const c = logic.adders.ripple(4);
    [c.size, c.depth]
    Output
    [17, 7]
    The exact counts depend on how the half and full adders are built; the check accepts the textbook range.
  18. 18.Master theoremAgrees
    Solve T(n)=2T(n/2)+nT(n) = 2T(n/2) + n and T(n)=8T(n/2)+n2T(n) = 8T(n/2) + n^2 asymptotically.
    Independent answer
    nlog⁡22=nn^{\log_2 2} = n matches f(n)=nf(n) = n: Θ(nlog⁡n)\Theta(n\log n). nlog⁡28=n3n^{\log_2 8} = n^3 beats n2n^2: Θ(n3)\Theta(n^3).
    RatMath code
    [discrete.masterTheorem({ a: 2, b: 2, d: 1 }).bound, discrete.masterTheorem({ a: 8, b: 2, d: 2 }).bound]
    Output
    ["Θ(n log n)", "Θ(n^3)"]
  19. 19.SAT as a decision problemAgrees
    Is (x1∨x2)∧(¬x1∨x3)∧(¬x2∨¬x3)∧(x1∨¬x3)(x_1 \lor x_2) \land (\lnot x_1 \lor x_3) \land (\lnot x_2 \lor \lnot x_3) \land (x_1 \lor \lnot x_3) satisfiable? Give a satisfying assignment.
    Independent answer
    Try x1=1x_1 = 1: then x3=1x_3 = 1 (clause 2), so x2=0x_2 = 0 (clause 3); clause 1 holds by x1x_1, clause 4 by x1x_1. Satisfiable with x1=1,x2=0,x3=1x_1 = 1, x_2 = 0, x_3 = 1.
    RatMath code
    const r = logic.satisfiable("(x1 | x2) & (~x1 | x3) & (~x2 | ~x3) & (x1 | ~x3)");
    [r.status, r.model ?? r.assignment]
    Output
    ["satisfiable", { x1: true, x2: false, x3: true }]
  20. 20.Counting accepted wordsAgrees
    How many binary words of length 4 have an even number of 1s?
    Independent answer
    Half of the 16: (40)+(42)+(44)=1+6+1=8\binom40 + \binom42 + \binom44 = 1 + 6 + 1 = 8.
    RatMath code
    const M = logic.automaton({ states: ["e", "o"], alphabet: "01", start: "e", accept: ["e"],
      delta: { e: { 0: "e", 1: "o" }, o: { 0: "o", 1: "e" } } });
    M.words(4).filter((w) => w.length === 4).length
    Output
    8