1 / 30

Discrete Maths

Discrete Maths. 242/240-213 , Semester 2, 2018 - 2019. Objective to introduce relations, show their connection to sets, and their use in databases. 5. Relations. Overview. Defining a Relation Relations using One Set Properties of a Relation reflexive, symmetric, transitive

nfindlay
Télécharger la présentation

Discrete Maths

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. Discrete Maths 242/240-213, Semester 2,2018-2019 • Objective • to introduce relations, show their connection to sets, and their use in databases 5. Relations

  2. Overview • Defining a Relation • Relations using One Set • Properties of a Relation • reflexive, symmetric, transitive • Composition of Relations • N-ary Relations • Databases and Relations • More Information

  3. 1. Defining a Relation • A relation connects two (or more) sets. • Two new ideas: • the Cartesian Product (AB) of sets • ordered pairs

  4. Cartesian Product Example • A = {Smith, Johnson} • B = {Calc, Math, History, Programming} B A  Art Smith Math History Johnson Programming the Cartesian Product creates all the possible links from elements in set A to set B

  5. A Set of Ordered Pairs • We can write these links as a set of ordered pairs, one pair for each link: • AxB = { (Smith, Art), (Smith,Math), (Smith,History), ..., (Johnson,History), (Johnston,Programming) } • The ordering of a pair matters: first an element from A, which is linked to an element from B. • A pair is sometimes called a 2-tuple.

  6. Relations • A relation R is defined by a subset of the ordered pairs in AxB. • For example one possible relation is: B A R Art Smith Math History Johnson Programming

  7. R can be written as a set of ordered pairs: • R = { (Smith,Art), (Smith,Math), ... (Johnson, History), (Johnston, Programming) } • R  A x B • We can also write each link as a relation (or predicate): • (Smith R Art) is trueor R(Smith, Art) is true • (Johnston R Art) is falseor R(Johnston,Art) is false yes, there is a link between relations and predicate logic

  8. 2. Relations Using One Set • It's possible to use the same set in a relation • e.g. R  A x A • For example, integer relations are  Z x Z R A A : : -1 -1 0 0 1 1 2 2 : : b = a + 1

  9. A relation involving numerical sets can often be summarized using set notation. • For example: • R = {(a,b) | a  Z, b  Z, b = a+1 } or without the set types: • R = {(a,b) |b = a+1 }

  10. Relations in Python

  11. 3. Properties of a Relation A relation R involving a set S may have special properties: • If (x R x) is true, then Ris reflexive. • If (x R y) is true when (y R x) is true, then Ris symmetric. • If (x R z) is true when (x R y) and (y R z) are true, then Ris transitive. S S R x x R x y y x R x y R R x y z y z R

  12. Properties in Words • Reflexive : "all go to self" • Symmetric: "arrows go both ways" • Transitive: "things which are linked, are linked with one arrow"

  13. Properties as Drawings • If the two sets are the same in a relation (R  S x S), then you can draw the relations as arrows between the things. • e.g. S = {1,2,3,4}, R = {(1, 1), (1, 2), (2, 3), (3, 3), (4, 4)} 1 2 R S S 1 1 2 2 OR 3 3 3 4 4 4

  14. Relation Property Examples • S = {0,1,2,3} R = {(0,3), (2, 3)} Reflexive: no, e.g. no (0,0) Symmetric: no, e.g. no (3,0) Transitive: yes • S = {0,1,2,3} R = {(0,0), (0,1), (0,2), (1,3)} Reflexive: no, e.g. no (1,1) Symmetric: no, e.g. no (3,1) Transitive: no, (0,1) and (1,3) but no (0,3) 0 1 2 3 0 1 2 3

  15. S = {0,1,2,3} R = {(1,2), (2,1), (1,3), (3,1)} Reflexive: no, e.g. no (0,0) Symmetric: yes Transitive: no, (2,1) and (1,3) but no (2,3) • S = {0,1,2,3} R = {(0,0), (1,1), (2,2), (0,1), (1,2), (2,3)} Reflexive: no, e.g. no (3,3) Symmetric: no, e.g. no (2,1) Transitive: no, (0,1) and (1,2) but no (0,2) 0 1 2 3 0 1 2 3

  16. Not Examinable Python Code for Property Testing def isReflexive(R): # all go to self elems = set() for (x,y) in R: # collect all elements elems.add(x) elems.add(y) for el in elems: if (el,el) not in R: return False return True def isSymmetric(R): # all arrows go both ways for (x,y) in R: if (y,x) not in R: return False return True def isTransitive(R): # all things which are linked, # are linked with one arrow for (x,y) in R: for (y1,z) in R: if (y == y1): # x --> y --> z if (x,z) not in R: return False return True def test(R): # test relation for three properties print("R:", R) print("-Reflexive", isReflexive(R)) print("-Symmetric", isSymmetric(R)) print("-Transitive", isTransitive(R)) print("")

  17. Examples print ("- 1 ----------------------") test({(0,3), (2, 3)}) test({(0,0), (0,1), (0,2), (1,3)}) test({(1,2), (2,1), (1,3), (3,1)}) test({(0,0), (1,1), (2,2), (0,1), (1,2), (2,3)}) print ("- 2 ----------------------") test({ (1,1), (2,2), (3,3), (2,1) }) test({ (1,2), (2,1), (3,3) }) test({ (0,1),(1,2),(2,3),(0,2),(1,3),(0,3) }) print ("- 3 -----------------------") nums = {1,2,3,4} test({(x,y) for x in nums for y in nums }) print ("- 4 -----------------------") test({(2, 2), (2, 3), (2, 4), (3, 2), (3, 3), (3, 4)}) test({(1,1),(1,2),(2,1),(2,2),(3,3),(4,4)}) test({(2,4), (4, 2)}) print ("- 5 -----------------------") test({(1, 2), (2, 3), (3, 4)}) test({(1,1),(2,2),(3,3),(4,4)}) test({(1,3),(1,4),(2,3),(2,4),(3,1),(3,4)})

  18. Make sure you understand why the results are true/false. - 2 ---------------------- R: {(3, 3), (1, 1), (2, 1), (2, 2)} -Reflexive True -Symmetric False -Transitive True R: {(1, 2), (3, 3), (2, 1)} -Reflexive False -Symmetric True -Transitive False R: {(0, 1), (1, 2), (1, 3), (2, 3), (0, 3), (0, 2)} -Reflexive False -Symmetric False -Transitive True - 3 ----------------------- R: {(1, 2), (3, 2), (1, 3), (4, 1), (3, 3), (3, 1), (4, 4), (2, 1), (1, 4), (2, 4), (2, 3), (4, 3), (2, 2), (4, 2), (3, 4), (1, 1)} -Reflexive True -Symmetric True -Transitive True - 1 ---------------------- R: {(0, 3), (2, 3)} -Reflexive False -Symmetric False -Transitive True R: {(0, 1), (0, 0), (0, 2), (1, 3)} -Reflexive False -Symmetric False -Transitive False R: {(1, 2), (1, 3), (3, 1), (2, 1)} -Reflexive False -Symmetric True -Transitive False R: {(0, 1), (1, 2), (0, 0), (2, 3), (2, 2), (1, 1)} -Reflexive False -Symmetric False -Transitive False

  19. Make sure you understand why the results are true/false. - 5 ----------------------- R: {(1, 2), (3, 4), (2, 3)} -Reflexive False -Symmetric False -Transitive False R: {(3, 3), (4, 4), (1, 1), (2, 2)} -Reflexive True -Symmetric True -Transitive True R: {(1, 3), (3, 1), (1, 4), (2, 3), (3, 4), (2, 4)} -Reflexive False -Symmetric False -Transitive False - 4 ----------------------- R: {(3, 2), (3, 3), (2, 3), (2, 2), (3, 4), (2, 4)} -Reflexive False -Symmetric False -Transitive True R: {(1, 2), (3, 3), (4, 4), (2, 1), (2, 2), (1, 1)} -Reflexive True -Symmetric True -Transitive True R: {(4, 2), (2, 4)} -Reflexive False -Symmetric True -Transitive False

  20. 4. Composition of Relations • Let R be a relation from a set A to a set B and S a relation from B to a set C. • The composite of R and S is written as SR • read this as "do R then do S" this strange ordering is to do with the connection between relations and functions (see the next part) R S a s 9 2 t 0 : : : d 3 h 4 4 u A B C

  21. Example 1 • Let R and S be relations on Z+= {1, 2, 3, …} R = {(a, b) | b = 2*a} S = {(a, b) | b = a-1} R S 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 : : : Z+ Z+ Z+

  22. We can write SR in terms of the ordered pairs of "start" and "finish" elements: • SR = {(1,1), (2,3), (3,5), (4,7), ... } • remember that  means "do R and then do S" • We can also summarise the composite using set notation: SR = {(a,b) | b = (2*a) – 1 } this combines the maths of set R and S

  23. Example 2 • Let R and S be relations on A = {1, 2, 3, 4} • R = {(a, b) | b = 5 - a} • S = {(a, b) | b > a} • this means that an 'a' can be linked to all 'b's which are bigger S 1 1 1 R 2 2 2 3 3 3 4 4 4 A A A

  24. As a set of ordered pairs: • SR = { (2,4), (3,3), (3,4), (4,2), (4,3), (4,4) } • only "start" and "finish" pairs are included • Using set notation: • S°R = {(a,b) | b > 5 – a} or • S°R = {(a,b) | a + b > 5} this combines the maths of set R and S

  25. 5. N-ary Relations • The Cartesian Product can involve any number of sets. We write it as A1 x A2 x … x An. • The sets A1, A2, …, An are called the domains of the relation, and n is called its degree. for this example, degree == 3 a s 9 2 0 4 h 4 u A1 A2 A3

  26. An n-ary relation R uses a subset of the links in an n-ary Cartesian Product. • e.g. a s 9 2 0 4 h 4 u A1 A2 A3

  27. 6. Databases and Relations • A database can be defined as an n-ary relation • this is known as therelational data model • A 'database' for students: • R = { (Ackermann, 231, CoE, 3.88), (Adams, 888, Physics, 3.45), (Chou, 102, CoE, 3.79), (Goodfriend, 453, Math, 3.45), (Rao, 678, Math, 3.90), (Stevens, 786, Math, 2.99) } • Each tuple is also called a record. called a 4-tuple (because there are 4 values)

  28. Each tuple (record) is made up of values from sets (also called domainsor fields). • e.g. each R tuple contains values from the fields Name, ID, Dept, and GPA • The database R is an n-ary relation: 001 0.01 CoE 002 0.02 Ackerman : : Math Adams 231 3.45 : : Physics Chou 888 3.88 = 1 tuple(or record) Chem : : : : : 4.00 Name ID Dept GPA 4 fields (domains) primary key

  29. Primary Key • A field is called a primary key if the relation's tuples are uniquely defined by that key's values • no two records can have the same primary key value • e.g. ID is the primary key in the R relation. • e.g. If you know ID == 102 then you know which tuple is wanted. • But if you know Dept == Math then you don't know which tuple is wanted.

  30. 7. More Information • Discrete Mathematics and its ApplicationsKenneth H. RosenMcGraw Hill, 2007, 7th edition • chapter 9, sections 9.1 – 9.2

More Related