Lecture 7
Lecture 7
1
Composite relation: R S
Let A,B,C be sets and let R be a relation from A to B and let
S be a relation from B to C. Then R and S give rise to a
relation from A to C denoted by
a ( R S )c if for some b B we have (a,b) R and (b,c) S
Example:
R {(1,1), (1,4), (2,3), (3,1), (3,4)}
S {(1,0), (2,0), (3,1), (3,2), (4,1)}
S R {(1,0), (1,1), (2,1), (2,2), (3,0), (3,1)}
2
n
Power of relation: R
1 n 1 n
R R R R R
4
Reflexive Closure
• Example: Consider the relation
R = {(1,1), (1,2), (2,1), (3,2)} on set {1,2,3}
– Is it reflexive?
5
Reflexive Closure – cont.
• When a relation R on a set A is not reflexive:
• The reflexive closure of R can be formed by adding all of the
pairs of the form (a,a) to R. In other words we should find:
R R {(a, a ) | a A}
6
Reflexive Closure – Cont.
• The diagonal relation on A is:
= {(a,a) | a A}.
• The reflexive closure of R is then:
R .
1 2 3 1 2 3
1 1 1 0 1 1 1 0
R= R =(r)
2 1 0 0 2 1 1 0
3 0 1 0 3 0 1 1
a (aRa)
R = { (1,1), (1,2), (2,1), (3,2) } U { (1,1), (2,2), (3,3) }
7
Symmetric Closure (optional)
• Example: Consider
R ={(1,1), (1,2), (2,2), (2,3), (3,1), (3,2)}
– R is not symmetric; the pairs missing are:
(2,1), (1,3).
– If we add those, we obtain the new relation:
{(1,1), (1,2), (1,3), (2,1), (2,2), (2,3), (3,1), (3,2)}.
The new relation is symmetric.
8
Symmetric Closure (optional)
• When a relation R on a set A is not symmetric:
• The inverse of R is:
R-1 = {(b,a) | (a, b) R}.
• The symmetric closure of R is then:
R R-1.
1 2 3 1 2 3 1 2 3
1 1 1 0 1 1 0 1 1 1 1 1
R= R =
-1
RR =-1
2 0 1 1 2 1 1 1 2 1 1 1
3 1 1 0 3 0 1 0 3 1 1 0
11
Example
12
Paths in Relations
• There is a path from a to b in R if there is a sequence
of elements: x1, x2, …, xn-1 with (a,x1)R, (x1,x2)R, …,
(xn-1, b)R.
• Example. R = {(1,3), (1,4), (2,1), (3,2)}
– a path in R (len. 2) from 1 to 2: (1,3), (3,2)R.
– a path in R (len. 3) from 3 to 4: (3,2), (2,1), (1,4)R.
– There is no path in R from 4 to 1, nor to 2, nor to 3.
13
Transitive Closure
• Definition: Let R be a relation on a set A.
The connectivity relation is the relation R*
defined as:
R* ={(a,b) | a path in R from a to b}.
• From the above definition of R* we can write
as:
R* = k=1 Rk.
• R(t) = R R2 R3 … R|A|. (No path can be
longer than |A|, the number of elements in A.)
14
Transitive Closure – Example 1
1 2 3
1 0 1 0 1 All paths of
R= length 1
2 0 0 1
3 1 1 1 2 3
1 2 3
1 0 0 1 1 All paths of
R =
2
2 1 1 1 length 2
3 1 1 1
2 3
1 2 3
1 1 1 1
1 All paths of
R×R = R =
2 3
2 1 1 1
length 3
3 1 1 1
2 3
1 2 3
1 1 1 1
RR R =
2 3
2 1 1 1
3 1 1 1
15
Transitive Closure – Example 2
1 2 3
1 0 0 0 1
R= All paths of
2 0 0 1
length 1
3 0 1 0 2 3
1 2 3
1 0 0 0 1
R = 2
All paths of
2 0 1 0
length 2
3 0 0 1 2 3
1 2 3
1 0 0 0 1
R×R = R =
2 3
2 0 0 1 All paths of
0 1 0 length 3
3 2 3
1 2 3
1 0 0 0 1
RR R =
2 3
2 0 1 1 Paths of
any length
3 0 1 1 2 3
16
Equivalence Relations
A relation R on a set A is an equivalence relation if R is
reflexive, symmetric, and transitive.
– Equivalence relations are about “equivalence”
– Examples:
• = for integers x=x reflexive
x=yy=x symmetric
x=y y=z x=z transitive
• = for sets A=A reflexive
A=BB=A symmetric
A=B B=C A=C transitive
• Let R be “has same major as” for college students
xRx reflexive: same major as self
xRy yRx symmetric: same major as each other
xRy yRz xRz transitive: same as, same as
same as
Partitions
• DEFINITION: PARTITION OF A SET It is a collection of
nonempty disjoint subsets of S whose union equals S.
• A partition of a set S is
– a set of subsets Si=1,2,…n of S
– such that ni=1 Si = S, Sj Sk = for j k.
• Each Si is called a block (also called an equivalence class)
• Example:
– Suppose we form teams (e.g. for a doubles tennis
tournament) from the set:
{Abe, Kay, Jim, Nan, Pat, Zed}
then teams could be: { {Abe, Nan}, {Kay, Jim}, {Pat, Zed} }
Partitions (continued…)
• Since individual elements can only appear in one
block (Sj Sk = for j k), blocks can be
represented by any element within the block.
e.g. Nan’s Team
Jimmer’s 2011 sweet-16 team
• Formally, [x] = set of all elements related to x and y
[x] iff xRy
e.g. [Nan] represents {Abe, Nan}, Nan’s team
[Abe] represents {Abe, Nan}, Abe’s team
[Jimmer] represents the set of players
who played on BYU’s 2011 sweet-16 team
n-ary Relations
• By an n- ary relation, we mean a set of ordered n-
tuples.
• For nay set S, a subset of the product set Sn is called
n-ary relation on S.
• An n-ary relation R on sets A1,…,An,
written (with signature) R:A1×…×An or R:A1,…,An, is
simply a subset
R A1× … × An.
• The sets Ai are called the domains of R.
• The degree of R is n.
20
Relational data model
n-ary relation R is represented with table
fields
R: Teaching assignments
Professor Department Course-number
records Cruz Zoology 335
Cruz Zoology 412
Farber Psychology 501
Farber Psychology 617
Rosen Comp. Science 518
Rosen Mathematics 575
primary key
(all entries are different)
Konstantin Busch - LSU 21
Relational Model for Databases
• A database is a collection of records manipulated by
computers.
• Database Management Systems (DBMS) are programs that
help users access the information in databases.
• The primary key of a relational table uniquely identifies each
record in the table.
• A DBMS responds to user’s request called as queries i.e. a
query is a request for information from databases.
22
Selection operator:
Selection ( ) Selects a subset of rows from
relation i.e. selects rows that satisfy selection
condition (C).