290 likes | 629 Views
Discrete Maths. 242-213 , Semester 2, 2013 - 2014. Objective to introduce relations, show their connection to sets, and their use in databases. 5. Relations. Overview. Defining a Relation Relations on a Set Properties of a Relation reflexive, symmetric, transitive
E N D
Discrete Maths 242-213, Semester 2,2013-2014 • Objective • to introduce relations, show their connection to sets, and their use in databases 5. Relations
Overview • Defining a Relation • Relations on a Set • Properties of a Relation • reflexive, symmetric, transitive • Combining Relations as Sets • Composition of Relations • N-ary Relations • Databases and Relations • More Information
1. Defining a Relation • We define a relation as the connection between two (or more) sets. • We need to first define: • the ordered pair • the Cartesian Product for two or more sets
An Ordered Pair • In an ordered pair of elements a and b, denoted (a, b), the ordering of the entries matters. • So and iff a = cand b = d
The Cartesian Product of 2 Sets • The Cartesian Product of sets A and B is the set consisting of all the ordered pairs (a, b),where : • One example Cartesian product is the Euclidean plane, which is the set of all ordered pairs of real numbers • In general
A Definition of Relation • A relationR from a set A to a set B is any subset of the Cartesian product A x B. • Thus, if P = Ax B, , then we say that x is related to y by R and use the notation: xRy
Relation Example • Let A={Smith, Johnson} be a set of students • B={Calc, Discrete Math, History, Programming} is a set of subjects. • Smith takes Calc and History, while Johnson takes Calc, Discrete Math and Programming. • Define relation R as: “student xtakes subject y”. • R={ (Smith, Calc), (Smith, History), (Johnson, Calc), (Johnson, Discrete Math), (Johnson, Programming) } • So Smith R Calc is true, whileJohnson RHistory is false.
2. Relations on a Set • A relation on the set A is a relation from A to A. • In other words, a relation on the set A is a subset of AA • Example: Let A = {1, 2, 3, 4}. Which ordered pairs are in the relation R = {(a, b) | a < b} ? R = { (1,2), (1,3), (1,4), (2,3), (2,4), (3,4) } continued
Graphically: R = { (1,2), (1,3), (1,4), (2,3), (2,4), (3,4) } to 1 1 X X X 2 2 from X X 3 3 X 4 4
No. of Relations in a Set • How many different relations can we define on a set A with n elements? • A relation on a set A is a subset of AA. • How many elements are in AA? • There are n2 elements in AA, so how many subsets (== relations on A) does AA have? • The number of subsets that we can form out of a set with m elements is 2m. Therefore, 2n2 subsets can be formed out of AA. • Answer: We can define 2n2 different relations on A.
3. Properties of a Relation A relation R on a set Smay have special properties: • If , is true, then R is called reflexive. • If is true whenever is true, then R is called symmetric. • If is true whenever are both true, then R is called transitive.
Reflexive Examples • Are the following relations on {1, 2, 3, 4} reflexive? 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.
Transitivity Examples • Are the following relations on {1, 2, 3, 4} transitive? 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.
4. Combining Relations as Sets • Relations are sets, and therefore, we can apply the usual set operations to them. • If we have two relations R1 and R2, and both from a set A to set B, then we can combine them to obtain R1 R2, R1 R2, or R1 – R2. • The result us another relation from A to B.
5. 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 compositeof R and S is the relation consisting of ordered pairs (a, c), where aA, cC, when there is an element bB such that (a, b)R and (b, c)S. • We denote the composite of R and S by SR. • In other words, if relation R contains a pair (a, b) and relation S contains a pair (b, c), then SR contains a pair (a, c).
Example 1 • Let D and S be relations on A = {1, 2, 3, 4} D = {(a, b) | b = 5 - a} “b equals (5 – a)” S = {(a, b) | a < b} “a is smaller than b” D = {(1, 4), (2, 3), (3, 2), (4, 1)} S = {(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)} SD = { (2,4), (3,3), (3,4), (4,2), (4,3), (4,4) } • D maps an element a to the element (5 – a), and afterwards S maps (5 – a) to all elements larger than (5 – a), resulting in S°D = {(a,b) | b > 5 – a} or S°D = {(a,b) | a + b > 5}.
Example 2 • Let X and Y be relations on A = {1, 2, 3, …} X = {(a, b) | b = a + 1} “b equals a plus 1” Y = {(a, b) | b = 3a} “b equals 3 times a” X = {(1, 2), (2, 3), (3, 4), (4, 5), …} Y = {(1, 3), (2, 6), (3, 9), (4, 12), …} XY = { (1,4), (2,7), (3,10), (4, 13), ... } • Y maps an element a to 3a, and afterwards X maps 3a to 3a + 1 XY = {(a,b) | b = 3a + 1}
6. N-ary Relations • To apply relations to databases, we generalize binary relations into n-ary relations. • Let A1, A2, …, An be sets. An n-ary relation on these sets is a subset of A1 x A2 x … x An. • The sets A1, A2, …, An are called the domains of the relation, and n is called its degree.
Example • Let R = {(a, b, c) | (a = 2b) (b = 2c), with a, b, c Z} • What is the degree of R? • The degree of R is 3, so its elements are triples • What are its domains? • Its domains are all the set of integers • Is (2, 4, 8) in R? No • Is (4, 2, 1) in R? Yes
7. Databases and Relations • The database representation based on relations is called therelational data model • A database consists of n-tuples called records, which are made up of fields • These fields are the entriesof the n-tuples • The relational data model represents a database as an n-ary relation • in other words, a database is a set of records
Example • Consider a database of students, whose records are represented as 4-tuples with the fields Student Name, ID Number, Department, and GPA: R = { (Ackermann, 00231455, CoE, 3.88),(Adams, 00888323, Physics, 3.45),(Chou, 00102147, CoE, 3.79),(Goodfriend, 00453876, Math, 3.45),(Rao, 00678543, Math, 3.90),(Stevens, 00786576, Psych, 2.99) } • Relations that represent databases are also called tables, since they are often displayed as tables.
Primary Key • A domain of an n-ary relation is called a primary key if the n-tuples are uniquely determined by the values from this domain. • this means that no two records have the same primary key value • e.g., which of the fieldsStudent Name, ID Number, Department, and GPAare primary keys? • Student Name and ID Number are primary keys, because no two students have identical values in those fields • In a real student database, only ID Number would be a primary key
8. More Information • Discrete Mathematics and its ApplicationsKenneth H. RosenMcGraw Hill, 2007, 7th edition • chapter 9, sections 9.1 – 9.2