© 2011 Carnegie Mellon University
Binary Decision Diagrams Part 2
15-414 Bug Catching: Automated Program Verification and Testing
Sagar ChakiSeptember 14, 2011
2
Binary Decision Diagrams – Part 2Sagar 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 2Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
Running Example: Comparator
Comparator
a1 a2 b1 b2
(A) f = 1 , a1 = b1 Æ a2 = b2
(B) f = 1 , a1 = b1 Ç a2 = b2
4
Binary Decision Diagrams – Part 2Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
Conjunctive Normal Form
(: a1 Ç b1 ) Æ (: b1 Ç a1) Æ (: a2 Ç b2 ) Æ (: b2 Ç a2)
(: b1 Ç a1 ) Æ (: a1 Ç b1) Æ (: a2 Ç b2 ) Æ (: b2 Ç a2)
(b1 Ç a1 ) Æ (: a1 Ç : b1) Æ (: a2 Ç b2 ) Æ (: b2 Ç a2)
(: a1 Ç b1 ) Æ (: b1 Ç a1) Æ (: a2 Ç : b2 ) Æ (b2 Ç a2)
5
Binary Decision Diagrams – Part 2Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
Truth TableRow# a1 b1 a2 b2 f
0 0 0 0 0 0
1 0 0 0 1 1
2 0 0 1 0 0
3 0 0 1 1 1
4 0 1 0 0 0
5 0 1 0 1 1
6 0 1 1 0 0
7 0 1 1 1 0
8 1 0 0 0 0
9 1 0 0 1 1
10 1 0 1 0 1
11 1 0 1 1 0
12 1 1 0 0 1
13 1 1 0 1 0
14 1 1 1 0 0
15 1 1 1 1 0
6
Binary Decision Diagrams – Part 2Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
Representing a Truth Table using a Graph
a1
b1 b1
a2 a2
b1 b2 b2 b2
a2 b2
b2 b2 a2 a2
7
Binary Decision Diagrams – Part 2Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
Representing a Truth Table using a Graph
a1
b1 b1
a2 a2
b2 b2
1 0 1 1
b2 b2
1 0 0 0
a2 a2
b2 b2
1 0 0 0
b2 b2
0 0 0 0
8
Binary Decision Diagrams – Part 2Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
OBDT to ROBDD
a1
b1 b1
a2
b2 b2
10
a2
b2 b2
A B C D
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 2Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
OBDT to ROBDD
a1
b1 b1
b2
10
a2
b2
Is this a ROBDD?
(1) YES
(2) NO
10
Binary Decision Diagrams – Part 2Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
OBDT to ROBDD
a1
b1 b1
b2
10
a2
b2
What function does X represent?
(1) a2 = b2 (2) a2 = (: b2)
(3) a2 ) b2 (4) a2 © b2
(5) :(a2 © b2)
(6) (a2 Æ b2) Ç (: a2 Æ : b2)
X1,5,6
11
Binary Decision Diagrams – Part 2Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
ROBDD (a.k.a. BDD) Summary
If BDD(f1) and BDD(f2) are isomorphic then:
1. f1 = f2
2. f1 and f2 have the same variables
3. BDD(f1) and BDD(f2) have the same variable ordering
If BDD(f) is the leaf node “1” then f is: 4. Satisfiable5. Unsatisfiable6. Valid
12
Binary Decision Diagrams – Part 2Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
ROBDD and variable ordering
a1
a2 a2
b1 b1
b2
1 0
b1 b1
b2 b2
Is this a ROBDD?
(1) YES
(2) NO
13
Binary Decision Diagrams – Part 2Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
ROBDD and variable ordering
a1
a2 a2
b1 b1
b2
1 0
b1 b1
b2
Is this a ROBDD?
(1) YES
(2) NO
14
Binary Decision Diagrams – Part 2Sagar 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 2Sagar 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(f1) £ BDD(f2) BDD(f1 Æ f2)
Or : BDD(f1) £ BDD(f2) BDD(f1 Ç f2)
Exist : BDD(f) £ v BDD(9 v. f)
16
Binary Decision Diagrams – Part 2Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
Basic BDD Operations
True False
Var(v)
1 0
10
v
17
Binary Decision Diagrams – Part 2Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
BDD Operations: Not
1 00 1
10
v
O(1) O(1)
18
Binary Decision Diagrams – Part 2Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
BDD Operations: Not
1 00 1
01
v
Swap “0” and “1”
O(1) O(1)
O(n)
19
Binary Decision Diagrams – Part 2Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
BDD Operations: And
vWhat formula does this
represent?
What formula does this
represent?
Suppose this is the BDD for f
20
Binary Decision Diagrams – Part 2Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
BDD Operations: And
vfv=0
Suppose this is the BDD for f
fv=1
fv=0 and fv=1 are known as the co-factors of f w.r.t. v
f = (X Æ fv=0) Ç (Y Æ fv=1)
21
Binary Decision Diagrams – Part 2Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
BDD Operations: And
vfv=0
Suppose this is the BDD for f
fv=1
fv=0 and fv=1 are known as the co-factors of f w.r.t. v
f = (: v Æ fv=0) Ç (v Æ fv=1)
22
Binary Decision Diagrams – Part 2Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
BDD Operations: And (Simple Cases)
And (f, ) = 0 0
And (f, ) = 1 f
And ( ,f ) = 1 f
And ( ,f ) = 0 0
23
Binary Decision Diagrams – Part 2Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
BDD Operations: And (Complex Case)
v1
f1 g1
v2
f2 g2
(: v1 Æ f1) Ç (v1 Æ g1) (: v
2 Æ f2) Ç (v2 Æ g2)
ÆÆ
24
Binary Decision Diagrams – Part 2Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
BDD Operations: And (Complex Case 1)
v1
f1 g1
v1
f2 g2
(: v1 Æ f1) Ç (v1 Æ g1) (: v
1 Æ f2) Ç (v1 Æ g2)
ÆÆ
v1 = v2
25
Binary Decision Diagrams – Part 2Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
BDD Operations: And (Complex Case 1)
(: v1 Æ f1) Ç (v1 Æ g1) (: v
1 Æ f2) Ç (v1 Æ g2)Æ
v1 = v2
(: v1 Æ X) Ç (v1 Æ Y)
26
Binary Decision Diagrams – Part 2Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
BDD Operations: And (Complex Case 1)
(: v1 Æ f1) Ç (v1 Æ g1) (: v
1 Æ f2) Ç (v1 Æ g2)Æ
v1 = v2
(: v1 Æ (f1 Æ f2)) Ç (v1 Æ (g1 Æ g2))
Compute recursivelyCompute recursively
27
Binary Decision Diagrams – Part 2Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
BDD Operations: And (Complex Case 1)
(: v1 Æ f1) Ç (v1 Æ g1) (: v
1 Æ f2) Ç (v1 Æ g2)Æ
v1 = v2
(: v1 Æ (f1 Æ f2)) Ç (v1 Æ (g1 Æ g2))
v1
f1 Æ f2 g1 Æ g2
What if f1 Æ f2 = g1 Æ g2 ?
Return f1 Æ f2
28
Binary Decision Diagrams – Part 2Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
BDD Operations: And (Complex Case 2)
v1
f1 g1
v2
f2 g2
(: v1 Æ f1) Ç (v1 Æ g1) (: v
2 Æ f2) Ç (v2 Æ g2)
ÆÆ
v1 < v2
v1 appears before v2 in the variable
ordering
d2
29
Binary Decision Diagrams – Part 2Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
BDD Operations: And (Complex Case 2)
(: v1 Æ f1) Ç (v1 Æ g1) d2Æ
v1 < v2
(: v1 Æ (f1 Æ d2)) Ç (v1 Æ (g1 Æ d2))
v1
f1 Æ d2 g1 Æ d2
What if f1 Æ d2 = g1 Æ d2 ?
Return f1 Æ d2
O(n1 £ n2)
30
Binary Decision Diagrams – Part 2Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
BDD Operations: Or
Or(d1,d2)
=
Not ( And ( Not(d1), Not(d2) ) )
O(n1 £ n2)
31
Binary Decision Diagrams – Part 2Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
BDD Operations: Exist
Exist(“0”,v) = ?
32
Binary Decision Diagrams – Part 2Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
BDD Operations: Exist
Exist(“0”,v) = “0”
Exist(“1”,v) = ?
33
Binary Decision Diagrams – Part 2Sagar 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) = ?
34
Binary Decision Diagrams – Part 2Sagar 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) = ?
35
Binary Decision Diagrams – Part 2Sagar 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))
O(n2)
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 2Sagar 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 2Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
BDD Application: Counting Sudoku Solutions
1
3 2
1 2 3 4
1
2
3
4
How many ways can you solve this puzzle?
38
Binary Decision Diagrams – Part 2Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
BDD Application: Counting Sudoku Solutions
0 3 1 2
2 1 0 3
3 0 2 1
1 2 3 0
1 2 3 4
1
2
3
4
How many ways can you solve this puzzle? At least 2.
39
Binary Decision Diagrams – Part 2Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
BDD Application: Counting Sudoku Solutions
0 3 1 2
1 2 0 3
3 0 2 1
2 1 3 0
1 2 3 4
1
2
3
4
How many ways can you solve this puzzle? At least 2.
40
Binary Decision Diagrams – Part 2Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
BDD Application: Counting Sudoku Solutions
1
3 2
1 2 3 4
1
2
3
4
a11,b11
a44,b44
41
Binary Decision Diagrams – Part 2Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
BDD Application: Counting Sudoku Solutions
1
3 2
1 2 3 4
1
2
3
4
: a13 Æ b13
a33 Æ : b33
a31 Æ b31
42
Binary Decision Diagrams – Part 2Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
BDD Application: Counting Sudoku Solutions
1
3 2
1 2 3 4
1
2
3
4
Distinct Elements
a11 © a12 Ç b11 © b12
Æ
a11 © a13 Ç b11 © b13
Æ
a11 © a14 Ç b11 © b14
Æ
a12 © a13 Ç b12 © b13
Æ
a12 © a14 Ç b12 © b14
Æ
a13 © a14 Ç b13 © b14
Repeat for each row, column and sub-square
Construct BDD
Count number of solutions
43
Binary Decision Diagrams – Part 2Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
Graph Reachability
0
1
2
3
4
5
6
7
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 2Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
Graph Reachability
0
1
2
3
4
5
6
7
Use three Boolean variables (a,b,c) to encode each node?
: a Æ : b Æ : ca Æ b Æ c
45
Binary Decision Diagrams – Part 2Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
Graph Reachability
0
1
2
3
4
5
6
7
Use three Boolean variables (a,b,c) to encode each node?
: a Æ : b Æ : ca Æ b Æ c
a Æ : b Æ : c
46
Binary Decision Diagrams – Part 2Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
Graph Reachability
0
1
2
3
4
5
6
7
Use three Boolean variables (a,b,c) to encode each node?
: a Æ : b Æ : ca Æ b Æ c
a Æ : b Æ : c
a Æ : b Æ c
47
Binary Decision Diagrams – Part 2Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
Graph Reachability
0
1
2
3
4
5
6
7
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 2Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
Graph Reachability
0
1
2
3
4
5
6
7
Key Idea 1: Every Boolean formula represents a set of nodes!
a Æ b Æ : c = {6}
49
Binary Decision Diagrams – Part 2Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
Graph Reachability
0
1
2
3
4
5
6
7
Key Idea 1: Every Boolean formula represents a set of nodes!
a Æ b = ?
50
Binary Decision Diagrams – Part 2Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
Graph Reachability
0
1
2
3
4
5
6
7
Key Idea 1: Every Boolean formula represents a set of nodes!
a Æ b = {6,7}
51
Binary Decision Diagrams – Part 2Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
Graph Reachability
0
1
2
3
4
5
6
7
Key Idea 1: Every Boolean formula represents a set of nodes!
a © b = ?
52
Binary Decision Diagrams – Part 2Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
Graph Reachability
0
1
2
3
4
5
6
7
Key Idea 1: Every Boolean formula represents a set of nodes!
a © b = {2,3,4,5}
53
Binary Decision Diagrams – Part 2Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
Graph Reachability
0
1
2
3
4
5
6
7
• 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 2Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
Graph Reachability
0
1
2
3
4
5
6
7
: a Æ : b Æ : c Æ : a’ Æ : b’ Æ c’
Key Idea 2: Edges can also be represented by Boolean formulas
55
Binary Decision Diagrams – Part 2Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
Graph Reachability
0
1
2
3
4
5
6
7
a Æ : b Æ c Æ : a’ Æ b’ Æ : c’
Key Idea 2: Edges can also be represented by Boolean formulas
56
Binary Decision Diagrams – Part 2Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
Graph Reachability
0
1
2
3
4
5
6
7
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 2Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
Graph Reachability
0
1
2
3
4
5
6
7
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’]
Variable renaming : replace a’ with a
58
Binary Decision Diagrams – Part 2Sagar 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 1020 nodes.
59
Binary Decision Diagrams – Part 2Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
Questions?
Sagar ChakiSenior Member of Technical StaffRTSS ProgramTelephone: +1 412-268-1436Email: [email protected]
U.S. MailSoftware Engineering InstituteCustomer Relations4500 Fifth AvenuePittsburgh, PA 15213-2612USA
Webwww.sei.cmu.edu/staff/chaki
Customer RelationsEmail: [email protected]: +1 412-268-5800SEI Phone: +1 412-268-5800SEI Fax: +1 412-268-6257