<<

Binary Decision Diagrams

Hao Zheng

Department of and Engineering University of South Florida Tampa, FL 33620 Email: [email protected] Phone: (813)974-4757 Fax: (813)974-5456

Hao Zheng (CSE, USF) Comp Sys Verification 1 / 44 1 Binary Decision

2 Ordered Binary Decision Diagram

3 From BDT to BDDs

4 Variable Ordering

5 Logic Operations by BDDs

Hao Zheng (CSE, USF) Comp Sys Verification 2 / 44 Binary Decision Diagrams

Binary decision diagrams (BDDs) are graphs representing Boolean • functions. They can be made canonical. • They can be very compact for many applications. • They are important since many applications can be converted to • sequences of Boolean operations. References: • • R. Bryant, Graph-Based Algorithms for Manipulation, IEEE Transactions on Computers, 1986. • R. Bryant Symbolic Boolean Manipulation with Ordered Binary Decision Diagrams, ACM Computing Surveys, 1992. • Textbook, 6.7.3 6.7.4 −

Hao Zheng (CSE, USF) Comp Sys Verification 3 / 44 Representing Switching Functions

Truth Tables • • Satisfiability and equivalence check: easy; boolean operations also easy. • Very space inefficient: 2n entries for n variables. Disjunctive Normal Form (DNF) • • Satisfiability is easy: find a disjunct without complementary literals. • and conjunction complicated. Conjunctive Normal Form (CNF) • • Satisfiability problem is NP-complete (Cook’s theorem). • Negation and disjunction complicated.

Hao Zheng (CSE, USF) Comp Sys Verification 4 / 44 Representing Switching Functions

representation compact? sat equ ∧ ∨ ¬ never hard hard hard hard hard DNF sometimes easy hard hard easy hard CNF sometimes hard hard easy hard hard propositional formula often hard hard easy easy easy reduced ordered binary decision diagram often easy easy∗ medium medium easy

∗ Provided appropriate implementation techniques are used.

Hao Zheng (CSE, USF) Comp Sys Verification 5 / 44 ExampleSymbolic Symbolic Encoding: Representation An Example of a Transition System

a

0/ s0 s1

s3 s2

a,b b { } 0 0 0 Switching function: ∆(x1, x2, x1, x2) = 1 if and only if s s Switching function: (x1,x2,x10 ,x20 )=1 if and only if s s0→ s s0 ! s 0 0 s0 0 0 ∆(x1, x2, x1, x2|){z =} | {z } ( x1 x2 x1 x2) | {z } | {z } ¬ ∧ ¬ ∧ ¬0 ∧ 0 (x1,x2,x0 ,x0 )=( ( xx11 xx22 xx10 x02)) 1 2 ∨ ¬¬ ^∧¬ ¬ ^∧¬0 1 ∧^ 20 (( xx1 x2x xx1 x )x2) ∨_ ¬¬ 1 ^∧ ¬ 2∧^ 10 ∧^ ¬20 ...( x1 x2 x10 x20 ) ∨_ ¬ ^ ^ 0 ^ ¬0 (...x1 x2 x x ) ∨_ ∧ ∧ 1 ∧ 2 (x1 x2 x10 x20 ) Hao Zheng (CSE, USF) Comp_ Sys Verification^ ^ ^ 6 / 44

Chris J. Myers (Lecture 6: CTL) Verification of Cyber-Physical Systems 100 / 155 Contents

1 Binary Decision Tree

2 Ordered Binary Decision Diagram

3 From BDT to BDDs

4 Variable Ordering

5 Logic Operations by BDDs

Hao Zheng (CSE, USF) Comp Sys Verification 7 / 44 Transition Relation as a BDT Binary Decision Tree: Example

x1

x2 x2

x10 x10 x10 x10

x x x x20 x20 x20 x20 x20 20 20 20

0 1 0 1 0 11001111110

The BDT for function f on ~x = x1, . . . , xn has depth n. • { } Every node is labeled with a variable. < < < • A BDT representing for our example using ordering x1 x2 x10 x20 . Every node labeled with x has two outgoing edges. • i • 0-edge: xi = 0 (dashed) and, • 1-edge: xi = 1 (solid). Chris J. Myers (Lecture 6: CTL) Verification of Cyber-Physical Systems 110 / 155

Hao Zheng (CSE, USF) Comp Sys Verification 8 / 44 Binary DecisionTransition Tree: Example Relation as a BDT

x1

x2 x2

x10 x10 x10 x10

x x x x20 x20 x20 x20 x20 20 20 20

0 1 0 1 0 11001111110

Every non-terminal node n has two successor nodes. • • low(n): the node at the end of the 0-edge of node n. A BDT representing for our example using ordering x1 < x2 < x10 < x20 . • high(n): the node at the end of the 1-edge of node n.

Chris J. Myers (Lecture 6: CTL) Verification of Cyber-Physical Systems 110 / 155 Hao Zheng (CSE, USF) Comp Sys Verification 8 / 44 Binary DecisionTransition Tree: Example Relation as a BDT

x1

x2 x2

x10 x10 x10 x10

x x x x20 x20 x20 x20 x20 20 20 20

0 1 0 1 0 11001111110

The edge labelings of a path from the root to a terminal is an • evaluationA BDT representings = [x1 =b1for, . . our . , x examplem = bm using], where orderingbi x <0,x1 <. x < x . ∈1 { 2} 10 20 The labeling of the terminal node is the output of f(s). •

Chris J. Myers (Lecture 6: CTL) Verification of Cyber-Physical Systems 110 / 155 Hao Zheng (CSE, USF) Comp Sys Verification 8 / 44 Transition Relation as a BDT Binary Decision Tree: Example

x1

x2 x2

x10 x10 x10 x10

x x x x20 x20 x20 x20 x20 20 20 20

0 1 0 1 0 11001111110

The subtree of node v at level i for variable ordering x1 < . . . < xn represents: A BDT representing for our example using ordering x1 < x2 < x10 < x20 . fv = f x =b ,...,x =b | 1 1 i−1 i−1 which is a switching function over xi, . . . , xn and where x1 = b1,..., { } xiChris−1 J.= Myersbi− (Lecture1 are 6: CTL) the decisionsVerification made of Cyber-Physical along Systems the path from root to node110v /. 155

Hao Zheng (CSE, USF) Comp Sys Verification 8 / 44 Binary Decision Tree

The BDT for function f on Var = z1, . . . , zm has depth m with • { } outgoing edges for node at level i stand for zi = 0 (dashed) and zi = 1 (solid). For evaluation s = [z = b , . . . , z = b ], f(s) is the value of the leaf • 1 1 m m reached by traversing the BDT from the root using branch zi = bi. The subtree of node v at level i for variable ordering z < . . . < z • 1 m represents: fv = f z =b ,...,z =b | 1 1 i−1 i−1 which is a switching function over zi, . . . , zm and where z1 = b1,..., { } zi−1 = bi−1 are the decisions made along the path from root to node v.

Hao Zheng (CSE, USF) Comp Sys Verification 9 / 44 Considerations on BDTs

BDTs are a different form of truth tables. • BDTs are not compact: • • A BDT for switching function f on n variables has 2n leafs. • The size of a BDT does not change if the variable order changes. They are as space inefficient as truth tables! ⇒ BDTs contain a lot of redundancy: • • All leafs with value one (zero) could be collapsed into a single leaf. • A similar scheme could be adopted for isomorphic subtrees.

Two graphs rooted at nodes u and v are isomorphic, denoted as u v when both following conditions hold. ≡ value(u) = value(v) if u and v are terminals. • low(u) low(v) high(u) high(v), otherwise. • ≡ ∧ ≡

Hao Zheng (CSE, USF) Comp Sys Verification 10 / 44 Contents

1 Binary Decision Tree

2 Ordered Binary Decision Diagram

3 From BDT to BDDs

4 Variable Ordering

5 Logic Operations by BDDs

Hao Zheng (CSE, USF) Comp Sys Verification 11 / 44 Ordered Binary Decision Diagram (OBDD)

OBDDs rely on compactions of BDT representations. • Idea: skip redundant fragments of BDT representations. • Collapse subtrees with all terminals having same value. • Identify nodes with isomorphic subtrees. • This yields directed acyclic graphs with outdegree two. • Inner nodes are labeled with variables. • Leafs are labeled with function values (zero and one). • A unique variable ordering is followed by every path. • Each variable is assigned an unique index. • Each BDD node v has an index index(v) which is the index of the • variable labeled in v. • index(v) < index(low(v)) if low(v) is a non-terminal, • index(v) < index(high(v)) if high(v) is a non-terminal.

Hao Zheng (CSE, USF) Comp Sys Verification 12 / 44 Transition RelationTransition as a Relation BDT as a BDT

x1

x2 x2

x10 x10 x10 x10

x x x x20 x20 x20 x20 x20 20 20 20

0 1 0 1 0 11001111110

< < <0 0 A BDTA BDT representing representing∆ forfor our our example example using ordering orderingxx1 1

ChrisHao J. Zheng Myers (Lecture(CSE, USF) 6: CTL) VerificationComp of Cyber-PhysicalSys Verification Systems 11013 / 155/ 44 Transition Relation as a BDD Transition Relation as an ROBDD

x1

x2 x2

x10 x10 x10

x20 x20

0 1

An example ROBDD representing for our example using x1 < x2 < x10 < x20 . 0 0 A BDT representing ∆ for our example using ordering x1 < x2 < x1 < x2. Chris J. Myers (Lecture 6: CTL) Verification of Cyber-Physical Systems 120 / 155

Hao Zheng (CSE, USF) Comp Sys Verification 14 / 44 OBBDs and Boolean Functions

Let ℘ be a variable ordering for Var where ℘ = (z , . . . , z ). • 1 m Every OBDD is defined wrt a given variable ordering. • • The nodes in every path are labeled with variables in the order as in ℘. A terminal node represents a constant Boolean function either 1 or 0. • For a non-terminal node n labeled with z representing a Boolean • function fn, its two successor nodes represent Boolean functions: • Node at the end of the 0-edge (low(n)): fn z=0. | • Node at the end of the 1-edge (high(n)): fn z=1. | Therefore, fn = z fn z=0 z fn z=1 ¬ ∧ | ∨ ∧ | A OBDD is reduced (i.e. ROBDD) if for every pair (v, w) of nodes, •

v = w implies fv = fw. 6 6

Hao Zheng (CSE, USF) Comp Sys Verification 15 / 44 Universality and Canonicity Theorem

[Fortune, Hopcroft & Schmidt, 1978]

Let ~x be a finite of Boolean variables and ℘ a variable ordering for ~x. (a) For each switching function f for ~x there existsa ℘-ROBDD OBDD with fOBDD = f. (b) For any ℘-ROBDDs G and H with fG = fH , G and H are isomorphic, i.e., agree up to renaming of the nodes. • ROBDDs are canonical for a fixed variable ordering.

Hao Zheng (CSE, USF) Comp Sys Verification 16 / 44 The Importance of Canonicity

Absence of redundant vertices: • If fB does not depend on xi, ROBDD B does not contain an xi node. Test for equivalence: f(x1, . . . , xn) g(x1, . . . , xn)? • ≡ Generate ROBDDs Bf and Bg, and check isomorphism. Test for validity: f(x , . . . , x ) = 1? • 1 n Generate ROBDD Bf and check whether it only consists of a 1-leaf. Test for implication: f(x1, . . . , xn) g(x1, . . . , xn)? • → Generate ROBDD Bf ¬g and check if it just consists of a 0-leaf. ∧ Test for satisfiability: • f is satisfiable if and only if Bf has a reachable 1-leaf.

Hao Zheng (CSE, USF) Comp Sys Verification 17 / 44 Contents

1 Binary Decision Tree

2 Ordered Binary Decision Diagram

3 From BDT to BDDs

4 Variable Ordering

5 Logic Operations by BDDs

Hao Zheng (CSE, USF) Comp Sys Verification 18 / 44 Reducing OBDDs

Generate an OBDD (or BDT) for a boolean expression, then reduce by • means of a recursive descent over the OBDD. Elimination rule: • If low(v) = high(v) = w, eliminate v and redirect all incoming edges to v to node w. Isomorphism rule: • • If v = w are roots of isomorphic subtrees, remove v, and redirect all incoming6 edges to v to node w. • (Special case) Combine all 0/1-leaves, redirect all incoming edges.

Hao Zheng (CSE, USF) Comp Sys Verification 19 / 44 From BDT toTransition ROBDD Relation as a BDT

x1

x2 x2

x10 x10 x10 x10

x x x x20 x20 x20 x20 x20 20 20 20

0 1 0 1 0 11001111110

0 0 An OBDD representing ∆ for our example using ordering x1 < x2 < x1 < x2. A BDT representing for our example using ordering x1 < x2 < x10 < x20 .

Hao Zheng (CSE, USF) Comp Sys Verification 20 / 44

Chris J. Myers (Lecture 6: CTL) Verification of Cyber-Physical Systems 110 / 155 From BDTTransition to ROBDD Relation as a OBDD

x1

x2 x2

x10 x10 x10 x10

x x x x20 x20 x20 x20 x20 20 20 20

0 1 0 11000 11 1 0 11

After isomorphism rule Next,Isomorphism elimination rule. rule

Hao Zheng (CSE, USF) Comp Sys Verification 21 / 44

Chris J. Myers (Lecture 6: CTL) Verification of Cyber-Physical Systems 127 / 155 From BDTTransition to ROBDD Relation as a OBDD

x1

x2 x2

x10 x10 x10 x10

x x20 x20 x20 x20 x20 20

0 1 0 1 0 1 110 0 1 1 0 1

Next, isomorphism rule

Elimination rule.

Hao Zheng (CSE, USF) Comp Sys Verification 22 / 44

Chris J. Myers (Lecture 6: CTL) Verification of Cyber-Physical Systems 128 / 155 From BDTTransition to ROBDD Relation as a OBDD

x1

x2 x2

x10 x10 x10 x10

x20 x20 x20

0 1 110 1 0 1

Next, elimination rule Isomorphism rule.

Hao Zheng (CSE, USF) Comp Sys Verification 23 / 44

Chris J. Myers (Lecture 6: CTL) Verification of Cyber-Physical Systems 129 / 155 Transition Relation as a OBDD From BDT to ROBDD

x1

x2 x2

x10 x10 x10

x20 x20 x20

0 1 110 1 0 1

Next, isomorphism rule Elimination rule.

Hao Zheng (CSE, USF) Comp Sys Verification 24 / 44

Chris J. Myers (Lecture 6: CTL) Verification of Cyber-Physical Systems 130 / 155 From BDTTransition to ROBDD Relation as a OBDD

x1

x2 x2

x10 x10 x10

x20 x20

0 1 110 1

Next, isomorphism rule Isomorphism rule.

Hao Zheng (CSE, USF) Comp Sys Verification 25 / 44

Chris J. Myers (Lecture 6: CTL) Verification of Cyber-Physical Systems 131 / 155 From BDT to ROBDD Transition Relation as an ROBDD

x1

x2 x2

x10 x10 x10

x20 x20

0 1

Isomorphism rule. Final reduced BDD Chris J. Myers (Lecture 6: CTL) Verification of Cyber-Physical Systems 132 / 155

Hao Zheng (CSE, USF) Comp Sys Verification 26 / 44 Contents

1 Binary Decision Tree

2 Ordered Binary Decision Diagram

3 From BDT to BDDs

4 Variable Ordering

5 Logic Operations by BDDs

Hao Zheng (CSE, USF) Comp Sys Verification 27 / 44 Variable Ordering Variable Ordering

x1 x1

x2 x2 x10 x10

x10 x10 x10 x2

x20 x20 x20 x20

0 1 0 1

(a) ordering x1 < x2 < x10 < x20 (b) ordering x1 <0 x10 <0 x2 <0 x20

Hao Zheng (CSE, USF) Comp Sys Verification 28 / 44

Chris J. Myers (Lecture 6: CTL) Verification of Cyber-Physical Systems 133 / 155 Variable Ordering and Size of BDDs

The size of the ROBDD crucially depends on the variable ordering. • # nodes in ROBDD OBDD = # of ℘-consistent co-factors of f. • Some switching functions have linear and exponential ROBDDs. • e.g., the addition function, or the stable function. Some switching functions only have polynomial ROBDDs. • • This holds, e.g., for symmetric functions. • Examples f(...) = x1 ... xn, or f(...) = 1 iff k variables xi are true. ⊕ ⊕ ≥ Some switching functions only have exponential ROBDDs. • This holds, e.g., for the middle bit of the multiplication function.

Hao Zheng (CSE, USF) Comp Sys Verification 29 / 44 The Function Stable with Exponential ROBDD The Function Stable with Exponential ROBDD

x1

x2 x2

x3 x3 x3 x3

y1 y1 y1 y1 y1 y1 y1 y1

y2 y2 y2 y2

y3 y3

1

The ROBDD of f (x,y)=(x y ) ... (x y ) stab 1 $ 1 ^ ^ n $ n Thehas ROBDD3 2n 1 ofverticesfstab ( underx, y) ordering = (x1 x <...

Hao Zheng (CSE, USF) Comp Sys Verification 30 / 44 The Function Stable with Linear ROBDD The Function Stable with Linear ROBDD

x1

y1 y1

x2

y2 y2

x3

y3 y3

1

The ROBDD of f (x,y)=(x y ) ... (x y ) stab 1 $ 1 ^ ^ n $ n The ROBDDhas 3 n + of2fverticesstab (x, undery) = ordering (x1 x1y<1)y1 <...<... xn <(yxnn. yn) · ↔ ∧ ∧ ↔ Chris J. Myers (Lecture 6: CTL) Verification of Cyber-Physical Systems 136 / 155 has 3 n + 2 vertices under ordering x1

Hao Zheng (CSE, USF) Comp Sys Verification 31 / 44 Another Function with an Exponential ROBDD Another Function with an Exponential ROBDD

z1

z2 z2

z3 z3 z3 z3

y1 y1 y1 y1

y2 y2

y3

0 1

ROBDD for f (z,y)=(z y ) (z y ) (z y ) 3 1 ^ 1 _ 2 ^ 2 _ 3 ^ 3 ROBDD for forf3 the(~z, variable~y) = ordering (z1 z1

Hao Zheng (CSE, USF) Comp Sys Verification 32 / 44 An Optimal Linear ROBDD An Optimal Linear ROBDD

z1

y1

z2 ROBDDROBDD for forf ( )=(f3( )z =y (z)1 (zy1)y )(z(2z yy2)). (z3 y3). • 3 · · 1 ^ 1 _∧ 2 ^ ∨2 _ 3∧^ 3 ∨ ∧ ForFor ordering orderingz1

1 0

Chris J. Myers (Lecture 6: CTL) Verification of Cyber-Physical Systems 138 / 155

Hao Zheng (CSE, USF) Comp Sys Verification 33 / 44 The Multiplication Function

Consider two n-bit integers: • Let bn−1bn−2 . . . b0 and cn−1cn−2 . . . c0 where bn−1 is the most significant bit, and b0 the least significant bit. Multiplication yields a 2n-bit integer: • n The ROBDD OBDDfn−1 has at least 1.09 vertices where fn−1 denotes the (n 1)-st output bit of the multiplication. −

Hao Zheng (CSE, USF) Comp Sys Verification 34 / 44 Optimal Variable Ordering

The size of ROBDDs is dependent on the variable ordering. • Is it possible to determine ℘ such that the ROBDD has minimal size? • • To check whether a variable ordering is optimal is NP-hard. • Polynomial reduction from the 3SAT problem. [Bollig & Wegener, 1996] There are many switching functions with large ROBDDs: • 2n For almost all switching functions the minimal size is in Ω( n ). How to deal with this problem in practice? • • Guess a variable ordering in advance. • Rearrange the variable ordering during the ROBDD manipulations. • Not necessary to test all n! orderings, best known algorithm in (3n n2). O ·

Hao Zheng (CSE, USF) Comp Sys Verification 35 / 44 Dynamic Re-ordering

Finding an optimal ordering is NP-hard. • Static ordering does not work well across different applications, or for • BDDs that need to be transformed during different stages of an application. Automated dynamic re-ordering rearranges the variable orders • periodically to reduce the size of BDDs. Rudell’s “sifting” is widely used. • • Try moving a variable to all other positions, leaving the others fixed. Then place variable in the position that minimizes BDD size. • Do this for all variables.

Hao Zheng (CSE, USF) Comp Sys Verification 36 / 44 Dynamic Re-ordering

Greatly improved effectiveness of BDDs. • It is usually performed in the background. • It may slow down the performance. • • BDD operations stop when re-ordering is activated. It makes a difference between success and failure in complete an • application. Some functions are inherently hard, e.g. outputs of integer multiplier. •

Hao Zheng (CSE, USF) Comp Sys Verification 37 / 44 Contents

1 Binary Decision Tree

2 Ordered Binary Decision Diagram

3 From BDT to BDDs

4 Variable Ordering

5 Logic Operations by BDDs

Hao Zheng (CSE, USF) Comp Sys Verification 38 / 44 Logic Operations on BDDs

Restriction f[b/x]: replacing variable x with a value 0 or 1. • • if b = 0, direct all incoming edges of node labeled with x to low(v), or • if b = 1, direct all incoming edges of node labeled with x to high(v), • remove node x and its outgoing edges. The result of restriction is a cofactor of f. •

a a

b

c c

0 1 0 1

f f[1/b]

Hao Zheng (CSE, USF) Comp Sys Verification 39 / 44 Logical Operations on BDDs (cont’d)

Negation is a constant time operation with OBDDs. • • Swap terminal nodes. The binary operations are based on the Shannon expansion. •

f g = x (f[0/x] g[0/x]) x (f[1/x] g[1/x]). ◦ · ◦ ∨ · ◦ where is some binary logic operator. ◦ Both BDDs must have the same variable ordering. • The new BDD for f g is constructed as follows. • ◦ • The root of f g is the root of f or g with the smaller index. • For any node in◦ f g, ◦ low(v) = f[0/x] g[0/x], and high(v) = f[1/x] g[1/x] ◦ ◦ • Repeat the above step for low(v) and high(v) until either of them becomes terminal. • Reduce the constructed BDD to make it canonical.

Hao Zheng (CSE, USF) Comp Sys Verification 40 / 44 Logical Operations on BDDs — Example

f = (a c) f g ¬ ^ _ a a

g = b c f a=1 g ^ | _ b b

f f a=1 g b=0 f a=1 g b=1 |a=1 | _ | | _ | c c c c

1 0 0 1 1 0

Hao Zheng (CSE, USF) Comp Sys Verification 41 / 44 Logical Operations on BDDs — Example

f g f g _ _ a a

f a=1 g f g | _ |a=1 _ b b

f a=1 g b=0 f a=1 g b=1 f g | _ | | _ | |a=1 _ |b=0 c c c

1 0 1 0

Hao Zheng (CSE, USF) Comp Sys Verification 42 / 44 Variants of BDDs

Various kinds of BDDs for compactness or different applications. • Compactness • • Multi-rooted BDDs, free BDDs, partitioned OBDDs, etc Arithmetic operations • • Multi-terminal BDDs (ADDs), edge-valued BDDs, binary moment diagrams (BMDs), etc.

Hao Zheng (CSE, USF) Comp Sys Verification 43 / 44 Implementation: Shared OBDDs

A shared ℘-OBDD is an OBDD with multiple roots.

0 1

Shared OBDD representing z1 z2, z2 , z1 z2 and z1 z2. ∧ ¬ ¬ ⊕ ¬ ∨ f1 f2 f3 f4 | {z } |{z} | {z } | {z } Main underlying idea: combine several OBDDs with same variable ordering such that common ℘-consistent co-factors are shared.

Hao Zheng (CSE, USF) Comp Sys Verification 44 / 44