© 2011 Carnegie Mellon University Binary Decision Diagrams Part Bug Catching: Automated Program Verification and Testing Sagar Chaki September 14, 2011
2 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University BDDs Recap Typically mean Reduced Ordered Binary Decision Diagrams (ROBDDs) Can be viewed as reduced forms of Ordered Binary Decision Trees Obtained by eliminating duplicate nodes and redundant nodes Often substantially smaller than the OBDT Canonical representation of Boolean formulas Unlike other normal forms like CNF and DNF Size of BDD depends critically on variable ordering In practice, BDDs are built up from their components Via (efficient) Boolean operations Dynamic variable ordering used to manage BDD size
3 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University Running Example: Comparator Comparator a1a1 a2a2 b1b1 b2b2 (A) f = 1, a 1 = b 1 Æ a 2 = b 2 (B) f = 1, a 1 = b 1 Ç a 2 = b 2
4 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University Conjunctive Normal Form ( : a 1 Ç b 1 ) Æ ( : b 1 Ç a 1 ) Æ ( : a 2 Ç b 2 ) Æ ( : b 2 Ç a 2 ) ( : b 1 Ç a 1 ) Æ ( : a 1 Ç b 1 ) Æ ( : a 2 Ç b 2 ) Æ ( : b 2 Ç a 2 ) (b 1 Ç a 1 ) Æ ( : a 1 Ç : b 1 ) Æ ( : a 2 Ç b 2 ) Æ ( : b 2 Ç a 2 ) ( : a 1 Ç b 1 ) Æ ( : b 1 Ç a 1 ) Æ ( : a 2 Ç : b 2 ) Æ (b 2 Ç a 2 )
5 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University Truth Table Row#a1a1 b1b1 a2a2 b2b2 f
6 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University Representing a Truth Table using a Graph a1a1 b1b1 b1b1 a2a2 a2a2 b1b1 b2b2 b2b2 b2b2 a2a2 b2b2 b2b2 b2b2 a2a2 a2a2
7 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University Representing a Truth Table using a Graph a1a1 b1b1 b1b1 a2a2 a2a2 b2b2 b2b b2b2 b2b a2a2 a2a2 b2b2 b2b b2b2 b2b2 0000
8 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University OBDT to ROBDD a1a1 b1b1 b1b1 a2a2 b2b2 b2b2 10 a2a2 b2b2 b2b2 AB CD Which pairs are isomorphic? (1){A,B} and {C,D} (2){A,C} and {B,D} (3){A,D} and {B,C} Which pairs are isomorphic? (1){A,B} and {C,D} (2){A,C} and {B,D} (3){A,D} and {B,C}
9 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University OBDT to ROBDD a1a1 b1b1 b1b1 b2b2 10 a2a2 b2b2 Is this a ROBDD? (1) YES (2) NO Is this a ROBDD? (1) YES (2) NO
10 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University OBDT to ROBDD a1a1 b1b1 b1b1 b2b2 10 a2a2 b2b2 What function does X represent? (1) a 2 = b 2 (2) a 2 = ( : b 2 ) (3) a 2 ) b 2 (4) a 2 © b 2 (5) : (a 2 © b 2 ) (6) (a 2 Æ b 2 ) Ç ( : a 2 Æ : b 2 ) What function does X represent? (1) a 2 = b 2 (2) a 2 = ( : b 2 ) (3) a 2 ) b 2 (4) a 2 © b 2 (5) : (a 2 © b 2 ) (6) (a 2 Æ b 2 ) Ç ( : a 2 Æ : b 2 ) X 1,5,6
11 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University ROBDD (a.k.a. BDD) Summary If BDD(f 1 ) and BDD(f 2 ) are isomorphic then: 1.f 1 = f 2 2.f 1 and f 2 have the same variables 3.BDD(f 1 ) and BDD(f 2 ) have the same variable ordering If BDD(f) is the leaf node “1” then f is: 1.Satisfiable 2.Unsatisfiable 3.Valid
12 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University ROBDD and variable ordering a1a1 a2a2 a2a2 b1b1 b1b1 b2b2 10 b1b1 b1b1 b2b2 b2b2 Is this a ROBDD? (1) YES (2) NO Is this a ROBDD? (1) YES (2) NO
13 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University ROBDD and variable ordering a1a1 a2a2 a2a2 b1b1 b1b1 b2b2 10 b1b1 b1b1 b2b2 Is this a ROBDD? (1) YES (2) NO Is this a ROBDD? (1) YES (2) NO
14 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University ROBDD and variable ordering There exists a function whose BDD grows polynomially in the number of variables for some ordering and exponentially for others? TRUE There exists a function whose BDD grows exponentially for all variable orderings? TRUE There exists a function whose BDD grows linearly for all variable orderings? TRUE
15 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University BDD Operations True : BDD(TRUE) False: BDD(FALSE) Var : v BDD(v) Not : BDD(f) BDD( : f) And : BDD(f 1 ) £ BDD(f 2 ) BDD(f 1 Æ f 2 ) Or : BDD(f 1 ) £ BDD(f 2 ) BDD(f 1 Ç f 2 ) Exist : BDD(f) £ v BDD( 9 v. f)
16 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University Basic BDD Operations TrueFalse Var(v) v
17 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University BDD Operations: Not v O (1)
18 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University BDD Operations: Not v Swap “0” and “1” O (1) O (n)
19 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University BDD Operations: And v What formula does this represent? Suppose this is the BDD for f
20 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University BDD Operations: And v f v=0 Suppose this is the BDD for f f v=1 f v=0 and f v=1 are known as the co-factors of f w.r.t. v f = (X Æ f v=0 ) Ç (Y Æ f v=1 )
21 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University BDD Operations: And v f v=0 Suppose this is the BDD for f f v=1 f v=0 and f v=1 are known as the co-factors of f w.r.t. v f = ( : v Æ f v=0 ) Ç (v Æ f v=1 )
22 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University BDD Operations: And (Simple Cases) And (f, ) = 00 1 f And (,f ) = 1 f 0 0
23 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University BDD Operations: And (Complex Case) v1v1 f1f1 g1g1 v2v2 f2f2 g2g2 ( : v 1 Æ f 1 ) Ç (v 1 Æ g 1 ) ( : v 2 Æ f 2 ) Ç (v 2 Æ g 2 ) Æ Æ
24 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University BDD Operations: And (Complex Case 1) v1v1 f1f1 g1g1 v1v1 f2f2 g2g2 ( : v 1 Æ f 1 ) Ç (v 1 Æ g 1 ) ( : v 1 Æ f 2 ) Ç (v 1 Æ g 2 ) Æ Æ v 1 = v 2
25 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University BDD Operations: And (Complex Case 1) ( : v 1 Æ f 1 ) Ç (v 1 Æ g 1 ) ( : v 1 Æ f 2 ) Ç (v 1 Æ g 2 ) Æ v 1 = v 2 ( : v 1 Æ X) Ç (v 1 Æ Y)
26 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University BDD Operations: And (Complex Case 1) ( : v 1 Æ f 1 ) Ç (v 1 Æ g 1 ) ( : v 1 Æ f 2 ) Ç (v 1 Æ g 2 ) Æ v 1 = v 2 ( : v 1 Æ (f 1 Æ f 2 )) Ç (v 1 Æ (g 1 Æ g 2 )) Compute recursively
27 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University BDD Operations: And (Complex Case 1) ( : v 1 Æ f 1 ) Ç (v 1 Æ g 1 ) ( : v 1 Æ f 2 ) Ç (v 1 Æ g 2 ) Æ v 1 = v 2 ( : v 1 Æ (f 1 Æ f 2 )) Ç (v 1 Æ (g 1 Æ g 2 )) v1v1 f 1 Æ f 2 g 1 Æ g 2 What if f 1 Æ f 2 = g 1 Æ g 2 ? Return f 1 Æ f 2
28 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University BDD Operations: And (Complex Case 2) v1v1 f1f1 g1g1 v2v2 f2f2 g2g2 ( : v 1 Æ f 1 ) Ç (v 1 Æ g 1 ) ( : v 2 Æ f 2 ) Ç (v 2 Æ g 2 ) Æ Æ v 1 < v 2 v 1 appears before v 2 in the variable ordering d2d2 d2d2
29 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University BDD Operations: And (Complex Case 2) ( : v 1 Æ f 1 ) Ç (v 1 Æ g 1 ) d2d2 d2d2 Æ v 1 < v 2 ( : v 1 Æ (f 1 Æ d 2 )) Ç (v 1 Æ (g 1 Æ d 2 )) v1v1 f 1 Æ d 2 g 1 Æ d 2 What if f 1 Æ d 2 = g 1 Æ d 2 ? Return f 1 Æ d 2 O (n 1 £ n 2 )
30 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University BDD Operations: Or Or(d1,d 2 ) = Not ( And ( Not(d 1 ), Not(d 2 ) ) ) Or(d1,d 2 ) = Not ( And ( Not(d 1 ), Not(d 2 ) ) ) O (n 1 £ n 2 )
31 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University BDD Operations: Exist Exist(“0”,v) = ?
32 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University BDD Operations: Exist Exist(“0”,v) = “0” Exist(“1”,v) = ? Exist(“0”,v) = “0” Exist(“1”,v) = ?
33 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University BDD Operations: Exist Exist(“0”,v) = “0” Exist(“1”,v) = “1” Exist(( : v Æ f) Ç (v Æ g), v) = ? Exist(“0”,v) = “0” Exist(“1”,v) = “1” Exist(( : v Æ f) Ç (v Æ g), v) = ?
34 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University BDD Operations: Exist Exist(“0”,v) = “0” Exist(“1”,v) = “1” Exist(( : v Æ f) Ç (v Æ g), v) = Or(f,g) Exist(( : v’ Æ f) Ç (v’ Æ g), v) = ? Exist(“0”,v) = “0” Exist(“1”,v) = “1” Exist(( : v Æ f) Ç (v Æ g), v) = Or(f,g) Exist(( : v’ Æ f) Ç (v’ Æ g), v) = ?
35 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University BDD Operations: Exist Exist(“0”,v) = “0” Exist(“1”,v) = “1” Exist(( : v Æ f) Ç (v Æ g), v) = Or(f,g) Exist(( : v’ Æ f) Ç (v’ Æ g), v) = ( : v’ Æ Exist(f,v)) Ç (v’ Æ Exist(g,v)) Exist(“0”,v) = “0” Exist(“1”,v) = “1” Exist(( : v Æ f) Ç (v Æ g), v) = Or(f,g) Exist(( : v’ Æ f) Ç (v’ Æ g), v) = ( : v’ Æ Exist(f,v)) Ç (v’ Æ Exist(g,v)) O (n 2 ) But f is SAT iff 9 V. f is not “0”. So why doesn’t this imply P = NP? Because the BDD size changes!
36 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University 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? Or if your problem involves computing a fixed point Set of nodes reachable from a given node in a graph
37 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University BDD Application: Counting Sudoku Solutions How many ways can you solve this puzzle?
38 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University BDD Application: Counting Sudoku Solutions How many ways can you solve this puzzle? At least 2.
39 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University BDD Application: Counting Sudoku Solutions How many ways can you solve this puzzle? At least 2.
40 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University BDD Application: Counting Sudoku Solutions a 11,b 11 a 44,b 44
41 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University BDD Application: Counting Sudoku Solutions : a 13 Æ b 13 a 33 Æ : b 33 a 31 Æ b 31
42 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University BDD Application: Counting Sudoku Solutions Distinct Elements a 11 © a 12 Ç b 11 © b 12 Æ a 11 © a 13 Ç b 11 © b 13 Æ a 11 © a 14 Ç b 11 © b 14 Æ a 12 © a 13 Ç b 12 © b 13 Æ a 12 © a 14 Ç b 12 © b 14 Æ a 13 © a 14 Ç b 13 © b 14 a 11 © a 12 Ç b 11 © b 12 Æ a 11 © a 13 Ç b 11 © b 13 Æ a 11 © a 14 Ç b 11 © b 14 Æ a 12 © a 13 Ç b 12 © b 13 Æ a 12 © a 14 Ç b 12 © b 14 Æ a 13 © a 14 Ç b 13 © b 14 Repeat for each row, column and sub-square Construct BDD Count number of solutions Repeat for each row, column and sub-square Construct BDD Count number of solutions
43 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University Graph Reachability Which nodes are reachable from “7”? {2,3,5,6,7} But what if the graph has trillions of nodes?
44 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University Graph Reachability Use three Boolean variables (a,b,c) to encode each node? : a Æ : b Æ : c a Æ b Æ c
45 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University Graph Reachability Use three Boolean variables (a,b,c) to encode each node? : a Æ : b Æ : c a Æ b Æ c a Æ : b Æ : c
46 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University Graph Reachability Use three Boolean variables (a,b,c) to encode each node? : a Æ : b Æ : c a Æ b Æ c a Æ : b Æ : c a Æ : b Æ c
47 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University Graph Reachability Key Idea 1: Every Boolean formula represents a set of nodes! a Æ b Æ : c = ? The nodes whose encodings satisfy the formula.
48 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University Graph Reachability Key Idea 1: Every Boolean formula represents a set of nodes! a Æ b Æ : c = {6}
49 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University Graph Reachability Key Idea 1: Every Boolean formula represents a set of nodes! a Æ b = ?
50 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University Graph Reachability Key Idea 1: Every Boolean formula represents a set of nodes! a Æ b = {6,7}
51 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University Graph Reachability Key Idea 1: Every Boolean formula represents a set of nodes! a © b = ?
52 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University Graph Reachability Key Idea 1: Every Boolean formula represents a set of nodes! a © b = {2,3,4,5}
53 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University Graph Reachability 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’) 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’)
54 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University Graph Reachability : a Æ : b Æ : c Æ : a’ Æ : b’ Æ c’ Key Idea 2: Edges can also be represented by Boolean formulas
55 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University Graph Reachability a Æ : b Æ c Æ : a’ Æ b’ Æ : c’ Key Idea 2: Edges can also be represented by Boolean formulas
56 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University Graph Reachability a Æ : b Æ c Æ : a’ Æ b’ Æ : c’ Ç : a Æ : b Æ : c Æ : a’ Æ : b’ Æ c’ Key Idea 2: Edges can also be represented by Boolean formulas a Æ : b Æ c Æ : a’ Æ b’ Æ : c’ Ç : a Æ : b Æ : c Æ : a’ Æ : b’ Æ c’
57 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University Graph Reachability 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 Image(S,R) = ( 9 a,b,c. (S Æ R)) [ a \ a’, b \ b’, c \ c’] Image(S,R) = ( 9 a,b,c. (S Æ R)) [ a \ a’, b \ b’, c \ c’] Variable renaming : replace a’ with a
58 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University 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 nodes.
59 Binary Decision Diagrams – Part 2 Sagar Chaki, Sep 14, 2011 © 2011 Carnegie Mellon University Questions? Sagar Chaki Senior Member of Technical Staff RTSS Program Telephone: U.S. Mail Software Engineering Institute Customer Relations 4500 Fifth Avenue Pittsburgh, PA USA Web Customer Relations Telephone: SEI Phone: SEI Fax: