<<

CHAPTER 4 BOOLEAN LOGIC

The world divides into facts. Each fact can be the case or not the case, while everything else remains the same. Ludwig Wittgenstein

It can be true, it can be false. You'll be given the same reward. The Clash

4.1 BOOLEAN EXPRESSIONS Let us fix a count ably infinite alphabet of Boolean variables X = {Xl, X2, . . .}. These are variables that can take the two truth values true and false. We can combine Boolean variables using Boolea connectives such as V (logical or), 1\ (logical and) and -, (logical not), in much the same way as we combine real variables in algebra by +, x, and - to form arithmetic expressions. The definition of Boolean expressions follows. Definition 4.1: A Boolean expression can be anyone of (a) a Boolean variable, such as Xi, or (b) an expression of the form -,4Jl, where 4JI is a Boolean expression, or (c) an expression of the form (4Jl V 4J2), where 4JI and 4J2 are Boolean expressions, or (d) an expression of the form (4JI 1\ 4J2) where 4Jl and 4J2 are Boolean expressions. In case (b) the expression is called the negation of 4JI; in case (c), it is the disjunction of 4Jl and 4J2; and in case (d), it is the conjunction of 4JI and 4J2. An expression of the form Xi or 'Xi is called a li teral. D We have defined only the syntax of Boolean expressions, that is, their su­ perficial, apparent structure. What gives a logical expression life is its seman­ tics, its meaning. The semantics of Boolean expressions is relatively simple: These expressions can be true or false depending on whether the Boolean vari­ ables involved are true or false. Now, the definition of a Boolean expression is

73 74 Chapter 4: BOOLEAN LOGIC inductive, starting from the simplest case of a variable and combining simple expressions by connectives to form more complicated ones. Accordingly, much of our arguing about Boolean expressions will be inductive. Our definitions of properties of Boolean expressions must follow the same inductive path as the original definition, and our proofs will use induction on the structure of the expreSSIon. Definition 4.2: A truth assignment T is a mapping from a finite set X' of Boolean variables, X' c X, to the set of truth values {true, false}. Let c/J be a Boolean expression. We can define the set X (c/J) c X of the Boolean variables appearing in c/J inductively as follows: If c/J is a Boolean variable Xi, then X(c/J) = {Xi}. If c/J == -,c/JI, then X(c/J) == X(c/Jl)' If c/J == (c/JI V c/J2), or if c/J == (c/JI/\ c/J2), then X(c/J) = X(c/JI) U X(c/J2). Now let T-be a truth assignment defined on a set X' of Boolean variables such that X (c/J) c X' ; we call such a truth assignment appropriate to c/J. Suppose that T is appropriate to c/J. We define next what it means for T to satisfy c/J, written T F c/J. If c/J is a variable Xi E X (c/J), then T F c/J if T(Xi) = true. If c/J = .c/JI, then T F c/J if T c/JI (that is, if it is not the case that T F c/JI). If c/J = (c/JI V c/J2), then T F c/J if either T F c/JI or T F c/J2' Finally, if c/J == (c/JI /\ c/J2), T F c/J if both T F c/JI and T F c/J2 hold. D Example 4.1: Consider the Boolean expression c/J = (('XI V X2) /\ X3). An example of an appropriate truth assignment is the one that has T(XI) == T(X3) = true, and T(X2) = false. Does T F c/J? Clearly T F X3. However, the definition of satisfaction of conjunctions requires that ('XI V X2) also be satisfied by T. Is it? To begin with, T F Xl, and thus T V= ,XI. Also, T V= X2. It follows that T V= ('XI V X2), because it is not the case that one of the disjuncts is .satisfied. Finally, T V= c/J, because it fails to satisfy one of its conjuncts-namely ('Xl V X2). D We shall find it convenient to use two more Boolean connectives: (c/JI => c/J2) is a shorthand for (.c/JI V c/J2); and (c/JI {::} c/J2) is a shorthand for (( c/JI => c/J2) /\ (c/J2 => c/J I ) ) . We say that two expressions c/JI, c/J2 are equivalent, written c/JI = c/J2 if for any truth assignment T appropriate to both of them, T F c/JI if and only if T F c/J2 (this is the same as saying that, for any appropriate T, T F (c/JI {::} c/J2), see Problem 4.4.2). If two Boolean expressions are equivalent then they can be considered as different representations of one and the same object, and can be used interchangeably. Boolean connectives possess some basic useful p operties, such as commutativity and associativity, very much like arithmetic operations. We summarize them next. Proposition 4.1: Let c/JI, c/J2, and c/J3 be arbitrary Boolean expressions. Then: ( 1 ) (c/J I V c/J2) - (c/J2 V c/J I ) . (2) (c/JI /\ c/J2) = (c/J2 /\ c/JI). 4.1 Boolean Expressions 75

(3) -,-'c/Jl = c/JI. (4) ((c/JI V c/J2) V c/J3) = (c/JI V (c/J2 V c/J3)). (5) (( c/JI /\ c/J2) /\ c/J3) = (c/Jl /\ (c/J2 /\ c/J3)). (6) ((c/JI/\ c/J2) V c/J3) = ((c/JI V c/J3) /\ (c/J2 V c/J3))' (7) (( c/JI V c/J2) /\ c/J3) = (( c/JI /\ c/J3) V (c/J2 /\ c/J3))' (8) -,( c/JI V c/J2) = (-,c/JI /\ -,c/J2). (9) -,( c/JI /\ c/J2) = (-'c/Jl V -,c/J2). (10) c/JI V c/JI = c/JI. (11) c/JI/\ c/JI - c/JI'. D The commutativity properties (1) and (2) are direct consequences of the symmetry in the definition of V and /\, and property (3) follows directly from the definition of -'. Properties (4) and (5), the associativity properties of V and /\, are also immediate consequences of the definition. For the proofs of properties (6) and (7) (the distributive laws for /\ and V), of (8) and (9) (De Morgan's laws), and of (10) and (11) (the idempotency of Boolean expressions) as well as for an introduction to the truth table method, a general technique for establishing such facts, see Problem 4.4.2. It is worth noting the complete symmetry, or "duality," between V and /\ apparent in properties (6) through (9). Duality in Boolean expressions is much stronger than in the arithmetic case ('Yhere, for example, addition fails to distribute over multiplication). Proposition 4.1 allows us to use henceforth a simplified notation when rep­ resenting Boolean expressions. We shall omit parentheses when they separate binary connectives of the same kind (V or /\). That is, we shall write expres­ sion like (((Xl V -'X3) V X2) V X4 V-(X2 V xs)) as (Xl V -'X3 V X2 V X4 V X2 V xs), allowing for "long disjunctions" and conjunctions. Notice that, using commu­ tativity and idempotency, we can guarantee that long disjunctions and con­ junctions involve distinct expressions: We can rewrite the disjunction above as (Xl V -'X3 V X2 V X4 V xs). We shall occasionally use a mathematical notation that parallels the E and TI notation in algebra: I\ l c/Ji stands for (c/JI/\c/J2/\.. ./\c/Jn), and similarly V =l c/Ji stands for (c/JI V c/J2 V . .'. V c/Jn). Furthermore, using these properties we shall establish that every Boolean expression can be rewritten into an equivalent one in a convenient specialized style. In particular, a Boolean expression c/J is in conjunctive normal form if c/J = I\ =l C i , where n > 1, and each of the Cjs is the disjunction of one or more literals. The Cjs are called the clauses of the expression in conjunctive normal form. Symmetrically, we say that an expression c/J is in disjunctive normal form if c/J = V =l D i where n > 1, and each of the Djs is the conjunction of one or more literals. The Djs are called the implicants of the e pression in disjunctive normal form. Theorem 4.1: Every Boolean expression is equivalent to one in conjunctive normal form, and to one in disjunctive normal form. 76 Chapter 4: BOOLEAN LOGIC

Proof: By induction on the structure of c/J. If c/J = x j, a single variable, the statement is trivially true. If c/J = .c/JI, suppose that, by induction, we have turned c/JI into disjunctive normal form with implicants Dj, j = 1,..., n. Then, by de Morgan's laws (properties (8) and (9) in Proposition 4.1), c/J is the con­ junction of the .Dj's. But each .D j is, by another application of de Morgan's law, the disjunction of literals (the negations of the literals appearing in D j ). Similarly for the disjunctive normal form of c/J, starting from the conjunctive normal form of c/JI. Suppose now that c/J = (c/JI Vc/J2). For disjunctive normal form, the induction step is trivial: If both c/JI and c/J2 are in disjunctive normal form, then so is c/JI V c/J2. To put c/J into conjunctive normal form, suppose that c/JI and c/J2 are both already in conjunctive normal form, and let {Dli : i = 1,... ,nl} and {D 2j : j = 1,..., n2} be the two sets of clauses. Then it is easy to see that the conjunction of the following set of nl . n2 clauses is equivalent to c/J: {(D li V D 2j ) : i = 1, . . . , nl, j = 1, . . . , n2}. The case c/J = (c/JII\c/J2) is completely symmetric. D The construction in the proof of Theorem 4.1 appears algorithmic enough, and in fact in a promising, polynomial way: The hardest construction in the induction step is the one with the nl . n2 clauses, which still can be carried out in time quadratic in the length of the inductively assumed conjunctive normal forms. However, this polynomial appearance is deceiving: Repeated squaring due to a long disjunction can lead to resulting normal forms that are exponential in the size of the original expressions (see Problem 4.4.5; naturally, exponential is the worst that can happen). In fact, we can assume that our normal forms are a little more standardized: There are no repeated clauses or implicants (if there are, they can be omitted by Proposition 4.1, parts (10) and (11)), and there are no repeated literals in any clause or implicant (same reason). 4.2 SATISFIABILITY AND VALIDITY We say that a Boolean expression c/J is satisfiable if there is a truth assignment T appropriate to it such that T F c/J. We say that c/J is valid or a tautology if T F c/J for all T appropriate to c/J. Thus, a valid expression must be satisfiable (and an unsatisfiable expression cannot be valid). If c/J. is valid, we write F c/J, with no mention of a truth assignment T, since any T would do. Furthermore, suppose that a Boolean expression c/J is unsatisfiab e, and consider .c/J. Obviously, for any truth assignment T appropriate to c/J, T .c/J, and thus T F .c/J: Proposition 4.2: A Boolean expression is unsatisfiable if and only if its nega­ tion is valid. D In other words, the universe of all Boolean expressions is as shown in Figure 4.1, where negation can be thought of as a "flipping" of the figure around its vertical axis of symmetry. 4.2 Satisfiability and Validity 77

UNSAT­ ISFIABLE

Figure 4-1. The geography of all Boolean expressions.

Example 4.2: Expression ((Xl V 'X2) /\ 'Xl) is satisfiable, satisfied by T(XI) == T(X2) = false. On the other hand, expression

4J = ((Xl V X2 V X3) 1\ (Xl V ,X2) /\ (X2 V 'X3) /\ (X3 V 'Xl) /\ (-:XI V 'X2 V,X3)) is not satisfiable. To see this, we first notice that it is in conjunctive normal form, and so a satisfying truth assignment has to satisfy all clauses. The first clause requires that one of the three values T(XI), T(X2), T(X3) is true. It is easy to see that the next three clauses require that all three values be the same. Finally, the last clause requires that one of the three valu s be false. It follows that 4J cannot be satisfied by any truth assignment. Once we have established this, we also know that .4J == (('Xl /\ ,X2 1\ ,X3) V ('Xl /\ X2) V (,X2 /\ X3) V ('X3 /\ Xl) V (Xl /\ X2 /\ X3)) is valid. D Satisfiability and validity are important properties of Boolean expressions, so we shall study in great detail the corresponding computational problems. A Boolean expression can be represented for processing by an algorithm as a string over an alphabet that contains the symbols X, 0, and 1 (useful for writing variable names with indices in binary), and also the symbols (, ), V, " /\ required by the syntax of Boolean expressions. The length of a Boolean expression is the length of the corresponding string. SATISFIABILITY (or SAT, for short) then is the following problem: Given a Boolean expression 4J in conjunctive normal form, is it satisfiable? We require that the expression be given in conjunctive 78 Chapter 4: BOOLEAN LOGIC normal form for two reasons: First, we know that all expressions can in prin­ ciple be so represented. And second, this special form of satisfiability seems to capture the intricacy of the whole problem (as Example 4.2 perhaps indicates). It is of interest to notice immediately that SAT can be solved in O(n 2 2 n ) time by an exhaustive algorithm that tries all possible combinations of truth values for the variables that appear in the expression, and reports "yes" if one of them satisfies it, and "no" otherwise. Besides, SAT can be very easily solved by a nondeterministic polynomial algorithm, one that guesses the satisfying truth assignment and checks that it indeed satisfies all clauses; hence SAT is in NP. As with another important member of NP, TSP (D), presently we do not know whether SAT is in P (and we strongly suspect that it is not). Horn Clauses Still, there is an interesting special case of SAT that can be solved quite easily. We say that a clause is a Horn clause if it has at most one positive literal. That is, all its literals, except possibly for one, are negations of variables. The following clauses are therefore Horn: (-'X2 V X3), (-'XI V -'X2 V -'X3 V -'X4), and (Xl)' Of these clauses, the second is a purely negative clause (it has no positive literals), while the rest do have a positive literal, and are called implications. They are called so because they can be rewritten as ((XI/\ X2 /\ ... /\ x m ) => Y) -where y is the positive literal. For example, the two implications among the three clauses above can be recast as follows: (X2 => X3), and (true => Xl) (in the last clause, the conjunction of no variables was taken to be the "expression" true) . Are all these clauses satisfiable? There is an efficient algorithm for testing whether they are, based on the implicational form of Horn clauses. To make the description of the algorithm clear, it is better to consider a truth assignment not as a function from the variables to {true, false}, but rather as a set T of those variables that are true. We wish to determine whether an expression cp, the conjunction of Horn clauses, is satisfiable. Initially, we only consider the implications of cp. The algorithm builds a satisfying truth assignment of this part of cp. Initially, T :== 0; that is, all variables are false. We then repeat the following step, until all implications are satisfied: Pick any unsatisfied implication ((Xl/\X2/\. . ./\x m ) => y) (that is, a clause in which all the XiS are true and y is false), and add y to T (make it true). This algorithm will terminate, since T gets bigger at each step. Also, the truth assignment obtained must satisfy all implications in cp, since this is the only way for the algorithm to end. Finally, suppose that another truth assignment T' also satisfies all implications in cp; we shall show that T C T'. Because, if not, consider the first time during the execution of the algorithm at which T ceased being a subset of T': The clause that caused this insertion to 4.3 Boolean Functions and Circuits 79

T cannot be satisfied by T'. We can now determine the satisfiability of the whole expression cp. We claim that cp is satisnable if and only if the truth assignment T obtained by the algorithm just explained satisnes cp. For suppose that there is a purely negative clause of cp that is not satisfied by T-say ('XI V ,X2 V . . . V,xm). Thus {Xl, . . . , Xm} C T. It follows that no superset of T can satisfy this clause, and we know that all truth assignments that satisfy cp are supersets of T. Since the procedure outlined can obviously be carried out in polynomial time, we have proved the following result (where by HORNSAT we denote the satisfiability problem in the special case of Horn clauses; this is one of many special cases and variarits-of SAT that we shall encounter in this book). Theorem 4.2: HORNSAT is in P. D

4.3 BOOLEAN FUNCTIONS AND CIRCUITS Definition 4.3: An n-ary is a function f {true, false} n {true, false}. For example, V, /\, =>, and {:} can be thought of as four of the sixteen possible binary Boolean functions, since they map pairs of truth values (those of the constituent Boolean expressions) to {true, false}. . is a unary Boolean function (the only other ones are the constant functions and the identity function). More generally, any Boolean expression cp can be thought of as an n-ary Boolean function f, where n == IX(cp)l, since, for any truth assignment T of the variables involved in cp, a truth value of cp is defined: true if T F cp, and false if T cp. Formally, we say that Boolean expression cp with variables Xl, . . . , X n expresses the n-ary Boolean function f if, for any n-tuple of truth values t == (tl,... , t n ), f(t) is true if T F cp, and f(t) is false if T cp, where T(Xi) == t i for i == 1,... ,n. D So, every Boolean expression expresses some Boolean function. The con­ verse is perhaps a little more interesting. Proposition 4.3: Any n-ary Boolean function f can be expressed as a Boolean expression cp! involving variables Xl, . . . , Xn. Proof: Let F be the subset of {true, false}n consisting of all n-tuples of truth values such that make f true. For each t == (tl,..., t n ) E F, let Dt be the conjunction of all variables Xi with ti == true, with all negations of variables 'Xi such that t i == false. Finally, the required expression is cp! == VtEF Dt (notice that it is already in disjunctive normal form). It is easy to see that, for any truth assignment T appropriate to cp, T F cp! if and only if f(t) == true, where ti == T(Xi). D The expression produced in the proof of Proposition 4.3 has length (number of symbols needed to represent it) O(n 2 2 n ). Although many interesting Boolean functions can be represented by quite short expressions, it can be shown that in 80 Chapter 4: BOOLEAN LOGIC

their great majority, they cannot be (see Theorem 4.3 for a stronger statement). Definition 4.4: There is a potentially more economical way than expressions for representing Boolean functions-namely Boolean circuits. A Boolean circuit is a graph C = (V, E), where the nodes in V = {I,..., n} are called the gates of C. Graph C has a rather special structure. First, there are no cycles in the graph, so we can assume that all edges are of the form (i, j), where i < j (recall Problem 1.4.4). All nodes in the graph have indegree (number of incoming edges) equal to 0, 1, or 2. Also, each gate i E V has a sort s( i) associated with it, where s( i) E {true, false, V, /\, -,} U {Xl, X2, . . .}. If s( i) E {true, false} U {Xl, X2, . . .}, then the indegree of i is 0, that is, i must have no incoming edges. Gates with no incoming edges are called the inputs of C. If s(i) = -', then i has indegree one. If s(i) E {V, /\}, then the indegree of i must be two. Finally, node n (the largest numbered gate in the circuit, which necessarily has no outgoing edges) is called the output gate of the circuit. (In fact, we shall be considering circuits that have several outputs, thus computing several functions simultaneously. In this case, any gate with no outgoing edges will be considered an output.) This concludes our definition of the syntax of circuits. The semantics of circuits specifies a truth value for each appropriate truth assignment. We let X (C) be the set of all Boolean variables that appear in the circuit C (that is, X(C) = {x EX: s(i) = X for some gate i of C}). We say that a truth assignment T is appropriate for C if it is defined for all variables in X ( C). Given such a T, the truth value of gate i E V, T(i), is defined, by induction on i, as follows: If s( i) = true then T( i) = true, and similarly if s( i) = false. If s(i) E X, then T(i) = T(s(i)). If now s(i) = -', then there is a unique gate j < i such that (j, i) E E. By induction, we know T(j), and then T(i) is true if T(j) = false, and vice versa. If s( i) = V, then there are two edges (j, i) and (j', i) entering i. T( i) is then true if and only if at least one of T(j), T(j') is true. If s( i) = /\, then T( i) is true if and only if both T(j) and T(j') are true, where (j, i) and (j', i) are the incoming edges. Finally, the value of the circuit, T( C), is T( n), where n is the output gate. D Example 4.3: Figure 4.2(a) shows a circuit. The circuit has no inputs of sort true or false, and thus it can be thought of as representing a Boolean expression cp = ((Xl V (XI/\ X2)) V ((Xl /\ X2) /\ -'(X2 V X3))), also shown in the figure. Conversely, given a Boolean expression cp, there is a simple way to construct a circuit C4> such that, for any T appropriate to both, T( C4» = true if and only if T F cp. The construction follows the inductive definition of cp, and builds a new gate i for each sub expression encountered. The circuit C4> is shown in Figure 4.2(b). Notice the difference between Figures 4.2(a) and 4.2(b). The "standard" circuit C4> is larger, because it does not take advantage of 4.3 Boolean Functions and Circuits 81

(X3 /\ -'((XI V X2) /\ ('Xl V ,X2))) V (,X3 /\ (Xl V X2) /\ ('Xl V,X2))

(a) (b) Figure 4-2. Two circuits.

"shared subexpressions." It is the possibility of such shared sub expressions (gates with out-degree greater than one) that make circuits more economical than expressions in representing Boolean functions. 0 Example 4.4: Figure 4.3 shows a variable-free circuit C, that is, a circuit without gates of sorts in X. Its truth value T( C) is independent of the truth assignment T. In this example, it happens to be false. D There is an interesting computational problem related to circuits, called CIRCUIT SAT. Given a circuit C, is there a truth assignment T appropriate to C such that T( C) = true? It is easy to argue that CIRCUIT SAT is computa­ tionally equivalent to SAT, and thus presumably very hard; this computational equivalence, or reduction, will be shown formally in Chapter 8 Consider, how­ ever, the same problem for circuits with no variable gates (Figure 4.3). This problem, known as CIRCUIT VALUE, obviously has a polynomial-time algorithm: 82 Chapter 4: BOOLEAN LOGIC

Figure 4-3. A variable-free circuit.

Compute the values of all gates in numerical order, as in the definition of the value of a circuit; no truth assignment is required. The CIRCUIT VALUE problem is another one of our fundamental computational problems related to logic. In analogy with Boolean functions "expressed" by Boolean expressions, Boolean circuits "compute" Boolean functions. For example, both circuits in Figure 4.2 compute the parity of Xl, X2, and X3 (true if an odd number of the variables is true, and false otherwise). Formally, we say that Boolean circuit C with variables Xl,. . . , X n computes the n-ary Boolean function f if, for any n­ tuple of truth values t = (tl, . . . , t n ), f (t) = T( C), where the truth assignment T is defined like this: T(Xi) = ti for i = 1,... ,n. Clearly, any n-ary Boolean function can be computed by a Boolean cir­ cuit (since it can be expressed as a Boolean expression first). The interesting question is, how large does the circuit need to be, as a function of n? Theorem 4.3: For any n > 2 there is an n-ary Boolean function f such that no Boolean circuit with : or fewer gates can compute it. Proof: Suppose, for the sake of contradiction, that for some n > 2 all n-ary Boolean functions can be computed by circuits with m = : or fewer gates. There are 2 2n different n-ary Boolean functions. But how many circuits with m or fewer gates are there? The precise answer is hard to compute, but it is easy to come up with an overestimate: Each circuit is completely defined once we choose two things for each gate: Its sort, and the gates that feed into it. Thus, for each of the m gates we have at most (n + 5) . m 2 choices, or ((n + 5) . m2)m choices in toto. Many of these choices will result in illegal circuits, and several will be guises of the same circuit, but certainly the number of circuits with n variables and m or fewer gates is no larger than ((n+5) .m2)m. Now it is easy to see that this overestimate on the number of circuits with n 4.3 Boolean Functions and Circuits 83

variables and m or fewer gates is less than the number of Boolean functions on n variables. To see why, just take the logarithms (in base 2) of the two quantities, n log .!!L..2 remembering that m == n : The results are 2 n (1 - 2 +5 ) and 2 n , respectively; and the atter is clearly larger, if we recall that n > 2. So, having assumed that each Boolean function on n variables is computed by a circuit with at most m gates, we must conclude that two different n-ary Boolean functions f and f' are computed by the same circuit, which is of course absurd. D One of the major frustrations of complexity theory is this: Although we know that many exponentially difficult Boolean functions exist (in fact, most Boolean functions with n inputs are exponentially difficult, see Problem 4.4.14), nobody has been able to come up with a natural family of Boolean functions that requires more than a linear number of gates to compute!