Documente Academic
Documente Profesional
Documente Cultură
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
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
{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