Transcript ppt
Binary Decision Diagrams
Part 2
15-414 Bug Catching: Automated
Program Verification and Testing
Sagar Chaki
September 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
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
2
Running Example: Comparator
a1
a2
b1
b2
Comparator
(A) f = 1 , a1 = b1 Æ a2 = b2
(B) f = 1 , a1 = b1 Ç a2 = b2
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
3
Conjunctive Normal Form
(b1 Ç a1 ) Æ (: a1 Ç : b1) Æ (: a2 Ç b2 ) Æ (: b2 Ç a2)
(: b1 Ç a1 ) Æ (: a1 Ç b1) Æ (: a2 Ç b2 ) Æ (: b2 Ç a2)
(: a1 Ç b1 ) Æ (: b1 Ç a1) Æ (: a2 Ç b2 ) Æ (: b2 Ç a2)
(: a1 Ç b1 ) Æ (: b1 Ç a1) Æ (: a2 Ç : b2 ) Æ (b2 Ç a2)
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
4
Truth Table
Row#
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
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
5
Representing a Truth Table using a Graph
a1
b1
b1
a2
b1
a2
b2
b2
b2
a2
b2
b2
b2
a2
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
a2
6
Representing a Truth Table using a Graph
a1
b1
b1
a2
a2
b2
1
b2
0
1
b2
1
1
a2
b2
0
0
a2
b2
0
1
b2
0
0
b2
0
0
b2
0
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
0
0
7
Which pairs are isomorphic?
OBDT to ROBDD
(1) {A,B} and {C,D}
(2) {A,C} and {B,D}
a1
(3) {A,D} and {B,C}
b1
b1
a2
a2
A
C
B
b2
b2
D
b2
0
b2
1
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
8
Is this a ROBDD?
OBDT to ROBDD
(1) YES
a1
(2) NO
b1
b1
a2
b2
b2
0
1
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
9
OBDT to ROBDD
What function does X represent?a1
(1) a2 = b2
(2) a2 = (: b2)
(3) a2 ) b2
(4)b1a2 © b2
(5) :(a2 © b2)
b1
1,5,6
X
a2
(6) (a2 Æ b2) Ç (: a2 Æ : b2)
b2
b2
0
1
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
10
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:
1. Satisfiable
2. Unsatisfiable
3. Valid
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
11
Is this a ROBDD?
ROBDD and variable ordering
(1) YES
a1
(2) NO
a2
b1
a2
b1
b1
b2
b1
b2
1
b2
0
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
12
Is this a ROBDD?
ROBDD and variable ordering
(1) YES
a1
(2) NO
a2
b1
a2
b1
b1
b1
b2
b2
1
0
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
13
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
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
14
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)
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
15
Basic BDD Operations
True
False
1
0
Var(v)
v
0
1
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
16
BDD Operations: Not
1
0
1
0
O(1)
O(1)
v
0
1
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
17
BDD Operations: Not
1
0
1
0
O(1)
O(1)
v
O(n)
Swap “0” and “1”
1
0
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
18
BDD Operations: And
Suppose this is
the BDD for f
What formula
does this
represent?
v
What formula
does this
represent?
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
19
BDD Operations: And
Suppose this is
the BDD for f
fv=0
v
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)
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
20
BDD Operations: And
Suppose this is
the BDD for f
fv=0
v
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)
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
21
BDD Operations: And (Simple Cases)
And (f,
0
)=
And (f,
1
)= f
0
And (
1
,f ) = f
And (
0
,f ) =
0
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
22
BDD Operations: And (Complex Case)
v1
f1
v2
Æ
g1
(: v1 Æ f1) Ç (v1 Æ g1)
f2
Æ
g2
(: v2 Æ f2) Ç (v2 Æ g2)
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
23
BDD Operations: And (Complex Case 1)
v1 = v2
v1
f1
v1
Æ
g1
(: v1 Æ f1) Ç (v1 Æ g1)
f2
Æ
g2
(: v1 Æ f2) Ç (v1 Æ g2)
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
24
BDD Operations: And (Complex Case 1)
v1 = v2
(: v1 Æ X) Ç (v1 Æ Y)
(: v1 Æ f1) Ç (v1 Æ g1)
Æ
(: v1 Æ f2) Ç (v1 Æ g2)
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
25
BDD Operations: And (Complex Case 1)
v1 = v2
Compute recursively
(: v1 Æ (f1 Æ f2)) Ç (v1 Æ (g1 Æ g2))
(: v1 Æ f1) Ç (v1 Æ g1)
Æ
(: v1 Æ f2) Ç (v1 Æ g2)
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
26
BDD Operations: And (Complex Case 1)
v1 = v2
What if f1 Æ f2 = g1 Æ g2 ?
v1
f1 Æ f2
Return f1 Æ f2
g1 Æ g2
(: v1 Æ (f1 Æ f2)) Ç (v1 Æ (g1 Æ g2))
(: v1 Æ f1) Ç (v1 Æ g1)
Æ
(: v1 Æ f2) Ç (v1 Æ g2)
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
27
BDD Operations: And (Complex Case 2)
v1 appears before
v2 in the variable
ordering
v1 < v2
v1
f1
v2
Æ
g1
(: v1 Æ f1) Ç (v1 Æ g1)
d2
f2
Æ
g2
(: v2 Æ f2) Ç (v2 Æ g2)
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
28
BDD Operations: And (Complex Case 2)
v1 < v2
What if f1 Æ d2 = g1 Æ d2 ?
v1
Return f1 Æ d2
f1 Æ d2
g1 Æ d2
(: v1 Æ (f1 Æ d2)) Ç (v1 Æ (g1 Æ d2))
(: v1 Æ f1) Ç (v1 Æ g1)
Æ
d2
O(n1 £ n2)
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
29
BDD Operations: Or
Or(d1,d2)
=
Not ( And ( Not(d1), Not(d2) ) )
O(n1 £ n2)
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
30
BDD Operations: Exist
Exist(“0”,v) = ?
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
31
BDD Operations: Exist
Exist(“0”,v) = “0”
Exist(“1”,v) = ?
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
32
BDD Operations: Exist
Exist(“0”,v) = “0”
Exist(“1”,v) = “1”
Exist((: v Æ f) Ç (v Æ g) , v) = ?
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
33
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) = ?
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
34
BDD Operations: Exist
O(n2)
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))
But f is SAT iff 9 V. f is not “0”. So why doesn’t this imply P = NP?
Because the BDD size changes!
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
35
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
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
36
BDD Application: Counting Sudoku Solutions
1
1
2
3
4
1
2
3
3
2
4
How many ways can you solve this puzzle?
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
37
BDD Application: Counting Sudoku Solutions
1
2
3
4
1
0
3
1
2
2
2
1
0
3
3
3
0
2
1
4
1
2
3
0
How many ways can you solve this puzzle? At least 2.
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
38
BDD Application: Counting Sudoku Solutions
1
2
3
4
1
0
3
1
2
2
1
2
0
3
3
3
0
2
1
4
2
1
3
0
How many ways can you solve this puzzle? At least 2.
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
39
BDD Application: Counting Sudoku Solutions
a11,b11
1
1
2
3
4
1
2
3
4
3
2
a44,b44
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
40
BDD Application: Counting Sudoku Solutions
: a13 Æ b13
1
1
2
3
4
1
2
3
4
3
2
a33 Æ : b33
a31 Æ b31
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
41
BDD Application: Counting Sudoku Solutions
a11 © a12 Ç b11 © b12
Æ
a11 © a13 Ç b11 © b13
Æ
a11 © a14 Ç b11 © b14
Æ
a12 © a13 Ç b12 © b13
Æ
1
1
2
3
4
1
2
3
3
Distinct
Elements
2
4
a12 © a14 Ç b12 © b14
Repeat for each row, column and sub-square
Æ
Construct BDD
a13 © a14 Ç b13 © b14
Count number of solutions
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
42
Graph Reachability
1
3
5
0
7
2
4
6
Which nodes are reachable from “7”?
{2,3,5,6,7}
But what if the graph has trillions of nodes?
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
43
Graph Reachability
1
3
5
0
7
2
:aÆ:bÆ:c
4
6
aÆbÆc
Use three Boolean variables (a,b,c) to encode each node?
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
44
Graph Reachability
1
3
5
0
7
2
4
:aÆ:bÆ:c
6
aÆbÆc
aÆ:bÆ:c
Use three Boolean variables (a,b,c) to encode each node?
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
45
Graph Reachability
aÆ:bÆc
1
3
5
0
7
2
4
:aÆ:bÆ:c
6
aÆbÆc
aÆ:bÆ:c
Use three Boolean variables (a,b,c) to encode each node?
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
46
Graph Reachability
1
3
5
0
7
2
4
6
aÆbÆ:c=?
Key Idea 1: Every Boolean formula represents a set of nodes!
The nodes whose encodings satisfy the formula.
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
47
Graph Reachability
1
3
5
0
7
2
4
6
a Æ b Æ : c = {6}
Key Idea 1: Every Boolean formula represents a set of nodes!
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
48
Graph Reachability
1
3
5
0
7
2
4
6
aÆb= ?
Key Idea 1: Every Boolean formula represents a set of nodes!
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
49
Graph Reachability
1
3
5
0
7
2
4
6
a Æ b = {6,7}
Key Idea 1: Every Boolean formula represents a set of nodes!
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
50
Graph Reachability
1
3
5
0
7
2
4
6
a©b= ?
Key Idea 1: Every Boolean formula represents a set of nodes!
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
51
Graph Reachability
1
3
5
0
7
2
4
6
a © b = {2,3,4,5}
Key Idea 1: Every Boolean formula represents a set of nodes!
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
52
Graph Reachability
1
3
5
0
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
Binary Decision Diagrams – Part 2
encoded using (a,b,c) and n’ is encoded using (a’,b’,c’)
Sagar Chaki, Sep 14, 2011
53
© 2011 Carnegie Mellon University
Graph Reachability
1
3
5
0
7
2
4
6
: a Æ : b Æ : c Æ : a’ Æ : b’ Æ c’
Key Idea 2: Edges can also be represented by Boolean formulas
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
54
Graph Reachability
1
3
5
0
7
2
4
6
a Æ : b Æ c Æ : a’ Æ b’ Æ : c’
Key Idea 2: Edges can also be represented by Boolean formulas
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
55
Graph Reachability
1
3
5
0
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
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
56
Graph Reachability
1
3
5
0
7
2
4
6
Variable renaming :
replace a’ with a
Image(S,R) =
(9 a,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
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
57
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.
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
58
Questions?
Sagar Chaki
Senior Member of Technical Staff
RTSS Program
Telephone: +1 412-268-1436
Email: [email protected]
U.S. Mail
Software Engineering Institute
Customer Relations
4500 Fifth Avenue
Pittsburgh, PA 15213-2612
USA
Web
www.sei.cmu.edu/staff/chaki
Customer Relations
Email: [email protected]
Telephone:
+1 412-268-5800
SEI Phone:
+1 412-268-5800
SEI Fax:
+1 412-268-6257
Binary Decision Diagrams – Part 2
Sagar Chaki, Sep 14, 2011
© 2011 Carnegie Mellon University
59