Lecture 4.2: Relations Basics

23
Lecture 4.2: Relations Basics CS 250, Discrete Structures, Fall 2011 Nitesh Saxena *Adopted from previous lectures by Cinda Heeren, Zeph Grunschlag

description

Lecture 4.2: Relations Basics. CS 250, Discrete Structures, Fall 2011 Nitesh Saxena * Adopted from previous lectures by Cinda Heeren , Zeph Grunschlag. Course Admin. Mid-Term 2 Exam Solution will be posted soon Should have the results by the coming weekend HW3 - PowerPoint PPT Presentation

Transcript of Lecture 4.2: Relations Basics

Page 1: Lecture 4.2: Relations Basics

Lecture 4.2: Relations Basics

CS 250, Discrete Structures, Fall 2011

Nitesh Saxena

*Adopted from previous lectures by Cinda Heeren, Zeph Grunschlag

Page 2: Lecture 4.2: Relations Basics

10/27/2011 Lecture 4.2 -- Relations

Course Admin Mid-Term 2 Exam

Solution will be posted soon Should have the results by the coming

weekend HW3

Solution will be posted soon Results should be ready by the coming

weekend

Page 3: Lecture 4.2: Relations Basics

10/27/2011 Lecture 4.2 -- Relations

Outline

Relation Examples and Definitions Matrix Representation Closures

Page 4: Lecture 4.2: Relations Basics

10/27/2011

Composing Relations

Q: Suppose R defined on N by: xRy iff y = x 2

and S defined on N by: xSy iff y = x 3

What is the composition SR ?

Lecture 4.2 -- Relations

Page 5: Lecture 4.2: Relations Basics

10/27/2011

Composing RelationsxRy iff y = x 2 xSy iff y = x 3

A: These are functions (squaring and cubing) so the composite SR is just the function composition (raising to the 6th power). xSRy iff y = x 6 (in this odd case SR = RS)

Q: Compose the following:1 1 1 12 2 2 23 3 3 34 4 4

5 5Lecture 4.2 -- Relations

Page 6: Lecture 4.2: Relations Basics

10/27/2011

Composing Relations1 1 12 2 23 3 34 4

5A: Draw all possible shortcuts. In our case,

all shortcuts went through 1:1 12 23 34

Lecture 4.2 -- Relations

Page 7: Lecture 4.2: Relations Basics

10/27/2011

Composing Relations: Picture1 1 12 2 23 3 34 4

5A: Draw all possible shortcuts. In our case,

all shortcuts went through 1:1 12 23 34

Lecture 4.2 -- Relations

Page 8: Lecture 4.2: Relations Basics

10/27/2011

Composing Relations: Picture1 1 12 2 23 3 34 4

5A: Draw all possible shortcuts. In our case,

all shortcuts went through 1:1 12 23 34

Lecture 4.2 -- Relations

Page 9: Lecture 4.2: Relations Basics

10/27/2011

Composing Relations: Picture1 1 12 2 23 3 34 4

5A: Draw all possible shortcuts. In our case,

all shortcuts went through 1:1 12 23 34

Lecture 4.2 -- Relations

Page 10: Lecture 4.2: Relations Basics

10/27/2011

Composing Relations: Picture1 1 12 2 23 3 34 4

5A: Draw all possible shortcuts. In our case,

all shortcuts went through 1:1 12 23 34

Lecture 4.2 -- Relations

Page 11: Lecture 4.2: Relations Basics

10/27/2011

Inverting Relations

Relational inversion amounts to just reversing all the tuples of a binary relation.

DEF: If R is a relation from A to B, then the relation R -1 from B to A defined by setting bR -1a if and only aRb.

Q: Suppose R defined from Z to N by: xRy iff y = x 2. What is the inverse R -1 ?

Lecture 4.2 -- Relations

Page 12: Lecture 4.2: Relations Basics

10/27/2011

Inverting Relations

A: xRy iff y = x 2. R is the square function so R -1 is square

root: i.e. the union of the two square-root branches. I.e:

yR -1x iff y = x 2 or in terms of square root:xR -1y iff y = ±x where x is non-negative

Lecture 4.2 -- Relations

Page 13: Lecture 4.2: Relations Basics

Lecture 4.2 -- Relations

Relations – matrix representation

Suppose we have a relation R on AxB, where A={1,2,3,4}, and B={u,v,w},

R={(1,u),(1,v),(2,w),(3,w),(4,u)}.Then we can represent R as:

The labels on the outside are for clarity. It’s

really the matrix in the middle that’s

important.

This is a |A| x |B| matrix whose entries indicate membership in R.

u v w

1 1 1 0

2 0 0 1

3 0 0 1

4 1 0 0

10/27/2011

Page 14: Lecture 4.2: Relations Basics

Lecture 4.2 -- Relations

Relations – matrix representation

Some things to think about.Let R be a relation on a set A, and let MR be the matrix representation

of R.Then R is reflexive if, ______________.

A. All entries in MR are 1.

B. The \ diagonal of MR contains only 1s.

C. The first column of MR contains no 0s.

D. None of the above.

1***

*1**

**1*

***1

10/27/2011

Page 15: Lecture 4.2: Relations Basics

Lecture 4.2 -- Relations

Relations – matrix representation

Some things to think about.Let R be a relation on a set A, and let MR be the matrix

representation of R.Then R is symmetric if, ______________.

A. All entries above the \ are 1.

B. The first and last columns of MR contain an equal # of 0s.

C. MR is visually symmetric about the \ diagonal.

D. None of the above.

*101

1*01

00*0

110*

10/27/2011

Page 16: Lecture 4.2: Relations Basics

Lecture 4.2 -- Relations

Relations – matrix representation

Suppose we have R1 and R2 defined on A:

R1 u v w

u 1 0 1

v 0 0 1

w 1 1 0

R2 u v w

u 1 1 0

v 0 1 1

w 0 0 1

Then R1 R2 is the bitwise “or” of the entries (Join By):

Then R1 R2 is the bitwise “and” of the entries (Meet):

1 1 1

0 1 1

1 1 1

1 0 0

0 0 1

0 0 0

MR1R2 = MR1 v MR2

MR1R2 = MR1 MR2

10/27/2011

Page 17: Lecture 4.2: Relations Basics

Lecture 4.2 -- Relations

Relations – composition using matrices

Suppose we have R and S defined on A:

R u v w

u 1 0 1

v 0 0 1

w 1 1 0

S u v w

u 1 1 0

v 0 1 1

w 0 0 1

Then SR corresponds to the boolean product1 1 1

0 0 1

1 1 1

10/27/2011

Page 18: Lecture 4.2: Relations Basics

Cs173 - Spring 2004

Relations - A TheoremTheorem: If R is a transitive relation, then Rn R, n.

How to prove? What strategy or technique should we use?

Page 19: Lecture 4.2: Relations Basics

Cs173 - Spring 2004

Relations - A TheoremIf R is a transitive relation, then Rn R, n.

Typical way of provin

g subset.

Proof by induction on n.Base case (n=1): R1 R because by definition, R1 = R.Induction case: if R is transitive, then Rk R.Prove: if R is transitive, then Rk+1 R.

We are trying to prove that Rk+1 R. To do this, we select an element of Rk+1 and show that it is also an element of R.

Let (a,b) be an element of Rk+1. Since Rk+1 = Rk R, we know there is an x so that (a,x) R and (x,b) Rk.

By assumption at the induction step, since Rk R, (x,b) R.

But wait, if (a,x) R, and (x,b) R, and R is transitive, then (a,b) R.

Page 20: Lecture 4.2: Relations Basics

Cs173 - Spring 2004

Relations - Another TheoremIf R is a reflexive relation, then Rn is reflexive relation, n.

Whiteboard!

Page 21: Lecture 4.2: Relations Basics

N-ary Relations So far, we were talking about binary

relations – defined on two sets. Can be generalized to N sets Ex: R = {(a, b, c): a < b < c}, defined

on set of integers – a 3-ary relation Applications in databases

10/27/2011 Lecture 4.2 -- Relations

Page 22: Lecture 4.2: Relations Basics

Lecture 4.2 -- Relations

Closure

Consider relation R={(1,2),(2,2),(3,3)} on the set A = {1,2,3,4}.

Is R reflexive?What can we add to R to make it reflexive?

No

(1,1), (4,4)

R’ = R U {(1,1),(4,4)} is called the reflexive closure of

R.

10/27/2011

Page 23: Lecture 4.2: Relations Basics

10/27/2011 Lecture 4.2 -- Relations

Today’s Reading Rosen 9.1 and 9.3