5-44 Bug Catching: Automated Program Verification and Testing based on slides by SagarChaki 2 Carnegie Mellon University
BDDs in a nutshell Typically mean Reduced Ordered (ROBDDs) Canonical representation of Boolean formulas Often substantially more compact than a traditional normal form Can be manipulated very efficiently Conjunction, Disjunction, Negation, Existential Quantification R. E. Bryant. Graph-based algorithms for boolean function manipulation. IEEE Transactions on Computers, C-35(8), 986. 2
Running Example: Comparator a a 2 b b 2 Comparator f =,a = b Æa 2 = b 2 3
Conjunctive Normal Form f a = b Æ a 2 = b 2 a )b Æb )a Æ a 2 )b 2 Æb 2 )a 2 (:a Çb ) Æ(:b Ça ) Æ(:a 2 Çb 2 ) Æ(:b 2 Ça 2 ) (:b Ça ) Æ(:a Çb ) Æ(:a 2 Çb 2 ) Æ(:b 2 Ça 2 ) Not Canonical 4
Truth Table () a b a 2 b 2 f Still Not Canonical 5
Truth Table (2) a a 2 b b 2 f Canonical if you fix variable order. But always exponential in # of variables. Let s try to fix this. 6
Shannon s / Boole s Expansion Every Boolean formula f(a, a,, a n ) can be written as (a Æf(true, a,, a n )) Ç(:a Æf(false, a,, a n )) or, simply, ITE (a, f(true, a,, a n ), f(false, a,, a n )) where ITE stands for If-Then-Else The formula f(true, a,, a n ) is called the cofactorof f w.r.t. a The formula f(false, a,, a n ) is called the cofactorof f w.r.t. :a 7
Representing a Truth Table using a Graph a a 2 a 2 a 2 a 2 Binary Decision Tree (in this case ordered) 8
Binary Decision Tree: Formal Definition Balanced binary tree. Length of each path = # of variables Leaf nodes labeled with either or Internal node v labeled with a Boolean variable var(v) Every node on a path labeled with a different variable Internal node v has two children: low(v) and high(v) Each path corresponds to a (partial) truth assignment to variables Assign to var(v) if low(v) is in the path, and if high(v) is in the path Value of a leaf is determined by: Constructing the truth assignment for the path leading to it from the root Looking up the truth table with this truth assignment 9
Binary Decision Tree low(v) v a var(v) = a high(v) a 2 a 2 a 2 a 2
Binary Decision Tree a b a 2 b 2 The truth assignment corresponding to the path to this leaf is: a =? b =? a 2 =? b 2 =?
Binary Decision Tree a b a 2 b 2 f a b a 2 b 2 The truth assignment corresponding to the path to this leaf is: a = b = a 2 = b 2 = 2
Binary Decision Tree a b a 2 b 2 f a b a 2 b 2 The truth assignment corresponding to the path to this leaf is: a = b = a 2 = b 2 = 3
Binary Decision Tree a b a 2 b 2 f a b a 2 b 2 The truth assignment corresponding to the path to this leaf is: a = b = a 2 = b 2 = 4
Binary Decision Tree (BDT) a a 2 a 2 a 2 a 2 Canonical if you fix variable order (i.e., use ordered BDT) But still exponential in # of variables. Let s try to fix this. 5
Reduced Ordered BDD Conceptually, a ROBDD is obtained from an ordered BDT (OBDT) by eliminating redundant sub-diagrams and nodes Start with OBDT and repeatedly apply the following two operations as long as possible:. Eliminate duplicate sub-diagrams. Keep a single copy. Redirect edges into the eliminated duplicates into this single copy. 2. Eliminate redundant nodes. Whenever low(v) = high(v), remove v and redirect edges into v to low(v). Why does this terminate? ROBDD is often exponentially smaller than the corresponding OBDT 6
OBDT to ROBDD a a 2 a 2 a 2 a 2 7
OBDT to ROBDD a a 2 a 2 a 2 a 2 Duplicate subdiagram 8
OBDT to ROBDD a a 2 a 2 a 2 a 2 9
OBDT to ROBDD a a 2 a 2 a 2 a 2 2
OBDT to ROBDD a a 2 a 2 a 2 a 2 2
OBDT to ROBDD a a 2 a 2 a 2 a 2 22
OBDT to ROBDD a a 2 a 2 a 2 a 2 23
OBDT to ROBDD a a 2 a 2 a 2 a 2 24
OBDT to ROBDD a a 2 a 2 a 2 a 2 Redundant node 25
OBDT to ROBDD a a 2 a 2 a 2 a 2 b 2 26
OBDT to ROBDD a a 2 a 2 a 2 a 2 b 2 27
OBDT to ROBDD a a 2 a 2 a 2 a 2 28
OBDT to ROBDD a a 2 a 2 a 2 a 2 29
OBDT to ROBDD a If a = and b = then f = irrespective of the values of a 2 and b 2 a 2 a 2 a 2 3
OBDT to ROBDD a a 2 a 2 3
OBDT to ROBDD a a 2 a 2 32
OBDT to ROBDD a a 2 a 2 b 2 33
OBDT to ROBDD a a 2 a 2 b 2 34
OBDT to ROBDD a a 2 a 2 b 2 b 2 35
OBDT to ROBDD a a 2 a 2 b 2 b 2 36
OBDT to ROBDD a Let s move things around a little bit so that the BDD looks nicer. a 2 b 2 b 2 37
OBDT to ROBDD a Bryant gave a linear-time algorithm (called Reduce) to convert OBDT to ROBDD. b 2 a 2 b 2 In practice, BDD packages don t use Reduce directly. They apply the two reductions on-the-fly as new BDDs are constructed from existing ones. Why? 38
ROBDD (a.k.a. BDD) Summary BDDs are canonical representations of Boolean formulas f = f 2,? 39
ROBDD (a.k.a. BDD) Summary BDDs are canonical representations of Boolean formulas f = f 2,BDD(f ) and BDD(f 2 ) are isomorphic f is unsatisfiable,? 4
ROBDD (a.k.a. BDD) Summary BDDs are canonical representations of Boolean formulas f = f 2,BDD(f ) and BDD(f 2 ) are isomorphic f is unsatisfiable,bdd(f) is the leaf node f is valid,? 4
ROBDD (a.k.a. BDD) Summary BDDs are canonical representations of Boolean formulas f = f 2,BDD(f ) and BDD(f 2 ) are isomorphic f is unsatisfiable,bdd(f) is the leaf node f is valid,bdd(f) is the leaf node BDD packages do these operations in constant time Logical operations can be performed efficiently on BDDs Polynomial in argument size BDD size depends critically on the variable ordering Some formulas have exponentially large sizes for all ordering Others are polynomial for some ordering and exponential for others 42
ROBDD and variable ordering a a 2 a 2 43
ROBDD and variable ordering a a 2 a 2 44
ROBDD and variable ordering a a 2 a 2 45
ROBDD and variable ordering a a 2 a 2 46
ROBDD and variable ordering a a 2 a 2 b 2 47
ROBDD and variable ordering a a 2 a 2 b 2 48
ROBDD and variable ordering a Let s move things around a little bit so that the BDD looks nicer. a 2 a 2 b 2 b 2 49
ROBDD and variable ordering a 8 nodes a nodes a 2 a 2 a 2 b 2 b 2 b 2 b 2 a < b < a 2 < b 2 a < a 2 < b < b 2 5
ROBDD and variable ordering a? n + 2 nodes a? 2 n nodes a n a n a n b n b n b n b n a < b < < a n < b n a < < a n < b < < b n 5
ROBDD and variable ordering a 3 n + 2 nodes a 3 2 n nodes a n a n a n b n b n b n b n a < b < < a n < b n a < < a n < b < < b n 52
BDD Operations True : BDD(TRUE) False: BDD(FALSE) Var: v BDD(v) Not : BDD(f) BDD(:f) And : BDD(f ) BDD(f 2 ) BDD(f Æf 2 ) Or : BDD(f ) BDD(f 2 ) BDD(f Çf 2 ) Exists : BDD(f) v BDD(9 v. f) 53
Basic BDD Operations True False Var(v) v 54
BDD Operations: Not O() O() v 55
BDD Operations: Not O() O() v O(n) Swap and 56
BDD Operations: And Suppose this is the BDD for f What formula does this represent? v What formula does this represent? 57
BDD Operations: And Suppose this is the BDD for f f v= v f v= f v= and f v= are known as the co-factors of f w.r.t. v f = (X Æf v= ) Ç(Y Æf v= ) 58
BDD Operations: And Suppose this is the BDD for f f v= v f v= f v= and f v= are known as the co-factors of f w.r.t. v f = (:v Æf v= ) Ç(v Æf v= ) 59
BDD Operations: And (Simple Cases) And (f, ) = And (f, ) = f And (,f ) = f And (,f) = 6
BDD Operations: And (Complex Case) v Æ v 2 f f g g (:v Æf ) Ç(v Æf ) Æ (:v 2 Æg ) Ç(v 2 Æg ) 6
BDD Operations: And (Complex Case ) v = v 2 v Æ v f f g g (:v Æf ) Ç(v Æf ) Æ (:v Æg ) Ç(v Æg ) 62
BDD Operations: And (Complex Case ) v = v 2 (:v ÆX) Ç(v ÆY) (:v Æf ) Ç(v Æf ) Æ (:v Æg ) Ç(v Æg ) 63
BDD Operations: And (Complex Case ) v = v 2 Compute recursively (:v Æ(f Æg )) Ç(v Æ(f Æg )) (:v Æf ) Ç(v Æf ) Æ (:v Æg ) Ç(v Æg ) 64
BDD Operations: And (Complex Case ) v = v 2 What if f Æg = f Æg? v Return f Æg f Æg f Æg (:v Æ(f Æg )) Ç(v Æ(f Æg )) (:v Æf ) Ç(v Æf ) Æ (:v Æg ) Ç(v Æg ) 65
BDD Operations: And (Complex Case 2) v < v 2 v appears before v 2 in the variable ordering v Æ v 2 g f f g g (:v Æf ) Ç(v Æf ) Æ (:v 2 Æg ) Ç(v 2 Æg ) 66
BDD Operations: And (Complex Case 2) v < v 2 What if f Æg= f Æg? v Return f Æg f Æg f Æg (:v Æ(f Æg)) Ç(v Æ(f Æg)) (:v Æf ) Ç(v Æf ) Æ g O(n n 2 ) 67
BDD Operations: And BDD bddand (BDD f, BDD g) if (f == g f == True) return g if (g == True) return f if (f == False g == False) return False v = (var(f) < var(g))? var(f) : var(g) f = (v == var(f))? low(f) : f f = (v == var(f))? high(f) : f g = (v == var(g))? low (g) : g g = (v == var(g))? high (g) : g T = bddand (f, g); E = bddand (f, g) if (T == E) return T return mkunique (v, T, E) returns unique BDD for ite(v,t,e) 68
BDD Operations: Or Or(f,g) = Not ( And ( Not(f), Not(g) ) ) O(n n 2 ) 69
BDD Operations: Exists Exists(,v) =? 7
BDD Operations: Exists Exists(,v) = Exists(,v) =? 7
BDD Operations: Exists Exists(,v) = Exists(,v) = Exists((:v Æf) Ç(v Æg), v) =? 72
BDD Operations: Exists Exists(,v) = Exists(,v) = Exists((:v Æf) Ç(v Æg), v) = Or(f,g) Exists((:v Æf) Ç(v Æg), v) =? 73
BDD Operations: Exists O(n 2 ) Exists(,v) = Exists(,v) = Exists((:v Æf) Ç(v Æg), v) = Or(f,g) Exists((:v Æf) Ç(v Æg), v) = (:v ÆExists(f,v)) Ç(v ÆExists(g,v)) But f is SAT iff9v. f is not. So why doesn t this imply P = NP? Because the BDD size changes! 74
BDD Applications SAT is great if you are interested to know if a solution exists BDDs are great if you are interested in the set of all solutions How many solutions are there? How do you do this on a BDD? BDDs are great for computing a fixed points Set of nodes reachable from a given node in a graph 75
Graph Reachability 3 5 7 2 4 6 Which nodes are reachable from 7? {2,3,5,6,7} But what if the graph has trillions of nodes? 76
Graph Reachability 3 5 7 2 4 6 :a Æ:b Æ:c a Æb Æc Use three Boolean variables (a,b,c) to encode each node? 77
Graph Reachability 3 5 7 2 4 6 :a Æ:b Æ:c a Æb Æc a Æ:b Æ:c Use three Boolean variables (a,b,c) to encode each node? 78
Graph Reachability a Æ:b Æc 3 5 7 2 4 6 :a Æ:b Æ:c a Æb Æc a Æ:b Æ:c Use three Boolean variables (a,b,c) to encode each node? 79
Graph Reachability 3 5 7 2 4 6 a Æb Æ:c =? Key Idea : Every Boolean formula represents a set of nodes! The nodes whose encodings satisfy the formula. 8
Graph Reachability 3 5 7 2 4 6 a Æb Æ:c = {6} Key Idea : Every Boolean formula represents a set of nodes! 8
Graph Reachability 3 5 7 2 4 6 a Æ b =? Key Idea : Every Boolean formula represents a set of nodes! 82
Graph Reachability 3 5 7 2 4 6 a Æ b = {6,7} Key Idea : Every Boolean formula represents a set of nodes! 83
Graph Reachability 3 5 7 2 4 6 a xorb =? Key Idea : Every Boolean formula represents a set of nodes! 84
Graph Reachability 3 5 7 2 4 6 a xorb = {2,3,4,5} Key Idea : Every Boolean formula represents a set of nodes! 85
Graph Reachability 3 5 7 2 4 6 Key Idea 2: Edges can also be represented by Boolean formulas An edge is just a pair of nodes Introduce three new variables: a, b, c Formula represents all pairs of nodes (n,n ) that satisfy when n is encoded using (a,b,c) and n is encoded using (a,b,c ) 86
Graph Reachability 3 5 7 2 4 6 :a Æ:b Æ:c Æ:a Æ:b Æc Key Idea 2: Edges can also be represented by Boolean formulas 87
Graph Reachability 3 5 7 2 4 6 a Æ:b Æc Æ:a Æb Æ:c Key Idea 2: Edges can also be represented by Boolean formulas 88
Graph Reachability 3 5 7 2 4 6 a Æ:b Æc Æ:a Æb Æ:c Ç :a Æ:b Æ:c Æ:a Æ:b Æc Key Idea 2: Edges can also be represented by Boolean formulas 89
Graph Reachability 3 5 7 2 4 6 Variable renaming : replace a with a Image(S,R) = (9a,b,c. (S ÆR)) [ a \a, b \b, c \ c ] Key Idea 3: Given the BDD for a set of nodes S, and the BDD for the set of all edges R, the BDD for all the nodes that are adjacent to S can be computed using the BDD operations 9
Graph Reachability Algorithm S = BDD for initial set of nodes; R = BDD for all the edges of the graph; while (true) { I = Image(S,R); // compute adjacent nodes to S if (And(Not(S),I) == False) // no new nodes found break; S = Or(S,I); // add newly discovered nodes to result } return S; Symbolic Model Checking. Has been done for graphs with 2 nodes. 9