An Introduction to Binary Decision Diagrams



Similar documents

The Classes P and NP

Introduction to Logic in Computer Science: Autumn 2006

Offline 1-Minesweeper is NP-complete

Mathematical Induction

NP-Completeness and Cook s Theorem

CHAPTER 7 GENERAL PROOF SYSTEMS

December 4, 2013 MATH 171 BASIC LINEAR ALGEBRA B. KITCHENS

Discuss the size of the instance for the minimum spanning tree problem.

3. Mathematical Induction

Mathematics for Computer Science/Software Engineering. Notes for the course MSM1F3 Dr. R. A. Wilson

Chapter 1. NP Completeness I Introduction. By Sariel Har-Peled, December 30, Version: 1.05

Determination of the normalization level of database schemas through equivalence classes of attributes

Lecture 1: Course overview, circuits, and formulas

Lecture 7: NP-Complete Problems

Exponential time algorithms for graph coloring

WHAT ARE MATHEMATICAL PROOFS AND WHY THEY ARE IMPORTANT?

One last point: we started off this book by introducing another famously hard search problem:

Analysis of Algorithms, I

Handout #1: Mathematical Reasoning

Quotient Rings and Field Extensions

Complexity Theory. IE 661: Scheduling Theory Fall 2003 Satyaki Ghosh Dastidar

Approximation Algorithms

The Characteristic Polynomial

Parametric Domain-theoretic models of Linear Abadi & Plotkin Logic

Cost Model: Work, Span and Parallelism. 1 The RAM model for sequential computation:

2.1 Complexity Classes

1 Sets and Set Notation.

Revised Version of Chapter 23. We learned long ago how to solve linear congruences. ax c (mod m)

Why? A central concept in Computer Science. Algorithms are ubiquitous.

A Working Knowledge of Computational Complexity for an Optimizer

Generating models of a matched formula with a polynomial delay

Chapter. NP-Completeness. Contents

Simulation-Based Security with Inexhaustible Interactive Turing Machines

Lecture 3: Finding integer solutions to systems of linear equations

You know from calculus that functions play a fundamental role in mathematics.

Integer roots of quadratic and cubic polynomials with integer coefficients

Formal Languages and Automata Theory - Regular Expressions and Finite Automata -

Bounded Treewidth in Knowledge Representation and Reasoning 1

CHAPTER II THE LIMIT OF A SEQUENCE OF NUMBERS DEFINITION OF THE NUMBER e.

A Propositional Dynamic Logic for CCS Programs

Indiana State Core Curriculum Standards updated 2009 Algebra I

ON THE COMPLEXITY OF THE GAME OF SET.

Lecture 15 An Arithmetic Circuit Lowerbound and Flows in Graphs

The Goldberg Rao Algorithm for the Maximum Flow Problem

MATRIX ALGEBRA AND SYSTEMS OF EQUATIONS

Math 312 Homework 1 Solutions

Regular Languages and Finite Automata

Notes on Factoring. MA 206 Kurt Bryan

CS510 Software Engineering

Mathematics Course 111: Algebra I Part IV: Vector Spaces

MATH10212 Linear Algebra. Systems of Linear Equations. Definition. An n-dimensional vector is a row or a column of n numbers (or letters): a 1.

Factoring & Primality

Discrete Mathematics & Mathematical Reasoning Chapter 10: Graphs

6.852: Distributed Algorithms Fall, Class 2

Topology-based network security

Some Polynomial Theorems. John Kennedy Mathematics Department Santa Monica College 1900 Pico Blvd. Santa Monica, CA

Continued Fractions and the Euclidean Algorithm

C H A P T E R. Logic Circuits

Notes on Determinant

Factorization Theorems

Outline. NP-completeness. When is a problem easy? When is a problem hard? Today. Euler Circuits

OPRE 6201 : 2. Simplex Method

A first step towards modeling semistructured data in hybrid multimodal logic

Network (Tree) Topology Inference Based on Prüfer Sequence

Bounded-width QBF is PSPACE-complete

Model Checking: An Introduction

NOTES ON LINEAR TRANSFORMATIONS

Polynomial Degree and Lower Bounds in Quantum Complexity: Collision and Element Distinctness with Small Range

How To Know If A Domain Is Unique In An Octempo (Euclidean) Or Not (Ecl)

JUST-IN-TIME SCHEDULING WITH PERIODIC TIME SLOTS. Received December May 12, 2003; revised February 5, 2004

Diagonalization. Ahto Buldas. Lecture 3 of Complexity Theory October 8, Slides based on S.Aurora, B.Barak. Complexity Theory: A Modern Approach.

Coverability for Parallel Programs

Sudoku as a SAT Problem

Information Theory and Coding Prof. S. N. Merchant Department of Electrical Engineering Indian Institute of Technology, Bombay

9.2 Summation Notation

Section 1.1. Introduction to R n

Discrete Mathematics. Hans Cuypers. October 11, 2007

CMSC 858T: Randomized Algorithms Spring 2003 Handout 8: The Local Lemma

Lecture 4: AC 0 lower bounds and pseudorandomness

The Set Data Model CHAPTER What This Chapter Is About

Linear Codes. Chapter Basics

Cryptography and Network Security Prof. D. Mukhopadhyay Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

INCIDENCE-BETWEENNESS GEOMETRY

Random vs. Structure-Based Testing of Answer-Set Programs: An Experimental Comparison

Tree-representation of set families and applications to combinatorial decompositions

Software Modeling and Verification

Chapter 7 Uncomputability

Chapter 7: Products and quotients

How To Find Out What A Key Is In A Database Engine

PYTHAGOREAN TRIPLES KEITH CONRAD

Compact Representations and Approximations for Compuation in Games

The Basics of Graphical Models

Project and Production Management Prof. Arun Kanda Department of Mechanical Engineering Indian Institute of Technology, Delhi

Lecture 2: Universality

KNOWLEDGE FACTORING USING NORMALIZATION THEORY

2 Temporal Logic Model Checking

each college c i C has a capacity q i - the maximum number of students it will admit

Fairness in Routing and Load Balancing

Reading 13 : Finite State Automata and Regular Expressions

Transcription:

An Introduction to Binary Decision Diagrams Henrik Reif Andersen x y y z 1 0 Lecture notes for Efficient Algorithms and Programs, Fall 1999 E-mail: hra@itudk Web: http://wwwitudk/people/hra The IT University of Copenhagen Glentevej 67 2400 Copenhagen NV

1

2 Preface This note is a short introduction to Binary Decision Diagrams It provides some background knowledge and describes the core algorithms More details can be found in Bryant s original paper on Reduced rdered Binary Decision Diagrams [Bry86] and the survey paper [Bry92] A recent extension called Boolean Expression Diagrams is described in [AH97] This note is a revision of an earlier version from fall 1996 (based on versions from 1995 and 1994) The major differences are as follows: Firstly, RBDDs are now viewed as nodes of one global graph with one fixed ordering to reflect state-of-the-art of efficient BDD packages The algorithms have been changed (and simplified) to reflect this fact Secondly, a proof of the canonicity lemma has been added Thirdly, the sections presenting the algorithms have been completely restructured Finally, the project proposal has been revised Acknowledgements Thanks to the students on the courses of fall 1994, 1995, and 1996 for helping me debug and improve the notes Thanks are also due to Hans Rischel, Morten Ulrik Sørensen, Niels Maretti, Jørgen Staunstrup, Kim Skak Larsen, Henrik Hulgaard, and various people on the Internet who found typos and suggested improvements

3

CNTENTS 4 Contents 1 Boolean Expressions 6 2 Normal Forms 7 3 Binary Decision Diagrams 8 4 Constructing and Manipulating RBDDs 15 41 MK 15 42 BUILD 16 43 APPLY 17 44 RESTRICT 19 45 SATCUNT, ANYSAT, ALLSAT 21 46 SIMPLIFY 23 47 Existential Quantification and Substitution 25 5 Implementing the RBDD operations 26 6 Examples of problem solving with RBDDs 27 61 The 8 Queens problem 27 62 Correctness of Combinational Circuits 28 63 Equivalence of Combinational Circuits 28 7 Verification with RBDDs 29 71 Knights tour 32 8 Project: An RBDD Package 34 References 35

CNTENTS 5

1 BLEAN EXPRESSINS 6 ( 1 Boolean Expressions The classical calculus for dealing with truth values consists of Boolean variables, the constants true and false, the operators of conjunction, disjunction, negation, implication, and bi-implication which together form the Boolean expressions Sometimes the variables are called propositional variables or propositional letters and the Boolean expressions are then known as Propositional Logic Formally, Boolean expressions are generated from the following grammar: where ranges over a set of Boolean variables This is called the abstract syntax of Boolean expressions The concrete syntax includes parentheses to solve ambiguities Moreover, as a common convention it is assumed that the operators bind according to their relative priority The priorities are, with the highest first:,,,, Hence, for example!"!# $# $# % A Boolean expression with variables & '!( denotes for each assignment of truth values to the variables itself a truth value according to the standard truth tables, see figure 1 Truth assignments are written as sequences of assignments of values to variables, eg, )*,+- +/,+ +&0 which assigns to and, to and With this particular truth assignment the above expression has value, whereas )*1+- +,+2,+ 0 yields Figure 1: Truth tables 54 The set of truth values is often denoted 3 76 If we fix an ordering of the variables of a Boolean expression we can view as defining a function from 3 to 3 where 8 is the number of variables Notice, that the particular ordering chosen for the variables is essential for what function is defined Consider for example the expression If we choose the ordering :9; then this is the function! =#, true if the first argument implies the second, but if we choose the ordering >9? then it is the function @ " @#, true if the second argument implies the first When we later consider compact representations of Boolean expressions, such variable orderings play a crucial role Two Boolean expressions and BA are said to be equal if they yield the same truth value for all truth assignments A Boolean expression is a tautology if it yields true for all truth assignments; it is satisfiable if it yields true for at least one truth assignment Exercise 11 Show how all operators can be encoded using only and Use this to argue that any Boolean expression can be written using only,, variables, and applied to variables Exercise 12 Argue that and A are equal if and only if A is a tautology Is it possible to say whether is satisfiable from the fact that is a tautology?

2 NRMAL FRMS 7 R X I I ( 2 Normal Forms A Boolean 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 C ED2D2D where each NM is either a variable M F&G # ED2D2D HJI JI D2D2D M or a negation of a variable P Q# =# JI FLK # (1) An example is which is a well-known function of and (which one?) A more succinct presentation of (1) is to write it using indexed versions of and : TUFWV X M"YZ M!S S Similarly, a Conjunctive Normal Form (CNF) is an expression that can be written as M!S TUFWV R S where each NM is either a variable or a negated variable It is not difficult to prove the following proposition: Proposition 1 Any Boolean expression is equal to an expression in CNF and an expression in DNF In general, it is hard to determine whether a Boolean expression is satisfiable This is made precise by a famous theorem due to Cook [Coo71]: Theorem 1 (Cook) Satisfiability of Boolean expressions is NP-complete (For readers unfamiliar with the notion of NP-completeness the following short summary of the pragmatic consequences suffices Problems that are NP-complete can be solved by algorithms that run in exponential time No polynomial time algorithms are known to exist for any of the NP-complete problems and it is very unlikely that polynomial time algorithms should indeed exist although nobody has yet been able to prove their non-existence) Cook s theorem even holds when restricted to expressions in CNF For DNFs satisfiability is decidable in polynomial time but for DNFs the tautology check is hard (co-np complete) Although satisfiability is easy for DNFs and tautology check easy for CNFs, this does not help us since the conversion between CNFs and DNFs is exponential as the following example shows Consider the following CNF over the variables [ # [ M"YZ # ED2D2D [ / " [! / /! ( ( [ #\ ( :

3 BINARY DECISIN DIAGRAMS 8 ( ( ( ( ( The corresponding DNF is a disjunction which has a disjunct for each of the 8 -digit binary [ (for numbers from 17 / 71 to 17 / 71 the ] th digit representing a choice of either ) or (for ): (_^ [ [ D2D2D2 [ [ # (_^ [ [ D2D2D2 [ # D2D2D2 D2D2D (_^ (_^ [ # #\ Whereas the original expression has size proportional to 8 the DNF has size proportional to 8a` The next section introduces a normal form that has more desirable properties than DNFs and CNFs In particular, there are efficient algorithms for determining the satisfiability and tautology questions Exercise 21 Describe a polynomial time algorithm for determining whether a DNF is satisfiable Exercise 22 Describe a polynomial time algorithm for determining whether a CNF is a tautology Exercise 23 Give a proof of proposition 1 Exercise 24 Explain how Cook s theorem implies that checking in-equivalence between Boolean expressions is NP-hard Exercise 25 Explain how the question of tautology and satisfiability can be decided if we are given an algorithm for checking equivalence between Boolean expressions 3 Binary Decision Diagrams Let >b [ c be the if-then-else operator defined by >b [ c [ # d"# hence, b [ is true if and [ are true or if is false and is true We call the test expression All operators can easily be expressed using only the if-then-else operator and the constants and Moreover, this can be done in such a way that all tests are performed only on (un-negated) variables and variables occur in no other places Hence the operator gives rise to a new kind of normal form For example, is eb #, is eb fb #$ fb # Since variables must only occur in tests the Boolean expression is represented as eb An If-then-else Normal Form (INF) is a Boolean expression built entirely from the if-then-else operator and the constants and such that all tests are performed only on variables

3 BINARY DECISIN DIAGRAMS 9 If we by )*1+0 denote the Boolean expression obtained by replacing with in then it is not hard to see that the following equivalence holds: Eeb )g+0h )i,+0, (2) This is known as the Shannon expansion of with respect to This simple equation has a lot of useful applications The first is to generate an INF from any expression If contains no variables it is either equivalent to or which is an INF therwise we form the Shannon expansion of with respect to one of the variables in Thus since )*1+0 and )g+0 both contain one less variable than, we can recursively find INFs for both of these; call them [ and An INF for is now simply eb $ [ We have proved: Proposition 2 Any Boolean expression is equivalent to an expression in INF Example 1 Consider the Boolean expression jk d"# 7&# If we find an INF of by selecting in order the variables $ d-!2 _ on which to perform Shannon expansions, we get the expressions lmb - [ [ cjb [B[ cjb B$ [B[ nb [B[ $ [B[B[ B nb BB$ B [ [B[B[ 7ob [B[ 7ob B [ 7ob BB 7ob Figure 2 shows the expression as a tree Such a tree is also called a decision tree p A lot of the expressions are easily seen to be identical, so it is tempting to identify them For example, instead of B [ we can use [B[B[ and instead of BB we can use [B[ If we substitute [B[B[ for B [ in the right-hand side of B and also [B[ for BB, we in fact see that [B[ and B are identical, and in we can replace B with [B[ If we in fact identify all equal subexpressions we end up with what is known as a binary decision diagram (a BDD) It is no longer a tree of Boolean expressions but a directed acyclic graph (DAG) Applying this idea of sharing, can now be written as: lmb - [ [ cjb [B[ cjb [B[ [B[ nb [B[ $ [B[B[ [B[B[ 7ob [B[ 7ob

3 BINARY DECISIN DIAGRAMS 10 t s t t s q,r u/r u/r q7v q7v u$v u$v u$v u$v s t t s t Figure 2: A decision tree for cl# lines high-branches _-# Dashed lines denote low-branches, solid Each subexpression can be viewed as the node of a graph Such a node is either terminal in the case of the constants and, or non-terminal A non-terminal node has a low-edge corresponding to the else-part and a high-edge corresponding to the then-part See figure 3 Notice, that the number of nodes has decreased from w in the decision tree to x in the BDD It is not hard to imagine that if each of the terminal nodes were other big decision trees the savings would be dramatic Since we have chosen to consistently select variables in the same order in the recursive calls during the construction of the INF of, the variables occur in the same orderings on all paths from the root of the BDD In this situation the binary decision diagram is said to be ordered (an BDD) Figure 3 shows a BDD that is also an BDD Figure 4 shows four BDDs Some of the tests (eg, on in y ) are redundant, since both the low- and high-branch lead to the same node Such unnecessary tests can be removed: any reference to the redundant node is simply replaced by a reference to its subnode If all identical nodes are shared and all redundant tests are eliminated, the BDD is said to be reduced (an RBDD) RBDDs have some very convenient properties centered around the canonicity lemma below (ften when people speak about BDDs they really mean RBDDs) To summarize:

3 BINARY DECISIN DIAGRAMS 11 ~ ~ ~ s z s z s { z\{ z@{ Figure 3: A BDD for c"# drawn as dotted lines and high-edges as solid lines } ~ _-# with ordering 9 co9 9 7 Low-edges are s s s ƒ{ { m } ~ a b c d Figure 4: Four BDDs: a) An BDD for b) Another BDD for with two redundant tests c) Same as y with one of the redundant tests removed d) An BDD for with one redundant test

3 BINARY DECISIN DIAGRAMS 12 Š Š Š u q ( 3 ( q ˆu q q q q Figure 5: The ordering and reducedness conditions of RBDDs Left: Variables must be ordered Middle: Nodes must be unique Right: nly non-redundant tests should be present A Binary Decision Diagram (BDD) is a rooted, directed acyclic graph with one or two terminal nodes of out-degree zero labeled 0 or 1, and a set of variable nodes of out-degree two The two outgoing edges are given by two functions ŒŽ # and c # (In pictures, these are shown as dotted and solid lines, respectively) A variable 7 # is associated with each variable node A BDD is rdered (BDD) if on all paths through the graph the variables respect a given linear order 9 9 D2D2D 9 ( An ()BDD is Reduced (R()BDD) if Š (uniqueness) no two distinct nodes and have the same variable name and low- and high-successor, ie, 7 # 7 #$ ŒŽ # ŒŽ #- c # c # implies and (non-redundant tests) no variable node has identical low- and highsuccessor, ie, ŒŽ # š c #\ The ordering and reducedness conditions are shown in figure 5 RBDDs have some interesting properties They provide compact representations of Boolean expressions, and there are efficient algorithms for performing all kinds of logical operations on RBDDs They are all based on the crucial fact that for any function b 3 there is exactly one RBDD representing it This means, in particular, that there is exactly one RBDD for the constant true (and constant false) function on 3 : the terminal node (and in case of false) Hence, it is possible to test in constant time whether an RBDD is constantly true or false (Recall that for Boolean expressions this problem is NP-complete) To make this claim more precise we must say what we mean for an RBDD to represent a function First, it is quite easy to see how the nodes of an RBDD inductively defines Boolean expressions Bœ : A terminal node is a Boolean constant A non-terminal node marked with is an if-then-else expression where the condition is and the two branches are the Boolean expressions given by the low- or high-son, respectively: [ œ 7 #jb " "ž Ÿ / œ' B œ/ if is a variable node

3 BINARY DECISIN DIAGRAMS 13 œ š œ 3 œ ( ² œ ( ² 3 Moreover, if n9 9 D2D2D 9 ( is the variable ordering of the RBDD, we associate with each node the function that maps y - y 2 / / y (1#n 3 to the truth value of Bœ )*y +& y + / / y ( +(70 We can now state the key lemma: Lemma 1 (Canonicity lemma) For any function D2D2D 9 ( such that œ b 3 there is exactly one RBDD with variable ordering $ / / "(,# 9? 9 Proof: The proof is by induction on the number of arguments of For 8 there are only two Boolean functions, the constantly false and constantly true functions Any RBDD containing at least one non-terminal node is non-constant (Why?) Therefore there is exactly one RBDD for each of these: the terminals and Assume now that we have proven the lemma for all functions ( «@ of 8 arguments We proceed to show it for all functions of 8 ª arguments Let b 3 be any Boolean function of 8 arguments Define the two functions [ and of 8 arguments by fixing the first argument of to respectively : 2 / '!( «@!# y "2 $!(2«@!# for y 3 (Sometimes [ and are called the negative and positive co-factors of with respect to ) These functions satisfy the following equation: - / / "(,# mb / / "(,#- [ 2 '!(1#\ (3) Since [ and take only 8 arguments we assume by induction that there are unique RBDD nodes [ and œ' with [ œ G and There are two cases to consider If [ œ' n œ G then and [ œ' o œ G Hence [ is an RBDD for It is also the only RBDD for since due to the ordering, if is at all present in the RBDD rooted at, would need to be the root node However, if then [ J œ' )i,+b0 and "ž ŸW œ' ) 2+J0 Since [ œ$ œ G by assumption, the low- and high-son of would be the same, making the RBDD violate the reducedness condition of non-redundant tests If [ œ then š œ G by the induction hypothesis (using the names 2 / '!( «@ in place of $ / /!( ) We take to be the node with 7 #, ŒŽ # [, and c #, ie, œ ƒb œ G œ$which is reduced By assumption œ$ œ G and [ therefore œ± using (3) we get Suppose that is some other node with ² Clearly, ² must depend on, ie, ²_)i,+J0³š ²1)g+ 0 (otherwise also [ ²1)i,+ 0 ²_)g+J0, a contradiction) Due to the ordering this means that 7 # 7 # Moreover, from J ² it follows that [ œ$and œ G "ž ŸW /, which by the induction hypothesis implies that ŒŽ # [ ŒŽ # and c # c # From the reducedness property of uniqueness it follows that p An immediate consequence is the following Since the terminal is an RBDD for all variable orderings it is the only RBDD that is constantly true So in order to check whether an RBDD is constantly true it suffices to check whether it is the terminal which is definitely a constant time operation Similarly, RBDDs that are constantly false must be identical to the terminal In fact, to determine whether two Boolean functions are the same, it suffices to construct their RBDDs (in the same graph) and check whether the resulting nodes are the same!

3 BINARY DECISIN DIAGRAMS 14 º ( ` ~ The ordering of variables chosen when constructing an RBDD has a great impact on the size of the RBDD If we consider again the expression d"# _-# and construct an RBDD using the ordering 9 f9µ c 9µ _ the RBDD consists of w nodes (figure 6) and not x nodes as for the ordering n9 c 9 9? 7 (figure 3) s { { z s z s z s z s z@{ z@{ Figure 6: The RBDD for d"# _-# with variable ordering n9 9? d 9? 7 Exercise 31 Show how to express all operators from the if-then-else operator and the constants and Exercise 32 Draw the RBDDs for cl# 7&# n9 9 9? do9 7 9 7 and n9 co9 9 7 9 9? 7 7&# with orderings Exercise 33 Draw the RBDDs for d"# _-# with orderings o9 9? co9 _ and o9? co9 9 7 How does it compare with the example in figures 3 and 6? Based on the examples you have seen so far, what variable ordering would you recommend for constructing a small RBDD for d"# 7-# 7-# ED2D2D F F #? Exercise 34 Give an example of a sequence of RBDDs (Q ¹8 which induces exponentially bigger decision trees Ie, if ( has size º 8 # then the decision tree should have size # Exercise 35 Construct an RBDD of maximum size over six variables

4 CNSTRUCTING AND MANIPULATING RBDDS 15 Á  À À  `  5 6 4 7 2 3» ½¼ b ] &¾B - # var ŒŽ c ` Á Á À ` Á x à ` Á À x Figure 7: Representing an RBDD with ordering 9Ä 9? 9? The numbers inside the vertices are the identities used in the representation The numbers and are reserved for the terminal nodes The numbers to the right of the RBDD shows the index of the variables in the ordering The constants are assigned an index which is the number of variables in the ordering plus one (here Á ª À ) This makes some subsequent algorithms easier to present The lowand high-fields are unused for the terminal nodes 4 Constructing and Manipulating RBDDs In the previous section we saw how to construct an BDD from a Boolean expression by a simple recursive procedure The question arises now how do we construct a reduced BDD? ne way is to first construct an BDD and then proceed by reducing it Another more appealing approach, which we follow here, is to reduce the BDD during construction To describe how this is done we will need an explicit representation of RBDDs Nodes will be represented as numbers ` / with and reserved for the terminal nodes The variables in the ordering E9Å9 D2D2D 9Å( are represented by their indices ` / ' 8 The RBDD is stored in a table» Ƽ b ] &¾B - # which maps a node to its three attributes 7 # ], ŒŽ # ¾, and c # Figure 7 shows the representation of the RBDD from figure 3 (with the variable names changed to 9 9 9 ) 41 MK In order to ensure that the BDD being constructed is reduced, it is necessary to determine from a triple ] &¾B - # whether there exists a node with 7 # ] ŒŽ # ¾, and c # For this purpose we assume the presence of a table Ç ] &¾B - # ¼ b mapping triples ] &¾B - # of variable indices ], and nodes ¾B - to nodes The table Ç is the inverse of the table», ie, for variable nodes,» # È ] &¾B - #, if and only if, Ç ] ¾J & # The operations needed on the

4 CNSTRUCTING AND MANIPULATING RBDDS 16 Ç» MK) Ç 0 ] &¾B - # 1: if ¾ then return ¾ 2: else if member Ç ] &¾B - # then 3: return lookup Ç ] ¾J & # 4: else >É add» ] &¾B - # 5: insert Ç ] &¾B - # 6: ÊËdÌ2ÍÎÊ Ïe» Figure 8: The function MK) Ç 0 ] &¾B - # two tables are:» >¼ b ] &¾B - # init» # initialize» to contain only and >É add» ] &¾B - # allocate a new node with attributes ] &¾B - # 7 #$ ŒŽ #$ c # lookup the attributes of in» ] &¾B - # ¼ b init Ç # initialize Ç to be empty yé member Ç ] &¾B - # check if ] &¾B - # is in Ç >É lookup Ç ] &¾B - # find Ç ] ¾J & # insert Ç ] &¾B - # make ] ¾J & # map to in Ç We shall assume that all these operations can be performed in constant time, Ð will show how such a low complexity can be achieved» The function MK) Ç 0 # Section 5 ] ¾J & # (see figure 8) searches the table Ç for a node with variable index ] and low-, high-branches ¾B - and returns a matching node if one exists therwise it creates a new node, inserts it into Ç and returns the identity of it The running time of MK is Ð # due to the assumptions on the basic operations on» and Ç The BDD is ensured to be reduced if nodes are only created through the use of MK In describing MK and subsequent» algorithms, we make use of the notation ) Ç 0 to indicate that MK depends on the global data structures» and Ç, but we leave out the arguments when invoking it as part of other algorithms 42 BUILD The construction of an RBDD from a given Boolean expression proceeds as in the construction of an if-then-else normal form (INF) in section 2 An ordering of the variables Ñ9 D2D2D 9 ( is fixed Using the Shannon expansion b ) 2+J0W )*,+J0, a node for is constructed by a call to MK, after the nodes for )i,+j0 and )g+b0 have been constructed by recursion The algorithm is shown in figure 9 The call BUILD C ] # constructs an RBDD for a Boolean expression with variables in 4! «@- / /!( 6 It does so by first recursively constructing RBDDs [ and for )i,+ 0 and )g+ 0 in lines 4 and 5, and then proceeding to find the identity of the node for in line 6 Notice that if [ and are identical, or if there already is a node with the same ], [ and, no new node is created

4 CNSTRUCTING AND MANIPULATING RBDDS 17 (» BUILD) Ç 0 H # 1: function BUILD C ] # = 2: if ]ƒò 8 then 3: if is false then return 0 else return 1 4: else [ É BUILD H )*1+ 0h ]@ ª # 5: É BUILD H ) + 0h ] ª # 6: Ê Ë,ÌÍ Ê Ï MK ] [!# 7: end BUILD 8: 9: return BUILD H # Figure 9: Algorithm for building an RBDD from a Boolean expression using the ordering 9;Ó9 D2D2D 9ª( In a call BUILD C ] #, ] is the lowest index that any variable of can have Thus when the test ]ÒÔ8 succeeds, contains no variables and must be either constantly false or true An example of using BUILD to compute an RBDD is shown in figure 10 The running time of BUILD is bad It is easy to see that for a variable ordering with 8 variables there will always be generated on the order of ` calls 43 APPLY All the binary Boolean operators on RBDDs are implemented by the same general algorithm NÕ-Ö APPLY & -# that for two RBDDs computes the RBDD for the Boolean expression Bœ G Õ&ÖÑBœ' The construction of APPLY is based on the Shannon expansion (2): Eeb )g+0h )i,+0, bserve that for all Boolean operators op the following holds: eb - $# Õ-Ö>eb A A # >b Õ&ÖØ A Õ&ÖÑ A (4) If we start from the root of the two RBDDs we can construct the RBDD of the result by recursively constructing the low- and the high-branches and then form the new root from these Again, to ensure that the result is reduced, we create the node through a call to MK Moreover, to avoid an exponential blow-up of recursive calls, dynamic programming is used The algorithm is shown in figure 11 Dynamic programming is implemented using a table of results Ù Each entry ] BÚc# is either empty or contains the earlier computed result of APP ] BÚc# The algorithm distinguishes between four different cases, the first of them handles the situation where both arguments are terminal nodes, the remaining three handle the situations where at least one argument is a variable node If both and are terminal, a new terminal node is computed having the value of op applied to the two truth values (Recall, that terminal node is represented by a node with identity and similarly for )

4 CNSTRUCTING AND MANIPULATING RBDDS 18 ~ } b Û Û*ÜQÞ½ÜWâ'ãà"äJå èhâ d Û ÛÝÜßÞ>à!áNâ$ãà!äBå v â Û ÛÝÜßÞ r â$ãà"äjå èwâ BUILD Û Ûæà çqþ>à!ánâ$ãà!äbå r â c g e Û Û r Þ½ÜWâ$ãà!äBåŽèhâ f Û Û r Þeà!áNâ$ã\à!äJå v â e Û Û r Þ r â'ãà"äjå èhâ Û Û*ÜQÞ½ÜWâ'ã\ÜLå é â Û Û*ÜQÞ r â$ã\ü"å é â Û Û*ÜßÞ½Ü â$ã r å é â Û Û r Þ½Ü â$ã\ü"å é â Û Û*ÜcÞ r â$ã r å é â Û Û r Þ r â$ã\ü"å é â Û Û r Þ½ÜWâ/ã r å é â Û Û r Þ r â'ã r å é â a ƒ{ ~ ~ } b c d s { { { { { ~ } ~ } ~ } e f g Figure 10: Using BUILD on the expression $# (a) The tree of calls to BUILD (b) The RBDD after the call BUILD! Ñ #  # (c) After the call BUILD " Ñ #  # (d) After the call BUILD! ±$# 2 ` # (e) After the calls BUILD! ˆ # 2  # and BUILD " # 2  # (f) After the call BUILD! $# 2 ` # (g) The final result

4 CNSTRUCTING AND MANIPULATING RBDDS 19» APPLY) Ç 0 hõ&ö -# 1: init Ù # 2: 3: ê Í%ÏÎë7ÌìCí@Ï APP - # 4: ìcêù - &# š empty Ìî ËßÏeÊË,ÌÍÎÊ Ï±Ù - -# 5: Ëcïhð'ËñìCêo o 4 _6òÏÎóô 4 Õ-Ö 76oÌîÎËcÏ u É - -# 6: Ëcïhð'ËñìCê var "# var -# ÌîÎËcÏ 7: u É MK var "#- APP ŒŽ "#$ ŒŽ -#!#- APP c "#- c 8 Ëcïhð'ËñìCê var "#n9 var -# ÌîÎËcÏ 9 u É MK var "#- APP ŒŽ "#$ -#- APP c "#- -#!# õ 10: Ëcïhð'Ë var "# Ò var -# õ # 11: u É MK var -#- APP - ŒŽ -#!#- APP - c &#!#!# 12: Ù - -# É u 13: ÊË,ÌÍÎÊ Ï u 14: ËcÏÎó APP 15: 16: return APP - -#» Figure 11: The algorithm APPLY ) Ç 0 hõ&ö & # &#!#"# If at least one of and are non-terminal, we proceed according to the variable index If the nodes have the same index, the two low-branches are paired and APP recursively computed on them Similarly for the high-branches This corresponds exactly to the case shown in equation (4) If they have different indices, we proceed by pairing the node with lowest index with the low- and high-branches of the other This corresponds to the equation b $ -# op 5 b op op (5) which holds for all Since we have taken the index of the terminals to be one larger than the index of the non-terminals, the last two cases, 7 "#n9 7 -# and 7 "# ÒÔ 7 -#, take account of the situations where one of the nodes is a terminal Figure 12 shows an example of applying the algorithm on two small RBDDs Notice how pairs of nodes from the two RBDDs are combined and computed To analyze the complexity of APPLY we let denote the number of nodes that can be reached from in the RBDD Assume that Ù can be implemented with constant lookup and insertion times (See section 5 for details on how to achieve this) Due to the dynamic programming at most 2 _ calls to APPLY are generated Each call takes constant time The total running time is therefore Ð /2 _ # 44 RESTRICT The next operation we consider is the restriction of a RBDD That is, given a truth assignment, for example )i,+2 +ö 2+!0, we want to compute the RBDD for Bœ under this

4 CNSTRUCTING AND MANIPULATING RBDDS 20 ü ý 8 6 7 ø G ø 5 9 7 8 5 ø-ù 3 4 5 6 3 4 ø-ú 3 4 2 ø$û 2 2 0 1 0 1 1 0 8,5 ø G 3,2 2,2 0,2 5,3 0,0 4,0 6,3 2,0 0,2 0,3 0,0 0,1 0,4 0,2 7,4 5,4 3,0 2,0 0,1 0,2 4,2 2,2 ø ø ù ø-ú ø$û 1,1 0,0 0,1 0,0 1,0 0,0 Figure 12: An example of applying the algorithm APPLY for computing the conjunction of the two RBDDs shown at the top left The result is shown to the right Below the tree of arguments to the recursive calls of APP Dashed nodes indicate that the value of the node has previously been computed and is not recomputed due to the use of dynamic programming The solid ellipses show calls that finishes by a call to MK with the variable index indicated by the variables to the right of the tree

4 CNSTRUCTING AND MANIPULATING RBDDS 21» RESTRICT) Ç 0 BÚ1 y # = 1: function res # = 2: ìnê 7 # Ò Ú Ì2îÎËcÏeÊ Ë,ÌÍ Ê Ï> 3: ËßïNð/ËÓìCê 7 #o9ú Ìî ËcϱÊË,ÌÍ Ê_Ï MK 7 #$ res ŒŽ 4: ËßïNð/Ë (* 7 # Ú *) ìcêy ÌîÎËcÏeÊËdÌ2ÍÎÊ Ï res ŒŽ 5: ËßïNð/Ë (* 7 6: end res 7: return res # # Ú1 y Figure 13: The algorithm RESTRICT ) *) ÊË,ÌÍÎÊ Ï res c #"# #"#$ res c #!# #"#!#» Ç 0 Ú1 y # which computes an RBDD for Bœ ) Ú +7y 0 restriction, ie, find the RBDD for œ )i,+ 2+ö +!0 As an example consider the RBDD of figure 10(g) (repeated below to the left) representing the Boolean expression -# Restricting it with respect to the truth assignment )i,+ 0 yields an RBDD for -# It is constructed by replacing each occurrence of a node with label by its left branch yielding the RBDD at the right: x1 x2 x2 x1 x3 x3 1 0 1 0 The algorithm again uses MK to ensure that the resulting BDD is reduced Figure 13 shows the algorithm in the case where only singleton truth assignments ()*y$+ M 0, y 4 76 ) are allowed Intuitively, in computing RESTRICT BÚ1 y # we search for all nodes with 7 Ú and replace them by their low- or high-son depending on y Since this might force nodes above the point of replacemen to become equal, it is followed by a reduction (through the calls to MK) Due to the two recursive calls in line 3, the algorithm has an exponential running time, see exercise 47 for an improvement that reduces this to linear time 45 SATCUNT, ANYSAT, ALLSAT In this section we consider operations to examine the set of satisfying truth assignments of a node A truth assignment þ satisfies a node if Bœ )iþ 0 can be evaluated to using the truth tables of the Boolean operators Formally, the satisfying truth assignments is the set sat # : sat # ÿ4 þ ø G ø 3 œ )iþ 0 is true 6 ø G ø where 3 denotes the set of all truth assignments for variables 4 - / / "( 6, ie, functions from 4 $ $!( 4 6 to the truth values 3 76 The first algorithm, SATCUNT, computes the size of sat #, see figure 14 The algorithm exploits the following fact If is a node

4 CNSTRUCTING AND MANIPULATING RBDDS 22 ) ` œ» SATCUNT) 0 # 1: function count # 2: if then res É 3: else if then res É B œ' ^ 4: else res É ` "ž Ÿ œ' ^ 5: return res 6: end count 7: 8: return ` œ' ^ õ ;` count # œ' ^ õ count ŒŽ #"# œ' ^ õ count c Figure 14: An algorithm for determining the number of valid truth assignments Recall, that the variable index 7 of and in the RBDD representation is 8ˆ Ä when the ordering contains 8 variables (numbered through 8 ) This means that var # and var # always gives 8 ; #!# with variable index 7 # then two sets of truth assignments can make true The first set has 7 _ equal to, the other has 7 equal to For the first set, the number is found by finding the number of truth assignments count ŒŽ #"# making ŒŽ # true All variables between 7 # and 7 ŒŽ #!# in the ordering can be chosen arbitrarily, therefore in the case of B œ' ^ œ' ^ õ 7 being, a total of ` count ŒŽ #"# satisfying truth assignments exists To be efficient, dynamic programming should be applied in SATCUNT (see exercise 410) ANYSAT # 1: if then Error 2: else if 3: else if ŒŽ # 4: else return ) then return ) 0 then return ) œ' ¼ b ANYSAT c œ' ¼ b ANYSAT ŒŽ #!#J0 Figure 15: An algorithm for returning a satisfying truth-assignment The variables are assumed to be $ / /!( ordered in this way #"#J0 The next algorithm ANYSAT in figure 15 finds a satisfying truth assignment Some irrelevant variables present in the ordering might not appear in the result and they can be assigned any value whatsoever ANYSAT simply finds a path leading to 1 by a depth-first traversal, prefering somewhat arbitrarily low-edges over high-edges It is particularly simple due to the observation that if a node is not the terminal 0, it has at least one path leading to 1 The running time is clearly linear in the result ALLSAT in figure 16 finds all satisfying truth-assignments leaving out irrelevant variables from the ordering ALLSAT # finds all paths from a node to the terminal The running time is linear in the size of the result multiplied with the time to add the single assignments œ/ ¼ b 0 and ) œ' ¼ b 0 in front of a list of up to 8 elements However, the result can be exponentially large in œ, so the running time is the poor Ð 8 #