cs1022 computer programming & principles lecture 9.1 boolean algebra (1)
TRANSCRIPT
CS1022 Computer Programming &
Principles
Lecture 9.1Boolean Algebra (1)
Plan of lecture• Introduction• Boolean operations• Boolean algebra vs. logic• Boolean expressions• Laws of Boolean algebra• Boolean functions• Disjunctive normal form
2CS1022
IntroductionBoolean 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)
3CS1022
Boolean operations (1)• Boolean algebra uses set B 0, 1 of values• Operations: – Disjunction (),– Conjunction () and – Negation ()
4CS1022
0 10 0 01 0 1
0 10 0 11 1 1
1 00 1
Boolean operations (2)• If p and q are Boolean variables– “Variables” because they vary over set B 0, 1 – That is, p, q B
• We can build the following tables:
5CS1022
p q p q p q0 0 0 00 1 1 01 0 1 01 1 1 1
p p0 11 0
Boolean algebra vs. logicBoolean algebra is similar to propositional logic• Boolean variables p, q,... like propositions P, Q,...• Values B 0, 1 like F (false) and T (true)Operators are similar: • p q similar to P or Q• p q similar to P and Q• p similar to not PTables also called “truth tables” (as in logic)
6CS1022
• 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))
Boolean expressions
7CS1022
• 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
Equivalence of expressions
8CS1022
• 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
Laws of Boolean algebra (1)
9CS1022
Laws of Boolean algebra (2)
10CS1022
Commutative Laws
p q q pp q q p
Associativity Laws
p (q r) (p q) rp (q r) (p q) r
Distributive Laws
p (q r) (p q) (p r)p (q r) (p q) (p r)
Idempotent Laws
p p pp p p
Absorption Laws
p (p q) pp (p q) p
De Morgan’s Laws
(p q) p q(p q) p q
• Do they look familiar?– Logics, sets & Boolean algebra
are all related• Summary of correspondence:
Laws of Boolean algebra (3)
11CS1022
Logical operation
Set operation
Boolean operation
not or
and
• 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:
Laws of Boolean algebra (4)
12CS1022
• 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))
Laws of Boolean algebra (5)
13CS1022
• 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...
Laws of Boolean algebra (6)
14CS1022
• 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 (q q) 0 p from def. of
Laws of Boolean algebra (7)
15CS1022
• 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.
Boolean functions (1)
16CS1022
• Example: function minterm m(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 minterm m
Boolean functions (2)
17CS1022
p q r m0 0 0 00 0 1 00 1 0 00 1 1 11 0 0 01 0 1 01 1 0 01 1 1 0
• Any minterm m(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
Boolean functions (3)
18CS1022
p q r m0 1 1 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”
Disjunctive normal form (1)
19CS1022
• 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)
Disjunctive normal form (2)
20CS1022
p q r f0 0 0 00 0 1 10 1 0 00 1 1 01 0 0 01 0 1 11 1 0 11 1 1 1
p q r f0 0 0 00 0 1 10 1 0 00 1 1 01 0 0 01 0 1 11 1 0 11 1 1 1
p q r f0 0 0 00 0 1 10 1 0 00 1 1 01 0 0 01 0 1 11 1 0 11 1 1 1
p q r f0 0 0 00 0 1 10 1 0 00 1 1 01 0 0 01 0 1 11 1 0 11 1 1 1
p q r f0 0 0 00 0 1 10 1 0 00 1 1 01 0 0 01 0 1 11 1 0 11 1 1 1
p q r f0 0 0 00 0 1 10 1 0 00 1 1 01 0 0 01 0 1 11 1 0 11 1 1 1
• 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
Minimal set of operators
21CS1022
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
22CS1022