38
Relations CSC-2259 Discrete Structures Konstantin Busch - LSU 1

Relations CSC-2259 Discrete Structures Konstantin Busch - LSU1

Embed Size (px)

Citation preview

Relations

CSC-2259 Discrete Structures

Konstantin Busch - LSU 1

Relations and Their Properties

Konstantin Busch - LSU 2

A binary relation from set tois a subset of Cartesian product

A BBA

Example: }2,1,0{A },{ baB

)},2(),,1(),,0(),,0{( babaR A relation:

A relation on set :

Konstantin Busch - LSU 3

A relation on set is a subset ofA AA

Example:

}4,3,2,1{A

)}4,4(),1,4(),4,3(),2,2(),1,2(),2,1(),1,1{(R

Konstantin Busch - LSU 4

Reflexive relation on set :R

RaaAa ),(,

Example: }4,3,2,1{A

)}4,4(),3,4(),3,3(),4,3(),2,2(),1,2(),2,1(),1,1{(R

A

Konstantin Busch - LSU 5

Symmetric relation :R

RabRba ),(),(

Example: }4,3,2,1{A

)}4,4(),3,4(),4,3(),2,2(),1,2(),2,1(),1,1{(R

Konstantin Busch - LSU 6

Antisymmetric relation :R

baRabRba ),(),(

Example: }4,3,2,1{A

)}4,4(),4,3(),2,2(),2,1(),1,1{(R

Konstantin Busch - LSU 7

Transitive relation :R

RcaRcbRba ),(),(),(

Example: }4,3,2,1{A

)}4,2(),4,1(),3,1)(4,3(),3,2(),2,1(),1,1{(R

Konstantin Busch - LSU 8

Combining Relations

))3,3(),2,2{(

)}1,1{(

)}3,3(),2,2(),4,1(),3,1(),2,1(),1,1{(

21

21

21

RR

RR

RR

)}3,3(),2,2(),1,1{(1 R

)}4,1(),3,1(),2,1(),1,1{(2 R

Konstantin Busch - LSU 9

Composite relation:

)}4,3(),1,3(),3,2(),4,1(),1,1{(R

)}1,4(),2,3(),1,3(),0,2(),0,1{(S

)}1,3(),0,3(),2,2(),1,2(),1,1(),0,1{(RS

SbxRxaxRSba ),(),(:),( RS

Example:

RScaScbRba ),(),(),(Note:

Konstantin Busch - LSU 10

Power of relation: nR

RR 1 RRR nn 1

Example: )}3,4(),2,3(),1,2(),1,1{(R

)}2,4)(1,3(),1,2(),1,1{(2 RRR

)}1,4)(1,3(),1,2(),1,1{(23 RRR 334 RRRR

A relation is transitive if an only iffor all

Konstantin Busch - LSU 11

Theorem: RRRn

,3,2,1n

Proof: 1. If part: RR 2

2. Only if part: use induction

We will show that if then is transitive

Konstantin Busch - LSU 12

1. If part: RR 2

R

RRR 2Definition of power:

Definition of composition:RRcaRcbRba ),(),(),(

RR 2

Rca ),(

Assumption:

Therefore, is transitiveR

Konstantin Busch - LSU 13

2. Only if part:

We will show that if is transitive then for all

RRRn 1n

Proof by induction on

Inductive basis:

n

1n

RRR 1It trivially holds

Konstantin Busch - LSU 14

Inductive hypothesis:

RRk Assume that

nk 1for all

Konstantin Busch - LSU 15

Inductive step: RRn 1We will prove

1),( nRbaTake arbitrary

We will show Rba ),(

Konstantin Busch - LSU 16

1),( nRba

RRba n ),(

nRbxRxax ),(),(:

RbxRxax ),(),(:

Rba ),(End of Proof

definition of power

definition of composition

inductive hypothesis RRn

is transitiveR

n-ary relations

Konstantin Busch - LSU 17

An n-ary relation on setsis a subset of Cartesian product

nAAA ,,, 21 nAAA 21

Example: NNN A relation on

All triples of numbers with ),,( cba cba

}),5,2,1(),4,2,1(),3,2,1{( R

Konstantin Busch - LSU 18

Professor Department Course-number

Cruz Zoology 335

Cruz Zoology 412

Farber Psychology 501

Farber Psychology 617

Rosen Comp. Science 518

Rosen Mathematics 575

Relational data model

fieldsR: Teaching assignments

records

primary key(all entries are different)

n-ary relation is represented with tableR

Result of selection operator

Konstantin Busch - LSU 19

Selection operator: )(RsCkeeps all records that satisfy conditionC

Psychology Department : CExample:

Professor Department Course-number

Farber Psychology 501

Farber Psychology 617

)(RsC

Konstantin Busch - LSU 20

Projection operator:

Keeps only the fields of

)(,,, 21RP

miii

miii ,,, 21

Example: )(Department Professor, RP

Professor Department

Cruz Zoology

Farber Psychology

Rosen Comp. Science

Rosen Mathematics

R

Konstantin Busch - LSU 21

Join operator: ),( SRJ k

Concatenates the records of and where the last fields of are the same with the first fields of

R SR

Sk

k

Konstantin Busch - LSU 22

Department Course-number

Room Time

Comp. Science

518 N521 2:00pm

Mathematics 575 N502 3:00pm

Mathematics 611 N521 4:00pm

Psychology 501 A100 3:00pm

Psychology 617 A110 11:00am

Zoology 335 A100 9:00am

Zoology 412 A100 8:00am

S: Class schedule

Konstantin Busch - LSU 23

J2(R,S)Professor Departmen

tCourse Number

Room Time

Cruz Zoology 335 A100 9:00am

Cruz Zoology 412 A100 8:00am

Farber Psychology 501 A100 3:00pm

Farber Psychology 617 A110 11:00am

Rosen Comp. Science

518 N521 2:00pm

Rosen Mathematics 575 N502 3:00pm

Representing Relations with Matrices

Konstantin Busch - LSU 24

10101

01101

00010

},,{ 321 aaaA },,,,{ 54321 bbbbbB

)},(),,(),,(),,(),,(),,(),,{( 53331342321221 babababababaaaR

1a

2a

3a

1b 2b 3b 4b 5b

A

BRM

Relation Matrix

Konstantin Busch - LSU 25

Reflexive relation on set :RRaaAa ),(,

Example: }4,3,2,1{A)}4,4(),3,4(),3,3(),4,3(),2,2(),1,2(),2,1(),1,1{(R

A

11

11

11

111a

2a

3a

4a

1a 2a 3a 4a

Diagonal elements must be 1

Konstantin Busch - LSU 26

11

1

11

111a

2a

3a

4a

1a 2a 3a 4a

Matrix is equal to its transpose:

Symmetric relation :R RabRba ),(),(

TRR MM

Example: }4,3,2,1{A

)}4,4(),3,4(),4,3(),2,2(),1,2(),2,1(),1,1{(R

],[],[ ijMjiM RR

ji,For all

Konstantin Busch - LSU 27

11

1

11

11a

2a

3a

4a

1a 2a 3a 4a

Antisymmetric relation :R

Example: }4,3,2,1{A

],[],[ ijMjiM RR

baRabRba ),(),(

ji

)}4,4(),1,4(),4,3(),1,2(),2,2(),1,1{(R

For all

Konstantin Busch - LSU 28

Union :

010

001

101

RM

001

110

101

SM

011

111

101

SRSR MMMSR

Intersection :SR

000

000

101

SRSR MMM

Konstantin Busch - LSU 29

Composition :

000

011

101

RM

101

100

010

SM

000

110

111

SRRS MMM

RS Boolean matrix product

Konstantin Busch - LSU 30

Power :

001

110

010

RM

010

111

110

2 RRRMMM

RRR 2 Boolean matrix product

Konstantin Busch - LSU 31

Digraphs (Directed Graphs)

)},(),,(),,(),,(),,(),,(),,{( bdbcacdbbbdabaR

a b

d c

Konstantin Busch - LSU 32

Theorem: nRba ),(

if and only ifthere is a path of length from to in

na b R

Konstantin Busch - LSU 33

i

iRRRRR

1

321*

Connectivity relation:

*),( Rba if and only ifthere is some path (of any length) from to in a b R

Konstantin Busch - LSU 34

nRRRRR 321*Theorem:

Proof: if thenfor some

1),( nRba iRba ),(},...,1{ ni

a bxRepeated node

Closures and Relations

Konstantin Busch - LSU 35

Reflexive closure of :

Smallest size relation that containsand is reflexive

R

R

Easy to find

Konstantin Busch - LSU 36

Symmetric closure of :

Smallest size relation that containsand is symmetric

R

R

Easy to find

Konstantin Busch - LSU 37

Transitive closure of :

Smallest size relation that containsand is transitive

R

R

More difficult to find

If and is transitive

Konstantin Busch - LSU 38

is the transitive Closure ofRTheorem: *R

Proof:*R is transitivePart 1:

Part 2: SR S

Then SSR **