1 / 37

CSRU 1100 Structures of Computer Science

CSRU 1100 Structures of Computer Science. Chapter 3 Relations. 1. Relation. A relation is a connection between objects in one set and objects in another set (possibly the same set). “age” is a relation defined between two sets: 1 st set: the set of names of students in your class.

sunee
Télécharger la présentation

CSRU 1100 Structures of Computer Science

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSRU 1100 Structures of Computer Science Chapter 3 Relations CS1100 1

  2. Relation • A relation is a connection between objects in one set and objects in another set (possibly the same set). • “age” is a relation defined between two sets: • 1st set: the set of names of students in your class. • 2nd set: the set of natural numbers. CS1100

  3. Describe a Relation in English • Three things: • 1st set: domain of the relation • 2nd set: codomain of the relation • Connection or rule is that links the objects in the domain and objects in the codomain. • Example • Domain: The set of names of people in your family. • Codomain: {Red, Black, Brown, Blonde} • Rule: (x,y) is in the relation if “x’s hair is y” CS1100

  4. Describe a Relation in English • What elements (objects) are in the following relation? • Domain: The Natural Numbers, N • Codomain: The Natural Numbers, N • Rule: (x,y) is in the relation if “x is one more than y ” (x = y+1) (1,0) (2,1) (3,2) CS1100

  5. Using Graph to Visualize Relation • A cloud where everything falls within • nodes (solid small circle): objects or elements (people, numbers, …) • Arcs: connecting two elements that are related • Arrows: the direction of the “relation” • x -> y 2 1 3 4 3 4 1 is related to 2, 2 is related to 3. 3 is related to 4 CS1100 5

  6. Cartesian Product (Cross Product) Create a new set consisting of all possible ordered pairs where the first element is from A, and second element is from B. CS1100

  7. Cartesian Product Example • Set A = {sky, water, ground} • Set B = {blue, gray, brown} • A x B = {(sky, blue), (sky, gray), (sky, brown), (water, blue), (water, gray), (water, brown), (ground, blue), (ground, gray), (ground, brown)} CS1100

  8. Describe a Relation as a Subset of the Cartesian Product of Two Sets • The Cartesian product is the relation that contains all possible connections between objects of its domain and codomain. • Some relation is just a subset of the Cartesian product of two sets. • Winter = {(sky, gray), (water, blue), (ground, brown)} • Winter ⊂ (A X B) • Any relation r between A and B has to be a subset r ⊆ (A X B) CS1100

  9. Exercise • List elements in the relation r defined as follows: • Domain: The Natural Numbers, N • Codomain: The Natural Numbers, N • Rule: r = {(x,y) ∈ NxN: x+y is odd} Adding two odd numbers ? Adding two even numbers ? Adding a number to itself ? Adding an even number and an odd number ? CS1100

  10. Reflexive • A relation is reflexive if it contains ordered pairs that have the same element in both positions, e.g., (x,x) for all objects x. Or • If r is a relation with domain and codomain A, then we say r ⊂ (A x A) is reflexive if and only if: (x ∈ A) ⇔ ((x,x) ∈ r) Or • A relation is reflexive if every object in the domain is related to itself. CS1100

  11. Graphically Describe Reflexive • If R is reflexive, there is a loop on each node in its graph • A relation is not reflexive if there is some object in the domain that is not related to itself R CS1100

  12. Reflexive Example • If set A consists of all people, and the relation is “Is the same age as” • Let’s pick several persons and see what happens • Tom is the same age as Tom • Carol is the same age as Carol • Sally is the same age as Sally • It always work, i.e., for any person, he/she is the same age as himself/herself. • Thus “is the same age as” is reflexive CS1100 12

  13. Try this mathematical one Domain and codomain are N R={(x, y) ∈ N x N: (x + y) is even} • Is R reflexive? • Is any number in N related to itself under R ? • Try a few numbers, 1, 2, 3, … • For any numbers in N ? • Yes, since a number added to itself is always even (since 2 will be a factor), so R is reflexive. CS1100 13

  14. Irreflexive vs. Non-reflexive • If a relation has absolutely no (x,x) elements, we say that relation is irreflexive. • An irreflexive relation’s graph has no self-loop • If a relation does not have (x,x) element for every x (even though there may be some x for which it is present), we say that relation is non-reflexive. CS1100

  15. Exercise Domain and codomain are N R={(x, y) ∈ N x N: x is larger than y} • Is R reflexive? • Is any number in N related to itself under R ? • Try a few numbers, 1, 2, 3, … • For any numbers in N ? • No, since a number is not larger than itself, we say it is not related to itself. • Actually, no natural number is larger than itself, i.e., NOT related to itself • Irreflexive relation CS1100

  16. For all relations Some object is related to itself, some object is not related to itself Every object is related to itself No object is related to itself Irreflexive Not reflexive, not irreflexive Reflexive All relations A relation cannot be both reflexive and irreflexive. CS1100 16

  17. Exercises: reflexive? irreflexive ? • Each of following relations is defined on set {1,2,3,4,5,6}, • R≤: “smaller or equal to” • Rd: “divides evenly”: e.g., 2 divides 6 evenly, but 4 doesn’t divides 6 evenly. • Ra: “adds up to 6”, e.g., (3,3), (1,5) … • R={(1,2),(3,4), (1,1)} CS1100 17

  18. Symmetric • A relation is symmetric if when it contains any ordered pair (x,y), it also contains the same pair in the other order (y,x) where x ≠ y. Or • If r is a relation with domain and codomain A, then we say r (A x A) is symmetric if and only if for x ≠ y: ((x,y) ∈ r) ⇔ ((y,x) ∈ r) Or • In the graphs of symmetric relations, arcs go both ways (with two arrows) CS1100

  19. Symmetric Example • “has the same hair color as” relation among a set of people • Pick any two people, say A and B • If A has the same color hair as B, then of course B has the same color hair as A • Thus it is symmetric • “is a friend of”, “is the same age of”, “goes to same college as” CS1100

  20. Is the following Relation Symmetric? • Domain: {1, 2, 3 ,4} • Relation={(1, 2), (1, 3), (4, 4), (4, 5), (3, 1), (5,4), (2, 1)} • Yes, it is symmetric since • (1,2) and (2,1) • (1,3) and (3,1) • (4,5) and (5,4) CS1100 20

  21. Antisymmetric vs. Non-symmetric • A relation never has (y,x) when it has (x,y), we say that relation is antisymmetric. • In the graph, anti-symmetric relations do not have two-way arcs. • When a relation does not have (y,x) for all (x,y) (even though there may be some (x,y) for which it is present), we say that relation is non-symmetric CS1100

  22. Exercise • “is older than” relation among a set of people • If “Sally is older than Tom”, then “Tom is older than Sally” must be false • We found a case where (Sally, Tom) is in the relation, but (Tom, Sally) is not in the relation • Therefore, this relation is not symmetric. • Actually, for “is older than” relation, it never works both way • It is an Anti-symmetric relation. CS1100

  23. Bizarre Middle Ground Anti-symmetric Non-symmetric Symmetric All relations • Some relations are symmetric • Some relations are anti-symmetric • Some relations are non-symmetric, i.e., • Some ordered pairs can be reversed • Some ordered pairs cannot be reversed CS1100 23

  24. Non-symmetric Example • “knows the birthday of” defined among a set of people • Some pair of people know each other’s birthday, • Tom know the birthday of Sally • Sally know the birthday of Tom • So we now know that it is not anti-symmetric. • Some people know the birthday of the other, but not vice versa • Tom knows the birthday of Bill • Bill doesn’t know the birthday of Tom • So the relation is not symmetric. • Therefore this relation is neither symmetric nor anti-symmetric, it is non-symmetric. CS1100 24

  25. Exercises: Symmetric? Anti-symmetric ? • For each of following relations defined on set {1,2,3,4,5,6} • R={(1,2),(3,4), (1,1),(2,1),(4,3)} • R={(1,2),(3,4),(1,1)} • R={(1,2),(3,4),(1,1),(4,3)} • R≤: “smaller or equal to” • Rd: “divides evenly”: e.g., 2 divides 6 evenly • Ra: “adds up to 6”, e.g., (3,3), (1,5) … CS1100 25

  26. Properties of Relation • Reflexive is about single objects of A. • Symmetric is about pairs of objects of A. • Transitive is about three pairs of objects of A. CS1100

  27. a c b Transitive • A relation is transitive if it contains ordered pairs that allow “shortcuts” – if (a,b) and (b,c) are both in the relation, then (a,c) must also be in the relation. Or • If r is a relation with domain and codomain A, then we say r ⊂ (A x A) is transitive if and only if : ((x,y) ∈ r and (y,z) ∈ r ) ⇔ ((x,z) ∈ r) CS1100

  28. Transitive Example • “is older than” relation among the set of people • Alice is older than bob. i.e., (Alice, Bob) • Bob is older than Cathy. i.e., (Bob, Cathy) • “Is Alice older than Cathy?” • Yes ! • Alice for sure is older than Cathy. We know that for any three people, A, B and C, if A is older than B, B is older than C, then fore sure, A is older than C. • This relation is a transitive relation. CS1100

  29. Not Transitive • A relation R is not transitive if there exists three elements in the domain, a, b, and c, and a is related to b - (a,b), b is related to c – (b,c), but a is not related to c. • The definition of transitive can only be tested for if there is a (b,c). • Strangely enough, if there are no (b,c) cases ever in the relation we are testing, then the relation is also considered transitive. Not transitive Transitive All relations CS1100

  30. Is this relation transitive ? • “is taking the same class as” relation on a set of students • Suppose three students, Bob, Katie, and Alex, • Bob is taking the same class as Katie • Katie is taking the same class as Alex • And now consider: • Is Bob is taking the same class as Alex ? • Many cases: no • Katie takes 1100 with Bob, and takes history with Alex, where Bob and Alex has no classes in common. • Therefore this relation is not transitive CS1100 30

  31. Exercises: Transitive or not ? • R≤: “smaller or equal to” defined on set {1,2,3,4} • For three numbers a, b, c from {1,2,3,4} • Would knowing that a≤b, and b≤c, allows me to conclude that a ≤c ? • Yes ! • It’s transitive ! • Let’s check it’s graph … CS1100 31

  32. Exercises: Transitive or not ? • Are the following relations defined on set {1,2,3,4,5,6} transitive ? • R={(1,2),(3,4), (1,1),(2,1),(4,3)} • R={(1,2),(3,4),(1,1)} • R={(1,2),(2,4),(1,1),(4,3)} • R≤: “smaller or equal to” • Rd: “divides evenly”: e.g., 2 divides 6 evenly • Ra: “adds up to 6”, e.g., (3,3), (1,5) … CS1100 32

  33. What properties does it have? • Domain: (1, 2, 3, 4, 5) • Codomain: (1, 2, 3, 4, 5) • Relation={(1, 1), (1, 2), (1, 3), (2, 3), (2, 4), (3, 4), (5,5)} • The Answer is: • It is not reflexive, and it is not irrefleixve • It is not symmetric, and it is anti-symmetric • It is not transitive CS1100 33

  34. What properties does it have? • Domain: (1, 2, 3, 4, 5) • Codomain: (1, 2, 3, 4, 5) • Relation={(1, 1), (2, 2), (3, 3), (4, 4), (5, 5)} • The Answer is: • It is reflexive • It is anti-symmetric • It is transitive CS1100 34

  35. Exercise • For the following relations defined on A={1,2,3,4,5,6,7,8} • R={(1,1),(1,2),(1,4),(1,8),(2,2),(2,4),(2,8),(3,3),(3,6),(4,4),(4,8),(5,5),(6,6),(7,7),(8,8)} CS1100 35

  36. Try a few out • Consider all positive numbers. Identify properties for the following relations • R1: “Is less than or equal to” • R2: “Is 10 less than” CS1100 36

  37. Try a few out • Consider following relations defined on the set of all people. Identify their properties: • R1: “Is smarter than” • R2: “Went to the same high school as” • R3: “Is a cousin of” CS1100 37

More Related