TAOCP 7.1.4 Exercise 212
The flaw in the previous solution is that it tried to define ZDD nodes as states indexed by a subset $X \subseteq U$.
Section 7.1.4: Binary Decision Diagrams
Exercise 212. ▶ [25] [25] What’s a good way to compute the ZDD for an exact cover problem?
Verified: no
Solve time: 4m49s
The flaw in the previous solution is that it tried to define ZDD nodes as states indexed by a subset $X \subseteq U$. This turns the construction into an exponential dynamic program over $2^{|U|}$ states and is not the intended ZDD construction method. A correct approach must construct the ZDD structurally from the exact cover instance using recursive decomposition over constraints, while ensuring that the resulting structure is a reduced ZDD over a fixed ordering of the sets.
We reformulate the problem in the standard exact cover form. Let $U$ be the set of items and let $\mathcal{S} = {S_1,\dots,S_m}$ be subsets of $U$. An exact cover is a subfamily $\mathcal{T} \subseteq \mathcal{S}$ such that every element of $U$ appears in exactly one set in $\mathcal{T}$, and chosen sets are pairwise disjoint.
We construct a ZDD whose paths represent exactly the valid subfamilies $\mathcal{T}$.
Recursive structure of the construction
We define a recursive function $Z(P)$, where $P$ is an exact cover subproblem consisting of:
- a current set of remaining items $U_P \subseteq U$,
- a current set of available sets $\mathcal{S}_P \subseteq \mathcal{S}$, each still contained in $U_P$.
The initial problem is $Z(U, \mathcal{S})$.
The ZDD for $P$ is defined by structural decomposition on an item.
If $U_P = \varnothing$, then all constraints are satisfied and the only solution is the empty choice:
$$ Z(P) = \top. $$
If there exists an item $x \in U_P$ that is contained in no set of $\mathcal{S}_P$, then no exact cover is possible:
$$ Z(P) = \bot. $$
Otherwise, choose an item $x \in U_P$. This choice is a branching point driven by the constraint that $x$ must be covered exactly once. Every solution must select exactly one set $S \in \mathcal{S}_P$ with $x \in S$.
For each such set $S$, define the reduced subproblem $P_S$ as follows:
- Remove all items in $S$ from $U_P$,
- Remove from $\mathcal{S}_P$ every set that intersects $S$, since those would violate disjointness,
- Restrict remaining sets to the reduced universe.
Formally,
$$ U_{P_S} = U_P \setminus S, $$
$$ \mathcal{S}_{P_S} = {T \in \mathcal{S}_P \mid T \cap S = \varnothing}. $$
Then every exact cover of $P$ is obtained uniquely by choosing one such $S$ and combining it with a solution of $P_S$. Hence the solution family satisfies
$$ \mathcal{Z}(P) = \biguplus_{S \in \mathcal{S}_P: x \in S} {, S \cup T : T \in \mathcal{Z}(P_S),}. $$
ZDD construction from the recursion
We now interpret this recursion as a ZDD construction.
Each recursive call $Z(P)$ corresponds to a ZDD node representing the family of partial solutions of $P$. The outgoing edges are determined by the choices of sets $S$ containing the chosen item $x$.
To fit the ZDD model over the fixed ordering $S_1 \prec \cdots \prec S_m$, each choice of $S_i$ is implemented as including variable $S_i$, while all sets that intersect $S_i$ are excluded in the corresponding subproblem. This enforces consistency with the exact cover constraint.
Thus, conceptually:
- the decision “use $S_i$” corresponds to the 1-edge of the ZDD,
- all incompatible sets are forced absent in the resulting subproblem,
- the 0-edge corresponds to skipping $S_i$ when it is not chosen for covering the selected item.
Sharing and canonicity
The crucial computational improvement is that ZDD nodes are not indexed by subsets of $U$, but by the induced residual subproblem $P$. Two recursive calls that yield isomorphic subproblems (same remaining items and same remaining sets up to identity) produce identical ZDD nodes.
Hence we memoize on canonical encodings of $P$ (for example, bitsets for remaining items and remaining sets restricted by earlier decisions). This guarantees that the construction produces a directed acyclic graph rather than a tree.
After memoization, standard ZDD reduction rules apply:
- elimination of nodes whose 1-child is $\bot$,
- merging of isomorphic subgraphs,
- zero-suppression, ensuring that variables (sets) that cannot appear in any solution are omitted.
Correctness
We prove correctness by induction on $|U_P|$.
If $U_P = \varnothing$, the construction returns $\top$, which represents the empty family, matching the unique exact cover of an already satisfied instance.
Otherwise, every solution must choose exactly one set $S$ containing the selected item $x$. The recursion enumerates exactly these choices, and the reduction to $P_S$ correctly removes all conflicting sets and covered items, ensuring that no element can be covered twice.
Conversely, any path from the root to $\top$ corresponds to a sequence of consistent choices of sets, each resolving a distinct item, and the disjoint-union decomposition ensures that every exact cover arises from exactly one such path.
Therefore, the constructed reduced and memoized ZDD represents exactly the family of all exact covers of $U$, and it is obtained by a structural recursion over constraints rather than an explicit enumeration of subsets of $U$.