Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Discrete math with computers_3

.pdf
Скачиваний:
84
Добавлен:
16.03.2016
Размер:
2.29 Mб
Скачать

10.9. EQUIVALENCE RELATIONS

263

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

...

...

...

Figure 10.25: A Congruence Equivalence Relation

then a is congruent to b (modulo k). The mathematical notation for this statement is

a ≡ b (mod k).

It is necessary to ensure that k is a positive integer—positive means greater than 0—in order to avoid dividing by 0. We can define a relation, called the congruence relation, for all k:

Definition 60. The congruence relation Ck is defined for all natural k such that k > 0, as follows: aCk b if and only if a ≡ b (mod k).

The congruence relation is useful because it is an equivalence relation:

Theorem 75. For all natural k > 0, the congruence relation Ck is an equivalence relation.

Example 85. Consider partitioning the integers by congruence (C3) (see Figure 10.25). This gives rise to three sets: all the integers that are of the form n × 3 (this is just the set of multiples of 3), the integers of the form n × 3 + 1, and the integers of the form n × 3 + 2.

The following functions in the software tools module create the smallest possible equivalence relation from a digraph and determine whether a given relation is an equivalence relation. They do this by taking a digraph and calculating its transitive symmetric reflexive closure.

equivalenceRelation ::

(Eq a, Show a) => Digraph a -> Digraph a isEquivalenceRelation ::

(Eq a, Show a) => Digraph a -> Bool

Exercise 42. Evaluate the following expressions using the computer:

equivalenceRelation ([1,2],[(1,1),(2,2),(1,2),(2,1)]) equivalenceRelation ([1,2,3],[(1,1),(2,2)])

isEquivalenceRelation ([1,2],[(1,1),(2,2),(1,2),(2,1)]) isEquivalenceRelation ([1],[])

264

CHAPTER 10. RELATIONS

Exercise 43. Does the topological sort require that the graph’s relation is a partial order?

Exercise 44. Can the graph given to a topological sort have cycles?

10.10Suggestions for Further Reading

Extensive discussions of relations can be found in Discrete Mathematics in Computer Science, by Stanat and McAllister [29], and Discrete Mathematics, by Ross and Wright [26].

Relations are a basic tool used for a wide variety of applications. Two good examples are relational data bases [7] and circuit design [19].

10.11Review Exercises

Exercise 45. Which of the following relations is an equivalence relation?

(a)InTheSameRoomAs

(b)IsARelativeOf

(c)IsBiggerThan

(d)The equality relation

Exercise 46. Given a non-empty antisymmetric relation, does its transitive closure ever contain symmetric arcs?

Exercise 47. What relation is both a quasi order and an equivalence relation?

Exercise 48. Write a function that takes a relation and returns True if that relation has a power that is the given relation.

Exercise 49. A quasi order is transitive and irreflexive. Can it have any symmetric loops in it?

Exercise 50. Given an antisymmetric irreflexive relation, could its transitive closure contain reflexive arcs?

Exercise 51. Write a function that takes a relation and returns True if all of its powers have fewer arcs than it does.

Exercise 52. Write a function that takes a relation and returns True if the relation is smaller than its symmetric closure.

Exercise 53. Given the partial order

{(A, B), (B, C), (A, D)},

which of the following is not a topological sort?

10.11. REVIEW EXERCISES

265

[D,C,B,A]

[C,B,D,A]

[D,C,A,B]

Exercise 54. Is a reflexive and symmetric relation ever antisymmetric as well?

Exercise 55. Given a relation containing only a single path of length n, how many arcs can be added by its symmetric transitive closure?

Exercise 56. Given a relation containing only a cycle of length n containing all of the nodes in the domain, which power will be reflexive?

Exercise 57. Can we write a function that determines whether the equality relation over the positive integers is reflexive?

Exercise 58. Why can’t partial orders have cycles of length greater than 1?

Exercise 59. Is the last power of a relation always the empty set?

Exercise 60. The following list comprehension gives the arcs of a poset diagram. What kind of order relation does the diagram represent?

[(a,a+1) | a <- [1..]]

Exercise 61. Is the composition of a relation containing only a single cycle with its converse the equality relation?

Exercise 62. Give examples of partial orders in which the set of greatest elements is the same as the set of weakest elements.

Chapter 11

Functions

A function is an abstract model of computation: you give it some input, and it produces a result. The essential aspect is that the result is completely determined by the input: if you repeatedly apply the same function to the same argument, you will always obtain the same result. Examples of functions include:

An inquiry to a telephone directory service: you supply a person’s name, and the service provides the corresponding telephone number;

The mathematical sin function: you give it an angle, and the function returns the sin of that angle;

An addition circuit in a computer’s processor; you give it a pair of binary numbers, and it returns the binary representation of their sum.

In contrast, a weather prediction service would not be modelled as a function, because the answer to ‘What will the weather be tomorrow?’ changes from day to day.

Functions are an important tool, both in mathematics and in computing, because they provide a mechanism for abstraction. A function is a ‘black box’: to use it, you need to know the interface, but not the internal details about how the function is defined.

There are many ways to formalise the function concept. This chapter looks at the ones that are most important for computer science. We start with one of the most common mathematical approaches, which treats a function as a special kind of relation, and then we will consider a more algorithmic way of defining functions. It is good to remember, however, that the concept of ‘function’ is abstract, and there are many di erent formalisms that can be used to define it.

267

268

CHAPTER 11. FUNCTIONS

11.1The Graph of a Function

In this section, we examine one of the most common mathematical techniques for defining functions. This approach uses a set of ordered pairs, and it brings out a close connection between functions and relations.

A function specifies, for any particular input value x, the value y of the result. This can be represented as an ordered pair (x, y), and the entire function is represented as a set of ordered pairs. This representation of a function is called a function graph, and it is similar to the digraph used to represent relations.

A function is a relation, but some additional properties are required. A relation digraph is a set of ordered pairs, with no restrictions: thus, a relation R might contain two ordered pairs (x, y1) and (x, y2), and we would write x R y1 and also x R y2. This would not be acceptable for a function, though, because it would mean that given the argument x there are two possible results y1 and y2. A function must return a unique result for any argument. Accordingly, we define a function to be a relation with an extra requirement that only one result may be specified for each argument:

Definition 61. Let A and B be sets. A function f with type A → B is a relation with domain A and codomain B, such that

x A. y1 B. y2 B. ((x, y1) f (x, y2) f ) → y1 = y2.

A is called the argument type and B is called the result type of the function.

The definition says that a function is a set of ordered pairs, just like a relation. The set of ordered pairs is called the graph of the function. If an ordered pair (x, y) is a member of the function, then x A and y B. Furthermore, the definition states formally that if the result of applying a function to an argument x could be y1 but it could alternatively be y2, then it must be that y1 = y2. This is just a way of saying that there is a unique result corresponding to each argument.

Example 86. The set {(1, 4), (1, 5)} cannot be the graph of a function, because it contains two pairs with the same first element but di erent second elements: (1, 4) and (1, 5) (Figure 11.1). A function must return just one result for any argument; it can’t choose among several alternatives.

Example 87. The set of ordered pairs {(1, 2), (2, 2), (3, 4)} is the graph of a function. It doesn’t matter that several arguments, 1 and 2, produce the same result 2.

An expression denoting the result produced by a function when presented with an input x is called a function application. For example, sin(2 × π) is an application of the sin function to the argument 2 × π. The following definition specifies the syntax, type, and value of a function application:

11.1. THE GRAPH OF A FUNCTION

269

1 4

2 5

36

Figure 11.1: A Relation That Is Not a Function

Definition 62. An application of the function f to the argument x, provided that f :: A → B and x :: A, is written as either f x or as f (x), and its value is y if the ordered pair (x, y) is in the graph of f ; otherwise the application of f to x is undefined:

f x = y ↔ (x, y) f

A type can be thought of as the set of possible values that a variable might have. Thus the statement ‘x :: A’, which is pronounced ‘x has type A’, is equivalent to x A. The type of the function is written as A → B, which suggests that the function takes an argument of type A and transforms it to a result of type B. This notation is a clue to an important intuition: the function is a black box (you can think of it as a machine) that turns arguments into results.

If x A and there is a pair (x, y) f , then we say that ‘f x is defined to be y’. However, if x A but there is no pair (x, y) f , we say ‘f x is undefined’. A shorthand mathematical notation for saying ‘f x is undefined’ is ‘f x = ’, where the symbol denotes an undefined value.

It often turns out that some of the elements of A and B don’t actually appear in any of the ordered pairs belonging to a function graph. The subset of A consisting of arguments for which the function is actually defined is called the domain of the function. Similarly, the subset of B consisting just of the results that actually can be returned by the function is called the image. These sets are defined formally as follows:

Definition 63. The domain and the image of a function f are defined as:

domain f

=

{x

|

y. (x, y) f }

image f

=

{y

|

x. (x, y) f }

This definition says that the domain of a function is the set of all x such that (x, y) appears in its graph, while its image is the set of all y such that (x, y) appears. Thus a function must be defined for every element of its domain, and it must be able to produce every element of its image (given the right

270

CHAPTER 11. FUNCTIONS

14

25

36

AB

Figure 11.2: Function Type

14

25

3 6

domain image

Figure 11.3: Domain and Image of a Function

argument). However, the argument type and the result type may contain extra elements that do not appear in the function graph (Figure 11.2).

Unfortunately, the terminology for functions is not entirely standard. Many authors use ‘codomain’ to refer to a function’s argument type, but others define the codomain of a function di erently. Many authors define the range of a function to be its image; others define it to be the result type. Whenever you are reading a document that uses any of these terms, you need to check the definitions given in that document.

Example 88. The set {(1, 4), (2, 5), (3, 6)} is the graph of a function (Figure 11.3). The domain is {1, 2, 3} and the image is {4, 5, 6}. The function can have any type of the form A → B, provided that {1, 2, 3} A and {4, 5, 6} B.

Example 89. Let function f :: Integer → Integer be defined as

f = {(0, 1), (1, 2), (2, 4), (3, 8)}.

The argument type of f is Integer = {. . . , −2, −1, 0, 1, 2, . . .}, and its domain is

11.2. FUNCTIONS IN PROGRAMMING

271

Relation

14

2 5

3 6

14

2 5

3 6

Function

14

2 5

3 6

14

2 5

3 6

Figure 11.4: Two Relations and Two Functions

{0, 1, 2, 3}. The result type is Integer = {. . . , −2, −1, 0, 1, 2, . . .}, and the image is {1, 2, 4, 8}.

Example 90. Figure 11.4 shows the graph diagrams for two relations and two functions.

11.2Functions in Programming

The ‘function as a graph’ idea used to model a function mathematically is not exactly the same as a function written in a programming language, although both are realisations of the same idea. The di erence is that a set of ordered pairs specifies only what result should be produced for each input; there is no concept of an algorithm that can be used to obtain the result. The function graph approach ‘pulls the result out of a hat’. In contrast, a function in a programming language is represented solely by the algorithm, and the only way to determine the value of f x is to execute the algorithm on input x. A programming language function is a method for computing results; a mathematical function is a set of answers.

Besides providing a method for obtaining the result, a programming language function has a behaviour: it consumes memory and time in order to compute the result of an application. For example, we might write two sorting functions, one that takes very little time to run on a given test sample and one that takes a long time. We would regard them as di erent algorithms, and would focus on that di erence as being important. The graph model of a

272

CHAPTER 11. FUNCTIONS

function lacks any notion of speed and would make no distinction between the two algorithms as long as they always produce the same results.

There are several important classes of functions defined by algorithms, which we will examine in the next few sections. The essential questions we are interested in are the termination and execution speed of the function.

11.2.1Inductively Defined Functions

As its name suggests, inductively defined functions use a computation structure that is similar to induction, which can be used to prove properties of these functions.

Definition 64. A function defined in the following form, where h is a nonrecursive function, is inductively defined :

f 0 = k

f n = h(f (n − 1))

When the argument is 0, the function returns a constant k, and when the argument n is positive, it calls itself recursively on a smaller argument n − 1; the function can then use h to perform further calculations with the result of the recursive call.

As long as h always returns a result, an inductively defined function will always produce a result when applied to any nonnegative argument.

n

Example 91. The function defined below the sum i=0 i, counting backwards from n down to 0. It is written in the form required for inductively defined functions, letting k = 0 and h x = n + x.

f 0 = 0

f n = n + f (n-1)

Example 92. The add function, defined below, is inductively defined over the second argument.

add :: Int -> Int -> Int add n 0 = 0

add n k = n + add n (k - 1)

Example 93. The ‘91’ function is recursive, but is not inductively defined: it calls itself recursively on a larger argument, and it performs yet another recursion on the result (f91 (x+11)) of the first recursion. When applied to n, this function returns 91 if 0 ≤ n ≤ 100, and otherwise it returns n − 10.

f91 :: Int -> Int

f91 x = if x > 100 then x - 10

else f91 (f91 (x + 11))

11.2. FUNCTIONS IN PROGRAMMING

273

11.2.2Primitive Recursion

Computability theory is the branch of computer science that studies the properties of functions viewed as algorithms. One of the most important classes of algorithm is the set of primitive recursive functions, which are defined with a more flexible pattern than inductively defined functions. Primitive recursive functions are essentially equivalent to algorithms that can be expressed with looping structures, such as for loops, that are guaranteed to terminate.

Definition 65. A function f is primitive recursive if its definition has the following form, where g and h are primitive recursive functions.

f 0 x = g x

f (k + 1) x = h (f k x) k x

This definition specifies the standard form for a primitive recursive function. Any function that can be transformed into this form is primitive recursive, even if its definition doesn’t obviously match the definition.

Example 94. The sqr function, which takes a natural number x and returns x2, is primitive recursive, as shown by the following definition. This function satisfies the requirements vacuously, as it does not actually use recursion. All ‘basic functions’ that do not require recursion can be handled the same way, so they are all primitive recursive.

sqr x =

f 0

x

where

f

0

x = g x

 

g

x

= x*x

Example 95. The factorial function can be written in the standard primitive recursive form:

factorial

k

=

f k undefined

where f

0

x

=

1

f

(k+1)

x = (k+1) * (f k x)

The function f performs a recursion over k, starting from the argument to factorial and counting down to 0. Since factorial can be calculated simply by multiplying together all the numbers in this countdown, the x argument is not actually required, and f ignores the value of x. The definition of factorial could pick any arbitrary value for x, and undefined = is as good as any. Since x is not used, factorial doesn’t make full use of the power of primitive recursion. The following calculation shows how the application factorial 4

Соседние файлы в предмете Дискретная математика