1 / 25

# CS1022 Computer Programming & Principles - PowerPoint PPT Presentation

CS1022 Computer Programming & Principles. Lecture 5.1 Functions (1). Plan of lecture. Why functions? Inverse relations and composition of relations (background to functions) Functions Terminology Digraphs and graphs of functions Properties of functions. Why functions?.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about 'CS1022 Computer Programming & Principles' - misae

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### CS1022Computer Programming & Principles

Lecture 5.1

Functions (1)

• Why functions?

• Inverse relations and composition of relations (background to functions)

• Functions

• Terminology

• Digraphs and graphs of functions

• Properties of functions

CS1022

• Functions capture transformations

• Input arguments used to compute output results

• Input/output is key to computing

• No commitments to computers/architectures

• Abstract, clean, compact, focussed

• Functions represent computations

• Underpinned by -calculus (a kind of “interpreter”)

• What is x's age? Given a value for x, output the result: What is x's age?(Bill) => 34

CS1022

• Factorial: 5! = 5  4  3 2 1 = 120

• Formally: or

CS1022

• Given a binary relation R between sets A and B

• The inverse relation R–1 between A and B is

R–1 (b, a) : (a, b)  R

• For instance, the inverse of relation is a parent of on the set of People is the relation is a child of

• Graphically, the inverse of a binary relation is obtained by reversing all arrows in the digraph of original relation

R (p,1), (p,2), (q,2)

R–1 (1, p), (2, p), (2, q)

R

R–1

1

1

2

2

p

p

q

q

CS1022

• Let R be a binary relation between sets A and B

• Let S be a binary relation between sets B and C

• The composition of R and S, represented as SR, is a binary relation between A and C given by

SR(a,c) : aA, cC, aR b, bS c for some bB

• The new relation relates elements of A and C using elements of B as “intermediaries”

CS1022

• If R is a relation “is a sister of” on a set of people

• If S is a relation “is the mother of” on the same set

• The compositionSR is

SR(a,c) : ais a sister of b, bis mother of c

SR(a,c) : ais a sister of themother of c

SR(a,c) : ais an aunt of c

SS(a,c) : ais mother ofb, bis mother of c

SS(a,c) : ais mother ofthe mother of c

SS(a,c) : ais a grandmother of c

CS1022

• Let R and S be binary relations defined as

Then

aR 1 and 1 Sy implies (a,y)  SR

aR 2 and 2 Sx implies (a,x)  SR

aR 3 and 3 Sx implies (a,x)  SR

bR 2 and 2 Sx implies (b,x)  SR

R(a,1), (a,2), (a,3), (b,2)

2

1

2

3

S(1,y), (2,x), (3,x)

R

SR

S

1

x

x

y

y

3

a

a

b

b

CS1022

• Relations represented as a matrices for

• It easy to see relationship properties by inspecting the matrix, e.g. relationship is reflexive where it is 1 on the middle diagonal.

• Computers view relations as a two dimensional array

• How to compute compositions using matrices?

• Problem:

• Given the logical matrices of two relations R and S

• Compute the logical matrix of their composition SR

• Result is logical (or Boolean) matrix product

CS1022

• Let

Aa1,a2,,an Bb1,b2,,bm Cc1,c2,,cp

• Relations

R relates A and B S relates B and C

• The logical matrix M representing R is given by

• M(i, j)  T (true) if (ai, bj)  R

• M(i, j)  F (false) if (ai, bj)  R

• The logical matrix N representing S is given by

• N(i, j)  T (true) if (bi, cj)  S

• N(i, j)  F (false) if (bi, cj)  S

CS1022

• Logical matrix P represents SR

• If there is bk B with aiRbkandbkScj then

M(i, k) = T andN(k, j)  T

Since ai (SR)cj then P(i, j) is also T

• Otherwise

M(i, k) = F orN(k, j)  F, for all k,

and P(i, j)  F

• Logical matrix P representing SR is given by

• P(i, j)  T (true) if M(i, k)  T andN(k, j)  T, for some k

• P(i, j)  F (false) otherwise

bk is the bridge between the relations that allows the product relation.

CS1022

A Boolean product of two matrices, where the matrices have T/F values (1/0) –

It is the conjunction (and) of the disjunctions (ors), where a, b, and c reference the matrix and xi,jis the row and column of x in the matrix:

etc...

etc...

Find a pair with a bridge between relations and mark 1/0 in result.

R bill jillphilmary

bill 1 0 0 0

jill 0 1 0 0

phil 1 0 1 0

mary 1 0 1 0

S bill jillphilmary

bill 0 11 0

jill 0 1 1 0

phil0 0 1 1

mary 1 100

R S R S

c1,2 is the same row in R, but the next column in S.

• Alternatively,

P(i, j)  (M(i, 1) andN(1, j))

or (M(i, 2) andN(2, j))

...

or (M(i, n) andN(n, j))

• We write

PMN

For this Boolean matrix product

CS1022

• Functions: special kind of (restricted) relation

• Function from A to Bis a binary relation in which

• Every element of A is associated with a uniquely specified element of B

• That is, for all a A, there is exactly one pair (a, b)

• In terms of digraphs, a function is a relation with precisely one arc leaving every element of A

CS1022

Are these relations functions? Justify.

• Relation x is a sibling of y on the set of People

• No, since a person x may have several siblings (or none)

• R = (x, x2) : x  Z (defined on Z)

• Yes, since for any integer x, the value x2 is unique

• S = (x, y) : x  y2 (defined on R)

• No, because (2, 2) and (2, 2) belong to S

CS1022

• Let f be a function from set A to set B

• For each x A there exists a unique y B,(x, y)  f

• We write y f(x)

• We refer to f(x) as the image of x under f

• We also write f : A B and it reads:

• f is a function which transforms/maps each element of A to a uniquely determined element of set B

CS1022

• A is the domain of f and B is the co-domain of f

• The range of f is the set of images of all elements of A under f, and is denoted as f(A)

f(A) f(x) : x A

• Venn diagram:

A

B

f

f(A)

CS1022

• When f : A B and sets A, B are infinite, we cannot draw a digraph of the function

• We can plot the pairs to build a picture of f

• For instance, f : R R, f(x)  x2 is as follows:

y

• Horizontal axis xis the domain R

• Vertical axis yis the co-domain R

• Curve consists of points (x, y) in the Cartesian product R R for which f(x)  x2

(2, 4)

x

CS1022

• Let f : A B be a function

• We say f is injective (or one-to-one) function if

a1a2 ((a1, a2 A) and f(a1)  f(a2))  a1  a2

• That is,

If output is the same then input must be the same

• An injective function never repeats values

• Different inputs give different outputs:

a1a2 ((a1, a2 A and a1  a2)  f(a1)  f(a2))

CS1022

• Let f : A B be a function

• f is surjective (or onto) if its range coincides with its co-domain

• For every bBthere is an a A with b  f(a)

b a (bB and a A and b  f(a))

• In other words:

• Each element of the co-domain is a value of the function

CS1022

• Let f : A B be a function

• f is bijective if it is both injective and surjective

CS1022

• Consider the functions below (as graphs)

• Not injective as f(a)  f(b)  1

• Not surjective as 2 in codomain is not a value of anyf(x)

a

a

a

1

1

1

b

b

b

2

2

2

c

c

c

3

3

3

• Is injective as f(x) are all different

• Is surjective as range and codomain are equal

• Is bijective (injective and surjective)

• Is injective as f(x) are all different

• Not surjective as 2 in codomain is not a value of anyf(x)

CS1022

You should now know:

• Inverse relations and composition of relations

• Functions as a (special type) of relations

• Properties of functions and some terminology

CS1022

• R. Haggarty. “Discrete Mathematics for Computing”. Pearson Education Ltd. 2002. (Chapter 5)

• Wikipedia’s entry

• Wikibooks entry

CS1022