Download Document

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Boolean satisfiability problem wikipedia , lookup

Propositional formula wikipedia , lookup

Transcript
CS1022
Computer Programming &
Principles
Lecture 2
Boolean Algebra
Plan of lecture
• Simplifying Boolean expressions
• Karnaugh maps
• Logic circuits
CS1022
2
Simplifying Boolean expressions (1)
• Given a Boolean expression, can we find a “simpler”
equivalent expression?
– “Simpler” = “fewer symbols”
• Technique applied to disjunctive normal form (DNF)
– Even though it might be more complex than original
– Process: original expr.  DNF  simplified expr.
• Technique restricted to at most 3 Boolean variables
– Extension to more variables is possible
CS1022
3
Simplifying Boolean expressions (2)
• To simplify, let’s suppress “”
– Just like “” in algebra can be dropped (e.g., 2y = 2  y)
– For instance, the expression in disjunctive normal form
(p q r)  (p q  r)  (p  q r)
Will be represented as
pqr  pqr  pqr
• We can simplify the above expression as follows:
pqr  pqr  pqr  (prq  prq)  pqr Comm.& assoc. Laws
Distr. Law
 pr(q q)  pqr
 pr  pqr
Since q q = 1
CS1022
4
Simplifying Boolean expressions (3)
• Compare pr  pqr and pqr  pqr  pqr
– They are equivalent – all changes done via a “law”
– It is much simpler than original expression
• Very important: simplification can be automated
– Each step in the process is an operation
– Sequence of steps terminates at some point
• Notice:
– First step puts together two minterms which differed by
one symbol
– Second step reduced two terms to one
CS1022
5
Karnaugh maps (1)
• Simplification done via a Karnaugh map
– “Device” invented in the 1950s to help circuit design
– Uses a visual display to indicate which pairs of minterms
can be merged as a simpler expression
• For Boolean expressions with 3 variables (p, q and r)
– Karnaugh map is a table with 2 rows and 4 columns
– Columns labelled with all possible disjunctions of p and q
and their negations
pq pq pq pq
– Rows labelled with r and r
r
r
CS1022
6
Karnaugh maps (2)
• As we move from column to column precisely one
change occurs in the column label
• Cells correspond to 8 different minterms stemming
from 3 Boolean variables
• For a given Boolean expression, we write “1” in
each cell whose row/column expression appears
• For instance, pqr  pqr  pqr has map
pq pq pq pq
r
1 1
1
r
CS1022
7
Karnaugh maps (3)
• Required simplification suggested by “clusters” of 1s
– Shaded in green here
pq pq pq pq
r
1 1
1
r
• There is only one such cluster in the example
– Corresponds to terms we combined using the laws
CS1022
8
Karnaugh maps (3)
• Relabelling columns preserves adjacency
– Some clusters may be “hidden”
– NB relabelling must be consistent with requirement that
adjacent columns differ by one symbol only
• Example: Karnaugh map of pqr  pqr  pqr
pq
1
pq
pq
pq
1
r
1
r
– Relabelling columns produces an alternative Karnaugh
map and reveals a pair of adjacent minterms
pq pq pq pq
r
1
1
r 1
CS1022
9
Karnaugh maps (4)
• Hence,
pqr  pqr  pqr  pqr  (pqr  pqr)
 pqr  pr(q q)
 pqr  pr
pq pq pq pq
r
1 1
r 1
CS1022
10
Karnaugh maps (5)
Let’s simplify pqr  pqr  pqr  pqr  pqr
pq pq pq pq
• Karnaugh map:
r 1
1
1
1
r 1
• It contains a cluster of 4 (A) and a pair (B)
– There are no hidden pairs
– We must try relabelling exhaustively
CS1022
11
Karnaugh maps (6)
Cluster (A) corresponds to
pq pq pq pq
r 1
1
1
1
r 1
pqr  pqr  pqr  pqr (p  p)qr  (p  p)qr
 qr  qr
 q(r  r)
q
CS1022
12
Karnaugh maps (7)
Cluster (B) corresponds to
pq pq pq pq
r 1
1
1
1
r 1
pqr  pqr pr(q  q)
 pr
So, simplified expression is q  pr
CS1022
13
Logic circuits (1)
• One of the main application of Boolean algebra is
the design of binary devices
– They accept a (finite) number of inputs
– They produce a (finite) number of outputs
– Binary: only two possible values for each input & output
...
p1
p2
p3
pn
CS1022
?
...
• Circuits as “black boxes”:
q1
q2
q3
qm
14
Logic circuits (2)
• Devices are built of logic gates which perform basic
Boolean operations (, , and )
a
b
a
b
a
a
b
CS1022
ab
ab
a
(a  b)
OR gate
AND gate
NOT gate
NAND gate
15
Logic circuits (3)
• Interconnecting gates produces a logical circuit
• A logical circuit evaluates a Boolean expression
• Example: What is the final output of this circuit?
p
q
1
3
6
2
4
7
r
5
CS1022
16
Logic circuits (4)
• Example: What is the final output of this circuit?
pq pq pqr pqr pqr pqr  pqr
p
q
1
3
6
2
4
7
r
5
pqr  pqr  pqr
CS1022
17
Logic circuits (5)
• Solution: pqr  pqr  pqr
Gate
1
2
3
4
5
6
7
CS1022
Inputs
p, q
p, q
pq, r
pq, r
pq, r
pqr, pqr
pqr  pqr, pqr
Output
pq
pq
pqr
pqr
pqr
pqr  pqr
pqr  pqr  pqr
18
Logic circuits (6)
• Logic circuits can be simplified if we allow AND and
OR gates to have more than 2 inputs
• More dramatic simplifications w/ Karnaugh maps
– 2 clusters (one hidden)
• We can simplify things:
pq
r 1
r 1
pq
pq
pq
1
– pqr  pqr  pq(r  r)  pq
– pqr  pqr  (q  q)pr  pr
• That is, pqr  pqr  pqr  pq  pr
– We can further simplify this as p(q  r)
CS1022
19
Logic circuits (7)
• With p(q  r), we simplify previous circuit to obtain
p
q
r
CS1022
20
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
21