COMP11120: Mathematical Techniques for Computer Science

COMP11120: Mathematical Techniques
for Computer Science
Graham Gough
[email protected]
School of Computer Science
University of Manchester
These notes are based heavily on those created for earlier versions of
this course by Harold Simmons, Bill Mitchell and Peter Aczel. Any
mistakes which remain are my responsibility, please let me know of
any that you find.
Discrete Structures
COMP11120
1 Discrete Structures
1.1
Introduction
The title of this section “Discrete Structures” reflects the fact that most, but not all, structures
that we meet in the course are discrete (as opposed to continuous) objects. Of course, the
word ‘structure’ is not used in the informal everyday sense, but in a technical sense. One of the
aims of this course is to explain this technical meaning, partly through the study of particular
examples.
What has this got to do with computing?
What is computing?
We are interested in handling data. This data will come in many different forms, such as
different kinds of numbers and collections of numbers, various kinds of texts, etc. This data
will be extracted from many different places and there will be many connections between the
data. All this is organised into a structure (or data structure) which might be very complicated
but usually will be built from simple components.
We want to combine, change, update, and generally move around data. This is done using
various algorithms which describe how the data is manipulated. These algorithms are implemented in a programming language to produce a program which is then executed on a machine.
This implementation will usually include a representation of parts of the original data structure.
This activity has many different facets and phases, and in the large is very complex. It is
organised into several sub-activities which are often carried out by different people.
In this course we are concerned with the ideas and techniques which help us to describe,
understand and design algorithms and programs and, more importantly, prove the correctness
of these algorithms and programs. Other facets of the computing activity are dealt with in
other courses. You will find that many of the things you learn in this course help you in some
unexpected places.
ture. Each attribute has a fixed type which tells us which sort the value is in (if the attribute is
a value) or which sorts the attribute connects (if it is an operation or relation).
These notions will be explained and illustrated as the module progresses.
Some basic examples
Structures are usually built from a small selection of basic components. Let’s look at some of
these.
Carriers
Each of
N
Z
Q
R
:−
:−
:−
:−
the set of natural numbers
the set of integers
the set of rational numbers
the set of real numbers
are used as carriers, often when things have to be measured using some kind of number. These
four sets are infinite. Typical elements of these sets are:
N
Z
Q
R
: − 0, 1, 2, · · · , but not − 1, 1/2, 3.45 etc
: − 0, 1, −1, 2, −2, · · · but not 1/2, 3.45 etc
: − m/n for integers m, n ∈ Z with n 6= 0. 1/2, 87/23, −34/2, 57/1
√ etc.
: − all finite and infinite decimal numbers ; e.g.3.45, 3.333, π, 2, · · · etc
The set
Char
of characters is the set of all symbols appearing on a keyboard (i.e. lower and upper case letters,
digits, punctuation and various other symbols including the blank symbol). This set is finite
and on its own is not very interesting. We use it to form the set
String
of all strings of characters. This will be needed for text processing. Each string has a length,
which is a natural number. In particular the characters can be viewed as the strings of length 1
and there is the unique string of length 0, called the empty string.
A small but very important carrier is the set
Bool = {T, F}
Some terminology
of truth values. This supports the logic of various activities. The two truth values are written
true and false in SML.
A structure consists of certain sets and:
• some elements of these sets,
Operations
• some operations on these sets,
• some relations on these sets.
The sets are called the sorts or carriers of the structure and their elements are sometimes called
values. The distinguished values, operations, and relations are called the attributes of the struc1
Each of N, Z, Q, R carries the two operations of addition and multiplication. These are binary
operations (for they combine two elements to form a third element). In detail, for each of these
sets A and elements x, y ∈ A, we can form the sum and product
x+y ,
x×y
2
COMP11120: 1
Discrete Structures
of these elements in A. This produces two operations each of type
>A
A, A
i.e. a binary operation which consumes two elements of A, combines them, and delivers a third
element of A.
As we move from N to Z to Q to R, more arithmetical facilities become available. For
example, given m, n ∈ N it is not always possible to solve
A larger example
Most structures that are used in computing have many components. Let’s look at how one of
these structures can be built.
We take the sets
Z , R , Char , String , Bool
as the five carriers furnished with the attributes already mentioned. We also use various other
connecting attributes. For instance we may want the floor function
m = n+x
(for x) in N, but it is possible to solve this in Z. We usually denote the solution of this equation
by m − n. In other words, Z (and Q, R) carries the binary operation of subtraction.
The two sets Char and String support various operations. For instance concatenation of
strings is a binary operation of type
String, String
> String.
Attaching a character to the front of a string is an operation of type
Char, String
and or implies
T
F
T
T
F
F
F
T
T
F
T
F
T
F
F
F
T
T
T
F
T
F
T
T
Note that and, or, implies are binary but not is unary (sometimes called singulary).
Z
Each of N, Z, Q, R carries the binary comparison relations ≤ and <. The set String carries at
least three comparison relations, i.e.
· is an initial section of ·
· is a section of ·
· is embedded in ·
There are also mixed relations such as
· occurs in ·
which compares a character with a string.
All relations return values in Bool.
3
>R
whose use in programming languages is often indicated by
> n.0
i.e. the decimal point is used to indicate that the integer n should be treated as a real number.
Two operations with strings are the length function
len : String
>Z
and the occurrence function
occur : Char , String
> Bool
which test whether of not a given character occurs in a given string. Two useful attributes on
integers are the operations
quo : Z , Z
Relations
>Z
which converts a real number x into the “nearest” integer bxc. In the other direction there are
times when we want to consider an integer as a real number. This is achieved by the insertion
n
> String.
This is an example of an operation of a mixed selection of arguments.
The set Bool carries several boolean connectives such as not, and, or, implies. Because
Bool is finite these can be described using a truth table.
not
b·c : R
>Z ,
rem : Z , Z
>Z
which, when supplied with integers x, y will deliver the quotient and remainder obtained by
dividing x by y (as integers). In programming languages these are often written DIV and MOD.
There are several other simple attributes which we will probably need at some point. Most
programming languages have facilities for using about a dozen or so built in attributes. More
complicated attributes are built as they are needed.
1.2 A structure for the integers
There are many places where we need to perform measurements using one of several kinds of
numbers: natural numbers, integers, rational numbers, real numbers, . . . . Of these different
kinds of numbers the most basic are the integers. We look at some properties of these by
focusing on a particular simple structure for the integers and an associated formal language.
4
COMP11120: 1
Discrete Structures
The basic data
• ~ is commutative if and only if
Recall that the set Z of all integers
a1 ~ a2 = a2 ~ a1 for all a1 , a2 ∈ A
. . . , −3, −2, −1, 0, 1, 2, 3, . . .
• ~ is associative if and only if
includes the subset N of all natural numbers
(a1 ~ a2 ) ~ a3 = a1 ~ (a2 ~ a3 ) for all a1 , a2 , a3 ∈ A
0, 1, 2, 3, . . .
together with the negative integers. We will be concerned with three basic binary operations on
the carrier set Z:addition
multiplication
,
subtraction.
,
The standard notation for these is
+
×
,
• For all integers x, y, z,
−
,
used as infix operations, i.e. if x, y are arbitrary members of Z (arbitrary integers) then
x+y
,
x×y ,
x−y
is the
sum
,
product ,
difference
as an abbreviation for
x×y
and in some circumstances slightly different notations are used. In particular, in programming
languages x ∗ y is usually used.
These particular operations illustrate two properties which an arbitrary binary operation
may or may not have.
Both + and × are commutative but − is not.
• For all integers x, y both,
x+y = y+x
and
xy = yx.
Commutativity of a binary operation ensures that for most purposes the order in which the two
arguments are consumed is irrelevant.
Both + and × are associative but − is not.
• For all integers x, y, z both (x + y) + z = x + (y + z) ,
(xy)z = x(yz).
• There are integers x, y, z with (x − y) − z 6= x − (y − z).
Associativity ensures that for most purposes brackets are not needed to punctuate an expression.
For instance, we can make sense of
xyz
since for both ways of inserting brackets the resulting values are equal.
The definitions of commutativity and associativity are as follows. Suppose ~ is a binary
operator on a set A, then
5
This is called the distributive law.
The numbers 0 and 1 play special roles.
• For all integers x,
x + 0 = x = 0 + x.
• For all integers x,
x × 1 = x = 1 × x.
• the carrier Z
• the two distinguished elements 0,1
• the three distinguished binary operations +,×, −.
At a later stage it may be necessary to add further attributes (such as exponentiation, the ordering relation, etc), but for the time being we use just this basic data.
An associated language
• There are integers x, y with x − y 6= y − x.
x+y+z ,
x(y + z) = xy + xz.
In other words, 0 and 1 are neutral elements for addition and multiplication, respectively.
The basic structure Z of integers, that we will now be concerned with, consists of
of x and y (in that order). It is conventional to write
xy
Commutativity and associativity do not always go together. For instance, multiplication of
square matrices is associative but not commutative. Taking the average of two real numbers is
commutative but not associative.
There is an important identity connecting the two operations + and ×.
Associated with this structure there is a formal language which is used to talk about Z . This
language is built up from
• a stock of identifiers
• two constants 0, 1
• three operation symbols +, ×, −
• punctuation symbols ( and ).
Certain strings of these atomic symbols form the arithmetical expressions of our formal
language. To describe these it is convenient to have a convention where
x, y, z, . . .
a, b, c, . . .
range over identifiers
range over expressions.
6
COMP11120: 1
Discrete Structures
This convention means that, unless we say otherwise, whenever we use
‘x’
or
‘y’
or
‘z’
or
Consider an expression
a ::= (b • c)
...
this should be taken as an identifier of the formal language. Similarly, whenever we use
‘a’
or
‘b’
or
‘c’
or
...
this should be taken as an expression of the formal language (except when it is specifically
stated otherwise).
The expressions (of the formal language) are built up from the atoms in a recursive fashion
using the following clauses.
• Each identifier and each constant is an expression.
• If b and c are expressions, then so are (b + c), (b × c), (b − c).
This definition means that a string of atomic symbols is an expression if and only if it can be
constructed by repeated use of these rules. Thus, if a string is neither an identifier nor a constant
0, 1, then it is an expression if and only if it has the form
(b • c)
where b, c are two strings which are known to be expressions, and • is one of the three operation
symbols +, ×, −. The brackets are essential in this expression. They are used to ensure that the
expressions can be parsed in only one way.
1.1 EXAMPLE. Remembering that x, y, z, . . . are identifiers we see that
(x + y)
((x + y) × z)
(x × (y + z))
(x + 1)
are expressions. However
,
(y × z)
, ((y × z) + x)
, ((1 + 1) + 1)
, (x + (y × 0))
(x + y + z) ,
,
(z − x)
,
((z − x) × y)
, ((x × y) + (x × z))
,
(x × (1 + y))
(x + 2) ,
Each expression has a parsing tree which indicates how the expression is built up from the
identifiers and constants using the operation symbols.
1.2 EXAMPLE. The parsing trees of
are
y
x
respectively. z
(y + z)
(x × (y + z))
(×)
,
y
(x × y)
(×)
c
a
where
..
.
(•)
..
.
,
b
c
are the parsing trees of b, c respectively. The label (•) indicates which operation symbol is used
to construct a from b, c.
Note that
(x × (y + z)) ,
((x × y) + (x × z))
are two different expressions with quite different parsing trees. It is true that under the intended
interpretation (in Z ) these two expressions will always give the same value, but this is not the
whole story. The two expressions give different ways of combining the input values x, y, z to
arrive at the same output value.
Formal arithmetic is concerned with how we may recognise and prove that two such expressions always give the same resulting value. It is also concerned with the evaluation strategies
which produce the values of expressions.
When displaying the parsing tree of an expression it is not necessary to include all the
subexpressions at the intermediate nodes. Once we have the identifiers and constants at the
leaves, and the labels at each step, the whole expression can be reconstructed.
x
z
(x × z)
((x × y) + (x × z))
y
x
z
·
(+)
x
,
y
·
(×)
(×)
·
·
perhaps augmented with the final expression at the root. x
z
·
(×)
(+)
Parsing trees as algorithms
((x × y) + (x × z))
x
(+)
b
1.3 EXAMPLE. The two parsing trees of Example 1.2 can be displayed as
x((y
are not expressions. In the first case there aren’t enough brackets, in the second case 2 is not
an allowable atom, and the third string is just meaningless. (x × (y + z)) ,
i.e. an expression a built up from two smaller expressions b, c using the operation symbol •.
The parsing tree for a is
..
..
.
.
(×)
(+)
Arithmetical expressions can be thought of as simple algorithms which produce values in Z.
More precisely, an arithmetical expression provides an algorithm which, when supplied with
the appropriate input values (from Z) will deliver an output value in Z.
Before we can work out the value of an expression, we need to know the values of the
occurring identifiers. Thus, as it stands the expression
a ::= ((x + y) × (x + z))
does not have a value, but obtains a value as soon as x, y, z are allocated values. For instance
7
8
COMP11120: 1
Discrete Structures
• if x = 1, y = 2, z = 3 then a = 12
• Computational strategies are indicated by the parsing trees of expressions.
• if x = −2, y = 3, z = 0 then a = −2
• if x = 0, y = 7, z = −4 then a = −28.
We can make this process precise.
An allocation or assignment for an expression gives a numerical value to each identifier
occurring in the expression. For instance, in the above illustration we used three different
allocations where
• x=1 y=2
• x = −2 y = 3
• x=0 y=7
All computational activities have similar aspects (and often other aspects as well).
We conclude with a description of the evaluation of the expression b ::= (x × (y + z)) of
the previous example in an alternative style that we will also use in the next chapter. In this
style the evaluation is performed in a sequence of equality steps where the original expression
is simplified until its value is obtained. We start by replacing the identifiers by the numerical
values that are allocated to them.
z=3
z=0
z = −4
respectively.
Given an allocation for an expression, the parsing tree for that expression provides an algorithm by which the expression can be evaluated. At each leaf of the tree there is either a
constant (0 or 1) or an identifier. Replace each of these by the associated value (i.e. the canonical value of a constant or the allocated value for an identifier). Now let these values trickle
down through the tree. At each node use the indicated operation to pass from the numerator
values to the value of the node.
m
n
m+n
(+) ,
m
n
mn
m
(×) ,
n
m−n
(−)
2
3
,
−2
−6
(×)
(×)
6
6
show that under this allocation both b and c take the value 6. Z, Z
•
>Z
x • y = ax + by
for all x, y ∈ Z.
(a) What are the values (in terms of a and b) of
2•3
(x • y) • z
3
4
12
(×)
(+)
This process is a rather pedantic way of doing what you are already familiar with (i.e.
arithmetic on the integers). You should notice three different aspects.
• The structure Z is viewed as an abstract entity whose properties we are interested in.
• An associated arithmetical language is used to describe certain properties of Z and as a
vehicle for performing certain computations.
9
(3 × 2)
Exercises
Then
3
=
Note that in this evaluation we have allowed any integer numeral to be used as a constant that
can appear in expressions, not just 0, 1.
c ::= ((x × y) + (x × z))
x = 3 , y = −2 , z = 4.
(+)
(3 × (−2 + 4))
on Z, where the values of • are computed by the rule
(whose parsing trees are displayed in Example 1.2). Consider the allocation
4
=
1.1 Let a, b be two fixed (but unknown) integers, Using these we can form a binary operation
1.4 EXAMPLE. Consider the two expressions
−2
(x × (y + z))
6
Eventually a value appears at the root and this is the value of the expression.
b ::= (x × (y + z)) ,
=
u•v
y•x
x • (y • z) (x • y) • (v • u)
for arbitrary u, v, x, y, z. (Simplify your answers where possible).
(b)
(i) What can you deduce about the values of a and b if the operation • is commutative?
(ii) What can you deduce about the values of a and b if the operation • is associative?
Hence list all such operations which are both commutative and associative.
1.2 Repeat Q1.1 where this time • is defined by
x • y = (a + x)(b + y)
(for all x, y ∈ Z).
10
COMP11120: 1
Discrete Structures
1.3 By writing out the parsing tree, verify that each of the following is an arithmetical expression of the formal language described in this chapter.
(i) (x + y)
(iv) ((x × y) + z)
(vii) (((x + x) − (y × z)) × y)
(ii) (y × z)
(v) ((x + y) × z)
(viii) ((x + y) × ((y × z) − x))
(iii) (z − x)
(vi) ((x × y) × z)
(ix) (((z − y) − x) × y)
1.4 Say why each of the following is not an arithmetical expression of the formal language.
(i) (x + y) + z
(iv) (((x × y)) − z)
(ii) ((y + z) + 2)
(v) (y × 0) − x
(iii) ((z − x)y)
(vi) (x + x + · · · + x)
x2
x3
.
x4
.
x1
x5
.
x3
.
x6
.
x2
x5
.
x6
x7
.
.
x7
.
x4
x8
.
.
.
x8
.
.
respectively, but where a and b take the same value for all allocations. (There are many possible
answers to this question, you just need to come up with one). Can you find a third expression
c with a quite different parsing tree but which always gives the same value as a and b?
1.3
i.e. x 6∈ X means that x is not a member of X.
1.5 EXAMPLE. Let
E
P
S
= set of even natural numbers
= set of prime numbers (Remember that an integer is prime if, and only
if, its only factors are 1 and itself)
= set of square-free natural numbers
A natural number n is square-free if there is no prime number p such that p2 divides n. For
example 15 is square free since its prime factors are 3 and 5, and neither 32 nor 52 is a factor.
12 is not square-free, since it has a factor 22 .
With these three sets each of
1.5 Find two expressions a, b whose parsing trees have the shapes
x1
and so all four displayed phrases mean the same, i.e. x ∈ X.
We let
x∈
/ X abbreviate not[x ∈ X]
Simple properties of sets
We have met already several particular sets such as N, Z, Q, R, Char, String, Bool as well as
various subsets of these. There are many other places where we use sets of one kind or another.
We look at some of the things we can do with sets in general.
0∈E 1∈
/E 2∈E 3∈
/E 4∈E 5∈
/E 6∈E 7∈
/E 8∈E 9∈
/E
0∈
/P 1∈
/P 2∈P 3∈P 4∈
/P 5∈P 6∈
/P 7∈P 8∈
/P 9∈
/P
0∈
/S 1∈S 2∈S 3∈S 4∈
/S 5∈S 6∈S 7∈S 8∈
/S 9∈
/S
is true. In general elements of an arbitrary set X do not come in any particular order. In this respect
sets are different from lists. Also there is no notion of ‘repetition of elements’ of a set. In this
respect sets are different from multisets (or bags).
When a set is finite and small it is often possible to name the set by listing its elements in
some order. When we do this we use braces { and } to indicate that a set is meant. Thus
{1, 2, 3, 4}
{2, 1, 3, 4}
{2, 1, 3, 2, 4}
{1, 2, 1, 3, 4, 2}
Membership and equality of sets
all describe the same set (the set of the first four strictly positive integers). Remember that the
order in which the elements is listed is not important; also if for some reason an element is
listed more than once, then this does not change the set.
Two sets X and Y are equal
X =Y
A set is something that has elements or members. We write
if they have precisely the same members, i.e. if
For each a,
x∈X
to indicate that
x is an element of the set X
or
x is a member of the set X.
Other useful ways of phrasing this are
x is contained in X
11
,
X contains x
a∈X ⇔ a∈Y
holds.
Note that the same set may have many different descriptions. For instance, if
X = the set of natural numbers x
which satisfy x2 = x
,
Y = the set of natural numbers y
which satisfy 0 ≤ y ≤ 1
then X = Y , for they are both the set {0, 1}.
12
COMP11120: 1
Discrete Structures
Inclusion
In other words these two sets are defined by
Given two sets X,Y , we write
X ⊆Y
and say
X is a subset of Y
Y is a superset of X
X is included in Y
Y includes X
if every element of X is also an element of Y . Any of the four phrases can be used as is
convenient.
Notice that
X = Y ⇔ X ⊆ Y andY ⊆ X
and proofs of equality of sets usually spilt into two parts corresponding to the two inclusions.
1.6 EXAMPLE. Using the sets E, P, S of Example 1.5 we have P ⊆ S (i.e. every prime number
is square-free), but there are no other inclusions between these sets. The empty set
One particular set is the empty set
0/
which has no members at all. Note that
0/ ⊆ A
holds for all sets A. (For if not, then there is some x ∈ 0/ with x ∈
/ A, in particular, there is some
/
x ∈ 0!)
Singleton sets
For each entity a we can form a set
{a}
x ∈ A ∪ B ⇔ x ∈ A or x ∈ B
x ∈ A ∩ B ⇔ x ∈ A and x ∈ B.
Note that
A ∪ B is defined using or
A ∩ B is defined using and.
Many of the properties of ∪, ∩ are nothing more than translations of the properties of or and
and.
1.7 EXAMPLE. Consider the subsets E, P, S of N used in Example 1.5. The following table
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
E ∪P E ∪S P∪S E ∩P E ∩S P∩S
X
X
×
×
×
×
×
X
X
×
×
×
X
X
X
X
X
X
X
X
X
×
×
X
X
X
×
×
×
×
X
X
X
×
×
X
X
X
X
×
X
×
X
X
X
×
×
X
X
X
×
×
×
×
×
×
×
×
×
×
gives an indication of the members of the various sets E ∪ P, . . . , P ∩ S. In fact, since P ⊆ S we
see that P ∪ S = S and P ∩ S = P. Note also that E ∩ P = {2}. For all sets A, B we have
A ⊆ A∪B
A∩B ⊆ A
and these inclusions may be indicated pictorially by
A∪B
whose only element is a. You should not confuse the set {a} with its element
{a} 6= a.
Note that
holds for all sets A.
{a} ⊆ A ⇔ a ∈ A
B ⊆ A∪B
A∩B ⊆ B
I
@
@
@
@
A
B
I
@
@
@
@
A∩B
Union and intersection
There are several ways of combining sets A and B to produce new sets.
• The union A ∪ B is the set of all elements belonging to either A or B (or both),
• The intersection A ∩ B is the set of all elements belonging to both A and B.
13
where the upward arrows indicate the inclusions.
/
Two sets are disjoint if they have no common members, i.e. if A ∩ B = 0.
These two operations have several simple properties.
A∪B = B∪A
commutative
A∩B = B∩A
(A ∪ B) ∪C = A ∪ (B ∪C) associative (A ∩ B) ∩C = A ∩ (B ∩C)
A∪A = A
idempotent
A∩A = A
14
COMP11120: 1
Discrete Structures
The empty set 0/ is neutral for ∪
whereas it is extreme
A ∪ 0/ = A = 0/ ∪ A
Exercises
1.6 List the elements of the following sets:
A ∩ 0/ = 0/ = 0/ ∩ A
for ∩.
There are two important connections between ∪ and ∩. These are given by the distributive
laws.
A ∪ (B ∩C) = (A ∪ B) ∩ (A ∪C)
A ∩ (B ∪C) = (A ∩ B) ∪ (A ∩C)
Note the duality between these two laws.
Relative complement
{x : x ∈ N, x is even, x < 15}
{z : z ∈ Z, z2 + 1 = 10}
{n : n ∈ N, 4 + n = 2}
{x : x ∈ R, x2 + 1 = 0}
1.7 Consider the sets
where each of a, b is either 0 or 1. (There are four possible ways of choosing a, b). List these
different sets, in each case indicate the distinct elements and size of the set.
1.8 (a) Consider the three subsets E, P, S of N used in the Examples 1.5, 1.7, etc of the notes.
Make a table showing the members x of the 15 sets
Given two sets A, B the relative complement
A−B
E
E ∪P
E ∩P
E −P
P−E
is the set of all elements of A which are not members of B.
x ∈ A − B ⇔ x ∈ A and x ∈
/ B.
1.8 EXAMPLE. Continuing with Examples 1.5, 1.6, and 1.7 we see that table
0:
1:
2:
3:
4:
5:
6:
7:
8:
9:
E −P E −S P−E P−S S−E S−P
X
X
×
×
×
×
×
×
×
×
X
X
×
×
×
×
×
×
×
×
X
×
X
×
X
X
×
×
×
×
×
×
X
×
X
×
X
×
×
×
×
X
×
×
X
×
X
×
X
X
×
×
×
×
×
×
×
×
×
×
gives an indication of the members of the various sets E − P, . . . , S − P. There are many occasions where all the sets we are interested in are subsets of a fixed set S. In
these circumstances we refer to this set S as the universal set (for we pretend that it contains
everything in the universe).
Given such a set S, for each subset X ⊆ S we write
X0
and call
for
S−X
the complement of X (in S).
The De Morgan laws
P
E ∪S
E ∩S
E −S
S−E
S
P∪S
P∩S
P−S
S−P
which also satisfy 10 ≤ x ≤ 20.
(b) Draw a diagram showing all the inclusions between the top 9 of these sets, i.e. the sets
E, P, . . . , P ∩ S.
1.4 Boolean algebra of sets
Suppose we fix a universal set S and consider only subsets of S. This gives us
· two distinguished subsets 0/ and S,
· a 1-placed operation (·)0 on such subsets,
· two 2-placed operations ∩ and ∪ on such subsets.
Universal set
X0
{a + b , ab , a2 + b}
(X ∪Y )0 = X 0 ∩Y 0
(X ∩Y )0 = X 0 ∪Y 0
show how complementation (·)0 may be taken through ∪ and ∩.
15
These form a structure known as a Boolean algebra.
What kind of algebraic manipulations can we carry out with this structure? Here is a list of
the basic properties we can use.
X ∪ S=S
/
X ∪ 0=X
X ∪Y =Y ∪ X
X ∪ (Y ∪ Z)=(X ∪Y ) ∪ Z
X ∪ (Y ∩ Z)=(X ∪Y ) ∩ (X ∪ Z)
X ∪ X =X
X ∪ (X ∩Y )=X
(X ∪Y )0 =X 0 ∩Y 0
X ∪ X 0 =S
extreme
neutral
commutative
associative
distributive
idempotent
absorbtion
De Morgan
complement
16
/ 0/
X ∩ 0=
X ∩ S=X
X ∩Y =Y ∩ X
X ∩ (Y ∩ Z)=(X ∩Y ) ∩ Z
X ∩ (Y ∪ Z)=(X ∩Y ) ∪ (X ∩ Z)
X ∩ X =X
X ∩ (X ∪Y )=X
(X ∩Y )0 =X 0 ∪Y 0
X ∩ X 0 = 0/
COMP11120: 1
Discrete Structures
The above laws come in dual pairs. The involution law
X 00 = X
stands alone and may also be called the double complements law.
These laws can be used to prove many identities.
(Element proof) For each element x we have


 x ∈ X ∪Y 
and
x∈L ⇔


x∈
/ X ∩Y


 x ∈ X or x ∈ Y 
and
⇔


x∈
/ X or x ∈
/Y
1.9 EXAMPLE. Consider the sets


/Y 
 x ∈ X and x ∈
or
⇔


x ∈ Y and x ∈
/X
∗
∩Y )0 ∩ Z)0
A ::= ((X
B ::= (X ∩Y ) ∪ Z 0
C ::= (X ∪ Z 0 ) ∩ (Y ∪ Z 0 )


 x ∈ X ∩Y 0 
or
⇔


x ∈ Y ∩ X0
where X,Y, Z are subsets of the universal set S. Then
A =
=
=
=
((X ∩Y )0 ∩ Z)0
(X ∩Y )00 ∪ Z 0
(X ∩Y ) ∪ Z 0
B
definition of A
De Morgan law
involution
definition of B
⇔ x∈R
∗
where you should think about step ⇔.
(Boolean proof) Here we have
(1)
L = (X ∪Y ) ∩ (X ∩Y )0
(2)
and the distributive laws give B = C. = [X ∩ (X ∩Y )0 ] ∪ [Y ∩ (X ∩Y )0 ]
(3)
= [X ∩ (X 0 ∪Y 0 )] ∪ [Y ∩ (X 0 ∪Y 0 )]
Some identities can have several different proofs.
(4)
= [(X ∩ X 0 ) ∪ (X ∩Y 0 )] ∪ [(Y ∩ X 0 ) ∪ (Y ∩Y 0 )]
(5)
/
= [0/ ∪ (X ∩Y 0 )] ∪ [(Y ∩ X 0 ) ∪ 0]
1.10 EXAMPLE. Consider the sets
(6)
L = (X ∪Y ) ∩ (X ∩Y )0
R = (X ∩Y 0 ) ∪ (Y ∩ X 0 )
(7)
= R
(1) follows by the definition of L
where X,Y are arbitrary. We can show that
L=R
by three different methods
where
= (X ∩Y 0 ) ∪ (Y ∩ X 0 )
(2) follows by a distributive law
(3) follows by a De Morgan law
(4) follows by a distributive law
(5) follows by a complement law
• Using elements
(6) follows by an extreme law
• Using boolean algebra
(7) follows by the definition of R
as required.
• Using Venn diagrams
(Venn diagram proof) Draw two pictures of two overlapping circles and shade in the two
regions representing L and R. as follows.
17
18
COMP11120: 1
Discrete Structures
Exercises
1.9 Below you are given eight examples, (i – viii), of a set A built up from three sets X,Y, Z
each of which is regarded as a subset of a universal set S. In each case you have to find two
different expressions for A.
· A disjunctive normal form of the shape
A = C1 ∪ · · · ∪Cm
where each C is an intersection of some (perhaps all) of X,Y, Z.
· A conjunctive normal form of the shape
A = D1 ∩ · · · ∩ Dm
where each D is a union of some (perhaps all) of X,Y, Z.
The new expressions should be found using the various laws of boolean algebra.
(i)
(iii)
(v)
(vii)
((X ∪Y ) ∩ Z) ∪ X
((X ∩Y ) ∪ (Y ∩ Z)) ∩ (Z ∪ X)
((X ∪Y ) ∩ Z) ∪ ((Z ∩Y ) ∪ X)
(Y ∩ ((X ∩Y ) ∪ Z)) ∪ Z
(ii)
((X ∩Y ) ∪ Z) ∩ (Y ∪ Z)
(iv)
(((X ∪Y ) ∩ Z) ∪Y ) ∩ X
(vi)
((X ∩Y ) ∪ (X ∩ Z)) ∩ (Y ∪ Z)
(viii) (((X ∩ Z) ∪Y ) ∩ X) ∪ Z
1.10 For each of the expressions A of Exercise 1.9, use the De Morgan laws to express the
complement A0 in such a way that the operation (·)0 occurs only in the form X 0 , Y 0 , or Z 0 .
1.11 Given three subsets X,Y, Z of a universal set S, how many different subsets A can be
formed using ∩, ∪, and (·)0 .
Show that for suitable X,Y, Z it is possible to find 9 distinct such sets A0 , . . . , A8 with
A0 ⊆ A1 ⊆ · · · ⊆ A8 .
19
20