n-ary relation may also be regarded as a binary relation R(A, B) with A = A1A2 …An−1and B = An.
The data in the relational model are represented as a mathematicaln-ary relation.
In other words, a relation is defined as a set ofn-tuples, and is usually represented by a table. A table is a visual representation of the relation, and the data are organized in rows and columns. The data stored in each column of the table are of the same data type.
The basic relational building block is the domain or data type (often called just type). Each row of the table represents onen-tuple (one tuple) of the relation, and the number of tuples in the relation is the cardinality of the relation. Consider the PART relation taken from [3], where this relation consists of a heading and the body. There arefive data types representing part numbers, part names, part colours, part weights, and locations in which the parts are stored. The body consists of a set ofn-tuples, and the PART relation given in Fig.2.8is of cardinality six.
For more information on the relational model and databases see [4]
A function is a relation but not every relation is a function. For example, the relation in the diagram below is not a function since there are two arrows from the elementa2A.
The domain of the function (denoted bydom f) is the set of values in A for which the function is defined. The domain of the function is A provided thatfis a total function. The codomain of the function is B. The range of the function (denotedrngf) is a subset of the codomain and consists of
rngf ¼ frjr2Bsuch thatf að Þ ¼rfor somea2Ag:
Functions may be partial or total. Apartial function(or partial mapping) may be undefined for some values of A, and partial functions arise regularly in the com- putingfield (Fig.2.9).Total functions are defined for every value inAand many functions encountered in mathematics are total.
Example 2.13 (Functions) Functions are an essential part of mathematics and computer science, and there are many well-known functions such as the trigono- metric functions sin(x), cos(x), and tan(x); the logarithmic function ln(x); the exponential functionsex; and polynomial functions.
P# PName Colour Weight City P1
P2 P3 P4 P5 P6
Nut Bolt Screw Screw Cam Cog
Red Green Blue Red Blue Red
12 17 17 14 12 19
London Paris Rome London Paris London Fig. 2.8 PART relation
Fig. 2.9 Domain and range of a partial function
(i) Consider the partial functionf:ℝ!ℝwhere f xð Þ ¼1=x ðwherex6¼0Þ:
This partial function is defined everywhere except for x= 0 (ii) Consider the functionf:ℝ!ℝwhere
f xð Þ ¼x2 Then this function is defined for allx2ℝ
Partial functions often arise in computing as a program may be undefined or fail to terminate for several values of its arguments (e.g. infinite loops). Care is required to ensure that the partial function is defined for the argument to which it is to be applied.
Consider a programPthat has one natural number as its input and which for some input values will never terminate. Suppose that if it terminates it prints a single real result and halts. ThenPcan be regarded as a partial mapping fromℕtoℝ.
P:N!R
Example 2.14 How many total functionsf:A !Bare there from AtoB(where AandBarefinite sets)?
Each element of A maps to any element of B, i.e. there are |B| choices for each elementa 2A. Since there are |A| elements in A the number of total functions is given by
B
j jj j. . .B j jB ðj jA timesÞ
¼j jBAj total functions between A and B:
Example 2.15 How many partial functionsf:A!Bare there fromAtoB(where AandBarefinite sets) ?
Each element of A may map to any element of B or to no element of B (as it may be undefined for that element of A). In other words, there are |B| + 1 choices for each element of A. As there are |A| elements in A, the number of distinct partial functions between A and B is given by
B j j þ1
ð Þðj j þB 1Þ. . .ðj j þB 1Þ ðj jA timesÞ
¼ðj j þB 1ÞjAj
2.4 Functions 43
Two partial functionsfandg are equal if 1. domf = domg
2. f(a) = g(a) for alla 2dom f.
A functionfis less defined than a functiong(fg) if the domain offis a subset of the domain ofg, and the functions agree for every value on the domain off 1. domf dom g
2. f(a) = g(a) for alla 2dom f.
The composition of functions is similar to the composition of relations. Suppose f: A!B andg: B!C theng o f: A!C is a function, and this is written asg o f (x) org(f(x)) forx2A.
The composition of functions is not commutative and this can be seen by an example. Consider the functionf: ℝ!ℝsuch that f(x) =x2and the function g:
ℝ!ℝsuch that g(x) =x+ 2. Then g o f xð Þ ¼g x2 ¼x2þ2:
f o g xð Þ ¼f xð þ2Þ ¼ðxþ2Þ2¼x2þ4xþ4:
Clearly,g o f(x)6¼f o g(x) and so composition of functions is not commutative.
The composition of functions is associative, as the composition of relations is associative and every function is a relation. Forf: A!B,g: B!C, andh: C!D we have
h o g o fð Þ ¼ðh o gÞo f A functionf: A!B isinjective(one to one) if
f að Þ ¼1 f að Þ )2 a1¼a2:
For example, consider the function f: ℝ ! ℝ with f (x) =x2. Then f(3) =f(−3) = 9 and so this function is not one to one.
A functionf: A!CB issurjective(onto) if given anyb2B there exists ana2 A such thatf(a) =b(Fig.2.10). Consider the functionf:ℝ!ℝwithf(x) =x+ 1.
Clearly, given anyr2ℝthenf (r– 1) =rand sofis onto.
A function isbijectiveif it is one to one and onto (Fig. 2.11). That is, there is a one-to-one correspondence between the elements in A and B for eachb2Bthere is a uniquea2Asuch that f(a) =b.
The inverse of a relation was discussed earlier and the relational inverse of a functionf: A!B clearly exists. The relational inverse of the function may or may not be a function.
However, if the relational inverse is a function it is denoted by f−1: B ! A.
A total function has an inverse if and only if it is bijective whereas a partial function has an inverse if and only if it is injective.
The identity function 1A: A!A is a function such that 1A(a) =afor alla2A.
Clearly, when the inverse of the function exists then we have thatf−1o f= 1Aand f−o f−1= 1B.
Theorem 2.3 (Inverse of Function)A total function has an inverse if and only if it is bijective.
Proof Suppose f:A!Bhas an inversef−1. Then we show thatfis bijective.
Wefirst show thatfis one to one.
Supposef(x1) =f(x2) then
f1ðfðx1ÞÞ ¼f1ðfðx2ÞÞ
) f1o fðx1Þ ¼f1o fðx2Þ ) 1Aðx1Þ ¼1Aðx2Þ ) x1 ¼x2
Next wefirst show thatfis onto. Letb 2Band let a=f−1(b) then f að Þ ¼fðf1ðbÞÞ ¼band sofis surjective
A B A B
1-1, Not Onto Onto, Not 1-1
a b
p q r
a b c
p q
Fig. 2.10 Injective and surjective functions
a b c
p q q
Fig. 2.11 Bijective function (One to one and Onto)
2.4 Functions 45
The second part of the proof is concerned with showing that iff: A ! B is bijective then it has an inversef−1. Clearly, sincefis bijective we have that for each a2Athere exists a uniqueb 2Bsuch thatf(a) = b.
Defineg:B!Aby lettingg(b) be the uniqueain A such thatf(a) = b. Then we have
g o f að Þ ¼g bð Þ ¼aandf o g bð Þ ¼f að Þ ¼b:
Therefore,gis the inverse off.