Now that we have a good understanding of what a function is, our next step is to consider an important operation on functions. Our purpose is not to develop the algebra of functions as completely as we did for the algebras of logic, matrices, and sets, but the reader should be aware of the similarities between the algebra of functions and that of matrices. We first define equality of functions.

**7.3.1 Function Equality**

**Definition 7.3.1 Equality of Functions.** Let*f, g*:*A→B; that is, let* *f*
and*g*both be functions from*A* into*B. Thenf* is equal to*g* (denoted*f* =*g)*
if and only if*f*(x) =*g(x)*for all*x∈A.* ♢

CHAPTER 7. FUNCTIONS 141
Two functions that have different domains cannot be equal. For example,
*f* :Z*→*Zdefined by*f*(x) =*x*2 _{and} _{g}_{:}_{R}_{→}_{R} _{defined by}* _{g(x) =}_{x}*2

_{are not}

equal even though the formula that defines them is the same.

On the other hand, it is not uncommon for two functions to be equal
even though they are defined differently. For example consider the functions
*h* and *k, where* *h* : *{−*1,0,1,2*} → {*0,1,2*}* is defined by *h(x) =* *|x|* and
*k* : *{−*1,0,1,2*} → {*0,1,2*}* is defined by *k(x) =* *−x*_{3}3 +*x*2_{+} *x*

3 appear to be

very different functions. However, they are equal because *h(x) =* *k(x)* for
*x*=*−*1,0,1, and2.

**7.3.2 Function Composition**

One of the most important operations on functions is that of composition.
**Definition 7.3.2 Composition of Functions.** Let*f* :*A→B*and*g*:*B* *→*
*C. Then the composition of* *f* followed by*g, writteng◦f*, is a function from
*A*into*C* defined by(g*◦f)(x) =g(f*(x)), which is read “g of*f* of*x.”* ♢
The reader should note that it is traditional to write the composition of
functions from right to left. Thus, in the above definition, the first function
performed in computing *g* *◦f* is *f. On the other hand, for relations, the*
composition*rs* is read from left to right, so that the first relation is*r.*
**Example 7.3.3 A basic example.** Let *f* : *{*1,2,3*} → {a, b}* be defined
by *f(1) =* *a,* *f(2) =* *a, and* *f*(3) = *b. Let* *g* : *{a, b} → {*5,6,7*}* be defined
by *g(a) = 5* and *g(b) = 7. Then* *g◦f* : *{*1,2,3*} → {*5,6,7*}* is defined by
(g*◦f*)(1) = 5, (g*◦f*)(2) = 5,and (g*◦f*)(3) = 7. For example, (g*◦f*)(1) =
*g(f*(l)) =*g(a) = 5. Note thatf◦g* is not defined. Why?

Let*f* :R*→*R be defined by*f*(x) =*x*3 and let *g* : R*→*R be defined by
*g(x) = 3x*+ 1. Then, since

(g*◦f*)(x) =*g(f*(x)) =*g*(*x*3)= 3x3+ 1

we have*g◦f* :R*→*Ris defined by(g*◦f*)(x) = 3x3_{+1. Here}_{f}_{◦}_{g}_{is also defined}

and*f◦g*:R*→*Ris defined by(f*◦g)(x) = (3x+1)*3_{. Moreover, since}_{3x}3_{+1}_{̸}_{=}

(3x+1)3_{for at least one real number,}_{g}_{◦}_{f}_{̸}_{=}_{f}_{◦}_{g. Therefore, the commutative}

law is not true for functions under the operation of composition. However, the
associative law is true for functions under the operation of composition. □
**Theorem 7.3.4 Function composition is associative.** *If* *f* : *A* *→* *B,*
*g*:*B→C, andh*:*C→D, thenh◦*(g*◦f*) = (h*◦g)◦f.*

*Proof.* Note: In order to prove that two functions are equal, we must use the
definition of equality of functions. Assuming that the functions have the same
domain, they are equal if, for each domain element, the images of that element
under the two functions are equal.

We wish to prove that(h*◦*(g*◦f*))(x) = ((h*◦g)◦f*)(x)for all*x∈A, which*
is the domain of both functions.

(h*◦*(g*◦f*))(x) =*h((g◦f*)(x))by the definition of composition
=*h(g(f*(x))) by the definition of composition
Similarly,

((h*◦g)◦f*)(x) = (h*◦g)(f*(x))by the definition of composition
=*h(g(f*(x))) by the definition of composition .
Notice that no matter how the functions the expression*h◦g◦f* is grouped,

CHAPTER 7. FUNCTIONS 142
the final image of any element of*x∈A*is*h(g(f*(x)))and so*h◦*(g*◦f*) = (h*◦g)◦f*.
■
If*f* is a function on a set*A, then the compositions* *f◦f*,*f◦f◦f, . . .*are
valid, and we denote them as *f*2 , *f*3*, . . .. Repeated compositions of* *f* with
itself can be defined recursively. We will discuss this form of definition in detail
inSection 8.1.

**Definition 7.3.5 Powers of Functions.** Let*f* :*A→A.*
• *f*1=*f*; that is,*f*1(a) =*f*(a), for*a∈A.*

• For*n≥*1,*fn*+1 =*f* *◦fn*; that is,*fn*+1(a) =*f*(f*n*(a))for*a∈A.*
♢
Two useful theorems concerning composition are given below. The proofs
are left for the exercises.

**Theorem 7.3.6** **The composition of injections is an injection.** *If*
*f* :*A→B* *andg*:*B→C* *are injections, then* *g◦f* :*A→C* *is an injection.*
**Theorem 7.3.7** **The composition of surjections is a surjection.** *If*
*f* :*A→B* *andg*:*B→C* *are surjections, then* *g◦f* :*A→C* *is a surjection.*

We would now like to define the concepts of identity and inverse for func-
tions under composition. The motivation and descriptions of the definitions of
these terms come from the definitions of the terms in the set of real numbers
and for matrices. For real numbers, the numbers 0 and 1 play the unique role
that *x*+ 0 = 0 +*x* =*x*and *x·*1 = 1*·x*=*x*for any real number *x. 0 and*
1 are the identity elements for the reals under the operations of addition and
multiplication, respectively. Similarly, the*n×n*zero matrix 0 and the*n×n*
identity matrix*I*are such that for any*n×n*matrix*A,A*+ 0 = 0 +*A*=*A*and
*AI* =*IA*=*I. Hence, an elegant way of defining the identity function under*
the operation of composition would be to imitate the above well-known facts.
**Definition 7.3.8 Identity Function.** For any set*A, the identity function*
on*A*is a function from*A*onto*A, denoted byi*(or, more specifically,*iA*) such

that*i(a) =a*for all*a∈A.* ♢

Based on the definition of*i, we can show that for all functionsf* :*A→A,*
*f◦i*=*i◦f* =*f*.

**Example 7.3.9 The identity function on** *{*1,2,3*}***.** If *A*=*{*1,2,3*}*, then
the identity function*i*:*A→A*is defined by*i(1) = 1,i(2) = 2,*and *i(3) = 3.*
□
**Example 7.3.10 The identity function on** R. The identity function on

Ris*i*:R*→*Rdefined by*i(x) =x.* □

**7.3.3 Inverse Functions**

We will introduce the inverse of a function with a special case: the inverse of a function on a set. After you’ve taken the time to understand this concept, you can read about the inverse of a function from one set into another. The reader is encouraged to reread the definition of the inverse of a matrix in Section 5.2 (Definition 5.2.5) to see that the following definition of the inverse function is a direct analogue of that definition.

**Definition 7.3.11 Inverse of a Function on a Set.** Let *f* : *A* *→A. If*
there exists a function*g*:*A→A* such that*g◦f* =*f* *◦g*=*i, theng* is called

CHAPTER 7. FUNCTIONS 143
the inverse of*f* and is denoted by*f−*1 , read “f inverse.” ♢
Notice that in the definition we refer to “the inverse” as opposed to “an
inverse.” It can be proven that a function can never have more than one inverse
(see exercises).

An alternate description of the inverse of a function, which can be proven
from the definition, is as follows: Let*f* :*A→A* be such that*f*(a) =*b. Then*
when it exists,*f−*1_{is a function from}_{A}_{to}_{A}_{such that}* _{f}−*1

_{(b) =}

_{a. Note that}*f−*1 _{“undoes” what}_{f}_{does.}

**Example 7.3.12 The inverse of a function on***{*1,2,3*}***.** Let*A*=*{*1,2,3*}*
and let *f* be the function defined on *A* such that *f*(1) = 2, *f(2) = 3, and*
*f(3) = 1. Then* *f−*1 _{:} _{A}_{→}_{A}_{is defined by} * _{f}−*1

_{(1) = 3,}

*1*

_{f}−_{(2) = 1, and}

*f−*1_{(3) = 2.} _{□}

**Example 7.3.13 Inverse of a real function.** If *g* :R*→*R is defined by
*g(x) =x*3 _{, then}* _{g}−*1

_{is the function that undoes what}

_{g}_{does. Since}

_{g}_{cubes}

real numbers, *g−*1 _{must be the “reverse” process, namely, takes cube roots.}

Therefore, *g−*1 _{:} _{R} _{→}_{R} _{is defined by} * _{g}−*1

_{(x) =}

*√*3

_{x. We should show that}*g−*1

_{◦}_{g}_{=}

_{i}_{and}

*1*

_{g}_{◦}_{g}−_{=}

_{i. We will do the first, and the reader is encouraged}to do the second.

(

*g−*1*◦g*)(x) =*g−*1(g(x)) Definition of composition
=*g−*1(*x*3) Definition of*g*

=*√*3*x*3 _{Definition of}* _{g}−*1

=*x* Definition of cube root

=*i(x)* Definition of the identity function

Therefore,*g−*1_{◦}_{g}_{=}_{i. Why?}_{□}

The definition of the inverse of a function alludes to the fact that not all functions have inverses. How do we determine when the inverse of a function exists?

**Theorem 7.3.14 Bijections have inverses.** *Let* *f* :*A→A.* *f−*1 _{exists if}

*and only if f is a bijection; i. e. f is one-to-one and onto.*

*Proof.* (*⇒*) In this half of the proof, assume that*f−*1exists and we must prove
that *f* is one-to-one and onto. To do so, it is convenient for us to use the
relation notation, where*f*(s) =*t* is equivalent to(s, t)*∈f*. To prove that*f* is
one-to-one, assume that*f*(a) =*f*(b) =*c. Alternatively, that means*(a, c)and
(b, c) are elements of *f* . We must show that *a* =*b. Since* (a, b),(c, b) *∈* *f*,
(c, a) and (c, b) are in *f−*1_{. By the fact that} * _{f}−*1

_{is a function and}

_{c}_{cannot}

have two images,*a*and*b* must be equal, so*f* is one-to-one.

Next, to prove that*f* is onto, observe that for*f−*1_{to be a function, it must}

use all of its domain, namely A. Let *b* be any element of *A. Then b has an*
image under*f−*1 _{,} * _{f}−*1

_{(b). Another way of writing this is}(

*1*

_{b, f}−_{(b)})

*1*

_{∈}_{f}−_{,}

By the definition of the inverse, this is equivalent to(*f−*1(b), b)*∈f*. Hence,*b*
is in the range of*f*. Since*b* was chosen arbitrarily, this shows that the range
of*f* must be all of *A.*

(*⇐*) Assume*f* is one-to-one and onto and we are to prove*f−*1 _{exists. We}

leave this half of the proof to the reader. □ ■
**Definition 7.3.15 Permutation.** A bijection of a set *A*into itself is called

a permutation of*A.* ♢

Next, we will consider the functions for which the domain and codomain are not necessarily equal. How do we define the inverse in this case?

CHAPTER 7. FUNCTIONS 144
**Definition 7.3.16 Inverse of a Function (General Case).** Let*f* :*A→B,*
If there exists a function*g*:*B* *→A*such that*g◦f* =*iA*and*f◦g*=*iB* , then

*g*is called the inverse of*f* and is denoted by*f−*1 , read “f inverse.” ♢
Note the slightly more complicated condition for the inverse in this case
because the domains of *f◦g* and*g◦f* are different if*A*and *B* are different.
The proof of the following theorem isn’t really very different from the special
case where*A*=*B.*

**Theorem 7.3.17 When does a function have an inverse?** *Letf* :*A→B.*
*f−*1 _{exists if and only if f is a bijection.}

**Example 7.3.18 Another inverse.** Let *A* = *{*1,2,3*}* and *B* = *{a, b, c}*.
Define *f* : *A* *→* *B* by *f*(1) = *a,* *f(2) =* *b, and* *f*(3) = *c. Then* *g* : *B* *→* *A*
defined by*g(a) = 1,g(b) = 2, andg(c) = 3*is the inverse of*f*.

(g*◦f*)(1) = 1
(g*◦f*)(2) = 2
(g*◦f*)(3) = 3
*⇒* *g◦f* =*iA* and
(f*◦g)(a) =a*
(f*◦g)(b) =b*
(f*◦g)(c) =c*
*⇒* *f* *◦g*=*iB*
□

**7.3.4 Exercises for Section 7.3**

**1.** Let *A* = *{*1,2,3,4,5*}*, *B* = *{a, b, c, d, e, f}*, and *C* = *{*+,*−}*. Define
*f* : *A* *→* *B* by *f*(k) equal to the *kth* _{letter in the alphabet, and define}

*g*:*B→C* by*g(α) = +*if*α*is a vowel and *g(α) =−*if*α*is a consonant.
(a) Find *g◦f*.

(b) Does it make sense to discuss*f* *◦g? If not, why not?*
(c) Does*f−*1 _{exist? Why?}

(d) Does*g−*1 _{exist? Why?}

**2.** Let*A*=*{*1,2,3*}*. Definef :*A→A*by*f*(1) = 2, *f*(2) = 1, and*f*(3) = 3.
Find*f*2_{,}* _{f}*3

_{,}

*4*

_{f}_{and}

*1*

_{f}−_{.}

**3.** Let*A*=*{*1,2,3*}*.

(a) List all permutations of*A.*

(b) Find the inverse and square of each of the permutations of part a,
where the square of a permutation,*f*, is the composition *f◦f*.
(c) Show that the composition of any two permutations of *A* is a per-

mutation of*A.*

(d) Prove that if*A* is any set where*|A|*=*n, then the number of per-*
mutations of*A* is*n!.*

**4.** Define*s,u, andd, all functions on the integers, bys(n) =n*2_{,}_{u(n) =}_{n+1,}

and*d(n) =n−*1. Determine:
(a) *u◦s◦d*

CHAPTER 7. FUNCTIONS 145
(c) *d◦s◦u*

**5.** Based on the definition of the identity function, show that for all functions
*f* :*A→A,f◦i*=*i◦f* =*f*.

**6.** **Inverse images.** If*f* is any function from*A*into*B, we can describe the*
inverse image as a function from *B* into *P*(A), which is also commonly
denoted *f−*1_{. If}_{b}_{∈}_{B}_{,}* _{f}−*1

_{(b) =}

_{{}_{a}_{∈}_{A}_{|}

_{f(a) =}_{b}_{}}_{. If}

_{f}_{does have an}

inverse, the inverse image of*b*is{*f−*1(b)}.

(a) Let *g* :R*→*R be defined by*g(x) =* *x*2. What are *g−*1(4), *g−*1(0)
and*g−*1(*−*1)?

(b) If*r*:R*→*Z, where*r(x) =⌈x⌉*, what is*r−*1_{(1)?}

**7.** Let*f, g, andh*all be functions from Zinto Z defined by*f*(n) =*n*+ 5,
*g(n) =n−*2,and*h(n) =n*2_{. Define:}

(a) *f◦g*
(b) *f*3
(c) *f◦h*

**8.** Define the following functions on the integers by*f*(k) =*k*+ 1,*g(k) = 2k,*
and*h(k) =⌈k/2⌉*

(a) Which of these functions are one-to-one? (b) Which of these functions are onto?

(c) Express in simplest terms the compositions *f◦g,g◦f*,*g◦h,h◦g,*
and*h*2,

**9.** Let*A*be a nonempty set. Prove that if*f* is a bijection on*A*and*f◦f* =*f*,
then*f* is the identity function,*i*

**Hint. You have seen a similar proof in matrix algebra.**

**10.** For the real matrix*A*=

(

*a* *b*
*c* *d*

)

, det(A) =*ad−bc.*

Recall that a **bijection** from a set to itself is also referred to as a
**permutation**of the set. Let*π*be a permutation of*{a, b, c, d}*such that
*a*becomes*π(a),b*becomes *π(b), etc.*

Let *B* =

(

*π(a)* *π(b)*
*π(c)* *π(d)*

)

. How many permutations of *π* leave the
determinant of*A*invariant, that is, det*A*=det*B?*

**11.** State and prove a theorem on inverse functions analogous to the one that
says that if a matrix has an inverse, that inverse is unique.

**12.** Let *f* and *g* be functions whose inverses exist. Prove that (f *◦g)−*1 _{=}

*g−*1* _{◦}_{f}−*1

_{.}

**Hint. See Exercise 3 of Section 5.4.**
**13.** ProveTheorem 7.3.6andTheorem 7.3.7.
**14.** Prove the second half ofTheorem 7.3.14.

**15.** Prove by induction that if *n* *≥*2 and *f*1, *f*2 , \ldots , *fn* are invertible

functions on some nonempty set*A, then { }(f*1*◦f*2*◦ · · · ◦fn*)*−*1=*fn−*1*◦*

CHAPTER 7. FUNCTIONS 146
**16.**

(a) Our definition of cardinality states that two sets, *A* and *B, have*
the same cardinality if there exists a bijection between the two sets.
Why does it not matter whether the bijection is from *A* into *B* or
*B* into*A?*

(b) Prove that “has the same cardinality as” is an equivalence relation on sets.

**17.** Construct a table listing as many “Laws of Function Composition” as you
can identify. Use previous lists of laws as a guide.

**Chapter 8**

**Recursion and Recurrence Re-**

**lations**

An essential tool that anyone interested in computer science must master is how to think recursively. The ability to understand definitions, concepts, al- gorithms, etc., that are presented recursively and the ability to put thoughts into a recursive framework are essential in computer science. One of our goals in this chapter is to help the reader become more comfortable with recursion in its commonly encountered forms.

A second goal is to discuss recurrence relations. We will concentrate on methods of solving recurrence relations, including an introduction to generating functions.

**8.1 The Many Faces of Recursion**

Consider the following definitions, all of which should be somewhat familiar to you. When reading them, concentrate on how they are similar.

**8.1.1 Binomial Coefficients**

Here is a recursive definition of binomial coefficients, which we introduced in Chapter 2.

**Definition 8.1.1 Binomial Coefficient - Recursion Definition.** Assume
*n≥*0 and*n≥k≥*0. We define(*n*
*k*
)
by
• (*n*
0
)
= 1
• (*n*
*n*
)
= 1and
• (*n*
*k*
)
=(*n− _{k}*1)+(

*n*1

_{k}_{−}−_{1})if

*n > k >*0 ♢

**Observation 8.1.2**A word about definitions: Strictly speaking, when mathe- matical objects such as binomial coefficents are defined, they should be defined just once. Since we defined binomial coefficients earlier, in Definition 2.4.3, other statements describing them should be theorems. The theorem, in this case, would be that the “definition” above is consistent with the original defini-

CHAPTER 8. RECURSION AND RECURRENCE RELATIONS 148 tion. Our point in this chapter in discussing recursion is to observe alternative definitions that have a recursive nature. In the exercises, you will have the opportunity to prove that the two definitions are indeed equivalent.

Here is how we can apply the recursive definition to compute (5 2 ) . ( 5 2 ) = ( 4 2 ) + ( 4 1 ) = ( ( 3 2 ) + ( 3 1 ) ) + ( ( 3 1 ) + ( 3 0 ) ) = ( 3 2 ) + 2 ( 3 1 ) + 1 = ( ( 2 2 ) + ( 2 1 ) ) + 2( ( 2 1 ) + ( 2 0 ) ) + 1 = (1 + ( 2 1 ) ) + 2( ( 2 1 ) + 1) + 1 = 3 ( 2 1 ) + 4 = 3( ( 1 1 ) + ( 1 0 ) ) + 4 = 3(1 + 1) + 4 = 10

**8.1.2 Polynomials and Their Evaluation**

**Definition 8.1.3 Polynomial Expression in***x***over***S* **(Non-Recursive).**
Let*n* be an integer, *n* *≥*0. An *n*th degree polynomial in *x* is an expression
of the form*anxn*+*an−*1*xn−*1+*· · ·*+*a*1*x*+*a*0, where *an, an−*1*, . . . , a*1*, a*0 are

elements of some designated set of numbers, *S, called the set of coefficients*

and*an* *̸*= 0. ♢

We refer to *x*as a variable here, although the more precise term for *x*is
an*indeterminate. There is a distinction between the terms indeterminate and*
variable, but that distinction will not come into play in our discussions.

Zeroth degree polynomials are called constant polynomials and are simply elements of the set of coefficients.

This definition is often introduced in algebra courses to describe expressions
such as*f*(n) = 4n3+ 2n2*−*8n+ 9, a third-degree, or cubic, polynomial in*n.*
This definition has a drawback when the variable is given a value and the
expression must be evaluated. For example, suppose that *n* = 7. Your first
impulse is likely to do this:

*f*(7) = 4*·*73+ 2*·*72*−*8*·*7 + 9
= 4*·*343 + 2*·*49*−*8*·*7 + 9
= 1423

A count of the number of operations performed shows that five multiplica-
tions and three additions/subtractions were performed. The first two multipli-
cations compute72 _{and} _{7}3_{, and the last three multiply the powers of 7 times}

the coefficients. This gives you the four terms; and adding/subtracting a list
of*k*numbers requires*k−*1addition/subtractions. The following definition of
a polynomial expression suggests another more efficient method of evaluation.

CHAPTER 8. RECURSION AND RECURRENCE RELATIONS 149
**Definition 8.1.4 Polynomial Expression in***x***over** *S* **(Recursive).** Let
*S* be a set of coefficients and*x*a variable.

(a) A zeroth degree polynomial expression in*x*over*S* is a nonzero element
of*S.*

(b) For*n≥*1, an*nth*_{degree polynomial expression in}_{x}_{over}_{S}_{is an expres-}

sion of the form*p(x)x*+*a*where *p(x)*is an(n*−*1)*st* _{degree polynomial}

expression in*x*and*a∈S.*

♢
We can easily verify that *f*(n) = 4n3+ 2n2*−*8n+ 9 is a third-degree