discrete math for cs binary relation: a binary relation between sets a and b is a subset of the...

31
Discrete Math for CS Binary Relation: A binary relation between sets A and B is a subset of the Cartesian Product A x B. If A = B we say that the relation is a relation on A. Suppose A = {1, 3, 5, 7} and B = {2, 4, 6, 8}. Further suppose R = { (1,2), (3,4), (5,6), (7,8)}. This is a subset of A x B so is a binary relation between A and B A B 8 6 4 2 1 3 5 7

Upload: bruce-potter

Post on 02-Jan-2016

223 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Discrete Math for CS Binary Relation: A binary relation between sets A and B is a subset of the Cartesian Product A x B. If A = B we say that the relation

Discrete Math for CS

Binary Relation: A binary relation between sets A and B is a subset of the

Cartesian Product A x B. If A = B we say that the relation is a relation on A.

Suppose A = {1, 3, 5, 7} and B = {2, 4, 6, 8}.

Further suppose R = { (1,2), (3,4), (5,6), (7,8)}. This is a subset of A x B so is a binary relation between A and B

A

B8

6

4

2

1 3 5 7

Page 2: Discrete Math for CS Binary Relation: A binary relation between sets A and B is a subset of the Cartesian Product A x B. If A = B we say that the relation

Discrete Math for CS

Binary Relation Exercise: If R = { (a,b): a < b}. Show R on the diagram below.

A

B8

6

4

2

1 3 5 7

Page 3: Discrete Math for CS Binary Relation: A binary relation between sets A and B is a subset of the Cartesian Product A x B. If A = B we say that the relation

Discrete Math for CS

Binary Relations as Sets of Ordered Pairs: Because we mention one set before another in a Cartesian

Product, A x B, the element, (a,b), in any relation, R, over A and B must have its first element from A and its second element from B.

So we say that the elements of R form ordered pairs.

Page 4: Discrete Math for CS Binary Relation: A binary relation between sets A and B is a subset of the Cartesian Product A x B. If A = B we say that the relation

Discrete Math for CS

Exercise: If X = {1, 2, 3, 4, 5, 6}, find R = { (x,y): x is a divisor of y}

R = { (1,1), (1,2), (1,3), (1,4), (1,5), (1,6), (2,2), (2,4), (2,6), (3,3), (3,6) }

Page 5: Discrete Math for CS Binary Relation: A binary relation between sets A and B is a subset of the Cartesian Product A x B. If A = B we say that the relation

Discrete Math for CS

Graph Representation of a Binary Relation: If A and B are two finite sets and R is a binary relation between

A and B we can represent this relation as a graph (set of vertices and edges).

A = {1, 3, 5 ,7}, B = {2, 4, 6, 8}R = {(a,b): a < b}

1

3

5

7

2

4

6

8

We call this graph a directed graph.

Page 6: Discrete Math for CS Binary Relation: A binary relation between sets A and B is a subset of the Cartesian Product A x B. If A = B we say that the relation

Discrete Math for CS

Example: A = {1, 2, 3, 4, 5, 6}. R is a relation on A defined by the following

directed graph.

Exercise: Give a rule-baseddefinition of R

R = {(a,b): a divides b}

1

6

2

3

45

Page 7: Discrete Math for CS Binary Relation: A binary relation between sets A and B is a subset of the Cartesian Product A x B. If A = B we say that the relation

Discrete Math for CS

Matrix Representation of a Binary Relation: If A and B are finite sets and R is a binary relation between A

and B then create a matrix, M, with the following properties:

the rows of the matrix are indexed by the elements of A the columns of the matrix are indexed by elements of B

M(ai,b

j) = 1 if (a

i,b

j) belongs to R; 0 otherwise

1

3

5

7

A

2 4 6 8B

0 1 1 1

0 0 1 1

0 0 0 1

0 0 0 0

Exercise: Give a rule-based definition of R

R = {(a,b): a + 1 < b}

Page 8: Discrete Math for CS Binary Relation: A binary relation between sets A and B is a subset of the Cartesian Product A x B. If A = B we say that the relation

Discrete Math for CS

Notation: If R is a binary relation on a set, X, we write x R y whenever

(x,y) ε R.

Example: sister_of relationship on girls in a school can be expressedas x is_sister_of y.

Page 9: Discrete Math for CS Binary Relation: A binary relation between sets A and B is a subset of the Cartesian Product A x B. If A = B we say that the relation

Discrete Math for CS

RelationProperties: Suppose R is a relation on a set A.

We say

R is reflexive if a R a for all a ε A. R is symmetric if x R y ==> y R x for all x, y ε A. R is antisymmetric when (x R y and y R x ==> x == y) for all

x, y ε A. R is transitive when (x R y and y R z ==> x R z) for all

x, y, z ε A.

Page 10: Discrete Math for CS Binary Relation: A binary relation between sets A and B is a subset of the Cartesian Product A x B. If A = B we say that the relation

Discrete Math for CS

Understanding Relations as Ordered Pairs: R is reflexive if (x,x) ε R for all x ε A.

R is symmetric if when (x,y) ε R then (y,x) ε R for all x,y ε A.

R is antisymmetric if when (x,y) ε R and x != y then (y,x) ε R.

R is transitive if when (x,y) ε R and (y,z) ε R then (x,z) ε R

Page 11: Discrete Math for CS Binary Relation: A binary relation between sets A and B is a subset of the Cartesian Product A x B. If A = B we say that the relation

Discrete Math for CS

Understanding Relations as digraphs. If R is a relation represented as a di-graph then

R is reflexive if every node has a loop to itself attached. R is symmetric if every directed edge is directed in both

directions. R is antisymmetric if there is no bi-directional edge. If there is a directed edge from x to y and another from y to z

then there is a directed edge from x to z

x

y

z

Page 12: Discrete Math for CS Binary Relation: A binary relation between sets A and B is a subset of the Cartesian Product A x B. If A = B we say that the relation

Discrete Math for CS

Understanding Relations as Matrices: Given a binary relation R on a finite set X.

Let M be the matrix whose rows and columns are indexed by the elements of X.

R is reflexive if the elements on the leading diagonal are all 1(T).

R is symmetric if the matrix is symmetric about the main diagonal.

R is antisymmetric if there are no symmetrical elements. Hence if m

ij == 1 the m

ji != 1.

The text says transitivity is not readily apparent. We'll see!

Page 13: Discrete Math for CS Binary Relation: A binary relation between sets A and B is a subset of the Cartesian Product A x B. If A = B we say that the relation

Discrete Math for CS

Exercise: Evaluate the following relations for the previously mentioned

properties:

x divides y on the natural numbers x != y on the integers. x is the same age as y in the set of all people. a/b has the same value as c/d in the set of all rationals, Q.

Page 14: Discrete Math for CS Binary Relation: A binary relation between sets A and B is a subset of the Cartesian Product A x B. If A = B we say that the relation

Discrete Math for CS

The Closure of a Relation: Suppose R is a relation on a set A. Then R A x A.

If R does not possess a relation property P, say being reflexive, then there exists another relation R* on A with the three properties

1)R R*

– R* possesses the desired property.

– If any other relation, say R', satisfies 1) and 2) then R* R'. We call R* the closure of R with respect to the property P on the

set A.

U

U

U

Page 15: Discrete Math for CS Binary Relation: A binary relation between sets A and B is a subset of the Cartesian Product A x B. If A = B we say that the relation

Discrete Math for CS

Example: A = {1, 2, 3} and R is the relation {(1,1),(1,2),(1,3),(3,1),(2,3)}.

Find the closure of R with respect to the reflexive property.

Find the closure of R with respect to the symmetric property.

Find the closure of R with respect to the transitive property.

R* = R U {(2,2), (3,3)}

R* = R U {(2,1), (3,2)}R* = R U {(2,1), (3,2)}

R* = R U {(2,1),(3,2),(3,3),(2,2)}

Page 16: Discrete Math for CS Binary Relation: A binary relation between sets A and B is a subset of the Cartesian Product A x B. If A = B we say that the relation

Discrete Math for CS

Equivalence Relation: A relation R on a set A that is reflexive, symmetric and transitive

is said to be an equivalence relation.

Equivalence relations generalize the concept of equality.

Exercise: Show = is an equivalence relation.

Exercise: Show a/b has the same value as c/d in the set of all rationals, Q is an equivalence relation.

Exercise: Show “has the same angles” is an equivalence relation on the set of all triangles.

Exercise: Show that x R y if xy > 0 for all x, y ε R.

Exercise: Give a name to the relation R in the previous exercise.

Page 17: Discrete Math for CS Binary Relation: A binary relation between sets A and B is a subset of the Cartesian Product A x B. If A = B we say that the relation

Discrete Math for CS

Equivalence Relations and Partitions: The examples on the previous slide show there is a strong

relationship between equivalence relations and partitions.

A partition is a set of non-empty subsets A1, A

2, ... of a set A

such that

– A1 U A

2 U ... = A

– Ai A

j = O for every pair i, j where i != j.

We call each Ai a block of the partition.

U

Page 18: Discrete Math for CS Binary Relation: A binary relation between sets A and B is a subset of the Cartesian Product A x B. If A = B we say that the relation

Discrete Math for CS

Example:

A1

A5

A4

A3

A2

A

Page 19: Discrete Math for CS Binary Relation: A binary relation between sets A and B is a subset of the Cartesian Product A x B. If A = B we say that the relation

Discrete Math for CS

Equivalence Class: If R is an equivalence relation on a set E, then

is called the equivalence class of x.

An easy way to represent an equivalence class of an element x is [x].

Ex = { y ε E: y R x }

Page 20: Discrete Math for CS Binary Relation: A binary relation between sets A and B is a subset of the Cartesian Product A x B. If A = B we say that the relation

Discrete Math for CS

Here it comes!

Page 21: Discrete Math for CS Binary Relation: A binary relation between sets A and B is a subset of the Cartesian Product A x B. If A = B we say that the relation

Discrete Math for CS

Theorem: Let R be an equivalence relation on a set A, then the

equivalence classes of R form a partition of A.

Proof: All equivalence classes are non-empty subsets of A. This is because for every x ε A, xRx and so x ε E

x.

Second, if xRy, then Ex = E

y. Suppose xRz, then z ε E

x.

However zRx since R is symmetric and so zRy by transitivity and so z ε E

y. Hence E

x E

y. Similarly E

y E

x. Hence E

x = E

y.

Now we must show that equivalence classes satisfy the first property of a partition. Namely, that the union of all the equivalence classes is the entire set. First E

x A since the

relation is defined over A.UU U

U

Page 22: Discrete Math for CS Binary Relation: A binary relation between sets A and B is a subset of the Cartesian Product A x B. If A = B we say that the relation

Discrete Math for CS

Proof (cont):

So

Since every x in A belongs to an Ex (remember xRx) we can say

Hence

U Ex A

U

xU E

x A

U

x

A U Ex

U

xA U E

x

U

x

A = U Ex

x

Page 23: Discrete Math for CS Binary Relation: A binary relation between sets A and B is a subset of the Cartesian Product A x B. If A = B we say that the relation

Discrete Math for CS

Proof (cont): We are left having to prove the second part of the definition of a

partition. Namely that any two equivalence classes that are not equal are disjoint.

The contrapositive is actually easier to prove. Namely that if two equivalence classes are not disjoint, they must be equal.

P: Two equivalence classes are not equalQ: Two equivalence classes are disjointTo prove: P --> Q

P: Two equivalence classes are not equalQ: Two equivalence classes are disjointTo prove: P --> Q

~P: Two equivalence classes are equal~Q: Two equivalence classes are not disjointTo prove: ~Q --> ~P

Page 24: Discrete Math for CS Binary Relation: A binary relation between sets A and B is a subset of the Cartesian Product A x B. If A = B we say that the relation

Discrete Math for CS

Proof (cont): Suppose

So there is a z in both Ex and E

y. This means xRz and yRz.

However if yRz then zRy because R is symmetric.

Finally, since xRz and zRy we can use transitivity to say xRy.From this we can conclude that E

x and E

y are equal.

Thus the equivalence classes of an equivalence relation satisfy both properties of being blocks of a partition and so form a partition. QED.

Ex E

y = O

U

Page 25: Discrete Math for CS Binary Relation: A binary relation between sets A and B is a subset of the Cartesian Product A x B. If A = B we say that the relation

Discrete Math for CS

Example: Let R be a relation on R satisfying xRy iff x – y is an integer.

Show R is an equivalence relation and find the equivalence classes – [0], [½] and [sqrt(2)].

Reflexive: xRx iff x-x is an integer. Since x-x = 0 and this is an integer, R is reflexive

Symmetric: if xRy then x-y is an integer. Hence y-x is also an integer so yRx. Hence the relation is symmetric.

Transitive: if xRy and yRz then x-y is an integer and y-z is an integer. Therefore(x-y)+(y-z) is also an integer. But this is x-z and so xRz. Hence the relation istransitive.

[0] = { x ε R: x-0 is an integer} = Z.

[½] = { ±0.5, ±1.5, ±2.5, ...}

[sqrt(2)] = {sqrt(2), sqrt(2)±1, sqrt(2)±2, ...}

Page 26: Discrete Math for CS Binary Relation: A binary relation between sets A and B is a subset of the Cartesian Product A x B. If A = B we say that the relation

Discrete Math for CS

Partial Orders: A binary relation on a set A that is reflexive, anti-symmetric and

transitive is called a partial order and the set is called a partially ordered set with respect to the relation or a poset for short.

Partial orders allow elements to “preceed” one another but not necessarily.

Examples:

subsets of a set are ordered partiallyUU

Page 27: Discrete Math for CS Binary Relation: A binary relation between sets A and B is a subset of the Cartesian Product A x B. If A = B we say that the relation

Discrete Math for CS

Partial Orders: Example:

subsets of a set are ordered partiallyU

{1,2,3}

{ }

{3}{2}{1}

{1,3}{2,3}{1,2}

v

Reflexive:

Anti-symmetric:

Transitive:

Hasse Diagram

Page 28: Discrete Math for CS Binary Relation: A binary relation between sets A and B is a subset of the Cartesian Product A x B. If A = B we say that the relation

Discrete Math for CS

Partial Orders: Example:

is a divisor of in the set of natural numbers.

Reflexive:

Anti-symmetric:

Transitive:

n is a divisor of n?

n is a divisor of m and m a divisor of n implies n == m?

n is a divisor of m and m a divisor of p implies n is a divisor of p?

Page 29: Discrete Math for CS Binary Relation: A binary relation between sets A and B is a subset of the Cartesian Product A x B. If A = B we say that the relation

Discrete Math for CS

Posets: A set on which a partial order is defined is called a poset.

If R is a partial order on a set A and xRy we say that x is a predecessor of y and y is a successor of x.

An element of A can have many pedecessors but if xRy and there is no z such that xRz and zRy we say x is an immediate predecessor of y (written x ≺ y).

We use a Hasse Diagram to represent immediate predecessors.

The vertices of a Hasse Diagram represent the elements of A and if they are directly connected then the lower vertex is the immediate predecessor of the upper vertex.

Page 30: Discrete Math for CS Binary Relation: A binary relation between sets A and B is a subset of the Cartesian Product A x B. If A = B we say that the relation

Discrete Math for CS

Example: Let A = {1,2,3,6,12,18}. Let R be the relation “is a divisor of”.

R is reflexive (a is a divisor of a), anti-symmetric (a is a divisor of b and b is a divisor of a implies a == b) and transitive (a is a divisor of b and b is a divisor of c implies a is a divisor of c).

R is a partial order.

1

2 3

6

1812

aRb as long as thereis a path up the diagramfrom a to b.

Page 31: Discrete Math for CS Binary Relation: A binary relation between sets A and B is a subset of the Cartesian Product A x B. If A = B we say that the relation

Discrete Math for CS

Total Order: A total order is a partial order in which every two elements of

the set are related. Hence if a, b A, either aRb or bRa.

Examples: - <= on the real numbers. - lexicographical ordering of a dictionary. - {1,2,6,12} is a totally ordered subset of A using “is a divisor of” relation.

NOTE: In Computer Science we often need to sort elements. In order to do this the comparison operator needs to be a total order.