Binary Decision Diagrams Binary Decision Diagrams (BDDs) are a class of graphs that can be used as data structure for compactly representing boolean functions. BDDs were introduced by R. Bryant in 1986. BDDs are used to solve equivalence problems between formulas of propositional logic. Very important in the areas of hardware design and hardware optimization. 1
Graphs Recall some basic graph-theoretical concepts: directed graph node edge predecessor successor path cycle acyclic graph tree forest 2
Boolean Functions A boolean function of arity n 1 is a function {0, 1} n {0, 1}. Examples: or(x 1,x 2 ) = { 1 if x1 = 1 or x 2 = 1 0 if x 1 = 0 or x 2 = 0 3
if then else(x 1,x 2,x 3 ) = { x2 if x 1 = 1 x 3 if x 1 = 0 Z.B.: if then else(1, 0, 1) = 0, if then else(0, 0, 1) = 1 { 1 if x1 + x 2 = x 3 x 4 sum(x 1,x 2,x 3,x 4 ) = 0 otherwise Z.B.: sum(1, 1, 1, 0) = 1 (because 1 + 1 = 10), sum(0, 0, 0, 1) = 0 (because 0 + 0 = 00). 4
majority n (x 1,...,x n ) = 1 if the majority of x 1,...,x n has value 1 0 otherwise Z.B.: majority 4 (1, 1, 0, 0) = 0, majority 3 (1, 0, 1) = 1 parity n (x 1,...,x n ) = 1 if the number of inputs x 1,...,x n equal to 1 is even 0 otherwise Z.B.: parity 3 (1, 0, 1) = 1, parity 2 (1, 0) = 0 5
Formulas and boolean functions Let F be a formula, and let n be a number such that all atomic formulas occurring in F belong to {A 1,...,A n }. Example: F = A 1 A 2, n = 2, but also n = 3! We define the boolean function f n F : {0, 1}n {0, 1}: f n F(x 1,...,x n ) = truth value of F under the assignment that sets A 1,...,A n to x 1,...,x n 6
Example: For F = A 1 A 2 : f 3 F f 2 F (0, 1) = value of 0 1 = 0 (0, 1, 1) = value of 0 1 = 0 Remark: If all of {A 1,...,A n } occur in F, then ff n truth table of F. is essentially the Convention: We write e.g. f(x 1,x 2,x 3 ) = x 1 (x 2 x 1 ), meaning f = f 3 F for the formula F = A 1 (A 2 A 1 ). 7
Fact: Let F 1 and F 2 be two formulas, and let n be a number such that all atomic formulas occurring in F 1 or F 2 belong to {A 1,...,A n }. Then f n F 1 = f n F 2 iff F 1 F 2. Example: F 1 = A 1, F 2 = A 1 (A 2 A 2 ). f 2 F 1 (0, 0) = 0 = f 2 F 2 (0, 0) f 2 F 1 (0, 1) = 0 = f 2 F 2 (0, 1) f 2 F 1 (1, 0) = 1 = f 2 F 2 (1, 0) f 2 F 1 (1, 1) = 1 = f 2 F 2 (1, 1) Convention: The constants 0 and 1 represent the only two boolean functions of arity 0. 8
sum as binary decision tree A boolean function can be represented by a decision tree x1 1 0 x2 1 0 1 0 x3 x3 x3 x3 1 0 1 0 1 0 1 0 x4 x4 x4 x4 x4 x4 x4 x4 x2 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 0 1 0 0 0 0 1 0 0 0 1 0 0 0 0 1 9
Variable order A decision tree can use a variable order different from the order used in the function. x3 1 0 x1 1 0 1 0 x4 x4 x4 x4 1 0 1 0 1 0 1 0 x2 x2 x2 x2 x2 x2 x2 x2 x1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 0 0 1 0 0 0 0 0 0 1 0 0 1 0 0 1 10
A variable order is a bijection b: {1,...,n} {x 1,...,x n } We say that b(1),b(2),b(3),...,b(n) are the first, second, third,..., n-th variable w.r.t. the order b. We denote the bijection b(1) = x i1,...,b(n) = x in by x i1 < x i2 <... < x in. 11
Binary decision trees A decision tree for the variable order x i1 <... < x in is a tree satisfying the following conditions: (1) All leaves are labelled by 0 or by 1. (2) All other nodes are labelled by a variable and have exactly two children, the 0-child and the 1-child. The edges leading to these children are labelled by 0 resp. by 1. (3) If the root is not a leave, then it is labelled by x i1. (4) If a node is labelled by x in then its two children are leaves. (5) If a node is labelled by x ij and j < n, then its two children are labelled by x ij+1. 12
Every path of a decision tree determines an assignment of the variables x i1,...x in and vice versa. The boolean function f T represented by a decision tree T is defined as follows: f T (x 1,...,x n ) = label of the leaf reached by the path corresponding to the assignment x i1 x i2... x in A binary decision forest ist a forest of decision trees with the same variable order. A decision forest represents the set of functions represented by its elements. 13
Binary Decision Diagrams (informally) A BDD (multibdd) is a compact representation of a binary decision tree (decision forest). A BDD (multibdd) is obtained from a decision tree (forest) through repeated application of two compression rules (see example in the next slide): Rule 1: Sharing of identical subtrees. Rule 2: Elimination of nodes for which the 0-child and the 1-child coincide (redundant nodes). The rules are applied until all subtrees are different and there are no redundant nodes. 14
Example: sharing of subtrees x1 1 0 x2 1 0 1 0 x3 x3 x3 x3 1 0 1 0 1 0 1 0 x4 x4 x4 x4 x4 x4 x4 x4 x2 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 0 1 0 0 0 0 1 0 0 0 1 0 0 0 0 1 All 0- und 1-leaves are merged. 15
Example: sharing of subtrees x1 1 0 x3 x2 1 0 1 0 x3 1 0 1 0 1 0 1 0 x4 x4 x4 x4 x4 x4 x4 x4 x3 x2 x3 1 0 All 0- und 1-leaves are merged. 16
Example: sharing of subtrees x1 1 0 x2 1 0 1 0 x2 x3 x3 x3 x3 x4 x4 x4 1 0 Identical x 4 -nodes are merged. 17
Example: sharing of subtrees x1 x2 x2 x3 x3 x3 x4 x4 x4 1 0 Identical x 3 -nodes are merged. 18
Example: removing redundant nodes x1 x2 x2 x3 x3 x3 x4 x4 x4 1 0 Redundant x 4 -node is removed 19
Example: removing redundant nodes x1 x2 x2 x3 x3 x3 x4 x4 1 0 Redundant x 4 -node is removed 20
Formal Definition of BDDs A BDD for a given variable order is an acyclic directed graph satisfying the following properties: (1) There is exactly one node without predecessors (the root) (2) There is one or two nodes without succesors, labelled by 0 or 1 (if there are two then they carry different labels). (3) All other nodes are labelled by a variable and have exactly two distinct children, the 0-child and the 1-child. The edges leading to these children are labelled by 0 resp. by 1. (4) A child of a node is labelled by 0, by 1, or by a variable larger than the label of its parent w.r.t. the variable order. (5) All descendant-closed subgraphs of the graph are non-isomorphic. 21
MultiBDDs A multibdd is an acyclic graph satisfying (2)-(5) together a distinguished nonempty subset of nodes called the roots. Every node without predecessors is a root, but other nodes may also be roots. A multibdd represents a set of boolean functions, one for each root. 22
Remarks Remark: A closed subgraph of a BDD is again a BDD. Remark: The function true n (x 1,...,x n ) given by true n (x 1,...,x n ) = 1 for every x 1,x n {0, 1} n is represented, for every n 1 and for every variable order, by the BDD consisting of one single node labelled by 1. Similarly for false n (x 1,...,x n ) 23
Relevance of variable orders The variable order can have large impact on the size of the BDD. Example: f(x 1,...,x 2n ) = (x 1 x n+1 ) (x 2 x n+2 ) (x n x 2n ) Size grows exponentially in n for x 1 < < x n < x n+1 < < x 2n. Size grows linearly in n for x 1 < x n+1 < x 2 < x n+2 <... < x n < x 2n. Problem in practice: finding a good order. 24
Canonicity of BDDs We show that for a given boolean function and a given variable order there is a unique BDD representing the function. More generally (but simpler to prove!), we show that for every set of boolean functions of the same arity and for every variable order there is a unique multibdd representing the set. 25
The functions f[0] und f[1] Lemma I: Let f be a boolean function of arity n 1. There are exactly two boolean functions f[0] und f[1] of arity (n 1) satisfying f(x 1,...,x n ) = ( x 1 f[0](x 2,...,x n )) (x 1 f[1](x 2,...,x n )) ( ) Proof: The functions f[0] and f[1] defined by f[0](x 2,...,x n ) = f(0,x 2,...,x n ) and f[1](x 2,...,x n ) = f(1,x 2,...,x n ) satisfy (*). Let f 0 and f 1 be arbitrary functions satisfying (*). Then f(x 1,...,x n ) = ( x 1 f 0 (x 2,...,x n )) (x 1 f 1 (x 2,...,x n )) By the properties of and we have f(0,x 2,...,x n ) = f 0 (x 2,...,x n ) and together with f(0,x 2,...,x n ) = f[0](x 2,...,x n ) we get f 0 = f[0]. The proof that f 1 = f[1] holds is analogous. 26
Let f: {0, 1} n {0, 1} be a boolean function, let B be a BDD with variable order x 1 < x 2 <... < x n, and let v be the root of B. Define the nodes v[0] and v[1] as follows: (1) If v is labelled by x 1, then v[0] and v[1] are the 0-child and the 1-child of v. (2) Otherwise, v[0] = v = v[1]. Lemma II: B represents the function f iff v[0] and v[1] represent the functions f[0] and f[1], respectively. Proof: Easy. 27
Theorem: Let F be a nonempty set of boolean functions of arity n and let x i1 <... < x in be a variable order. There is exactly one multibdd that follows this order and represents F. Proof: We consider the order x 1 < x 2 <... < x n, for other orders the proof is similar. Proof by induction on the arity n. Basis: n = 0. There are exactly two boolean functions with n = 0, namely the constants 0 and 1, and two BDDs K 0,K 1 consisting of one single node labelled by 0 or by 1. The set {0} is represented by K 0, the set {1} by K 1, and the set {0,1} by the multibdd consisting of K 0 and K 1. 28
Step: n > 0. Let F = {f 1,...,f k }. Define F = {f 1 [0],f 1 [1],...,f k [0],f k [1]}, where f i [0] and f i [1] are as in Lemma I. By induction hypothesis there is exactly one multibdd B with roots v 10,v 11,...,v k0,v k1 representing F. I.e., for every function f i [j] the root v ij represents f i [j]. 29
Let B be the multibdd with roots v 1,...v n obtained from B after executing the following steps for i = 1, 2,..., k: If v i0 = v i1 then set v i := v i0. (In this case v i0 represents f i.) If v i0 v i1 and B has a node v such with v i0 as 0-child and v i1 as 1-child then set v i := v. If v i0 v i1 and B contains no such node then add a new node v i having v i0 as 0-Kind and v i1 as 1-Kind. (So v i represents f i, see Lemma II.) Clearly, B represents F. We now show that B is the only multibdd representing F. 30
Let B be an arbitrary multibdd with roots ṽ 1,...ṽ n representing F. By Lemma II, B contains nodes ṽ1 [0],ṽ 1 [1],...,ṽ k [0],ṽ k [1] representing the functions of F. By induction hypothesis, the multibdd containing these nodes and all its descendants is the multibdd B. In particular, we have v ij = ṽ i [j] for every i {1,...,k} and j {0, 1}. Let v i and ṽ i be the roots of B and B, representing f i. By Lemmas I und II, v i0 and ṽ i [0] represent f[0], and v i1 and ṽ i [1] represent f[1]. Since v i [0] = ṽ i [0] and v i [1] = ṽ i [1] we get v i = ṽ i. So B and B are equal. 31
Computing BDDs from Formulas Goal: Given a formula F over the atomic formulas A 1,...,A n and a variable order for {x 1,...,x n }, compute a BDD representing f F (x 1,...,x n ). Naive procedure: Compute the decision tree of f F and reduce it using the compression rules. Problem: The decision tree is too large! Better procedure (idea): Compute recursively the multibdd representing {f F[Ai /0],f F[Ai /1]} for a suitable A i, and derive from it the BDD for f F, where F[A i /0] bzw. F[A i /0] are the formulas obtained by replacing every occurrence of A i by 0 resp. by 1. In the next slides we formalize this idea. 32
Let S = {F 1,...,F n } be a nonempty set of formulas. We define a procedure multibdd(s) that returns the roots of a multibdd representing the set {f F1,...,f Fn }. K 0 denotes the BDD with only one node labelled by 0. K 1 denotes the BDD with only one node labelled by 1. A proper formula is a formula containing at least one occurrence of a variable (i.e., not only 0 and 1). An atomic formula A i is smaller than A j if x i appears before x j in the variable order. 33
The function multibdd(s) if S contains no proper formulas then if all formulas of S are equivalent to 0 then return {K 0 } else if all formulas in S are equivalent to 1 then return {K 1 } else return {K 0,K 1 } else choose a proper formula F S. Let A i be the smallest atomic formula occurring in F. Let B = multibdd( (S \ {F }) {F[A i /0],F[A i /1]} ). Let v 0,v 1 be the roots of B representing F[A i /0],F[A i /1]. if v 0 = v 1 then return B else add a new node v with v 0,v 1 as 0- and 1-child (if such a node does not exist yet); return (B \ {v 0,v 1 }) {v} 34
Equivalence problems Given two formulas F 1, F 2, the following algorithm decides whether F 1 F 2 holds: Choose a suitable variable order x 1 <... < x n. Compute a multibdd for {F 1,F 2 }. Check whether the roots v F1,v F2 are equal. For digital circuits: the BDDs are not derived from formulas, but directly from the circuits. 35
Operations on BDDs Given: two formulas F,G over the atomic formulas A 1,...,A n, a variable order for {x 1,...,x n }, a multibdd with two roots v F,v G representing the functions f F (x 1,...,x n ) and f F (x 1,...,x n ), and a binary boolean operation (e.g.,,, ) Goal: compute a BDD for the function f F G (x 1,...,x n ). With our convention we have f F G = f F f G 36
Idea Lemma: (f F f G )[0] = f F [0] f G [0] and (f F f G )[1] = f F [1] f G [1]. Proof: Exercise. Algorithm: (for the order x 1 < x 2 <... < x n, similar for others) Compute a multibdd for {f F [0] f G [0],f F [1] f G [1]}. (Recursively.) Use the Lemma to build a BDD for f F G (x 1,...,x n ). 37
The function Or(v F, v G ) if v F = K 1 or v F = K 1 then return K 1 else if v F = v G = K 0 then return K 0 else let v F0,v G0 be the nodes for F[0],G[0] and let v F1,v G1 be the nodes for F[1],G[1] v 0 := Or(v F0,v G0 ); v 1 := Or(v F1,v G1 ) if v 0 = v 1 then return v 0 else add a new node v with v 0,v 1 as 0- and 1-child (if such a node does not exist yet); return v 38
Implementing BDDs Source: An introduction to Binary Decision Diagrams Prof. H.R. Andersen http://www.itu.dk/people/hra/notes-index.html 39
Data structures BDD-nodes coded as numbers 0, 1, 2,... with 0, 1 for the end nodes. BDD-nodes are stored in a table T:u (i,l,h) where i,l,h are the label, the 0-child and the 1-child of u. (Here l stands for low and h for high.) We maintain a second table so that following invarinat holds: H: (i,l,h) u T(u) = (i,l,h) iff H(i,l,h) = u 40
Basic operations on T: init(t): Initializes T with 0 and 1 add(t,i,l,h): Adds node with attributes (i,l,h) to T and returns it var(u), low(u), high(u): Returns the variable, 0-child, 1-child of u Basic operations on H: init(h): Initializes H as the empty table member(h,i,l,h): Checks whether (i,l,h) belongs to H lookup(h, i, l, h): Returns the node H(i, l, h) insert(h, i, l, h, u): Adds (i, l, h) u to H (if not yet there) 41
The function Make(i, l, h) Look in H for a node with atributes (i,l,h). If found, then return it. Otherwise create a new node and return it. Make(i,l,h) 1: if l = h then return l 2: else if member(h, i, l, h) then 3: return lookup(h, i, h, l) 4: else u := add(t,i,l,h) 5: insert(h,i,l,h,u) 6: return u 42
Implementing Or Problem: the function can be called many times with the same arguments. Solution: dynamic programming. The results of all calls are stored. Each call checks first if the result has already been computed earlier. Or(u 1,u 2 ) 1: init G 2: return Or (u 1,u 2 ) 43
Or (u 1, u 2 ) 1: if G(u 1, u 2 ) empty then return G(u 1, u 2 ) 2: else if u 1 = 1 or u 2 = 1 then return 1 3: else if u 1 = 0 and u 2 = 0 then return 0 4: else if var(u 1 ) = var(u 2 ) then 5: u := Make(var(u 1 ), Or (low(u 1 ), low(u 2 )), Or (high(u 1 ), high(u 2 ))) 6: else if var(u 1 ) < var(u 2 ) then 7: u := Make(var(u 1 ), Or (low(u 1 ), u 2 ), Or (high(u 1 ), u 2 )) 8: else u := Make(var(u 2 ), Or (u 1, low(u 2 )), Or (u 1, high(u 2 ))) 9: G(u 1, u 2 ) = u 10: return u 44