<<

An Intro duction to

Binary Decision Diagrams

Henrik Reif Andersen

x

y

y z

1 0

Lecture notes for Advanced Algorithms E Octob er

Minor revisions Apr

Email hraitdtudk Web httpwwwitdtudkhra

Department of Information Technology Technical University of Denmark

Building DK Lyngby Denmark

Preface

This note is a short intro duction to Binary Decision Diagrams It provides some back

ground knowledge and describ es the core algorithms More details can b e found in

Bryants original pap er on Reduced Ordered Binary Decision Diagrams Bry and the

survey pap er Bry A recent extension called Bo olean Expression Diagrams is describ ed

in AH

This note is a revision of an earlier version from fall based on versions from

and The ma jor dierences are as follows Firstly ROBDDs are now viewed

as no des of one global graph with one xed ordering to reect stateoftheart of ecient

BDD packages The algorithms have b een changed and simplied to reect this fact

Secondly a pro of of the canonicity lemma has b een added Thirdly the sections presenting

the algorithms have b een completely restructured Finally the pro ject prop osal has b een

revised

Acknowledgements

Thanks to the students on the courses of fall and for helping me debug

and improve the notes Thanks are also due to Hans Rischel Morten Ulrik Srensen

Niels Maretti Jrgen Staunstrup Kim Skak Larsen Henrik Hulgaard and various p eople

on the Internet who found typ os and suggested improvements

CONTENTS

Contents

Bo olean Expressions

Normal Forms

Binary Decision Diagrams

Constructing and Manipulating ROBDDs

Mk

Build

Apply

Restrict

SatCount AnySat AllSat

Simplify

Existential Quantication and Substitution

Implementing the ROBDD op erations

Examples of problem solving with ROBDDs

The Queens problem

Correctness of Combinational Circuits

Equivalence of Combinational Circuits

Verication with ROBDDs

Knights tour

Pro ject An ROBDD Package

References

CONTENTS

BOOLEAN EXPRESSIONS

Bo olean Expressions

The classical calculus for dealing with truth values consists of Boolean variables x y

the constants true and false the op erators of conjunction disjunction

implication and biimplication which together form the Bo olean expressions

Sometimes the variables are called propositional variables or propositional letters and the

Bo olean expressions are then known as Propositional Logic

Formally Bo olean expressions are generated from the following grammar

t x j j j t j t t j t t j t t j t t

where x ranges over a of Bo olean variables This is called the abstract syntax of Bo olean

expressions The concrete syntax includes parentheses to solve ambiguities Moreover as

a common convention it is assumed that the op erators bind according to their relative

priority The priorities are with the highest rst Hence for example

x x x x x x x x

1 2 3 4 1 2 3 4

A Bo olean expression with variables x x denotes for each assignment of truth values

1 n

to the variables itself a truth value according to the standard truth tables see gure

Truth assignments are written as sequences of assignments of values to variables eg

x x x x which assigns to x and x to x and x With this particular

1 2 3 4 1 3 2 4

truth assignment the ab ove expression has value whereas x x x x yields

1 2 3 4

Figure Truth tables

The set of truth values is often denoted B f g If we x an ordering of the

n

variables of a Bo olean expression t we can view t as dening a function from B to B

where n is the numb er of variables Notice that the particular ordering chosen for the

variables is essential for what function is dened Consider for example the expression

x y If we cho ose the ordering x y then this is the function f x y x y true if

the rst argument implies the second but if we cho ose the ordering y x then it is the

function f y x x y true if the second argument implies the rst When we later

consider compact representations of Bo olean expressions such variable orderings play a

crucial role

Two Bo olean expressions t and t are said to b e equal if they yield the same truth

value for all truth assignments A Bo olean expression is a if it yields true for

all truth assignments it is satisable if it yields true for at least one truth assignment

Exercise Show how all op erators can b e enco ded using only and Use this

to argue that any Bo olean expression can b e written using only variables and

applied to variables

NORMAL FORMS

Exercise Argue that t and t are equal if and only if t t is a tautology Is it

p ossible to say whether t is satisable from the fact that t is a tautology

Normal Forms

A Bo olean expression is in Disjunctive Normal Form DNF if it consists of a disjunction

of conjunctions of variables and of variables ie if it is of the form

1 1 1 l l

t t t t t t

1 2 k 1 2 k

l

j j j

where each t is either a variable x or a negation of a variable x An example is

i i i

x y x y

which is a wellknown function of x and y which one A more succinct presentation of

is to write it using indexed versions of and

k

j

l

j

A

t

i

j =1 i=1

Similarly a Conjunctive Normal Form CNF is an expression that can b e written as

k

j

l

j

A

t

i

j =1 i=1

j

where each t is either a variable or a negated variable It is not dicult to prove the

i

following prop osition

Prop osition Any Boolean expression is equal to an expression in CNF and an expres

sion in DNF

In general it is hard to determine whether a Bo olean expression is satisable This is

made precise by a famous theorem due to Co ok Co o

Theorem Co ok Satisability of Boolean expressions is NPcomplete

For readers unfamiliar with the notion of NPcompleteness the following short summary

of the pragmatic consequences suces Problems that are NPcomplete can b e solved

by algorithms that run in exp onential time No p olynomial time algorithms are known

to exist for any of the NPcomplete problems and it is very unlikely that p olynomial

time algorithms should indeed exist although nob o dy has yet b een able to prove their

nonexistence

Co oks theorem even holds when restricted to expressions in CNF For DNFs satis

ability is decidable in p olynomial time but for DNFs the tautology check is hard coNP

complete Although satisability is easy for DNFs and tautology check easy for CNFs

BINARY DECISION DIAGRAMS

this do es not help us since the conversion b etween CNFs and DNFs is exp onential as the

following example shows

1 n 1 n

Consider the following CNF over the variables x x x x

0 0 1 1

n n 2 2 1 1

x x x x x x

1 0 1 0 1 0

The corresp onding DNF is a disjunction which has a disjunct for each of the ndigit binary

i

numb ers from to the ith digit representing a choice of either x

0

i

for or x for

1

n1

1 2 n

x x x x

0 0 0 0

n1

1 2 n

x x x x

0 0 0 1

n1

n 1 2

x x x x

1

0 1 1

n1

1 2 n

x x x x

1 1 1 1

Whereas the original expression has size prop ortional to n the DNF has size prop ortional

n

to n

The next section intro duces a normal form that has more desirable prop erties than

DNFs and CNFs In particular there are ecient algorithms for determining the satis

ability and tautology questions

Exercise Describ e a p olynomial time algorithm for determining whether a DNF is

satisable

Exercise Describ e a p olynomial time algorithm for determining whether a CNF is

a tautology

Exercise Give a pro of of prop osition

Exercise Explain how Co oks theorem implies that checking inequivalence b etween

Bo olean expressions is NPhard

Exercise Explain how the question of tautology and satisability can b e decided if

we are given an algorithm for checking equivalence b etween Bo olean expressions

Binary Decision Diagrams

Let x y y b e the ifthenelse op erator dened by

0 1

x y y x y x y

0 1 0 1

hence t t t is true if t and t are true or if t is false and t is true We call t the

0 1 0 1

test expression All op erators can easily b e expressed using only the ifthenelse op erator

and the constants and Moreover this can b e done in such a way that all tests are

p erformed only on unnegated variables and variables o ccur in no other places Hence

the op erator gives rise to a new kind of normal form For example x is x

x y is x y y Since variables must only o ccur in tests the Bo olean

expression x is represented as x

BINARY DECISION DIAGRAMS

An Ifthenelse Normal Form INF is a Bo olean expression built entirely

from the ifthenelse op erator and the constants and such that all tests are

p erformed only on variables

If we by tx denote the Bo olean expression obtained by replacing x with in t then

it is not hard to see that the following equivalence holds

t x tx tx

This is known as the Shannon expansion of t with resp ect to x This simple equation has

a lot of useful applications The rst is to generate an INF from any expression t If t

contains no variables it is either equivalent to or which is an INF Otherwise we form

the Shannon expansion of t with resp ect to one of the variables x in t Thus since tx

and tx b oth contain one less variable than t we can recursively nd INFs for b oth of

these call them t and t An INF for t is now simply

0 1

x t t

1 0

We have proved

Prop osition Any Boolean expression is equivalent to an expression in INF

Example Consider the Bo olean expression t x y x y If we nd an

1 1 2 2

INF of t by selecting in order the variables x y x y on which to p erform Shannon

1 1 2 2

expansions we get the expressions

t x t t

1 1 0

t y t

0 1 00

t y t

1 1 11

t x t t

00 2 001 000

t x t t

11 2 111 110

t y

000 2

t y

001 2

t y

110 2

t y

111 2

Figure shows the expression as a Such a tree is also called a decision tree 

A lot of the expressions are easily seen to b e identical so it is tempting to identify them

For example instead of t we can use t and instead of t we can use t If we

110 000 111 001

substitute t for t in the righthand side of t and also t for t we in fact see

000 110 11 001 111

that t and t are identical and in t we can replace t with t

00 11 1 11 00

If we in fact identify al l equal sub expressions we end up with what is known as a

binary decision diagram a BDD It is no longer a tree of Bo olean expressions but a

DAG

BINARY DECISION DIAGRAMS

x

1

y y

1 1

x x

2 2

y y y y

2 2 2 2

Figure A decision tree for x y x y Dashed lines denote lowbranches

1 1 2 2

solid lines highbranches

Applying this idea of sharing t can now b e written as

t x t t

1 1 0

t y t

0 1 00

t y t

1 1 00

t x t t

00 2 001 000

t y

000 2

t y

001 2

Each sub expression can b e viewed as the no de of a graph Such a no de is either terminal

in the case of the constants and or nonterminal A nonterminal no de has a lowedge

corresp onding to the elsepart and a highedge corresp onding to the thenpart See gure

Notice that the numb er of no des has decreased from in the decision tree to in

the BDD It is not hard to imagine that if each of the terminal no des were other big

decision trees the savings would b e dramatic Since we have chosen to consistently select

variables in the same order in the recursive calls during the construction of the INF of t

the variables o ccur in the same orderings on all paths from the ro ot of the BDD In this

situation the binary decision diagram is said to b e ordered an OBDD Figure shows a

BDD that is also an OBDD

Figure shows four OBDDs Some of the tests eg on x in b are redundant

2

since b oth the low and highbranch lead to the same no de Such unnecessary tests can

b e removed any reference to the redundant no de is simply replaced by a reference to

BINARY DECISION DIAGRAMS

x

y y

x

y y

Figure A BDD for x y x y with ordering x y x y Lowedges

1 1 2 2 1 1 2 2

are drawn as dotted lines and highedges as solid lines

x

x x

x

x

x

a b c d

Figure Four OBDDs a An OBDD for b Another OBDD for with two redundant

tests c Same as b with one of the redundant tests removed d An OBDD for x x

1 3

with one redundant test

BINARY DECISION DIAGRAMS

x

x y

x x

x

x z

y z

Figure The ordering and reducedness conditions of ROBDDs Left Variables must

b e ordered Middle No des must b e unique Right Only nonredundant tests should b e

present

its subno de If all identical no des are shared and all redundant tests are eliminated the

OBDD is said to b e reduced an ROBDD ROBDDs have some very convenient prop erties

centered around the canonicity lemma b elow Often when p eople sp eak ab out BDDs they

really mean ROBDDs To summarize

A Binary Decision Diagram BDD is a ro oted directed acyclic graph with

one or two terminal no des of outdegree zero lab eled or and

a set of variable no des u of outdegree two The two outgoing edges

are given by two functions low u and high u In pictures these

are shown as dotted and solid lines resp ectively A variable var u

is asso ciated with each variable no de

A BDD is Ordered OBDD if on all paths through the graph the variables

resp ect a given linear order x x x An OBDD is Reduced

1 2 n

ROBDD if

uniqueness no two distinct no des u and v have the same variable

name and low and highsuccessor ie

var u var v low u low v high u high v implies u v

and

nonredundant tests no variable no de u has identical low and

highsuccessor ie

low u high u

The ordering and reducedness conditions are shown in gure

ROBDDs have some interesting prop erties They provide compact representations of

Bo olean expressions and there are ecient algorithms for p erforming all kinds of logical

op erations on ROBDDs They are all based on the crucial fact that for any function

n

f B B there is exactly one ROBDD representing it This means in particular that

n

there is exactly one ROBDD for the constant true and constant false function on B

the terminal no de and in case of false Hence it is p ossible to test in constant time

whether an ROBDD is constantly true or false Recall that for Bo olean expressions this

problem is NPcomplete

BINARY DECISION DIAGRAMS

To make this claim more precise we must say what we mean for an ROBDD to represent

a function First it is quite easy to see how the no des u of an ROBDD inductively denes

u

Bo olean expressions t A terminal no de is a Bo olean constant A nonterminal no de

marked with x is an ifthenelse expression where the condition is x and the two branches

are the Bo olean expressions given by the low or highson resp ectively

0

t

1

t

u high (u) low (u)

t var u t t if u is a variable no de

Moreover if x x x is the variable ordering of the ROBDD we asso ciate

1 2 n

n

u

with each no de u the function f that maps b b b B to the truth value of

1 2 n

u

t b x b x b x We can now state the key lemma

1 1 2 2 n n

Lemma Canonicity lemma

n

For any function f B B there is exactly one ROBDD u with variable ordering x

1

u

x x such that f f x x

2 n 1 n

Pro of The pro of is by induction on the numb er of arguments of f For n there

are only two Bo olean functions the constantly false and constantly true functions Any

ROBDD containing at least one nonterminal no de is nonconstant Why Therefore

there is exactly one ROBDD for each of these the terminals and

Assume now that we have proven the lemma for all functions of n arguments We

n+1

pro ceed to show it for all functions of n arguments Let f B B b e any Bo olean

function of n arguments Dene the two functions f and f of n arguments by xing

0 1

the rst argument of f to resp ectively

f x x f b x x for b B

b 2 n+1 2 n+1

Sometimes f and f are called the negative and positive cofactors of f with resp ect to

0 1

x These functions satisfy the following equation

1

f x x x f x x f x x

1 n 1 1 2 n 0 2 n

Since f and f take only n arguments we assume by induction that there are unique

0 1

u u

ROBDD no des u and u with f f and f f

0 1 0 1

u u u u

There are two cases to consider If u u then f f and f f f f

0 1 0 1

f Hence u u is an ROBDD for f It is also the only ROBDD for f since due to

0 1

the ordering if x is at all present in the ROBDD ro oted at u x would need to b e the

1 1

u u low (u) u high (u)

ro ot no de However if f f then f f x f and f f x f

0 1 1 1

u u

Since f f f f by assumption the low and highson of u would b e the same

0 1

making the ROBDD violate the reducedness condition of nonredundant tests

u u

If u u then f f by the induction hyp othesis using the names x x

0 1 2 n+1

in place of x x We take u to b e the no de with var u x low u u and

1 n 1 0

u u u u

high u u ie f x f f which is reduced By assumption f f

1 1 1

u u

and f f therefore using we get f f Supp ose that v is some other no de

0

v v v v

with f f Clearly f must dep end on x ie f x f x otherwise also

1 1 1

BINARY DECISION DIAGRAMS

v v

f f x f x f a contradiction Due to the ordering this means that

0 1 1 1

v low (v ) u

var v x var u Moreover from f f it follows that f f f and

1 0

high (v ) u

which by the induction hyp othesis implies that low v u low u f f f

0 1

and high v u high u From the reducedness prop erty of uniqueness it follows that

1

u v 

An immediate consequence is the following Since the terminal is an ROBDD for all

variable orderings it is the only ROBDD that is constantly true So in order to check

whether an ROBDD is constantly true it suces to check whether it is the terminal

which is denitely a constant time op eration Similarly ROBDDs that are constantly

false must b e identical to the terminal In fact to determine whether two Bo olean

functions are the same it suces to construct their ROBDDs in the same graph and

check whether the resulting no des are the same

The ordering of variables chosen when constructing an ROBDD has a great impact on

the size of the ROBDD If we consider again the expression x y x y and

1 1 2 2

construct an ROBDD using the ordering x x y y the ROBDD consists of

1 2 1 2

no des gure and not no des as for the ordering x y x y gure

1 1 2 2

x

x x

y y y y

y y

Figure The ROBDD for x y x y with variable ordering x x y y

1 1 2 2 1 2 1 2

Exercise Show how to express all op erators from the ifthenelse op erator and the

constants and

Exercise Draw the ROBDDs for x y x y x y with orderings

1 1 2 2 3 3

x x x y y y and x y x y x y

1 2 3 1 2 3 1 1 2 2 3 3

CONSTRUCTING AND MANIPULATING ROBDDS

Exercise Draw the ROBDDs for x y x y with orderings x x

1 1 2 2 1 2

y y and x y x y How do es it compare with the example in gures and

1 2 1 1 2 2

Based on the examples you have seen so far what variable ordering would you recommend

for constructing a small ROBDD for x y x y x y x y

1 1 2 2 3 3 k k

Exercise Give an example of a sequence of ROBDDs u n which induces exp o

n

nentially bigger decision trees Ie if u has size n then the decision tree should have

n

n

size

Exercise Construct an ROBDD of maximum size over six variables

Constructing and Manipulating ROBDDs

In the previous section we saw how to construct an OBDD from a Bo olean expression

by a simple recursive pro cedure The question arises now how do we construct a reduced

OBDD One way is to rst construct an OBDD and then pro ceed by reducing it An

other more app ealing approach which we follow here is to reduce the OBDD during

construction

To describ e how this is done we will need an explicit representation of ROBDDs No des

will b e represented as numb ers with and reserved for the terminal no des The

variables in the ordering x x x are represented by their indices n

1 2 n

The ROBDD is stored in a table T u i l h which maps a no de u to its three

attributes var u i low u l and high u h Figure shows the representation of

the ROBDD from gure with the variable names changed to x x x x

1 2 3 4

Mk

In order to ensure that the OBDD b eing constructed is reduced it is necessary to deter

mine from a triple i l h whether there exists a no de u with var u i low u l and

high u h For this purp ose we assume the presence of a table H i l h u mapping

triples i l h of variable indices i and no des l h to no des u The table H is the inverse

of the table T ie for variable no des u T u i l h if and only if H i l h u The

op erations needed on the two tables are

T u i l h

initT initialize T to contain only and

u addT i l h allo cate a new no de u with attributes i l h

var u low u high u lo okup the attributes of u in T

H i l h u

initH initialize H to b e empty

b memberH i l h check if i l h is in H

u lookupH i l h nd H i l h

insertH i l h u make i l h map to u in H

CONSTRUCTING AND MANIPULATING ROBDDS

7

T u i l h

x

1

u var low high

5 6

x x

2 2

4

x

3

2 3

x x

4 4

Figure Representing an ROBDD with ordering x x x x The numb ers

1 2 3 4

inside the vertices are the identities used in the representation The numb ers and

are reserved for the terminal no des The numb ers to the right of the ROBDD shows the

index of the variables in the ordering The constants are assigned an index which is the

numb er of variables in the ordering plus one here This makes some subsequent

algorithms easier to present The low and highelds are unused for the terminal no des

MkT H i l h

if l h then return l

else if memberH i l h then

return lookupH i l h

else u addT i l h

insertH i l h u

return u

Figure The function mkT H i l h

CONSTRUCTING AND MANIPULATING ROBDDS

BuildT H t

function buildt i

if i n then

if t is false then return else return

else v buildtx i

0 i

v buildtx i

1 i

return mk i v v

0 1

end build

return buildt

Figure Algorithm for building an ROBDD from a Bo olean expression t

using the ordering x x x In a call buildt i i is the lowest

1 2 n

index that any variable of t can have Thus when the test i n succeeds t

contains no variables and must b e either constantly false or true

We shall assume that all these op erations can b e p erformed in constant time O Section

will show how such a low complexity can b e achieved

The function mk T H i l h see gure searches the table H for a no de with

variable index i and low highbranches l h and returns a matching no de if one exists

Otherwise it creates a new no de u inserts it into H and returns the identity of it The

running time of mk is O due to the assumptions on the basic op erations on T and H

The OBDD is ensured to b e reduced if no des are only created through the use of mk In

describing mk and subsequent algorithms we make use of the notation T H to indicate

that mk dep ends on the global data structures T and H but we leave out the arguments

when invoking it as part of other algorithms

Build

The construction of an ROBDD from a given Bo olean expression t pro ceeds as in the

construction of an ifthenelse normal form INF in section An ordering of the variables

x x is xed Using the Shannon expansion t x tx tx a no de for t

1 n 1 1 1

is constructed by a call to mk after the no des for tx and tx have b een constructed

1 1

by recursion The algorithm is shown in gure The call buildt i constructs an

ROBDD for a Bo olean expression t with variables in fx x x g It do es so by rst

i i+1 n

recursively constructing ROBDDs v and v for tx and tx in lines and and

0 1 i i

then pro ceeding to nd the identity of the no de for t in line Notice that if v and v are

0 1

identical or if there already is a no de with the same i v and v no new no de is created

0 1

An example of using build to compute an ROBDD is shown in gure The running

time of build is bad It is easy to see that for a variable ordering with n variables there

n

will always b e generated on the order of calls

CONSTRUCTING AND MANIPULATING ROBDDS

g

build'((x , x ) _ x 1)

1 2 3

d f

((0 , x ) _ x 2) ((1 , x ) _ x 2)

2 3 2 3

b c e e

((0 , 0) _ x 3) ((0 , 1) _ x 3) ((1 , 0) _ x 3) ((1 , 1) _ x 3)

3 3 3 3

((0 , 1) _ 0 4) ((0 , 0) _ 0 4) ((1 , 0) _ 0 4) ((1 , 1) _ 0 4)

((0 , 0) _ 1 4) ((0 , 1) _ 1 4) ((1 , 0) _ 1 4) ((1 , 1) _ 1 4)

a

x

x x

b c d

x

x x x x x

x x x

e f g

Figure Using build on the expression x x x a The tree of calls to build

1 2 3

b The ROBDD after the call build x c After the call build

3

x d After the call build x x e After the calls build x

3 2 3 3

and build x f After the call build x x g The nal

3 2 3 result

CONSTRUCTING AND MANIPULATING ROBDDS

Apply

ApplyT H op u u

1 2

initG

function appu u

1 2

if Gu u empty then return Gu u

1 2 1 2

else if u f g and u f g then u opu u

1 2 1 2

else if varu varu then

1 2

u mk varu applow u low u apphigh u high u

1 1 2 1 2

else if varu varu then

1 2

u mk varu applow u u apphigh u u

1 1 2 1 2

else varu varu

1 2

u mk varu appu low u appu high u

2 1 2 1 2

Gu u u

1 2

return u

end app

return appu u

1 2

Figure The algorithm applyT H op u u

1 2

All the binary Bo olean op erators on ROBDDs are implemented by the same general

algorithm applyop u u that for two ROBDDs computes the ROBDD for the Bo olean

1 2

u u

expression t op t The construction of apply is based on the Shannon expansion

t x tx tx

Observe that for all Bo olean op erators op the following holds

x t t op x t t x t op t t op t

1 2 1 2

1 2 1 2

If we start from the ro ot of the two ROBDDs we can construct the ROBDD of the result by

recursively constructing the low and the highbranches and then form the new ro ot from

these Again to ensure that the result is reduced we create the no de through a call to

mk Moreover to avoid an exp onential blowup of recursive calls dynamic programming

is used The algorithm is shown in gure

Dynamic programming is implemented using a table of results G Each entry i j is

either empty or contains the earlier computed result of appi j The algorithm distin

guishes b etween four dierent cases the rst of them handles the situation where b oth

arguments are terminal no des the remaining three handle the situations where at least

one argument is a variable no de

If b oth u and u are terminal a new terminal node is computed having the value of

1 2

op applied to the two truth values Recall that terminal no de is represented by a no de

with identity and similarly for

CONSTRUCTING AND MANIPULATING ROBDDS

If at least one of u and u are nonterminal we pro ceed according to the variable

1 2

index If the no des have the same index the two lowbranches are paired and app

recursively computed on them Similarly for the highbranches This corresp onds exactly

to the case shown in equation If they have dierent indices we pro ceed by pairing the

no de with lowest index with the low and highbranches of the other This corresp onds

to the equation

x t t op t x t op t t op t

i 1 2 i 1 2

which holds for all t Since we have taken the index of the terminals to b e one larger

than the index of the nonterminals the last two cases var u var u and var u

1 2 1

var u take account of the situations where one of the no des is a terminal

2

Figure shows an example of applying the algorithm on two small ROBDDs Notice

how pairs of no des from the two ROBDDs are combined and computed

To analyze the complexity of apply we let juj denote the numb er of no des that can

b e reached from u in the ROBDD Assume that G can b e implemented with constant

lo okup and insertion times See section for details on how to achieve this Due to

the dynamic programming at most ju j ju j calls to Apply are generated Each call takes

1 2

constant time The total running time is therefore O ju j ju j

1 2

Restrict

The next op eration we consider is the restriction of a ROBDD u That is given a truth

u

assignment for example x x x we want to compute the ROBDD for t under

3 5 6

u

this restriction ie nd the ROBDD for t x x x As an example consider the

3 5 6

ROBDD of gure g rep eated b elow to the left representing the Bo olean expression

x x x Restricting it with resp ect to the truth assignment x yields an

1 2 3 2

ROBDD for x x It is constructed by replacing each o ccurrence of a no de with

1 3

lab el x by its left branch yielding the ROBDD at the right 2

x1

x2 x2 x1

x3 x3

1 0 1 0

The algorithm again uses mk to ensure that the resulting OBDD is reduced Figure

shows the algorithm in the case where only singleton truth assignments bx b f g

j

are allowed Intuitively in computing restrictu j b we search for all no des with

var j and replace them by their low or highson dep ending on b Since this might

force no des ab ove the p oint of replacemen to b ecome equal it is followed by a reduction

through the calls to mk Due to the two recursive calls in line the algorithm has an

exp onential running time see exercise for an improvement that reduces this to linear time

CONSTRUCTING AND MANIPULATING ROBDDS

^ =

9

x

7 8

x

5 6

x

3 4

x

2

x

x

x

x

0,0 0,1 x

0,0 0,1 x

1,1 0,0 0,1 0,0 1,0 0,0

Figure An example of applying the algorithm apply for computing the conjunction

of the two ROBDDs shown at the top left The result is shown to the right Below the

tree of arguments to the recursive calls of app Dashed no des indicate that the value of

the no de has previously b een computed and is not recomputed due to the use of dynamic

programming The solid ellipses show calls that nishes by a call to mk with the variable

index indicated by the variables to the right of the tree

CONSTRUCTING AND MANIPULATING ROBDDS

RestrictT H u j b

function resu

if var u j then return u

else if var u j then return mk var u reslow u reshigh u

else var u j if b then return reslow u

else var u j b return reshigh u

end res

return resu

Figure The algorithm restrictT H u j b which computes an ROBDD

u

for t j b

SatCount AnySat AllSat

In this section we consider op erations to examine the set of satisfying truth assignments

u

of a no de u A truth assignment satises a no de u if t can b e evaluated to using

the truth tables of the Bo olean op erators Formally the satisfying truth assignments is

the set satu

fx x g

u

n

satu f B j t is true g

fx x g

n

where B denotes the set of all truth assignments for variables fx x g ie

1 n

functions from fx x g to the truth values B f g The rst algorithm Sat

1 n

Count computes the size of satu see gure The algorithm exploits the follow

ing fact If u is a no de with variable index var u then two sets of truth assignments

u

can make f true The rst set has var u equal to the other has var u equal to

For the rst set the numb er is found by nding the numb er of truth assignments

countlow u making low u true All variables b etween var u and var low u in

the ordering can b e chosen arbitrarily therefore in the case of var u b eing a total

var (low (u))var (u)1

of countlow u satisfying truth assignments exists To b e ecient

dynamic programming should b e applied in SatCount see exercise

The next algorithm AnySat in gure nds a satisfying truth assignment Some

irrelevant variables present in the ordering might not app ear in the result and they can

b e assigned any value whatso ever AnySat simply nds a path leading to by a depth

rst traversal prefering somewhat arbitrarily lowedges over highedges It is particularly

simple due to the observation that if a node is not the terminal it has at least one path

leading to The running time is clearly linear in the result

AllSat in gure nds all satisfying truthassignments leaving out irrelevant vari

ables from the ordering AllSatu nds all paths from a no de u to the terminal The

running time is linear in the size of the result multiplied with the time to add the single

assignments x and x in front of a list of up to n elements However

var (u) var (u)

juj

the result can b e exp onentially large in juj so the running time is the p o or O n

CONSTRUCTING AND MANIPULATING ROBDDS

SatCountT u

function countu

if u then res

else if u then res

var (low (u))var (u)1

else res countlow u

var (high (u))var (u)1

counthigh u

return res

end count

var (u)1

return countu

Figure An algorithm for determining the numb er of valid truth assign

ments Recall that the variable index var of and in the ROBDD repre

sentation is n when the ordering contains n variables numb ered through

n This means that var and var always gives n

AnySatu

if u then Error

else if u then return

else if low u then return x AnySathigh u

var (u)

else return x AnySatlow u

var (u)

Figure An algorithm for returning a satisfying truthassignment The

variables are assumed to b e x x ordered in this way

1 n

AllSatu

if u then return h i

else if u then return h i

else return

hadd x in front of all

var (u)

truthassignments in AllSatlow u

add x in front of all

var (u)

truthassignments in AllSathigh ui

Figure An algorithm which returns all satisfying truthassignments The

variables are assumed to b e x x ordered in this way We use h i to

1 n

denote sequences of truth assignments In particular h i is the empty sequence

of truth assignments and h i is the sequence consisting of the single empty

truth assignment

CONSTRUCTING AND MANIPULATING ROBDDS

Simplifyd u

function simd u

if d then return

else if u then return u

else if d then

return mk var u simd low u simd high u

else if var d var u then

if low d then return simhigh d high u

else if high d then return simlow d low u

else return mk var u

simlow d low u

simhigh d high u

else if var d var u then

return mk var d simlow d u simhigh d u

else

return mk var u simd low u simd high u

end sim

return simd u

Figure An algorithm due to Coudert et al CBM for simplifying an

ROBDD b that we only care ab out on the domain d Dynamic programming

should b e applied to improve eciency exercise

CONSTRUCTING AND MANIPULATING ROBDDS

mki u u O

0 1

n

Buildt O

Applyop u u O ju j ju j

1 2 1 2

Restrictu j b O juj See note

SatCountu O juj See note

AnySatu O jpj p Any S atu jpj O juj

juj

AllSatu O jr j n r Al l S atu jr j O

Simplifyd u O jdjjuj See note

Note These running times only holds if dynamic programming is

used exercises and

Table Worstcase running times for the ROBDD op erations The running times are the

exp ected running times since they are all based on a hashtable with exp ected constant

time search and insertion op erations

Simplify

The nal algorithm called Simplify is shown in gure The algorithm is used to

simplify an ROBDD by trying to remove no des The simplication is based on a domain

d of interest The ROBDD u is supp osed to b e of interest only on truth assignments

that also satisfy d This o ccurs when using ROBDDs for formal verication Section

shows how to do formal verication with ROBDDs but contains no example of using

Simplify

To b e precise given d and u Simplify nds another ROBDD u typically smaller

0

d u d u

It do es so by trying to identify sons and thereby than u such that t t t t

making some no des redundant A more detailed analysis is left to the reader

The running time of the algorithms of the previous sections is summarized in table

Existential Quantication and Substitution

When applying ROBDDs often existential quantication and composition is used Ex

istential quantication is the Bo olean op eration xt The meaning of an existential

quantication of a Bo olean variable is given by the following equation

xt tx tx

On ROBDDs existential quantication can therefore b e implemented using two calls to

Restrict and a single call to Apply

Composition is the ROBDD op eration p erforming the equivalent of substitution on

Bo olean expression Often the notation tt x is used to describ e the result of substituting

all free o ccurrences of x in t by t An o ccurrence of a variable is free if it is not within

1

the scop e of a quantier To p erform this substitution on ROBDDs we observe the

1

Since ROBDDs contain no quantiers we shall not b e concerned with the problems of free variables

0

of t b eing b ound by quantiers of t

CONSTRUCTING AND MANIPULATING ROBDDS

following equation which holds if t contains no quantiers

tt x tt x t tx tx

Since t tx tx t tx t tx we can compute this with two

applications of restrict and three applications of apply with the op erators

However by essentially generalizing apply to op erators op with three arguments we

can do b etter see exercise

Exercises

Exercise Construct the ROBDD for x x x with ordering x x x

1 2 3 1 2 3

using the algorithm Build in gure

Exercise Show the representation of the ROBDD of gure in the style of gure

Exercise Suggest an improvement BuildConjt of Build which generates only a

linear numb er of calls for Bo olean expressions t that are conjunctions of variables and

negations of variables

Exercise Construct the ROBDDs for x and x y using whatever ordering you

want Compute the disjunction of the two ROBDDs using apply

Exercise Construct the ROBDDs for x x and x x using build with the

1 3 2 3

ordering x x x Use apply to nd the ROBDD for x x x x

1 2 3 1 3 2 3

Exercise Is there any essential dierence in running time b etween nding restrictb

and restrictb n when the variable ordering is x x x

1 2 n

Exercise Use dynamic programming to improve the running time of Restrict

Exercise Generalise restrict to arbitrary truth assignments x b x b x

i i i i i

n

It might b e convenient to assume that x x x b

i i i i

n n

Exercise Suggest a substantially b etter way of building ROBDDs for large Bo olean

expressions than build

Exercise Change SatCount such that dynamic programming is used How do es

this change the running time

Exercise Explain why dynamic programming do es not help in improving the run

ning time of AllSat

Exercise Improve the eciency of Simplify with dynamic programming

Exercise Write the algorithm Composeu x u for computing the ROBDD of

1 2

u u x eciently along the lines of apply First generalize apply to op erators op

1 2

with three arguments as for example the ifthenelse op erator utilizing once again the

Shannon expansion Then use equation to write the algorithm

IMPLEMENTING THE ROBDD OPERATIONS

Implementing the ROBDD op erations

There are many choices that have to b e taken in implementing the ROBDD op erations

There is no obvious b est way of doing it This section gives hints for some reasonable

solutions

First the no de table T is an array as shown in gure The only problem is that the

size of the array is not known until the full BDD has b een constructed Either a xed

upp er b ound could b e assumed or other tricks must b e applied for example dynamic

arrays CLR sec The table H could b e implemented as a hashtable using for

instance the hash function

hi v v pairi pairv v mo d m

where pair is a pairing function that maps pairs of natural numb ers to natural numb ers

and m is a prime One choice for the pairing function is

i j i j

i pairi j

which is a bijection and therefore p erfect it pro duces no collisions As usual with

hashtables we have to decide on the size as a prime m However since the size of H

grows dynamically it can b e hard to nd a go o d choice for m One solution would b e to

take m very large for example m which is the th prime numb er

and then take as the hashing function

k

h i v v hi v v mo d

k

using a table of size Starting from some reasonable small value of k we could increase

k

the table when it contains elements by adding one to k construct a new table and

rehash all elements into this new table Again see for example CLR sec for

details For such a dynamic hashtable the amortized exp ected cost of each op eration is

still O

The table G used in Apply could b e implemented as a twodimensional array How

ever it turns out to b e very sparsely used esp ecially if we succeed in getting small

ROBDDs and it is b etter to use a hashtable for it The hashing function used could

b e g v v pair v v mo d m and as for H a dynamic hashtable could b e used

Examples of problem solving with ROBDDs

This section will describ e various examples of problems that can b e solved with an

ROBDDpackage The examples are not chosen to illustrate when ROBDDs are the

b est choice but simply chosen to illustrate the scop e of p otential applications

The Queens problem

A classical chessb oard problem is the queens problem Is it p ossible to place queens

on a chess b oard so that no queen can b e captured by another queen To b e a bit more

EXAMPLES OF PROBLEM SOLVING WITH ROBDDS

general we could ask the question for arbitrary N Is it p ossible to place N queens safely

on a N N chess b oard

To solve the problem using ROBDDs we must enco de it using Bo olean variables We

do this by intro ducing a variable for each p osition on the b oard We name the variables

as x i j N where i is the row and j is the column A variable will b e if a queen

ij

is placed on the corresp onding p osition

8

7

6

5

4

3

2

1

1 2 3 4 5 6 7 8

The capturing rules for queens require that no other queen can b e p ositioned on the

same row column or any of the diagonals This we can express as Bo olean expressions

For all i j

x x

il ij

1l N l =j

x x

k j ij

1k N k =i

x x

k j +k i ij

1k N 1j +k iN k =i

x x

k j +ik ij

1k N 1j +ik N k =i

Moreover there must b e a queen in each row For all i

x x x

i1 i2 iN

Taking the conjunction of all the ab ove requirements we get a predicate Sol x true at

N

exactly the congurations that are solutions to the N queens problem

Exercise Queens Problem Write a program that can nd an ROBDD for

Sol x when given N as input Make a table of the numb er of solutions to the N

N

queens problem for N When there is a solution give one

EXAMPLES OF PROBLEM SOLVING WITH ROBDDS

x y

^

xor

_ c

o

^

c

i

xor

s

Figure A fulladder

Correctness of Combinational Circuits

A ful ladder takes as arguments two bits x and y and an incoming carry bit c It

i

pro duces as output a sum bit s and an outgoing carry bit c The requirement is that

o

c s x y c in other words c is the most signicant bit of the sum of x y and

o i o

c and s the least signicant bit The requirement can b e written down as a table for c

i o

and a table for s in terms of values of x y and c From such a table it is easy to write

i

down a DNF for c and s

o

At the normal level of abstraction a combinational circuit is nothing else than a

Bo olean expression It can b e represented as an ROBDD using Build to construct

the trivial ROBDDs for the inputs and using a call to Apply for each gate

Exercise Find DNFs for c and s Verify that the circuit in gure implements a

o

one bit fulladder using the ROBDDpackage and the DNFs

Equivalence of Combinational Circuits

As ab ove we can construct an ROBDD from a combinational circuit and use the ROBDDs

to show prop erties For instance the equivalence with other circuits

Exercise Verify that the two circuits in gure are not equivalent using ROBDDs

Find an input that returns dierent outputs in the two circuits

EXAMPLES OF PROBLEM SOLVING WITH ROBDDS

x

^

_

nor

y

a

^

x

^

_

nor

y

x

^

y

:

^ :

b _

: ^

x

^

y

Figure Two circuits used in exercise

VERIFICATION WITH ROBDDS

t

c c

h

t h

t h

c c

h

t

Figure Milners Scheduler with cyclers The token is passed clo ckwise from c to c

1 2

to c to c and back to c

3 4 1

Verication with ROBDDs

One of the ma jor uses of ROBDDs is in formal verication In formal verication a mo del

of a system M is given together with some prop erties P supp osed to hold for the system

The task is to determine whether indeed M satisfy P The approach we take in which we

shall use an algorithm to answer the satisfaction problem is often called model checking

We shall lo ok at a concrete example called Milners Scheduler taken from Milners

b o ok Mil The mo del consists of N cyclers connected in a ring that coop erates

on starting and detecting termination of N tasks that are not further describ ed The

scheduler must make sure that the N tasks are always started in order but they are

allowed to terminate in any order This is one of the prop erties that has to b e shown to

hold for the mo del The cyclers try to fulll this by passing a token the holder of the

token is the only pro cess allowed to start its task

All cyclers are similar except that one of them has the token in the initial state The

cyclers cyc i N are describ ed in a statebased fashion as small transition systems

i

over the Bo olean variables t h and c The variable t is when task i is running and

i i i i

when it is terminated h is when cycler i has a token otherwise c is when cycler

i i

i has put down the token and cycler i not yet picked it up Hence a cycler starts a

task by changing t from to and detects its termination when t is again changed back

i i

to and it picks up the token by changing c from to and puts it down by changing

i

c from to The b ehaviour of cycler i is describ ed by two transitions

i+1

if c t then t c h

i i i i i

if h then c h

i (i mo d N )+1 i

The meaning of a transition if condition then assignment is that if the condition is

true in some state then the system can evolve to a new state p erforming the parallel

assignment Hence if the system is in a state where c is and t is then we can

i i

simultaneously set t to c to and h to

i i i

VERIFICATION WITH ROBDDS

The transitions are enco ded by a single predicate over the value of the variables b efore

the transitions the prestate and the values after the transition the poststate The

variables in the prestate are the t h c i N which we shall collectively refer to as

i i i

x and in the p oststate t h c i N which we shall refer to as x Each transition

i i i

is an atomic action that excludes any other action Therefore in the enco ding we shall

often have to say that a lot of variables are unchanged Assume that S is a subset of the

unprimed variables x We shall use a predicate unchanged over x x which ensures that

S

all variables in S are unchanged It is dened as follows

x x unchanged

def

S

xS

It is slightly more convenient to use the predicate assigned unchanged which

0 0

S xnS

express that every variable not in S is unchanged We can now dene P the transitions

i

of cycler i over the variables x x as follows

P c t t c h assigned

i def i i

fc t h g

i i i

i i i

assigned h c h

i

fc h g

i

(i mo d N )+1

i

i mo d N

The signalling of termination of task i by changing t from to p erformed by the

i

environment is mo deled by N transitions E i N

i

E t t assigned

i def i

ft g

i

i

expressing the transitions if t then t Now at any given state the system can

i i

p erform one of the transitions from one of the P s or the E s ie all p ossible transitions

i i

are given by the predicate T

T P P E E

def 1 n 1 n

In the initial state we assume that all tasks are stopp ed no cycler has a token and only

place c has a token Hence the initial state can b e characterized by the predicate I

1

over the unprimed variables x given by

I t h c c c

def 1 2 N

Here applied to a vector t means the conjunction of applied to each co ordinate t

i

The predicates describing Milners Scheduler are summarized in gure

Within this setup we could start asking a lot of questions For example

Can we nd a predicate R over the unprimed variables characterizing exactly the

states that can b e reached from I R is called the set of reachable states

How many reachable states are there

Is it the case that in all reachable states only one token is present

Is task t always only started after t

i i1

VERIFICATION WITH ROBDDS

V

unchanged x x

def

S

xS

assigned unchanged

0 0

def

S xnS

P c t t c h assigned

i def i i

c t h

i i i

i i i

h c h assigned

i

c h

i mo d N +1 i

i

i mo d N

E t t assigned

i def i

t

i

i

P E T

i i def

1iN

I t h c c c

def 1 2 N

Figure Milners Scheduler as describ ed by the transition predicate T and the initial

state predicate I

Do es Milners Scheduler p ossess a deadlo ck Ie is there a reachable state in which

no transitions can b e taken

To answer these questions we rst have to compute R Intuitively R must b e the set

of states that either satisfy I are initial or within a nite numb er of T transitions can

b e reached from I This suggest an iterative algorithm for computing R as an increasing

0 1 k

chain of approximations R R R Step k of the algorithm nd states that with

0

less than k transitions can b e reached from I Hence we take R the constantly false

k +1

predicate and compute R as the disjunction of I and the set of states which from one

k

transition of T can b e reached from R Figure illustrates the computation of R

At any How do we compute this with ROBDDs We start with the ROBDD R

p oint in the computation the next approximation is computed by the disjunction of I and

T comp osed with the previous approximation R We are done when the current and the

previous approximations coincide

ReachableStatesI T x x

R

rep eat

R R

R I x T R xx

until R R

return R

Knights tour

Using the same enco ding of a chess b oard as in section letting x denote the

ij

presence of a Knight at p osition i j we can solve other problems We can enco de moves

of a Knight as transitions For each p osition moves are p ossible if they stay on the

b oard A Knight at i j can b e moved to any one of i j i j assuming

they are vacant and within the b oard b oundary For all i j and k l with k l N

and k l fi j i j g

0 0

x M x x x x x

0 0

ijk l def ij k l i j

i j ij k l

0 0

(i j )f(ij )(k l )g

VERIFICATION WITH ROBDDS

Full state space

I R

R

R

R

Figure Sketch of computation of the reachable states

Hence the transitions are given as the predicate T x x

T x x M

def ijk l

1ijk l N (k l )f(i1j 2)(i2j 1)g

Exercise Knights tour Write a program to solve the following problem using

the ROBDDpackage Is it p ossible for a Knight p ositioned at the lower left corner to

visit all p ositions on an N N b oard Hint Compute iteratively all the p ositions that

can b e reached by the Knight Try it for various N

Exercise Why do es the algorithm ReachableStates always terminate

Exercise In this exercise we shall work with Milners Scheduler for N It is by

far b e the most convenient to solve the exercise by using an implementation of an ROBDD

package

a Find the reachable states as an ROBDD R

b Find the numb er of reachable states

c Show that in all reachable states at most one token is present on any of the

placeholders c c by formulating a suitable prop erty P and prove

1 N

that R P

PROJECT AN ROBDD PACKAGE

d Show that in all reachable states Milners Scheduler can always p erform a

transition ie it do es not p ossess a dead lock

Exercise Complete the ab ove exercise by showing that the tasks are always started

in sequence N

Exercise Write a program that given an N as input computes the reachable states

of Milners Scheduler with N cyclers The program should write out the numb er of

reachable states using SatCount Run the program for N Measure

the running times and draw a graph showing the measurements as a function of N What

is the asymptotic running time of your program

Pro ject An ROBDD Package

This pro ject implements a small package of ROBDDop erations The full package should

contain the following op erations

Initn

Initialize the package Use n variables numb ered through n

Printu

Print a representation of the ROBDD on the standard output Useful for debugging

Mki l h

Return the numb er u of a no de with var u i low u l high u h This could

b e an existing no de or a newly created no de The reducedness of the ROBDD should

not b e violated

Buildt

Construct an ROBDD from a Bo olean expression You could restrict yourself to the

expressions x or x or nite conjunctions of these Why

Apply op u u

1 2

Construct the ROBDD resulting from applying op on u and u

1 2

Restrictu j b

Restrict the ROBDD u according to the truth assignment bx

j

SatCountu

Return the numb er of elements in the set satu Use a typ e that can contain very

large numb ers such as oating p oint numb ers

AnySatu

Return a satisfying truth assignment for u

Subpro ject

Implement the tables T and H with their op erations listed in section On top of these

implement the op erations Initn Printu and Mki l h

REFERENCES

Subpro ject

Continue implementation of the package by adding the op erations Buildt and Ap

ply op u u

1 2

Subpro ject

Finish your implementation of the package by adding Restrictu j b SatCountu

and AnySatu

References

AH Henrik Reif Andersen and Henrik Hulgaard Bo olean expression diagrams In

Proceedings Twelfth Annual IEEE Symposium on Logic in

pages Warsaw Poland June July IEEE Computer So ciety

Bry Randal E Bryant Graphbased algorithms for Bo olean function manipulation

IEEE Transactions on Computers C

Bry Randal E Bryant Symb olic Bo olean manipulation with ordered binarydecision

diagrams ACM Computing Surveys Septemb er

CBM Olivier Coudert Christian Berthet and Jean Christophe Madre Verication

of synchronous sequential machines based on symb olic execution In J Sifakis

editor Automatic Verication Methods for Finite State Systems Proceedings

volume of LNCS pages SpringerVerlag

CLR Thomas H Cormen Charles E Leiserson and Ronald L Rivest Introduction

to Algorithms McGrawHill

Co o SA Co ok The complexity of theoremproving pro cedures In Proceedings of the

Third Annual ACM Symposium on the Theory of Computing pages

New York Asso ciation for Computing Machinery

Mil Robin Milner Communication and Concurrency Prentice Hall