Sunteți pe pagina 1din 14

1

Relations
Professor J. F. Baldwin
Binary Relation
Definition of Binary Relation R
Given sets S and T, a binary relation on is a subset of

S T S T
If is a binary relation then x y (x, y)
Example:
S = {1, 2}, T = {2, 3, 4}, is: x + y is odd, then
(1, 2) , (1, 4) and (2, 3)
Definition of a Binary Relation, R, on a set S
A binary relation on S is a subset of
i.e. a set of ordered pairs of members of S

S S S =
2
R S S
R S T
2
Matrix and Digraphs for Binary
Relations
Consider R = {(1, 1), (1, 2), (2, 1), (2, 2), (3, 2)}
defined on S
2
where S = {1, 2, 3}
Matrix Representation
1 1 0
1 1 0
0 1 0

element in ith row and jth column = 1


if (ith element of S, jth element of S) is in R
otherwise 0.
Digraph Representation
1
2 3
n-ary Relation
Definition of an n-ary Relation R
Give sets S1, S2, , Sn a relation on is a subset of
. i.e.
A unary relation is a special case and is a relation on S which is a
subset of S.
An n-ary relation on S is a relation on
S1 S Sn 2 ...
S1 S Sn 2 ... R S1 S Sn 2 ...
S S S = S
n
...
Databases are relations and are important for storing information
in Computers. Relational databases are used to store and process
huge volumes of data efficiently.
3
Databases
Course Num Section Num Instructor Course Title
CS 105 10 Levy Into. Comp. Sc
CS 105 20 Khalil Into. Comp. Sc
CS 105 30 Cassel Into. Comp. Sc
CS 170 10 Carberry Comp. Sc. 1
CS 170 20 Leathrum Comp. Sc. 1
CS 240 10 Levy Discrete Str.
CS 240 20 Slutzki Discrete Str.
R1
Sets : Course Num, Section Num, Instructor, Course Title - attributes
Each row is a member of the set R1
The order of the rows is not important
Projection
Let Rn be a n-ary relation, . The projection
of Rn onto is a m-ary relation, obtained from
Rn by choosing the i
1
th, i
2
th, , i
m
th components of each
element of Rn and is denoted by Rn
Si1

Sim

R S1 S Sn 2 ...
Si Si Si
1
m

2
...
R1course num, course title = R2 =
Course Num Course Title
CS105 Int. Comp. Sc
CS170 Comp. Sc. 1
CS240 Discrete Str
Note: there must not be any repeated rows.
4
Database projection onto the first
three attributes to give R3
Course Num Section Num Instructor
CS 105 10 Levy
CS 105 20 Khalil
CS 105 30 Cassel
CS 170 10 Carberry
CS 170 20 Leathrum
CS 240 10 Levy
CS 240 20 Slutzki
R3
We can denote this relation as
R(Course Num, Section Num, Instructor)
Composition and Join
Let and then the composition
is defined as
R A B S B C S R
S R={(x,y)| z: xRz zSy}
Example:
A = {a1, a2}, B = {b1, b2, b3}, C = {c1, c2}
R = {(a1, b1), (a2, b3)}, S = { (b1, c2), (b3, c1), (b3, c2)}
= {(a1, c2), (a2, c1), (a2, c2)} S R
Join: If R(A, B, C) and S(C, D, E) then we can form
the join of R and S over C to give a relation T(A, B, C, D, E)
in a similar way to composition but we keep all columns.
Example:
Join R2 and R3 over Course Num gives R1
5
Select and Relational Algebra
The select operation creates a new table with those rows of
the original table satisfying a certain property
Example:
Select R3 where Instructor = Levy to give R4
Course Num Section Num Instructor
CS 105 10 Levy
CS 240 10 Levy
A relational algebra is a theoretical database language in which
select, project and join operations can be combined.
An example of relational algebra
Consider we have databases
Person(Name, Address, City, State)
Pet(Pet-name, Pet-type, Breed)
Owns(Name, Pet-name)
Join Pet and Owns over Pet-name
= Pet-Owner(Name, Pet-name, Pet-type, Breed)
Query:
Give the names of all cats whose owners live in Bristol
Solution =
Project(Join(Select Pet-Owner where Pet-type = cat) and
(select Person where City = Bristol) over Name) over Pet-name
6
Properties of Binary Relations
Let be a binary relation on set S
.
is reflexive means ( x)(x S (x,x)
is symmetric means ( x)( y)(x S y S (x,y) (y,x)
is transitive means ( x)( y)( z)(x S y S z S
((x,y) (y,z) (x,z)



) )
is antisymmetric means ( x)( y)(x S y S (x,y)
(y,x) x=y



is asymmetric means ( x)( y)(x S y S (x,y) (y,x)


Relations as Predicates.
Special Relations
Consider the predicate P, x < y , x, y N

Let R ={(x, y) | x < y x y } N N
Predicate P defines relation R
___________
-1
of means {(y, x) | (x, y) }
Inverse Relation
Indentity Relation
The identity relation on A is defined as
I {(a,a) | a A}
A
=
The Universal Relation
Defined on A as
I {(a,b) | a A, b A}
U
=
7
Domains and Ranges
The Domain and Range of a Binary
relation are defied as:
R A B
Dom(R)={x|(x,y) R}
Ran(R)={y|(x,y) R}
It is easy to show that
Dom(R
-1
) = Ran(R)
Ran(R
-1
) = Dom(R)
Chess Board Example
Rows labelled
R ={a, b, c, d, e, f, g, h}
Columns labelled
C = {1, 2, 3, 4, 5, 6, 7, 8}
Let S=R C
Rook moves relation
defined by
Rm S S
Rm={((r1,c1)(r2,c2)) | (r1,r2 R) (c1,c2) C
((r1=r2) (c1 c2)) ((r1 r2) (c1=c2))}


Equivalence Relations
An Equivalence relation is one which is:
reflexive, symmetric and transitive
Example:
a
b
d
e
f
c
A = {a, b, c, d, e, f}
R = {(a,b), (b,a),
(a,d), (d,a)(a,a)
(b,b), (d,d), (b,d), (d,b)(c,c)
(e,f), (f,e), (e,e), (f,f)}
reflexive, transitive
symmetric
R partitions A into {{a, b, c}, {c}, {e, f}}
Equivalence relations
partition the set on
which it is defined.
8
Equivalence Classes
A = {1, 2, 3, 4, 5, 6}, A A
={(x,y)| x A, y A, (x-y) is divisible by 2}
Properties of
reflexive: (x, x)
symmetric: (x-y) divisible by 2 (y-x) is divisible by 2
transitive: if (x-y) and (y-z) are divisible by 2 then there exists
integers k and m such that
(x-y) = 2k and (y-z) = 2m
But (x-z) = (x-y) + (y-z) = 2(k + m)
so that (x-z) is divisible by 2.
The Equivalence classes of are:
[1] = {x | xA (x-1) is divisible by 2} = {1, 3, 5}
[2] = {x | xA (x-2) is divisible by 2} = {2, 4, 6}
Forms
Partition
Equivalence Classes for the Set
of all fractions
Let S = {a/b | a, b Z, b 0} i.e. the set of all fractions
a/bc/d iff ad = bc i.e. a/b equivalent to c/d, eg 1/2 2/4
The binary relation on S is
reflexive since ab = ba so that a/b ___
symmetric since a/b ___ then ad= bc so that cb = da so that
___ ___
__________ since if a/b c/d and c/d e/f then ad=bc and cf=d
so that adf=bcf and bcf=bde. Therefore adf=bde so that af=be.
Thus a/b e/f
1
2
{...,
3
6
,
2
4
,
1
2
,
1
2
,
2
4
,
3
6
,...}

is an equivalence class for


the set of fractions.
9
Partial Orderings
A binary relation on set S that is reflexive, antisymmetric
and transitive is called a partial ordering.
The pair (S, ) is called a partially ordered set or a posit.
We will denote an arbitrary posit by (S, )
Examples:
On N, = {(x, y) | x y}
On P(N), = {(A, B) | }
On N, = {(x, y) | x divides y}
A B
If (S, ) is a posit and
, then (A, ) is a posit. A S
{1, 2, 3, 6, 12, 18} , so that x divides y is
a partial ordering on {1, 2, 3, 6, 12, 18} and
On {1, 2, 3, 6, 12, 18}, = {(x, y) | x divides y}
N
Least upper bound (lub) and
Greatest lower bound (glb)
Let (S, ) be a partially ordered set. If x y then x < y or x = y.
If x < y then we say x is a predecessor of y or y is a successor
of x. If x < y but there is no z with x < z< y then x is an
immediate predecessor of y.
u is an upper bound of x and y iff x u and y u
l is a lower bound of x and y iff l x and l y
y S is a greatest element of S if x y for all x S
y S is a maximal element of S if there is no x S with y < x
y S is a least element of S if y x for all x S
y S is a minimal element of S if there is no x S with x < y
A greatest element is a lub or supremum
A least element is a glb or infimum
A maximum element is not necessarily a greatest element, a
minimum element is not always a least element.
10
Hasse Diagrams
If S is finite and (S, ) is a poset we can depict by a graph
called a Hasse diagram. Each element of S is depicted by a
node of the graph. If x is an immediate predecessor of y, then
node y is placed above node x and the two nodes are connected
by a straight line.
Example:
On P({1, 2}), = {(A, B) | } = {(, ), ({1}, {1}), ({2}, {2}),
({1, 2}, {1, 2}), (, {1}), (, {2}), (, {1, 2}), ({1}, {1, 2}),
({2}, {1, 2})}
A B
{1, 2}
{2}
{1}

{1, 2) lub
glb
Relation x divides y
on {1, 2, 3, 6, 12, 18}
1
2 3
6
12 18
12 and 18 are both maximal
but there is no greatest
1 is both minimal and least
glb.
TOTAL ORDERING
A partial ordering in which every element is related to every other
element is a total ordering or chain.
.
.
11
Closure of Relations
If a relation on S fails to have a certain property, we may be
able to extend to * on S that does have that property. By
extend we mean * will contain all ordered pairs of + extra
ordered pairs so that the property is satisfied.
Definition of closure of a relation
A binary relation * on a set S is the closure of a relation on S
with respect to property P if
* has property P
(2)
(3) * is a subset of any other relation on S that includes and
has property P
*
Examples: reflexive closure, symmetric closure, transitive closure
An Example of closures
S = {1, 2, 3}, = {(1, 1), (1, 2), (1, 3), (3, 1), (2, 3)}
Closure with respect to reflexivity :
= {(1, 1), (1, 2), (1, 3), (3, 1), (2, 3), (2, 2), (3, 3)}
Closure with respect to symmetry:
= {(1, 1), (1, 2), (1, 3), (3, 1), (2, 3), (2, 1), (3, 2)}
Closure with transivity:
= {(1, 1), (1, 2), (1, 3), (3, 1), (2, 3)}
Add (3, 2) because of (3, 1) and (1, 2), Add (3, 3) from (3, 1) and (1, 3)
Add (2, 1) from (2, 3) and (3, 1) to give
{(1, 1), (1, 2), (1, 3), (3, 1), (2, 3), (3, 2), (3, 3), (2, 1)}
Add (2, 2) because of (2, 3) and (3, 2)
= {(1, 1), (1, 2), (1, 3), (3, 1), (2, 3), (3, 2), (3, 3), (2, 1), (2, 2)}
must be done iteratively
12
Transitive Closure and
Composition
The process of finding the transitive relation we used on
previous slide is equivalent to:
Let
* =
(2)

(k)
where k is smallest integer such that P is satisfied.
Example:
S = {1, 2, 3}, = {(1, 1), (1, 2), (1, 3), (3, 1), (2, 3)}

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

(2)
=

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

(2)

(3)
=
{(1, 1), (1, 2), (1, 3), (3, 1), (2, 3), (3, 2), (3, 3), (2, 1), (2, 2)}
.
(2) (3)
= = etc ,
Transivity property satisfied.
Task Scheduling using Partial
Ordering and Hasse Diagrams.
Ernie and his brothers run a wood working shop and
manufacture rocking chairs with padded seats. The
manufacturing process can be broken down into a number
of tasks, some of which have other tasks as prerequisites.
Table of tasks with times to complete each task and
prerequisites required are shown on next slide.
We want to calculate critical path for the manufacturing
process and the total time required for making a rocking
chair.
13
Manufacturing Data
1. Selecting wood None 3.0
2. Carving rockers 1 4.0
3. Carving seat 1 6.0
4. Carving back 1 7.0
5. Carving arms 1 3.0
6. Selecting fabric None 1.0
7. Sewing cushion 6 2.0
8. Assembling back & seat 3, 4 2.0
9. Attaching arms 5, 8 2.0
10. Attaching rockers 2, 8 3.0
11. Varnishing 9, 10 5.0
12. Adding cushion 7, 11 0.5
TASK Prerequisite Tasks Hours to Perform
Pert Diagram - Equivalent to
Hasse Diagram
x y task x = task y OR task x is prerequisite of task y
x < y task x is prerequisite of task y
is reflexive, antisymmetric and transitive
Therefore partial ordering.
Draw Hasse Diagram - from left to right rather than bottom
to top.
2(4.0)
10(3.0)
3(6.0)
4(7.0)
5(3.0)
8(2.0)
9(2.0)
11(5.0)
12(0.5)
6(1.0)
7(2.0)
1(3.0)
14
Critical Path Calculation
Task Time from beginning to complete each task
Task 1: 3.0
Task 2: 3.0 + 4.0 = 7.0
Task 3: 3.0 + 6.0 = 9.0
Task 4: 3.0 + 7.0 = 10.0
Task 5: 3.0 + 3.0 = 6.0
Task 6: 1.0
Task 7: 1.0 + 2.0 = 3.0
Task 8: max(9.0, 10.0) + 2.0 = 12.0
Task 9: max(6.0, 12.0) + 2.0 = 14.0
Task 10: max(7.0, 12.0) + 3.0 = 15.0
Task 11: max(14.0, 15.0) + 5.0 = 20.0
Task 12: max(3.0, 20.0) + 0.5 = 20.5
We can mark
path corresponding
to max - see next slide
Total time
= 20.5
3.0
Critical Path
2(4.0)
10(3.0)
3(6.0)
4(7.0)
5(3.0)
8(2.0)
9(2.0)
11(5.0)
12(0.5)
6(1.0)
7(2.0)
1(3.0)
7.0
9.0
10.0
6.0
15.0
12.0
14.0
20.0
20.5
3.0
1.0
critical path : 1 -> 4 -> 8 -> 10 -> 11 -> 12
Work forwards for max costs
Backwards for critical path

S-ar putea să vă placă și