Lecture 6 BDD
Lecture 6 BDD
Boolean functions are mostly represented by truth tables and propositional formulas
�)
𝒇𝒇 𝒙𝒙, 𝒚𝒚 ≝ 𝒙𝒙(𝒚𝒚 + 𝒙𝒙
Representation with truth table:
o Space inefficient. 100 variables results in 2100 lines.
o Checking satisfiability and equivalence is also inefficient.
Representation with propositional formula:
o Better than truth table in terms of compact representation.
o Deciding whether two prop. formulas f and g denote same Boolean function is expensive.
Representing Switching Functions
Truth Tables
• Satisfiability and equivalence check: easy; Boolean operations also easy.
• Very space inefficient: 2n entries for n variables.
BDDs
• Graph representation similar to a binary tree
with terminal nodes labeled with either 0 or
1 and non-terminal nodes labeled with
Boolean variables x, y, z…
• Able to efficiently represent large functions
• Some representations are canonical (unique) Each non-terminal node has two edges
• One dashed line and one solid line
• Solid line: Corresponds when variable value = 1
• Dashed line: Corresponds when variable value = 0
Mux Representation of Boolean Functions
MUX circuit to implement logic function S
Mux Representation of Boolean Functions
Corresponding BDD to implement function S
One-to-one correspondence to the MUX gates in the flipped circuit
x1 0 0 0 0 1 1 1 1
x2 0 0 1 1 0 0 1 1
x3 0 1 0 1 0 1 0 1 x1
0 1
F 0 0 0 1 0 1 0 1
x2 x2
0 1 0 1
x3 x3 x3 x3
0 1 0 10 1 0 1
0 0 0 1 0 1 0 1
a a a a
b b
b b b b
x2 x2 x2 x2
x2
x3 x3 x3 x3 x3 x3 x3
0 0 0 1 0 1 0 1 0 1 0 1
Decision Tree
reduction BDD
14
BDD Formal Definition
• Directed Acyclic Graph (DAG)
o vertex set V
o edge set E (each edge has a head and tail => a direction)
o no cycles exist in G(V,E)
• Binary Decision Diagram (BDD)
o tree or rooted DAG where each vertex denotes a binary decision
o Example: F = (a + b)c
Representation of Boolean Functions - BDD
Ex : (a + b)(c + d)
Ex : ab + cd
a 1 a
0
0 b 1
0
b 1
c
1 1 c
0 0
0 d
0 1 d 0
0 1
0 1 0 1
BDD Formation – Steps for f = ab + cd
a a a a
1 1 1 1
0 0 0 0
b b b b
0 0 0
c c c c
1 1
1 1 1
d d
0 0 0
1
1 0 1 0 1
How to construct BDD?
f = ac + bc + a’b’c’
= a’ (b’c’ + bc) + a (c + bc)
= a’ (b’c’ + bc) + a (c) This is the
f first step.
The process
a is continued
for all input
variables.
b’c’ + bc c
BDD Reduction Rules
Cofactor and Boolean expansion
Example
BDD Synthesis
Binary Decision Diagram (BDD)
Example 2
Ordered binary decision diagrams ensure the variables appear in the same order along all paths
from the root to the leaves
Ordered Binary Decision Diagram (OBDD)
Different Ordering Lead to Different Complexity – Example 1
Variable ordering important, may result in a more complex (or simple) BDD
• All three BDDs below represent the same function
• Third ordering (b ≤ c ≤ a ≤ d) optimal because there is exactly one node for each variable
Ordered Binary Decision Diagram (OBDD)
Different Ordering Lead to Different Complexity – Example 2
ordered not
a a
order = a,c,b ordered
c c b c
b c b
0 1 0 1
c+bd b c+bd c
b b
c+d 0
c c c d+b d
d b b
d
0 1 0 1
28
ROBDD – Example 2
• f = (a + b).c
29
ROBDD – Example 3
30
ROBDD – Practice 1
f = abc + bd + cd
b b
0 1 0 1
cd ac + d + cd c c
0 0 1
b
0 1 0 d d
0 1 0
c c 1
0 1 0 0 1 a 1
1
0 1
0 d d a+d
0 1
31
ROBDD – Practice 2
f = abc + b’d + c’d
32
ROBDD – Practice 3
33
An Important Point
• The size of a BDD can vary drastically if the order in which the
variables are expanded is changed.
• The number of nodes in the BDD can be exponential in the number
of variables in the worst case, even after reduction.
Some Benefits of BDD
37
Zero Suppressed BDDs - ZBDDs
ZBDD’s were invented by Minato to efficiently represent sparse sets. They have
turned out to be useful in implicit methods for representing primes (which
usually are a sparse subset of all cubes).
ZBDD:
BDD: 0 1
0
1
0
0 1 0 1
0 1 0 1
38
Canonicity
Theorem: (Minato) ZBDD’s are canonical given a variable ordering and the support set.
x1 x3 x3
Example:
x1
x2
x2 1
1 0 1
BDD ZBDD if 1 0 ZBDD if
support is support is
x 1, x 2 x 1, x 2, x 3
x3
x1
x2
1
1 0 ZBDD if
BDD support is
x 1, x 2 , x 3
39
Zero-Suppressed BDDs
a a
b b b
b
0
x x x x
x x x x
• The size of a BDD can vary drastically if the order in which the variables
are expanded is changed.
• The number of nodes in the BDD can be exponential in the number of
variables in the worst case, even after reduction.
Use of BDD in Synthesis
• BDD is canonical for a given variable ordering.
• It implicitly uses factored representation:
x
x’h + xh = h
h y h y
a b a b
h x h x h x
y z y z y z
ah + bh = (a+b)h
• Variable reordering can reduce the size of BDD.
• Implicit logic minimization.
• Some redundancy rumored during the construction of
BDD itself.
MUX realization of functions
f f
x 0 1 x
f g
f g
MUX-based Functional Decomposition
f f
0 1
h
g
h
f g f
An example ===>
MUX-based Functional Decomposition’s Example
f f
a
0 1
b a
c d
c b
0 1
0 1
Synthesis Example
49
Synthesis Example (g)
50
Synthesis Example (h)
51
Synthesis Example (share)
Efficient Implementation of BDD’s
Unique Table:
• avoids duplication of existing nodes
– Hash-Table: hash-function(key) = value
– identical to the use of a hash-table in AND/INVERTER circuits
Computed Table:
• avoids re-computation of existing results
hash value
No collision chain
of key
53
Efficient Implementation of BDD’s
• BDDs is a compressed Shannon co-factoring tree:
• f = v fv + v fv
• leafs are constants “0” and “1”
• Three components make ROBDDs canonical (Proof: Bryant 1986):
– unique nodes for constant “0” and “1”
– identical order of case splitting variables along each paths
– hash table that ensures:
• (node(fv) = node(gv)) ∧ (node(fv) = node(gv)) ⇒ node(f) = node(g)
– provides recursive argument that node(f) is unique when using the
unique hash-table
f
v
0 1
fv fv
54
Onset is Given by all Paths to “1”
F = b’+a’c’ = ab’+a’cb’+a’c’ all paths to the 1 node
a f
0
fa = cb’+c’ 1
c 1 fa= b’
0 b
0
1
0 1
Notes:
• By tracing paths to the 1 node, we get a cover of pair wise disjoint cubes.
• The power of the BDD representation is that it does not explicitly
enumerate all paths; rather it represents paths by a graph whose size is
measures by its nodes and not paths.
• A DAG can represent an exponential number of paths with a linear
number of nodes.
• BDDs can be used to efficiently represent sets
– interpret elements of the onset as elements of the set
– f is called the characteristic function of that set
55
Implementation
Variables are totally ordered: If v < w then v occurs “higher” up in the ROBDD
Top variable of a function f is a variable associated with its root node.
Example: f = ab + a’bc + a’bc’. Order is (a < b < c).
fa = b, fa = b
f b is top variable of f
a
b f
f does not depend on a, b reduced
since fa = fa .
0 1 0 1
g h g
h
56
ITE Operator
ite(f , g , h=
) fg + f h
ITE operator can implement any two variable logic function. There are 16 such
functions corresponding to all subsets of vertices of B 2:
Table Subset Expression Equivalent Form
0000 0 0 0
0001 AND(f, g) fg ite(f, g, 0)
0010 f>g fg ite(f,g, 0)
0011 f f f
0100 f<g fg ite(f, 0, g)
0101 g g g
0110 XOR(f, g) f⊕g ite(f,g, g)
0111 OR(f, g) f+g ite(f, 1, g)
1000 NOR(f, g) f+g ite(f, 0,g)
1001 XNOR(f, g) f⊕g ite(f, g,g)
1010 NOT(g) g ite(g, 0, 1)
1011 f≥g f + g ite(f, 1, g)
1100 NOT(f) f ite(f, 0, 1)
1101 f≤g f + g ite(f, g, 1)
1110 NAND(f, g) fg ite(f, g, 1)
1111 1 1 1
57
Unique Table - Hash Table
hash index collision
of key chain
58
Recursive Formulation of ITE
60
Example
F G H I
a a b a
1 0 1 0 1 0 1 0
B D C J
b C c d b
1 0 1
1 0 1 0 1 0 1 0
D
1 0 1 0 1 0 0
Check: F = a + b, G = ac, H = b + d
ite(F, G, H) = (a + b)(ac) + ab(b + d) = ac + abd
61
Computed Table
Keep a record of (F, G, H ) triplets already computed by the ITE operator
– software cache ( “cache” table)
– simply hash-table without collision chain (lossy cache)
62
Extension - Complement Edges
Combine inverted functions by using complemented edge
– similar to circuit case
– reduces memory requirements
– BUT MORE IMPORTANT:
• makes some operations more efficient (NOT, ITE)
G G
two different
DAGs
0 1 0 1
G G
only one DAG
using complement
pointer
63
0 1
Extension - Complement Edges
V V V V
V V V V
Solution: Always choose one on left, i.e. the “then” leg must have no
complement edge.
64
Ambiguities in Computed Table
Standard Triples: ite(F, F, G ) ⇒ ite(F, 1, G )
ite(F, G, F ) ⇒ ite(F, G, 0 )
ite(F, G,F ) ⇒ ite(F, G, 1 )
ite(F,F, G ) ⇒ ite(F, 0, G )
66
Example: Tautology Checking
Algorithm ITE_CONSTANT(f,g,h) { // returns 0,1, or NC
if(TRIVIAL_CASE(f,g,h) return result (0,1, or NC)
if((res = HASH_LOOKUP_COMPUTED_TABLE(f,g,h))) return res
v = TOP_VARIABLE(f,g,h)
i = ITE_CONSTANT(fv,gv,hv)
if(i == NC) {
INSERT_COMPUTED_TABLE(NC, HASH_KEY{f,g,h}) // special table!!
return NC
}
e = ITE_CONSTANT(fv,gv,hv)
if(e == NC) {
INSERT_COMPUTED_TABLE(NC, HASH_KEY{f,g,h})
return NC
}
if(e != i) {
INSERT_COMPUTED_TABLE(NC, HASH_KEY{f,g,h})
return NC
}
INSERT_COMPUTED_TABLE(e, HASH_KEY{f,g,h})
return i;
}
67
Compose
Compose(F, v, G ) : F(v, x) → F( G(x), x), means substitute v by G(x)
Algorithm COMPOSE(F,v,G) {
if(TOP_VARIABLE(F) > v) return F // F does not depend
on v
if(TOP_VARIABLE(F) == v) return ITE(G,F1,F0)
i = COMPOSE(F1,v,G)
e = COMPOSE(F0,v,G)
return ITE(TOP_VARIABLE(F),i,e) // Why not
CREATE_NODE...
}
Notes:
1. F1 is the 1-child of F, F0 the 0-child.
2. G , i, e are not functions of v
3. If TOP_VARIABLE of F is v, then ite (G , i, e ) does replacement of
v by G.
68
Variable Ordering
• Static variable ordering
– variable ordering is computed up-front based on the problem structure
– works very well for many combinational functions that come from circuits we actually build
• general scheme: control variables first
• DFS order is pretty good for most cases
– work bad for unstructured problems
• e.g., using BDDs to represent arbitrary sets
– lots of research in ordering algorithms
• simulated annealing, genetic algorithms
• give better results but extremely costly
69
Dynamic Variable Ordering
• Changes the order in the middle of BDD applications
– must keep same global order
BDD Implementation:
... ...
... ...
70
Dynamic Variable Ordering
Theorem (Friedman):
Permuting any top part of the variable order has no effect on the nodes labeled by variables in
the bottom part.
Permuting any bottom part of the variable order has no effect on the nodes labeled by variables
in the top part.
• Trick: Two adjacent variable layers can be exchanged by keeping the original memory locations
for the nodes
mem1
f0 f f1 f0 f f1
mem1
mem2
a mem2 mem3 b b b
mem3
b b a a
c c c c c c c c
f00 f01 f10 f11 f00 f01 f10 f11
71
Dynamic Variable Ordering
• BDD sifting:
– shift each BDD variable to the top and then to the bottom and see which position had minimal
number of BDD nodes
– efficient if separate hash-table for each variable
– can stop if lower bound on size is worse then the best found so far
– shortcut:
• two layers can be swapped very cheaply if there is no interaction between them
– expensive operation, sophisticated trigger condition to invoke it
72
Garbage Collection
• Very important to free and ruse memory of unused BDD nodes
– explicitly freed by an external bdd_free operation
– BDD nodes that were temporary created during BDD operations
73
Garbage Collection
• Timing is very crucial because garbage collection is expensive
– immediately when node gets freed
• bad because dead nodes get often reincarnated in next operation
– regular garbage collections based on statistics collected during BDD operations
– “death row” for nodes to keep them around for a bit longer
74