22
Lecture 4.1: Relations Basics CS 250, Discrete Structures, Fall 2012 Nitesh Saxena Adopted from previous lectures by Cinda Heeren

Lecture 4.1: Relations Basics CS 250, Discrete Structures, Fall 2012 Nitesh Saxena Adopted from previous lectures by Cinda Heeren

Embed Size (px)

Citation preview

Page 1: Lecture 4.1: Relations Basics CS 250, Discrete Structures, Fall 2012 Nitesh Saxena Adopted from previous lectures by Cinda Heeren

Lecture 4.1: Relations Basics

CS 250, Discrete Structures, Fall 2012

Nitesh Saxena

Adopted from previous lectures by Cinda Heeren

Page 2: Lecture 4.1: Relations Basics CS 250, Discrete Structures, Fall 2012 Nitesh Saxena Adopted from previous lectures by Cinda Heeren

04/21/23

Course Admin Mid-Term 2 Exam

How was it? Solution will be posted soon Should have the results no later than next

Tues HW3

Was due today Bonus problem? Solution will be posted soon Results should be ready no later than next

Tues

Page 3: Lecture 4.1: Relations Basics CS 250, Discrete Structures, Fall 2012 Nitesh Saxena Adopted from previous lectures by Cinda Heeren

Roadmap Done with about 60% of the course We are left with two more homeworks We are left with one final exam

Cumulative – will cover everything Focus more on material covered after the two mid-terms

Please try to do your best in the remainder of the time; I know you have been working hard

Please submit your homeworks on time Still a lot of scope of improvement PLEASE DO ATTEND THE LECTURES

04/21/23

Page 4: Lecture 4.1: Relations Basics CS 250, Discrete Structures, Fall 2012 Nitesh Saxena Adopted from previous lectures by Cinda Heeren

04/21/23

Outline

Relation Definition and Examples Types of Relations Operations on Relations

Page 5: Lecture 4.1: Relations Basics CS 250, Discrete Structures, Fall 2012 Nitesh Saxena Adopted from previous lectures by Cinda Heeren

Relations

Recall the definition of the Cartesian (Cross) Product:

The Cartesian Product of sets A and B, A x B, is the setA x B = {<x,y> : xA and yB}.

A relation is just any subset of the CP!! R AxB

Ex: A = students; B = courses. R = {(a,b) | student a is enrolled in class b}We often say: aRb if (a,b) belongs to R

04/21/23

Page 6: Lecture 4.1: Relations Basics CS 250, Discrete Structures, Fall 2012 Nitesh Saxena Adopted from previous lectures by Cinda Heeren

Relations vs. Functions

Recall the definition of a function:f = {<a,b> : b = f(a) , aA and bB}

Is every function a relation?

Draw venn diagram of cross products, relations, functions

Yes, a function is a special kind

of relation.

04/21/23

Page 7: Lecture 4.1: Relations Basics CS 250, Discrete Structures, Fall 2012 Nitesh Saxena Adopted from previous lectures by Cinda Heeren

Properties of Relations

Reflexivity:A relation R on AxA is reflexive if for all aA, (a,a) R.

Symmetry:A relation R on AxA is symmetric if

(a,b) R implies (b,a) R .

04/21/23

Page 8: Lecture 4.1: Relations Basics CS 250, Discrete Structures, Fall 2012 Nitesh Saxena Adopted from previous lectures by Cinda Heeren

Properties of Relations

04/21/23

Page 9: Lecture 4.1: Relations Basics CS 250, Discrete Structures, Fall 2012 Nitesh Saxena Adopted from previous lectures by Cinda Heeren

Properties of Relations - Techniques

How can we check for the reflexive property?

Draw a picture of the relation (called a “graph”).

Vertex for every element of AEdge for every element of RNow, what’s R?

{(1,1),(1,2),(1,3),(1,4),(2,2),(2,3),(2,4),(3,3),(3,4),(4,4)}

12

34

Loops must exist on EVERY

vertex.

04/21/23

Page 10: Lecture 4.1: Relations Basics CS 250, Discrete Structures, Fall 2012 Nitesh Saxena Adopted from previous lectures by Cinda Heeren

Properties of Relations - Techniques

How can we check for the symmetric property?

Draw a picture of the relation (called a “graph”).

Vertex for every element of AEdge for every element of R

Now, what’s R?{(1,1),(1,2),(1,3),(1,4),(2,2),(2,3),(2,4),(3,3),(3,4),(4,4)}

12

34

EVERY edge must have a return edge.

04/21/23

Page 11: Lecture 4.1: Relations Basics CS 250, Discrete Structures, Fall 2012 Nitesh Saxena Adopted from previous lectures by Cinda Heeren

Properties of Relations - Techniques

How can we check for transitivity?

Draw a picture of the relation (called a “graph”).

Vertex for every element of AEdge for every element of RNow, what’s R?

{(1,1),(1,2),(1,3),(1,4),(2,2),(2,3),(2,4),(3,3),(3,4),(4,4)}

12

34

A “short cut” must be present for EVERY path

of length 2.

04/21/23

Page 12: Lecture 4.1: Relations Basics CS 250, Discrete Structures, Fall 2012 Nitesh Saxena Adopted from previous lectures by Cinda Heeren

Properties of Relations - Techniques

How can we check for the anti-symmetric property?

Draw a picture of the relation (called a “graph”).Vertex for every element of AEdge for every element of R

Now, what’s R?{(1,1),(1,2),(1,3),(1,4),(2,2),(2,3),(2,4),(3,3),(3,4),(4,4)}

12

34

No edge can have a

return edge.

Page 13: Lecture 4.1: Relations Basics CS 250, Discrete Structures, Fall 2012 Nitesh Saxena Adopted from previous lectures by Cinda Heeren

Properties of Relations - Examples

Let R be a relation on People,R={(x,y): x and y have lived in the same

country}

NoIs R transitive?

Is it reflexive? Yes

Is it symmetric? Yes

Is it anti-symmetric?

No

1 2

3?

1

?

1 2

?

04/21/23

Page 14: Lecture 4.1: Relations Basics CS 250, Discrete Structures, Fall 2012 Nitesh Saxena Adopted from previous lectures by Cinda Heeren

Properties of Relations - Examples

Let R be a relation on positive integers,R={(x,y): 3|(x-y)}

YesIs R transitive?

Suppose (x,y) and (y,z) are in R.

Then we can write 3j = (x-y) and 3k = (y-z) Definition of “divides”

Can we say 3m = (x-z)?

Is (x,z) in R?

Add prev eqn to get: 3j + 3k = (x-y) + (y-z)

3(j + k) = (x-z)

04/21/23

Page 15: Lecture 4.1: Relations Basics CS 250, Discrete Structures, Fall 2012 Nitesh Saxena Adopted from previous lectures by Cinda Heeren

Properties of Relations - Techniques

Let R be a relation on positive integers,R={(x,y): 3|(x-y)}

YesIs R transitive?

Is it reflexive? Yes

Is (x,x) in R, for all x?

Does 3k = (x-x) for some k?Definition of

“divides”

Yes, for k=0.

04/21/23

Page 16: Lecture 4.1: Relations Basics CS 250, Discrete Structures, Fall 2012 Nitesh Saxena Adopted from previous lectures by Cinda Heeren

Properties of Relations - Techniques

Let R be a relation on positive integers,R={(x,y): 3|(x-y)}

YesIs R transitive?

Is it reflexive? Yes

Is it symmetric? Yes

Suppose (x,y) is in R.

Then 3j = (x-y) for some j.Definition of

“divides”

Yes, for k=-j.

Does 3k = (y-x) for some k?

04/21/23

Page 17: Lecture 4.1: Relations Basics CS 250, Discrete Structures, Fall 2012 Nitesh Saxena Adopted from previous lectures by Cinda Heeren

Properties of Relations - Techniques

Let R be a relation on positive integers,R={(x,y): 3|(x-y)}

YesIs R transitive?

Is it reflexive? Yes

Is it symmetric? Yes

Is it anti-symmetric?

No

Suppose (x,y) is in R.

Then 3j = (x-y) for some j.Definition of

“divides”

Yes, for k=-j.

Does 3k = (y-x) for some k?

04/21/23

Page 18: Lecture 4.1: Relations Basics CS 250, Discrete Structures, Fall 2012 Nitesh Saxena Adopted from previous lectures by Cinda Heeren

More than one relation

Suppose we have 2 relations, R1 and R2, and recall that relations are just sets! So we can take unions, intersections, complements, symmetric differences, etc.

There are other things we can do as well…

04/21/23

Page 19: Lecture 4.1: Relations Basics CS 250, Discrete Structures, Fall 2012 Nitesh Saxena Adopted from previous lectures by Cinda Heeren

More than one relationLet R be a relation from A to B (R AxB), and let S be a relation

from B to C (S BxC). The composition of R and S is the relation from A to C (SR AxC):

SR = {(a,c): bB, (a,b) R, (b,c) S}

SR = {(1,u),(1,v),(2,t),(3,t),(4,u)}

A B C

1

2

3

4

x

y

z

s

t

u

v

R S

04/21/23

Page 20: Lecture 4.1: Relations Basics CS 250, Discrete Structures, Fall 2012 Nitesh Saxena Adopted from previous lectures by Cinda Heeren

More than one relationLet R be a relation on A. Inductively define

R1 = RRn+1 = Rn R

R2 = R1R = {(1,1),(1,2),(1,3),(2,3),(3,3),(4,1), (4,2)}

A A A

1

2

3

4

1

2

3

4

R R1

1

2

3

4

04/21/23

Page 21: Lecture 4.1: Relations Basics CS 250, Discrete Structures, Fall 2012 Nitesh Saxena Adopted from previous lectures by Cinda Heeren

More than one relationLet R be a relation on A. Inductively define

R1 = RRn+1 = Rn R

R3 = R2R = {(1,1),(1,2),(1,3),(2,3),(3,3),(4,1),(4,2),(4,3)}

A A A

1

2

3

4

1

2

3

4

R R2

1

2

3

4 … = R4

= R5

= R6… 04/21/23

Page 22: Lecture 4.1: Relations Basics CS 250, Discrete Structures, Fall 2012 Nitesh Saxena Adopted from previous lectures by Cinda Heeren

04/21/23

Today’s Reading Rosen 9.1