BDD Lecture
BDD Lecture
Pingqiang Zhou
ShanghaiTech University
Computational Boolean Algebra
Representations
Applying unate recursive paradigm (URP) in solving
tautology is a great “warm up” example.
Shows big idea: Boolean functions as things we manipulate with
software.
Data structure + operators
But URP is not the real way we do it.
2
Binary Decision Diagrams (BDD)
Originally studied by several people
… got practically useful in 1986
Randal Bryant of CMU made breakthrough on
Reduced Ordered BDD (ROBDD).
http://www.cs.cmu.edu/~bryant/
3
Binary Decision Diagrams for Truth Tables
Big Idea #1: Binary Decision Diagram x1 x2 x3 f
Turn a truth table for the Boolean function 0 0 0 0
into a Decision Diagram.
0 0 1 0
In simplest case, graph is just a tree.
0 1 0 0
By convention, don’t draw arrows on the
edges, we know where they go. 0 1 1 1
x1 1 0 0 0
Decision Tree
1 0 1 1
x2 x2 1 1 0 0
x3 x3 x3 1 1 1 1
x3
0 0 0 1 0 1 0 1
4
Binary Decision Diagrams
Vertex represents a variable. x1 x2 x3 f
Edge out of a vertex is a decision (0 or 1) 0 0 0 0
on that variable. 0 0 1 0
Follow green dashed line for 0. 0 1 0 0
Follow red solid line for 1. 0 1 1 1
Function value determined by leaf value. 1 0 0 0
x1 1 0 1 1
1 1 0 0
x2 x2
1 1 1 1
x3 x3 x3 x3
5 0 0 0 1 0 1 0 1
Binary Decision Diagrams
Some Terminology
A “variable” vertex
The “lo” pointer to
“lo” child of vertex
The “hi” pointer to
x1 “hi” child of vertex
A “constant” vertex
x2 x2 at the leaf of the tree
x3 x3 x3 x3
0 0 0 1 0 1 0 1
8
Terminology: Canonical form
Canonical: Representation that does not depend on gate
implementation of a Boolean function.
Same function of same variables always produces this exact
same representation.
9
Binary Decision Diagrams
What’s wrong with this diagram representation?
It is not canonical, and it is way too big to be useful (it is as
big as truth table!)
x1 x1
x2 x2 x2 x3
x3 x3 x3 x3 x3 x3 x2 x2
0 0 0 1 0 1 0 1 0 0 0 1 0 0 1 1
10
Binary Decision Diagrams
Big idea #2: ordering
Restrict global ordering of variables.
It means: every path from root to a leaf visit variables in the
SAME order.
Note: it is OK to omit a variable if you don’t need to check it
to decide which leaf node to reach for final value of function.
x1 x1
x2 x2 x2
0 0 0 1 0 0 1
11
Ordering BDD Variables
Assign (an arbitrary) global ordering to vars:
x1 < x2 < x3
Variables must appear in this specific order along all paths; ok to
skip vars
x1 x1 x3 x1
x2 x2
x3 x1
x3 x1
x1 x1
x2 x2 x2
x3 x3 x3 x3 x3 x3 x3
0 0 0 1 0 1 0 1 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 x2 node and its children, replace with x3 node is an
example of this.
x1 x1
x2 x2 x2
x3 x3 x3 x3 x3 x3 x3
0 0 0 1 0 1 0 1 0 0 0 1 0 1
14
Binary Decision Diagrams
Reduction
Why are we doing this?
Graph size: Want result as small as possible.
Canonical form: For same function, given same variable
order, want there to be 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
x2 x2 x2 x2
x3 x3 x3 x3 x3 x3 x3 x3
0 0 0 1 0 1 0 1
16
0 1
Reduction Rules
Reduction Rule 2: Merge isomorphic nodes
Isomorphic = 2 nodes with same variable and identical children
Cannot tell these nodes apart from how they contribute to
decisions in graph.
Note: means exact same physical child nodes, not just children
with same label. NOT isomorphic
x x
x x
y z y
y z isomorphic
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 below, 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
x2 x2 x2 x2
x3 x3 x3 x3 x3
x3
0 1 0 1
isomorphic
19
Reduction Rules
Reduction Rule 3: Eliminate Redundant Tests
Redundant test: both 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 x
node (x) into child (y) of the y
removed node. y
20
Reduction Rules
Apply Rule 3, eliminating redundant tests, to our example.
x1 x1
x2 x2
x2
We are done!
x3 x3 x3
0 1 0 1
21
Reduction Rules
The above is a simple example.
The reduction process terminates by applying each rule once.
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
variables, and reduce the diagram
Name: Reduced Ordered BDD (ROBDD)
0 1
ROBDD for 𝑓 𝑎, 𝑏, … , 𝑧 = 𝑎
𝑓
24 0 1
ROBDD for AND
𝑓 𝑎, 𝑏 = 𝑎𝑏
𝑓 𝑓 𝑓
a a a
b b b b b
0 0 0 1 0 1 0 1
25
ROBDD for OR
𝑓 𝑎, 𝑏 = 𝑎 + 𝑏
𝑓 𝑓 𝑓
a a a
b b b b b
0 1 1 1 0 1 0 1
26
ROBDD for AND/OR on Multiple Inputs
𝑓 𝑎, 𝑏, 𝑐, 𝑑 = 𝑎𝑏𝑐𝑑 𝑓 𝑎, 𝑏, 𝑐, 𝑑 = 𝑎 + 𝑏 + 𝑐 + 𝑑
𝑓 𝑓
a a
b b
c c
d d
1 0 1
0
27
ROBDD for XOR
𝑓 𝑎, 𝑏 = 𝑎⨁𝑏
𝑓 𝑓
a a
b b b b
0 1 1 0 0 1
28
ROBDD for XOR on Multiple Inputs
𝑓 𝑎, 𝑏, 𝑐, 𝑑 = 𝑎⨁𝑏⨁𝑐⨁𝑑
𝑓
a
b b
c c
d 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. 𝑏⨁𝑐 ⨁𝑑
a
BDDs good at extracting & representing
sharing of subfunctions in b b
subgraphs. 𝑐⨁𝑑
c c 𝑑
d d
0 1
30
BDD Sharing: Multi-Rooted BDD
If we are building BDDs for multiple functions,
…then there may be same subgraphs among different BDDs.
Don’t represent same things multiple times; share them!
31
Multi-Rooted BDD: Example
Build BDDs for two functions
𝑓1 𝑥1 , 𝑥2 , 𝑥3 = 𝑥1 𝑥2 𝑥3 𝑓2 𝑥1 , 𝑥2 , 𝑥3 = 𝑥1 + 𝑥2 𝑥3
𝑓1 𝑓2 𝑓1 𝑓2
Shaded parts are same
x1 x1 … Can be shared! x1 x1
x2 x2
x2
x3 x3
x3
0 1 0 1 0 1
32
Review
Binary decision diagram (BDD)
Big idea #1: Decision diagrams
Big idea #2: Assign global ordering to variables.
Big idea #3: Reduction
Merge equivalent leaves
Merge isomorphic nodes
Eliminate redundant tests
33
Review
ROBDD is a canonical form data structure for any Boolean
function.
Same function always generates exactly same graph... for
same variable 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 build a multi-rooted BDD for multiple functions.
34
Multi-Rooted BDD: Example
Look at sum S3 and carry out Cout
[Rob Rubentar]
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 better share stuff
[Rob Rubentar]
36
Multi-Rooted BDD: Example
Why stop at 2 roots?
Sharing among sets of functions
(several separate BDDs) reduces
the size of BDD!
37
BDD for OP of Functions
𝑓 = 𝑂𝑅(𝑔, ℎ)
𝑔 ℎ Order: 𝑎 < 𝑏 < 𝑐
𝑎 𝑏
𝑏 𝑐 ?
0 1 0 1
38
BDD and Cofactors
𝑓
x
𝑓𝑥 𝑓𝑥
39
How to Implement OP?
Example: op = AND 𝑓 =𝑔∙ℎ
BDD and cofactors:
x
𝑔∙ℎ 𝑥 𝑔∙ℎ 𝑥
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 combineBDD(x, fLo, fHi);
}
𝑔 𝑖𝑓 𝑥 < 𝑟𝑜𝑜𝑡(𝑔)
𝑛𝑒𝑔𝐶𝑜𝑓𝐵𝐷𝐷 𝑔, 𝑥 = 𝑔𝑥 =
𝑙𝑜(𝑔) 𝑖𝑓 𝑥 = 𝑟𝑜𝑜𝑡(𝑔)
𝑔 𝑖𝑓 𝑥 < 𝑟𝑜𝑜𝑡(𝑔)
𝑝𝑜𝑠𝐶𝑜𝑓𝐵𝐷𝐷 𝑔, 𝑥 = 𝑔𝑥 =
ℎ𝑖(𝑔) 𝑖𝑓 𝑥 = 𝑟𝑜𝑜𝑡(𝑔)
42
Example of OP
Obtain 𝑓 = 𝑂𝑅(𝑔, ℎ).
𝑔 Order: 𝑎 < 𝑏 < 𝑐 ℎ
Cofactor on variable 𝑎
𝑎 𝑏
𝑏 𝑐
0 1 ℎ𝑎 0 1 ℎ𝑎
𝑔𝑎 𝑔𝑎
𝑏 𝑏
𝑏 𝑐 𝑐
0
43 0 1 0 1 0 1
Example of OP (cont.)
Recursively compute 𝑂𝑅(𝑔𝑎 , ℎ𝑎 )
ℎ𝑎 We obtain:
𝑂𝑅(𝑔𝑎 , ℎ𝑎 )
𝑏
𝑔𝑎 𝑏
𝑐
0 0 1 𝑐
Termination condition 0 1
44
Example of OP (cont.)
Recursively compute 𝑂𝑅(𝑔𝑎 , ℎ𝑎 )
ℎ𝑎
𝑔𝑎 Cofactor on variable 𝑏
𝑏
𝑏 𝑐
0 1 0 1
ℎ𝑎𝑏
𝑔𝑎𝑏 ℎ𝑎 𝑏 𝑔𝑎𝑏
𝑐
0 0 1 0 1
Termination condition Termination condition
𝑂𝑅(𝑔𝑎𝑏 , ℎ𝑎𝑏 ) 𝑂𝑅(𝑔𝑎𝑏 , ℎ𝑎𝑏 )
0 1
46
Example of OP (cont.)
0 1
0 1
47
Example of OP (cont.)
Based on the recursion results, obtain 𝑂𝑅 𝑔, ℎ
Note: we cofactor on 𝑎.
𝑂𝑅(𝑔, ℎ)
𝑂𝑅(𝑔𝑎 , ℎ𝑎 )
𝑎
𝑂𝑅(𝑔𝑎 , ℎ𝑎 )
𝑏
𝑏 𝑏
𝑐 𝑏
𝑐
0 1 0 1
0 1
Done!
48
BDDs: Build Up Incrementally…
For a gate-level network, build the BDD for the output
incrementally.
a A D
AND
B F
b OR
C AND
c E
Each input is a BDD, each gate becomes an operator 𝑜𝑝
that produces a new output BDD.
BDD operator script
Build BDD for F as a script of calls 1. A = CreateVar(“a”)
to basic BDD operators. 2. B = CreateVar(“b”)
3. C = CreateVar(“c”)
Stick to a global ordering. 4. D = AND(A, B)
5. E = AND(B, C)
49 6. F = OR(D, E)
Example: Build BDD Incrementally
a A D BDD operator script
1. A = CreateVar(“A”)
AND
B F 2. B = CreateVar(“B”)
b OR 3. C = CreateVar(“C”)
C AND 4. D = AND(A, B)
c E 5. E = AND(B, C)
6. F = OR(D, E)
Global ordering: 𝑎 < 𝑏 < 𝑐
1. 𝐴 2. 𝐵 3. 𝐶 4. 𝐷 5. 𝐸
𝑎 𝑏 𝑐 𝑎 𝑏
0 1 0 1 0 1 𝑏 𝑐
50 0 1 0 1
Example: Build BDD Incrementally
a A D BDD operator script
1. A = CreateVar(“A”)
AND
B F 2. B = CreateVar(“B”)
b OR 3. C = CreateVar(“C”)
C AND 4. D = AND(A, B)
c E 5. E = AND(B, C)
6. F = OR(D, E)
Global ordering: 𝑎 < 𝑏 < 𝑐
𝐹
6.
𝑎
𝑏 𝑏
51 0 1
Application of BDD
Tautology checking
Solution:
Build BDD for 𝑓.
Check if the BDD is just the BDD for 𝑓 = 1.
52
Application of BDD
Satisfiability (SAT)
Satisfiability (SAT): Does there exist an input pattern for
variables that lets F = 1? If yes, return one pattern.
Recall: In network repair problem, we want to find
(𝑑0 , 𝑑1 , 𝑑2 , 𝑑3 ) so that ∀𝑎𝑏 𝑧 𝑑0 , 𝑑1 , 𝑑2 , 𝑑3 = 1
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.
Solution #2:
Build BDD for function 𝐹 ⨁𝐺 𝑓
Check if the BDD is just the BDD for 𝑓 = 1.
1
54
Application of BDD
Comparing Logic Implementations
What inputs make functions F and G give different
answers?
Solution:
Build BDD for 𝐻 = 𝐹⨁𝐺.
Ask “SAT” question for 𝐻.
55
BDDs: Seem Too Good To Be True?!
Problem : Variable ordering matters.
Example: 𝑎1 ∙ 𝑏1 + 𝑎2 ∙ 𝑏2 + 𝑎3 ∙ 𝑏3
Good ordering:𝑎1 < 𝑏1 < 𝑎2 Bad ordering: 𝑎1 < 𝑎2 < 𝑎3
< 𝑏2 < 𝑎3 < 𝑏3 < 𝑏1 < 𝑏2 < 𝑏3
56
Variable Ordering: How to Handle?
Variable ordering heuristics: make nice BDDs for
reasonable problems.
57
Variable Ordering: Intuition
Rules of thumb for BDD ordering
Related inputs should be near each other in order.
Groups of inputs that can determine function by themselves
should be (i) close together, and (ii) near top of BDD.
Example: 𝑎1 ∙ 𝑏1 + 𝑎2 ∙ 𝑏2 + 𝑎3 ∙ 𝑏3
Good ordering:
𝑎1 < 𝑏1 < 𝑎2 < 𝑏2 < 𝑎3 < 𝑏3
Why?
𝑎𝑖 and 𝑏𝑖 together can determine
the function value
58
Variable Ordering: Intuition
Rules of thumb for BDD ordering
Related inputs should be near each other in order.
Groups of inputs that can determine function by themselves
should be (i) close together, and (ii) near top of BDD.
Example: 𝑎1 ∙ 𝑏1 + 𝑎2 ∙ 𝑏2 + 𝑎3 ∙ 𝑏3
Bad ordering:
𝑎1 < 𝑎2 < 𝑎3 < 𝑏1 < 𝑏2 < 𝑏3
Why?
We need to remember 𝑎1, 𝑎2, 𝑎3
before we see any 𝑏’s.
59
Variable Ordering: Practice
Arithmetic circuits are important logic; how are their
BDDs?
Many carry chain circuits have easy linear sized ROBDD
orderings: Adders, Subtractors, Comparators.
Rule is alternate variables in the BDD order: a0, b0, a1, b1,
a2, b2, …, an, bn.
Are all arithmetic circuits easy?
No! Multiplication is exponential in number of nodes for any
order.
General experience with BDDs
Many tasks have reasonable ROBDD sizes; algorithms are
practical to about 100M nodes.
People spend a lot of effort to find orderings that work …
60
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.
Problems
Variable ordering matters; sometimes BDD is just too big.
Often, we just want to know SAT – don’t need to build the
whole function.
61