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

Discrete math with computers_3

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

10.8. ORDER RELATIONS

 

253

Sam

 

Bill

 

 

 

 

Harry

 

 

Anne

Martha

 

 

 

Edward

Patrick

 

 

Figure 10.19: The IsYoungerOrSameAgeAs Partial Order

who don’t have dogs, so it is only a partial order. Of course, it might happen that everyone (or no one) owns a dog, in which case we would still technically have a partial order. That is, it is possible that the entire partial order is sorted using some ordering; the point is just that this is not required.

Example 64. Consider the problem of ordering all the records in the database by people’s names. Some names are common, so there might be more than one record per name. Therefore this is a partial order.

Example 65. We are programming with a data structure that contains ordered pairs (x, y), and we define an ordering such that the pair (x1, y1) precedes the pair (x2, y2) if x1 ≤ x2 y1 ≤ y2. This is a partial order, because it doesn’t specify the ordering between (1, 4) and (2, 3).

The formal definition of partial orders is stated using the properties of relations that we have already defined:

Definition 49. A binary relation R over a set A is a partial order if it is reflexive, antisymmetric, and transitive.

Example 66. Suppose that we used age to order our database records. Our IsYoungerOrSameAgeAs relation is reflexive, antisymmetric, and transitive, so it is a partial order. Figure 10.19 gives its digraph.

Poset diagrams

The purpose of drawing the graph diagram for a relation is to make it easier to understand. It often defeats the purpose to include all of the relation’s arcs in the diagram, as there are so many of them. For a partial order, there is no point in drawing the reflexive and transitive arcs, because we know they must be there anyway and they clutter the diagram and make it hard to see the important arcs that tell us about the ordering.

254

CHAPTER 10. RELATIONS

Harry

Edward

Patrick

Bill

Anne

Sam

Martha

Figure 10.20: A Poset Diagram of the IsYoungerOrSameAgeAs Relation

A poset (partially ordered set) diagram is a relation diagram for partial orders, where the distracting transitive and reflexive arcs are omitted. It is important to state explicitly that the diagram shows a partial order (or a poset); without knowing this fact, a reader would not know that the relation also contains the reflexive and transitive arcs.

When drawing a poset diagram, we position it so that all of the arcs point upwards. All of the arcs of the partial order that are not implied by the reflexive or transitive properties must be drawn explicitly. We remove the reflexive and transitive arcs and the arrowheads of the remaining arcs.

If there is a directed path between two nodes in a poset diagram, then those nodes are comparable. An element of a partial order may be comparable to some of the elements but not to the others.

Example 67. We redraw Figure 10.19 so that it is a poset diagram (Figure 10.20). Now, it is easy to see the record ordering.

Weakest and Greatest Elements of a Poset

The following definitions give the standard terminology used to describe how two elements of a partial order are related to each other:

Definition 50. If there is a directed path from x to y in a partial order (i.e., if x precedes y in the partial order), then x is weaker than y. The mathematical notation for this x y. If x y is false, then we write x y.

Definition 51. Two nodes x and y in a partial order are incomparable if x y y x. That is, x and y are incomparable if there is no directed path from x to y, and there is also no directed path from y to x.

In a finite set of numbers, there must be a unique smallest element and a unique greatest element. However, a poset might have several least elements. For example, if x and y are incomparable, but they are both weaker than all

10.8. ORDER RELATIONS

255

a

e

 

f

b

d

 

c

Figure 10.21: A Poset Diagram

the other elements of the poset, then both are least elements. Similarly, there may be several greatest elements. The following definitions define the sets of least and greatest elements formally:

Definition 52. The set of least elements of a poset P is

{x P | y P. (x y (x y y x))}.

That is, the least elements of P are the elements that are either incomparable to or weaker than any other element.

Definition 53. The set of greatest elements of a poset P is

{x P | y P. (y x (x y y x))}.

That is, the greatest elements of P are the elements that are either incomparable to or greater than any other element.

Example 68. Suppose that a family in our database has the following children: Ray, aged 17, Tom, aged 6, and the twins Belle and Eunice, aged 5. We can define a partial order ( ) based on age, such that x y if x is younger than or the same age as y. Even though there are twins, there are no incomparable elements of this poset, since Belle Eunice and also Eunice Belle. However, both of the twins satisfy the requirements for the least element. The set of greatest nodes is {Ray} and the set of weakest nodes is {Belle, Eunice}.

Example 69. Figure 10.21 shows a poset where the set of weakest elements is {c}, and the set of greatest elements is {a, f, e}.

The following Haskell function, defined in the software tools file, takes a digraph and returns True if the digraph represents a partial order and False otherwise.

isPartialOrder ::

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

256

CHAPTER 10. RELATIONS

The following two functions each take a relation and an element. The first one returns True if the second argument is a least element in the relation, and False otherwise. The second function returns True if the element is a greatest element in the relation and False otherwise.

isWeakest, isGreatest ::

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

These functions each take a digraph; the first function returns the set of weakest elements while the second function returns the set of greatest elements:

weakestSet :: (Eq a, Show a) => Digraph a -> Set a greatestSet :: (Eq a, Show a) => Digraph a -> Set a

Exercise 32. Work out by hand whether the following digraphs are partial orders, and then check your results using the computer:

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

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

Exercise 33. Calculate the following by hand, and then evaluate using the computer:

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

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

Exercise 34. Calculate the following by hand, and then evaluate using the computer:

weakestSet ([1,2,3,4], [(1,4),(1,3),(1,2),(1,1), (2,3),(2,4),(2,2),(3,4), (3,3),(4,4)])

weakestSet ([1,2,3,4], [(1,4),(1,2),(1,1),(2,4), (2,2),(3,4),(3,3),(4,4)])

greatestSet ([1,2,3,4], [(1,2),(3,4),(1,1),(2,2),(3,3),(4,4)])

greatestSet ([1,2,3,4], [(2,3),(3,4),(2,4),(1,1),(2,2),(3,3),(4,4)])

Exercise 35. What are the greatest and weakest elements in a poset diagram that contains the following arcs:

(a){(a, b), (a, c)}

(b){(a, b), (c, d)}

(c){(a, b), (a, d), (b, c)}

10.8. ORDER RELATIONS

257

 

 

 

 

1

 

4

 

2

3

Figure 10.22: A Quasi Order

10.8.2Quasi Order

A quasi order is similar to a partial order, except that it is irreflexive:

Definition 54. A binary relation R over a set A is a quasi order if it is irreflexive and transitive.

Example 70. The relation (<) on numbers is a quasi order, but () is not.

Notice that the definition of a quasi order doesn’t mention symmetry. Can a quasi order be symmetric? Suppose there are two elements x and y, such that x y. If the quasi order were symmetric, then we would also have y x, and since it is also transitive, we then have x x, which violates the requirement that a quasi order be irreflexive. This argument would not apply, of course, in a trivial quasi order where no two elements are related by , but non-trivial quasi orders cannot be symmetric.

We should also inquire whether a quasi order can be (or must be) antisymmetric. By definition, it is antisymmetric if x y y x → x = y for any two elements x and y. Now, if we choose x and y to be the same, then x y y x is false, because the quasi order is irreflexive. This means the logical implication is vacuously true. If we choose x and y to be di erent, then x y y x is again false (as we have just shown while discussing symmetry). In all cases, therefore, the definition of antisymmetry is satisfied, but vacuously.

The conclusion is that quasi orders may be symmetric, but only if they are trivial, and they are always antisymmetric, but only because they satisfy the definition vacuously. The properties of symmetry and antisymmetry are uninteresting for quasi orders.

Example 71. Figure 10.22 gives the graph diagram for the quasi order (<) on the set {1, 2, 3, 4}.

The following function takes a digraph and returns True if the relation it represents is a quasi order, and False otherwise:

isQuasiOrder ::

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

258

CHAPTER 10. RELATIONS

Violet

Blue

Green

Yellow

Orange

Red

Figure 10.23: A Chain of the Rainbow Colours

Exercise 36. Work out the following expressions, and evaluate them with the computer:

isQuasiOrder ([1,2,3,4],[(1,2),(2,3),(3,4)]) isQuasiOrder ([1,2,3,4],[(1,2)])

10.8.3Linear Order

A linear order or total order is like a partial order, except that it requires that all of the relation’s elements must be related to each other.

Example 72. The () and () relations on real numbers are total orders: any two numbers x and y can be compared with each other, and it is guaranteed that either x ≤ y or y ≤ x will be true (and both are true if x = y).

Definition 55. A linear order is a partial order defined over a set A in which for each element a and b in A, either a b or b a.

Example 73. Suppose that the database recorded the exact time at which each child was born. We could then use a form of to order the children within the families. This information could be useful in a study of the influence of primogeniture on the medical history of an aristocracy.

The elements of a linear order can be said to form a chain. When we draw the graph diagram for a chain, we omit the arcs that are implied by transitivity and reflexivity. Without these extra arcs, and because no element can be incomparable to the others, the diagram looks like a real chain. For example, the colours of the rainbow are often given as a chain starting with Red and ending with Violet. As Red light has the longest wavelength and Violet the shortest, the relation that imposes this chain ordering on the set of six colours is the relation on the wave frequency (Figure 10.23).

The isLinearOrder function takes a digraph and returns True if it represents a linear order, and False otherwise.

isLinearOrder :: Eq a => Digraph a -> Bool

10.8. ORDER RELATIONS

259

Exercise 37. Evaluate the following expressions, by hand and using the computer:

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

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

10.8.4Well Order

A well order is a total (or linear) order that has a least element; furthermore, every subset of a well order must have a least element.

The existence of a least element is significant because it provides a base case for recursive functions and for inductive proofs. Note that any total order that has a finite number of elements must have a least element. Some total orders with an infinite number of elements have a least element, and others do not.

Example 74. The () relation on the set N = {0, 1, 2, . . .} of natural numbers is a total order. Furthermore, N has a least element, because x N. 0 ≤ x. Therefore () on N is a well order.

Example 75. The () relation on the set Z = {. . . , −2, −1, 0, 1, 2, . . .} of integers is a total order. However, Z does not have a least element, because

x Z. y Z.(y ≤ x y = x).

Therefore () on Z is only a total order, and not a well order.

Definition 56. Given a set S and a binary relation R over S, R is a well order if R is a linear order and every subset of S that is not empty contains a least element.

Well orders are important because they support induction, and they are countable. Informally, a countable set is a set in which an arbitrary item can eventually be processed by a computer. The set could be infinite: for example, the set of natural numbers is infinite, but every element of that set would eventually be reached if we just work on 0, 1, 2, . . . in sequence. For example, if a computer started printing the natural numbers, we would eventually see the number 4058000023. However, if it started printing an uncountable set such as the irrational numbers, then it might get stuck printing an infinite number of irrationals without reaching the number we are interested in.

Example 76. In our database, each record is given a numeric key that is unique. As there are a finite number of keys in the database, the relation over these keys is a well order.

Exercise 38. We have been watching a computer terminal. Is the order in which people come and use the terminal a total order?

260

CHAPTER 10. RELATIONS

Exercise 39. Is it always possible to count the elements of a linear order?

Exercise 40. Can a set that is not a well order be countable?

10.8.5Topological Sort

Computers are good at doing one task at a time, in sequence. When an algorithm is working on a data structure, it needs to know which element of the data structure to work on next. Often there is an order relation that must be followed (for example, we might want to output the items in a database in alphabetical order). If we have a total order on the elements of the data structure, the algorithm can use that to find the next piece of work. If, however, we have only a partial order on the data items, then there are several possible orders in which items could be processed while still respecting the order relation. Often we don’t care which order is used—we just want the algorithm to find one and proceed with the work.

The process of taking a partial order and putting its elements into a total order is called topological sorting.

Example 77. Some compilers analyse the order in which procedures call each other. Such a compiler could construct a ‘dependency graph’ for the program it is translating, where each node corresponds to a procedure, and arcs in the graph correspond to procedure calls. The dependency graph is a partial order. Now, suppose the compiler generates object code for the procedures in the order of their appearance in the call graph, so that the lowest-level procedures are processed first and the highest level ones are done last, in order to make as many procedure calls as possible into forward references. The compiler uses a topological sort to produce the total order in which it prints the information. The first name in the total order will be a procedure that doesn’t call any other procedure, while the last is the top-level procedure with which the program starts execution.

There is a simple and general algorithm for topological sorting. Choose x, one of the elements that is greatest in the set A, and make it the first in the sequence. Now do the same for the set A − {x}, and continue until A is empty.

Example 78. Suppose that we have a relation that expresses the call graph:

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

What would the topological sort of this graph be? First, the functions that call no other function would appear, followed by the functions that call them, followed by the functions calling them, etc. The result would be ’D’, ’C’, ’B’, ’A’ (Figure 10.24).

Example 79. What is the topological sort of {(1, 2), (1, 3)} given the nodes 1,2,3? The sequence 3, 2, 1 or 2, 3, 1.

10.9. EQUIVALENCE RELATIONS

261

D

C

B

A

Figure 10.24: A Call Graph

The following function takes a digraph and returns a topological sort of its relation.

topsort ::

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

Exercise 41. Check to see that the following partial orders are not, in fact, total orders. Use the computer to generate a total order, using a topological sort.

topsort ([1,2,3,4],[(1,2),(1,3),(2,3),(1,4),(2,4), (1,1),(2,2),(3,3),(4,4)])

topsort ([1,2,3],[(1,2),(1,3),(1,4),(1,1),(2,2),(3,3)])

10.9Equivalence Relations

Some relations can be used to break a set up into several categories or ‘partitions’, where each element of the set belongs to just one of the categories. Such a relation is called an equivalence relation.

Example 80. In organising a personal telephone list, it is convenient to organise the set S of people’s names into 26 sets corresponding to the first letter in the name. In other words, we are making a section of the telephone list for each letter of the alphabet.

Example 81. Given a genealogy database, we would expect to see many queries about membership in families. We might assume that persons a and b are in the same family if they have the same last name, or there might be some other way to define what a family is (but it needs to have the property that every person belongs to exactly one family). One common query might be ‘Is a in the same family as b?’. Once the InSameFamilyAs relation is defined, it provides all the information needed to organise all people into families.

262

CHAPTER 10. RELATIONS

These examples suggest that a relation we are using in this way needs to be reflexive (everything belongs in the same category as itself), symmetric (if a is in the same category as b, then obviously b must be in the same as a), and transitive (if a and b are in the same category, and so are b and c, then a and c must also be in the same category). These observations lead to the formal definition of an equivalence relation:

Definition 57. A binary relation R over a set A is an equivalence relation if it is reflexive, symmetric, and transitive.

Example 82. Suppose that everyone in the database lives in a real location somewhere in the world. We can represent the world as a map, and then partition the map into small areas by using a LivesIn SameLocationAs relation.

The equivalence classes of a non-empty equivalence relation can be thought of as a partition of the set into disjoint subsets. Now we define this term formally:

Definition 58. A partition P of a non-empty set A is a set of non-empty subsets of A such that

For each subset S1 and S2 of P , either S1 = S2 or S1 ∩ S2 = ;

• A = S P S.

Example 83. For example, let’s consider the set of people’s last names and the relation HasNameStartingWithSameLetterAs. This relation divides the set into 26 subsets. There can be no overlaps between the subsets, and the set of names is the union of the subsets.

Example 84. Computer keyboards can generate several characters from most of the keys, depending on whether the Control, Shift, or Meta keys are already down. We could define a relation IsOnSameKeyAs, which would partition the set of ASCII characters into equivalence classes, one for each key.

A good example of an equivalence relation, which is frequently used in computing applications, comes from the mathematical modulus (mod) operation on integers. The expression e mod k gives the remainder produced when dividing e by k; and the value of e mod k is a number between 0 and k − 1. Now, every number x which is a multiple of k will have the property that x mod k = 0, and we can build a set of all these numbers. Similarly, there is a set of numbers that have the same remainder when divided by 1, by 2, and so on when divided by k. To do this, we define the congruence relation as follows:

Definition 59. Let k be a positive integer, and let a and b be integers. If there is an integer n such that

(a − b) = n × k,

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