250 likes | 607 Views
CS1022 Computer Programming & Principles. Lecture 9.1 Boolean Algebra (1). Plan of lecture. Introduction Boolean operations Boolean algebra vs. logic Boolean expressions Laws of Boolean algebra Boolean functions Disjunctive normal form. Introduction. Boolean algebra
E N D
CS1022Computer Programming & Principles Lecture 9.1 Boolean Algebra (1)
Plan of lecture • Introduction • Boolean operations • Boolean algebra vs. logic • Boolean expressions • Laws of Boolean algebra • Boolean functions • Disjunctive normal form CS1022
Introduction Boolean algebra • Is a logical calculus • Structures and rules applied to logical symbols • Just like ordinary algebra is applied to numbers • Uses a two-valued set {0, 1} • Operations: conjunction, disjunction and negation • Is related with • Propositional logic and • Algebra of sets • Underpins logical circuits (electronic components) CS1022
Boolean operations (1) • Boolean algebra uses set B0, 1 of values • Operations: • Disjunction (), • Conjunction () and • Negation () CS1022
Boolean operations (2) • If p and q are Boolean variables • “Variables” because they vary over set B0, 1 • That is, p, q B • We can build the following tables: CS1022
Boolean algebra vs. logic Boolean algebra is similar to propositional logic • Boolean variables p, q,... like propositions P, Q,... • Values B0, 1 like F (false) and T (true) Operators are similar: • pqsimilar to P orQ • pq similar to P and Q • p similar to not P Tables also called “truth tables” (as in logic) CS1022
Boolean expressions • As in logic, we can build more complex constructs from basic variables and operators , and • Complex constructs are called Boolean expressions • A recursive definition for Boolean expressions: • All Boolean variables (p, q, r, s, etc.) are Boolean exprns. • If αand β are Boolean expressions then • α is a Boolean expression (and so is β) • (α β) is a Boolean expression • (α β) is a Boolean expression • Example: ((p q) (r s)) CS1022
Equivalence of expressions • Two Boolean expressions are equivalent if they have the same truth table • The final outcome of the expressions are the same, for any value the variables have • We can prove mechanically if any two expressions are equivalent or not • “Mechanic” means “a machine (computer) can do it” • No need to be creative, clever, etc. • An algorithm explains how to prove equivalence CS1022
Laws of Boolean algebra (1) • Some equivalences are very useful • They simplify expressions, making life easier • They allow us to manipulate expressions • Some equivalences are “laws of Boolean algebra” • We have, in addition to truth-tables, means to operate on expressions • Because equivalence is guaranteed we won’t need to worry about changing the meaning CS1022
Laws of Boolean algebra (2) CS1022
Laws of Boolean algebra (3) • Do they look familiar? • Logics, sets & Boolean algebra are all related • Summary of correspondence: CS1022
Laws of Boolean algebra (4) • Very important: • Laws shown with Boolean variables (p, q, r, etc.) • However, each Boolean variable stand for an expression • Wherever you see p, q, r, etc., you should think of “place holders” for any complex expression • A more “correct” way to represent e.g. idempotent law: CS1022
Laws of Boolean algebra (5) • What do we mean by “place holders”? • The laws are “templates” which can be used in many different situations • For example: ((p q) (q s)) ((p q) (q s)) ((p q) (q s)) ((p q) (q s)) ((p q) (q s)) ((p q) (q s)) CS1022
Laws of Boolean algebra (6) • We should be able to prove all laws • We know how to build truth tables (lectures 2.1 & 2.2) • Could you work out an algorithm to do this? • There is help at hand... CS1022
Laws of Boolean algebra (7) • Using the laws of Boolean algebra, show that • (p q) (p q) is equivalent to p • Solution: (p q) (p q) (p q) (p q) De Morgan’s (p q) (p q) since p p p (q q) Distr. Law p 0 since (qq) 0 p from def. of CS1022
Boolean functions (1) • A Boolean function of n variables p1, p2, ..., pn is • A function f : Bn B • Such that f(p1, p2, ..., pn) is a Boolean expression • Any Boolean expression can be represented in an equivalent standard format • The so-called disjunctive normal form • Format to make it easier to “process” expressions • Simpler, fewer operators, etc. CS1022
Boolean functions (2) • Example: function mintermm(p, q, r) • It has precisely one “1” in the final column of truth table • m(p, q, r) 1 if p 0, q 1 and r 1 • We can define m(p, q, r) p q r • The expression p q r is the product representation of the mintermm CS1022
Boolean functions (3) • Any mintermm(p1, p2, ..., pr) can be represented as a conjunction of the variables pi or their negations • Here’s how: • Look at row where m takes value 1 • If pi 1, then pi is in the product representation of m • If pi 0, then pi is in the product representation of m • We thus have m(p, q, r) p q r CS1022
Disjunctive normal form (1) • We can express any Boolean function uniquely as a disjunction (a sequence of “”) of minterms • This is the disjunctive normal form • The idea is simple: • Each minterm is a conjunction (sequence of “”) which, if all holds, then we have output (result) “1” • The disjunction of minterms lists all cases which, if at least one holds, then we have output (result) “1” • In other words: we list all cases when it should be “1” and say “at least one of these is 1” CS1022
Disjunctive normal form (2) • Find disjunctive normal form for (p q) (q r) • Let f (p q) (q r). Its truth table is as below • The minterms are (rows with f 1) • p q r • p q r • p q r • p q r • Disjunctive normal form is (p q r) (p q r) (p q r) (p q r) CS1022
Minimal set of operators • Any Boolean function can be represented with Boolean operators , , • This is a complete set of operators • “Complete” = “we can represent anything we need” • It is not minimal, though: • De Morgan’s (p q) p q, so (p q) (p q) • We can define “” in terms of , • Minimal set of Boolean operators: , or , • Actually, any two operators suffice • Minimality comes at a price: expressions become very complex and convoluted CS1022
Further reading • R. Haggarty. “Discrete Mathematics for Computing”. Pearson Education Ltd. 2002. (Chapter 9) • Wikipedia’s entry on Boolean algebra • Wikibooks entry on Boolean algebra CS1022