23
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

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

Embed Size (px)

Citation preview

1

Discrete Maths

• Objectiveto introduce relations, show their

connection to sets, and their use in databases

242-213, Semester 2, 2014-2015

5. Relations

2

Overview1. Defining a Relation2. Relations using One Set3. Properties of a Relation

reflexive, symmetric, transitive

4. Composition of Relations5. N-ary Relations6. Databases and Relations7. More Information

1. Defining a RelationA relation connects two (or more) sets.

Two new ideas:the Cartesian Product (AB) of setsordered pairs

3

Cartesian Product ExampleA = {Smith, Johnson}B = {Calc, Math, History, Programming}

4

Smith

Johnson

Art

Math

History

Programming

A B

the Cartesian Productcreates all the possible links from elements in set A to set B

A Set of Ordered PairsWe 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

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 BR

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) is true(Johnston R Art) is false

or R(Johnston,Art) is false

7

yes, there is alink betweenrelations andpredicate logic

2. Relations Using One SetIt'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

-1

0

Z R:

1

2:

-1

0

Z:

1

2:

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

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

Reflexive ExamplesAre 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??

Transitivity ExamplesAre 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??

4. Composition of RelationsLet 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 SR

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 strangeordering is todo with the connectionbetweenrelations andfunctions (seethe next part)

Example 1Let 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+

45

R

3

S

6

:

1

2

Z+

45

3

6

:

1

2

Z+

45

3

6

:

We can write SR in terms of the ordered pairs of "start" and "finish" elements:SR = {(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:

SR = {(a,b) | b = (2*a) – 1 }

15

this combines the maths of set R and S

Example 2Let 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

As a set of ordered pairs:SR = { (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

5. N-ary RelationsThe 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

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

6. Databases and RelationsA 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)

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 GPAThe 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)

Primary KeyA field is called a primary key if the relation's

tuples are uniquely defined by that key's valuesno two records can have the same primary key value

e.g. ID is the primary key in the R relation.

22

7. More Information• Discrete Mathematics and its Applications

Kenneth H. RosenMcGraw Hill, 2007, 7th edition• chapter 9, sections 9.1 – 9.2

23