Presentation is loading. Please wait.

Presentation is loading. Please wait.

Discrete Maths Objective to introduce relations, show their connection to sets, and their use in databases 242-213, Semester 2, 2014-2015 5. Relations.

Similar presentations


Presentation on theme: "Discrete Maths Objective to introduce relations, show their connection to sets, and their use in databases 242-213, Semester 2, 2014-2015 5. Relations."— Presentation transcript:

1 Discrete Maths Objective to introduce relations, show their connection to sets, and their use in databases 242-213, Semester 2, 2014-2015 5. Relations 1

2 Overview 1. Defining a Relation 2. Relations using One Set 3. Properties of a Relation reflexive, symmetric, transitive 4. Composition of Relations 5. N-ary Relations 6. Databases and Relations 7. More Information 2

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

4 Cartesian Product Example A = {Smith, Johnson} B = {Calc, Math, History, Programming} 4 Smith Johnson Art Math History Programming A B  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. 5

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

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 true or R(Smith, Art)istrue (Johnston R Art)is false or R(Johnston,Art)isfalse 7 yes, there is a link between relations and predicate logic 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 8 0 Z R : 1 2 : 0 Z : 1 2 :

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 } 9

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

11 Reflexive Examples Are the following relations on {1, 2, 3, 4} reflexive? 11 R = {(1, 1), (1, 2), (2, 3), (3, 3), (4, 4)} No. R = {(1, 1), (2, 2), (2, 3), (3, 3), (4, 4)} Yes. R = {(1, 1), (2, 2), (3, 3)} No. 1 2 S 3 4 1 2 S 3 4 R ??

12 Transitivity Examples Are the following relations on {1, 2, 3, 4} transitive? 12 R = {(1, 1), (1, 2), (2, 2), (2, 1), (3, 3)}Yes. R = {(1, 3), (3, 2), (2, 1)}No. R = {(2, 4), (4, 3), (2, 3), (4, 1)} No. 1 2 S 3 4 1 2 S 3 4 R ??

13 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" 13 a 2 A d 4 s t B 3 4 R : : 9 0 C h u : S this strange ordering is to do with the connection between relations and functions (see the next part) this strange ordering is to do with the connection between relations and functions (see the next part)

14 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} 14 1 2 Z+Z+ 4 5 R 3 S 6 : 1 2 Z+Z+ 4 5 3 6 : 1 2 Z+Z+ 4 5 3 6 :

15 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 } 15 this combines the maths of set R and S this combines the maths of set R and S

16 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 16 A A 3 4 R A 1 2 3 4 1 2 3 4 1 2 S

17 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} 17 this combines the maths of set R and S this combines the maths of set R and S

18 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. 18 a 2 A1 4 s A2 4 9 0 A3 h u for this example, degree == 3 for this example, degree == 3

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

20 6. Databases and Relations A database can be defined as an n-ary relation this is known as the relational 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. 20 called a 4-tuple (because there are 4 values) called a 4-tuple (because there are 4 values)

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

22 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. 22

23 7. More Information Discrete Mathematics and its Applications Kenneth H. Rosen McGraw Hill, 2007, 7th edition chapter 9, sections 9.1 – 9.2 23


Download ppt "Discrete Maths Objective to introduce relations, show their connection to sets, and their use in databases 242-213, Semester 2, 2014-2015 5. Relations."

Similar presentations


Ads by Google