bdd - University of Cincinnatigauss.ececs.uc.edu/Courses/c626/lectures/BDD/bdd.pdf · Title:...

Post on 08-Oct-2020

0 views 0 download

transcript

Binary Decision Diagrams��

��v0

��

��v1

��

��v2

��

��v3

��

��v4

T F

��

��

���/

?

��

���+

?

?

SSSw

SSSw

��

���+ ?

BBBBBBBN

1 0

1

01

0

0

0

1

1

{{v0, v1, v4}, {v0, v1, v2, v3, v4}, {v0, v1, v2, v3}, {v0, v2, v3, v4}, {v1, v2, v3}}

Rooted binary directed acyclic graphTwo leaves labeled T and FOther nodes are labeled with variable namesEdges are labeled 0 or 1Out of each non-leaf, one edge is labeled 0

and one edge is labeled 1There is an order on the variables of the BDD

and variables on any path obey itA single BDD compactly represents a Boolean

function

Binary Decision Diagrams��

��v0

��

��v1

��

��v2

��

��v3

��

��v4

T F

��

��

���/

?

��

���+

?

?

SSSw

SSSw

��

���+ ?

BBBBBBBN

1 0

1

01

0

0

0

1

1

{{v0, v1, v4}, {v0, v1, v2, v3, v4}, {v0, v1, v2, v3}, {v0, v2, v3, v4}, {v1, v2, v3}}

v0 v1 v2 v3 v4 f

0 0 0 0 0 0

0 0 0 0 1 1

0 0 0 1 0 0

0 0 0 1 1 1

0 0 1 0 0 0

0 0 1 0 1 1

0 0 1 1 0 0

0 0 1 1 1 1

0 1 0 0 0 1

0 1 0 0 1 1

0 1 0 1 0 1

0 1 0 1 1 1

0 1 1 0 0 0

0 1 1 0 1 1

0 1 1 1 0 0

0 1 1 1 1 0

1 0 0 0 0 1

1 0 0 0 1 1

1 0 0 1 0 1

1 0 0 1 1 1

1 0 1 0 0 0

1 0 1 0 1 1

1 0 1 1 0 0

1 0 1 1 1 0

1 1 0 0 0 1

1 1 0 0 1 1

1 1 0 1 0 1

1 1 0 1 1 1

1 1 1 0 0 0

1 1 1 0 1 1

1 1 1 1 0 0

1 1 1 1 1 0

Binary Decision Diagrams��

��v0

��

��v1

��

��v2

��

��v3

��

��v4

T F

��

��

���/

?

��

���+

?

?

SSSw

SSSw

��

���+ ?

BBBBBBBN

1 0

1

01

0

0

0

1

1

{{v0, v1, v4}, {v0, v1, v2, v3, v4}, {v0, v1, v2, v3}, {v0, v2, v3, v4}, {v0, v2, v3}}

Rooted binary directed acyclic graphTwo leaves labeled T and FOther nodes are labeled with variable namesEdges are labeled 0 or 1Out of each non-leaf, one edge is labeled 0

and one edge is labeled 1There is an order on the variables of the BDD

and variables on any path obey itA single BDD compactly represents a Boolean

functionEvery path to F represents a clause, anding

them gives the function

Binary Decision Diagrams

{{v0, v1, v2, v3, v4}, {v0, v1, v2, v3, v4}, . . . , {v0, v1, v2, v3, v4}, . . . , {v0, v1, v2, v3, v4}}

Binary Decision Diagrams��

��v0

��

��v1

��

��v2

��

��v3

��

��v4

T F

��

��

���/

?

��

���+

?

?

SSSw

SSSw

��

���+ ?

BBBBBBBN

1 0

1

01

0

0

0

1

1

ite(v0,

ite(v2,

ite(v3,F,ite(v4,T,F))

T)

ite(v1,

ite(v2,

ite(v3,F,ite(v4,T,0))

T)

ite(v4,T,F)))

order(v4, v3, v2, v1, v0)

ite(v4,T,F) ; BDD $1 Created

ite(v3,F,$1) ; BDD $2 Created

ite(v2,$2,T) ; bdd $3 created

ite(v1,$3,$1) ; bdd $4 created

ite(v0,$3,$4) ; bdd $5 created

print($5)

Binary Decision Diagrams

Via the BDD-visualizer - ordering affects size

Binary Decision Diagrams

order(v4, v3, v2, v1, v0)

ite(v4,T,F) ; BDD $1 Created

ite(v3,F,T) ; BDD $2 Created

ite(v2,$2,T) ; bdd $3 created

ite(v1,$2,$1) ; bdd $4 created

print($3,$4)

findOrCreateNode(v,t,e) {if (t == e) return t;

if ((node = lookup(<v,t,e>)) != null)

return node;

node = createNode(<v,t,e>);

insertNodeDataBase(<v,t,e>,node)

return node;

}

Hashtable lookup on nodes - nodes shared across BDDs

Binary Decision Diagrams

order(v4, v3, v2, v1, v0)

ite(v4,T,F) ; BDD $1 Created

ite(v3,F,T) ; BDD $2 Created

ite(v2,$2,T) ; bdd $3 created

ite(v1,$2,$1) ; bdd $4 created

and(3,4) ; bdd $5 created

print($5)

All interesting applications conjoin BDDs

Binary Decision Diagrams

Conjoining is a bottom up process - complexity linear in # nodes

Binary Decision Diagrams

Conjoining is a bottom up process - complexity linear in # nodes

Binary Decision Diagrams

Conjoining is a bottom up process - complexity linear in # nodes

Binary Decision Diagrams

Conjoining is a bottom up process - complexity linear in # nodesbut the number of nodes can double - hence 2n nodes may resultfrom n binary conjunctions.

Binary Decision Diagrams

Conjoining is a bottom up process - complexity linear in # nodesbut the number of nodes can double - hence 2n nodes may resultfrom n binary conjunctions. But more conjunctions may reducethe size of the BDD - here not(or(v2,v4)) is added.

Binary Decision Diagrams

Conjoining is a bottom up process - complexity linear in # nodesbut the number of nodes can double - hence 2n nodes may resultfrom n binary conjunctions. But more conjunctions may reducethe size of the BDD - here not(or(v2,v4)) is added. Then or(v2,v3)

Binary Decision Diagrams

Can something be done to prevent an exponential blowup?Existential quantification:

A Boolean function which can be written

f(v, ~x) = (v ∧ h1(~x)) ∨ (¬v ∧ h2(~x))

can be replaced by

f(~x) = h1(~x) ∨ h2(~x)

where ~x is a list of one or more variables.

There is a solution to f(~x) if and only if there is a solution tof(v, ~x) so it is sufficient to solve f(~x) to get a solution to f(v, ~x).

- a variable is eliminated!- this is natural for BDDs

Binary Decision Diagrams

exQuant (f, v)/* Input: BDD f , variable v *//* Output: BDD f with v existentially quantified away */

If root(f) == v then Return then(root(f))∨else(root(f)).If index(v) > index(root(f)) then Return 0. // If v is not in f do nothingSet hf1

← exQuant(then(root(f)), v).Let hf0

← exQuant(else(root(f)), v).If hf0

== hf1then Return hf1

.Return findOrCreateNode(root(f), hf1

, hf0).

where index(vi) == i.restriction: v must only be in f .

Binary Decision Diagrams��

� v1

��

� v2

��

� v2

��

� v3

��

� v3

F T

T⇒v3

��/

SSw

��/

QQ

QQs

�������

��/

@@R

���������)

��

��+

0 1

01 1

0

0 1 10

��

� v1

��

� v2

��

� v3

F T

��

� v1

T F

⇒v2

AAAU

����

?

HHHHj

?

��

�� @@R

01

0

11 0

1 0

Existentially quantifying away a variable can win big

infer v1 = T

eliminate BDD

Binary Decision Diagrams

0

v8

T

1 0

v7

1

v5

1 0

1

v4

1 0

0

v6

T F T F

v2

1 0

1

v1

1 0

0

v3

T F T F

But it can blur functional relationships -This BDD separates v1. v2, v3 from v4, v5, v6 if v7 is chosen during searchWithout ExQu, v8 = 0, v7 = 1, v6 = 1 reveals the inference v5 = 1.

Binary Decision Diagrams

1 0

T

v8

v6

1 0

v5

T

v2

1 0

1

v1

1 0

0

v3

T F T F

1 0

v4

T

v2

1 0

1

v1

1 0

0

v3

T F T F

1 0

But it can blur functional relationships -With ExQu must also have v2 = 0 and v1 = 0 to get the same inference.

Binary Decision DiagramsConsider the truth tables for BDDs f and c.

Build a new BDD g over variables in f and c.

On row of c’s truth table that has value 1,let the corresponding row in g map to the same value as f map to.

On other rows g maps to any value, independent of f .

Observe f ∧ c and g ∧ c are identical,so g can replace f in a collection of BDDs.

BDD g is said to be a reduction - there are several types

1. BDD g can be made smaller than f .

2. Inferences can be discovered.

3. BDDs can be removed from the collection without loss.

Binary Decision DiagramsObvious reduction: g maps to 0 all rows that c maps to 0.

Called zero-restrict, has weaknesses - for example

c = (v1 ∨ ¬v2) ∧ (¬v1 ∨ ¬v3)

f = (v2 ∨ ¬v3)

will yield

g = ¬v3 ∧ (v1 ∨ (¬v1 ∧ ¬v2))

instead of the possible

g = (¬v3)

Obvious dual: g maps to 1 all rows that c maps to 0

No better.

Desired: some rows of g to map to 1, others to 0 so that

g’s truth table reflects a logic pattern that generates inferences.

g maps 〈011〉 and 〈111〉 to 0 and 〈010〉 and 〈101〉 to 1 to get ¬v3.

Binary Decision Diagrams

Example:

f = (v1 ∨ ¬v2 ∨ v3) ∧ (¬v1 ∨ v2) and

c = (v1 ∨ ¬v2)

Let g have the same truth table as f except for row 〈011〉 which c maps

to 0 and g maps to 1.

Then g = (v1 ↔ v2) and f ∧ c is the same as g ∧ c but g is smaller than f .

Binary Decision Diagrams

The two BDDs on the right replace the two on the left

Binary Decision Diagrams

Use the symbol ⇓ to denote the restrict operator.

Then g = f ⇓ c or restrict(f ,c) is the result of

zero-restrict after all variables in c that are not in f

are existentially quantified away from c.

Binary Decision Diagrams

restrict (f, c)/* Input: BDD f , BDD c *//* Output: BDD f restricted by c */

If c or f is terminal(T ) or if f is terminal(F ) return f .If c == ¬f return terminal(F ).If c == f return terminal(T ).// f and c have a non-trivial relationshipSet vf ← root(f). // vf is a variableSet vc ← root(c). // vc is a variableIf index(vf ) > index(vc) return restrict(f , exQuant(c, vc)).If reduce0(vf , c) is terminal(F ) then

Return restrict(reduce1(vf , f), reduce1(vf , c)).If reduce1(vf , c) is terminal(F ) then

Return restrict(reduce0(vf , f), reduce0(vf , c)).Set hf1

← restrict(reduce1(vf , f), reduce1(vf , c)).Set hf0

← restrict(reduce0(vf , f), reduce0(vf , c)).If hf1

== hf0then Return hf1

.Return findOrCreateNode(vf , hf1

, hf0).

Binary Decision Diagrams

Example:

f = (v1 → v2) ∧ (¬v1 → (¬v3 ∧ v4)) and

c = (v1 ∨ v3).

Let g have the same truth table as f except g maps rows

〈0001〉 and 〈0101〉 to 0, as does c.

Then g = (v1) ∧ (v2), and two inferences are revealed.

Binary Decision Diagrams

��

� v1

��

� v3

��

� v2

��

� v4

T F

���/

SSSw

���/

?

��

��

��

���+

��

�/@

@R

0 1

10

01

1 0

��

� v1

��

� v3

T F?

SSSw

��/

SSw

1

0

1 0⇓

f = (v1 → v2) ∧ (¬v1 → (¬v3 ∧ v4))

c = (v1 ∨ v3) g = (v1) ∧ (v2)

��

� v1

��

� v2

T F?

���/

��/

SSw

1

01 0

A call to restrict(f ,c) returns g and two inferences

Binary Decision Diagrams

��

� v1

��

� v3

��

� v2

T F

���/

SSSw

?Q

QQQs ?

��

��+

1 0

01 0

1

��

� v2

��

� v3

T F

?

SSSw

���/

SSSw

1

0

0 1⇓

f = (v1 ∨ ¬v2) ∧ (¬v1 ∨ ¬v3) c = (v2 ∨ ¬v3)

��

� v1

��

� v3

��

� v2

T F

���/

SSSw

?Q

QQQs ?

��

��+

1 0

01 0

1

A call to restrict(f, c) results in no change.

Binary Decision Diagrams

��

� v1

��

� v3

��

� v2

T F

���/

SSSw

?Q

QQQs ?

��

��+

1 0

01 0

1

��

� v2

��

� v3

T F

?

SSSw

���/

SSSw

1

0

0 1 ⇓

c = (v1 ∨ ¬v2) ∧ (¬v1 ∨ ¬v3)f = (v2 ∨ ¬v3) g = (¬v3)

��

� v3

T F

���/

SSSw

0 1

But reversing the roles of f and c results in the inference g = ¬v3

Binary Decision Diagrams

��

� v1

��

� v3

��

� v2

T F

SSSw

���/

ZZ

ZZ~?

��

��=?

01

10 0

1

��

� v1

��

� v3

T F

?

SSSw

���/

SSSw

0

1

1 0⇓

f = (v1 ∨ ¬v2) ∧ (¬v1 ∨ v3) c = (¬v1 ∨ v3) g = (v1 ∨ ¬v2)

��

� v1

��

� v2

F T

?

���/

���/

SSSw

0

11 0

restrict(f, c) spreads an inference evident in one BDD over multiple BDDs.

If v3 is assigned 0 in f then v1 = 0 and v2 = 0 are inferred.

After replacing f with g = restrict(f, c), to get the inference v2 = 0 from

the choice v3 = 0, visit c to get v1 = 0 and then g to get v2 = 0.

Thus, restrict can increase work if not used properly. In this case,

restricting in the reverse direction leads to a better result.

Binary Decision Diagrams

BDD g is a generalized co-factor of f and c if for any truth assignment t,

g(t) has the same value as f(t′) where t′ is the “nearest” truth

assignment to t that maps c to 1.

The notion of “nearest” truth assignment depends on a permutation π of

the numbers 1, 2, . . . , n which states the variable ordering of the input BDDs.

Represent a truth assignment to n variables as a vector in {0, 1}n

for assignment t, let ti denote the ith bit of the vector representing t.

Then the distance between two truth assignments t′ and t′′ is defined as∑n

i=12n−i(t′πi

⊕ t′′πi).

One pair of assignments is nearer to each other than another pair if

the distance between that pair is less.

Distances between pairs are unique for each pair.

Binary Decision Diagrams

gcf (f, c)/* Input: BDD f , BDD c *//* Output: greatest co-factor of f by c */

If f == terminal(F ) or c == terminal(F ) return terminal(F ).If c == terminal(T ) or f == terminal(T ) return f .Set vm ← index−1(min{index(root(c)), index(root(f))}).// vm is the top variable of f and c

If reduce0(vm, c) == terminal(F ) thenReturn gcf(reduce1(vm, f), reduce1(vm, c)).

If reduce1(vm, c) == terminal(F ) thenReturn gcf(reduce0(vc, f), reduce0(vc, c)).

Set h1 ← gcf(reduce1(vm, f), reduce1(vm, c)).Set h0 ← gcf(reduce0(vm, f), reduce0(vm, c)).If h1 == h0 then Return h1.Return FindOrCreateNode(vm, h1, h0).

Binary Decision Diagrams

�v1

�v2

�v3

T F

��/

SSw

? ?Q

QQQs

��

��+

1 0

01 1

0

�v2

�v3

T F?

SSw

SSw

��/

10

1 0

�v1

�v2

�v2

�v3

F T

��/

SSw

?

JJ

JJJ ?

SSw

��/

������

1 0

10

01

0

1

f = (v1 → ¬v2) ∨ (¬v1 → v3) c = (v2 ∨ v3)

gcf(f, c) = (v1 → ¬v2) ∨ (¬v1 → (v2 → v3))

With variable ordering is v1 < v2 < v3, gcf(f ,c) is more complicated

Binary Decision Diagrams

�v1

�v2

�v2

�v3

�v3

T F

��/

SSw

? ?

SSw

��/

��/

������SSw

HHHHHj

1 0

01

10

0

1

1

0

�v1

�v2

T F?

SSw

SSw

��/

10

1 0

�v1

�v2

�v3

F T

QQ

QQs

? ?

��/

��/

SSw

0

1

1

01 0

f = (v3 → ¬v2) ∨ (¬v3 → v1)

c = (v1 ∨ v2)

gcf(f, c) = (v1 ∧ (v2 → ¬v3))

Same variable ordering, v1 and v3 are swapped

Binary Decision Diagrams

Theorem:

Given BDDs f1, ..., fk, for any 1 ≤ i ≤ k, f1 ∧ f2 ∧ ... ∧ fk is satisfiable

if and only if (f1|fi) ∧ ... ∧ (fi−1|fi) ∧ (fi+1|fi) ∧ ... ∧ (fk|fi) is satisfiable.

Moreover, any assignment satisfying the latter can be mapped to an

assignment that satisfies f1 ∧ ... ∧ fk.

So generalized co-factoring can be used to eliminate one of the BDDs

among a given conjoined set of BDDs: the solver finds an assignment

satisfying gcf(f1, fi) ∧ ... ∧ gcf(fk, fi) and then extends the assignment

to satisfy fi, otherwise the solver reports that the instance has no solution.

Binary Decision Diagrams

Other properties:

1. f = c ∧ gcf(f, c) ∨ ¬c ∧ gcf(f,¬c).

2. gcf(gcf(f, g), c) = gcf(f, g ∧ c).

3. gcf(f ∧ g, c) = gcf(f, c)∧ gcf(g, c).

4. gcf(f ∧ c, c) = gcf(f, c).

5. gcf(f ∧ g, c) = gcf(f, c)∧ gcf(g, c).

6. gcf(f ∨ g, c) = gcf(f, c)∨ gcf(g, c).

7. gcf(f ∨ ¬c, c) = gcf(f, c).

8. gcf(¬f, c) = ¬ gcf(f, c).

9. If c and f have no variables in common and c is satisfiablethen gcf(f, c) = f .