Binary Decision Diagrams
Total Page:16
File Type:pdf, Size:1020Kb
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 negation 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 set 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 tautology 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 negations of variables ie if it is of the form 1 1 1 l 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