Relations - PowerPoint PPT Presentation

About This Presentation
Title:

Relations

Description:

Let R:AB, and S:BC. Then the composite S R of R and S is defined as: S R = {(a,c) | aRb bSc} ... R be an n-ary relation and C a condition that elements in R may ... – PowerPoint PPT presentation

Number of Views:59
Avg rating:3.0/5.0
Slides: 43
Provided by: michae1480
Learn more at: https://www.cse.unr.edu
Category:
Tags: relations

less

Transcript and Presenter's Notes

Title: Relations


1
Relations
  • Rosen 5th ed., ch. 7

2
Relations
  • Relationships between elements of sets occur very
    often.
  • (Employee, Salary)
  • (Students, Courses, GPA)
  • We use ordered pairs (or n-tuples) of elements
    from the sets to represent relationships.

3
Binary Relations
  • A binary relation R from set A to set B, written
    RA?B, is a subset of AB.
  • E.g., let lt N?N (n,m) n lt m
  • The notation aRb means (a,b)?R.
  • E.g., a lt b means (a,b)? lt
  • If aRb we may say
  • a is related to b (by relation R)

4
Example
  • A students at UNR, B courses offered
    at UNR
  • R relation of students enrolled in
    courses
  • (Jason, CS365), (Mary, CS201) are in R
  • If Mary does not take CS365, then (Mary, CS365)
    is not in R!
  • If CS480 is not being offered, then (Jason,
    CS480), (Mary, CS480) are not in R!

5
Complementary Relations
  • Let RA?B be any binary relation.
  • Then, RA?B, the complement of R, is the binary
    relation defined by R (a,b) (a,b)?R
    (AB) - R
  • Note this is just R if the universe of discourse
    is U AB thus the name complement.
  • Note the complement of R is R.

Example lt (a,b) (a,b)?lt (a,b) altb

6
Inverse Relations
  • Any binary relation RA?B has an inverse relation
    R-1B?A, defined by R-1 (b,a) (a,b)?R.
  • E.g., lt-1 (b,a) altb (b,a) bgta gt.

7
Functions as Relations
  • A function fA?B is a relation from A to B
  • A relation from A to B is not always a function
    fA?B (e.g., relations could be one-to-many)
  • Relations are generalizations of functions!

8
Relations on a Set
  • A (binary) relation from a set A to itself is
    called a relation on the set A.

A 1,2,3,4 R(a,b) a divides b
9
Example
  • How many relations are there on a set A with n
    elements?

10
Reflexivity
  • A relation R on A is reflexive if ?a?A, aRa.
  • E.g., the relation (a,b) ab is
    reflexive.
  • Is the divide relation on the set of positive
    integers reflexive?

11
Symmetry Antisymmetry
  • A binary relation R on A is symmetric iff R
    R-1, that is, if (a,b)?R ? (b,a)?R.
  • E.g., (equality) is symmetric. lt is not.
  • is married to is symmetric, likes is not.
  • A binary relation R is antisymmetric if (a,b)?R
    ? (b,a)?R.
  • lt is antisymmetric, likes is not.

12
Transitivity
  • A relation R is transitive iff (for all
    a,b,c) (a,b)?R ? (b,c)?R ? (a,c)?R.
  • Examples
  • is an ancestor of is transitive.
  • Is the divides relation on the set of positive
    integers transitive?

13
Combining Relations
  • Two relations can be combined in a similar way to
    combining two sets.

14
Composite Relations
  • Let RA?B, and SB?C. Then the composite S?R of
    R and S is defined as
  • S?R (a,c) aRb ? bSc
  • Function composition f?g is an example.
  • The nth power Rn of a relation R on a set A can
    be defined recursively by
  • R1 R Rn1 Rn?R for all n0.

15
Example
  • R is a relation from 1,2,3 to 1,2,3,4
  • R (1,1),(1,4),(2,3),(3,1),(3,4)
  • S is a relation from 1,2,3,4 to 0,1,2
  • S (1,0),(2,0),(3,1),(3,2),(4,1)
  • R?S (1,0),(1,1),(2,1),(2,2),(3,0),(3,1)

16
7.2 n-ary Relations
  • An n-ary relation R on sets A1,,An, written
    RA1,,An, is a subset R ? A1
    An.
  • The degree of R is n.
  • Example R consists of 5-tuples (A,N,S,D,T)
  • A airplane flights, N flight
    number,
  • S starting point, D destination, T
    departure time

17
Databases
  • The time required to manipulate information in a
    database depends in how this information is
    stored.
  • Operations add/delete, update, search, combine
    etc.
  • Various methods for representing databases have
    been developed.
  • We will discuss the relational model.

18
Relational Databases
  • A database consists of records, which are
    n-tuples, made up of fields.
  • A relational database represents records as an
    n-ary relation R.
  • (STUDENT_NAME, ID, MAJOR, GPA)
  • Relations are also called tables (e.g.,
    displayed as tables often)

19
Relational Databases
  • A domain Ai of an n-ary relation is called
    primary key when no two n-tuples have the same
    value on this domain (e.g., ID)
  • A composite key is a subset of domains Ai, Aj,
    such that an n-tuple (,ai,,aj,) is
    determined uniquely for each composite value (ai,
    aj,)?AiAj

20
Selection Operator
  • Let R be an n-ary relation and C a condition that
    elements in R may satisfy.
  • The selection operator sC maps any (n-ary)
    relation R on A to the n-ary relation of all
    n-tuples from R that satisfy C.

21
Example
  • Suppose we have a domain A StudentName
    Standing SocSecNos
  • Suppose we define a certain condition on A,
    UpperLevel(name,standing,ssn) (standing
    junior) ? (standing senior)
  • Then, sUpperLevel is the selection operator that
    takes any relation R on A (database of students)
    and produces a relation consisting of just the
    upper-level classes (juniors and seniors).

22
Projection Operators
  • The projection operator maps the
  • n-tuple
  • to the m-tuple
  • In other words, it deletes n-m of the components
    of an n-tuple.

23
Example
  • Note that fewer rows may result when a projection
    is applied !

24
More Examples
  • Suppose we have a ternary (3-ary) domain
    CarsModelYearColor. (note n3).
  • Consider the index sequence ik 1,3. (m2)
  • Then the projection P simply maps each tuple
    (a1,a2,a3) (model,year,color) to its image
  • This operator can be usefully applied to a whole
    relation R?Cars (database of cars) to obtain a
    list of model/color combinations available.

ik
25
Join Operator
  • Puts two relations together to form a sort of
    combined relation.
  • If the tuple (A,B) appears in R1, and the tuple
    (B,C) appears in R2, then the tuple (A,B,C)
    appears in the join J(R1,R2).
  • A, B, C can also be sequences of elements rather
    than single elements.

26
Example
27
Join Example
  • Suppose R1 is a teaching assignment table,
    relating Professors to Courses.
  • Suppose R2 is a room assignment table relating
    Courses to Rooms,Times.
  • Then J(R1,R2) is like your class schedule,
    listing (professor,course,room,time).

28
SQL Example
  • SELECT Departure_Time
  • FROM Flights
  • WHERE destinationDetroit
  • Find projection P5 of the selection of 5-tuples
    that satisfy the constraint destinationDetroit

29
7.3 Representing Relations
  • Some ways to represent n-ary relations
  • With an explicit list or table of its tuples.
  • With a function from the domain to T,F.
  • Or with an algorithm for computing this function.
  • Some special ways to represent binary relations
  • With a zero-one matrix.
  • With a directed graph.

30
Using Zero-One Matrices
  • To represent a relation R by a matrix MR
    mij, let mij 1 if (ai,bj)?R, else 0.
  • E.g., Joe likes Susan and Mary, Fred likes Mary,
    and Mark likes Sally.
  • The 0-1 matrix representationof that
    Likesrelation

31
Zero-One Reflexive, Symmetric
  • Terms Reflexive, non-Reflexive, symmetric, and
    antisymmetric.
  • These relation characteristics are very easy to
    recognize by inspection of the zero-one matrix.

any-thing
any-thing
anything
anything
any-thing
any-thing
Reflexiveall 1s on diagonal
Non-reflexivesome 0s on diagonal
Symmetricall identicalacross diagonal
Antisymmetricall 1s are acrossfrom 0s
32
Using Directed Graphs
  • A directed graph or digraph G(VG,EG) is a set VG
    of vertices (nodes) with a set EG?VGVG of edges
    (arcs,links). Visually represented using dots
    for nodes, and arrows for edges. Notice that a
    relation RA?B can be represented as a graph
    GR(VGA?B, EGR).

Edge set EG(blue arrows)
GR
MR
Joe
Susan
Fred
Mary
Mark
Sally
Node set VG(black dots)
33
Digraph Reflexive, Symmetric
  • It is extremely easy to recognize the
    reflexive/irreflexive/ symmetric/antisymmetric
    properties by graph inspection.

?
?
?
?
?
?
?
?
?
?
?
ReflexiveEvery nodehas a self-loop
IrreflexiveNo nodelinks to itself
SymmetricEvery link isbidirectional
AntisymmetricNo link isbidirectional
Asymmetric, non-antisymmetric
Non-reflexive, non-irreflexive
34
7.4 Closures of Relations
  • For any property X, the X closure of a set A is
    defined as the smallest superset of A that has
    the given property.
  • The reflexive closure of a relation R on A is
    obtained by adding (a,a) to R for each a?A.
    I.e., it is R ? IA
  • The symmetric closure of R is obtained by adding
    (b,a) to R for each (a,b) in R. I.e., it is R ?
    R-1
  • The transitive closure or connectivity relation
    of R is obtained by repeatedly adding (a,c) to R
    for each (a,b),(b,c) in R.
  • I.e., it is

35
Paths in Digraphs/Binary Relations
  • A path of length n from node a to b in the
    directed graph G (or the binary relation R) is a
    sequence (a,x1), (x1,x2), , (xn-1,b) of n
    ordered pairs in EG (or R).
  • An empty sequence of edges is considered a path
    of length 0 from a to a.
  • If any path from a to b exists, then we say that
    a is connected to b. (You can get there from
    here.)
  • A path of length n1 from a to a is called a
    circuit or a cycle.
  • Note that there exists a path of length n from a
    to b in R if and only if (a,b)?Rn.

36
Simple Transitive Closure Alg.
  • A procedure to compute R with 0-1 matrices.
  • procedure transClosure(MRrank-n 0-1 mat.)
  • A B MR
  • for i 2 to n begin A A?MR B B ? A
    joinendreturn B Alg. takes T(n4) time

note A represents Ri
37
Roy-Warshall Algorithm
  • Uses only T(n3) operations!
  • Procedure Warshall(MR rank-n 0-1 matrix)
  • W MR
  • for k 1 to n for i 1 to n for j 1 to
    n wij wij ? (wik ? wkj)return W this
    represents R

wij 1 means there is a path from i to j going
only through nodes k
38
7.5 Equivalence Relations
  • An equivalence relation (e.r.) on a set A is
    simply any binary relation on A that is
    reflexive, symmetric, and transitive.
  • E.g., itself is an equivalence relation.
  • For any function fA?B, the relation have the
    same f value, or f (a1,a2) f(a1)f(a2)
    is an equivalence relation, e.g., let mmother
    of then m have the same mother is an e.r.

39
Equivalence Relation Examples
  • Strings a and b are the same length.
  • Integers a and b have the same absolute value.
  • Real numbers a and b have the same fractional
    part (i.e., a - b ? Z).

40
Equivalence Classes
  • Let R be any equiv. rel. on a set A.
  • The equivalence class of a, aR b aRb
    (optional subscript R)
  • It is the set of all elements of A that are
    equivalent to a according to the eq.rel. R.
  • Each such b (including a itself) is called a
    representative of aR.
  • Since f(a)aR is a function of a, any
    equivalence relation R be defined using aRb a
    and b have the same f value, given that f.

41
Equivalence Class Examples
  • Strings a and b are the same length.
  • a the set of all strings of the same length
    as a.
  • Integers a and b have the same absolute value.
  • a the set a, -a
  • Real numbers a and b have the same fractional
    part (i.e., a - b ? Z).
  • a the set , a-2, a-1, a, a1, a2,

42
Partitions
  • A partition of a set A is the set of all the
    equivalence classes A1, A2, for some e.r. on
    A.
  • The Ais are all disjoint and their union A.
  • They partition the set into pieces. Within
    each piece, all members of the set are equivalent
    to each other.
Write a Comment
User Comments (0)
About PowerShow.com