Sets, Functions, Relations and Strings
Sets, Functions, Relations and Strings
As we studied elementary mathematics we came to believe that the fundamental object in mathematics is the number. Then, as we began calculus, we learnt of a new type of mathematical object the function. At first we thought of a function as a formula, or an algebraic expression, such as the function x2 . But then they tried to tell us that functions are more than just formulae. They are rules which associate with every element in one set a unique element of another. Every function has to go from one set to another. The deeper we get into mathematics the more we become involved with sets. We learn of vector spaces and groups these are sets on which there is some algebraic structure. In geometry we no longer deal with shapes, but rather, sets of points. And in calculus were increasingly concerned with domains of functions and regions of convergence and sets of solutions to differential equations. The set is paramount in mathematics. In computing science, sets are one of many important data structures. In databases they are the fundamental data type.
Definition: A set is a collection of objects called elements. We write x S if x is an element of the set S and x S if it is not. Generally elements are denoted by lower case letters and sets by capitals. However since there are sets whose elements are sets, and sets of sets of sets etc., it is not always possible to maintain this distinction. There are three main ways of describing or defining a set: (1) Use a standard name N = the set of natural numbers 0, 1, 2, ... ; Example 1:
NOTE: Some books exclude zero from this set and consider the natural numbers to be synonymous with positive integers.
41
Z = the set of integers 0, 1, 2, ... (from the German Zahlen, meaning numbers); Q = the set of rational numbers (Q stands for quotient); R = the set of real numbers; C = the set of complex numbers.
(2) List the elements {x1, x2, ... , xn} denotes the set with the elements: x1, x2, ... , xn. Strictly speaking this notation can only be used for finite sets but if there is an obvious pattern we can indicate certain infinite sets this way. Example 2: {4, 5, 13} is the set whose elements are the integers 4, 5 and 13; {0, 1, 4, 9, ... } indicates the set of square integers. (3) Describe a property that characterises the elements. {x S | Px} denotes the set of all elements x, in S, for which the statement Px is true. If the set S is understood we often omit it and write {x | Px}. Example 3: {x Z | x > 0} is the set of positive integers, commonly denoted by Z+; {x Z | 3 < x < 7} = {4, 5, 6}; {x R | x2 < 1} denotes the open interval (1, 1) i.e. all real numbers x such that 1 < x < 1; {n | q[n = 7q]} denotes the set of all multiples of 7. NOTES: (1) The symbol x in the notation {x | Px} is a dummy variable. It can be replaced throughout by any other symbol not otherwise used. Thus {x | Px} = {r | Pr}. (2) y {x | Px} Py, that is, y belongs to the set if and only if it has the defining property. (3) When we describe a finite set by listing the elements the order does not matter. Also any repetitions are ignored. For example: {3, 1, 2, 4} = {1, 2, 3, 4} = {1, 2, 2, 3, 4, 4, 4}. (4) It was once naively thought that for every property there must be a set, but this can lead to certain paradoxes. The most famous is the Russell Paradox named after the philosopher and mathematician Bertrand Russell. If S = {x | x x} then S S if and only S S. [If S S it satisfies the defining property and if S S it also satisfies the defining property!] We clearly cannot allow a mathematical system in which such paradoxes exist. Those interested in the foundations of mathematics set up axioms for set theory in which there are precise restrictions on which properties are allowed to give rise to a set. Fortunately such deep problems are far removed from the coal-face of useful mathematics and we can safely ignore them. Definition: Two sets are defined to be equal if every element of one set is an element of the other. In symbols: S = T x[x S x T] Example 4: {x Z | x2 < 2} = {1, 0, 1} = {x R | x3 = x}
42
Definition: A set S is a subset of a set T if every element of S is an element of T. We write S T to denote the fact that S is a subset of T. In symbols this is very similar to the definition of equality of sets: S T x [x S x T] In particular every set is a subset of itself. Subsets S which are not the whole set T are called proper subsets. We denote this by S T. A very important set is the empty set. This is the set with no elements and can be described by an empty list {} or an impossible property {x | x x}. Note that there is only one empty set. For example, the empty set of integers is the same as the empty set of triangles. The statement mermaids dont exist can be expressed by saying that the set of mermaids is the empty set. The empty set is denoted by . Like the number 0, it is an extremely useful concept. If we want to prove that a given property P can never hold we can consider the set S = {x | Px} and then prove that S = . Another important set is the universe a set consisting of all the elements with which we are concerned in a given context. For example if we are considering sets of integers we would take our universe to be the set of all integers.
We now list a large number of definitions of functions which assign to each set S, or pair of sets S and T, a certain set. Intersection: Union: Difference: Complement: S T = {x | x S and x T} S T = {x | x S or x T} S T = {x | x S and x T}
S = {x |x S} = U S where U is the universe in which we are working. Example 5 Suppose S = {1, 2, 3} and T = {1, 3, 5, 7}. Then S T = {1, 3}; S T = {1, 2, 3, 5, 7}; S T = {2}; T S = {5, 7} If our universe is the set of all integers and S is the set of even numbers, S is the set of all odd numbers.
Some of these concepts can be illustrated by diagrams where sets are represented by regions drawn in the plane and elements by points inside them. The simplest system is to divide a rectangle into two rows and two columns. The left side represents a set and the right side, its complement. Then the top half can represent a second set and the bottom, its complement.
43
S T S Example 6:
T S
S T T
S T
S T T
S
T UNION
T INTERSECTION
If there is a third set we can simply draw a circle in the middle, cutting across all four regions. The region inside the circle represents the third set and the outside represents its complement.
S T S R R
Cartesian Product: S T = {(x, y) | x S and y T} = the set of all ordered pairs whose components come from S and T respectively S T R = {(x, y, z) | x S, y T, z R} etc. Cartesian Power: Sn = S S ... S (n factors) Power Set: (S) = {A | A S} = the set of all subsets of S Example 7: Suppose S = {1, 2, 3} and T = {1, 3, 5, 7}. Then S T = {(1, 1), (1, 3), (1, 5), (1, 7), (2, 1), (2, 3), (2, 5), (2, 7), (3, 1), (3, 3), (3, 5), (3, 7)} (S T)3 = {(1, 1, 1), (1, 1, 3), (1, 3, 1), (1, 3, 3), (3, 1, 1), (3, 1, 3), (3, 3, 1), (3, 3, 3)} (S) = {, {1}, {2}, {3}, {1, 2},{1, 3}, {2, 3}, {1, 2, 3}} NOTES: (1) While the order of the elements of a set does not matter there is often a particular order that is more natural than the others. The advantage of using a systematic order is that it ensures that every combination has been accounted for. Look carefully at the order in which we have listed the elements in the above example. (2) Venn Diagrams cannot be used to depict these extended set functions.
44
Jack is my mother' s uncle. That is what we usually think of when we talk about relations people with whom we are related. But it is the relationship itself that mathematicians would call a relation. Definition: A relation is a statement involving two variables, or two gaps, where the names of people or things can be inserted. So x is the mother of y, or just mother of is one such relation. In mathematics there are numerous relations such as is less than, is parallel to, is a power of . We can denote the fact that x has the relation with y by xRy. This reflects the word order in natural language, and indeed where we have invented symbols for particular relations in mathematics we usually use this format, such as x < y. But a relation, as well as being a connection between two sets, can also be considered as a set in itself. Suppose R is a relation such that xRy makes sense for x S and y T. We can represent R by the set of ordered pairs (x, y) for which the relation holds, that is {(x, y) | xRy}. Now S T denotes the set of all ordered pairs (x, y) with x S, y T. So the set of ordered pairs for which xRy (is true) is just a subset of S T. This gives us an alternative definition of a relation. Definition: A relation from a set S to a set T is any subset of S T. Definition: A relation on a set S is a relation from S to S i.e. a subset of S S. Example 8: If S = {1, 2, 3, 4} then the relation normally written x < y would be: {(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)} n and the relation x = y for some integer n 1 becomes: {(2, 2), (3, 3), (4, 2), (4, 4)}. When a computer makes reference to a relation such as < it certainly doesn' t consult an array of ordered pairs. But when dealing with the relation student x is enrolled in course y we might store this information in a database as an array of ordered pairs. STUDENT COURSE AARON Adam MATHS 1 AARON Adam PHYSICS 1 ANTON Tom MATHS 1 In mathematical notation we would write this relation as {(AARON Adam, MATHS 1), (AARON Adam, PHYSICS 1), (ANTON Tom, MATHS 1)} Example 9:
3.6. Relations
Definition: If R and S are relations on the set X then the sum of R and S is the relation R + S defined on X by: x(R+S)y if xRy or xSy. Example 10: The relation spouse of means husband or wife of. If H = husband of and W = wife of then H + W is the relation spouse of. Example 11: If L is the relation < on the set of real numbers, and E is the relation of equality, then L + E is the relation . (We could write this as < + = = but this would be a little confusing!)
45
Definition: If R and S are relations on the set X then the product of R and S is the relation RS defined on X by: xRSy if there is some z X such that xRz and zSy. Example 12: Jack is married to Mary and Marys mother, Ruby, is therefore Jack' s motherin-law. The relation mother-in-law of is built up from the simpler relations of spouse of and mother of. Ruby is the mother-in-law of Jack because there exists a person, Mary, such that Ruby is the mother of Mary and Mary is the spouse of Jack. There is a chain between Ruby and Jack and Mary is the link. If H + W denotes spouse of, as in example 10, and M = mother of then M(H+ W) denotes the relation mother-in-law of. Definitio: If R is a relation on a set X, R2 = RR, R3 = RRR etc. Example 13: Marys parents are Ruby and Ted. Ted is the husband of Mary' s mother. So with H and M defined above, HM = father of. And M + HM = parent of. So (M + HM)2 = grandparent of .
Like all mathematical objects, relations can be classified according to whether they satisfy certain properties. The three most important properties for a relation R on a set S are the reflexive, symmetric and transitive properties. Definition: R is reflexive if xRx for all x. R is symmetric if xRy yRx for all x, y. R is transitive if xRy and yRz xRz for all x, y, z. Example 14: x < y on the set of integers. Not Reflexive: For example 1 < 1 is FALSE. Not Symmetric: For example 1 < 2 is TRUE but 2 < 1 is FALSE. Transitive: If x < y and y < z it follows that x < z. Example 15: xRy x = yn for some n N on the set N. Reflexive: Since x = x1 for all x. Not symmetric: Now 4R2 (as 4 = 22) but it is not true that 2R4 (2 4n for any positive n). Transitive: Suppose xRy and yRz. Then x = yn for some n N and y = zm for some m N. (We mustn' t fall into the trap of assuming that the power is the same in each case.) Hence x = (zm)n = zmn. Since mn N it follows that xRz. Example 16: xRy |x y| < 3 Reflexive: Since for all x, |x x| = 0 which is less than 3. Symmetric: Since |y x| = |x y|. Not Transitive: For example 1R3 and 3R5 but it is not true that 1R5. Example 17: xRy x y is even, defined on the set Z. Reflexive : For all x, x x = 0 which is even. Symmetric: Suppose xRy.
46
This means that x y is even, say x y = 2h for some h Z. Then y x = 2h = 2(h) which is even, so yRx. Transitive: Suppose xRy and yRz. This means that x y = 2h for some h Z and y z = 2k for some k Z. Now x z = (x y) + (y z) = 2h + 2k = 2(h+k) which is even. Hence xRz. Definition: An equivalence relation is a relation on a set that is reflexive, symmetric and transitive. Example 17 is an example of an equivalence relation.
Definition: If R is an equivalence relation and x S, we define [x]R = { y S | xRy }, that is, the set of all elements of S that are equivalent to x under the relation R. We call [x]R the equivalence class containing x. Often we omit the subscript R. NOTE: The fact that [x] does indeed contain x follows from the reflexive property. To find the equivalence classes for a given equivalence relation use the following simple algorithm. Choose an element x which has not yet been included in an equivalence class (any element will do to begin with). List all the elements that are related to x. These form an equivalence class. Now choose another element not yet listed. Continue until all elements have been included. Equivalence classes are somewhat like families. Those in the same class are related those in different classes are not. Human families do not provide a perfect analogy with equivalence classes since being related is not an equivalence relation. (The relation of being related is not transitive since my cousin has cousins who I would not consider as being related to me.) Example 18: Consider the set of railway stations of the world with xRy meaning that you can travel by rail from station x to station y (possibly having to change trains several times). The equivalence classes are the connected railway networks. One would be the government railways of mainland Australia. Another would be the Tasmanian Railways (unfortunately this has become the empty set). Some privately owned railways would form little equivalence classes. Until recently the British Rail network in England, Scotland and Wales formed an entire equivalence class, but with the opening of the Channel Tunnel they now form only part of a larger network that extends across Europe. Example 19: Let S = {1,2,3} and let R = {(1,1), (1,3), (2,2), (3,1), (3,3)}. It is easy to show that R is an equivalence relation. The equivalence classes are: [1] = {1,3} = [3] and [2] = {2}.
47
Theorem 1: x [y] [x] = [y] Proof: Suppose that [x] = [y]. Since x [x], x [y]. Conversely, suppose that x [y]. Then yRx (and by the symmetric property, xRy). We must show that [x] = [y]. We first show that [x] [y]. Suppose that z [x]. Then xRz. Since yRx, it follows from the transitive property that yRz and hence zRy by the symmetric property. Thus z [y], and so [x] [y]. Similarly, [y] [x], and so [x] = [y]. Theorem 2: For all x, y S, either [x] [y] = or [x] = [y]. Proof: Suppose that [x], [y] are not disjoint. Let z [x] [y]. Then z [x] and z [y]. By the previous theorem, [x] = [z] and [z] = [y] and so [x] = [y]. We have proved that any two distinct equivalence classes are disjoint. So: IF R IS AN EQUIVALENCE RELATION ON A SET S THEN S IS THE DISJOINT UNION OF ITS EQUIVALENCE CLASSES This means that S can be chopped up into non-overlapping equivalence classes so that elements are equivalent if and only if they belong to the same class.
Definition: A function f from a set S to a set T is the pair of sets (S, T) together with a rule that associates with each element x S a unique element of T, written f(x). This element is called the image of f. We indicate that f is a function from S to T by writing f:ST. The set S is called the domain of f and T is called the codomain. Other words that are used instead of function are map, operator and transformation. Example 20: f: S S where S is the set of people who are now living or who have died, with f(x) defined to be the father of x. But note that the son of x does not define a function on S because, although everyone has a unique (genetic) father, not everyone has a unique son and many people in S have no sons at all. The simplest way to describe a function from one finite set to another is by means of a table of values. Example 21: f::{1, 2, 3, 4}{1, 2, 3, 4} where f is defined by means of the following table of values: x f(x) 1 2 2 1 3 3 4 2
3.10. Functions
48
You do not have to discover any pattern in this table before you can call f a function. The rule here is simply to look up the table. The second column could have been filled up in any way, as long as no symbols other than 1, 2, 3, 4 is used. Example 22: There are 8 functions from {1, 2, 3} to {A, B} viz. x f1(x) f2(x) f3(x) f4(x) f5(x) f6(x) f7(x) 1 A A A A B B B 2 A A B B A A B 3 A B A B A B A Notice the systematic way in which these functions have been listed. f8(x) B B B
Definition: A function f:S T is 1-1 (pronounced one to one) if f(x) = f(y) x = y. Definition: The image of a function f:ST is {f(x) | x S}. It is denoted by im f. Definition: A function f:ST is onto if t T s[f(s) = t and s S]. Definition: A permutation on a set S is a 1-1 and onto function f from a set to itself. Example 23: Consider the following functions from R R: f(x) = x + 1; g(x) = x2 + 1; h(x) = x3 + x2; k(x) = ex. Then f is 1-1 and onto, g is neither h is onto but not 1-1 and k is 1-1 but not onto. Example 24: The function father of defined above is not 1-1 since there are different people with the same father. The function current husband of is a function from the set of married women to the set of married men. If no man is in a state of bigamy, this function is 1-1. Example 25: The six permutations on the set {1,2,3} are given by the following table: x f1(x) f2(x) f3(x) f4(x) f5(x) f6(x) 1 1 1 2 2 3 3 2 2 3 1 3 1 2 3 3 2 3 1 2 1 Note the systematic order in which these permutations have been listed.
Definition: The cardinal number of a finite set is its number of elements. We denote the cardinal number of S by #S. For example # = 0. Example 26: If S = {1, 2, 3} and T = {3, 5} then: #S = 3, #T = 2, #(S T) = 1, #(S T) = 4, #(S T) = 2, #(S T) = 6, #(S) = 8.
49
The following properties describe how the various set functions affect cardinal numbers. (1) #(S T) = #S + #T #(S T); (2) #(S T) = (#S).(#T); (3) #(S T) = #S #(S T); (4) #(S) = 2#S; (6) # = 0. Theorem 3: Let S be a set of size n. The number of relations on S is 2n. The number of reflexive relations on S is 2n(n1). The number of symmetric relations on S is 2n(n+1). Proof: A relation on S is a set of ordered pairs. There are thus two choices for each ordered pair the relation holds for that pair or it doesnt. Since there are n2 ordered pairs there are 2n choices, that is 2n relations altogether. We can represent these ordered pairs by putting dots in an n n table where each dot can be changed to a tick or a cross depending on whether or not the relation holds in that case. For a reflexive relation the diagonal of this table will have to consist of all s leaving n2 n dots for which there is a choice of a tick or a cross. Thus the number of reflexive relations is 2n(n1). For a symmetric relation the table of ticks and crosses has to be symmetric about the diagonal. So we are free to choose a tick or a cross for the dots above the diagonal and also those on the diagonal. But having done so there is no further choice available below the diagonal. This gives n(n 1) positions above the diagonal and n positions on the diagonal, giving a total of n(n + 1) positions altogether and so 2n(n + 1) symmetric relations. Example 27: The relation {(1, 1), (1, 3), (2, 2), (2, 4), (3, 1), (3, 3), (4, 2), (4, 4)} on the set {1, 2, 3, 4} gives rise to the table: 1 2 3 4 1 2 3 4 from which we can see that the relation is both reflexive and symmetric. Transitive and equivalence relations are much harder to count. We will see how it is possible to count equivalence relations in a later chapter.
It is possible to extend the notion of counting from finite sets to infinite ones, producing an arithmetic of infinite numbers. At first sight it might seem that this arithmetic is not very interesting, with just one infinite number called . But in fact there are different levels of infinitude! Counting involves two stages. We must first have a notion of same size and then a collection of standard sets, one for each size. This is analogous to the old method of weighing objects with a set of scales. Two objects have the same weight if the scales balance with one object in each pan.
50
In the case of same size we use the existence of a 1-1 and onto function from one set to the other. Definition: Sets S, T have the same size if there exists a 1-1 and onto function f: S T. This is how we first learnt to count in kindergarten. In pointing to each object in turn we were setting up such a 1-1 and onto function from the set to be counted to one of the standard sets {1, 2, , n}. These standard sets conveniently nest one inside the other and any set in 1-1 correspondence with {1, 2, , n} is said to have size n. The same definition of same size applies to infinite sets too, but the surprising fact is that not all infinite sets have the same size. So we shall have to abandon the symbol and develop a whole collection of new symbols to describe the varying levels of infinity. The ones we use may look strange when you first meet them, but they are the ones used by Georg Cantor when he first introduced these ideas towards the end of the nineteenth century. Cantor used the first letter of the Hebrew alphabet, the aleph written as . The smallest infinite number he denoted by 0 (aleph zero). The standard set for 0 is the set {1, 2, 3, .} of all positive integers. Any set that can be put into 1-1 correspondence with this set is said to have 0 elements. Such a set can be enumerated in a list: x1, x2, and conversely any set which can be so listed has cardinal number 0. Example 28: The set of even positive integers has cardinal number 0. The fact that we have thrown away half the numbers does not alter the fact that the even positive integers can be listed in an infinite list: 2, 4, 6, Unlike finite sets where taking some elements out reduces the size, infinite sets can have elements removed without changing their size. But we have yet to be convinced that there really is a bigger set than {1, 2, 3, }. Example 29: The set of natural numbers has size 0 The natural numbers are 0, 1, 2, 3, and since these are listed in an infinite list the set of all the natural numbers has size 0. But we added one extra numbers to the set of positive integers so should we not get one more? Not at all adding one more makes no difference to the size of an infinite set. In the arithmetic of infinite cardinal numbers 0 + 1 = 0. (Before you start thinking about subtracting 0 from both sides to get a contradiction let me hasten to point out that subtraction is not possible with infinite cardinal numbers.) Example 30: The set of all integers has size 0. Although we usually consider the integers in two infinite lists: 0, 1, 2, 3, . 1, 2, 3, 4 . we can easily merge these lists to produce a single infinite list: 0, 1, 1, 2, 2, 3, 3, .. So 0 + 0 = 0.
51
Example 31: #(Z Z) = 0. To see this we need to imagine the elements of Z Z represented by points with integer coordinates in the x-y plane. We have 0 lists, each with 0 points. It would seem very complicated to merge all these lists. But in fact it isnt. Just start at the origin (0, 0) and trace a spiral path around the origin so that every point with integer coordinates is eventually reached. The elements of Z Z can simply be listed in the order in which they occur in this spiral.
Z Z = {(0, 0), (0, 1), (1, 1), (1, 0), (1, 1), (0, 1), (1, 1), (1, 0), (1, 1), (1, 2), (0, 2), (1, 2), (2, 2), (2, 1), (2, 0), (2, 1), (2, 2), (1, 2), (0, 2), (1, 2), (2, 2), (2, 1), } So 0 0 = 0. (This means that division is not possible for infinite numbers.) This arithmetic is beginning to seem very boring. But wait. There is a surprise just around the corner. Neither addition nor multiplication are powerful enough to break the 0 barrier. What about exponentiation? Before we go for 00 we will just try 20. That will be enough. Recall that for finite sets 2n is the number of subsets of a set of size n. Recall too that 2 > n for every finite number n. Would it not be exciting if this were the case for infinite sets as well? We would not only break the 0 barrier but we would suddenly get infinitely many infinite numbers: 0 < 20 < 22 0 <
n
If n =#S then we define 2n = #(S). This can be demonstrated for finite numbers and is used as a definition for infinite numbers. We define n+1 = 2n and so we get an infinite list of infinite numbers: 0 < 1 < 2 < Theorem 5: Every set has more subsets than elements. Proof: Let S be a set and suppose that (S) has the same size as S. This means that there is a 1-1 and onto function from S to the set of subsets of S. Lets call this f. We now define X = {x S | x f(x)}, the set of all those elements of S which do not belong to the subset they correspond to. Now X is clearly a subset of S and, since f is onto, we must have X = f(x) for some x S. The question we now ask is Does x X? This is a perplexing question because it has no satisfactory answer. For, if x X then x {x S | x f(x)} and so x f(x). But f(x) = X and so this gives a contradiction. But on the other hand, if x X = f(x) then x satisfies the property that defines X, which means that x X. Both alternatives lead to a contradiction yet one of them must be true. The only way out of this mess is for our original assumption to be false.
52
So S, and its power set (S), dont have the same number of elements. But (S) cant be smaller because it contains the subsets {s} of size 1 and theres one of these for every s S. So (S) must be strictly bigger. Corollary: 2n > n for all cardinal numbers n, finite or infinite. We define n+1 = 2n and so we get an infinite list of infinite numbers: 0 < 1 < 2 < 00 is defined as the number of functions f:NN. This might seem very much bigger than 20 but, in fact, we can show that 00 = 20 = 1. There are deep logical mysteries awaiting anyone who wants to go further into infinite cardinal numbers (luckily we do not need to here). For a start there are not only the infinite numbers 0, 1, 2, . but there are even bigger ones. Also we have suggested that 1 is the next infinite number after 0 but we did not prove it for the simple reason that it is unprovable. It has been proved that the question is there a cardinal number between 0 and 1 is undecidable! Cantor actually defined 1 to be the next infinite number after 0 so for him the question was is 1 = 20? I have taken the view that although we cannot prove there are no numbers between 0 and 20 we certainly will never be able to find one (to do so would be to contradict the undecidability) and so we may as well define 1 to be 20. Putting those mysteries behind us let us now ask if this is this any use. The answer is definitely yes. Often a question of existence can be answered by counting. Is there somebody at the party who wasnt invited? Well there are 20 names on the guest-list and 21 people in the room. Therefore there must exist a gate-crasher. We will apply infinite cardinality to the question of computability. Computer programs can be written to compute functions from the positive integers to the positive integers, such as a program to compute the nth prime. Is there such a function for which no computer program can ever be written? It seems rather dangerous to make the bold assertion that non-computable functions exist because who knows how clever future generations of programmers will be and how powerful their computers will be! And yet we can prove that non-computable functions do exist. Why? Simply because there are only 0 potential computer programs in any programming language (finitely many of each size so they can be listed according to size). Yet there are 1 possible functions on the set of natural numbers. Hence there are more such functions than there are potential programs and so there must exist functions that cannot be programmed. A natural follow-up question is well, can you show me such a function? Unfortunately counting cannot do that, but in a later chapter we will exhibit a specific non-computable function.
3.15. Strings
If A is any set of symbols (it is usually finite and is called an alphabet) then a string on A is a finite sequence of symbols juxtaposed without any separators: a1a2 an.
Example 32: The following are some strings on the alphabet {1, 2, +, =}: = 1+1=2, = 2+2+2+2+2+2=12, = 22+12=11, = ++=
53
Strings and represent true statements in ordinary arithmetic. String makes sense but is false. String is pure nonsense. At least it does not make sense in ordinary arithmetic, though it could be given a meaning. But all four are valid strings on the alphabet. The length of the string = a1a2 an is defined to be n and is denoted by ||. Example 33: For the strings in example 1, || = 5, || = 14, || = 8, || = 3. The null string is the unique string of length 0 and is denoted by the symbol . It might seem a bit ridiculous to invent a word, and a symbol, for a string of no symbols. But people once thought that about the number zero. Nothing, whether it is the number zero, the empty set, or the null string, is a very useful concept. Life would be so much more complicated without it. The product of two strings = a1a2 am, = b1b2 bn., is defined to be the string = a1a2 amb1b2 bn. This operation is called concatenation. Example 34: Suppose , , and are the strings defined in example 1, and is the null string. Then = 1+1=2++= while = ++=1+1=2. Neither makes much sense but both are nevertheless valid strings. Observe that . In general, strings do not commute. But note that = since both give just 1+1=2. In fact concatenating the null string on either end of any string leaves that string unchanged. Theorem 6: For all strings on a given alphabet: () = () = || = ||+ || || = 0 The set of all strings on a given alphabet is closed under the operation of concatenation, the associative law holds and there is an identity, namely the null string. However strings do not have inverses (apart from the null string which is its own inverse) because concatenation cannot make a string shorter. An abstract structure in which there is a binary operation that satisfies both the closure law and the associative law, and which has an identity element, is known as a semigroup. A group is a semigroup in which every element has an inverse. The structure formed by the set of all strings on a given alphabet by the concatenation operation, is therefore a semigroup, but not a group because of the lack of inverses.
54
Normally we think of languages as special systems where certain strings are given meaning. There are the natural languages with which we communicate with one other in our daily lives such as English, French or Swahili. Then there are the specialised programming languages with which we communicate with computers such as BASIC, PASCAL and C++. But there are many other specialised systems that we do not usually think of as languages. Morse Code is a language that is built on top of a natural language. Chess enthusiasts and knitters have developed specialised languages in which they describe their activities. There are two aspects to any language syntax and semantics. Syntax is concerned with determining whether a given string has a valid form while semantics is concerned with the meanings that are given to acceptable strings. Semantics can only be discussed in the context of a particular language but syntax is more formal and can be discussed abstractly. The sentence The silver prejudice accelerated within the complicated sausage. is a syntactically correct English sentence. The verbs, nouns and adjectives are all in correct positions relative to one another. But semantically it is nonsense. It lacks meaning. On the other hand the syntax of the sentence Is near post-office where? is incorrectly structured for English, but if a foreign tourist stopped you in the street with this question you might be able to guess that he wanted to be directed to the nearest post-office. So correct syntax is not always necessary for communication. But it certainly makes communication much easier! Decimal notation for real numbers is a mini-language that uses the ten digits, the minus sign and the decimal point. The semantics of this language involves concepts such as powers of 10 and limits of sequences, but the syntax can be expressed by a few simple rules: (1) If a minus sign is included it must be at the front. (2) At most one decimal point may be included and if included it must have a digit on either side. (3) The first digit cannot be 0 unless it is the only symbol or a decimal point follows. In order to make our discussion as general as possible we use the term language to refer to any (finite or infinite) set of strings. If A is an alphabet: An denotes the set of all strings of length n. A0 = {}; A1 = A (since we identify a string of length 1 with the symbol itself); An denotes the set of all strings of length n, on the alphabet A; A* denotes the set of all strings, of any length, on A; A+ denotes the set of all non-null strings on A. Example 35: If A = {a, b}, A0 = {}, A1 = {a, b}, A2 = {aa, ab, ba, bb}, A3 = {aaa, aab, aba, abb, baa, bab, bba, bbb}; A* = {, a, b, aa, ab, ba, bb, aaa, ....}; A+ = {a, b, aa, ab, ba, bb, aaa, ....} A language on an alphabet A is defined to be any subset of A*. Example 36:
55
3.16. Languages
The following are a few of the (infinitely) many languages on the alphabet A = {a, b}: (1) , the empty set; (2) {b, ab, bab}; (3) {aba, ababa, abababa, ...}; (4) all strings on {a, b} with a prime number of a' s; (5) all palindromes on A, that is, all strings which are the same in both directions; (6) all strings on A except for abba; (7) all strings on A (ie A* itself). If L is a language on an alphabet A: Ln denotes the set of all strings of the form w1 w2 ... wn where each wi L, that is, all strings which can be made up of exactly n pieces, each coming from L. L* denotes the set of all strings which can be made up of any number of pieces, each coming from L. Since the number of pieces could be zero, L* includes the null string. L+ denotes the set of all strings which can be made up of one or more pieces, each coming from L. In other words L+ consists of all the elements of L* except the null string. L* is called the Kleene closure of L. The star of something roughly means any amount of it. Example 37: Suppose A = {x, y} and L = {xy, yx, x}. Then A2 = {xx, xy, yx, yy} and L2 = {xyxy, xyyx, xyx, yxxy, yxyx, yxx, xxy, xyx, xx}. While there are 9 ( = 32) combinations for L2 there are only 8 different elements in L2 because xyx can be obtained in two different ways Example 38: If A = {0, 1} and L is the set of all strings on A which contain an even number of 1' s then L3 consists of all strings on A where the number of 1' s is a multiple of 6 and L* = L. If L, M are languages on an alphabet A then: L M is the intersection of the two sets (as usual); L + M is the union of the two sets, that is, the strings that are in one or both of L and M. In the context of languages it is customary to use the symbol + instead of ); LM (sometimes we might write L.M) is {| L, M}. Example 39: Let L = {0,11} and M = {, 11, 1111, 111111, ....}. Then L M = {11}; L + M = {, 0, 11, 1111, 111111, ....}; LM = {0, 11, 011, 1111, ....}; L* = {, 0, 11, 00, 011, 110, 0011, 0110, 1100, 1111, ....} M* = M. Note that M = (11)*. The fact that M* = M is a particular case of the general fact that for any language, L, L** = L*.
56
Theorem 7: For any languages L, M: (1) L* + M* (L + M)*; (2) (L M)* L* M*; (3) L* = L L* + . Proof: (1) Suppose L* + M*. Then either L* or M*. In the first case = a1a2 an for some n 0, where each ai L. Since each ai L+M it follows that (L + M)*. Similarly in the second case. (2) is proved similarly. (3) The language L.L* consists of strings made up from one or more substrings from L and so is L+, excluding the null string. Throwing in we obtain L* itself. Example 40: Let L = {YES} and M = {NO}. Then the string YESYESNONONOYES belongs to (L+M)* but not to L* + M*. This shows that we cannot guarantee equality in (1). In fact the only strings in L* + M* are those which are either repeated YES' s or repeated NO' s, not mixtures. Example 41: Let L = {xyx} and M = {xy, yx, x}. Then = xyxxyx but (L M)* since L M is empty. This shows that we cannot guarantee equality in (2).
57
58