Graph Theory

profilegoodjob
booklet.zip

SETS.pdf

SETS A SET is a collection of objects, called the ELEMENTS of the set. For example:

• {Essex, Yorkshire, Devon} • {2, 3, 5, 7, 11} • {cheese, eggs, milk, cream}

A set is denoted by a capital letter for reference purposes. For instance, S = {3, 2, 11, 5, 7} is the set S containing the given elements. We write a∈S to denote that the object a is an element of the set S. If a is not an element of S we write a∉S. For a large set, especially an infinite set, we define the set by using a predicate. We write

S = {x : P(x)} to denote the set of objects x for which the predicate P(x) is true. For example,

S = {x : x is an odd positive integer} or

S = {2n − 1 : n is a positive integer} both define the set

S = {1, 3, 5, 7, . . . . . } EXAMPLE 4.1 Find simpler descriptions of the following sets:

A = {x : x is an integer and x2 + 2x = 0}; B = {x : x is a month not containing the letter r}.

Solution

SETS 37

Standard sets are:

Ø or { } denotes the EMPTY SET. N = {1, 2, 3, . . . . }, the NATURAL NUMBERS. Z = {0, ±1, ±2, ±3, . . }, the INTEGERS. Q ={ p

q : p, q integers, q ≠ 0} the RATIONAL NUMBERS.

R denotes the set of REAL NUMBERS.

A set A is called a SUBSET of a set S, if every element of A is an element of S. This is denoted by A ⊆ S and can be shown diagrammatically as below:

A S

Venn diagram of A ⊆ S

Two sets are EQUAL if each is a subset of the other. Hence, to prove that two sets are equal requires us to show that they contain the same elements. Hence, A = B if (x ∈ A ⇒ x ∈ B) and (x ∈ B ⇒ x ∈ A) are both true. The UNION of two sets A and B is the set

A ∪ B = {x : x∈A or x∈B}

A B

Venn diagram of A ∪ B

SETS 38

The INTERSECTION of two sets A and B is the set A ∩ B = {x : x∈A and x∈B}

A B

Venn diagram of A ∩ B

The COMPLEMENT of a set B relative to a set A is the set

A − B = {x : x∈A and x∉B}

A B

Venn diagram of A − B

When we are dealing with the subsets of some large set U, we call U the UNIVERSAL SET for the problem in question. This is represented by the rectangle in our Venn diagrams. The complement U – A of a subset A of the universal set U is often written as ~A. So, ~A = {x : x∉A}.

A

Venn diagram of ~A

SETS 39

The SYMMETRIC DIFFERENCE of two sets A and B is the set A Δ B = {x : (x∈A and x∉B)or(x∈B and x∉A)}

A B

Venn diagram of A Δ B

The symmetric difference can be expressed in terms of the other set operations as

A Δ B = (A − B) ∪ (B − A).

EXAMPLE 4.2 Let A = {1, 3, 5, 7}

B = {2, 4, 6, 8} C = {1, 2, 3, 4, 5}

Find A ∪ C, B ∩ C, A − C and B Δ C. Solution A ∪ C = B ∩ C = A − C = B Δ C = (B − C) ∪ (C − B) =

SETS 40

EXAMPLE 4.3 Let U = {1, 2, 3, 4, 5, 6} be the universal set, A = {x : 1 ≤ x ≤ 6 and x is an even integer} and B = {x : 1 ≤ x ≤ 6 and x is a multiple of 3}. Verify that ~(A ∩ B) = ~A ∪ ~B Solution

Many properties of sets can be derived using the operations on sets detailed above. Some are ‘obvious’, others require a proof. Proofs exploit the following correspondence between set operations and logical operations on predicates. SET OPERATION LOGICAL OPERATION ∼ not ∪ or ∩ and ⊆ ⇒

SETS 41

EXAMPLE 4.4 Prove that ~(A ∩ B) = ~A ∪ ~B for any A and B. Solution

This property is one of De Morgan’s laws. Fundamental properties such as this constitute the laws of the ALGEBRA OF SETS, listed below. Associative laws A∪(B∪C) = (A∪B)∪C A∩(B∩C) = (A∩B)∩C Commutative laws A∪B = B∪A A∩B = B∩A Identity laws A∪Ø = A A∩U = A A∪U = U A∩Ø = Ø Idempotent laws A∪A = A A∩A = A Distributive laws A∩(B∪C) = (A∩B)∪(A∩C) A∪(B∩C) = (A∪B)∩(A∪C) Complement laws A∪∼A = U A∩∼A = Ø ∼U = Ø ∼Ø = U ∼(∼A) = A ∼(∼A) = A De Morgan’s laws ∼(A∪B) = ∼A∩∼B ∼(A∩B) = ∼A∪∼B

SETS 42

Laws in the right-hand column can be obtained from the ones in the left-hand column by interchanging ∩ and ∪, and interchanging Ø and U. This is called the DUALITY PRINCIPLE. EXAMPLE 4.5 Use the laws of the algebra of sets to prove that ∼((∼A) ∩( A ∪ B)) = (A ∪ ∼B) for any A and B. Solution ∼((∼A) ∩( A ∪ B))

The CARDINALITY of a set S is the number of elements in S, and is denoted by |S|. PRINCIPLE OF INCLUSION AND EXCLUSION

|A ∪ B| = |A| + |B| − |A ∩ B| Proof The set A ∪ B is the union of the sets A − B, A ∩ B and B − A, sets that have no elements in common.

A B

A − B A ∩ B B − A

SETS 43

Let | A − B | = m , |A ∩ B| = n , | B − A | = p Then, |A ∪ B| = m + n + p = (m + n) + (n + p) − n = |A| + |B| − |A ∩ B|

■ EXAMPLE 4.6 Of 63 Stage I students registered for the CO Field, 16 took U08600, 37 took U08606 and 5 took both. How many students took neither? Solution Let A = {CO students who took U08600} and B = {CO students who took U08606}. Then |A| = , |B| = and |A ∩ B| = So, |A ∪ B| = Hence, students took neither.

■ An ORDERED PAIR is an object of the form (a, b) where a is an element of some set A and b is an element of a set B. The set of all ordered pairs of this form is called the CARTESIAN PRODUCT of the sets A and B and is denoted by A × B. Hence, A × B = {(a, b): a∈A and b∈B}. For example, if A = {x, y} and B = {1, 2, 3}, then

A × B = {(x, 1), (x, 2), (x, 3), (y, 1), (y, 2), (y, 3)}

Notice that in this example | A × B | = 6 = |A|.|B| In general, | A × B | = |A|.|B| The Cartesian product of any collection of sets A1, A2, . . . , An is the set

A1×A2× . . . ×An = { (a1, a2, . . . , an) : ai∈Ai} Subsets of such a Cartesian product are the objects that are manipulated in databases (covered in a later application).

SETS 44

EXERCISES 4 - SETS 4.1 (a) Enumerate the elements of the following sets. A = {x : x ∈ Z and 10 ≤ x ≤ 17} B = {x : x ∈ Z and x2 < 24} C = {x : x ∈ Z and 6x2 + x − 1 = 0} D = {x : x ∈ Q and 6x2 + x − 1 = 0} [Hint: 6x2 + x − 1 = (3x − 1)(2x + 1)] (b) Write a set specification in predicate form for the following sets. S = { 2, 5, 8, 11, . . . } T = {1, 1/3, 1/7, 1/15, . . . } 4.2 In this question the universal set is {p, q, r, s, t, u, v, w}. Let A = {p, q, r, s}, B = {r, t, v} and C = {p, s, t, u}. Find the following sets. (a) B ∩ C (e) (A ∪ B) ∩ (A ∩ C) (b) A ∪ C (f) ∼(A ∪ B) (c) ∼C (g) B − C (d) A ∩ B ∩ C (h) B Δ C 4.3 Consider the following subsets of Z. A = {3n : n ∈ Z and n ≥ 4} B = {2n : n ∈ Z } C = {n : n ∈ Z and n2 ≤ 100} Using set operations, express each of the following in terms of A, B and C. (a) The set of all odd integers. (b) {−10, −8, −6, −4, −2, 0, 2, 4, 6, 8, 10} (c) {6n : n ∈ Z and n ≥ 2} (d) {−9, −7, −5, −3, −1, 1, 3, 5, 7, 9} 4.4 Make a sequence of Venn diagrams to illustrate the set identity A ∩ ( B Δ C ) = ( A ∩ B ) Δ ( A ∩ C ) 4.5 Use the laws of the algebra of sets to prove the following. (a) ~(A ∩ ~B) ∪ B = ~A ∪ B (b) ~(~A ∩ ~(B ∪ C)) = A ∪ B ∪ C (c) (A ∪ B ∪ C) ∩ (A ∪ ∼B ∪ C) ∩ ∼(A ∪ C) = ∅

SETS 45

4.6 (a) With the help of Venn diagrams, show that for finite sets A, B and C

|A ∪ B ∪ C| = |A| + |B| + |C| – |A ∩ B| – |B ∩ C| – |A ∩ C| + |A ∩ B ∩ C| (b) There are 25 students taking CO, 27 students taking MA and 12 students

taking ST. There are 20 students taking both CO and MA, 5 students taking both CO and ST and 3 students taking both ST and MA. There are no students taking all three fields.

How many students are taking at least one of the three fields? How many

of the students involved are taking only ST? 4.7 Let A = {p, q, r} and B = {p, s}. List the elements of A × B Does A × B = B × A? 4.8 Prove that

A × (B ∩ C) = (A × B) ∩ (A × C) for any sets A, B and C.

SETS 46

SOLUTIONS TO EXERCISES 4 - SETS 4.1 (a) A = {10, 11, 12, 13, 14, 15, 16, 17} C = ∅ B = {−4, −3, −2, −1, 0, 1, 2, 3, 4} D = {−1/2,

1/3}

(b) S = {3n − 1 : n ∈ N} T = { 1 2 1n −

: n ∈ N}

4.2 (a) {t} (d) ∅ (g) {r, v} (b) {p, q, r, s, t, u} (e) {p, s} (h) {p, r, s, u, v} (c) {q, r, v, w} (f) {u, w} 4.3 (a) ∼B (c) A ∩ B (b) B ∩ C (d) C − B 4.4

B Δ C A ∩ ( B Δ C )

A∩B horizontal shading ( A ∩ B ) Δ ( A ∩ C ) A∩C vertical shading 4.5 (a) ∼(A∩∼B)∪B = (∼A∪B)∪B De Morgan’s and complement laws = ∼A∪(B∪B) associative law = ∼A∪B idempotent law (b) ∼(∼A∩∼(B∪C) = A∪(B∪C) De Morgan’s and complement laws = A∪B∪C as a consequence of the associative law (c) (A∪B∪C)∩(A∪∼B∪C)∩∼(A∪C) = ((B∪(A∪C))∩(∼B∪(A∪C)))∩∼(A∪C) commutative and associative laws = ((B∩∼B)∪(A∪C))∩∼(A∪C) commutative and distributive laws = (∅∪(A∪C))∩∼(A∪C) complement law = (A∪C)∩∼(A∪C) identity law = ∅ complement law

SETS 47

4.6 (a)

A B

2 5 6

1 4 3

7 8 C

Label the distinct regions 1, 2, . . . , 8 as above, and suppose that region i contains ni elements for i = 1, 2, . . . , 8 Then , |A| + |B| + |C| = (n1 + n2 + n4 + n5) + (n1 + n2 + n3 + n6) + (n1 + n3 + n4 + n7) = 3n1 + 2n2 + 2n3 + 2n4 + n5 + n6 + n7 and |A∩B| + |B∩C| + |A∩C| = (n1 + n2) + (n1 + n3) + (n1 + n4) = 3n1 + n2 + n3 + n4 and |A∩B∩C| = n1. Therefore |A| + |B| + |C| − |A∩B| − |B∩C| − |A∩C| + |A∩B∩C| = 3n1 + 2n2 + 2n3 + 2n4 + n5 + n6 + n7 - 3n1 - n2 - n3 - n4 + n1 = n1 + n2 + n3 + n4 + n5 + n6 + n7 = |A∪B∪C| as required.

(b) Let A = set of students taking CO B = set of students taking MA C = set of students taking ST From the given information

|A∪B∪C| = 25 + 27 + 12 − 20 − 5 − 3 = 36 Hence, there are 36 students taking at least one of the three fields. The numbers can be displayed on a Venn diagram as below

A B

20 0 4

0 5 3

4 C

Only 4 students are taking ST alone. 4.7 A×B = {(p, p), (p,s), (q, p), (q, s), (r, p), (r, s)} The set B×A does not equal A×B. For example, B×A contains (p, r) but A×B does not. 4.8 Let x ∈ A × (B ∩ C). Then x = (a, t) where a ∈ A and t ∈ (B ∩ C). Hence x = (a, t) where a ∈ A and t ∈ B and t ∈ C. Therefore x ∈ A × B and x ∈ A × C In other words, x ∈ (A × B) ∩ (A × C). This proves that A × (B ∩ C) ⊆ (A × B) ∩ (A × C).

SETS 48

Conversely, if x ∈ (A × B) ∩ (A × C) then x ∈ (A × B) and x ∈ (A × C). Hence x = (a, t) where a ∈ A and t ∈ B and t ∈ C. Therefore x = (a, t) where a ∈ A and t ∈ (B ∩ C). In other words, x ∈ A × (B ∩ C). This proves that (A × B) ∩ (A × C) ⊆ A × (B ∩ C). Finally, A × (B ∩ C) = (A × B) ∩ (A × C).

SETS 49

SETS 50

  • EXAMPLE 4.4

APPS1.pdf

APPLICATIONS 1 This week we use the theoretical ideas we have developed to date to look at two applications: knowledge based systems and database management systems. Knowledge based systems are used in the field of artificial intelligence (covered in later modules) where researchers seek to develop machines that can supplement the work of experts in specialised fields. Such expert systems seek to capture the knowledge and experience of experts in a given field. This is done by setting up a knowledge base of known facts together with a set of inference rules. Queries to the system can then be inferred (or answered directly) from the knowledge base. Database systems are used for record-keeping and use data stored in a computer in database. The collection of programs which allow the user to extract information from the database, or to modify it, form a database management system. As we shall see, data is stored in tables whose rows form n-ary relations and the operations used to extract and update information form the basis of the theory of relational databases covered in later modules.

KNOWLEDGE BASED SYSTEMS We construct a simple expert system, THE ROYAL FAMILY, to answer queries about British Kings and Queens since George I, and their families. First we supply a list of facts parent(george 1, george 2) wife(sophia, george 1) parent(george 3, george 4) wife(wilhemina, george 2) parent(george 3, william 4) wife(charlotte, george 3) parent(george 3, edward) wife(caroline, george 4) parent(edward, victoria) wife(adelaide, william 4) parent(victoria, edward 7) wife(victoria, albert) parent(edward 7, george 5) wife(alexandra, edward 7) parent(george 5, edward 8) wife(victoria mary, george 5) parent(george 5, george 6) wife(elizabeth q. m., george 6) parent(george 6, elizabeth 2) wife(elizabeth 2, philip) parent(victoria, alice) parent(alice, victoria alberta) parent(victoria alberta, alice mountbatten) parent(alice mountbatten, philip) The predicate parent(x, y) means that x is a parent of y and wife(x, y) means that x is the wife of y. This is the standard use of predicates used in logic programming languages such as PROLOG.

APPLICATIONS 1 65

We interrogate the database by asking questions such as ‘was george 1 a parent of george 3’. The response is no since the given list of facts does not contain parent(george 1, george 3). Queries are written in the form ? - predicate and any variables that appear are assumed to be existentially qualified. For example, ? - wife(x, george 4) gets the response yes since x = caroline produces a listed fact. EXAMPLE 6.1 Determine the responses to the following queries (a) ? - wife(elizabeth 2, philip), (b) ? - parent(sophia, george 2), (c) ? - female(caroline), (d) ? - wife(philip, elizabeth 2). Solution (a) (b) (c) (d)

■ To give ROYAL FAMILY its problem-solving power we introduce some inference rules. An inference rule defines a new predicate in terms of the predicates used previously. Responses to queries about the new predicates can then be deduced from the fact list via the inference rules. For example, we could introduce predicates to identify female members of the ROYAL FAMILY and husbands. (1) female(x) from wife(x, y) This means that if x is the wife of y then x is a female. (2) husband(y, x) from wife(x, y) This means that if x is the wife of y then y is the husband of x.

APPLICATIONS 1 66

EXAMPLE 6.2 How do responses to the queries in Example 6.1 change ? Answer the following additional queries (e) ? - female(alice mountbatten), (f) ? - husband(albert, victoria), (g) ? - male(albert). Solution The responses to (a), (b) and (d) are unchanged. (c) (e) (f) (g)

We can define further inference rules to gain information about males and sons. (3) male(x) from . . . . . . . . . . . . (4) son(x, y) from . . . . . . . . . . . . . and. . . . . . . . . . . . . . . .

APPLICATIONS 1 67

EXAMPLE 6.3 Answer the following queries. (a) ? - male(william 4) (b) ? - son(william 4, george 3) (c) ? - son(william 4, charlotte) (d) ? - son(edward 8, george 5) Solution (a) (b) (c) (d)

■ Note from the responses to parts (c) and (d) the limitations imposed on the system by it having to adhere to the given facts and inference rules. Only monarchs are given as parents and the spouses of male monarchs appear as wives. So, although charlotte was married to george 3 and william 4 was one of their sons, the database only lists george 3 as a parent of william 4 and so inference rule (4) cannot elicit a yes response to (c). The problem in (d) is that no wife is recorded for edward 8, so that inference rule (3) gives a no response to the query ? - male(edward 8). If the information provided is incomplete, as is often the case in a real expert system, great care must be exercised in defining inference rules so as to avoid the introduction of contradictory responses. For example, consider the, seemingly reasonable, inference rules (A) male(x) from wife(y, x) (B) female (x) from (not male(x)) If the original list of facts is used with only the inference rules (A) and (B), what is the response to the query ? - female(edward 8)?

APPLICATIONS 1 68

EXAMPLE 6.4 Formulate an inference rule to extract mothers from the ROYAL FAMILY. Interpret mother(x, y) as meaning that either x is the wife of a parent of y or x is female and a parent of y. Apply this new inference rule together with inference rule (1) to the original data to determine as many mothers as possible. Is your inference rule satisfactory? Solution

APPLICATIONS 1 69

DATABASE MANAGEMENT SYSTEMS Data can be stored in tables. For example, Table 1 stores information about a group of students giving their (unique) student number, name, gender, date of birth, marital status and address. Table 2 lists some students and their grades on some of the modules they have taken. Table 1 T1 = PERSONAL-DETAILS

Student number

Name Sex Date of birth

Marital status

Address

4000123 Jones F 1.2.84 single 2 The Motte, Abingdon 5001476 Singh M 4.5.85 married 4A New Road, Oxford 5112391 Smith F 21.3.85 single 17 The Crescent, Oxford 5072411 Smith M 12.12.85 single 21 Pudding Lane, Bicester 5532289 Ching M 15.8.84 single 4A New Road, Oxford 5083001 Grant M 9.7.84 married 18 Iffley Road, Oxford 5196236 McKay F 21.3.85 single 133 Uff Road, Reading 4936201 French F 7.10.77 married 11 Finn Road, Abingdon

Table 2 T2 = MODULE-RESULTS

Name 8600 8003 8606 8001 Cummings A B+ B A

Jones B+ B B+ C Grant B B+ A B Singh B B+ A C French C F B B McKay A A B+ A

Cookson B A A B+ The rows of a table with n columns labelled A1, A2, . . . , An, may be regarded as subset of the Cartesian product A1 × A2 × . . . × An. The rows form lists of n items, one from each Ai , and the complete table forms an n-ary relation. For example, T2 is a subset of A1 × A2 × . . . × A5 where A1 denotes the set of modular students and A2 = A3 = A4 = A5 = {A, B+, B, C, F}. One of the elements in this 5-ary relation is given by the row (Jones, B+, B, B+, C). In order to extract information from and to modify the tables corresponding to a collection of relations we define some basic operations on them, namely, project, join and select. These are just three of the many operations available for manipulating databases the theory of which requires the language of sets, relations and functions.

APPLICATIONS 1 70

The operation project chooses specified columns of a table to form a new table. For example project( T1 , {Name, Address}) produces Table 3 below. Table 3 T3 = project( T1 , {Name, Address})

Name Address Jones 2 The Motte, Abingdon Singh 4A New Road, Oxford Smith 17 The Crescent, Oxford Smith 21 Pudding Lane, Bicester Ching 4A New Road, Oxford Grant 18 Iffley Road, Oxford

McKay 133 Uff Road, Reading French 11 Finn Road, Abingdon

EXAMPLE 6.5 Find project( T2 , {Name, 8600, 8606}) Solution

Name 8600 8606

■ The operation join takes two tables and forms a larger table by putting together tuples from each table which agree on shared attributes. For example, join( T3 , T2) produces the table

Name Address 8600 8003 8606 8001 Jones 2 The Motte, Abingdon B+ B B+ C Grant 18 Iffley Road, Oxford B B+ A B Singh 4A New Road, Oxford B B+ A C French 11 Finn Road, Abingdon C F B B McKay 133 Uff Road, Reading A A B+ A

APPLICATIONS 1 71

The operation select extracts the rows of a table that satisfy certain selection criteria. For example, select( T1 , Sex=M and Marital_status=married) returns the table

Student number

Name Sex Date of birth

Marital status

Address

5001476 Singh M 4.5.85 married 4A New Road, Oxford 5083001 Grant M 9.7.84 married 18 Iffley Road, Oxford

EXAMPLE 6.6 Find select( T2 , 8606 = A) Solution

Name 8600 8003 8606 8001

Combining the three operations allows us to extract a variety of information as the following illustrate. EXAMPLE 6.7 Find the final table produced by the following sequence of operations:

R1 = project(T2, {Name, 8003, 8001}) R2 = select(R1, 8003 = A or 8001 = A)

Solution

Name 8003 8001

APPLICATIONS 1 72

EXAMPLE 6.8 Find the tables produced by the following sequence of operations:

R1 = select(T1, Sex = F) R2 = project(T2, {Name, 8606}) R3 = join(R1, R2)

Solution R1 is as follows:

Student number

Name Sex Date of birth

Marital status

Address

4000123 Jones F 1.2.84 single 2 The Motte, Abingdon 5112391 Smith F 21.3.85 single 17 The Crescent, Oxford 5196236 McKay F 21.3.85 single 133 Uff Road, Reading 4936201 French F 7.10.77 married 11 Finn Road, Abingdon

R2 is as follows:

Name 8606 Cummings B

Jones B+ Grant A Singh A French B McKay B+

Cookson A R3 is as follows:

Student number

Name Sex Date of birth

Marital status

Address 8606

APPLICATIONS 1 73

EXAMPLE 6.9 Describe, using the three operations, how to find the names and addresses of all the female students who scored at least a B+ on 8600 and 8606. Solution

APPLICATIONS 1 74

EXERCISES 6 - APPLICATIONS 1 6.1 The knowledge based system MODULAR contains the following list of basic

facts: male(Andrew) basic(U08003) studied(Andrew, U08003) male(Charles) basic(U08606) studied(Andrew, U08016) male(Eric) studied(Beryl, U08606) advanced(U07171) studied(Beryl, U08424) female(Beryl) advanced(U08016) studied(Charles, U07171) female(Deidre) advanced(U08424) studied(Deidre, U08016) female(Fiona) studied(Eric, U08003) studied(Fiona, U08003) studied(Fiona, U07171)

Note that male(x) means that x is a male student, female(x) means that x is a female student, basic(x) means that x is a basic module, advanced(x) means that x is an advanced module, and studied(x, y) means that student x studied module y.

A rule of inference is given by:

incommon(x, y) from (studied(x, z) and studied(y, z))

Answer the following queries, briefly justifying your answers. (a) ? - studied(Charles, U08003) (b) ? - studied(Andrew, x) and advanced(x) (c) ? - male(x) and studied(x, U07171) (d) ? - incommon(x, Deidre) (e) ? - female(x) and female(y) and incommon(x, y) (f) ? - studied(Andrew, x) and (not(studied(Fiona, x)))

Write down formal queries asking each of the following: (g) Is there a student who has studied both advanced and basic modules? (h) Is there a basic module that has been studied by both male and female

students?

APPLICATIONS 1 75

6.2 Two relation tables T1 = AUTHOR-DETAILS and T2 = BOOK-DETAILS contain the following information.

Author Country Book Title John Lee USA Programming in C++ Celia Ringworth UK Mathematics for Computing Ng Depak India Database Techniques John Lee USA Programming in Java Alf Belling USA Discrete Mathematics Sarah Wilton UK Intelligent Systems Ng Depak India Formal Methods in Computing

T1 = AUTHOR-DETAILS

Book Title Publisher Subject

Programming in C++ Grant Computing Programming in Java Grant Computing Discrete Mathematics Delphi Mathematics Mathematics for Computing Brooks Mathematics Formal Methods in Computing Grant Computing Intelligent Systems Delphi Computing Database Techniques Delphi Computing

T2 = BOOK-DETAILS

(a) Calculate the tables T3, T4 and T5 given by the following sequence of operations:

T3 = join (T1, T2)

T4 = select (T3, Publisher = Grant)

T5 = project (T4, {Book Title, Author}) (b) Determine a sequence of operations that will produce a table giving

the authors and titles of UK authors who have published computing texts.

(c) Tables T1 and T2 are updated when Brooks publishes a new book by Celia Ringworth entitled ‘Discrete Mathematics’. Explain why the

relation join(T1, T2) contains false information and propose changes you would make to the table T1 to avoid this problem?

APPLICATIONS 1 76

APPLICATIONS 1 77

6.3 An operation rename is defined on any n-ary relation as follows. Given a table, rename produces a new table with the same entries as the original table but with relabelled column headings.

For example, below is a table M of mothers and children and the renamed table N = rename(M, CHILD → CHILD1)

CHILD MOTHER CHILD1 MOTHER Charles Elizabeth Charles Elizabeth Edward Elizabeth Edward Elizabeth Anne Elizabeth Anne Elizabeth

William Diana William Diana Harry Diana Harry Diana

Table M Table N

(a) Calculate the relations given by each step of the following sequence of operations:

P1 = join (M, N) P2 = select ( P1, CHILD < CHILD1 ) P3 = project ( P2, { CHILD, CHILD1 })

[Note : P1 contains thirteen tuples, and < denotes the usual lexicographical ordering of words]

(b) Suppose now that M is a general table of mothers and children in some

specified group of people. Describe in words the tuples occurring in the relation P3.

(c) Suppose that in addition to the table M in part (b) there is a table F of

fathers and children (using the column headings FATHER and CHILD). Given that two children are full siblings if they have the same father and the same mother, give a sequence of operations which produces a table of full siblings.

APPLICATIONS 1 78

SOLUTIONS TO EXERCISES 6 – APPLICATIONS 1 6.1 (a) The response is NO; studied(Charles, U08003) is not in the fact list. (b) The response is YES; Andrew studied the advanced module x = U08016. (c) The response is YES; x = Charles is male and studied U07171. (d) The response is YES; x = Andrew studied U08016 as did Deidre. (e) The response is NO; no two females studied the same module. (f) The response is YES; Andrew studied x = U08016 but Fiona did not. (g) ? - studied(x, y) and basic(y) and studied(x, z) and advanced(z) (h) ? - studied(x, z) and female(x) and studied(y, z) and male(y) and basic(z) 6.2 (a) Table T3 has five columns labelled Author, Country, Book Title, Publisher and Subject.

There are seven rows of data.

Author Country Book Title Publisher Subject John Lee USA Programming in C++ Grant Computing Celia Ringworth

UK Mathematics for Computing

Brooks Mathematics

Ng Depak India Database Techniques Delphi Computing John Lee USA Programming in Java Grant Computing Alf Belling USA Discrete Mathematics Delphi Mathematics Sarah Wilton UK Intelligent Systems Delphi Computing Ng Depak India Formal Methods in

Computing Grant Computing

Table T4 has the same columns as T3 but only three rows of data.

Author Country Book Title Publisher Subject John Lee USA Programming in C++ Grant Computing John Lee USA Programming in Java Grant Computing Ng Depak India Formal Methods in

Computing Grant Computing

Table T5 is as follows:

Book Title Author Programming in C++ John Lee Programming in Java John Lee Formal Methods in Computing

Ng Depak

(b) One possible sequence of operations is: R1 = join (T1, T2) ← combined data R2 = select ( R1, Subject = Computing) ← choose computing texts R3 = select ( R2, Country = UK) ← choose UK authors R4 = project (R3, {Book Title, Author}) ← list titles and authors (c) The table join(T1, T2) will find several matches on the title ‘Discrete Mathematics’.

This will give rise to erroneous entries such as: Celia Ringworth, U.K., Discrete Mathematics, 442-76003-8, Delphi, Mathematics One way to avoid this is to include an ISBN column in table T1. This uniquely identifies

each book and so the operation join(T1, T2) will then match on both Title and ISBN, producing consistent data entries.

6.3 (a)

APPLICATIONS 1 79

CHILD MOTHER CHILD1 CHILD MOTHER CHILD1 Charles Elizabeth Charles Charles Elizabeth Edward Charles Elizabeth Edward Anne Elizabeth Charles Charles Elizabeth Anne Anne Elizabeth Edward Edward Elizabeth Charles Harry Diana William Edward Elizabeth Edward Table P2 Edward Elizabeth Anne Anne Elizabeth Charles Anne Elizabeth Edward CHILD CHILD1 Anne Elizabeth Anne Charles Edward

William Diana William Anne Charles William Diana Harry Anne Edward Harry Diana William Harry William Harry Diana Harry Table P3

Table P1 (b) Pairs of children who share the same mother (c) One way to do this is to first construct a table Q giving both the father and mother of

each child. Then adopt the strategy followed in part (a). This produces the following sequence of operations: Q = join (F, M) ← matching on CHILD R = rename(Q, CHILD → CHILD1) ← new column heading R1 = join (R, Q) ← matching on {FATHER, MOTHER} R2 = select ( R1, CHILD < CHILD1 ) ← removing repeats R3 = project ( R2, { CHILD, CHILD1 }) ← producing sibling pairs

APPLICATIONS 1 80

APPLICATIONS 1 81

  • KNOWLEDGE BASED SYSTEMS
    • EXAMPLE 6.1
    • EXAMPLE 6.2
    • Solution
    • EXAMPLE 6.3
    • Solution
    • EXAMPLE 6.4
    • Solution
    • EXAMPLE 6.5
      • Solution
        • Name
      • Solution
    • EXAMPLE 6.7
      • Solution
        • Name
    • EXAMPLE 6.8
      • R1 is as follows:
        • Name

APPS2.pdf

APPLICATIONS 2 This week we look at two applications of functions and combinatorics: functional programming languages and efficiency of algorithms. A functional programming language manipulates symbols using basic primitive functions. They have been used successfully to build expert systems, model common sense reasoning, construct natural language interfaces and aid research into aspects of speech and vision. The power of such languages lies in their ability to build complicated processes from simple ones by composition of the basic primitive functions. One of the central issues in computer science is the design and analysis of ‘efficient’ computer algorithms. To carry out such an analysis we need to measure the requirements of an algorithm in terms of time and space. To measure the former we estimate the time taken as a function of the number of values processed. One way of doing this is to count, for a given input of size n, the number of elementary operations performed.

FUNCTIONAL PROGRAMMING LANGUAGES We describe some simple functional algorithms for manipulating text such as might be required in a simple text editor. We begin by specifying the sorts of objects that are to be handled and then specify some basic primitive functions that perform simple operations on the objects concerned. We shall not worry about how the machine evaluates these primitive functions. They will be available for use in much the same way as particular functions on a calculator may be called on at the press of a button. Let P denote the set {0, 1, 2, . . . }. Let C = {‘a’, ‘b’, ‘c’, . . . ,‘z’} denote the set of lower case characters of the English alphabet . Let S denote the set of strings of such characters. [For example, “bat” is an element of S, as is the empty string “ ”.]

APPLICATIONS 2 107

Now suppose that the following basic primitive functions are available : CHAR : S → C where CHAR(s) is the first character of the non-empty string s, REST : S → S where REST(s) is the string obtained from the non-empty string s by removing its first character, ADDCHAR : C × S → S where ADDCHAR(c, s) is the string obtained by adding the character c to the front of the string s, LEN : S → P where LEN(s) is the number of characters in s. As mentioned previously we do not worry about how the machine evaluates these functions. It is worth noting however that CHAR and REST are not strictly speaking functions since neither can be evaluated on the empty string. They are called PARTIAL FUNCTIONS since they have standard input and output sets but restricted domains. Therefore, we need to be careful in applying them to avoid undefined expressions, especially when forming compositions. EXAMPLE 9.1 If s = “bat” evaluate CHAR(s), REST(s), ADDCHAR(‘o’, s), LEN(REST(s)), and ADDCHAR(CHAR(s), ADDCHAR(‘o’, REST(s))). Solution CHAR(s) = REST(s) = ADDCHAR(‘o’, s) = LEN(REST(s)) = ADDCHAR(CHAR(s), ADDCHAR(‘o’, REST(s))) =

APPLICATIONS 2 108

EXAMPLE 9.2 Describe in words the effect of the function

ADDCHAR(CHAR(s), ADDCHAR(c, REST(s))) when s is any string and c is any character. Solution

EXAMPLE 9.3 A function THIRD : S → C is required which extracts the third character of any input string of length three or more. Express THIRD in terms of CHAR and REST. Solution

■ EXAMPLE 9.4 Find a function REVERSE2 : S → S which reverses the first two characters of an input string of length two or more. Solution

APPLICATIONS 2 109

EXAMPLE 9.5 Trace the following algorithm when s = “bard” Input s begin u := “ ”; t := s; i := 0; while i < LEN(s) do c := CHAR(t); t := REST(t); u := ADDCHAR(c, u); i := i + 1; end Output u What does the algorithm do ? Solution

loop c t u i i < 4? 0 - “bard” “ ” 0 yes

APPLICATIONS 2 110

APPLICATIONS 2 111

EFFICIENCY OF ALGORITHMS Consider the problem of determining whether or not a given word X is in a dictionary containing n words. We could employ a sequential search. This search compares X with the first word in the dictionary, then the second word and so on until either X is found, or else, in the worst case, not found. The worst case requires n comparisons. On the other hand, a binary search compares X with the ‘middle word’ in the dictionary and uses the lexicographical ordering of words to decide whether or not to search in the first or second half of the dictionary. This process is then repeated on a dictionary of ‘half the original size’, and so on. In the worst case this method requires 1 + log2n comparisons. As the table below shows, a binary search is much more efficient than a sequential search.

n 1 + log2n 8 4 64 7

218 ≈ 250,000 19

Note that log22k = k

EXAMPLE 9.6 Five algorithms, labelled A, B, C, D and E involve n, 3n2, 2n2 + 4n, n3 and 2n elementary operations respectively. If each operation performed takes one millisecond, estimate the running times when n = 1, 10, 100 and 1000. Solution

A B C D E n 3n2 2n2 + 4n n3 2n

1ms 3ms 6ms 1ms 2ms 10ms 300ms 240ms 1sec 1.024sec 100ms 30sec 20.4sec 0.28hrs 4×1017 centuries

1000ms 0.83hrs 0.56hrs 11.6days 10176 centuries

■ As the table shows there is a huge qualitative difference between formulae involving powers of n (polynomial functions) and those involving powering by n (exponential functions). Amongst polynomial functions there are orders of magnitude difference between functions involving different highest powers of n. When the same highest power of n is involved, the running times are comparable (see algorithms B and C). Suppose that f(n) and g(n) measure the efficiencies of two algorithms; the so-called time complexity functions. We say that f(n) is of order at most g(n), written as O(g(n)), if there is a positive constant C such that ⏐f(n)⏐ ≤ C⏐g(n)⏐ for all but a finite number of natural numbers n.

EXAMPLE 9.7 Show that 2n2 + 4n is O(n2). Solution

■ We can define a hierarchies of functions each of which has a greater order of magnitude than its predecessors. One example of such a hierarchy is as follows: 1 log2n 2 3 kn n n n1444442444443L L 2

n

↑ ↑ ↑ ↑ constant logarithmic polynomial exponential We could further refine this hierarchy by inserting (n log2n) between n and n2, (n2 log2n) between n2 and n3, and so on. What is important about the hierarchy is that as we move from left to right successive functions have greater orders of magnitude than previous functions. Consequently, as n increases the values of later functions increase more rapidly than the values of earlier ones.

16,384 2n 8192 n3 4096 2048 1024 512 256 128 n2 64 32 16 n 8 4 log2n 2 1 1

2 4 6 8 10 12 16 18 20 n

APPLICATIONS 2 108

A given time complexity function f(n) can now be assigned to some function g(n) in the hierarchy so that f(n) has order at most g(n) and f(n) has a greater order of magnitude than functions in the hierarchy occurring before g(n). To do so we use the hierarchy to identify the fastest increasing term in the given time complexity function, and then assign the time complexity function to the corresponding function in the hierarchy. For example, consider f(n) = 9n + 3n6 + 7log2n. Since constant multipliers do not affect orders of magnitude 9n is O(n), 3n6 is O(n6) and 7log2n is O(log2n). Since n and log n occur earlier in the hierarchy than n6, the fastest increasing term in f(n) is 3n6 and so f(n) is O(n6). EXAMPLE 9.8 Determine the least order of magnitude to which the following functions can be assigned. (a) n4 + 2n3 + 3n2 log2n (b) 6n5 + 2 n Solution (a) (b)

■ When calculating the time complexity function of an algorithm decisions need to be made as to what is the most appropriate measure of the size n of the problem and which are the most significant elementary operations to count.

APPLICATIONS 2 109

EXAMPLE 9.9 Find a time complexity function for the following fragment of pseudocode, by calculating the number of times the assignment statement x := x + 1 is executed. begin for i:=1 to 2n do for j:=1 to n do for k:=1 to j do x := x + 1; end Solution The outer loop (indexed by i) is executed _____ times. The loop indexed by j is executed ____ times. For each value of j, the statement x:= x + 1 is executed ____ times. Hence, for each value of i, the statement x:= x + 1 is executed _________________________ times. Therefore, the time complexity function T(n) is given by T(n) = ____________________ . Hence, T(n) is O( ).

APPLICATIONS 2 110

APPLICATIONS 2 111

EXERCISES 9 - APPLICATIONS 2 9.1 A function REVERSE : S → S is defined by REVERSE(s) = the string

obtained from s by reversing the characters in s. For example, REVERSE(“pots”) = “stop”.

(a) A function F : S → S is given by

F(s) = REVERSE(REST(REVERSE(REST(s)))). Evaluate F(s) when s = “pots”. Describe in words the effect of F on any string s containing at least two

characters. (b) A function G : C × S → S is required which adds a character c onto the

end of a string s. Express G in terms of ADDCHAR and REVERSE. (c) Trace the values of u, v and LEN(v) in the following algorithm when s =

“straw” and t = “berry” Input s Input t begin u := t; v := REVERSE (s); while LEN(v) ≠ 0 do begin u := ADDCHAR(CHAR(v),u); v := REST(v); end end Output u Describe in words the effect of the algorithm on arbitrary string s and t. 9.2 Construct a pseudocode algorithm that will count the number of times the

character ‘a’ occurs in a given string s. 9.3 Determine the least order of magnitude of each of the following time

complexity functions. (a) ( )22 1n n+ + (b) 2 25 log 7n n n+ +

(c) 19n + 3 n n

APPLICATIONS 2 112

9.4 The following algorithm finds the location of the first term in a sequence of integers that equals some previous term in the sequence.

Input x , x , . . . , xn 1 2 begin location := 0; i := 2; while i ≤ n and location = 0 do begin j := 1; while j < i and location = 0 do if =i jx x then location := i; else j := j + 1; i := i + 1; end end

(a) Verify that the algorithm works on the sequence 4, 8, 5, 8, 4. (b) Find a time complexity function for the algorithm by calculating how

many times the comparison i jx x= is performed for an input sequence of length n.

APPLICATIONS 2 113

SOLUTIONS TO EXERCISES 9 – APPLICATIONS 2 9.1 (a) F(“pots”) = REVERSE(REST(REVERSE(REST(“pots”)))) = REVERSE(REST(REVERSE(“ots”))) = REVERSE(REST(“sto”)) = REVERSE(“to”) = “ot” F removes the first and last characters of s . (b) G(c, s) = REVERSE(ADDCHAR(c, REVERSE(s))) (c) The trace table is as follows:

u v LEN(v) “berry ” “warts” 5 “wberry” “arts” 4 “awberry” “rts” 3 “rawberry” “ts” 2 “trawberry” “s” 1 “strawberry” “ ” 0

The algorithm concatenates two strings s and t. 9.2 Input s begin count := 0; t := s; while LEN(t) ≠ 0 do if CHAR(t) = ‘a’ then count := count + 1; else {do nothing}; t := REST(t); end Output count

9.3 (a) is O(n4). ( )2 4 3 22 1 2 3 2 n n n n n n=+ + + + + + 1 (b) Since n2 < n2log2n < n

3, the function is O(n3). (c) Since 1 < n < n, the function is O(n2). 9.4 (a) The following five comparisons are carried out.

comparison location x2 = 8 with x1 = 4 0 x3 = 5 with x1 = 4 0 x3 = 5 with x2 = 8 0 x4 = 8 with x1 = 4 0 x4 = 8 with x2 = 8 4

(b) In the worst case there is one comparison when i = 2, two comparisons when i = 3 and

so on up to n – 1 comparisons when i = n. Hence, T(n) = 1 + 2 + . . + (n – 1) = ( )12 1n n− . Therefore, T(n) is O(n2).

APPLICATIONS 2 114

  • FUNCTIONAL PROGRAMMING LANGUAGES
    • EXAMPLE 9.1
      • Solution
        • EXAMPLE 9.3
        • Solution
        • EXAMPLE 9.5
    • EXAMPLE 9.6
    • Solution
    • EXAMPLE 9.7
    • EXAMPLE 9.8

COMB.pdf

COMBINATORICS COMBINATORICS is a branch of mathematics concerned with counting. Problem 1 A small shop has a number of cakes left at the end of the day. There are four vanilla sponge cakes, two chocolate sponge cakes and three fruit cakes. A customer rushes in to buy a cake before closing time. How many cakes can the customer choose from? Problem 2 A mixed doubles team is to be chosen to represent the local tennis club. There are six female players and nine male players. How many different pairs are possible? The first problem is easy - since all the cakes are different we can simply add the number of different cakes. This gives the customer 4 + 2 + 3 = 9 cakes from which to make a selection. For the second problem, there are six ways to select the female player and for each of these women there are nine possible male partners. This gives a total of 6 × 9 = 54 pairs. These two simple problems illustrate two fundamental counting principles: The ADDITION PRINCIPLE: If A and B are disjoint events and there are n1 possible outcomes for event A and n2 possible outcomes for event B, then the total number of possible outcomes for the event ‘A or B ’ is just n1 + n2 . The MULTIPLICATION PRINCIPLE: If there is a sequence of k events with n1 possible outcomes for the first event, n2 possible outcomes for the second event, up to nk possible outcomes for the kth event then, the total number of possible outcomes for the sequence of k events is given by n1 × n2 × . . . × nk. EXAMPLE 8.1 A computer password consists of six characters. If the first three are lower case letters and the remaining three are digits, how many different passwords are possible? Solution

COMBINATORICS 95

Suppose that a child is offered a bag containing three types of sweets, aniseed drops (A), butter mints (B) and cherry drops (C). In how many ways can the child select two sweets? The question can be answered in a number of ways. We are not told whether or not the same type of sweet can be selected twice. For instance, is AA allowed? Also, does it matter in which order the sweets are chosen? In other words, is AB to be treated as a different choice from BA, or not? This gives us four cases to consider: • Repeats allowed and order matters; this gives the nine possibilities AA, AB, AC,

BA, BB, BC, CA, CB and CC. • Repeats allowed and order does not matter; this gives the six possibilities AA,

AB, AC, BB, BC and CC. • Repeats not allowed and order matters; this gives the six possibilities AB, AC,

BA, BC, CA and CB. • Repeats not allowed and order does not matter; this gives the three possibilities

AB, AC and BC.

Consider the selection of k objects from a set of n objects where order matters and repeats are allowed. Any such selection is called a k-sample. Since repetition is allowed, there are n ways to select the first object. After selecting the first object, there are n ways to select the second object and so on until all k objects have been selected. By the multiplication principle, this gives nk possible k- samples. EXAMPLE 8.2 A computer represents integers using N binary digits; the first to indicate the sign (+ or −), the remaining N − 1 to represent the magnitude of the integer. How many distinct integers can be represented with N binary digits? Solution

COMBINATORICS 96

COMBINATORICS 97

Now consider the selection of k objects from a set of n objects where order matters and repeats are not allowed. Any such selection is called a k-permutation and the total number of possible k-permutations is denoted by P(n, k). Since repetition is not allowed, there are n ways to select the first object, (n − 1) ways to select the second object, (n − 2) ways to select the third object and so on up to (n − k + 1) ways to select the kth object. By the multiplication principle, this gives

P(n, k) = n(n − 1) . . . (n − k + 1) = ! ( )!

n n k−

.

n n n n! is read as factorial and equals ( ) . . For example, 4! = 4 3 2 1 = 24

× − × × × × × ×

L N M M

O Q P P

1 2 1

EXAMPLE 8.3 How many four-letter ‘words’ can be made with distinct letters from the list a, g, m, o, p and r? Solution

■ Next consider the selection of k objects from a set of n objects where order does not matter and repeats are not allowed. Any such selection is called a k-combination and the total number of possible k-combinations is denoted by C(n, k). By the multiplication principle, the number of permutations of k distinct objects selected from n objects is the number of unordered ways to select the objects multiplied by the number of ways to order the objects selected. In other words, P(n, k) = C(n, k) × k! Therefore, C(n, k) = ( , )

! P n k

k = !

( )! ! n

n k k− .

COMBINATORICS 98

EXAMPLE 8.4 A menu in a Chinese restaurant allows you to order exactly three of seven main dishes. How many different combinations can you order? Solution

■ Finally, a k-selection is the unordered selection of k objects from a set of n objects with repetition allowed. It can be shown [see p.106 of Haggarty] that the total number of k-selections from a set of n objects is given by

C(n + k − 1, n − 1) = ( 1 ! ( 1)! n k k n

)!+ − −

EXAMPLE 8.5 A florist stocks roses in four different colours. How many different bouquets of one dozen roses can be made up? Solution

COMBINATORICS 99

In summary:

Order matters

Order does not matter

Elements repeated

k-sample kn

k-selection

( 1 ! ( 1)! n k k n

)!+ − −

Elements

not repeated

k-permutation !

( )! n

n k−

k-combination !

( )! ! n

n k k−

EXAMPLE 8.6 Twelve people, including Mary and Peter, are candidates to serve on a committee of five. How many different committees are possible? Of these, how many (a) contain both Mary and Peter? (b) contain neither Mary nor Peter? (c) contain either Mary or Peter? Solution There are C( , ) = = possible committees (a) (b) (c)

IT'S A LOTTERY! In the National Lottery, the Lotto draw consists of six (different) numbers chosen at random from the numbers 1, 2, . . . , 49. Anyone who has preselected these six numbers wins a share of the jackpot prize of several million pounds. But what are the odds of hitting the jackpot? Well, there are C(49, 6) different ways of choosing an unordered set of six (different) numbers. Since C(49, 6) = 49!

43! 6! = 13,983,816, the odds of hitting the jackpot is an

astronomical 13,983,816 to 1. You are more likely to be struck by lightning! There are lesser cash prizes for anyone who gets five, four or three correct numbers in their choice of six numbers. Bonus prizes are also given to anyone who gets five correct and whose sixth choice matches an extra randomly drawn ‘bonus’ number. The amount of prize money available depends on the number of lottery tickets sold except that three (and no more) correct numbers wins an automatic £10. So, what are the odds of winning £10? Well, you can regard your preselection of six numbers as comprising (with hindsight) of two disjoint events; selecting three correct numbers and selecting three incorrect numbers. There are C(6, 3) ways of selecting three of the six numbers declared in a Lotto draw, and C(43, 3) ways of selecting three incorrect numbers. The total number of winning combinations is thus C(6, 3) × C(43, 3) =

6! 3! 3!

× 43! 40! 3!

= 246,820.

This gives odds of 13,983,816

246,820 to 1 or, approximately 57 to 1.

The odds for the various other draws can be calculated in a similar manner.

COMBINATORICS 100

The numbers C(n, k) arise naturally when we expand the expression (a + b)n algebraically. For example, (a + b)3 = (a + b) (a + b) (a + b) = aaa + aab + aba + abb +

baa + bab + bba + bbb = a3 + 3 a2 b + 3 a b2 + b3 Each of the six terms in the original expansion arises from multiplying together one term from each bracket. This leads, for instance, to three terms involving one a and two bs. This is because there are C(3, 2) = 3 ways of selecting two bs from the three brackets. Therefore, the coefficients in the simplified expansion are C(3, 0) = 1, C(3, 1) = 3, C(3, 2) = 3 and C(3, 3) = 1. In general, the expansion of (a + b)n contains terms of the form k n ka b − for k = 0, 1, . . . , n and there are precisely C(n, k) such terms. Therefore,

( ) ( ) 0

, n

n n k k

k

a b C n k a b− =

+ = ∑ This is called the BINOMIAL EXPANSION and in this context, C(n, k) is called a BINOMIAL COEFFICIENT. Binomial coefficients can be arranged in a triangular array called Pascal’s triangle

C(0, 0) C(1, 0) C(1,1)

C(2, 0) C(2, 1) C(2, 2) C(3, 0) C(3, 1) C(3, 2) C(3, 3)

C(4, 0) C(4, 1) C(4, 2) C(4, 3) C(4, 4) C(5, 0) C(5, 1) C(5, 2) C(5, 3) C(5, 4) C(5, 5)

C(n, 0) C(n, 1) . . . . . . . . C(n, n−1) C(n, n)

COMBINATORICS 101

If we calculate numerical values Pascal’s triangle has the form 1

1 1 1 2 1

1 3 3 1 1 4 6 4 1

1 5 10 10 5 1 . .

Since C(n, 0) = C(n, n) = 1, the outer edges of Pascal’s triangle are 1s. The vertical symmetry in the table follows since C(n, k) = C(n, n − k). Also, any element not on the outer edge is the sum of the two elements directly above it in the preceding row. [For a proof of this result, see p. 111 of Haggarty.] Finally, we consider the number of different rearrangements that can be made from a set of objects some of which are repeated. For example, how many different rearrangements there are of the letters in the word DEFENDER. There are 8 letters which can be arranged in 8! ways. However, in any arrangement, the three Es are indistinguishable and can be permuted in 3! ways without changing the arrangement. Similarly the two Ds can be permuted in 2! ways without affecting any particular arrangement. Therefore, the number of distinguishable rearrangements is just 8!

3! 2! = 3360.

In general we have the REARRANGEMENT THEOREM which states that there are

1 2

! ! ! . . . !r

n n n n

different re-arrangements of n objects of which n1 are of type 1,

n2 are of type 2 and so on up to nr of type r. EXAMPLE 8.7 A coin is flipped 20 times. How many ways are there of getting 15 heads and 5 tails? Solution

COMBINATORICS 102

EXERCISES 8 - COMBINATORICS 8.1 (a) How many four digit numbers less than 6000 can be made using only odd

digits? (b) A computer password consists of six characters. The first two must be

lower case letters and the remaining four can be either lower case letters or digits. How many different passwords are possible?

8.2 Find the values of the following: (a) P(7, 2), P(8, 5), P(6, 4), P(n, 1) and P(n, n − 1) (b) C(10, 7), C(9, 2), C(8, 6), C(n, 1) and C(n, n − 1) Show that C(n, k) = C(n, n − k). 8.3 (a) A computer password is chosen as in Exercise 8.1(b). How many

passwords are there which contain no repeated characters? (b) A hockey squad has 18 players; 11 players make a team. How many

different teams are possible? 8.4 A select committee of three MPs is to be formed from five Conservative, three

Labour and four Liberal Democrat members. (a) In how many ways can the committee be formed? (b) In how many ways can the committee be formed if at least one Liberal

Democrat must be included? (c) In how many ways can the committee be formed if it cannot include both

Labour and Conservative members? (d) In how many ways can the committee be formed if it must include at least

one Conservative and at least one Labour member? 8.5 You are buying five Christmas cards from a shop which stocks four different types

that you like. 8.6 (a) How many ways are there to buy the five cards? (b) How many possibilities include exactly two of the four types? 8.6 The eighth row of Pascal’s triangle is 1 7 21 35 35 21 7 1 Generate the ninth and tenth rows Hence, or otherwise, find the coefficient of a5b4 in the expansion of (a + b)9 8.7 By putting a = b = 1 in the binomial expansion, show that

COMBINATORICS 103

C(n , 0) + C(n , 1) + . . . + C(n , n ) = 2n for n = 0, 1, 2, . . . Deduce that if S is a set containing n elements then S has 2n different subsets. [Hint: how many subsets of S contain k elements?] 8.8 How many distinct rearrangements are there of the letters in the word

ABRACADABRA? How many of these (a) begin with the letter C? (b) have both Bs together?

COMBINATORICS 104

SOLUTIONS TO EXERCISES 8 - COMBINATORICS 8.1 (a) The first digit must be 1 or 3 or 5. The remaining three digits can each be selected from

the five odd digits. Therefore, there are 3 × 5 × 5 × 5 = 375 four digit numbers less than 6000 and containing only odd digits.

(b) There are 26 × 26 = 676 ways to select the first two characters. Each of the remaining

four characters can be selected from 36 characters (26 letters and 10 digits). This can be done in 36 × 36 × 36 × 36 = 1,679,616 ways. Therefore, 676 × 1,679,616 = 1,135,420,416 different passwords are possible.

8.2 (a) P(7, 2) = 42, P(8, 5) = 6720, P(6, 4) = 360, P(n, 1) = n and P(n, n−1) = n! (b) C(10, 7) = 120, C(9, 2) = 36, C(8, 6) = 28, C(n, 1) = C(n, n−1) = n The numbers of ways of selecting k different elements from a set with n elements is the same as

the number ways of not selecting n − k elements. Hence, C(n, k) = C(n, n − k). 8.3 (a) There are P(26, 2) = 650 ways to select the first two characters and P(34, 4) = 1,113,024

ways to select the last four characters. This gives a total of 650 × 1,113,024 = 723,465,600 different passwords which contain no repeated characters.

(b) Order of selection does not matter and repeats are not allowed. Hence, there are C(18,

11) = 31,824 different team selections possible. 8.4 (a) C(12, 3) = 220 (b) There are C(8, 3) = 56 committees which contain no Liberal Democrat members. Since

there are 220 committees that can be formed when no restrictions are imposed, there are 220 − 56 = 164 possible committees containing at least one Liberal Democrat.

(c) If the committee contains no Labour members there are C(9, 3) = 84 choices. If the

committee contains no Conservative members there are C(7, 3) = 35 choices. This gives a total of 84 + 35 = 119 possibilities. However, this counts the number of committees consisting solely of Liberal Democrats twice. There are C(4, 3) = 4 of these and so there are 115 committees of the required type.

(d) There are C(5, 2)×C(3, 1) = 10×3 = 30 committees consisting of two Conservatives and

one Labour member, C(5, 1)×C(3, 2) = 15 consisting of one Conservative and two Labour members, and C(5, 1)×C(3, 1)×C(4, 1) = 60 consisting of one member from each party. This gives a total of 30 + 15 + 60 = 105 possibilities.

8.5 (a) This is the unordered selection of five objects from a set of four objects with repetition

allowed. This can be done in C(4 + 5 − 1, 4 − 1) = C(8, 3) = 56 ways. (b) There are C(4, 2) = 6 ways of specifying any two types of card. For each of these

specifications we make an unordered selection of five objects from the two types with repetition. This can be done in C(2 + 5 − 1, 2 − 1) = C(6, 1) = 6 ways. However, two of these possibilities contain only one type of card. Therefore, there are 6 × 4 = 24 selections of five cards drawn from exactly two of the types of cards on offer.

COMBINATORICS 105

8.6 The ninth and tenth rows are generated as below: 1 7 21 35 35 21 7 1

1 8 28 56 70 56 28 8 1 1 9 36 84 126 126 84 36 9 1

The required coefficient is the fifth entry in the tenth row of Pascal's triangle. i.e. 126 8.7 The number of k-element subsets of a set S containing n elements is just the number of ways of

selecting k elements from a set of n elements (unordered and without repeats). This is precisely the quantity C(n, k). Since subsets of S contain 0 or 1 or 2 or . . . or n elements, there are C(n, 0) + C(n, 1) + C(n, 2) + . . . + C(n, n) = 2n different subsets of S.

8.8 There are 11! permutations of the letters A, B, R, A, C, A, D, A, B, R and A. Since the five As,

two Bs and two Rs are indistinguishable, there are 11!

5! 2! 2! = 83160 different

rearrangements.

(a) There are 10!

5! 2! 2! = 7560 different rearrangements of the letters A, B, R, A, A, D, A,

B, R and A. Hence there are 7560 different rearrangements which begin with C. (b) We require the number of different rearrangements of the ten objects (BB), A, R, A, C,

A, D, A, R and A. This is just 10!

5! 2! = 15120.

COMBINATORICS 106

CONTENTS.pdf

CONTENTS Page WELCOME i COURSE CONTENT ii BOOKS iv INTRODUCTION 1 EXERCISES 1 – ALGORITHMS 7 LOGIC 11 EXERCISES 2 – LOGIC 18 PROOF 23 EXERCISES 3 – PROOF 32 SETS 37 EXERCISES 4 – SETS 45 RELATIONS 51 EXERCISES 5 – RELATIONS 60 APPLICATIONS 1 65 EXERCISES 6 – APPLICATIONS 1 75 FUNCTIONS 81 EXERCISES 7 – FUNCTIONS 91 COMBINATORICS 95 EXERCISES 8 – COMBINATORICS 103 APPLICATIONS 2 107 EXERCISES 9 – APPLICATIONS 2 111 GRAPHS 115 EXERCISES 10 – GRAPHS 127 DIGRAPHS 133 EXERCISES 11 – DIGRAPHS 144 HANDBOOK 151

WELCOME Welcome to U08606 Discrete Mathematics. The main aim of the module is to introduce you to those key mathematical concepts required in computing. The concepts discussed in the course underpin many of the computing topics developed in advanced computing modules. This booklet contains information about each topic and a full set of the slides used in lectures. Each week lectures will cover the material covered in the relevant slides. During lectures, the solutions to the numerous Examples and Problems will be presented for you to write in the spaces provided. The practical sessions which support the module will enable you to start working on the week’s (unassessed) exercises, designed to test your understanding of the lecture material, with a tutor available for advice. It is important that you endeavour to complete these exercises in your own time which is why the solutions are included. Any difficulties you encounter with the lecture material and/or the exercises can be raised in the practical sessions and at the surgeries which support this module. The module is based on the recommended book Discrete Mathematics for Computing by Rod Haggarty. Copies of this text and numerous other books on discrete mathematics are held in the University library, some in the short loan collection. A full list is given on page iv. The coursework assessment for the module will consist of two assignments, each worth 25% of the overall assessment. The coursework will test both your understanding of the mathematical content of the module and the major applications covered in weeks 6 and 9. The end of semester examination, which accounts for 50% of the overall assessment, is two hours long and will test only the mathematical content of the module and not any of the major applications. In the examination you will receive a copy of the Discrete Mathematics Handbook which contains all the key definitions and results. If you have any queries about this module, please feel free to raise them with the module leader or with your practical tutor. Above all, do try to keep up with the pace of the course and do devote sufficient time to independent study of the course materials.

i

COURSE CONTENT WEEK 1 – INTRODUCTION

Lecture topics: Mathematical modelling. Prim’s algorithm. Pseudocode.

Practical: EXERCISES 1 – ALGORITHMS WEEK 2 – LOGIC

Lecture topics: Logic and propositions. Predicates. Valid arguments.

Practical: EXERCISES 2 – LOGIC WEEK 3 – PROOF

Lecture topics: Methods of proof. Correctness of algorithms. Mathematical induction.

Practical: EXERCISES 3 – PROOF WEEK 4 – SETS Lecture topics: Set notation and operations on sets. The laws of the algebra of

sets. Further properties of sets.

Practical: EXERCISES 4 – SETS WEEK 5 – RELATIONS Lecture topics: Relations. Binary relations and their graphical and matrix

representations. Properties of relations. Equivalence relations and partitions. Order relations.

Practical: EXERCISES 5 – RELATIONS WEEK 6 – APPLICATIONS 1

Lecture topics: Knowledge based systems. Database management systems.

Practical: EXERCISES 6 – APPLICATIONS 1 WEEK 7 – FUNCTIONS

Lecture topics: Composition of relations and inverse relations. Functions. Injective, surjective and bijective functions. Inverse functions.

Practical: EXERCISES 7 – FUNCTIONS

ii

WEEK 8 – COMBINATORICS Lecture topics: Combinatorial problems. Addition and multiplication principles.

Samples, permutations, combinations and selections. The binomial expansion and Pascal’s triangle.

Practical: EXERCISES 8 – COMBINATORICS WEEK 9 – APPLICATIONS 2

Lecture topics: Functional programming languages. Efficiency of algorithms.

Practical: EXERCISES 9 – APPLICATIONS 2 WEEK 10 – GRAPHS

Lecture topics: Graphs and terminology. Eulerian trails and Hamiltonian cycles. The travelling salesperson problem and the nearest neighbour algorithm. The minimum connector problem and the minimal spanning tree algorithm. Trees. Binary search trees.

Practical: EXERCISES 10 – GRAPHS WEEK 11 – DIGRAPHS Lecture topics: Directed graphs and terminology. Consistent labellings and the

topological sort algorithm. Paths in digraphs and the reachability matrix. Shortest paths and Dijkstra’s algorithm. Communication networks.

Practical: EXERCISES 11 – DIGRAPHS

iii

iv

BOOKS Books on Discrete Mathematics are held in both the Wheatley and Headington libraries and are located at 510 (mathematics section) and 004.0151 (computing section). A keyword search in the electronic catalogue reveals all! They all contain useful information, but the following are most relevant to the module. There are multiple copies of most of these books and some copies of those marked with an asterisk are held in the four-hour short loan collections. Biggs N. L. Discrete Mathematics, 1e/2e, Oxford 1985/2002 [510/BIG] Booth D. J. Foundation Discrete Mathematics for Computing, Chapman Hall

1995 [004.0151/BOO] * Chetwynd A. Discrete Mathematics, Arnold 1995 [510/CHE] Finkbeiner D. T. A Primer of Discrete Mathematics, Freeman 1987 [510/FIN] * Garnier R. Discrete Mathematics for New Technology, 1e/2e, IOP

1992/2002 [510/GAR] Gerstein L. J. Discrete Mathematics and Algebraic Structures, Freeman 1987

[510/GER] Gersting J. L. Mathematical Structures for Computer Science, 3e/4e/5e,

Freeman 1993/1999/2002 [004.0151/GER] Grossman P. Discrete Mathematics for Computing, Macmillan 1995

[004.0151/GRO] * Haggarty R. J. Discrete Mathematics for Computing, Addison-Wesley 2002

[004.0151/HAG] Johnsonbaugh R. Discrete Mathematics, 3e/4e, Prentice Hall 1993/1997 [510/JOH] Liu C. L. Elements of Discrete Mathematics, McGraw Hill 1985 [510/LIU] Mattson H. F. Discrete Mathematics with Applications, Wiley 1993 [510/MAT] Rosen K. H. Discrete Mathematics and its Applications, 3e/4e, McGraw Hill

1995/1999 [510/ROS] * Skvarcius R. Discrete Mathematics with Computer Science Applications,

Benjamin 1986 [004.0151/SKV] Truss J. K. Discrete Mathematics for Computer Scientists, 1e/2e, Addison-

Wesley 1991/1999 [004.0151/TRU] Wiitala S. A. Discrete Mathematics, a Unified Approach, McGraw Hill 1987

[510/WII]

  • WELCOME i
  • INTRODUCTION 1
  • LOGIC 11
  • PROOF 23
  • SETS 37
  • RELATIONS 51
  • APPLICATIONS 1 65
  • FUNCTIONS 81
  • COMBINATORICS 95
  • APPLICATIONS 2 107
  • GRAPHS 115
  • DIGRAPHS 133
  • HANDBOOK 151
    • WELCOME
      • WEEK 1 – INTRODUCTION
      • WEEK 2 – LOGIC
      • WEEK 3 – PROOF
      • WEEK 4 – SETS
      • WEEK 5 – RELATIONS
      • WEEK 6 – APPLICATIONS 1
      • WEEK 7 – FUNCTIONS
      • WEEK 9 – APPLICATIONS 2
      • WEEK 10 – GRAPHS
      • WEEK 11 – DIGRAPHS

DIGRAPH.pdf

DIGRAPHS DIRECTED GRAPHS or DIGRAPHS for short, are used to model situations in which there is a partial ordering between objects. A DIGRAPH is a pair G = (V, E) where V is a finite set of vertices and E is a relation on V. The elements of E are called arcs and they are depicted as directed edges linking related ordered pairs of vertices. In a simple digraph there are no loops or multiple edges. Hence, there is at most one arc uv from vertex u to vertex v, and at most one arc vu from vertex v to vertex u. If uv is an arc, u is called an ANTECEDENT of v. For example,

a b

c d

is a simple digraph with vertex set V = {a, b, c, d} and arc set E = {ab, bd, cb, db, dc}. The adjacency matrix is:

F T F F F F F T F T F F F T T F

a b c d

a b c d

⎡ ⎤ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥⎣ ⎦

[a, c and d are all antecedents of b] A CYCLE in a digraph is a sequence of vertices v0, v1, . . . , vk such that vi−1vi is an arc for i = 1 to k, the first and last vertices are the same and no other vertices (or arcs) are repeated. If there are no such cycles, G is called an ACYCLIC digraph. Acyclic digraphs are useful as models of situations in which given tasks need to be implemented in a certain order. In task-scheduling problems the corresponding acyclic digraph is known as a PERT chart.

DIGRAPHS 133

EXAMPLE 11.1 As part of a Biosciences degree programme a student wishes to include the eight modules listed in the table below. Draw a PERT chart to illustrate the given prerequisite structure.

prerequisite modules Advanced Biotechnology B Biotechnology C Cell Biology H DNA Structures C Enzyme Activity D, G Food Science E Genetic Engineering C Human Biology none

Solution

Suppose the student wants to find an order in which to take the modules which is consistent with the given prerequisites. This can be achieved by using the following TOPOLOGICAL SORT ALGORITHM. The vertices of the digraph are labelled 1, 2, . . . , n such that when uv is an arc and u has label i and v has label j, then i < j. This is called a CONSISTENT LABELLING

DIGRAPHS 134

THE TOPOLOGICAL SORT ALGORITHM {The algorithm produces a consistent labelling of an acyclic digraph G = (V, E). Initially, A(v) contains the antecedents of each vertex v.} begin for v ∈ V do

calculate A(v); label := 0; while unlabelled vertices v remain for which A(v) = Ø do begin

label := label + 1; u := a vertex with A(u) = Ø; assign label to u; for each unlabelled vertex v∈V do A(v) := A(v) – {u}; end end The algorithm successively assigns labels to vertices. A vertex receives the smallest available label whenever it has no unlabelled antecedents. EXAMPLE 11.2 Find a consistent labelling for the digraph in Example 11.1. Solution Before entering the while loop for the first time we calculate the antecedent sets. A(A) = {B} A(E) = {D, G} A(B) = {C} A(F) = {E} A(C) = {H} A(G) = {C} A(D) = {C} A(H) = Ø Now we enter the while loop for the first time and assign label 1 to vertex ____ and delete all references to that vertex to obtain A(A) = {B} A(E) = {D, G} A(B) = {C} A(F) = {E} A(C) = Ø A(G) = {C} A(D) = {C}

DIGRAPHS 135

On the second pass through the while loop we assign label 2 to vertex ___ and delete all references to that vertex to obtain A(A) = {B} A(E) = {D, G} A(B) = Ø A(F) = {E} A(D) = Ø A(G) = Ø This gives some choice as to the next vertex to be labelled. Each choice leads to a different consistent labelling. As shown on pp. 152-3 of Haggarty, one possible consistent labelling is H, C, B, A, D, G, E, F.

■ Directed graphs can be used to represent such things as airline routes between various cities in the world, or communication paths between computers in a computer network. In such networks it is important to know the consequences of the temporary removal of any link (arc or vertex), on the overall network. For example, if aircraft cannot land for refuelling in some city due to adverse weather conditions, it may be impossible to reroute to reach other cities. Similarly, if one or more paths in a computer network fail, some computer users may no longer be able to access certain servers. We therefore address the problem of determining the existence of paths between any two vertices in a directed graph. Let G = (V, E) be a digraph with n vertices and M be its adjacency matrix. An entry of T in the matrix M indicates the existence of an arc in G; an arc is a path of length 1. The logical Boolean product of M with itself is M 2. Each T entry in M 2 indicates the existence of a path of length 2. Paths of length 3 are recorded as T entries in the matrix M 3 = M.M.M and so on. Finally, the REACHABILITY MATRIX

M* = M or M 2 or . . . or M n records the existence of paths of some length between vertices. [The logical or of two matrices (with the same number of rows and the same number of columns) is defined to be the result of forming the logical or of corresponding entries.]

DIGRAPHS 136

DIGRAPHS 137

EXAMPLE 11.3 Calculate the reachability matrix of the following digraph:

a b

c d

Solution

F T F F F F T T

F F F F F F T F

M =

⎡ ⎤ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥⎣ ⎦

2

F T F F F T F F F F T T F F T T

= F F F F F F F F F F T F F F T F

M =

⎡ ⎤ ⎡ ⎤ ⎡ ⎢ ⎥ ⎢ ⎥ ⎢ ⎢ ⎥ ⎢ ⎥ ⎢ ⎢ ⎥ ⎢ ⎥ ⎢ ⎢ ⎥ ⎢ ⎥ ⎢⎣ ⎦ ⎣ ⎦ ⎣

⎤ ⎥ ⎥ ⎥ ⎥⎦

Further calculation gives

3

F F T F F F F F

F F F F F F F F

M =

⎡ ⎤ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥⎣ ⎦

4

F F F F F F F F

F F F F F F F F

M =

⎡ ⎤ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥⎣ ⎦

Therefore,

* M =

⎡ ⎤ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥⎣ ⎦

There are more efficient algorithms for computing M* such as WARSHALL’S algorithm. See pp. 155-157 of Haggarty for further details.

We now consider the problem of finding a shortest path between any two vertices in a weighted digraph. This has obvious applications to transport networks and communications networks. Consider the following WEIGHTED DIGRAPH for which we seek the shortest path from vertex A to each of the other vertices.

B 1 C

2 5

A 4 F

3 1

D 2 E

We shall use DIJKSTRA’S ALGORITHM, one of many shortest path algorithms. For each vertex v in the digraph, we assign the label d[v] to represent the distance from A to v. Initially d[v] is the weight of the edge (A, v), if such an edge exists, or ∞ otherwise. We traverse the vertices of the digraph and improve the computed value of d[v] as we go. At each step of the algorithm a vertex u is marked once we are sure that we have found the best route to it. For the remaining unmarked vertices v, the label d[v] is replaced by the minimum of its current value and the distance to v via the last marked vertex u. The algorithm terminates once all vertices have received their final label and all possible vertices have been marked. For each step described below, we generate a row in a table which records the vertex to be marked, the current values of d[v] and the remaining unmarked vertices. Step 0 Since we are to start at A, we use the first row of ω for the initial values of d[v]. Since A is closest to A we mark A.

Step

Vertex to be marked

Distance to vertex

A B C D E F

Unmarked vertices

0 A 0 2 ∞ 3 ∞ ∞ B,C,D,E,F

Step 1

DIGRAPHS 138

Mark B, since B is the closest unmarked vertex to A, and calculate distances to unmarked vertices via B. If a shorter distance is found use this. In this instance, A→ B→C is 3 and A→B→E is 6, whereas previously d[C] and d[E] were ∞.

Step

Vertex to be marked

Distance to vertex

A B C D E F

Unmarked vertices

0 A 0 2 ∞ 3 ∞ ∞ B,C,D,E,F 1 B 0 2 3 3 6 ∞ C, D, E, F

Step 2 Of the remaining unmarked vertices D and C are closest to A. We arbitrarily pick D to mark first. Because the path A→D→E has length 5, the current value of d[E] can be updated to 5.

Step

Vertex to be marked

Distance to vertex

A B C D E F

Unmarked vertices

0 A 0 2 ∞ 3 ∞ ∞ B,C,D,E,F 1 B 0 2 3 3 6 ∞ C, D, E, F 2 D 0 2 3 3 5 ∞ C, E, F

Step 3 Now C is still closest to A, so we mark it and recompute distances. The vertex F can be accessed for the first time by the path A→B→C→F, and so d[F] = 8.

Step

Vertex to be marked

Distance to vertex

A B C D E F

Unmarked vertices

0 A 0 2 ∞ 3 ∞ ∞ B,C,D,E,F 1 B 0 2 3 3 6 ∞ C, D, E, F 2 D 0 2 3 3 5 ∞ C, E, F 3 C 0 2 3 3 5 8 E, F

Step 4/Step 5 We mark E next, which reduces the distance to F from 8 to 6. Finally mark F.

Step

Vertex to be marked

Distance to vertex

A B C D E F

Unmarked vertices

0 A 0 2 ∞ 3 ∞ ∞ B,C,D,E,F 1 B 0 2 3 3 6 ∞ C, D, E, F 2 D 0 2 3 3 5 ∞ C, E, F 3 C 0 2 3 3 5 8 E, F 4 E 0 2 3 3 5 6 F 5 F 0 2 3 3 5 6

The shortest distances from A to all vertices are recorded in the final line of the table above: so d[A] = 0, d[B] = 2, d[C] = 3, d[D] = 3, d[E] = 5 and d[F] = 6.

DIGRAPHS 139

With care the shortest paths can be recovered from this table by examining when each vertex first receives its final label. For example, the shortest path from A to F is A→D→E→F. For completeness we now give the full algorithm which not only computes the shortest distances from the start vertex but also records the shortest path involved. DIJKSTRA’S ALGORITHM {Let (V, E) be a weighted digraph and A be a vertex. The algorithm finds the shortest path PATHTO(v) from A to any vertex v and the overall distance d[v]. For vertices u and v, ω(u, v) is the ‘weight’ of the arc uv.} begin for each v∈V do begin d[v] := ω(A, v); PATHTO(v) := A; end Mark vertex A; while unmarked vertices remain that have finite distance from A do begin u := unmarked vertex whose distance from A is minimal; Mark vertex u; for each unmarked vertex v with uv∈E do begin d' := d[u] + ω(u, v) if d' < d[v] then begin d[v] := d'; PATHTO(v):= PATHTO(u), v; end end end end

DIGRAPHS 140

Communication Networks Computer networks can be modelled by digraphs in which the vertices (or nodes) represent computing devices, and the arcs are communication links. Each arc is assigned a weight which represents the capacity of the link for communication purposes. For example, the following represents a simple 7-node network

1 2 3

4 5 6

7

5

5

5

3

3

3

3

3

2

2

2

2

2

4

4

4

4

1

Once a network has been installed the problem arises as to how messages can be routed between non-adjacent nodes. A static routing procedure uses information about link capacities to determine a set of fixed routes between nodes. An algorithm such as Dijkstra’s is used to find the optimal paths through the network. This approach is vulnerable to failure in links or network nodes, and delays can occur when the capacity of a link is exceeded. A dynamic routing procedure continually updates link capacities in the light of changing demands. A protocol, or set of rules, is devised to enable individual nodes to decide when and where to transmit new information. Each node maintains its own set of routing tables and so routing calculations are distributed throughout the network. For the network above, each node runs Dijkstra’s algorithm to determine the best paths to other nodes and stores this information in a tree whose root is the ‘home node’.

DIGRAPHS 141

For example, for node 1 we obtain the following tree:

1

2

5

6 7

3

4

5

3

2

1

4 5

To forward messages each node maintains a table that indicates the neighbour to which to pass a message for each destination. For node 1 this table is as follows:

Destination 2 3 4 5 6 7 Next node 2 2 4 4 4 4

EXAMPLE 11.4 Use Dijkstra’s algorithm to find the shortest paths from node 2 to all the other nodes in the network above. Hence, draw the tree of shortest paths and construct the routing table for node 2. Solution

Step

Marked vertex

Distance to vertex 1 2 3 4 5 6 7

Unmarked vertices

0 2 4 0 3 3 ∞ ∞ ∞ 1,3,4,5,6,7 1 2 3 4 5 6

DIGRAPHS 142

The tree of shortest paths and the corresponding routing table are as follows:

Destination 1 3 4 5 6 7 Next node

DIGRAPHS 143

EXERCISES 11 - DIGRAPHS 11.1 Draw the digraph with vertices {1, 2, 3, 4, 5, 6} whose adjacency matrix is:

T F F T F F T F T F F T F T T F T F T F F T T T F F F T F T F T T T T F

L

N

M M M M M M M

O

Q

P P P P P P P

Suppose that each arc has weight 1. Find by inspection (if they exist) (a) the shortest path(s) from 1 to 2, (b) the shortest path(s) from 3 to 6, (c) a cycle of length five. 11.2 Apply the Topological Sort algorithm to the digraph whose adjacency matrix

is: a b c d e f

a b c d e f

F F F T F F T F F F F T F T F F T F F F F F F T F F F T F T F F F F F F

L

N

M M M M M M M

O

Q

P P P P P P P

Rewrite the matrix with the rows and columns ordered by the new labels. What

pattern can you see in the new version? What would happen if you applied the Topological Sort algorithm to the

digraph in Exercise 11.1?

DIGRAPHS 144

11.3 Given the following task chart, find a total ordering in which the tasks can be performed sequentially.

Task Prerequisite Tasks

A Chop onions I B Wash lettuce K C Make dressing K D Do stir fry J E Toss salad B, C F Cut up chicken None G Grate ginger I H Chop bok choy I I Marinate chicken F J Heat wok A, G, H, K

K Prepare rice None 11.4 A digraph G has adjacency matrix

M =

F T F F F F T F F F F T T F F F

L

N

M M M M

O

Q

P P P P

Calculate M 2, M 3and M 4. Hence, find the reachability matrix M*. 11.5. Use Dijkstra’s algorithm to find the shortest distance to each vertex (a) from vertex A , (b) from vertex C.

B 4 E

3 12 2 6

A 11 D 8 F 10 5

C

11.6 This question concerns the 7-node network given on page 220.

(a) If the delay from node 2 to node 4 decreases from 3 to 1, determine a shortest path tree and routing table for node 2.

(b) What happens to the shortest path tree and routing table for node 2 if the

links between nodes 5 and 6 are removed?

DIGRAPHS 145

SOLUTIONS TO EXERCISES 11 – DIGRAPHS 11.1 (a) The shortest path from 1 to 2 is 1, 4, 6, 2. (b) The shortest paths from 3 to 6 are 3, 5, 6 or 3, 2, 6 (c) For example, the cycle 1, 4, 6, 3, 2, 1 is a cycle of length five. 11.2 (a) The initial antecedent sets are A(a) = {b}, A(b) = {c}, A(c) = Ø, A(d) = {a, e}, A(e) = {c}

and A(f) = {b, d, e}. So label c(1) and remove c and associated edges. Then A(a) = {b}, A(b) = Ø, A(d) = {a, e}, A(e) = Ø and A(f) = {b, d, e}. So, for instance, label b(2) and remove b and associated edges. Then A(a) = Ø, A(d) = {a, e}, A(e) = Ø and A(f) = {d, e}. So, for instance, label a(3) and remove a and associated edges. Then A(d) = {e}, A(e) = Ø and A(f) = {d, e}. So label e(4) and remove e and associated edges. Then A(d) = Ø and A(f) = {d}. So label d(5) and remove d and associated edges. Finally, A(f) = Ø. So label f(6). This gives the consistent labelling, c(1), b(2), a(3), e(4), d(5) and f(6). (b) The new adjacency matrix is

c b a e d f c b a e d f

F T F T F F F F T F F T F F F F T F F F F F T T F F F F F T F F F F F F

L

N

M M M M M M M

O

Q

P P P P P P P

The use of the ordering of vertices in a consistent labelling allows us to store just the upper

triangular portion of the adjacency matrix of the digraph. The graph in Exercise 11.1 is such that none of the antecedent sets is empty - hence the

graph contains cycles and so there is no consistent labelling of its vertices. 11.3 The PERT chart is as follows:

A B C D

E F

G H

I J K

DIGRAPHS 146

The initial antecedent sets are A(A) = {I}, A(B) = {K}, A(C) = {K}, A(D) = {J}, A(E) = {B, C}, A(F) = Ø, A(G) = {I}, A(H) = {I}, A(I) = {F}, A(J) = {A, G, H, K} and A(K) = Ø. So label F as 1, K as 2 and remove F, K and associated arcs.

Then A(A) = {I}, A(B) = Ø, A(C) = Ø, A(D) = {J}, A(E) = {B, C}, A(G) = {I}, A(H) = {I}, A(I)

= Ø and A(J) = {A, G, H}. So label B as 3, C as 4, I as 5 and remove B, C and I and associated arcs.

Then A(A) = Ø, A(D) = {J}, A(E) = Ø, A(G) = Ø, A(H) = Ø and A(J) = {A, G, H}. So label A as 6, E as 7, G as 8, H as 9 and remove A, E, G and H and associated arcs. Then A(D) = {J} and A(J) = Ø. Finally label J as 10, remove J and associated arcs, and then

label D as 11. This gives the consistent labelling, F, K, B, C, I, A, E, G, H, J, D. 11.4

M 2 = =

F T F F F F T F F F F T T F F F

L

N

M M M M

O

Q

P P P P

F T F F F F T F F F F T T F F F

L

N

M M M M

O

Q

P P P P

F F T F F F F T T F F F F T F F

L

N

M M M M

O

Q

P P P P

M 3 = =

F T F F F F T F F F F T T F F F

L

N

M M M M

O

Q

P P P P

F F T F F F F T T F F F F T F F

L

N

M M M M

O

Q

P P P P

F F F T T F F F F T F F F F T F

L

N

M M M M

O

Q

P P P P

M 4 = =

F T F F F F T F F F F T T F F F

L

N

M M M M

O

Q

P P P P

F F F T T F F F F T F F F F T F

L

N

M M M M

O

Q

P P P P

T F F F F T F F F F T F F F F T

L

N

M M M M

O

Q

P P P P

Therefore,

M* = M or M 2 or M 3 or M 4 =

T T T T T T T T T T T T T T T T

L

N

M M M M

O

Q

P P P P

DIGRAPHS 147

DIGRAPHS 148

11.5 (a)

Step Marked ver tex

Distance to vertex A B C D E F

Unmarked vertices

0 A 0 3 10 ∞ ∞ ∞ B, C, D, E, F 1 B 0 3 10 15 ∞ ∞ C, D, E, F 2 C 0 3 10 15 ∞ ∞ D, E, F 3 D 0 3 10 15 17 23 E, F 4 E 0 3 10 15 17 23 F 5 F 0 3 10 15 17 23

1 2 shortest distances from A

44444 344444

(b)

Step Marked ver tex

Distance to vertex A B C D E F

Unmarked vertices

0 C ∞ ∞ 0 5 ∞ ∞ A, B, D, E, F 1 D ∞ ∞ 0 5 7 13 A, B, E, F 2 E ∞ 11 0 5 7 13 A, B, F 3 B ∞ 11 0 5 7 13 A, F 4 F ∞ 11 0 5 7 13 A

1 2 shortest distances from C

44444 344444

11.6 (a) Running Dijkstra’s algorithm with the delay on link 24 reset to 1 produces:

Step Marked ver tex

Distance to vertex 1 2 3 4 5 6 7

Unmarked vertices

0 2 4 0 3 1 ∞ ∞ ∞ 1,3,4,5,6,7 1 4 3 0 3 1 2 ∞ ∞ 1,3,5,6,7 2 5 3 0 3 1 2 6 7 1,3,6,7 3 1 3 0 3 1 2 6 7 3,6,7 4 3 3 0 3 1 2 6 7 6,7 5 6 3 0 3 1 2 6 7 7 6 7 3 0 3 1 2 6 7

There are several shortest path trees one of which is:

2

156

7

3 4 23

1

1

5

3

The corresponding routing table is:

Destination 1 3 4 5 6 7 Next node 4 3 4 4 3 4

(b) The link 56 is not used from node 2 and so there is no change to the tree or the routing table

calculated in Example 11.4.

DIGRAPHS 149

DIGRAPHS 150

  • Communication Networks
    • Distance to vertex
      • SOLUTIONS TO EXERCISES 11 – DIGRAPHS

FUNCTIONS.pdf

FUNCTIONS A function is a special type of binary relation. Before defining a function we look at two ways of generating new relations from existing ones. Given a relation R between sets A and B, the INVERSE RELATION R-1 between B and A is given by

R-1 = {(b, a) : (a, b) ∈ R} For example, the inverse of the relation is a parent of on the set of all people is is a child of. In graphical terms, the inverse of a relation is obtained by reversing all the arrows in the digraph of the original relation. Now let R be a relation between sets A and B, and S be a relation between B and a third set C. Suppose that elements a in A, b in B and c in C are such that a is R- related to b and b is S-related to c. Then a is related to c by a new relation that is a combination of the two relations R and S. This new relation between A and C is called the COMPOSITION of R and S and is denoted (rather bizarrely at first sight) by S . Ro Formally,

S Ro = { (a, c) : a R b and b S c for some b∈B } This new relation relates elements of A and C using the elements of B as intermediaries.

A B

a R b a b b S c a ( S Ro ) c c C

FUNCTIONS 81

EXAMPLE 7.1 Let R and S be the relations whose digraphs are below. Find the digraph of the relation . S Ro a • • 1 1 • • x • 2 2 • b • • 3 3 • • y

R S

Solution a R 1 and 1 S y ⇒ ∈ S Ro a R 2 and 2 S x ⇒ ∈ S Ro a R 3 and 3 S x ⇒ ∈ S Ro b R 2 and 2 S x ⇒ ∈ S Ro This gives the following digraph

a • • x b • • y

S Ro

■ EXAMPLE 7.2 If R is the relation is a sister of and S is the relation is the mother of on the set of all people, describe the relations S and S . Ro So Solution

FUNCTIONS 82

Given the logical matrices of two relations we can form the logical matrix of the composition; this is called THE LOGICAL (BOOLEAN) MATRIX PRODUCT. Let R be a relation between sets A = {a1, a2, . . . , an} and B = {b1, b2, . . . , bm}. The logical matrix M representing R is given by

M(i, j) = T if (ai, bj) ∈ R M(i, j) = F if (ai, bj) ∉ R

Let S be a relation between sets B = {b1, b2, . . . , bm} and C = {c1, c2, . . . , cp}. The logical matrix N representing S is given by

N(i, j) = T if (bi, cj) ∈ S N(i, j) = F if (bi, cj) ∉ S

Now suppose that the entry in row i and column k of M is a T and that the entry in row k and column j of N is also a T as shown below.

b1 b2 . . . . bk . . . . c1 c2 . . cj . . . . . . a1 b1 a2 b2 : : ai T :

: bk T : : : : M N Hence, (ai, bk) ∈ R and (bk, cj) ∈ S which means that (ai, cj) ∈ . So the entry in row i and column j of the matrix P representing S is T. Otherwise, there is no T in the ith row of M which matches with a corresponding T in the jth column of N and so the entry in row i and column j of P is F.

S Ro Ro

We write P = MN for the logical matrix product of M and N. Notice that M represents the relation R, N represents the relation S and P = MN represents the relation . S Ro From the above discussion, the entry P(i, j) in the logical matrix P of S o R is given by: P(i, j) = [M(i, 1) and N(1, j)] or [M(i, 2) and N(2, j)] . . . . . . . . . . . . . . . . or [M(i, n) and N(n, j)] This combination of row i of M with column j of N only produces T if there is a T in the ith row of M matched by a T in the same position in the jth column of N.

FUNCTIONS 83

EXAMPLE 7.3 Calculate the logical matrix product P = MN when

M = and N = . T T T F T F

⎡ ⎤ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥⎣ ⎦

F T T F T F

⎡ ⎤ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎣ ⎦

[These are the logical matrices of the relations R and S given in Example 7.1.] Solution

P = ⎢ T T T F T F

⎢ ⎥ ⎢ ⎥ ⎢ ⎥⎣ ⎦

⎤ ⎥

F T T F T F

⎡ ⎤ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎣ ⎦

Since M has two rows and N has two columns P has two rows and two columns. We combine the first row of M with the columns of N in turn to get P(1, 1) and P(1, 2).

Hence, P(1, 1) = = T T T T⎡⎣ ⎦⎤ F T T

⎡ ⎤ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥⎣ ⎦

and P(1, 2) = = T. T T T⎡⎣ ⎦⎤ T F F

⎡ ⎤ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥⎣ ⎦

Then combine the second row of M with each column of N in turn to produce P(2, 1) and P(2, 2).

We get P = .

⎡ ⎤ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥⎣ ⎦

FUNCTIONS 84

A FUNCTION f from a set A to a set B is a binary relation in which every element of A is associated with a uniquely specified element of B. In digraph terms a function is a relation such that precisely one arc leaves every element of A. For example,

a • • 1

b • • 2

c • is a function from {a,b,c} to {1,2}. EXAMPLE 7.4 Which of the following relations are functions? a) x is the sister of y on the set P of people. b) The relation on Z given by {(x, x2) : x ∈ Z}. Solution a) b)

■ Let f be a function from a set A to a set B. Since for each x∈A, there exists a uniquely determined y∈B with (x, y) ∈f, we write y = f(x), and refer to f(x) as the image of x under f. We also write f : A → B to indicate that f is a function which maps each element of A to a uniquely determined element of B. We call A the DOMAIN of f, and B the CODOMAIN of f. The RANGE of f is the set of images of all the elements of A under f. The range is given by

f(A) = {f(x) : x∈A}.

FUNCTIONS 85

f

f(A)

domain A codomain B

When dealing with a function f : A → B where A and B are infinite sets of numbers, it is not possible to draw the digraph representation. However, we can use the more traditional mathematical idea of graphing a function to give a geometric picture of the function. For example, the graph of the function f : R → R given by f(x) = x2 is as below.

y

(2, 4)

x

Using the axes above, sketch the graph the function g : R → R given by g(x) = 2 − x. Let f : A → B be a function. We call f an INJECTIVE (or ONE-TO-ONE) function if f(a1) = f(a2) ⇒ a1 = a2 for all a1,a2 ∈A. Since this is logically equivalent to the statement a1 ≠ a2 ⇒ f(a1) ≠ f(a2), an injective function never repeats values. In other words, different inputs give different outputs. We call f a SURJECTIVE (or ONTO) function if the range of f coincides with the codomain of f. This means that for every b∈B there exists an a∈A with b = f(a). In other words, each element in the codomain is a value of the function. Finally, we call f a BIJECTIVE function if f is both injective and surjective.

FUNCTIONS 86

EXAMPLE 7.5 Decide which of the following functions are injective, surjective or bijective. 1 2

a • • 1

b • • 2

c • • 3

a • • 1

b • • 2

c • • 3

injective yes/no injective yes/no surjective yes/no surjective yes/no bijective yes/no bijective yes/no 3 4

a • • 1

b • • 2

c •

a • • 1

b • • 2

• 3

injective yes/no injective yes/no surjective yes/no surjective yes/no bijective yes/no bijective yes/no

FUNCTIONS 87

EXAMPLE 7.6 Show that the function h : Z → Z given by h(x) = x2 is neither injective nor surjective. Solution To show that h is not injective, it is sufficient to find integers x1 and x2 such that x1 ≠ x2 but h(x1) = h(x2). To show that h is not surjective, it is sufficient to find an integer in the codomain that is not a value of h.

■ EXAMPLE 7.7 Show that the function k : R → R given by k(x) = 4x + 3 is bijective. Solution Suppose that k(x1) = k(x2). Therefore k is injective. Let b∈R, the codomain of k. Therefore k is surjective. Since k is both injective and surjective, it is bijective.

FUNCTIONS 88

Since any function f : A → B is a relation we can form the inverse relation f −1. If this inverse relation is itself a function, we say that f is an invertible function and write f -1 : B → A for the INVERSE FUNCTION. EXAMPLE 7.8 Which of the functions depicted in Example 7.5 is invertible? Solution The inverse relations are given by simply reversing the arrows in the graphical representations. Only number 2 produces an inverse function.

■ An adaptation of this idea of reversing arrows to decide whether or not a function is invertible can be used for more complicated examples. For example, consider the function k : R → R given by k(x) = 4x + 3. The effect of k can be broken down into steps.

x 4x 4x + 3 multiply by 4

add 3

Reversing the arrows reveals the inverse function.

¼(x−3) x−3 x divide by 4

subtract 3

Alternatively, an algebraic approach can be used. Let y = k(x), so that x = k −1(y). Since y = 4x + 3, a little algebra shows that x = ¼(y - 3) Therefore k −1(y) = ¼(y − 3) or, since we usually use x as an input label, k -1(x) = ¼(x - 3). The question arises as to which functions are invertible. The answer is given by the following theoretical result. THEOREM

A function f is invertible if and only if f is a bijective function. [For a proof see p.88 of Haggarty.]

FUNCTIONS 89

Composition of functions is much easier to handle than the composition of relations. If f : A → B and g: B → C are functions, then the composite relation g is a function from A to C.

fo

Moreover, : A → C is given by ( )(x) = g ( f (x)). g fo g fo EXAMPLE 7.9 Consider the function f : R → R given by f(x) = x2 and the function g : R → R given by g(x) = 4x+3. Find ,g fo f go and . g go Solution ( )(x) = g ( f(x)) = g fo ( f go )(x) = f ( g(x)) = ( )(x) = g ( g(x)) = g go

FUNCTIONS 90

FUNCTIONS 91

EXERCISES 7 - FUNCTIONS

7.1 Let R be the relation between {1, 2, 3} and {1, 2, 3, 4} given by R = {(1, 1), (2, 3), (2, 4), (3, 1), (3, 4)}

Let S be the relation between {1, 2, 3, 4} and {1, 2} given by

S = {(1, 1), (1, 2), (2, 1), (3, 1), (4, 2)} Calculate R -1, S -1 and S o R . 7.2 Let R denote the relation is a parent of and S denote the relation is a brother of

on the set of all people. Give concise verbal descriptions of the relations R −1 , S −1 , R o S , S −1 o R −1 and R o R .

7.3 Relations R and S are represented by the matrices M and N respectively where

M = and N = T F F T F T

⎡ ⎢ ⎣ ⎦

⎤ ⎥

T F T T T T F F F T T T

L

N M M M

O

Q P P P

Calculate the logical Boolean product MN. What relation is represented by

MN? 7.4 Let A = {0, 2, 4, 6} and B = {1, 3, 5, 7}. Determine which of the following

relations between A and B forms a function with domain A and codomain B. (a) {(6, 3), (2, 1), (0, 3), (4, 5)} (b) {(2, 3), (4, 7), (0, 1), (6, 5)} (c) {(2, 1), (4, 5), (6, 3)} (d) {(6, 1), (0, 3), (4, 1), (0, 7), (2, 5)} For those which are functions, which are injective and which are surjective? 7.5 Sketch the graphs of each of the following functions. Hence, state which are

injective and which are surjective? (a) f : Z → N given by f(x) = x2 + 1 (b) g : N → N given by g(x) = 2x (c) h : R → R given by h(x) = 5x − 1

(d) j : R → R given by j(x) = 2 3 if x 1

+ 1 if < 1 x x x

− ≥⎧ ⎨ ⎩

FUNCTIONS 92

7.6 The floor function assigns to a real number x the largest integer that is less than or equal to x. This is denoted by x .

(a) If S = {−1, 0, 1, 2} then find f(S) when f(x) = 2 1 3

x⎢ ⎥+ ⎢ ⎥ ⎣ ⎦

(b) Determine whether the function g : Z→Z given by g(n) = 2 n⎢ ⎥

⎢ ⎥⎣ ⎦ is

injective or surjective (or both).

7.7 The function f : A → B is given by f x x

( ) = +1 2

where A denotes the set of

real numbers excluding 0 and B denotes the set of real numbers excluding 1. Show that f is injective. Given that f is also surjective, determine the inverse function. 7.8 Functions f : R → R and g : R → R are given by

f(x) = x2 and g x x xx x( ) = + ≥

− < RST 2 1

0 if if

0

Find formulae for f o g and g o f.

SOLUTIONS TO EXERCISES 7 - FUNCTIONS 7.1 = {(1, 1), (1, 3), (3, 2), (4, 2), (4, 3)}. 1R −

= {(1, 1), (1, 2), (1, 3), (2, 1), (2, 4)}. 1S −

SoR = {(1, 1), (1, 2), (2, 1), (2, 2), (3, 1), (3, 2)}. 7.2 is the relation is a child of. 1R −

is the relation is a sibling of (i.e. is the brother or sister of). 1S −

R o S is the relation is an uncle of. o is the relation is a nephew or niece of. 1S − 1R −

R o R is the relation is a grandparent of.

7.3 MN = T F F T F T

T F T T T T F F F T T T

F T T T T T T

L NM O QP L

N M M M

O

Q P P P L NM

O QP

= T

.

MN represents the relation SoR. 7.4 (a) and (b) are functions. (c) is not a function since no element of B is associated with 0 ∈ A. (d) is not a function because there is not a unique element of B is associated with 0 ∈ A. (a) is neither one-to-one [two different elements of A are associated with 3 ∈ B] nor onto [no

element of A is associated with 7 ∈ B] (b) is both one-to-one and onto. 7.5

10

8

5 4

2 2

1

−3 −2 −1 0 1 2 3 1 2 3

f : Z → N g : N → N f(x) = x2 + 1 g(x) = 2

x

FUNCTIONS 93

2

9

1

−2 −1 1 2 −2 −1 1 2

−6 −1

h : R → R j : R → R

h(x) = 5x − 1 j(x) = 2 3 1

1 < 1

x if x

x if x

− ≥

+

⎧ ⎨ ⎩

(a) Since f(−1) = f(1) = 2, f is not injective. Since the equation f(x) = 3 has no integer solution, f is not surjective. (b) If g(a) = g(b) then 2a = 2b and so a = b. Hence, g is injective. Since the equation g(x) = 3 has no integer solution, g is not surjective. (c) If h(a) = h(b) then 5a − 1 = 5b − 1 and so a = b. Hence, h is injective.

If h(x) = y then x = 1

5 (y + 1) and so h is surjective.

(d) Since j(−1) = j( 32 ) = 0, j is not injective. The range and codomain of j coincide and so j is surjective.

7.6 (a) f(−1) = 2 1 2( 1)

3 3

+− =

⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥⎣ ⎦⎣ ⎦

= 0

Similarly, f(0) = 0, f(1) = 0 and f(2) = 1. Hence, f(S) = {0, 1}. (b) g is not injective since for example, g(0) = g(1) = 0.

g is surjective since g(2n) = 2

2

n⎢ ⎥ ⎢ ⎥⎣ ⎦

= n.

7.7 If f(a) = f(b) then 2 2

1 1 a b

+ = + and so a = b. Therefore, f is injective.

If f(x) = y then 2

1 y x

+ = and so 2

1 x

y =

− . Therefore, f −1(x) =

2

1x − .

7.8 (f o g )(x) = f(g(x)) = 2

2

(2 1) if 0

if 0

x x

x x

+ ≥

<

⎧ ⎨ ⎩

.

(g o f )(x) = g(f(x)) = 2x2 + 1.

FUNCTIONS 94

  • EXAMPLE 7.1

GRAPH.pdf

GRAPHS Graphs originated in the 18th century when the mathematician, Leonhard Euler, tried to solve the Königsberg Bridge Problem. At that time the city of Königsberg consisted of two islands in a river linked by seven bridges. The problem was to find a route beginning and ending at the same place which crossed each bridge exactly once.

C g

c d

Kneiphof A e D

a b

Königsberg B f

The problem can be modelled by a GRAPH which consists of a set of VERTICES and a set of EDGES connecting these vertices as shown below.

C

c d g

A e D

a b f

B An EULERIAN TRAIL through such a graph is a route beginning and ending at the same vertex using all the edges once. Euler proved that in a graph in which every pair of vertices is joined by at least one path, there is an Eulerian trail if and only if all the vertices have even degree. Such a graph is called an EULERIAN GRAPH. The DEGREE of a vertex, denoted by δ(v), is the number of edges incident to v.

GRAPHS 115

GRAPHS 116

There is no such path in the graph representing the Königsberg Bridge Problem since δ(B) = δ(C) = δ(D) = 3 and δ(A) = 5. In other words, there are vertices of odd degree. A SIMPLE GRAPH G = (V, E) consists of a finite set of vertices V and a finite set of edges E, such that G contains no loops (no vertex is joined to itself by an edge) and no multiple edges (there is never more than one edge joining any pair of vertices). Two vertices u and v are said to be ADJACENT if they are joined by an edge e. The edge e is said to be INCIDENT to u (and v). Therefore, we can regard the set E of edges as a set of pairs of adjacent vertices and so E defines a symmetric relation on V. We shall write uv to denote the unique edge joining vertices u and v in a simple graph. The logical matrix M of the edge relation E is called the ADJACENCY MATRIX; M is symmetric about its leading diagonal. EXAMPLE 10.1 Write down the adjacency matrix of the following simple graph

b c

a

e d

Solution

a b c d a b c d e

L

N

M M M M M M M

O

Q

P P P P P P P

e

A SUBGRAPH of a graph G = (V, E) is any graph G' = (V', E') such that E' ⊆ E and V' ⊆ V. EXAMPLE 10.2 Consider the following graphs

G H

K L

Which of H, K or L form subgraphs of G? Solution

■ A PATH of length k in a graph is a sequence of distinct vertices v0, v1, . . . , vk such that vi−1vi is an edge for i = 1 to k. A CYCLE of length k in a graph is a sequence of vertices v0, v1, . . . , vk such that vi−1vi is an edge for i = 1 to k, the first and last vertices are the same and no other vertices (or edges) are repeated.

GRAPHS 117

EXAMPLE 10.3 Find all the cycles of length four in the graph in Example 10.1. Solution

A graph containing no cycles is called an ACYCLIC GRAPH. The tree structures that arise in computing are acyclic graphs. A graph G = (V, E) is CONNECTED if there exists a path between every pair of vertices. Otherwise, G can be partitioned into a number of subgraphs each of which is connected; the minimum number of such connected components is the CONNECTIVITY NUMBER , c(G). Questions of connectivity are important in the application of graphs to computer networks. The following algorithm calculates the value of c(G). THE CONNECTIVITY ALGORITHM begin V' := V; c := 0; while V' ≠ Ø do begin Choose y ∈ V'; Find all vertices joined to y by some path; Remove these vertices and y from V'; c := c + 1; end end

GRAPHS 118

EXAMPLE 10.4 Trace the effect of the connectivity algorithm on the following graph:

1 5

2 6

3 7

4 8

Solution V ' c

initial values {1,2,3,4,5,6,7,8} 0

choose y =

choose y =

choose y =

Therefore c(G) =

■ A HAMILTONIAN CYCLE in a graph is a cycle which passes through each vertex exactly once. There is no simple test for Hamiltonian graphs but many graphs are Hamiltonian. For example, if every vertex in a graph is connected to every other vertex there is always a Hamiltonian cycle. Such graphs are called COMPLETE GRAPHS and one with n vertices is denoted by Kn. For instance, the complete graph K5 below has twelve different Hamiltonian cycles.

GRAPHS 119

Finding Hamiltonian cycles (when they exist) in an arbitrary (connected) graph is not always so straightforward. If, in addition, every edge is assigned a weight and we seek a Hamiltonian cycle of least possible total weight, the problem is even harder. This is a classical problem which underlies many practical real world problems. The Travelling Salesperson Problem: A salesperson wishes to visit a number of towns joined by interconnecting roads. Find a route visiting each town exactly once and keeping travel costs to a minimum. In graphical terms a solution to this problem is a Hamiltonian cycle of minimal total weight. Since there is no known efficient algorithm for solving this problem we use algorithms which find suboptimal solutions; a suboptimal solution is not necessarily the best one but it is often shorter than most other Hamiltonian cycles! NEAREST NEIGHBOUR ALGORITHM {This algorithm finds a suboptimal solution to the travelling salesperson problem by generating a Hamiltonian cycle in a weighted graph with vertex set V. The cycle is given by the final value of route and its total weight by the final value of w. } begin Choose v ∈ V; route := v; w := 0; v' := v; Mark v'; while unmarked vertices remain do begin Choose an unmarked vertex u closest to v'; route := route u; w := w + weight of the edge v'u; v' := u; Mark v'; end route := route v; w := w + weight of the edge v'v; end

GRAPHS 120

EXAMPLE 10.5 Apply the nearest neighbour algorithm to the graph below, starting at vertex D.

A 5 B

8 7 6 1 0

C 3 D Solution u route w v' Initially - D 0 D Exit loop An exhaustive search of this small graph reveals two other circuits; ABCDA, of length 23, and ACBDA, of length 31.

■ A graph G = (V, E) is a TREE if G is connected and acyclic [i.e. contains no cycles]. The following statements about a graph G with n vertices and m edges are equivalent: • G is a tree. • There is exactly one path between any two vertices of G. • G is connected and m = n − 1. • G is connected and removal of a single edge disconnects G. • G is acyclic and adding a new edge creates a cycle. Most of the above equivalences are easy to establish. The only characterisation of a tree which is more difficult is the third one [for a proof, see p. 132 of Haggarty]. Any connected graph G contains subgraphs which are trees. A subgraph of G that is a tree and that includes all the vertices of G is called a SPANNING TREE. To construct a spanning tree select edges of G one at a time without creating cycles. Stop when the addition of any other edge would create a cycle.

GRAPHS 121

By the third characterisation of a tree given above, we know that when the graph G contains n vertices this will require us to select precisely n − 1 edges of G to construct a spanning tree. A graph may contain several different spanning trees as the following example demonstrates. EXAMPLE 10.6 Find two different spanning trees for the following graph G.

a b c

d e f

g

Solution

■ The process used in Example 10.6 can be adapted to solve another classical problem, the MINIMUM CONNECTOR PROBLEM. The Minimum Connector Problem: A railway network connecting a number of towns is to be constructed. Given the cost of building a direct link between any two towns, find a network of minimal total cost. In graphical terms we require a spanning tree for a graph with weighted edges of minimal total weight. Such a tree is called a MINIMAL SPANNING TREE or MST for short. Unlike the travelling salesperson problem, there is an algorithm for solving this problem which produces an optimal solution. This is similar to Prim’s algorithm that we used in week 1 to solve the minimum connector problem for a collection of six Scottish towns.

GRAPHS 122

MINIMAL SPANNING TREE ALGORITHM {Let G = (V, E) be a connected graph with weighted edges. The algorithm obtains a MST for G by successively selecting edges of least possible weight to construct a spanning tree. The MST is stored as a set T of edges.} begin e := an edge of G of smallest weight; T := {e}; E' := E − {e} while E' ≠ Ø begin e' := a smallest weight edge in E'; T := T ∪ {e'}; E' := the set of edges in E' − T whose addition to T does not create cycles; end end EXAMPLE 10.7 The table below gives the distances (in miles) between five towns A, B, C, D and E. Find a minimum connector.

A B C D E A - 13 3 9 9 B 13 - 11 11 13 C 3 11 - 9 7 D 9 11 9 - 2 E 9 13 7 2 -

Solution

A •

B • • C

D • • E

GRAPHS 123

A ROOTED TREE is one in which a vertex is distinguished and called the ROOT. Rooted trees are usually drawn with the root at the top and with the remaining vertices arranged in levels below. Vertices adjacent to a vertex v in a rooted tree and immediately below v are called the CHILDREN of v. A BINARY ROOTED TREE has the property that each vertex v has at most two children. These children themselves form the roots of binary rooted trees referred to as the LEFT-SUBTREE and RIGHT-SUBTREE of v.

← ROOT

v

← RIGHT-SUBTREE of v

Ordered data, such as a set of strings of characters ordered lexicographically can be stored as the vertices of a binary rooted tree in a manner which respects the ordering being applied. This is achieved by ensuring that each data item in the left-subtree of any vertex v is less than the data item stored at v and each data item in the right-subtree of any vertex v is greater than the data item stored at v. The resulting tree is called a BINARY SEARCH TREE. The advantage of storing ordered data in a binary search tree is that it is then possible to devise efficient algorithms for searching for particular data and inserting new data. The Search algorithm determines whether or not a given item (the search key) is a vertex in the given binary search tree by comparing the search key with the root of the tree and then, if necessary, recursively searching the left- and right-subtrees. The Insert algorithm inserts an item (the insertion key) into a binary search tree by creating a new vertex on the right or left of an existing vertex so that the vertex keys in the resulting binary search tree still respect the given ordering of keys.

GRAPHS 124

Search(tree) begin if tree is null then search := false;

else if search key = root key then

search := true; else if search key < root key then search := search(left-subtree); else search := search(right-subtree); end PROBLEM 10.8 Trace the Search algorithm on the tree below when the search key is R and vertex keys are ordered alphabetically.

K

C T

M V Solution Since R > K the search moves to the right-subtree of K. Then, since R < T the search moves to the left-subtree of T. Finally, since R≠M and the left- and right-subtrees of M are both null the algorithm terminates and the item is not found.

GRAPHS 125

Insert(item, tree) begin if tree is null then add new item; else if insertion key = root key then print “the item is already in the tree”; else if insertion key < root key then insert := insert(item, left-subtree); else insert := insert(item, right-subtree); end EXAMPLE 10.9 Trace the Insert algorithm on the tree in Example 10.8 where the items to be inserted successively are R, A and L. Draw the final binary search tree. Solution R > K so apply insert to the right-subtree of K. R < T so apply insert to the left-subtree of T. R > M and the right-subtree of M is null so write R on the right of M to obtain the tree below.

K

C T

M V

R

Now insert A and then L as shown above.

GRAPHS 126

EXERCISES 10 - GRAPHS 10.1 Explain why the sum of all the vertex degrees in a simple graph G is equal to

twice the number of edges.[This is called the handshaking lemma for simple graphs.]

Use the handshaking lemma to show that the complete graph Kn has

½ n(n−1) edges. For what values of n is Kn an Eulerian graph? 10.2 Draw the graph G whose adjacency matrix is

F T F T F T T F T F T F F T F T F T T F T F T F F T F T F F T F T F F F

L

N

M M M M M M M

O

Q

P P P P P P P

Describe the adjacency matrix of the complete graph Kn. 10.3 Which of the following could form subgraphs of the graph G in Exercise

10.2?

10.4 Find a Hamiltonian cycle in the following graph.

2 3 4

8

1

6 7 5

Find cycles of length 3, 4, 5, 6 and 7.

GRAPHS 127

10.5 Apply the Nearest Neighbour Algorithm to find Hamiltonian cycles in the weighted graph below, starting at (a) the vertex A, and (b) the vertex D.

A

C

D E2

B

5

8

3

5

2

5 4

4 3

10.6 Determine whether each of the following adjacency matrices represents a

tree.

(a)

F F F F F T F F F T F T F F F F T T F T F F T F F F T T F F T T T F F F

(b)

F F F F F T F F T F F F F T F T F T F F T F T F F F F T F F T F T F F F

L

N

M M M M M M M

O

Q

P P P P P P P

L

N

M M M M M M M

O

Q

P P P P P P P

10.7 A tree T has three vertices of degree 3 and four vertices of degree 2. The

remaining vertices all have degree one. How many vertices of degree one are there? [Hint: let T have n vertices and apply the handshaking lemma.]

10.8 Find a minimal spanning tree for the graph below.

A

3 13 5

B 12 2

C 10 D E 4 F 9 6

G 11 7

8

H

GRAPHS 128

10.9 The following table gives the distances (in miles) between six places in Ireland.

Athlone Dublin Galway Limerick Sligo Wexford

Athlone - 78 56 73 71 114 Dublin 78 - 132 121 135 96

Galway 56 132 - 64 85 154 Limerick 73 121 64 - 144 116

Sligo 71 135 85 144 - 185 Wexford 114 96 154 116 185 -

Use the minimal spanning tree algorithm to find a road network of minimal

total length connecting all six places. 10.10 The In-order traversal algorithm prints all the items in a binary search tree

in order. The algorithm proceeds as follows: for each vertex, beginning with the root, print all the items in the left-subtree, then print the vertex, then print all the elements in the right-subtree.

In-order traversal(tree) begin if tree is null then do nothing; else begin in-order traversal(left-subtree); print root key; in-order traversal(right-subtree); end end

Apply the algorithm to the following tree:

A

L

K

C T

M V

R

GRAPHS 129

SOLUTIONS TO EXERCISES 10 - GRAPHS 10.1 Every edge in G joins two vertices and hence contributes one to the degree of each of these two

vertices. Hence the sum of the degrees of all the vertices counts every edge twice. Therefore, the sum of all the vertex degrees is equal to twice the number of edges in a simple graph.

Every vertex is joined to every other vertex in Kn and so each of the n vertices has degree n − 1.

Hence, the sum of all the vertex degrees is n(n − 1). By the handshaking lemma, Kn has ½ n(n−1) edges.

All the vertices of Kn have the same degree and so, for n ≥ 3, Kn is Eulerian if and only if n is

odd (since then all vertices have even degree). 10.2

2 3

1 4

6 5

The adjacency matrix of Kn has n rows and n columns with Ts in every position (except on the main diagonal).

10.3 Two of the given graphs are subgraphs as shown below.

2 3 1 6

2 3

1 4 5 4

10.4 One Hamiltonian cycle is 1, 2, 6, 8, 7, 5, 4, 3, 1 There are numerous cycles of lengths 3, 4, 5 and 6. You need one example of each. Cycles of length 3 are: 2862, 2872, 4784 and 4574. Cycles of length 4 are: 27862, 28472 and 45784. Cycles of length 5 are: 127431, 128431, 268472 and 275482. Cycles of length 6 are: 1275431, 1287431, 1278431 and 1268431. Cycles of length 7 are: 13457821 and 12687431. 10.5 (a) The algorithm generates the path A, C, B, D, E, A of total length 4 + 2 + 3 + 2 + 8 = 19. (b) The algorithm generates the path D, E, B, C, A, D of total length 2 + 4 + 2 + 4 + 5 = 17. 10.6 (a) is not a tree since it contains cycles, e.g. 2, 4, 5, 3, 6, 2 (b) is a tree since it is connected with 6 vertices and 5 edges 10.7 The sum of the vertex degrees is 3 + 3 + 3 + 2 + 2 + 2 + 2 + (n − 7) = n + 10. Since T is a tree it has n − 1 edges. Hence, by the handshaking lemma, n + 10 = 2(n − 1). Therefore, n = 12 and so there are 5 leaves.

GRAPHS 130

10.8 Successively choose edges of minimum weight, without creating cycles, until all vertices are connected. Edges are chosen as follows, BE, AB, EF, EG, FH, DG and CD. The resulting MST is as in Figure 1 below.

A

3

B 2

C 10 D E 4 F

9 6

G 7

H

A G

S L

W D

Figure 1 Figure 2 10.9 Let A = Athlone, D = Dublin and so on. The minimal spanning tree selects AG (weight 56), GL

(weight 64) and AS (weight 71). It rejects AL (weight 73) since that would create a cycle. It then selects AD (weight 78). Then GS (weight 85) is rejected to avoid a cycle. Finally DW (weight 96) is selected. This gives the MST in Figure 2 above (of total weight 365).

10.10 Applying the algorithm to the specified tree gives the alphabetic listing A, C, K, L, M, R, T, V.

This corresponds to travelling anticlockwise around the tree, printing items as you pass under a vertex.

A

L

K

C T

M V

R

GRAPHS 131

GRAPHS 132

  • Solution
    • Solution
  • Solution
    • Solution

INTRO.pdf

INTRODUCTION 1

INTRODUCTION Discrete mathematics and mathematical logic lie at the heart of any modern study of computer science. The sort of mathematics that arises when an understanding is sought of how computers operate, either in terms of hardware or software, is covered in this module. Therefore, our fundamental aim in studying discrete mathematics is to acquire the tools and techniques required to design and understand computer systems. When, and how, to use these tools and techniques forms a methodology known as mathematical modelling. This process may be represented diagrammatically as follows:

Problem Solution

⇓ ⇑ Abstract ⇒ Transformed model model

As an example of this process, consider the following problem: The distance (in miles) between six Scottish towns, Aberdeen, Edinburgh, Fort William, Glasgow, Inverness and Perth, are given in the table below.

Aberdeen Edinburgh Fort William Glasgow Inverness Perth Aberdeen - 120 147 142 107 81

Edinburgh 120 - 132 42 157 45 Fort William 147 132 - 108 66 105

Glasgow 142 42 108 - 168 61 Inverness 107 157 66 168 - 112

Perth 81 45 105 61 112 - Find a road network of minimal total length connecting all six towns.

INTRODUCTION 2

The table itself is an abstract model of the real-world problem. For our solution, however, we transform this into a geometric model. We draw a GRAPH whose VERTICES denote the towns, and whose EDGES represent the connecting roads. In this abstract model, each edge is assigned a WEIGHT equal to the distance between the connected towns as given in the original table.

Aberdeen

81 120

107 147 Perth 45 Edinburgh

105 157 112 132

142

Inverness 66 Fort William 61 42

168 108

Glasgow We construct a new graph which connects all six towns which has minimal total weight using PRIM'S ALGORITHM Step 1 Select any vertex and connect it to its nearest neighbour. Step 2 Find an unconnected vertex that is closest to previously connected vertices, and connect it. Step 3 Repeat Step 2 until all vertices are connected. We obtain the following (beginning at the vertex Perth):

Aberdeen

81 120

107 147 Perth 45 Edinburgh

105 157 112 132

142

Inverness 66 Fort William 61 42

168 108

Glasgow The total weight of this minimal connecting network is 339.

INTRODUCTION 3

To avoid ambiguities which may arise in using a natural language like English to describe algorithms a more structured approach can be taken. A PSEUDOCODE consists of a small number of structured language elements together with English-like descriptions of the actions of the algorithms being implemented. We shall use a pseudocode, based on Pascal, in which algorithms take the form

begin statements to perform operations statements to control order of execution end

The building blocks of an algorithmic language are assignment and control statements. An ASSIGNMENT STATEMENT assigns a value to a variable and has the general form

variable name := expression EXAMPLE 1.1 {Algorithm to add two numbers, First and Second, and assign the result to Sum} begin Input First and Second; Sum := First + Second; end

■ CONTROL STATEMENTS are of three main types

• compound statements • conditional statements • iterative statements

A COMPOUND STATEMENT is a list of statements to be executed as a single unit, in the order given, and takes the form

begin statement 1; statement 2; . . . . . . statement n; end

EXAMPLE 1.2

INTRODUCTION 4

{Algorithm to interchange the values assigned to two variables One and Two} begin Input One and Two; Temp := One; One := Two; Two := Temp; end To see how this works, suppose that One and Two are given the values 5 and 7. The algorithm proceeds as follows:

Temp One Two Line 1 Line 2 Line 3 Line 4

A CONDITIONAL STATEMENT allows a choice between two alternatives and is of the form if-then or if-then-else.

begin if condition then statement end

or

begin if condition then statement 1 else statement 2 end

EXAMPLE 1.3 {Algorithm to compute the absolute value of n and store the result in abs} begin Input n; if n < 0 then abs := −n else abs := n; Output abs; end

INTRODUCTION 5

An alternative algorithm which avoids the use of an else clause is begin Input n; if n < 0 then n := −n; abs := n; Output abs; end

ITERATIVE STATEMENTS or LOOPS can take a number of forms. The for-do loop

for variable := initial value to final value do statement

The loop is executed a specified number of times. A variation of the for-do loop is

for all elements of a set do statement

The while loop This is given by

while expression do statement The loop is executed an indeterminate number of times for as long as the expression remains true. Once it becomes false, the loop is exited.

The repeat-until loop This is given by

repeat statement 1; statement 2; . . . . . . statement n; until condition

The loop is also executed an indeterminate number of times until the final condition becomes false. The only difference between a while loop and a repeat-until loop is that in the latter, the loop is executed at least once since the condition is checked after each pass of the loop.

INTRODUCTION 6

EXAMPLE 1.4 {Algorithm to compute the sum of the squares of the first n positive integers} begin sum := 0; for i := 1 to n do begin j := i * i; sum := sum + j; end; Output sum; end We trace this algorithm in the case n = 4.

i j sum Initially Loop 1 Loop 2 Loop 3 Loop 4

■ Finally we give the pseudocode version of Prim’s algorithm seen earlier. EXAMPLE 1.5 {Algorithm to build a graph of minimal total weight connecting the vertices of a given weighted graph} begin v := any vertex; u := nearest adjacent vertex; Connect v and u; while unconnected vertices remain do begin u := an unconnected vertex nearest a connected vertex; Connect u to the nearest connected vertex; end end

INTRODUCTION 7

EXERCISES 1 - ALGORITHMS 1.1 The graph below represents a road network connecting a set of seven villages

where distances are given in miles. Use Prim’s algorithm to find a road network of minimal total length connecting all the villages.

B 3 C

3 3 6 1 G

A 2 D

2 3 3 5

F 4 E 1.2 Find the output from the following algorithm when (a) n = 3, (b) n = 5. begin f := 1; Input n; for i := 1 to n do f := f * i; Output f; end What is the output for a general positive integer input n? 1.3 Trace the values of i and j in the following algorithm when m = 3 and n = 4.

begin Input m, n; i := 1; j := m; while i ≠ n do begin i := i + 1; j := j ∗ m; end Output j; end

Describe, in words, the output for general integers m and n when n > 0. What happens if n = 0?

INTRODUCTION 8

1.4 Trace the values of l, sum and k in the following algorithm when n = 6. begin Input n; k := 1; l := 0; sum := 0; while k < 2n do begin l := l + k; sum := sum + l; k := k + 2; end Output sum; end Describe, in words, the output for a general positive integer input n. 1.5 Trace the following algorithm for the network shown in Exercise 1.1. What is its effect? begin Order the edges according to decreasing weight and number them 1, 2,

3 . . . and so on; m := number of vertices; remaining := number of edges; current := 1; while remaining > m − 1 do begin if removing current edge does not disconnect the graph then begin delete current edge; remaining := remaining − 1; end current := current + 1; end end

INTRODUCTION 9

SOLUTIONS TO EXERCISES 1 - ALGORITHMS 1.1 One possible network of minimal total length, starting with vertex B is

B C

3 3 1

G A 2 D

2 3

F E The total length of any minimal network is 14. 1.2 (a) f = 6 , (b) f = 120 For a general positive integer input n the output is f = n × (n − 1) × . . . × 1. This quantity is

called n factorial and is usually written as n! Hence, for example, 3! = 3 × 2 × 1 = 6. 1.3

i j i ≠ 4? 1 3 yes 2 9 yes 3 27 yes 4 81 no

For n > 0, the algorithm computes . nm If n = 0, the algorithm is invalid and will loop forever will since the while condition holds for

all values of i. 1.4

l sum k k < 12? 0 0 1 yes 1 1 3 yes 4 5 5 yes 9 14 7 yes

16 30 9 yes 25 55 11 yes 36 91 13 no

The output is the sum of the squares of the first n positive integers.

INTRODUCTION 10

1.5 current edge weight

1 BG 6 2 DE 5 3 EF 4 4 AB 3 5 BC 3 6 CD 3 7 EG 3 8 FG 3 9 AF 2

10 CE 2 11 CG 1

There is a choice in the ordering of edges of equal weight. Initially r = 11 and m = 7. The

loop is executed five times and the edges BG, DE, EF, AB and EG are removed to leave the minimal network

B 3 C

3 1

G A 2 D

2 3

F E

  • Aberdeen

LOGIC.pdf

LOGIC A PROPOSITION is a statement which can be assigned a truth value; either true (labelled T) or false (labelled F). For example:

• the earth is flat ←P • Sarah is a doctor ←Q • 29 is a prime number ←R

New propositions can be constructed from others by using logical operators such as not, or and and to produce a COMPOUND PROPOSITION. For example,

• (not P) is the proposition the earth is not flat, • (PorQ) is the proposition the earth is flat or Sarah is a doctor • (PandQ) is the proposition the earth is flat and Sarah is a doctor.

EXAMPLE 2.1 Let P be the proposition logic is fun and Q be the proposition today is Friday. Express each of the following in logical symbols: a) Logic is not fun and today is Friday. b) Today is not Friday, nor is logic fun. c) Either logic is fun or it is Friday. Solution

LOGIC 11

If P denotes a proposition, its NEGATION is the proposition (notP). Its truth value is the opposite of Ps.

P (notP) T F F T

If P and Q are two propositions, their CONJUNCTION is the proposition (PandQ). This is true only when P and Q are both true.

P Q (PandQ) T T T T F F F T F F F F

If P and Q are two propositions, their DISJUNCTION is the proposition (PorQ). This is true when at least one of P and Q is true.

P Q (PorQ) T T T T F T F T T F F F

EXAMPLE 2.2 What is the truth value of either the moon is made of green cheese and Henry VIII had six wives, or it is not true that the dodo is extinct ? Solution

■ Two propositions can be built up from the same basic propositions in different ways and yet have the same truth value. Such propositions are said to be LOGICALLY EQUIVALENT.

LOGIC 12

EXAMPLE 2.3 Show that R = (not(Pand(notQ))) is logically equivalent to S = ((notP)orQ). Solution

P Q notP notQ Pand(notQ) R S T T F F T F F T F T T F F F T T

Since the last two columns are identical, R is logically equivalent to S.

■ A CONDITIONAL PROPOSITION is one such as if it is Saturday then I play golf. This proposition has the form if P then Q where P is the proposition it is Saturday and Q is the proposition I play golf. We call P the PREMISE and Q the CONCLUSION. If I do play golf on a Saturday the proposition if P then Q is true but if I do not play golf on a Saturday the proposition is false. However, if it is not Saturday then whether I play golf or not tells us nothing about the truth value of if P then Q. In general the truth value of if P then Q depends on the truth value of the conclusion Q when the premise P is true but not when P is false. We therefore define if P then Q to be false when P is true and Q is false. In all other cases, it is defined to be true. Therefore, the truth table for if P then Q, written symbolically as (P⇒Q), is as follows:

P Q (P⇒Q) T T T T F F F T T F F T

EXAMPLE 2.4 Show that (P⇒Q) is logically equivalent to ((notQ)⇒(notP)). [(notQ)⇒(notP) is called the CONTRAPOSITIVE of (P⇒Q).]

LOGIC 13

Solution

P Q notP notQ (P⇒Q) (notQ)⇒(notP) T T F F T T F F T F F T T F T F F T T T

Since the last two columns are identical, the logical equivalence is established.

■ Consider the statement x is an integer satisfying x = x2. This is true when x = 0 or x = 1. It is false for all other integer values of x. A statement such as this that involves one or more variables and is either true or false depending on the values assigned to the variables is called a PREDICATE. We shall denote a general predicate involving a single variable x as P(x). A predicate involving two variables x and y can be written as P(x, y) and so on. Predicates can be combined using the logical operators we have seen to date to produce more complex predicates whose truth value will depend on the values assigned to the variables involved and the way in which the individual predicates involved have been combined. The resulting logical system can deal with many of the statements that arise in mathematics and computer science and can be further extended by introducing two more operators: the UNIVERSAL QUANTIFIER denoted by the symbol ∀ and the EXISTENTIAL QUANTIFIER denoted by ∃. The proposition ∀x P(x) is read as for all x, P(x). It is true provided the predicate P(x) is true for all possible values of x and false if P(x) is false for at least one value of x. EXAMPLE 2.5 Let P(x) be the predicate x is a real number and x2 ≥ 0. Express the proposition ∀x P(x) in words and determine its truth value. Solution

LOGIC 14

The proposition ∃x P(x) is read as there exists x, P(x). It is true provided the predicate P(x) is true for at least one value of x and false if P(x) is false for all values of x. EXAMPLE 2.6 Let P(x) be the predicate x is an integer and x2 = 16. Express the proposition ∃x P(x) in words and determine its truth value. Solution

■ EXAMPLE 2.7 Let Q(x) be the predicate x is a real number and x2 + 1 = 0. Express the proposition ∃x(Q(x)) in words and determine its truth value. Solution

■ The negation of the proposition ∃x Q(x) in the previous example is not(∃x Q(x)). This is a true proposition which asserts that there is no real number x for which x2+1 = 0. This can be expressed as, for all real numbers x, x2+1 ≠ 0, which is just the proposition ∀x (not Q(x)). For a general predicate P(x) we have the following logical equivalences: not(∃x P(x)) ≡ ∀x (not (P(x)) not(∀x P(x)) ≡ ∃x (not (P(x)) The system of logic we have described forms the basis of the sort of reasoning that is involved in proving mathematical results and in verifying that computer algorithms operate as intended. Both these themes will be taken up in detail next week.

LOGIC 15

To see how logic forms the basis for reasoning, consider the following sequence of propositions (the HYPOTHESES): If I study hard then I get A grades. I study hard. If both these propositions are true can we conclude that I get A grades (the CONCLUSION) is also true? The answer is yes. To see why, let P be the proposition I study hard and Q be the proposition I get A grades. When both P and P⇒Q are true, the truth table for P⇒Q shows that Q must also be true. This is an example of deductive reasoning in which the truth of the hypotheses leads via truth tables to the truth of the conclusion. Our example illustrates one of the many rules of inference namely, that the truth of a proposition Q can be inferred from the truth of the two propositions P and P⇒Q. This particular rule is known by its Greek name of modus ponens. There are numerous such inference rules and a selection is given in the following table.

Rule Hypotheses Conclusion Modus ponens

P , P⇒Q

Q

Modus tollens

P⇒Q , notQ

notP

Addition

P

PorQ

Simplification

PandQ

P

Conjunction

P , Q

PandQ

Hypothetical syllogism

P⇒Q , Q⇒R

P⇒R

Each rule is proved by inspecting the relevant truth tables. Once established each rule allows us to deduce the truth of the conclusion from the truth of the hypotheses. Combining the rules enables us to determine whether or not a given argument is valid.

LOGIC 16

EXAMPLE 2.8 Use the rules of inference to show that the following argument is valid.

If Chelsea can beat Arsenal then Chelsea can beat Manchester United. If Chelsea can beat Arsenal then Chelsea can beat Liverpool. Chelsea can beat Arsenal if Chelsea get a new striker. Chelsea get a new striker. Therefore, Chelsea can beat both Manchester United and Liverpool.

Solution Let P denote Chelsea get a new striker, Q denote Chelsea can beat Arsenal, R denote Chelsea can beat Manchester United and S denote Chelsea can beat Liverpool. Then the hypotheses are:

■ EXAMPLE 2.9 Let P, Q, R and S be propositions. Show that the conclusion S can be inferred from the hypotheses P⇒Q, notP⇒R, Q⇒S and notR. Solution

LOGIC 17

EXERCISES 2 - LOGIC 2.1 Let P be the proposition Roses are red and Q be the proposition Violets are blue. Express each of the following propositions as logical expressions. (a) If roses are not red, then violets are not blue. (b) Roses are red or violets are not blue. (c) Either roses are red or violets are blue (but not both). Use a truth table to show that (a) and (b) are logically equivalent. 2.2 A compound proposition which is always true is called a tautology. By constructing truth tables, decide which of the following are tautologies. (a) not(Pand(notP)) (b) P⇒(notP) (c) (Pand(P⇒Q))⇒Q 2.3 Show that (P⇒Q) ⇒R is logically equivalent to ((notP)⇒R) and (Q⇒R). 2.4 Let x stand for cat and P(x) be the predicate x has whiskers. Write each of the following propositions in symbolic form. (a) All cats have whiskers. (b) There is a cat with no whiskers. (c) No cat has whiskers. Express the negation of (b) in symbolic form and express the negation of (c)

both in symbols and in English. 2.5 Let P(x) stand for x is tall, and Q(x) stand for x is fat where x is a person. Express the proposition ∀x (P(x)andQ(x)) in words and determine which of the following forms its negation: (a) someone is short and fat; (b) no-one is tall and thin: (c) someone is short or thin.

LOGIC 18

2.6 In this question, P denotes I study hard, Q denotes I get A grades and R denotes I get rich. Formulate each of the following arguments symbolically and

determine (using inference rules) whether the argument is valid. (a) If I study hard then I get A grades. If I don’t get rich then I don’t get A grades. I study hard. Therefore, I get rich. (b) If I study hard then I get A grades. If I get A grades then I get rich. I don’t get rich. Therefore, I study hard. 2.7 Establish the inference rule known as disjunctive syllogism which states that

from PorQ and notP we can infer Q.

2.8 Determine which rules of inference are required to show that the following is a

valid argument concerning some object. It is hot or green. If it is heavy then it is not soft. If it is green or red then it is

soft. It is not hot. Therefore, it is not heavy.

LOGIC 19

SOLUTIONS TO EXERCISES 2 - LOGIC 2.1 (a) (not P) ⇒ (not Q) (b) P or (not Q) (c) (P or Q) and (not(P and Q))

P Q not P not Q P or (not Q) ((not P) ⇒ (not Q)) T T F F T T T F F T T T F T T F F F F F T T T T

Since the last two columns are identical, (a) and (b) are logically equivalent. 2.2

P not P P and (not P) not(P and (not P)) P ⇒ not P T F F T F F T F T T

P Q P ⇒ Q P and (P ⇒ Q) (P and (P ⇒ Q) ⇒ Q T T T T T T F F F T F T T F T F F T F T

(a) and (c) are tautologies. 2.3

P Q R P⇒Q (notP)⇒R Q⇒R (P⇒Q)⇒R ((notP)⇒R) and (Q⇒R) T T T T T T T T T T F T T F F F T F T F T T T T T F F F T T T T F T T T T T T T F T F T F F F F F F T T T T T T F F F T F T F F

Since the last two columns are identical, (P⇒Q)⇒R is logically equivalent to ((notP)⇒R) and (Q⇒R).

2.4 (a) ∀x (P(x)) (b) ∃x (not P(x)) (c) ∀x (not P(x)) The negation of (b) is ∀x (P(x)) [so that (a) is the negation of (b)]. The negation of (c) is ∃x (P(x)) in symbols which says that there is a cat with whiskers.

2.5 Everyone is tall and fat. Its negation is (c).

2.6 (a) The hypotheses of the argument are P⇒Q, (notR)⇒(notQ) and P. The conclusion is R. We can infer Q from P and P⇒Q using modus ponens. Then, using the fact that not(notQ) is logically equivalent to Q, we apply modus tollens to Q and (notR)⇒(notQ) to infer not(notR). Since not(notR) is logically equivalent to R we have therefore inferred R from the given hypotheses and so the argument presented is valid.

LOGIC 20

(b) The hypotheses of the argument are P⇒Q, Q⇒R and notR. The conclusion is P. We can infer P⇒R from P⇒Q and Q⇒R using hypothetical syllogism. We can then apply modus tollens to notR and P⇒R to infer notP. Since notP has the opposite truth value to P, the argument is invalid. [We could have used modus tollens twice as an alternative]. 2.7 Consider the following combined truth table for notP and PorQ.

P Q not P P or Q T T F T T F F T F T T T F F T F

If notP and PorQ are both true (see line 3 of the table) then Q is also true. Hence, we can infer Q from notP and PorQ thus establishing the rule. 2.8 Disjunctive syllogism applied to it is not hot and it is hot or green allows us to infer it is green. Then, use addition to infer that it is green or red. Combine this with if it is green or red then it is soft using modus ponens to infer it is soft. Combine this with if it is heavy then it is not soft using modus tollens to infer that it is not heavy as required.

LOGIC 21

LOGIC 22

  • EXAMPLE 2.3
  • Hypotheses
  • Conclusion
    • Modus ponens
    • Modus tollens
    • Addition
  • P
    • Simplification
  • PandQ
    • Conjunction
      • P , Q
        • P
        • P
        • P
        • P
        • P

PROOF.pdf

PROOF In mathematics, logical arguments are used to give us proofs of theorems. In computing such proofs are essential in the design and verification of algorithms. The commonest types of proofs are ones where we wish to establish the truth of a proposition of the form P⇒Q. There are several standard methods of proof, each of which rules out the case where P is true and Q is false which, recall, is the only case where P⇒Q is false. DIRECT ARGUMENT

Assume P is true and show that Q is true. CONTRAPOSITIVE ARGUMENT

Assume Q is false and show that P is false. PROOF BY CONTRADICTION

Assume P is true and Q is false and derive a contradiction . EXAMPLE 3.1 Use a direct method of proof to show that if x and y are odd integers, then xy is also odd. Solution Each odd integer can be written in the form 2m + 1 for some other integer m. So, we let x = 2m + 1 and y = 2n + 1 where m and n are some integers. Then xy =

■ EXAMPLE 3.2 Let n be a positive integer. Prove, using the contrapositive, that if n2 is odd, then n is odd. Solution If n is odd is false then n is even and so we can write n = 2m for some integer m. Then n2 =

■ EXAMPLE 3.3

PROOF 23

Use a proof by contradiction to show that for all real numbers x and y, if x + y ≥ 2 then either x ≥ 1 or y ≥ 1. Solution Assume that x + y ≥ 2 but that the conclusion (x ≥ 1 or y ≥ 1) is false. Hence, x < 1 and y < 1. Then x + y <

■ In computing a program is said to be CORRECT if it behaves in accordance with its specifications. PROGRAM TESTING shows that selected input values give acceptable output values. PROOF OF CORRECTNESS uses formal logic to prove that for any input values, the output values are correct. To prove that an algorithm is correct (in other words it does what it is claimed to do) we need to check any claims made about the variables used in the algorithm before, during and after the execution of the algorithm. Let P be a predicate which is true for the input values of an algorithm A and Q be a predicate describing conditions that the output values should satisfy. The statement {P}A{Q} means that ‘if the execution of algorithm A starts with the predicate P true, then it will terminate with the predicate Q true’. The predicate P is called the PRECONDITION and the Q is called the POSTCONDITION. The statement {P}A{Q} is itself a predicate and proving the correctness of algorithm A requires us to prove that {P}A{Q} is true. For simple algorithms this is quite straightforward.

PROOF 24

EXAMPLE 3.4 Prove that the following algorithm is correct.

Algorithm A {x = 3} begin y := x + 1; y := x * y ; end {y = 12}

Solution The precondition P is x =3 and the postcondition Q is y = 12. To prove that {P}A{Q} is true we begin with x = 3 and show that after the two assignments y = 12.

■ When a sequence of statements occur in an algorithm A we break A into suitable segments A1, A2, . . . , An and prove the chain of assertions {P}A1{Q1}, {Q1}A2{Q2}, . . . , {Qn-1}An{Q} where the postcondition for any segment becomes the precondition for the following segment. EXAMPLE 3.5 Prove that the algorithm Quad is correct.

Algorithm Quad {x is a real number} begin y := a * x; y := (y + b) * x; y := y + c; end { y = ax

2 + bx + c}

PROOF 25

Solution Introduce pre and post conditions as below P → { x = x1} begin y := a * x; Q1 → { y = ax1 and x = x1} y := (y + b) * x; Q2 → { y = ax1

2 + bx1} y := y + c; end Q → { y = ax12 + bx1 + c} As demonstrated, successive substitutions show that

{P} y := a * x{Q1}, {Q1} y := (y + b) * x {Q2}, and {Q2} y := y + c {Q}

are all true. So {P}Quad{Q} is true and hence Quad is correct. ■ Algorithms containing conditional statements are also susceptible to proof techniques. When a single if . . . then control structure is in place, pre and post conditions have to reflect the alternative routes through the algorithm. Specifically, given the conditional statement if condition then statement 1 else statement 2 introduce an initial precondition P and a final postcondition Q and prove that both of the statements

{P and condition}statement 1{Q}, and {P and not(condition)}statement 2{Q}

are true. EXAMPLE 3.6 Prove that the algorithm Absolute is correct.

Algorithm Absolute {x is a real number} begin if x ≥ 0 then abs := x; else abs := −x; end

{abs is the absolute value of x}

PROOF 26

Solution

■ When iterative statements like while . . . do are involved the use of pre- and post- conditions becomes unwieldy. Proving the correctness of algorithms containing loops uses a powerful method of proof called MATHEMATICAL INDUCTION. Consider the following recursive algorithm, intended to calculate the maximum element in a list a1, a2, . . . , an of positive integers. begin i := 0; M := 0; while i < n do begin i := i + 1; M := max(M, ai); end end We trace the algorithm for the input list a1 = 4, a2 = 7, a3 = 3 and a4 = 8.

i M i < 4?

The output is M = 8, which is correct. Notice that after each execution of the loop, M is the maximum of the elements of the list so far considered. Consider an input list a1, a2, . . . , an of length n. 1. For an input list a1 of length one, the loop is executed once and M is assigned

to the maximum of 0 and a1 which is just a1 which is correct. 2. If, after k executions of the loop, M is the maximum element of the list a1, a2, .

. . , ak then after one more loop M is assigned the value max(M, ak+1) which will then be the maximum element of the list a1, a2, . . . , ak+1.

PROOF 27

By 1. the algorithm works for any list of length one, and so by 2. it works for any list of length two. By 2. again it works for any list of length three, and so on . . . . . . . Hence, the algorithm works for any list of any length n and so the algorithm is correct. This process can be formalised as follows:

THE PRINCIPLE OF MATHEMATICAL INDUCTION

Let P(n) be a predicate defined for n ≥ 1. Suppose that 1. P(1) is true, and 2. ∀k ≥ 1 (P(k) ⇒ P(k + 1)) is true. Then P(n) is true for all n ≥ 1.

EXAMPLE 3.7 Use a proof by induction to prove that the algorithm Square is correct.

Algorithm Square {n is a positive integer} begin sq := 0; for i := 1 to n do sq := sq + 2i − 1; end

{sq = n 2 }

PROOF 28

Solution Define P(n) to be the predicate sq = n2 after n executions of the loop Let sqk be the value of sq after k executions of the loop. We prove that (1) sq1= 12 (2) if sqk = k2 then sqk+1 = (k+1)2

So, P(1) is true and for all k ≥ 1, P(k) ⇒ P(k + 1) is also true so that, by the Principle of Mathematical Induction, P(n) is true for all n ≥ 1. Hence, Square is correct.

■ Mathematical induction is used extensively to prove results in mathematics. EXAMPLE 3.8

PROOF 29

Prove that 1 + 2 + . . . + n = ½ n(n + 1) for all integers n ≥ 1. Solution Let P(n) be the predicate

1 + 2 + . . . + n = ½n(n + 1) In the case n = 1, the left-hand side is simply 1, and the right-hand side is ½ (1)(1 + 1) = 1. Therefore, P(1) is true. Assume now that 1 + 2 + . . . + k = ½k(k + 1) for some k ≥1. Then, 1 + 2 + . . + (k + 1) = (1 + 2 + . . + k) + (k + 1) = ½k(k + 1) + (k + 1) = ½[k(k + 1) + 2(k + 1)] = ½[(k + 2)(k + 1)] = ½(k + 1)(k + 2) = ½(k + 1)((k + 1) + 1) Hence, ∀k ≥ 1 (P(k) ⇒ P(k + 1)) is true. By induction, P(n) is true for all n ≥ 1.

■ EXAMPLE 3.9 A sequence of integers x1, x2, . . . , xn is defined recursively as follows:

x1 = 1 and xk+1 = xk + 8k for k ≥ 1 Prove that xn = (2n − 1)

2 for all n ≥ 1. Solution Let P(n) be the predicate xn = (2n − 1)

2.

PROOF 30

■ EXAMPLE 3.10 Prove that n! ≥ 2

n − 1 for all integers n ≥ 1. Solution Recall that n! = n × (n − 1) × . . . × 1. For example, 4! = 4 × 3 × 2 × 1 = 24. Let P(n) be the predicate n! ≥ 2

n − 1.

PROOF 31

EXERCISES 3 - PROOF 3.1 (a) Give a direct proof that

n and m are even integers ⇒ n + m is an even integer is true. (b) Give a contrapositive proof that

n2 is an even integer ⇒ n is an even integer is true. 3.2 Prove each of the following by contradiction.

(a) If 100 coins are distributed among nine bags then some bag contains 12 or more coins. (b) If 189 coins are distributed among nineteen bags so that each bag contains

at least one coin then at least two bags contain the same number of coins. [Hint: for part (b) consider nineteen bags no two of which contain the same number of coins, put the bags in ascending order of the number of coins they contain and then calculate the minimum number of coins in total. You may find the result of Example 3.5 useful.]

3.3 Using suitable pre- and post-conditions prove that the algorithm Cubic is

correct. Algorithm Cubic

{x is a real number} begin

y := a * x; y := (y + b) * x; y := (y + c) * x; y := y + d;

end { y = ax3 + bx2 + cx + d}

PROOF 32

PROOF 33

3.4 Use a proof by induction to prove that the algorithm Hanoi is correct. Hanoi {n is a positive integer} begin H := 0; for i := 1 to n do H := 2H + 1; end

{H = 2n − 1} 3.5 Prove each of the following by induction on n. (a) 1 5 9 4 3 + + + + −. . . nb g = n n(2 1)− for all n ≥ 1.

(b) 1

1 3 1

3 5 2 1 2 1 2 1b gb g b gb g b gb g . . . 1

+ + + − +

= +n n

n n

for all n ≥ 1.

(c) 1 1 2 2 1 1! ! ! !b g b g b g b g . . . + + + = + −n n n for all n ≥ 1. [Recall that the quantity n!, read as n factorial, is an abbreviation for the

product of the numbers 1, 2, 3, . . . , n. Hence, ( ) ( )! 1 2 . . n n n n 2 1= × − × − × × × .]

3.6 A sequence of real numbers x1 , x2 , . . . , xn is defined by

x1 = 1 and k k

k x

x x

+ +

1 2

= for k ≥ 1

Calculate x2 , x3 , and x4 Prove by induction that n nx =

1 2 1−

for all n ≥ 1

PROOF 34

SOLUTIONS TO EXERCISES 3 - PROOF

3.1 (a) If n and m are even integers we can write n = 2a and m = 2b where a and b are also integers. Then, n + m = 2a + 2b = 2(a + b).

Hence n + m is an even integer. (b) If n is an odd integer we can write n = 2a + 1 where a is also an integer. Then, n2 = (2a + 1)2 = 4a2 + 4a + 1 = 2(2a2 + 2a) + 1. Hence n2 is an odd integer. Therefore if n2 is an even integer, then n is an even integer. 3.2 (a) Deny the conclusion so that all bags contain at most 11 coins. Since there are nine bags,

the maximum number of coins they can collectively contain is 9 × 11 = 99, a contradiction.

Hence, the stated result is true. (b) Deny the conclusion so that no two bags contain the same number of coins. Ordering

the bags in ascending order of the number of coins they contain means that the first bag contains at least one coin, the second at least two coins and so on up to the nineteenth bag which contains at least 19 coins. This gives a total of at least 1 + 2 + 3 + . . . + 19 = ½(19×20) = 190 coins (using Example 3.5).

This contradicts the fact that we only have 189 coins and so the stated result is true. 3.3 A suitable pre-condition is x = x1. Starting with {x = x1} successive assignments produce the

following post-conditions each of which becomes the pre-condition for the next assignment. After y := a * x we have {x = x1 and y1 = a x1}. After y := (y + b) * x we have {x = x1 and y1 = a x1

2 + b x1}. After y := (y + c) * x we have {x = x1 and y1 = a x1

3 + b x1 2 + c x1}.

After y := y + d we have {x = x1 and y1 = a x1 3 + b x1

2 + c x1 + d}. Hence, Cubic is correct. 3.4 Let Hk be the value of H after k executions of the loop. If n = 1 the loop is executed once and H1 = 1 = 2

1 − 1. Now assume that Hk = 2

k − 1 for some k ≥ 1. Then, after one more execution of the loop, Hk+1 = 2Hk + 1 = 2(2

k − 1) + 1 = 2k + 1 − 2 + 1 = 2k + 1 − 1. Hence, by the Principle of Mathematical Induction, Hn = 2

n − 1 for all n ≥ 1. Therefore, Hanoi is correct.

3.5 (a) Let P(n) be the predicate ( )1 5 9 . . . 4 3n+ + + + − = (2 1)n n − . When n = 1, the left hand side evaluates to 1 and (2 1)n n − = 1. Hence, P(1) is true. Suppose now that P(k) is true for some k ≥ 1. We show that P(k + 1) is true. = ( )1 5 9 . . . 4( 1) 3k+ + + + + − ( )1 5 9 . . . 4 3k+ + + + − + (4k + 1) = k(2k − 1) + (4k + 1) = ( )22 4k k k 1+ +− = 22 3k k 1+ + = ( ) ( )1 2 1k k+ + = ( ) ( )1 2( 1) 1k k+ + − By induction, P(n) is true for all n ≥ 1.

PROOF 35

(b) Let P(n) be the predicate

( ) ( ) ( ) ( ) ( ) ( )

1 1 1 . . .

1 3 3 5 2 1 2 1 2 1

n

n n n + + + =

− + + .

When n = 1, the left hand side evaluates to 1 3 and

1 2 1 3

n n =+ .

Hence, P(1) is true. Suppose now that P(k) is true for some k ≥ 1. We show that P(k + 1) is true.

( ) ( ) ( ) ( ) ( ) ( )

1 1 1 . . .

1 3 3 5 2( 1) 1 2( 1) 1k k + + +

+ − + +

= ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( )

1 1 1 1 . . .

1 3 3 5 2 1 2 1 2 1 2 3k k k k + + + +

− + + +

= ( ) ( ) ( )

1

2 1 2 1 2 3

k

k k +

+ + k +

= ( )

( ) ( ) ( ) ( ) 2 3 1

2 1 2 3 2 1 2 3

k k

k k k k

+ +

+ + + +

= ( )

( ) ( ) 2 3 1

2 1 2 3

k k

k k

+ +

+ +

= ( ) ( )

22 3 1

2 1 2 3

k k

k k

+ +

+ +

= ( ) ( )

( ) ( ) 2 1 1

2 1 2 3

k k

k k

+ +

+ +

= 1

2 3 k k + +

= 1

2( 1) 1 k k

+ + +

as required. By induction, P(n) is true for all n ≥ 1. (c) Let P(n) be the predicate ( ) ( ) ( ) ( )1 1! 2 2! . . . ! 1 ! 1n n n+ + + = + − . When n = 1, the left hand side evaluates to 1 and (n + 1)! − 1 = 1. Hence, P(1) is true. Suppose now that P(k) is true for some k ≥ 1. We show that P(k + 1) is true. ( ) ( ) ( )1 1! 2 2! . . . ( 1) ( 1) !k k+ + + + + ( ) ( ) ( ) ( )1 1! 2 2! . . . ! ( 1) ( 1) !k k k k= + + + + + + ( ) ( )1 ! 1 ( 1) ( 1)!k k k= + − + + + ( )( 2) ( 1) ! 1 k k= + + − ( )2 ! 1 k= + − as required. By induction, P(n) is true for all n ≥ 1.

3.6 x2 = 1 3

x3 = 1 7

and x4 = 1

15 .

Let P(n) be the predicate 1

2 1 n n

x = −

.

Substituting n = 1 shows P(1) is true. Suppose now that P(k) is true for some k ≥ 1.

Then 1

2 k

k

k

x x

x +

= +

=

1

2 1 1

2 2 1

k

k

+ −

⎛ ⎞ ⎜ ⎟ ⎝ ⎠

⎛ ⎞ ⎜ ⎟ ⎝ ⎠

= 1

1 2(2 1)k+ − =

1

1

2 1k + − .

Hence P(k + 1) is also true. By induction, P(n) is true for all n ≥ 1.

PROOF 36

  • EXAMPLE 3.5
  • Solution
  • Specifically, given the conditional statement
    • Solution

RELATIONS.pdf

RELATIONS If we learn that two people, Horace and Anna are related we understand that there is some family connection between them. The ordered pair (Horace, Anna) is distinguished from other pairs of people in that there is some relationship (cousin, father, etc.) that Horace and Anna satisfy. The mathematical analogue is to distinguish certain pairs in the Cartesian product A × B of two sets A and B from other ordered pairs because they satisfy some relationship that the components of other pairs do not. Consider the Modular Programme at Oxford Brookes University. Let S denote the set of modular degree students, and M denote the set of modules on offer. The subset of S × M consisting of ordered pairs (s, m) drawn from S × M with the property that student s is registered for module m. captures the relationship is registered for that holds between the set of students and the set of modules. A BINARY RELATION between sets A and B is simply a subset R of A × B. In the case when A = B, we refer to R as a RELATION ON A. EXAMPLE 5.1 Consider the set P of people in the following family tree:

Fred + Mavis John + Mary

Alice Ken + Sue Mike Penny

Jane Fiona Alan

Write down the ordered pairs belonging to the following relations on the set P: a) R = {(x, y) : x is a grandmother of y}, b) S = {(x, y) : x is a brother of y}. Solution

RELATIONS 51

EXAMPLE 5.2 Write down the ordered pairs belonging to the following binary relations between A = {1, 3, 5, 7} and B = {2, 4, 6}: a) U = {(x, y) : x + y = 9} b) V = {(x, y) : x < y} Solution

■ EXAMPLE 5.3 The following defines a relation on {1,2,3,4,5,6}

R = {(x, y) : x is a divisor of y} Write down the ordered pairs belonging to R. Solution

■ GRAPHICAL REPRESENTATION OF RELATIONS We represent the elements of two sets A and B as the vertices of a graph. For each of the ordered pairs in a relation R between A and B, draw an arrow linking the related elements. e.g. for the relation V of Example 5.2

1 • • 2

3 • • 4

5 • • 6

7 •

RELATIONS 52

For a relation on a single set A we use a directed graph in which a single set of vertices represent the elements of A and arrows link the related elements. e.g. for the relation R of Example 5.3

1 • • 2

6 • • 3

5 • • 4

MATRIX REPRESENTATION OF RELATIONS We can also represent a relation by an array. Let R be a relation between sets A = {a1, a2, . . . , an} and B = {b1, b2, . . . , bm}. We can represent R by an array M with n rows and m columns. Such an array is called an n by m matrix. The entry in the row i and column j of this matrix is given by M(i, j) where M(i, j) = T if (ai, bj) ∈ R M(i, j) = F if (ai, bj) ∉ R For the relation U of Example 5.2 the matrix representation is:

2 4 6

1

3

5

7

F F F F F T F T F T F F

⎡ ⎤ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎣ ⎦

RELATIONS 53

EXAMPLE 5.4 Write down the matrix representation of the relation R in Example 5.3. Solution

1 2 3 4 5 6

1

2

3

4

5

6

⎡ ⎤ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥⎣ ⎦

If R is a relation between sets we can write x R y whenever (x, y) ∈ R. The predicate x R y is read as x is R-related to y. For example, the predicate x is the sister of y defines a relation on the set of all people. Another example is Example 5.3 where the predicate x is a divisor of y gives a neat verbal description of the relation. To summarise: A binary relation R can be described in any of the following ways • in words (using some predicate) • as a set of ordered pairs • as a digraph • as a matrix

RELATIONS 54

We now restrict attention to general relation R on a single set A and look at properties which R may or may not possess. A relation R on a set A is • REFLEXIVE when x R x for all x in A; • SYMMETRIC when (x R y ⇒ y R x) for all x and y in A;

• ANTISYMMETRIC when (x R y and y R x ⇒ x = y) for all x and y in A;

• TRANSITIVE when (x R y and y R z ⇒ x R z) for all x, y and z in A. In terms of the ordered pair representation of a relation, R is reflexive if R contains all possible pairs of the form (x, x); symmetric if whenever R contains the pair (x, y) it also contains the symmetrical pair (y, x); antisymmetric if when x ≠ y and R contains the pair (x, y) then R does not contain the pair (y, x); transitive if whenever R contains the pairs (x, y) and (y, z) then R also contains the pair (x, z). In the directed graph representation, R is reflexive if there is always an arc from every vertex to itself; symmetric if whenever there is an arc from x to y there is also an arc from y to x; antisymmetric if whenever x ≠ y and there is an arc from x to y there is no arc from y to x; transitive if whenever there are arcs from x to y and from y to z there is also an arc from x to z. In the matrix representation, R is reflexive if each leading diagonal entry is T; symmetric if there is symmetry about the leading diagonal; antisymmetric if not all T entries are symmetrical. Transitivity is not explicitly evident.

RELATIONS 55

EXAMPLE 5.5 Determine whether or not the relation x is a divisor of y on the set of natural numbers is reflexive, symmetric, antisymmetric or transitive ? Solution

■ An EQUIVALENCE RELATION on a set A is a relation R which is • reflexive • symmetric • transitive An equivalence relation is an abstraction of the notion of equality. Under an equivalence relation, related elements share some common attribute(s). The following are examples of equivalence relations: • x has the same angles as y on the set of all triangles; related triangles are said

to be SIMILAR; • the relation R given by x R y if and only if xy > 0 on the set of non-zero

integers; related integers have the same SIGN; • x has the same age as y on the set of all people; related people belong to the

same AGE GROUP.

RELATIONS 56

The above examples demonstrate that under an equivalence relation there is a natural partition of the elements of the underlying set into disjoint subsets called blocks. Elements of any block are in some sense equivalent to each other. This is a powerful idea which underlies all classification systems. Given an equivalence relation R on a set A we define the EQUIVALENCE CLASS of any x ∈ A to be the set Ex consisting of all those elements of A which are R-related to x. It can be proved that these equivalence classes form a PARTITION of A in the sense that A is the union of the equivalence classes and different equivalence classes have no elements in common.

A E x E y

E v E u

E z

EXAMPLE 5.6 Given that the relation x − y is even defines an equivalence relation on the set Z of all integers, determine the equivalence classes. Solution E1 = {x : x is an integer and x − 1 is even} is just the set of odd integers. E2 = {x : x is an integer and x − 2 is even} gives the set of even integers. An integer is either even or odd, so these two classes account for all the elements of Z. Hence, there are two equivalence classes E1 and E2.

■ This example is a special case of the more general equivalence relation on Z given by x − y is divisible by n where n is an integer, n ≥ 2. In the general case there are n equivalences classes called congruence classes modulo n. An arithmetic of congruence classes can be developed which is used extensively in defining hashing functions in computing and in cryptology (codes).

RELATIONS 57

PARTIAL ORDERINGS Partial orderings are important in situations where we wish to characterise precedence; in other words deciding if and when one element of a set precedes another. A PARTIAL ORDER on a set A is a relation R which is • reflexive • antisymmetric • transitive Examples of partial orderings are: • x ≤ y on the set of real numbers • X ⊆ Y on subsets of the universal set • x is a divisor of y on the set natural numbers Sets on which a partial order is defined are called POSETS. If R is a partial ordering on a set A and x R y, x ≠ y we call x a PREDECESSOR of y or equivalently, y is a SUCCESSOR of x. A given element y may possess many predecessors. But, if x is a predecessor of y and there is no z for which x R z and z R y, we call x an IMMEDIATE PREDECESSOR of y. We can represent immediate predecessors on a graph known as the HASSE DIAGRAM. The vertices of this diagram represent the elements of the poset A and if x is an immediate predecessor of y, the vertex x is placed below the vertex y and the two vertices are joined by an edge. EXAMPLE 5.7 The relation is a divisor of is a partial order on the set A = {1, 2, 3, 6, 12, 18}. Draw up a table of predecessors and immediate predecessors and draw the corresponding Hasse diagram. Solution

element predecessors Immediate predecessors 1 2 3 6

12 18

RELATIONS 58

A TOTAL ORDERING on a set A is a partial ordering in which every pair of elements are related. The Hasse diagram of a total order is just a long chain. Examples of total orders are: • x ≤ y on the set of real numbers • the usual lexicographical ordering of words in a dictionary

In computer science, various sorting procedures require that the elements of the set are totally ordered and hence can be sorted into an ordered list. Other applications use posets and rely on the fact that in any poset there are minimal elements (ones with no predecessors) and maximal elements (ones with no successors). In Example 5.7, the poset has one minimal element, namely the number 1. There are two maximal elements, namely 12 and 18.

RELATIONS 59

EXERCISES 5 - RELATIONS 5.1 List the set of ordered pairs and draw the graphical form of the relation with

matrix

1 T F T F 2 T F T F 3 F T T F

a b c d

⎡ ⎤ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥⎣ ⎦

5.2 For each of the following relations on N, list the ordered pairs that belong to the

relation. R = {(x, y) : 2x + y = 9} S = {(x, y) : x + y < 7} T = {(x, y) : y = x2} 5.3 Let R be the relation on {1, 2, 3, 4} given by u R v if and only if u + 2v is odd.

Represent R in each of the following ways. (a) as a set of ordered pairs (b) in graphical form (c) in matrix form 5.4 Determine which of the following relations on the set of people is reflexive?

symmetric? transitive? (a) has the same parents as (b) is a brother of (c) is older or younger than 5.5 Determine which of the following relations on Z is reflexive? symmetric? transitive? (a) x + y is an odd integer (b) x + xy is an even integer

RELATIONS 60

5.6 For each of the following equivalence relations R on the given set A, describe the blocks (equivalence classes) into which the relation partitions the set A.

(a) A is the set of books in the library ; R is given by x R y if and only if the

colour of x’s cover is the same as the colour of y’s cover. (b) A = Z ; R is given by x R y if and only if x − y is divisible by 3. (c) A is the set of all people; R is given by x R y if and only if x and y are of

the same sex. 5.7 Draw the Hasse diagram for each of the following posets. What do you notice? (a) x is a divisor of y on the set {1, 2, 3, 5, 6, 10, 15, 30} (b) X is a subset of Y on the set of all subsets of {1, 2, 3} 5.8 Lexicographical ordering works as follows: given two words X and Y, compare

X and Y letter by letter, passing over equal letters. If at any point a letter in X alphabetically precedes the corresponding letter in Y, then X precedes Y; if all the letters in X are equal to the corresponding letters in Y but we run out of letters in X before letters in Y, then X precedes Y; otherwise Y precedes X.

Use this technique to lexicographically order the words boo, bug, be, bah and

bugg. Note why each word precedes the next.

RELATIONS 61

SOLUTIONS TO EXERCISES 5 - RELATIONS 5.1 (a) {(1, a), (1, c), (2, a), (2, c), (3, b), (3, c)}

1 • • a

2 • • b

3 • • c

• d

5.2 R = {(1, 7), (2, 5), (3, 3), (4, 1)} S = {(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (2, 1), (2, 2), (2, 3), (2, 4), (3, 1), (3, 2), (3, 3), (4, 1), (4, 2), (5, 1)} T = {(n, n2) : n∈N} 5.3 (a) R = {(1, 1), (1, 2), (1, 3), (1, 4), (3, 1), (3, 2), (3, 3), (3, 4)} (b) (c)

1 • • 2

T T T T F F F F T T T T F F F F

L

N

M M M M

O

Q

P P P P

3 • • 4

5.4 (a) is reflexive, symmetric and transitive. (b) is transitive but not reflexive or symmetric. (c) is symmetric, but not reflexive or transitive. 5.5 (a) The relation is not reflexive [since x + x is always even] and not transitive [for example, 1 + 4 and 4 + 3 are both odd, but 1 + 3 is not], but it is symmetric [since x + y equals y + x]. (b) The relation is reflexive [since x + x2 = x(1 + x) is the product of two consecutive integers, one of which must be even] and transitive [if x + xy is even and y + yz is even then either x is even, in which case x + xz is automatically even or x is odd in which case all of x, y and z are odd and so, again, x + xz is even], but it is not symmetric [for example, 2 + (2)(3) = 8 is even, but 3 + (3)(2) = 9 is not]. 5.6 (a) Each equivalence class consists of all those books of a fixed colour. (b) There are three equivalence classes, E0 = {. . , −6, −3, 0, 3, 6, . . }, E1 = {. . , −5, −2, 1, 4, 7, . . } and E2 = {. . , −4, −1, 2, 5, 8, . . }. (c) There are two equivalence classes, males and females.

RELATIONS 62

RELATIONS 63

5.7 (a)

3 0

6 1 0

2 3 5

1

1 5

(b) The Hasse diagram is identical with the following correspondence between vertices: 1 ↔ ∅, 2 ↔ {1}, 3 ↔ {2}, 5 ↔ {3}, 6 ↔ {1, 2}, 10 ↔ {1, 3}, 15 ↔ {2, 3} and 30 ↔ {1, 2, 3}.

5.8. bah, be, boo, bug, bugg

RELATIONS 64

  • MATRIX REPRESENTATION OF RELATIONS
  • PARTIAL ORDERINGS
    • Immediate predecessors