5.2. Three-State Tree-Covering DP
Tile-role states. Depth-2 templates introduce only a local dependency between a parent and one child, not arbitrary global constraints. Because tiles are non-overlapping and each covers at most two levels, a node’s covering role reduces to three possibilities: acting independently, initiating a depth-2 tile as root, or being subsumed by a parent’s tile as leaf. These three cases exhaust all tile roles under the depth constraint. The resulting parent–child state coupling () places the problem in the class of tree DPs with constrained state assignment. This well-studied framework encompasses maximum weighted matching and maximum independent sets on trees.
The coupling between candidate selection and tile assignment (
Section 4) is resolved by decomposing each node’s role into three mutually exclusive states:
State − (independent): The node contributes
(Equation (
2)), and all children are free (state − or ↓).
State ↓ (tile-root): The node forms a depth-2 tile with one selected child; the chosen child enters state ↑, and all others remain free.
State ↑ (tile-leaf): The node is committed by its parent’s depth-2 tile and forms no tile of its own; all children are free.
A node cannot simultaneously be tile-root and tile-leaf, as this would place it in two overlapping tiles. The assignment of state ↑ is decided by the parent, who evaluates whether committing the child improves the total covering value. This asymmetry resolves depth-2 context dependence (
Section 4.2) without violating optimal substructure.
Template compilation. Before the DP begins, the template set is compiled offline into two hash-indexed lookup tables. maps each operator to its depth-1 templates, and maps each (parent operator, child position, child operator) triple to its depth-2 templates. Both support lookup. A single key may index multiple templates differing only in predicate constraints .
Per-e-node structural information. For each e-node
, the bottom-up pass computes three quantities that together characterize
n’s potential tile roles before evaluating the DP recurrences. The
depth-1 contribution measures the best value
n can achieve as an independent tile. It is computed according to Equation (
2) in
Section 3.3. The implementation looks up matching depth-1 templates in
and takes the maximum weight among satisfied templates, falling back to the weighted-AST default
when no explicit template matches. The
compatibility signature then summarizes which parent operators could form a depth-2 tile with
n as tile-leaf. Its computation uses the
predicate profile , a bitvector where
if child e-class
contains only constants or variables. Formally,
iff a position
i and a template
exist whose predicates
are satisfied. Because the input space
is finite, the mapping
can be pre-compiled into a static lookup table (analogous to BURS automaton state compilation [
10,
13]), yielding
per e-node. Finally, the per-e-class aggregate
enables pruning of unnecessary depth-2 evaluations. If
, no candidate in
c can serve as tile-leaf for parent operator
, and the depth-2 evaluation for that child position is skipped entirely.
Bottom-up DP values. Each state’s optimal value depends only on the subtree below the node. The parent determines which state a child enters, but does not affect the optimal value within that state, establishing the optimal substructure that the bottom-up pass exploits.
The bottom-up pass requires an acyclic e-graph. Cyclic e-nodes are removed by a standard preprocessing step [
2]. For each e-node
in topological order, after computing structural information, three DP values are computed. In state ↑ (tile-leaf),
n is committed by its parent and forms no tile:
In state − (independent),
n contributes its depth-1 value
:
In state ↓ (tile-root),
n selects child position
j, template
p, and candidate
to form a depth-2 tile;
enters state ↑, and the other children are free:
If no depth-2 template matches,
. After all e-nodes are processed, each e-class aggregates the best free value:
does not bind a specific candidate. Candidate selection is deferred to trace-back. The complete bottom-up procedure is given in Algorithm 1.
| Algorithm 1: Bottom-Up DP Pass |
![Algorithms 19 00377 i001 Algorithms 19 00377 i001]() |
Top-down trace-back. Because a single-direction pass cannot simultaneously access parent context and subtree costs (
Section 4.2), the bottom-up pass defers all commitment, and the top-down pass resolves each node’s actual state once the parent’s decision is known.
Starting from the root e-class, the trace-back recursively selects candidates and assigns tile roles using pre-computed DP values. When an e-class c is free (not committed by its parent), the procedure selects the candidate achieving and compares against . If , enters state ↓, forming a depth-2 tile with the recorded best child , which enters state ↑, while all other children are recursed as free. Otherwise, enters state −, and all children are free. When c is committed with a specific candidate , that candidate enters state ↑, meaning no tile is formed, and all children are recursed as free.
Recursive calls carry no visit markers since DAG-to-tree unfolding (
Section 5.1) ensures each tree position is an independent subproblem. The complete procedure is given in Algorithm 2.
| Algorithm 2: Top-Down Pass: Trace-Back |
![Algorithms 19 00377 i002 Algorithms 19 00377 i002]() |
5.3. Illustrative Example
We revisit the running example from
Section 4 (
Figure 2a). For readability, this toy instance sets the weighted-AST fallback cost
to zero for all involved operators, so that Equation (
2) reduces to
whenever no explicit depth-1 template matches. Nonzero fallback terms
add the corresponding contributions without changing the parent-context dependency illustrated here.
E-class c contains and . Candidate does not match any depth-1 template, so under the simplified fallback. Candidate matches a depth-1 bitwise-OR simplification template with . Denoting , the bottom-up pass yields , , and . Neither serves as tile-root, so and , with as the best free candidate. Meanwhile, the depth-2 variable-isolation template p fires when an parent pairs with a child, so while .
At Position Eq (
Figure 2b, left), the parent
computes
and
(with
under the simplified fallback). Since
, the trace-back selects state ↓, committing
c to candidate
in state ↑. The depth-2 gain
outweighs the forgone depth-1 weight
of
.
At Position BvUle (
Figure 2b, right), the parent
finds no applicable depth-2 template (
), so it selects state −, leaving
c free. The trace-back selects
, which contributes its depth-1 weight
. The two decisions are independent: Position Eq selects
via state ↓ for the depth-2 match, while Position BvUle retains
via state − for the depth-1 match, consistent with the DAG-to-tree unfolding principle of
Section 5.1.