Binary Decision Diagrams (BDDs) Pingqiang Zhou ShanghaiTech University
Computational Boolean Algera Representations Applying unate recursive paradigm (URP) in solving tautology is a great warm up example. Shows ig idea: Boolean functions as things we manipulate with software. Data structure + operators But URP is not the real way we do it. Let s look at a real, important, elegant way to do this Binary Decision Diagrams: BDDs 2
Binary Decision Diagrams (BDD) Originally studied y several people got practically useful in 1986 Randal Bryant of CMU made reakthrough on Reduced Ordered BDD (ROBDD). http://www.cs.cmu.edu/~ryant/ 3
Binary Decision Diagrams for Truth Tales Big Idea #1: Binary Decision Diagram Turn a truth tale for the Boolean function into a Decision Diagram. In simplest case, graph is just a tree. By convention, don t draw arrows on the edges, we know where they go. Decision Tree x1 x3 x3 x3 x3 x1 x3 f 0 0 0 0 0 0 1 0 0 1 0 0 0 1 1 1 1 0 0 0 1 0 1 1 1 1 0 0 1 1 1 1 4 0 0 0 1 0 1 0 1
Binary Decision Diagrams Vertex represents a variale. Edge out of a vertex is a decision (0 or 1) on that variale. Follow green dashed line for 0. Follow red solid line for 1. Function value determined y leaf value. x1 x3 x3 x3 x3 x1 x3 f 0 0 0 0 0 0 1 0 0 1 0 0 0 1 1 1 1 0 0 0 1 0 1 1 1 1 0 0 1 1 1 1 5 0 0 0 1 0 1 0 1
Binary Decision Diagrams Some Terminology The lo pointer to lo child of vertex x1 A variale vertex The hi pointer to hi child of vertex A constant vertex at the leaf of the tree x3 x3 x3 x3 0 0 0 1 0 1 0 1 6 The variale ordering, which is the order in which decisions aout variales are made. Here, it is x1 < < x3.
Ordering Different variale orders are possile. x1 x3 x3 x3 x3 0 0 0 1 0 1 0 1 Order for this sutree is < x3 x1 Order for this sutree is x3 < x3 x3 x3 0 0 0 1 0 0 1 1 7
Binary Decision Diagrams Oservations Each path from root to leaf traverses variales in some order. Each such path constitutes a row of the truth tale, i.e., a decision aout what output is when variales take particular values. However, we have not yet specified anything aout the order of decisions. The decision diagram is NOT unique for this function. 8
Terminology: Canonical form Canonical: Representation that does not depend on gate implementation of a Boolean function. Same function of same variales always produces this exact same representation. Example: a truth tale is canonical (up to variale order). We want a canonical form data structure. 9
Binary Decision Diagrams What s wrong with this diagram representation? It is not canonical, and it is way too ig to e useful (it is as ig as truth tale!) x1 x3 x3 x3 x3 0 0 0 1 0 1 0 1 x1 x3 x3 x3 0 0 0 1 0 0 1 1 10
Binary Decision Diagrams Big idea #2: ordering Restrict gloal ordering of variales. It means: every path from root to a leaf visit variales in the SAME order. Note: it is OK to omit a variale if you don t need to check it to decide which leaf node to reach for final value of function. x1 0 0 0 1 x1 0 0 1 11
Ordering BDD Variales Assign (an aritrary) gloal ordering to vars: x1 < < x3 Variales must appear in this specific order along all paths; ok to skip vars x1 x1 x3 x1 x3 x3 x1 x1 12 Property: No conflicting assignments along path (see each var at most once on path).
Binary Decision Diagrams OK, now what s wrong with it? Variale ordering simplifies things, ut still too ig, and not canonical. Original Decision Diagram x1 x3 x3 x3 x3 0 0 0 1 0 1 0 1 Equivalent, ut Different Decision Diagram x1 x3 x3 x3 0 0 0 1 0 1 13
Binary Decision Diagrams Big Idea #3: Reduction Identify redundancies in the graph that can remove unnecessary nodes and edges. Removal of node and its children, replace with x3 node is an example of this. x1 x3 x3 x3 x3 0 0 0 1 0 1 0 1 x1 x3 x3 x3 0 0 0 1 0 1 14
Binary Decision Diagrams Reduction Why are we doing this? Graph size: Want result as small as possile. Canonical form: For same function, given same variale order, want there to e exactly one graph that represents this function. 15
Reduction Rules Reduction Rule 1: Merge equivalent leaves Just keep one copy of each constant leaf anything else is totally wasteful. Redirect all edges that went into the redundant leaves into this one kept node. Apply Rule 1 to our example... x1 x1 16 x3 x3 x3 x3 0 0 0 1 0 1 0 1 x3 x3 x3 x3 0 1
Reduction Rules Reduction Rule 2: Merge isomorphic nodes Isomorphic = 2 nodes with same variale and identical children Cannot tell these nodes apart from how they contriute to decisions in graph. Note: means exact same physical child nodes, not just children with same lael. NOT isomorphic x x y z isomorphic x x y z y 0 0 0 1 17
Steps of Merging Isomorphic Nodes 1. Remove redundant node. 2. Redirect all edges that went into the redundant node into the one copy that you kept. For the example elow, edges into right x node now into left as well. x x x y z y z 18
Reduction Rules Apply Rule 2, merging redundant nodes, to our example. x1 x1 x3 x3 x3 x3 0 1 isomorphic x3 0 1 x3 19
Reduction Rules Reduction Rule 3: Eliminate Redundant Tests Redundant test: oth children of a node (x) go to the same node (y) so we don t care what value x node takes. Steps 1. Remove redundant node. 2. Redirect all edges into redundant node (x) into child (y) of the removed node. x y y 20
Reduction Rules Apply Rule 3, eliminating redundant tests, to our example. x1 x1 x3 x3 x3 We are done! 0 1 0 1 21
Reduction Rules The aove is a simple example. The reduction process terminates y applying each rule once. But in real case, you may need to iteratively apply Rule 2 and 3. It is only done when you cannot find any match of rule 2 or 3. Is this how programs really do it? No!! We will talk aout that later 22
Binary Decision Diagrams (BDDs) Big Results Recap: What did we do? Start with a decision diagram in the form of a tree, order the variales, and reduce the diagram Name: Reduced Ordered BDD (ROBDD) 23 Big result: ROBDD is a canonical form data structure for any Boolean function. Same function always generates exactly same graph... for same variale ordering. Two functions identical if and only if ROBDD graphs are isomorphic (i.e., same). Nice property: Simplest form of graph is canonical.
BDDs: Representing Simple Things NOTE: In a ROBDD, a Boolean function is really just a pointer to the root node of the graph. ROBDD for f a,,, z = 0 f ROBDD for f a,,, z = 1 f 0 ROBDD for f a,,, z = a f 1 a 24 0 1
ROBDD for AND f a, = a f f f a a a 0 0 0 1 0 1 0 1 Same graph for f a,,, z = a 25
ROBDD for OR f a, = a + f f f a a a 0 1 1 1 0 1 0 1 Same graph for f a,,, z = a + 26
ROBDD for AND/OR on Multiple Inputs f a,, c, d = acd f f a,, c, d = a + + c + d f a c d a c d 27 0 1 0 1
ROBDD for XOR f a, = a f f a a 0 1 1 0 0 1 Same graph for f a,,, z = a 28
ROBDD for XOR on Multiple Inputs f a,, c, d = a c d f a c d c d 0 1 29
Sharing in BDDs Very important technical point: Every BDD node (not just root) represents some Boolean function in a canonical way. BDDs good at extracting & representing sharing of sufunctions in sugraphs. c d f a,, c, d = a c d f c d a c c d d d 0 1 30
BDD Sharing: Multi-Rooted BDD If we are uilding BDDs for multiple functions, then there may e same sugraphs among different BDDs. Don t represent same things multiple times; share them! As a result of sharing, the BDD can have multiple entry points, or roots. Called a multi-rooted BDD. 31
Multi-Rooted BDD: Example Build BDDs for two functions f 1 x 1, x 2, x 3 = x 1 x 2 x 3 f 2 x 1, x 2, x 3 = x 1 + x 2 x 3 f 1 f 2 f 1 f 2 x1 x1 Shaded parts are same Can e shared! x1 x1 x3 x3 x3 0 1 0 1 0 1 32
Review Binary decision diagram (BDD) Big idea #1: Decision diagrams Big idea #2: Assign gloal ordering to variales. Big idea #3: Reduction Merge equivalent leaves Merge isomorphic nodes Eliminate redundant tests Result: Reduced Ordered Binary Decision Diagram (ROBDD) 33
Review ROBDD is a canonical form data structure for any Boolean function. Same function always generates exactly same graph... for same variale ordering. Nice property: Simplest form of graph is canonical. Sharing in BDDs Every BDD node (not just root) represents some Boolean function in a canonical way. We can uild a multi-rooted BDD for multiple functions. 34
Multi-Rooted BDD: Example Look at sum S3 and carry out Cout [Ro Ruentar] 35
Multi-Rooted BDD: Example Don t represent it twice! BDD can have multiple entry points, or roots Called a multi-rooted BDD Recall Every node in a BDD represents some Boolean function Multi-rooting idea just explicitly exploits this to etter share stuff 36 [Ro Ruentar]
Multi-Rooted BDD: Example Why stop at 2 roots? Sharing among sets of functions (several separate BDDs) reduces the size of BDD! Real example: Adders Separately 4-it adder: 51 nodes 64-it adder: 12,481 nodes Shared 4-it adder: 31 nodes 64-it adder: 571 nodes 37
BDD for OP of Functions g a h f = OR(g, h) Order: a < < c c? 0 1 0 1 38
BDD and Cofactors f f x x f x What are these two functions? 39
How to Implement OP? Example: op = AND f = g h BDD and cofactors: g h x x g h x Therefore, we only need to otain BDDs for g h x and 40 g h x Property of cofactors: g h x = g x h x g h x = g x h x Since we are given BDDs for g and h, it is easy to get BDDs for g x, g x, h x, and h x. We recursively apply op on (g x, h x ) and (g x, h x ) first.
How Are BDDs Really Implemented? Recursively! Cofactor and divide-and-conquer are two keys. Note: Boolean function can e decomposed: f = op(g, h) op can e either AND, OR, XOR, NOT, Idea: uild ROBDD for g and ROBDD for h, then uild ROBDD for f from the previous two ROBDDs. op looks like: BDD op(bdd g, BDD h); BDDs for g, h, and f can share. Start from the ase cases: ROBDDs for constants 0 and 1 and a single variale. f g h 41
Algorithm for Implementing OP BDD op(bdd g, BDD h) { if (g is a leaf or h is a leaf) // termination condition: // either g = 0 or 1, or h = 0 or 1 return proper BDD; var x = min(root(g), root(h)) // get the lowest order var BDD flo = op( negcofbdd(g, x), negcofbdd(h, x) ); BDD fhi = op( poscofbdd(g, x), poscofbdd(h, x) ); return cominebdd(x, flo, fhi); } 42 negcofbdd g, x = g x = poscofbdd g, x = g x = g if x < root(g) lo(g) if x = root(g) g if x < root(g) hi(g) if x = root(g)
Example of OP g a Otain f = OR(g, h). Order: a < < c Cofactor on variale a h c 0 1 h a 0 1 h a g a g a 0 c c 43 0 1 0 1 0 1
Example of OP (cont.) Recursively compute OR(g a, h a) g a h a c We otain: OR(g a, h a) 0 0 1 c Termination condition 0 1 44
Example of OP (cont.) Recursively compute OR(g a, h a ) h a g a Cofactor on variale c 0 1 0 1 h a g a g a h a c 45 0 1 0 0 1
Example of OP (cont.) Recursively compute OR(g a, h a ) Recursively compute OR(g a, h a ) h a g a h a g a c 0 0 1 0 1 Termination condition OR(g a, h a ) Termination condition OR(g a, h a ) 0 1 46
Example of OP (cont.) OR(g a, h a ) OR(g a, h a ) 0 1 Based on the recursion results, otain OR g a, h a Note: we cofactor on. OR(g a, h a ) 0 1 47
Example of OP (cont.) Based on the recursion results, otain OR g, h Note: we cofactor on a. OR(g a, h a) 0 c 1 OR(g a, h a ) 0 1 OR(g, h) 0 a c 1 48 Done!
BDDs: Build Up Incrementally 49 For a gate-level network, uild the BDD for the output incrementally. a c A B C AND AND OR Each input is a BDD, each gate ecomes an operator op that produces a new output BDD. Build BDD for F as a script of calls to asic BDD operators. Stick to a gloal ordering. D E F BDD operator script 1. A = CreateVar( a ) 2. B = CreateVar( ) 3. C = CreateVar( c ) 4. D = AND(A, B) 5. E = AND(B, C) 6. F = OR(D, E)
Example: Build BDD Incrementally a c A B C AND AND D OR Gloal ordering: a < < c E BDD operator script 1. A = CreateVar( A ) 2. B = CreateVar( B ) 3. C = CreateVar( C ) 4. D = AND(A, B) 5. E = AND(B, C) 6. F = OR(D, E) 1. A 2. B 3. C 4. D 5. F E a c a 0 1 0 1 0 1 c 50 0 1 0 1
Example: Build BDD Incrementally a c A B C AND AND D E OR Gloal ordering: a < < c 6. F F BDD operator script 1. A = CreateVar( A ) 2. B = CreateVar( B ) 3. C = CreateVar( C ) 4. D = AND(A, B) 5. E = AND(B, C) 6. F = OR(D, E) a c 51 0 1
Application of BDD Tautology checking Solution: Build BDD for f. Check if the BDD is just the BDD for f = 1. f 1 52
Application of BDD Satisfiaility (SAT) Satisfiaility (SAT): Does there exist an input pattern for variales that lets F = 1? If yes, return one pattern. Recall: In network repair prolem, we want to find (d 0, d 1, d 2, d 3 ) so that a z d 0, d 1, d 2, d 3 = 1 Solution: If the BDD for F is not the BDD for f = 0. Then, SAT answer is yes. If yes, any path from root to 1 leaf is a solution. x1 x4 0 1 SAT? Yes. SAT pattern: x 1, x 2, x 3, x 4 = 0,1,, 1 (1,,, 1) 53
Application of BDD Comparing Logic Implementations Are two given Boolean functions F and G the same? Solution #1: Build BDD for F. Build BDD for G Compare pointers to roots of F and G If and only if pointers are same, F = G. 54 Solution #2: Build BDD for function F G Check if the BDD is just the BDD for f = 1. f 1
Application of BDD Comparing Logic Implementations What inputs make functions F and G give different answers? Solution: Build BDD for H = F G. Ask SAT question for H. 55
BDDs: Seem Too Good To Be True?! Prolem : Variale ordering matters. Example: a1 1 + a2 2 + a3 3 Good ordering:a1 < 1 < a2 < 2 < a3 < 3 Bad ordering: a1 < a2 < a3 < 1 < 2 < 3 56
Variale Ordering: How to Handle? Variale ordering heuristics: make nice BDDs for reasonale prolems. Characterization: know which prolems never make simple BDDs (e.g., multipliers) Dynamic ordering: let the BDD software package pick the order on the fly. 57
Variale Ordering: Intuition Rules of thum for BDD ordering Related inputs should e near each other in order. Groups of inputs that can determine function y themselves should e (i) close together, and (ii) near top of BDD. Example: a1 1 + a2 2 + a3 3 Good ordering: a1 < 1 < a2 < 2 < a3 < 3 Why? a i and i together can determine the function value 58
Variale Ordering: Intuition Rules of thum for BDD ordering Related inputs should e near each other in order. Groups of inputs that can determine function y themselves should e (i) close together, and (ii) near top of BDD. Example: a1 1 + a2 2 + a3 3 Bad ordering: a1 < a2 < a3 < 1 < 2 < 3 Why? We need to rememer a1, a2, a3 efore we see any s. 59
Variale Ordering: Practice 60 Arithmetic circuits are important logic; how are their BDDs? Many carry chain circuits have easy linear sized ROBDD orderings: Adders, Sutractors, Comparators. Rule is alternate variales in the BDD order: a0, 0, a1, 1, a2, 2,, an, n. Are all arithmetic circuits easy? No! Multiplication is exponential in numer of nodes for any order. General experience with BDDs Many tasks have reasonale ROBDD sizes; algorithms are practical to aout 100M nodes. People spend a lot of effort to find orderings that work
BDD Summary Reduced, Ordered, Binary Decision Diagrams, ROBDDs Canonical form a data structure for Boolean functions. Two Boolean functions the same if and only if they have identical BDD. A Boolean function is just a pointer to the root node of the BDD graph. Every node in a (shared) BDD represents some function. Basis for much of today s general manipulation or Boolean stuff. 61 Prolems Variale ordering matters; sometimes BDD is just too ig. Often, we just want to know SAT don t need to uild the whole function.