CDM

Boolean Decision Diagrams 1 Boolean Functions

 Clones and Post

Klaus Sutner Carnegie Mellon University  Minimal Automata 24-bdd 2017/12/15 23:21

 Binary Decision Diagrams

Boolean Functions 3 There Are Lots 4

n Obviously there are 22 Boolean functions of arity n. Let us return to the two-element B = ff, tt . { } To lighten notation we will overload and write 2 = 0, 1 instead. { }

Definition n 1 2 3 4 5 6 7 8 A of arity n is any function of the form n 22 4 16 256 65536 4.3 109 1.8 1019 3.4 1038 1.2 1077 2n 2 × × × × →

20 Note that we only consider single outputs here, f : 2n 2 rather than 22 = 6.7 10315652 = → f : 2n 2m. × ∞ → Slightly more vexing is the question of whether one should include n = 0. We’ll . . . , for all practical intents and purposes. Since there are quite so many, it is a fudge things a bit. good idea to think about how to describe and construct these functions.

Different Angles 5 The Challenge Problems 6

The propositional logic angle immediately suggests the following computational problems. Combinatorics: 2n 2 → Problem: Tautology Logic: Instance: A propositional formula ϕ. Question: Is ϕ a tautology? Circuits: logic gates

Datatype: BDDs Problem: Satisfiability Instance: A propositional formula ϕ. Question: Is ϕ a contingency? In a sense, this is all the same–but different perspectives lead to different ideas and very different algorithms. Problem: Equivalence For example, from the logic perspective it is natural to generate tautologies, a Instance: Two propositional formulae ϕ and ψ. boring idea from the viewpoint of circuits. Question: Are ϕ and ψ equivalent?

Alas, we know from complexity theory that these are all hard. Connections 7

 Boolean Functions

These are all closely connected:

ϕ is a tautology iff ϕ fails to be satisfiable 2 Clones and Post ¬ ϕ is a tautology iff ϕ is equivalent to > ϕ and ψ are equivalent iff ϕ ψ is a tautology Minimal Automata ⇔ 

Note, though, that some care is necessary to deal with normal forms (which are often required by specific algorithms).  Binary Decision Diagrams

Taxonomy: Zero or One Inputs 9 Binary Boolean Functions 10

Case n = 2: This is much more interesting: here all the well-known operations from propositional logic appear. For small arities n we can easily enumerate all Boolean functions. x y Hand(x, y) Hor(x, y) Himp(x, y) Case n = 0: All we get is 2 constant functions 0 and 1. 0 0 0 0 1 0 1 0 1 1 1 0 0 1 0 1 1 1 1 1 Case n = 1: 2 constants, plus

x y Hequ(x, y) Hxor(x, y) Hnand(x, y) Hid = x, the identity, and 0 0 1 0 1 Hnot(x) = 1 x, negation. − 0 1 0 1 1 1 0 0 1 1 Incidentally, the latter two are reversible. 1 1 1 0 0

Via composition we can generate all Boolean functions from these.

Expressing Boolean Functions 11 Propositional Bases 12

It is well-known that some Boolean functions can be expressed in terms of As it turns out, there are lots of bases. others. For example, one can write “exclusive or” in , ⊕ conjunctive normal, or using only nand ¯ or nor ¯ as follows: ∧ ∨ Theorem (Basis Theorem) Any Boolean function f : 2n 2 can be obtained by composition from the → x y = (x y) (x y) basic functions (unary) negation and (binary) disjunction. ⊕ ∧ ∨ ∧ = (x y) (x y) ∨ ∧ ∨ Proof. = ((x ¯ x) ¯(y ¯ y)) ¯(x ¯ y) Consider the for f. ∨ ∨ ∨ ∨ ∨ For every truth assignment α such that f(α) = 1 define a conjunction = (x ¯(x ¯ y)) ¯((x ¯ y) ¯ y) Cα = z1 ... zn by zi = xi whenever α(xi) = 1, zi = xi otherwise. ∧ ∧ ∧ ∧ ∧ ∧ ∧ Then f(x) Cα ... Cα . ≡ 1 ∨ ∨ r Use Morgan’s law to eliminate the conjunctions from this disjunctive normal A set of Boolean functions is a basis or functionally complete if it produces all form. Boolean functions by composition. 2 Other Bases 13 Clones 14

Here are some more bases: Definition (Clones) , , Write for all Boolean functions and for Boolean functions of arity n. {∧ ¬} BF BFn , , n { ⇒ ¬} A family F of Boolean functions is a clone if it contains all projections Pi and , 0 , is closed under composition. { ⇒ } , , 0 {∨ ⇔ } Recall that a projection is a simple map ¯ {∧} n n n ¯ Pi : 2 2 Pi (x1, . . . , xn) = xi {∨} →

On the other hand, , does not work. and closure under composition means that {∨ ⇔ } m f : 2 2 f(x) = h(g1(x), . . . , gn(x)) One might be tempted to ask when a set of Boolean functions is a basis. →

m n is in F, given functions gi : 2 2 for i = 1, . . . , n , and h : 2 2 , all in F. To tackle this problem we need to be a little more careful about what is meant → → by basis. More succinctly, we can write h (g1, . . . , gn). ◦

Characterization of Clones 15

One can rephrase the definition of a clone into a longer list of simpler According to rules (2), (3), (4), (5) we can construct h from f via conditions (that may be easier to work with). Let f, g F be an arbitrary ∈ h(x1, . . . , xn) = f(x , . . . , x ) functions of suitable arity. π(1) π(m) The following functions h must also be in . for any function π :[m] [n] . F → monadic identity:P 1 1 Together with the last rule this suffices to get arbitrary compositions.

cylindrification: h(x1, . . . , xn) = f(x1, . . . , xn 1) −

diagonalization: h(x1, . . . , xn) = f(x1, . . . , xn, xn)

rotation: h(x1, . . . , xn) = f(x2, x3, . . . , xn, x1) Exercise Show that this characterization of clones is correct. transposition: h(x1, . . . , xn) = f(x1, . . . , xn 2, xn, xn 1) − −

composition: h(x1, . . . , xn+m) = f(x1, . . . , xn, g(xn+1, . . . , xn+m))

Classifying Clones 17 Self-Dual Functions 18

Wild Question: Is there a way to describe all clones? For any Boolean function f we can define its dual f by

f(x) = f(x) This may seem utterly hopeless at first, but at least a few simple clones can be b dealt with easily. For example, = and = . b ∨ ∧ ¬ ¬ It is clear that BF is a clone; we write for this clone. At the other end, the > smallest clone is the set all projections; we write for this clone. b b ⊥ A function is self-dual if The collections of all clones form a lattice under set inclusion. Operation is f(x) = f(x) u just intersection, but requires closure under composition. t Examples of self-dual functions are and projections. b ¬ Dual Clones 19 0/1-Preserving Functions 20

To find more interesting examples of clones, consider the following. For any clone F we can define the dual clone by Definition F = f f F { | ∈ } Let f be a Boolean function. b b f is 0-preserving if f(0) = 0. f is 1-preserving if f(1) = 1. So the claim here is that F is again a clone. f is bi-preserving if f is 0-preserving and 1-preserving. Unfortunately, for our trivialb clones and duality does not buy us anything. > ⊥ But later we will find a nice symmetric structure. For example n-ary disjunction and conjunction are both bi-preserving.

Claim Claim The collection D of all self-dual Boolean functions is a clone. The collections P0, P1 and P of all 0-preserving, 1-preserving and bi-preserving Boolean functions are clones.

Monotone Functions 21 Dedekind Numbers 22

For Boolean vectors a and b of the same length write a b iff ai bi (the ≤ ≤ product order on 2). Counting the number of n-ary monotone Boolean functions is not easy, the problem was first introduced by R. Dedekind in 1897, see Definition https://oeis.org/A000372. A Boolean function f is monotone if Here are the first few Dedekind numbers Dn, for arities n = 0,..., 8. a b implies f(a) f(b) ≤ ≤ 2, 3, 6, 20, 168, 7581, 7828354, 2414682040998, 56130437228687557907788

For example n-ary disjunction and conjunction are both monotone. One can define a topology on 2n so that these functions are precisely the One can show that Dn is the number of anti-chains in the powerset of [n]. For continuous ones. example, there are 6 anti-chains over [2]

Claim , , 1 , 2 , 1, 2 , 1 , 2 ∅ {∅} {{ }} {{ }} {{ }} {{ } { }} The collection M of all monotone Boolean functions is a clone.

More Anti-Chains 23 Quasi-Monadic Functions 24

And 20 anti-chains over [3]: These are functions that are essentially unary: they are formally n-ary but depend on only one argument.

, , 1 , 2 , 3 , 1, 2 , 1, 3 , 2, 3 , 1, 2, 3 , Definition ∅ {∅} {{ }} {{ }} {{ }} {{ }} {{ }} {{ }} {{ }} 1 , 2 , 1 , 3 , 1 , 2, 3 , 2 , 3 , 2 , 1, 3 , 3 , 1, 2 , A Boolean function f is quasi-monadic if there is some index i such that {{ } { }} {{ } { }} {{ } { }} {{ } { }} {{ } { }} {{ } { }} 1, 2 , 1, 3 , 1, 2 , 2, 3 , 1, 3 , 2, 3 , 1 , 2 , 3 , {{ } { }} {{ } { }} {{ } { }} {{ } { } { }} xi = yi implies f(x) = f(y) 1, 2 , 1, 3 , 2, 3 {{ } { } { }}

Claim Make sure you understand how the anti-chains correspond to monotone The collection U of all quasi-monadic Boolean functions is a clone. functions: every set in an anti-chain over [n] defines a clause over n Boolean variables x1, . . . , xn. Define f to be true on exactly these clauses. Quasi-Monadic Clones 25 Relations Between Quasi-Monadic Functions 26

Quasi-monadic functions are rather simple, so it is not too surprising that we can actually determine all clones containing only quasi-monadic functions: U there are exactly 6 of them. M The first 4 contain only monotone functions.

0 1 , the trivial clone of projections, ⊥ D 0-preserving quasi-monadic functions, 1-preserving quasi-monadic functions, ⊥ monotone quasi-monadic functions, self-dual quasi-monadic functions, all quasi-monadic functions. The arrows are precise in the sense that there are no other clones in between.

It is not hard to see that each of these classes of functions is in fact a clone. The hard part is to make sure that each quasi-monadic clone appears on the Exercise the list. Figure out exactly what these clones look like.

Self-Dual Functions 27 Threshold Functions 28

We have mentioned that projections and negation are self-dual. Here is another Majority is an important special case of a broader class of functions. important example. Definition Definition (Majority) A threshold function thrn , 0 m n, is an n-ary Boolean function defined by m ≤ ≤ The ternary majority function maj is defined by n 1 if #(i xi = 1) m, maj(x, y, z) = (x y) (x z) (y z) thrm(x) = ≥ ∧ ∨ ∧ ∨ ∧ (0 otherwise.

Then majority is self-dual: Lots of Boolean functions can be defined in terms of threshold functions. n maj(x, y, z) = (x y) (x z) (y z) thr0 is the constant tt. ∧ ∨ ∧ ∨ ∧ n = (x y) (x z) (y z) thr1 is n-ary disjunction. ∧ ∧ ∧ ∧ ∧ n = (x y) (x z) (y z) thrn is n-ary conjunction. ∨ ∧ ∨ ∧ ∨ = maj(x, y, z) thrn(x) thrn (x) is the counting function: “exactly k out of n.” k ∧ ¬ k+1

Counting with 8 Variables 29 Affine Functions 30

Definition An affine Boolean function has the form

f(x) = a0 aixi ⊕ i M where the ai 2 are fixed. ∈

Here denotes exclusive or (equivalently, addition modulo 2). If a0 = 0 we ⊕ have a linear Boolean function. Note that these functions are additive:

f(x y) = f(x) f(y) ⊕ ⊕

Claim The collection A of all affine functions is a clone. Exhaustion sets in . . . 31 Emil Post 32

At this point the literate reader might wonder whether this is the beginning of an infinite story: every time you turn around, a new clone pops up somewhere. The same Post as in Post’s On and on and on . . . problem and Post tag systems.

Post found a classification of As it turns out, the world is actually pretty tame. all clones, published in 1941.

Classification 33 Generators 34

Careful inspection shows that all the clones in the diagram have a finite set of generators: for any clone there are finitely many Boolean functions f1, . . . , fr C so that the least clone containing all fi is . C Here are some examples:

, > ∨ ¬ P0 , ∨ ⊕ P1 , ∨ → P if-then-else M , , 0, 1 ∨ ∧ D maj, ¬ A , 0 ↔ U , 0 ¬

Since our diagram contains all clones it follows that all Boolean clones are finitely generated.

Aside: Finite-Basis Theorem 35 Alas, there are infinitely many . . . 36

One can generalize the property of being 0-preserving:

However, one can also directly show that Boolean clones must be finitely Definition generated. The proof rests on the following strange property. A Boolean function f is m-fold 0-preserving if

Definition x1 ... xm = 0 implies f(x1) ... f(xm) = 0 ∧ ∧ ∧ ∧ An n-ary Boolean function f is a rejection function if for all a 2: ∈ Call this class Zm and let Z = Zm. ∞ #(i xi = a) n 1 implies f(x) = a ≥ − T

Claim For example, majority is a rejection function. Zm and Z are clones. ∞

Lemma Note that Zmis properly contained in Zm 1. For m 2, Zm is generated by m+1 − ≥ thrm and . As it turns out, f Z iff f(x) xi. A clone containing a rejection function is finitely generated. ¬ → ∈ ∞ ≤ There also are variants by adding monotonicity and/or 1-preservation.

And we can do the same for 1-preserving functions. Post’s Lattice 37 Satisfiability and Post’s Lattice 38

The largest clones other than are M, D, A, P0 and P1. Suppose we have a finite set C of logical connectives. > How hard is Satisfiability if the formula is written in terms of the connectives in There are eight infinite families of clones, indexed by a simple numerical C? parameter.

There are a few “sporadic” clones. Theorem (H. R. Lewis 1979) Satisfiability for C is -complete if the clone generated by C contains Z . NP ∞ That’s it, no other clones exist. Otherwise the problem is in polynomial time.

The original proof was supposedly 1000 pages long, the published version is still The proof exploits Post’s lattice from above. around 100 pages.

Back to Bases 39

There are exactly two functionally complete sets of size 1: NAND and NOR,  Boolean Functions discovered by Peirce around 1880 and rediscovered by Sheffer in 1913 (Pierce arrow and Sheffer stroke).

 Clones and Post It follows from Post’s result that a set of Boolean functions is a basis iff for each of the following 5 classes, it contains at least one function that fails to lie in the class: 3 Minimal Automata monotonic affine self-dual  Binary Decision Diagrams truth-preserving false-preserving

Challenge 41 Boolean Functions as Languages 42

Suppose we have some Boolean function f : 2n 2 . In the spirit of Is there an implementation for Boolean functions that makes it easy to decide → equivalence? disjunctive normal form we can represent f by the collection of all the Boolean vectors a 2n such that f(a) = 1. ∈

Obviously there are complexity theoretic constraints: if the implementation worked perfectly we could easily solve Tautology. But then we can think of this collection as a binary language:

n n Lf = a 2 f(a) = 1 2 So what we want is a data structure plus algorithms that work well in some { ∈ | } ⊆ interesting cases (but we full-well expect failure in others).

One plausible line of attack is to use some kind of canonical normal form: if we This language is, of course, finite, so there is a finite state machine that accepts represent the function in normal form equivalence comes down to equality (or it. In fact, the partial minimal DFA is just a DAG: all words in the language perhaps isomorphism). correspond to a fixed-length path from the initial state to the unique final state. UnEqual 43 The Fix 44

The reason our DFA is large, while the formula is small, is that the machine has to contend with input bits x and x being far apart. We already know an example: the UnEqual language i i+k By contrast, in the formula (rather, the parse tree), they are close together. 2k k Lk = uv 2 u = v 2 { ∈ | 6 ∈ } This suggests a way to get around this problem. Change the language to k k It turns out that the state complexity of Lk is 3 2 k + 2 = Θ(2 ). · − 2k Kk = x 2 i (x2i = x2i+1) { ∈ | ∃ 6 } This is uncomfortably large since this Boolean function has a very simple assuming 0-indexing. In other words, the corresponding formula is now description as a propositional formula:

UE0(x) = x2i x2i+1 UE(x) = xi xi+k ⊕ ⊕ _ _ This formula clearly has size Θ(k). You might worry that these shenanigans won’t generalize, and you would be right. But let’s ignore this for the time being.

Much Better 45 The Complement 46

The state complexity of Kk is much smaller than for Lk: 5k.

0 1

0 1

1 0 1 0 0 1 0 1

0 1 0, 1 1 0 1 0 0, 1 0 1 0, 1 0, 1 The partial minimal DFA for the negation of the UnEqual function (aka as the Equal function). The minimal DFA for K2.

The Complement? 47 Example: n = 4, #12346 48

Note that complementation here is a bit weird: by flipping final and non-final states in a DFA M we get a DFA M 0 such that

? (M 0) = 2 (M). L − L

But that’s not really what we want for f, instead we need M 0 such that

n (M 0) = 2 (M). L − L We need to fix things up a bit. The obvious tree automaton on 31 states for the Boolean function

( x1 x2 x4) (x2 x3) The same holds for product automata for unions and intersections when the ¬ ∧ ¬ ∧ ∨ ∧ ¬ variables are not the same. For example, one needs to be careful when constructing the machine for the conjunction of, say, f(x, z) and g(x, y, z, u). Trim Part 49 Minimal Partial 50

MDA 51 Four Arguments 52

Here are the frequencies of the state complexities of all the minimal automata The good news is this: because of the uniqueness of the minimal DFA, we do associated with the 65536 Boolean functions of 4 arguments. have a some kind of canonical normal form. In particular, to check whether two Boolean functions are equivalent, we can check their minimal DFAs for 1 1 isomorphism. This representation is known as minimal deterministic automaton 6 81 (MDA). 7 162 8 828 9 3264 Still, from the previous comments it looks like this is not quite the right 10 11040 setting. 11 22440 12 27720

Here is another nuisance: the DFA for Kk should not have the chain on the right: it should just “output” 1 when we get over there and be done. This is Recall that the natural tree automata all have size 31. justified since UE0(0, 1, y) = 1 no matter what y is, The automaton of size 1 corresponds to the constant false functions, the others actually describe languages L 24. ⊆

Improvements 54

 Boolean Functions

One might conjecture that we have to relax the constraints on our diagrams a bit: more choices should make it easier to build them (of course, while  Clones and Post preserving normal form properties).

 Minimal Automata One fairly natural idea is to drop the fixed-length condition in an MDA: we may reach the exit without reading all the variables (in fact, always as soon as enough information has been accumulated to determine the value of the function). 4 Binary Decision Diagrams The Key Papers 55 Decision Trees 56

C. Y. Lee Binary Decision Programs x Bell System Tech. J., 4 (1959) 4: 985–999. 0 1

y y S. B. Akers 0 1 0 1 Binary Decision Diagrams IEEE Trans. Computers C-27 (1978) 6: 509–516. z z z z 0 1 0 1 0 1 0 1

a0 a1 a2 a3 a4 a5 a6 a7 R. E. Bryant Graph-based Algorithms for Boolean Function Manipulation IEEE Trans. Computers C-35 (1986) 8: 677–691. A general purpose method: express a Boolean function as a decision tree.

Reductions 57 Example 58

In its abstract form, there is not much one can do with a full decision tree. x But if we have concrete values for the ai, things change. 0 1

y y We can reduce the data structure according to the following reduction rules. 0 1 0 1

Merge terminal nodes with the same label. z z z z Merge nodes with the same descendants. 0 1 0 1 0 1 0 1

Skip over nodes with just one child. 1 0 1 0 0 0 0 1

Except for the Skip part, this is the same as state merging in the realm of FSMs. The function f = (x + z)(x + y)(x + y + z).

Preserving Sanity 59 Merging Terminals 60

x x

y y y y

z z z z z z z z

1 0 1 0 0 0 0 1 0 1

A more legible version. Merging Nodes 61 Skipping Nodes 62

x x

y y y

z z z z z z

0 1 0 1

Final Result 63 Reality Check 64

x Just to be clear: reducing a given decision tree is mostly of conceptual interest. The full decision tree always has exponential size, so we can only handle it for a y few variables.

z z What we really need is a way to get at the reduced version directly: starting from the constant functions, use Boolean operators to construct more complicated ones. All these intermediate functions are kept in reduced form, so hopefully none of them will exhibit exponential blowup. 0 1

Boole-Shannon Expansion 65 If-Then-Else 66

A good way to think about expansions is in terms of if-then-else: Here is a more algebraic description of this basic idea. ite(x, y1, y0) = x y1 + x y0

Suppose f is a Boolean function with variable x. Define the cofactors of f by Together with the Boolean constants, if-then-else provides yet another basis: fx(u, x, v) = f(u, 1, v)

fx(u, x, v) = f(u, 0, v) x = ite(x, 0, 1) Note that fx does not depend on x. Also fxy = fyx. ¬ x y = ite(x, y, 0) ∧ The internal nodes of our DAGs are based on the standard Boole-Shannon x y = ite(x, 1, y) expansion ∨ x y = ite(x, y, 1) f = x fx + x fx ⇒ x y = ite(x, ite(y, 0, 1), y) ⊕ If-Then-Else Normal Form 67 Example 68

Consider the Boolean function f (x1 = y1) (x2 = y2) (strictly speaking we ≡ ∧ should have written xi yi, but that’s harder to read). ⇔ It follows that we can define if-then-else normal form (INF): the only allowed The INF of f is given by operations are if-then-else and constants. Moreover, tests are performed only f = ite(x1, f1, f0) on variables (not compound expressions). f0 = ite(y1, 0, f00) f = ite(y , f , 0) We can express Boole-Shannon expansion in terms of if-then-else: 1 1 11 f00 = ite(x2, f001, f000)

f = ite(x, fx, fx) f11 = ite(x2, f111, f110)

f000 = ite(y2, 0, 1)

f001 = ite(y2, 1, 0) We also assume that the variables are given in a fixed order x, y, z . . . so that f110 = ite(y2, 0, 1) we can lighten notation a bit and write f101 instead of fxyz. f111 = ite(y2, 1, 0)

Here the implicit order is x1, y1, x2, y2. Substituting back into the first line we get INF.

Example 69 The DAG 70

Sharing common subexpressions (such as f000 and f110): f

f0 f1 f = ite(x1, f1, f0)

f0 = ite(y1, 0, f00) f00 f1 = ite(y1, f00, 0)

f00 = ite(x2, f001, f000) f000 f001 f000 = ite(y2, 0, 1)

f001 = ite(y2, 1, 0)

0 1 We can now interpret these functions as nodes in a DAG just like the one obtained by reducing a decision tree.

The fully reduced DAG.

Same 71 Binary Decision Diagrams 72

x1 Fix a set Var = x1, x2, . . . , xn of n Boolean variables. { }

y1 y1 Definition A binary decision diagram (BDD) (over Var) is a rooted, directed acyclic graph x2 with two terminal nodes (out-degree 0) and interior nodes of out-degree 2. The interior nodes are labeled in Var. y2 y2 We write var(u) for the labels. The two successors of an interior node are traditionally referred to as lo(u) and 0 1 hi(v), corresponding to the false and true branches. We can think of the terminal nodes as being labeled by constants 0 and 1, indicating values false and true.

The DAG again, but this time labeled by the variables that are tested at each particular level. Ordered BDDs 73 Reduced Ordered BDDs 74

Fix an ordering x1 < x2 < . . . < xn on Var. For simplicity assume that Definition xi < 0, 1. A OBDD is reduced (ROBDD) if it satisfies Uniqueness: there are no nodes u = v such that Definition 6 A BDD is ordered (OBDD) if the label sequence along any path is ordered. var(u) = var(v), lo(u) = lo(v), hi(u) = hi(v) Non-Redundancy: for all nodes u: lo(u) = hi(u) Thus 6 var(u) < var(lo(u)), var(hi(u))

The uniqueness condition corresponds to shared subexpressions: we could In the corresponding INF, the variables are always ordered in the sense that merge u and v. Non-redundancy corresponds to taking shortcuts: we can skip ahead to the next test. ite(x, ite(y, f1, f2), ite(z, f3, f4)) implies x < y, z Since ROBDDs are the most important type, we simply refer to them as BDD.

Canonicity 75 Proof 76

By a straightforward induction we can associate any BDD with root u with a n Boolean function Fu: The claim is clear for n = 0, so consider a function f : B B , written → f(x, y).

F0 = 0 F1 = 1 By IH we may assume that the BDDs for f(0, y) and f(1, y) are unique, say, with roots u and v, respectively. Fu = ite(var(u),Flo(u),Fhi(u)) If f(0, y) = f(1, y) this is also the BDD for f.

If the BDDs under consideration are also ordered and reduced we get a useful Otherwise introduce a new node w and set representation. λ(w) = x lo(w) = u hi(w) = v

The resulting BDD represents f and is unique. Theorem (Canonical Form Theorem) 2 n For every Boolean function f : B B there is exactly one ROBDD u such → that f = Fu.

Reduction 77 Complete Construction 78

Suppose we have a decision tree for a Boolean function and would like to Here is the key idea: suppose we construct all the Boolean functions in a transform it into the (unique) BDD. particular computation from scratch, starting from the constants and applying the usual arsenal of Boolean connectives. We can use a bottom-up traversal of the DAG to merge or eliminate nodes that violate Uniqueness or Non-Redundancy. In this scenario, all functions are kept in canonical form; if an intermediate This traversal requires essentially only local information and can be handled in result is not canonical, clean it up using the reduction machinery. As a (expected) linear time using a hash table. consequence, every one of these functions has a unique representation: a particular node u in a BDD represents function Fu (over some set of variables).

Of course, if the starting point is indeed a full decision tree this method is of little interest. But reduction is important as a clean-up step in the construction of complicated BDDs form simpler ones. But then equivalence testing is O(1): we just compare two pointers. Very nice. Operations on BDDs 79 Negation 80

Obviously we need a collection of operations on BDDs. The first operation to consider is simple negation: obtaining a BDD for f from a BDD for f. Reduce Turn a decision tree into a BDD. Note that the two BDDs differ only in the terminal nodes, which are swapped. Apply Given two BDDs u and v and a Boolean operation , determine  This can be exploited to make complementation constant time. the BDD for Fu Fv.  Restrict Given a BDD u and a variable x, determine the BDD for Clearly all nodes in a BDD represent Boolean functions, it is natural to keep Fu[a/x]. track of them via pointers. By adding a “negation bit” to the pointers (complement vs. regular pointers), we can obtain f at constant cost.

Note: The purpose of Reduce is not to convert a raw (exponential size) We can also add negation bits to all the internal edges of the diagram, decision tree, but to clean up after other operations. indicating that the next node represents g rather than g.

Preserving Canonicity 81 Ops 82

In this setting, one can get away with a single terminal node, say, 1. Since all Cofactors coexist peacefully with Boolean operations. paths end at 1, evaluation comes down to counting the number of complement edges (modulo 2). Proposition

f x = fx, (f + g)x = fx + gx, (fg)x = fxgx However, a little care is needed to preserve canonicity. We have Hence, the apply operation can be handled by recursive, top-down algorithm

f = xfx + xfx = (xf x + xf x) since

ite(x, s, t) ite(x, s0, t0) = ite(x, s s0, t t0) so there are two ways to represent f. We adopt the convention that the then    branch is chosen to be regular.

Since complementation is O(1), it even suffices to just implement, say, logical With this convention, our BDDs are still canonical. and.

Conjunction 83 Apply for Conjunction 84

So suppose we want to build the BDD for fg, given BDDs for f and g. handle terminal cases The algorithm uses top-down recursion. The exit conditions are easy: suppose x is next variable

compute h1 = fx gx • f, g = 0 0 compute h2 = f g • x x f = 1 g if h1 = h2 return h1 g = 1 f • return BDD for ite(x, h1, h2) f = g g • f = g 0 With proper hashing the running time is O( u v ), though in practice the | || | Note that they can all be checked in constant time. method is often faster. Decision Problems and BDDs 85 Quantifiers 86

In principle, we can even handle quantified Boolean formulae by expanding the Suppose we have constructed a BDD for a Boolean function f. quantifiers:

Then it is trivial to check if f is a tautology: the test is for f = 1 and is constant time. x ϕ(x) ϕ[0/x] ϕ[1/x] ∃ ≡ ∨ x ϕ(x) ϕ[0/x] ϕ[1/x] Likewise, it is trivial to check if f is satisfiable: we need f = 0. ∀ ≡ ∧ 6 In fact, we can count satisfying truth assignments in O( u ): they correspond | | to the total number of paths (including potentially phantom variables) from the So this comes down to restriction, a (quadratic) apply operation, followed by root to terminal 1. reduction.

We can even construct a BDD s for all satisfying truth assignments in time Alas, since SAT is just a problem of validity of an existentially quantified u O(n s ) where s = O(2| |). Boolean formula, this is not going to be fast in general. QBF in general is even | | | | PSPACE-complete.

Bad News 87 Variable Ordering 88

Of course, there is no way to avoid exponential worst-case cost when building a n BDD representing some Boolean function from scratch: after all, there are 22 In general, the size of a BDD depends drastically on the chosen ordering of functions to be represented. So some representations must have size at least 2n variables (see the UnEqual example above). bits for information-theoretic reasons.

It would be most useful to be able to construct a variable ordering that works well with a given function. In practice, variable orderings are computed by As it turns out, addition of k-bit natural numbers can be expressed nicely in a heuristic algorithms and can sometimes be improved with local optimization linear size BDD. Alas, multiplication causes problems (this should sound and even simulated annealing algorithms. familiar).

Alas, it is NP-hard to determine whether there is an ordering that produces a BDD of size at most some given bound. Lemma BDDs for multiplication require exponential size.

Symmetric Functions 89 Average Performance 90

Note that for a symmetric Boolean functions variable ordering does not matter: Again: for information-theoretic reasons, there is no way a canonical form such we always get the same BDD. as a MDA or BDD is always small.

conjunctions, disjunctions As a matter of fact, one can show that both worst case and the average sizes parity (xor) of MDA and BDD agree in the limit (for many variables).

threshold (counting) Moreover, there are exponential gaps between the size of standard representations for some Boolean functions. So, to really deal with Boolean Exercise functions effectively, one has to have many different implementations available–and switch back and forth, to whatever works best under the Figure out what the BDD for n-ary xor looks like. circumstances.