COMPSCI 225:
Discrete Structures in Mathematics and Computer Science
Relations and Functions
Ordered pairs, Cartesian products, power set
Given two objects x and y , we can form the ordered pair(x,y) . Unlike the pair set {x,y} , the order in which the elements appear is important: given two ordered pairs (x,y) and (u,v) , we have
(x,y)=(u,v)⇔x=u and y=v.
Similarly, we have ordered triples (x,y,z) , ordered 4-tuples (w,x,y,z) , and so on, where if (x1,x2,…,xn) and (y1,y2,…,yn) are ordered n -tuples then
We will show later that if A has n elements then P(A) has 2n elements.
Relations
Let A and B be sets. A binary relation fromA to B is a subset of A×B . A binary relation onA is a subset of A×A .
We use binary relations to represent relationships between the elements of A and the elements of B . If R is a binary relation from A to B , and (x,y)∈A×B , then (x,y)∈R if and only if the relationship holds between the element x and the element y .
Example 4.5.Link copiedLet A be the set of students at Auckland University, and let B be the set of courses. Let R be the relation that consists of those pairs (a,b) where a is a student in course b . For instance, if Sooyoun Lee and Wiremu Ngata are enrolled in 225, the pair (Sooyoun Lee, 225) and (Wiremu Ngata, 225) belong to R . If Sooyoun Lee is also enrolled in 220, then the pair (Sooyoun Lee, 220) is also in R . However, if Wiremu Ngata is not enrolled in 220, then the pair (Wiremu Ngata, 220) is not in R .
We often use infix notation for relations. In other words, we write (x,y)∈R as xRy . This is particularly common with relations like < , = , ⊆ and so on that have standard names. If (x,y)∈/R , we write xRy .
There are various properties that a relation on a set might or might not have. We say that a relation R on a set A is
reflexive if for all x∈A , xRx .
symmetric if for all x,y∈A , if xRy then yRx .
antisymmetric if for all x,y∈A , if xRy and yRx then x=y .
transitive if for all x,y,z∈A , if xRy and yRz then xRz .
Example 4.9.Link copiedCan a relation be both symmetric and antisymmetric? Example 4.10.Link copiedA relation on a finite set S can be pictured as a directed (pseudo)graph. Let the vertex set of the graph be S and the pairs (x,y) satisfying the relation are represented as directed edges (x,y) . Draw the relation ∣ on the set S={1,2,3,4,5,6} .
Equivalence relations
Let A be a set. An equivalence relation on A is a binary relation on A that is reflexive, symmetric and transitive. We often use symbols like ∼ , ≡ and ≅ for equivalence relations.
Example 4.11.Link copiedOn the set of students attending Auckland University, define one student to be related to another whenever their surnames begin with the same letter. Is this an equivalence relation? Example 4.12.Link copiedLet S denote the set of all people in New Zealand. Define a relation R on S by letting xRy mean that x has the same mother as y . Is this an equivalence relation?
Does the answer change if we define xRy to mean that x has the same mother or father as y ?
Example 4.13.Link copiedConsider the set Z of integers. Fix some integer m>1 . Then the relation of_ congruence modulo m , _defined byx≡y(modm)⇔m∣x−y,
is an equivalence relation.
Example 4.14.Link copiedLet G=(V,E) be a simple graph. Consider the binary relation on V such that, for u,v∈V , uRv if and only if there is a path from u to v . This is an equivalence relation, which we call connectivity. Example 4.15.Link copiedLet C denote the set of all computer programs. Define a relation ∼ on C as follows: For C,C′∈C (in other words, for any two computer programs C and C′ ) we write C∼C′ if, for all possible program inputs x , either C and C′ both do not terminate on input x , or else both C and C′ terminate on input x and return the same output. Example 4.16.Link copiedLet f:A→B be a function. Then the relation ∼f on A , defined byx∼fy⇔f(x)=f(y)
is an equivalence relation. We call this relation the equivalence relation induced by f .
Let ∼ be an equivalence relation on a set A , and let x∈A . The equivalence class
of x under ∼, denoted by [x] , is the set of all elements of A that are related to x under ∼ , i.e.
[x]={y∈A:x∼y}
We denote the set of equivalence classes of elements of A under ∼ by [A] or by A/∼ .
Example 4.17.Link copiedConsider the relation of congruence modulo~2 on the set Z . What are [0] , [1] ? Example 4.18.Link copiedLet G be a simple graph and let R be the relation of connectivity. The equivalence classes are the connected components of the graph.
If ∼ is an equivalence relation on A , and x,y∈A , then the following are equivalent:
x∼y .
[x]=[y] .
[x]∩[y]=∅ .
Proof: We show that (1) implies (2), (2) implies (3) and (3) implies (1).
(1)⇒(2) Suppose x∼y . Let z∈[y] . Then y∼z , so we have x∼y∼z , and so by transitivity we have x∼z . Thus z∈[x] . Hence [y]⊆[x] . Conversely, let w∈[x] . Then x∼w , so by symmetry we have w∼x. So w∼x∼y , and so w∼y . Hence y∼w , so w∈[y] . Thus [x]⊆[y] , so [x]=[y] .
(2)⇒(3) Suppose [x]=[y] . Then [x]∩[y]=[x]∩[x]=[x] . By reflexivity, x∼x , so x∈[x] , so [x]=∅ . Thus [x]∩[y]=∅.
(3)⇒(1) Suppose [x]∩[y]=∅ . Let z∈[x]∩[y] . Then x∼z and y∼z . By symmetry, we have z∼y , so x∼z∼y , and therefore by transitivity we have x∼y .
Hence (1), (2) and (3) are equivalent.
In other words, if [x] and [y] are equivalence classes then either [x]=[y] or [x] and [y] are disjoint. We say that the relation ∼partitions the set A .
Definition 4.1A (finite) partition of a set X is a set {X1,…,Xt} where Xi⊆X , Xi=∅ and:
Xi∩Xj=∅when1≤i<j≤t ;
X1∪X2∪⋯∪Xt=X .
An example of a partition is cutting a cake into pieces: No slice of cake is empty, slices of cake have no crumbs in common, putting the slices together forms the whole cake.
Example 4.19.Link copiedLet R be the relation on Z such that xRy if and only if x=y or x=−y . Show that R is an equivalence relation. What are [1] , [−14] , and [0] ?
Solution.[1]={1,−1} , [14]={14,−14} , and [0]={0} .
The graph representing an equivalence relation on a finite set is a union of complete graphs (with loops at every vertex).
A commonly used type of relation is an ordering. For instance, words in a dictionary are ordered using a relation called “lexicographical ordering”. We also order real numbers and integers in familiar ways.
Consider the usual ordering on Z . Then the set of integers satisfying the relation is {(x,y)∈Z2∣x≤y} . One can verify that this relation is reflexive, antisymmetric, and transitive. Other relations that have these properties can be considered to be somehow “analogous” to familiar orderings.
A relation R on a set S is called a partial ordering or partial order if it is reflexive, antisymmetric, and transitive. A set S together with a partial ordering R is called partially ordered set, or poset, and is denoted by (S,R).
Example 4.20.Link copiedShow that the “greater than or equal” relation (≥) is a partial ordering on the set of integers. Example 4.21.Link copiedThe divisibility relation ∣ is a partial ordering on the set of positive integers P=Z≥1 , since it is reflexive, antisymmetric, and transitive, as was shown in Example 4.2. We see that (P,∣) is a poset. Example 4.22.Link copiedShow that the inclusion relation ⊆ is a partial ordering on the power set of a set S . Example 4.23.Link copiedLet A be the set of all strings over the alphabet {0,1} . We say that x∈A is a_ prefix _of y∈A if y=xv for some v∈A . For example 01 is a prefix of 01111 , while 01 is not a prefix of 1111 . Show that this relation is a partial ordering. Example 4.24.Link copiedLet S be the set of all students in COMPSCI 225. Define a partial ordering ⪯ such that x⪯y if and only if x ‘s height is less than or equal to y ‘s height. Is (S,⪯) a poset?
In a poset the notation a⪯b denotes that (a,b)∈R . This symbol is used to emphasise the similarity to the familiar “less than or equal to” relation. But be careful: Just because a relation is denoted by a symbol like ⪯ does not automatically imply it has exactly the same properties as ≤ .
We write a≺b to mean a⪯b , but a=b . Also, we say ”a is less than b ” or ”b is greater than a ” if a≺b .
When a and b are elements of the poset (S,⪯) , it is not necessary that either a⪯b or b⪯a . For instance, in (P(Z),⊆) , {1,2} is not related to {1,3} , and vice versa, since neither set is contained within the other. Similarly, in (P,∣) , 2 is not related to 3 and 3 is not
related to 2 , since 2∤3 and 3∤2 . This leads to the following definition.
The elements a and b of a poset (S,≺) are called comparable if either a⪯b or b⪯a . When a and b are elements of S such that neither a⪯b nor b⪯a, a and b are called incomparable.
Example 4.25.Link copiedIn the poset (P,∣) , are the integers 3 and 9 comparable? Are 5 and 7 comparable? Example 4.26.Link copiedIn the poset (P(S),⊆) , where S={a,b,c} , {a} is not comparable to {b} , and {a,b} is not comparable to {b,c} .
The adjective “partial” is used to describe partial orderings since pairs of elements may be incomparable. When every two elements in the set are comparable, the relation is called a total ordering.
Definition 4.2If (S,⪯) is a poset and every two elements of S are comparable, S is called a totally ordered or linearly ordered set, and ⪯ is called a total order or a linear order. A totally ordered set is also called a chain.
Example 4.27.Link copiedThe poset (Z,≤) is totally ordered, since a≤b or b≤a whenever a and b are integers. Example 4.28.Link copiedThe poset (P,∣) is not totally ordered since it contains elements that are incomparable, such as 5 and 7.
We say that (S,⪯) is a well-ordered set if it is a poset such that ⪯ is a total ordering and such that every nonempty subset of S has a least element. (a is a least element of (S,⪯) if a⪯b for all b∈S .)
Example 4.29.Link copiedThe most familiar example of a well-ordered set is the set (N,≤) of natural numbers, with the usual “less than or equals” ordering. Example 4.30.Link copiedThe set of ordered pairs of positive integers, P×P , with (a1,a2)⪯(b1,b2) if a1<b1 , or if a1=b1 , and a2≤b2 (the lexicographic ordering), is a well-ordered set.
Example 4.31The set Z , with the usual ≤ ordering, is not well ordered since the set of
negative integers, which is a subset of Z , has no least element.
The letters of the alphabet have a standard ordering ′a′<′b′<⋯<‘z′ . The “dictionary” or “lexicographic” ordering turns this ordering on letters into an ordering on words. This is a special case of a general principle: given an alphabet set A that is a poset one can impose an ordering on strings over the alphabet A . We now explain this construction.
First, we will show how to construct a partial ordering on the Cartesian product of two posets, (A1,⪯1) and (A2,⪯2) . The lexicographic ordering⪯ on A1×A2 is defined by specifying that one pair is less than a second pair if the first entry of the first pair is less than (in A1 ) the first entry of the second pair, or if the first entries are equal, but the second entry of this pair is less than (in A2 ) the second entry of the second pair. In other words, (a1,a2) is less than (b1,b2) , that is
(a1,a2)≺(b1,b2),
either if a1≺1b1 or if both a1=b1 and a2≺2b2 .
We obtain a partial ordering ⪯ by adding equality to the ordering ≺ on A×B . The verification of this is left as an exercise.
Example 4.32.Link copiedDetermine whether (3,5)≺(4,8) , whether (3,8)≺(4,5) , and whether (4,9)≺(4,11) in the poset (Z×Z,⪯) , where ⪯ is the lexicographic ordering constructed from the usual ≤ relation on Z . Example 4.33.Link copiedOn figure below, indicate the set of ordered pairs in P×P that are less than (3, 4).
A lexicographic ordering can be defined on the Cartesian product of n posets (A1,⪯1),(A2,⪯2),…,(An,⪯n) . Define the partial ordering ⪯ on A1×A2×⋯×An by
(a1,a2,…,an)≺(b1,b2,…,bn)
if a1≺1b1 , or if there is an integer i>0 such that a1=b1,…,ai=bi , and ai+1≺i+1bi+1 . In other words, one n -tuple is less than a second n -tuple if the entry of the first n -tuple in the first position where the two n -tuples disagree is less than the entry in that position in the second n -tuple.
Example 4.34.Link copiedNote that (1,2,3,5)≺(1,2,4,3) , since the entries in the first two positions of these 4-tuples agree, but in the third position the entry in the first 4-tuple, 3, is less than that in the second 4-tuple, 4. (Here the ordering on 4-tuples is the lexicographic ordering that comes from the usual “less than or equals” relation on the set of integers.)
Consider the strings ala2⋯am and b1b2⋯bn on a partially ordered set S . Suppose these strings are not equal in length. Let t be the minimum of m and n . The definition of lexicographic ordering is that the string a1a2⋯am is less than
b1b2⋯bn if and only if
where ≺ in this inequality represents the lexicographic ordering of St . In other words, to determine the ordering of two different strings, the longer string is truncated to the length of the shorter string, namely, to t=min(m,n) terms. Then the t -tuples made up of the first t terms of each string are compared using the lexicographic ordering on St . One string is less than another string if the t -tuple corresponding to the first string is less than the t -tuple of the second string, or if these two t -tuples are the same, but the second string is longer. The verification that this is a partial ordering is left as an exercise.
We can draw a directed graph of a poset (called a Hasse diagram or lattice diagram) as follows. Each element of the poset is represented by a dot, called a vertex. An arrow, called a directed edge, is drawn from element a to element b if a⪯b . The diagram (a) below is a digraph of the poset
({1,2,3,4},≤) .
Many edges in the directed graph for a finite poset do not have to be shown since they must be present. For instance, consider the directed graph for the partial ordering {(a,b)∣a≤b} on the set {l,2,3,4}, shown in diagram (a) below. Since this relation is a partial ordering, it is reflexive, and its directed graph has loops at all vertices.
Consequently, we do not have to show these loops since they must be present; in diagram (b) loops and arrows are not shown.
Because a partial ordering is transitive, we do not have to show those edges that must be present because of transitivity. For example, in diagram (c) the edges (1,3),(1,4) , and (2,4) are not shown since they must be present. If we assume that all edges are pointed “upward” (as they are drawn in the figure), we do not have to show the directions of the edges; diagram (c) does not show directions.
To draw the diagram, execute the following procedure. Start with the directed graph for this relation. Draw it so that arrows are always pointing upwards. Because a partial ordering is reflexive, a loop is present at every vertex. Remove these loops. Remove all edges whose presence is imposed by transitivity. For instance, if (a,b) and (b,c) are in the partial ordering, remove the edge (a,c) , since it must be present also. Furthermore, if (c,d) is also in the partial ordering, remove the edge (a,d) , since is must be present also. Finally, arrange each edge so that its initial vertex is below its terminal vertex (as it is drawn on paper). Remove all the arrows on the directed edges, since the direction on the edges is now indicated by which vertices are “higher” on the page.
Note that Hasse diagrams never have horizontal edges!
These steps are well-defined, and only a finite number of steps need to be carried out for a finite poset. When all the steps have been taken, the resulting diagram contains sufficient information to find the partial ordering. This diagram is called a Hasse diagram, named after the 20th century German mathematician Helmut Hasse.
Example 4.35.Link copiedDraw the Hasse diagram representing the partial ordering {(a,b):a∣b} on {1,2,3,4,6,8,12}. Example 4.36.Link copiedDraw the Hasse diagram for the poset (P(S),⊆) , where S is the set {a,b,c} .
For many applications it is useful to be able to identify certain extremal elements of partially ordered students.
For example, a prize might be given to the student in a course with the highest grade.
An element of a poset is called maximal if it is not less than any element of the poset. That is, a is maximal in the poset (S,⪯) if there is no b∈S such that a≺b . Similarly, an element of a poset is called minimal if it is not greater than any element of the poset. That is, a is minimal if there is no element b∈S such that b≺a . Maximal and minimal elements are easy to spot using a Hasse diagram. They are the elements with nothing above them or with nothing below them.
Sometimes there is an element in a poset that is greater than every other element. Such an element is called the greatest element. That is, a is the greatest element of the poset (S,⪯) if b⪯a for all b∈S . The greatest element is unique when it exists. Likewise, an element is called the least element if it is less than all the other elements in the poset. That is, a is the least element of (S,⪯) if a⪯b for all b∈S . The least element is unique when it exists.
Example 4.38.Link copiedDetermine whether the posets represented by the Hasse diagrams below have a greatest element and a least element. Determine whether the posets have maximal and minimal elements. Is a maximal element always a greatest element? Is a greatest element always a maximal element?Example 4.39.Link copiedLet S be a set. Determine whether there is a greatest element and a least element in the poset (P(S),⊆) .
Solution. The least element is the empty set, since ∅⊆T for any subset T<L of S . The set S is the greatest element in this poset, since T⊆S whenever T is a subset of S .
Solution. The integer 1 is the least element since 1∣n whenever n is a positive integer. Since there is no integer that is divisible by all positive integers, there is no greatest element.
Let (S,⪯) be a poset and A⊆S a subset of it. Then (A,⪯) is also a poset. Sometimes it is possible to find an element in S that is greater than all the elements in the subset A . An element u∈S such that a⪯u for all elements a∈A is called an upper bound of A . Likewise, an element l∈S such that l⪯a for all elements a∈A is called a lower bound of A .
Example 4.41.Link copiedFind the lower and upper bounds of the subsets {a,b,c},{j,h} , and ” {a,c,d,f} in the poset with the Hasse diagram shown below
An element x is called the least upper bound of the subset A if x is an upper bound that is less than every other upper bound of A . Since there is only one such element, if it exists, it makes
sense to call this element the least upper bound. That is, x is the least upper bound of A if a⪯x whenever a∈A , and x⪯z whenever z is an upper bound of A . Similarly, the element y is called the greatest lower bound of A if y
is a lower bound of A and z⪯y whenever z is a lower bound of A . The greatest lower bound of A is unique if it exists. The greatest lower bound and least upper bound of a subset A are denoted by glb(A) and lub(A) , respectively.
Example 4.42.Link copiedFind the greatest lower bound and the least upper bound of {b,d,g} , if they exist, in the poset shown above. Example 4.43.Link copiedFind the greatest lower bound and the least upper bound of the sets {3,9,12} and {1,2,4,5,10} if they exist, in the poset (P,∣) .
Solution. An integer is a lower bound of {3,9,12} if 3, 9, and 12 are divisible by this integer. The only such integers are 1 and 3. Since 1∣3 , 3 is the greatest lower bound of {3,9,12} . The only lower bound for the set {1,2,4,5,10} with respect to ∣ is the element 1. Hence, 1 is the greatest lower bound for {1,2,4,5,10} .
Relational calculus
There are various operations that can be performed on relations to obtain a new relation.
Let A and B be sets. Informally, a functionf from A to B is a rule that assigns, to each element x of A a unique element f(x) of B , called the image of x under f .
Formally, a function is a special type of relation. A function from A to B is a binary relation f from A to B such that for every x∈A there is exactly one y∈B such that (x,y)∈f . We never use infix notation for functions: instead, we use the notation we are already familiar with, by abbreviating (x,y)∈f as y=f(x) .
We indicate that f is a function from A to B by writing f:A→B . We call A the domain of f , denoted by Dom(f) , and B the codomain of f . Note that not every element of B has to be the image of some element of A : the set of all such elements of B is called the range or image of f , in other words
Im(f)={y:for somex∈Dom(f),y=f(x)}. Example 4.50.Link copiedLet X be the set of all real numbers between 0 and 100 inclusive, and let Y be the set of all real numbers between 32 and 212 inclusive. The function F:X→Y that assigns to each Celsius temperature c its corresponding Fahrenheit temperature F(c) is defined by F(c)=59c+32
What are the domain, codomain and image of F ?
Equality of functions
We say the functions f:A→B and g:C→D are equal if
A=C (the functions have the same domain);
B=D (the functions have the same codomain);
f(a)=g(a) for all a∈A (the functions ‘agree’ on A ).
In other words, all three things making up the function must be the same.
Proof Notice that h∘(g∘f) and (h∘g)∘f both have domain A and codomain D . For every x∈A we have
(h∘(g∘f))(x)
=h(g∘f(x)) =h(g(f(x))) =h∘g(f(x)) =((h∘g)∘f)(x)
So h∘(g∘f) and (h∘g)∘f have the same domain, and take the same values for every element of the domain, that is they are equal.
Partial functions
A partial functionf:A→B is a weaker form of a function, still having an input set A and an output set or codomain B , but with a rule f that assigns to each element a of a subset of A a unique element f(a) of B .
In other words, a partial function is defined for just some of the elements of its input set.
For example, take A=B=R and define f(x)=1/x when x is non-zero.
The domain of a partial function f:A→B is the set of all a∈A for which f(a) is defined. Hence if D is the domain of f , then the restriction h of f to D is a function h:D→B .
The range of a partial function f:A→B is still the set of values it takes, i.e. {f(a):a∈D} , where D is the domain.
A partial function f:A→B is called a total function(or just a function) if its domain is A.
Theorem 4.1If f:A→B is a function, then f has an inverse if and only if f is a bijection.
Proof: Suppose first that f is a bijection. If y∈B then there is some x∈A with f(x)=y . Since f is 1-1, this x is unique. So we can define g:B→A by
g(y)=the uniquex∈A such thatf(x)=y.
For any x∈A we have g(f(x))=x , and for every y∈B we have f(g(y))=y . So g=f−1 .
Conversely, suppose that f has an inverse. We must show f is 1-1 and onto.
fis 1-1 Suppose x,y∈A with f(x)=f(y) . Then f−1(f(x))=f−1(f(y)) , that is x=y .
fis onto Let y∈B . Put x=f−1(y) . Then y=f(x) .
So f is a bijection, as required.
Example 4.52.Link copiedLet f be the function from {a,b,c} to {1,2,3} such that f(a)=2 , f(b)=3 , f(c)=1 . Is f invertible? If it is, what is its inverse? Example 4.53.Link copiedLet f be the function from Z to Z with f(x)=x2 . Is f invertible?
What about if we restrict it to a function from N to N ?
If a function is not invertible all is not lost. We call the set of elements in the domain of a function f:S→T that are mapped to y∈T by f the preimage of y under f . This is denoted by f←(y) .
f←(y)={x:f(x)=y}
We can also consider the preimage of a set.
f←(B)={x:f(x)∈B,B⊆T} Example 4.54.Link copiedLet f be the function from Z to Z with f(x)=x2 . What is the preimage of 4? What is the preimage of {4,9} ?