Relations - PowerPoint PPT Presentation

1 / 77
About This Presentation
Title:

Relations

Description:

1. Relations and their properties. 2. n-ary relations and their applications ... n : nonnegative integers. The relation Rn is defined inductively as follows: ... – PowerPoint PPT presentation

Number of Views:211
Avg rating:3.0/5.0
Slides: 78
Provided by: chengch
Category:

less

Transcript and Presenter's Notes

Title: Relations


1
Chapter 8
  • Relations

2
Contents
  • 1. Relations and their properties
  • 2. n-ary relations and their applications
  • 3. Representing relations
  • 4. Closure of relations
  • 5. Equivalence relations
  • 6. Partial orderings

3
6.1 Relations and their properties
  • Def. 1 A, B two sets.
  • A (binary) relation from A to B is a subset of
    AxB.
  • (i.e., any subset of (x,y) x Î A and y Î B
    )
  • Notes
  • If R Í AxB, we usually use a R b (or not aRb) to
    denote that (a,b) Î R (or (a,b) Ï R).
  • Binary relations can be generalized to n-ary ( n
    gt 0) relations
  • Ex1 A the set of all students B the set of
    all courses
  • let R (a,b) student a is enrolled in
    course b Í AxB
  • is a binary relation from A to B.
  • Ex2 A the set of all cities B the set of
    all nations.
  • Let R (x,y) city x is located in nation
    y.
  • gt (New York, USA) Î R and Tokyo R Japan, ...

4
Relations on a set
  • Ex3 A 1,2,3,4 Ba,b
  • gt (0,a),(0,b),(1,a),(2,b) is a relation
    from A to B.
  • gt Graphical representation ?
  • gt table (or matrix) representation ?
  • Def. 2 A relation R on (or over) a set A is a
    subset of AxA ( A2).
  • Ex4 A1,2,3,4
  • Let R (a,b) Î A2 a divides b
  • gt R ? graphical and table representation
    ?
  • Ex 5 Let R1,R2,...,R6 be relations on integers Z
    defined as
  • R1 (a,b) a b) R2
    (a,b) a gt b
  • R3 (a,b) a b or a -b R4(a,b) a
    b
  • R5(a,b) a b1 R6(A,B) A B lt 4
  • gt Which relations contain the pairs
  • (1,1), (1,2),(2,1),(1,-1),(2,2)?

5
Examples of relations
  • Ex 6 A n B m
  • gt What is the number of relations from A to B
    ?
  • (i.e., let Rel(A,B) R R is a relation from A
    to B,
  • gt rel(A,B) ? )
  • Sol 1. AxB ____.
  • 2. Since Rel(A,B) 2AxB. Hence
    rel(A,B) ___
  • Ex6' Rel(A,A) relations on A ____

6
Properties of (binary) relations on a set
  • Def 3,4,5 R a binary relation on a set A
  • R is reflexive if aRa for every a Î A.
  • R is irrelexive if (a,a) Ï R for each a Î A.
  • R is symmetric if for every a,b ÎA, (a,b) Î R gt
    (b,a) ÎR.
  • R is asymmetric if for every a,b Î A, (a,b) Î R
    gt (b,a) Ï R.
  • R is antisymmetric if for every a,b Î A, if
    (a,b) Î R and (b,a) Î R, then a b.
  • R is transitive if for every a,b,c Î A, if aRb
    and bRc then aRc.
  • Def
  • R is a partial ordering(p.o.) iff it is ref. sym.
    and transitive.
  • R is a strict partial ordering(s.p.o) if it is
    irref. and trans.
  • R is a preordering iff it is ref. and transitive.
  • R is an equivalence relation iff it is ref. sym.
    and transitive.

7
Example
  • Let B 1,2,3,4
  • R1 (1,1),(1,2),(2,1),(2,2),(3,4),(4,1),(4,4)
  • R2(1,1),(1,2),(2,1)
  • R3(1,1),(1,2),(1,4),(2,1),(2,2),(3,3),(4,1),(4,4
    )
  • R4(2,1),(3,1),(3,2),(4,1),(4,2),(4,3)
  • R5((1,1),(1,2),(1,3),(1,4),(2,2),(2,3),(2,4),(3,
    3),(3,4),(4,4)
  • R6(3,4)
  • Ex 7 Which of these relations are ref. ?
  • Ex 7' Which of these relations are irref. ?
  • Note 1. using graphical representations may help
    determining properties of relations.
  • 2. There are relations which is neither
    ref. nor irref.
  • Ex 8 Is the "divides" relation on integers
    reflexive ?
  • Ex 9 which of the relations on Ex5 are ref. ?

8
Examples
  • Ex10. Which of these relations are symmetric,
    antisymmetric ?
  • Ex10' Which are asymmetric ?
  • What is the graphical characteristic of sym.,
    asym. and antisym. relations ?
  • What is the relation among symmetric,
    antisymmetric and asymmetric properties ?
  • Ex 12 Is the "divides" relation on integers sym.
    ? Is it antisym. or asym. ?
  • Ex13 Which of these relations in Ex 7 are
    transitive. ?
  • Ex13' Which of the relations inEx 7 are
    preordering, p.o., s.p.o.
  • or equivalence?
  • Ex15 is the "divides" relation transitive ?

9
Counting the number of relations
  • A n.
  • 1. relations on A ? (2(n2))
  • 2. reflexive relations on A ? (2 (n2 - n))
  • 3. irreflexive relation on A ? ( sol of 2,
    why?)
  • 4. irref and symmetric relation on A ?
    (2(n(n-1)/2) )
  • 5. symmetric relation on A ? (2(n(n1)/2))
  • 6. asymmetric relations on A ? (3(n(n-1)/2))
  • 7. antisymmetric relations on A ?
    (3(n(n-1)/2) x 2n)
  • 8. transitive relations on A ? (hard!)
  • 9. equivalence relations on A ? (hard!)
  • 10. strict partial ordering on A ? (hard!)
  • 11. preordering on A ? (hard!)
  • 12. partial ordering on A ? (hard!)

10
Combining relations
  • Relations are sets
  • gt All set operations can be applied to
    relations
  • Ex R1 R2 relations on A
  • gt R1 U R2 R1 ? R2 R1 \ R2 and R are defined
    as usual.
  • Binary relation compositions
  • R Í AxB S Í BxC
  • gt RS (the composition of R and S) Í AXC is
    the set
  • (x,z) y Î B s.t. xRy ? ySz
  • Note The textbook use SR (less popular) instead
    of RS to denote the composition.

11
Examples of relation compositions
  • Ex19 A1,2,3 B1,2,3,4 C0,1,2
  • R ÍAXB (1,1),(1,4),(2,3),(3,1),(3,4)
  • S Í BXC (1,0),(2,0),(3,1),(3,2),(4,1) gt
    RS ?
  • Fact Relation composition is associative (i.e.,
    R(ST) (RS)T )
  • Hence we can write RST w/t parentheses.

12
The power of relations
  • Def.7 R a relation on A (i.e., a subset of
    AxA)
  • n nonnegative integers.
  • The relation Rn is defined inductively as
    follows
  • 1. R0 IA def (x,x) x ? A.
  • 2. Rk1 RRk (x,z) y Î R s.t. (x,y) ÎR /\
    (y,z) ÎRk .
  • Hence R2 RR R3RR2 R(RR) (RR)R R2 R
    RRR .
  • in general we have Rn RRRR...R. (n Rs)

13
  • Ex show that If a, b ? 0 then RaRb Rab.
  • Pf by induction on a.
  • basis a 0 gt RaRb IARb for all x, y,
  • (x,z)? IARb
  • iff y (x,y) Î IA /\ (y,z) Î Rb
  • iff (x,x) Î IA /\ (x,z) Î Rb
  • iff (x,z) Î Rb.
  • Ind. step assume RaRb Rab gt
  • Ra1Rb (RRa)Rb R(RaRb) --- (Assoc of
    Relations)
  • RRab Ra1b.
  • Corollary RRk Rk R.

14
Example
  • Ex20 A 1,2,3,4 R(1,1),(2,1),(3,2),(4,3)
    is relation on A.
  • R0, R1, R2, R3, R4,R5, ... ____? (use graphical
    representation)
  • Lemma If Rn Rn1 then Rk Rk-1 for all k gt n.
  • Ex If R5 R6 gt R10 R6R4 R5R4 R9
  • Pf left as an exercise.
  • Corollary If Rn Rn1 gt Rk Rn for all k gt
    n.
  • Pf Rk Rk-1 R(k-1)-1 Rk-2 ...Rn1 Rn.

15
  • Theorem 1 Relation R on a set A is transitive
    iff RnÍ R for all n gt 0.
  • Pf (gt) By induction on n.
  • Basis n 1 gt Rn R1 R R0 R ? R.
  • Ind. step for any pair (x,y)
  • (x,z) Î Rn1
  • gt y s.t. (x,y) Î Rn /\ (y,z) Î R1.
  • But, by ind. hyp., both (x,y) and (y,z) Î R.
  • And by tran. of R, (x,z) Î R.
  • (lt) for any x,y,z, (x,y) Î R /\ (y,z) Î R gt
    (x,z) Î R2 gt (x,y) Î R. QED
  • Corollary R is transitive iff R2 Í R. (why ?)
  • pf the if-part of Theorem 1 uses only the case
    that n 2.

16
6.2 n-ary relations
  • Def. 1 A1,A2,...,An n sets.
  • 1. An n-ary relation R on A1,A2,...,An is any
    subset of A1xA2x...xAn (x1,...,xn) x1 Î
    A1,...,xn Î An .
  • 2. A1,..., An are the domains of R
  • 3. n the degree (or arity) of R.
  • 4. 1-ary (0-ary, 2-ary, 3-ary) relations are
    called unary (nullary, binary, ternary)
    relations.
  • Ex If R Í Z3 ( ZxZxZ) def (x,y,z) x lt y lt
    z, x,y,z ?Z.
  • Then (1,2,3) Î R but (2,4,3) Ï R.

17
Relations and databases
  • Database
  • In a database, lots of information about the real
    world are extracted and stored.
  • Since the database is a "representation" of the
    world, if the real world changes, the database
    should change over time accordingly
  • Issues in database theory (data model)
  • How to represent various information (or data) in
    a database?
  • (i.e., can we have a unifying method or view by
    which we can always (or in most cases) easily
    organize or arrange all data to be stored in the
    database ).
  • Any such method is called a data model.

18
The relational data model
  • A database DB (at a time instant, say, time t)
    consists of a pair ltDom,Relgt where
  • Dom A1,...,An is a set of sets, each Ai is
    called a domain of the database.
  • Rel R1,...,Rm where each Ri ?
    Aj1xAj2x...xAjki is a ki-ary relations on domain
    Aj1,Aj2,...,Ajki.
  • Each Ki-tuple (a1,a2,...,aki) ? Ri is called a
    record (or row) of Ri.
  • Each Ajs ( 1 lt s lt ki) is called a field (or a
    column) of the relation Ri (and the record).
  • Each relation Ri can be represented by a table
    (w/t duplicate rows) with Ri rows and Ki
    columns.

19
Example relations
  • Ex Student records S in a school may be
    represented by 4-tuples of the form
    (student-name, register-id, major, GPA )
  • Let A the set of all student names
  • B the set of all student identifier
    numbers.
  • C the set of all departments names
  • D the set of all possible GPA numbers
    (0,5).
  • gt DB ltA,B,C,D, Sgt where S must be a subset
    of AxBxCxD.
  • A possible value of S may be represented by the
    table
  • NAME ID MAJOR GPA
  • --------------------------------------------------
    -----
  • Allen 23 CS 3
  • Bob 15 math 4.1
  • Chen 34 CS 4.0
  • ...

20
Operations on n-ary relations
  • A domain Bj of an n-ary relation R (in database)
    is called a primary key iff it can be used to
    uniquely identify records in R at all time.
  • (in other words, If R is an n-ary relation on
    domain B1,B2,...,Bn, then for any time t, R(t)
    must be a subset of B1xB2...xBn.
  • Bj is a primary key iff for any time t and for
    any (a1,a2,...,an) and (b1,..,bn) Î R(t), if aj
    bj then (a1,...an) (b1,...,bn).
  • Note R(t) changes over time.
  • Operations on n-ary relations
  • Selection (or restriction) --- find subset of R
    with a certain

  • property.
  • Projections.
  • Joins --- generalization of binary composition
  • and other usual set operations (U, /,
    intersection,...)

21
relation operations
  • Restriction (selection)
  • P(x1,...,xn) a statement (or property) on
    n-tuples
  • (x1,...,xn).
  • R a relation on A1xA2...,xAn.
  • gt RP ( find tuples of R with property P) is
    the set
  • (a1,...,an) (a1,...,an) Î R and
    P(a1,...,an) is true .
  • Ex Let P(x1,x2,x3) " x1 lt x2 lt x3 or x3 2
    " and
  • R (1,2,3),
  • (1,3,2),
  • (2,1,3), gt Rp (1,2,3),
    (1,3,2), (3,1,2) .
  • (2,3,1),
  • (3,1,2), (3,2,1)

22
Projections
  • R a relation on A1,A2,...,An
  • w j1j2...jm is a sequence with 0lt jslt n1 for
    all s 1,..,m.
  • gt Pw(R) the projection of R on Aj1Aj2...Ajm
    is a m-ary relation on Aj1Aj2...Ajm with value
  • (xj1,...,xjm) (x1,...,xn) Î R .
  • Ex R (1,2,3), (1,3,3), (2,3,1), (2,2,2)
  • gt P1(R) 1,1,2,2 1,2
  • P2(R) 2,3
  • P31(R) (3,1),(3,1),(1,2),(2,2)
    (3,1),(1,2),(2,2)
  • P313(R) (3,1,1), (1,2,1),(2,2,2)

23
Joins (combining two relations into one)
  • R A1xA2x...xAm-pxAm-p1x...xAm
  • S B1 xB2x..Bpx
    Bp1x ..xBm
  • p min(m,n).
  • gt Jp(R,S) (a1,...,am-p, c1,c2,...,cp,
    bp1,..., bn)
  • (a1,...,am-p, c1,c2,...,cp ) Î R /\
  • (c1,c2,...,cp, bp1,..., bn) Î S
  • Ex R(1,2,3), (2,2,3), (3,3,3) (6,1,2)
    S(2,3,4), (3,3,3), (2,2,2)
  • gt J2(R,S) ?
  • J1(R,S) ?
  • J3(S,R) ?

24
6.3 Relation representation
  • Three possible ways to represent a finite
    relation
  • a list (or array) of ordered pairs.
  • (0-1) matrix
  • digraph (directed graph)
  • Ex A1,2,3, Ba,b,
  • R (2,a),(3,a),(3,b).
  • 1. list representation
  • ((2,1),(3,1),(3,2)) possibly ordered by
    fields.
  • 2. Matrix (table) 3. Digraph
  • A\B a b
  • 1 0 0
    or
  • 2 1 0
  • 3 1 1

25
Formalization of relation representation
  • Aa1,a2,..,am B b1,...,bn R a binary
    relation from A to B.
  • MR (the matrix representation of R) (mij) mxn
    where
  • mij 1 if (ai,bj) ? R and
  • 0 if (ai,bj) ? R.
  • Ex Aa1,a2,a3 B b1,b2,...,b5.
  • If R1(a1,b1),(a1,b2),(a3,b2),(a3,b5)
  • gt MR1 ?
  • 01000
  • If MR2 10110 gt R2 ?
  • 10101

26
Notes
  • If R ? AxA with A n, gt MR is a nxn 0-1
    matrix.
  • R is ref. (resp. irref. ) iff mii 1 (0) for
    every 1?i ? n.
  • R is symmetric (asym) iff for all i, j mij
    mji (mij /\ mji ? 1)
  • R is tansitive iff for all i,j,k if mijmjk1 gt
    mik 1.

27
0-1 matrix operation
  • Boolean operation on 0,1
  • x ? y max(x,y)
  • x ? y min(x,y)
  • x 1-x
  • Boolean operations on 0-1 matrices
  • (M1 \/ M2) ij M1ij \/ M2ij for all i,j. ---
    pairwise or
  • (M1 /\ M2) ij M1ij /\ M2ij for all i,j. ---
    pairwise and
  • (M1) ij 1- M1ij for all i,j. --
    complement.
  • (M1x M2)ij \/k1,n M1ik /\ M2kj. --- product
  • M0 I Mn1 Mn x M. --- power of 0-1 matrix.

28
Matrix representation for relation operations
  • R1, R2 two relations from A to B.
  • 1. MR1 UR2 MR1 ? MR2.
  • 2. MR1ÇR2 MR1 ? MR2
  • 3. MR1 MR1.
  • R AXB S BxC with
  • A a1,...,am, B b1,...,bn and C
    c1,..,cp.
  • gt RS (ai,ck) bj ? B s.t. (ai,bj) ? R and
    (bj,ck) ? S
  • Hence (MRS)ik 1 iff
  • there is 1 ? j ? n s.t. (aj,bj) ? R and (bj,ck)
    ? S iff
  • there is 1 ? j ? n s.t. (MR)ij 1 /\ (MS)jk
    1 iff
  • \/j1,n (MR)ij /\ (MS)jk 1
  • Hence MRS MR x MS, and MRk (MR)k for any
    k ? 0.

29
Examples
  • Ex3 MR 110 111 011
  • gt Is R ref, symmetric or antisymmetric ?
  • Ex4 R1,R2 two relation on A.
  • MR1 101100010 MR2 101011100.
  • gt MR1UR2 ? MR1Ç R2 ? MR1 ?
  • Ex5 MR 101110000 MS 010 001101
  • gt MRS ?
  • Ex6 MR 010011100
  • gt MR2 (MR)2 ?

30
Representing relations using digraphs
  • Def 1. A diagraph (or directed graph) is a pair
    (V,E) where
  • V is a set, each member is called a node or a
    vertex.
  • E is a set of ordered pairs of nodes. (i.e. E Í
    V2), each member of which is called an edge.
  • If (a,b) ? E is an edge, we say a is the initial
    (or starting) vertex and b the terminal (or
    ending) vertex of the edge.
  • If (a,a) is an edge, we say it is a loop.
  • Ex7 In Fig 3, we have G(V,E) where
  • V a,b,c,d and
  • E (a,b),(a,d),(b,b)(b,d),
  • (c,a),(c,b),(d,b).
  • Note E in fact is a relation on V.

Fig 3 (p 377)
31
Representing relations by digraphs (cont'd)
  • A a1,...,am, B b1,...,bn
  • R a binary relation from A to B
  • gt we can use the digraph G(V,E) to represent
    R, where
  • V A U B.
  • E R.
  • Ex A 1,2,3 B 2, 3,4
  • R (x,y) x gt y ? AXB.
  • gt R ?
  • The digraph GR for R (1,2,3,4, R).
  • gt What is its graphical representation ?

32
6.4 closure of relations
  • R a relation on A
  • P(-) a property (or statement or predicate)
    about relations.
  • eg 1. P(R) " R is reflexive" or
  • 2. P(R) " R is symmetric and
    transitive" or
  • 3. P(R) " R is not finite" etc.
  • Def P a property about relations on A
  • The operation CLp 2AxA -gt 2AxA is defined to
    be the function s.t. for each each relation R
    on A, CLP(R) is the smallest of all relations on
    A which include R and satisfy the property P.
    (i.e., CLp(R) is the relation C on A s.t.
  • 1. R Í C --- including R
  • 2. P(C) is true --- satisfying P
  • 3. For any relation S on A, if it satisfies (1)
    and (2),
  • then C Í S. --- smallest
  • CLp(R) is called the P closure of R.

33
Properties about closure
  • Ex A 1,2,3 R(1,1),(1,2),(2,1),(3,2)
  • Let P(S) means " S is reflexive"
  • gt CLp(R) (reflexive closure of R) ? (exist ?
    unique ?)
  • 1. Let TT T T ÍA2 /\ T is ref. and R Í T.
  • Note by def. every T ? TT satisfies item (1)
    and(2) of the def. of P-closure.
  • gt TT ?
  • 2. Which is the smallest in TT ? ( R U IA !)

34
  • Theorem CLP(R), if existing, is unique.
  • pf Let S and T be any two P-closures of R.
  • gt By minimality (3) of closure S ÍT and T Í
    S,
  • hence S T.
  • 3. Problem Does CLP(R) always exist ? No !
  • Ex R (a,a), (a,b) has no irreflexive
    closure.
  • Why ?
  • Ans Since the irreflexive property and R are
    contradictory.
  • (I.e., No superset of R could be irreflexive. )

35
closure properties
  • Def P a property about relations
  • P is said to be increasing if for each relation
    R, there exists a relation S satisfying P and
    including R.
  • P is said to be closed under intersection(or
    conjunctive) if for any set TT of relations, if
    all R Î TT satisfies P, then so is their
    intersection Ç TT.
  • Ex The following properties are increasing and
    closed under intersection
  • 1. reflexivity 2. symmetry 3. transitivity.
  • Since A2 (the largest relation on A) includes R
  • and satisfies 1,2,3.
  • Theorem If P is increasing and closed under
    intersection, then CLp(R) always exist for all
    relations R (on a universe set A).

36
Example
  • Ex property increasing closed under
    intersection
  • ref. yes
    yes
  • symmetric yes yes
  • transitivity yes yes
  • irref. no(why?)
    yes(why?)
  • asymmetric ? ?
  • antisymmetric ? ?
  • equivalence ? ?
  • partial order ? ?
  • total ordering ? ?
  • ref or symmetric yes no
    (why?)
  • Fact If P is not increasing, then CLp may not
    exist.
  • If P is not conjunctive,then CLp may not exist.

37
reflexive and symmetric closures of relations
  • Theorem 1' R a binary relation on A, then
  • 1. reflexive closure of R (Rref) R U IA.
  • 2. symmetric closure of R (Rsym) R U R-1.
  • Ex if MR 100,101,000, then
  • 1. IA ?
  • 2. MRref ?
  • 3. MR-1 ?
  • 4. MRsym ?.
  • pf Let Tref S S is a relation on A and is
    ref and R Í S.
  • gt 1. (1) RUIA Î Tref (2) S Î T ref gt
    R U IA Í S.
  • Hence R U IA CLref(R).
  • 2. Similar to 1.

38
Transitive closure of relations
  • R a binary relation on A
  • gt What is the transitive closure of R (i.e.,
    the smallest transitive relation S on A including
    R (i.e., if (a,b) and (b,c) Î S, then (a,c) is
    also Î S)).
  • Ex If R (1,2)(2,6)(6,1)(1,3)(3,4)(4,5)
  • gt GR Fig 1.
  • gt MR ?
  • gt GS ?
  • gt MS ?
  • gt S ?
  • The reflexive and transitive closure of R is the
    smallest reflexive and transitive relation on A
    including R.

39
Path
  • Def. 1 G (A,R) a digraph a, b two
    vertices in A.
  • 1. A path of length n (n ? 0) from a to b is a
    sequence
  • x0,x1,...,xn of vertices s.t.
  • x0 a (starting point) and xn b (ending
    point) and
  • (xi, xi1) Î R for all i 0,..,n-1.
  • 2. If x0 xn and n gt 0 gt the path is called
    a cycle ( or circuit).
  • Ex If R (1,2),(2,1),(2,3)(3,4)(4,2)(4,5)
  • gt "1,2,3,4,5" is a path of length ? from ? to
    ?.

40
Correspondence b/t relation and digraph
  • Theorem 1 A m R is binary relation on A
  • (Hence G(A,R) is a digraph)
  • gt for all n ³ 0, (x,y) Î Rn iff a path of
    length n from x to y.
  • pf by induction on n.
  • Basis n 0 (and R0 IA (x,x) x Î A)
  • path of length 0 from x to y iff xy iff
    (x,y) Î IA.
  • Ind. case n k1.
  • a path of length n from x to y ltgt (by def)
  • a path of length k from x to z and
  • a path of length 1 from z to y for some z ?
    A ltgt
  • (by ind. hyp.) z Î A s.t. (x,z) Î Rk /\
    (z,y) Î R ltgt (by def)
  • (x,y) ? Rn. QED

41
correspondence b/t relation and digrqph
  • Def. 2 R a binary relation on A (hence (A,R) is
    a digraph).
  • R def (x,y) a path of length gt 0 from x
    to y in (A,R)
  • (i.e., R Uk1, Rk)
  • R def (x,y) there is a path of length ³ 0
    from x to y
  • Uk0, Rk R U (x,x) x in A R U
    IA.
  • Theorem 2
  • 1. R is the transitive closure of R.
  • 2. R is the reflexive and transitive closure
    of R.
  • Note If R encodes the property that " (a,b) ? R
    iff b can be reached from a by 1 step" then
  • 1. (a,b) ? Rk means "b can be reached from a by
    k steps."
  • 2. (a,b) ? R means " b is reachable from a" or
  • means " b is connected to
    a".

42
Proof of Theorem 2
  • pf (1) left to the students.
  • (2). Properties needed to be proved
  • 1. R ÍR
  • 2. R is ref. and transitive.
  • 3. If T is another relation with property 1
    2, then R Í T.
  • 1. R Í R and R is ref. trivial, since R
    IA U R U R2 U ...
  • 2. R is transitive
  • (x,y) Î R and (y,z) Î R
  • gt m and n ³ 0 s.t. (x,y) Î Rm and (y,z) Î Rn
  • gt (x,z)Î Rmn
  • gt (x,z) Î R.

43
  • 3. Smallest of R
  • Let T be any ref. and tran. relation including R.
  • We will show by induction on n that Rn ÍT for all
    n ? 0.
  • Hence R R0 U R U R2 U... Í T.
  • case 1 n 0 R0 IA Í T (since T is
    reflexive).
  • case 2 n 1 R Í T (since T includes R)
  • case 3 n k1 gt 1 by ind.hyp., RJ Í T for
    all J lt n.
  • gt if (x,y) Î Rn
  • gt z s.t. (x,z) Î R and (z,y) Î Rk
  • gt (by ind. hyp.) (x,z)Î T and (z,y)Î T
  • gt (by tran. of T) (x,y)Î T.

44
Some lemmas
  • Lem 1 A n gt 0, R a relation on A.
  • 1. If there is a path of length m gt n from a to
    b, then there is a path of length 1 ? p lt m from
    a to b.
  • 2. If there is a path of length gt 0 from a to b
    then there is a path of length 1? p ? n from a to
    b.

45
The proof
  • Let ax0, x1,x2,..., xm-1,xmb be the path of
    length m from a to b.
  • gt x1,x2,..., xm-1,xm A n.
  • Since m gt n, by pigeonhole principle,
  • there are 1? iltj ? m s.t. xi xj.
  • gt The path x0,x1,..,xi, xj1,...,xm is a
    path from a to b with
  • shorter length m (j-i) lt m.

46
  • 2. Let a0 be any path of length gt 0 from a to b.
  • If a0 n, we are done.
  • O/w, by 1. we can construct a path a1 with
    length 0 lt a1 lta0.
  • if a1 n, then we are done.
  • O/w, repeat the same process, we can obtain a
    sequence of path a0, a1, a2,... from a to b with
    a0 gt a1 gt ... gt0
  • But since a0 is finite, we will
    eventually find a path ak with length n .
  • QED

47
Some lemmas
  • Lem 1 A n gt 0, R a relation on A.
  • 1. If there is a path of length m n from a to
    b, then there is a path of length 0 p lt m from
    a to b.
  • 2. If there is a path ( of length 0 ) from a
    to b then there is a path of length 0 p lt n
    from a to b.
  • pf (Same as Lem1 except that x0 is taken into
    account when applying pigeonhole principle )
  • 1. Let ax0, x1,x2,..., xm-1,xmb be the path of
    length m from a to b.
  • Since m n, and x0, x1,x2,..., xm-1,xm
    A n,
  • by pigeonhole principle, there is xi xj for
    some 0? iltj ? m.
  • gt The path x0,x1,..,xi, xj1,...,xm is a path
    from a to b with length m (j-i) lt m.
  • 2. same as Lem 1.

48
Corollaries
  • lemma If a ¹ b and there is a path of length m ?
    n from a to b, then there is a path of length 0 lt
    p lt n from a to b.
  • pf similar to previous Lemma (1.11.2) with the
    note that, m ? n, x0 ? xm, and x0, x1,x2,...,
    xm-1,xm A n.
  • By pigeonhole principle,
  • there is 0? iltj ? m s.t. xi xj where (i,j)
    ? (0,m).
  • Hence 0lt (j-i) lt m and the resulting path has
    shorter size
  • 0lt m (j-i) lt m.
  • Corollary A n.
  • 1. R Uk 1.. n Rk.
  • Pf (1.) It suffices to show that ?m gt n, Rm Í
    RUR2U...URn.
  • (x,y) Î Rm ltgt a path from x to y of length
    m gt n
  • gt a path from x to y of length 0 lt p ? n
  • ltgt (x,y) Î Rp ltgt (x,y) Î RUR2U...URn.

49
  • Corollary 2. R Uk0..n-1 Rk.
  • Pf Same as 1 but using Lem1.
  • i.e., It suffices to show that ?m n, Rm Í
    R0UR1U...URn-1.
  • Theorem
  • 1. MR MR \/ MR2 \/...\/MRn.
  • 2. MR MR0 \/ MR \/ MR2 \/...\/MRn-1.

50
Algorithm for transitive closure
  • TR(MR a 0-1 nxn matrix)
  • 1. A MR
  • 2. B A
  • 3. for i 2 to n do
  • 3.1 A A x MR // A MRi.
  • 3.2 B B \/ A // B Uk1,i MRk.
  • 4. return(B) // B MR.
  • Exercise Give an algorithm for finding MR.
  • Complexity
  • each execution of step 3.1 takes time O(n3)
  • Total execution time O(n4).
  • gt more efficient algorithm ( O(n3) ) is
    possible!!

51
Warshall's algorithm
  • Applying dynamic programming technique
  • running time O(n3).
  • R a relation on A v1,v2,...,vn.
  • note (MR)ij 1 iff (vi,vj) ? R.
  • W0,W1,...,Wn a sequence of nxn 0-1 matrices,
    where
  • (WK)ij 1 ( 0 k n ) iff
  • there is a path of length gt0 from vi to vj s.t.
    all interior vertices of this path are in the set
    v1,...,vk.
  • (i.e., a path vi,u1,u2,...,us,vj (s³ 0)
    s.t.u1,...,usÍ v1,...,vk.

52
Facts about Wk
  • W0 MR.
  • since (vi,vj) Î R
  • ltgt a path of length 1 from vi to vj
    (no interior vertex)
  • ltgt (i,j) Î W0.
  • 2. Wn MR.
  • Since (vi,vj) Î R
  • ltgt a path of length gt 0 from vi to vj
  • ltgt a path of length gt 0 from vi to vj
    where all interior vertices are among the set of
    vertices v1,v2,...,vn
  • ltgt (i,j)Î Wn.

53
Warshall's algorithm (cont'd)
  • Fafct 3 Wk1 can be defined in terms of Wk.
  • Lem 2 (Wk1)ij (Wk)ij \/ ((Wk)i(k1) /\
    (Wk)(k1)j )
  • pf (Wk1)ij 1 ltgt (by def.)
  • a path avi,u1,...,us, vj from vi to vj
  • with u1,...,us
    Ív1,...,vk1.
  • ltgt two possible cases
  • case 1 no ui (i 1,..,s) is vk1 (i.e., vk1Ï
    u1,...,us).
  • gt a is also a path from vi to vj with
    interior vertices among v1, ...,vk
  • gt (Wk)ij 1.

54
Warshall's algorithm (cont'd)
  • case 2 a path a vi,u1,...,us, vj from vi
    to vj
  • with some ui vk1. (i.e., vk1
    ? u1,...,us).
  • Now let IDX i ui vk1 and let
  • p min(IDX) q max(IDX).
  • Note that p will equal to q iff IDX 1.
    Now the path
  • b v1,u1,...,up-1,up,uq1,...,us,v
    j
  • is a path from vi to vj containing only one
    vk1 up.
  • gt v1,u1,..,up ia a path from v1 to vk1
    and
  • up,u q1,..,vj is a path from vk1 to
    vj
  • gt (Wk)i(k1) (Wk)(k1)j 1.
  • As a result (Wk)ij \/ ( (Wk)i(k1) /\ (Wk)(k1)j
    ) 1. QED

55
Warshall's algorithm (cont'd)
  • EX Let MR 0001,1010,1001,0010. find
    W0,W1,W2,W3,W4.
  • The Warshall's algorithm
  • Procedure WS1(MR nxn 0-1 matrix)
  • 1. W0 MR
  • 2. for k 1 to n do // compute Wk
  • 3. for i 1 to n do
  • 4. for j 1 to n do // compute (Wk)ij
  • 5. Wki,j Wk-1i,j \/
    (Wk-1i,k/\Wk-1k,j )
  • 6. Return(Wn) // Wn MR.
  • analysis 1. The algorithm takes time O(n3).
  • 2. The algorithm requires space O(n3).
  • gt can be reduced to O(n2) by collapsing all
    Wk into one W.
  • (and become the final Warshall's algorithm
    shown in the book). (why?)

56
Why may all Wjs be collapsed into one?
  • Note that Wki,j Wk-1i,j \/
    (Wk-1i,k/\Wk-1k,j ) ---(). Hence the value
    of cell (i,j) at time k depends on cells (i,j),
    (i,k) and (k,j) at time k-1.
  • But for Wki,k or Wkk,j, we have
  • Wki,k Wk-1i,k \/ (Wk-1i,k/\Wk-1k,k )
  • Wk-1i,k , and
  • Wkk,j
  • Wk-1k,j \/ (Wk-1k,k/\Wk-1k,j )
  • Wk-1k,j .
  • i.e, The cells i,k and k,j
  • do not change value during
  • the computation of Wk.
  • Hence Wki,j can be correctly
  • computed no matter the
  • content of i,k and k,j
  • come from Wk or Wk-1.

57
6.5 Equivalence relations
  • Def. 1 R a relation on a set A.
  • R is an equivalence relation on A iff it is ref.
    symmetric and transitive.
  • x and y in A are said to be equivalent (or
    R-equivalent) if (x,y) ? R.
  • Ex1 R a relation on the set of all bit strings
    0,1 defined by
  • xRy iff x y.
  • gt 1. R is an equivalence relation on 0,1
    ?.
  • 2. 00110 and 11100 are R-equivalent
    since both have the
  • same length.
  • Ex 2 R a relation on integers s.t. xRy iff
    xy or x -y.
  • gt R is an equivalence relation ?
  • Ex3 R a relation on real numbers s.t. xRy iff
    x-y is an integer.
  • gt R is an equivalence relation ?

58
More equivalence relations
  • Ex5 congruence modulo m(??)
  • m a positive integer gt 1.
  • ºm a binary relation on Z s.t. x ºm y iff m
    divides (x-y).
  • gt ºm is an equivalence relation on Z.
  • pf
  • Equivalence classes
  • Def 2 R an equivalence relation on A
  • a any element of A.
  • gt the equivalence class of a w.r.t. R, denoted
  • aR, is the set of all elements of A
    equivalent to a, i.e.,
  • aR b aRb ? b Î A.
  • If b ? aR, then say b is a representative of
    the class aR.

59
Equivalence classes
  • Ex 5 R a binary relation on Z s.t. xRy iff x
    y or x -y.
  • gt 1. R is an equivalence relation.
  • 2. For each x in Z, xR x, -x.
  • 3. e.g., 3R y Î Z 3 y or 3 -y
    3, -3.
  • Ex6 R? Z2 s.t. xRy iff x º y (mod 4).
  • gt 0R y ÎZ 4 (0-y) ..., -8, -4, 0,
    4, 8 ,...
  • 1R y ÎZ 4 (1-y) ..., -7, -3,
    1, 5, 9 ,...,
  • 2R y ÎZ 4 (2-y) ..., -6, -2,
    2, 6, 10 ,...,
  • 3R y ÎZ 4 (3-y) ..., -5, -1,
    3, 7,11,...
  • 4R y ÎZ 4 (0-4) ..., -8, -4,
    0, 4, 8 ,... 0R.
  • 5R 1R, 6R 2R,...
  • gt 1. iR i4kR, where i and k are any
    integers.
  • 2. There are only 4 equivalence classes
    w.r.t. R. i.e.,
  • aR a Î Z 0R, 1R, 2R,
    3R
  • ...,-4,0,4,.., ...,-3,1,5,..,
    ...,-2,2,6,..., ...,-1,3,7,....

60
Congruence relation
  • 3. the class aR is called a congruence class
    (modulo m).
  • 4. For any integer x, xÎ yR iff xRy.
  • Quotient set
  • A a set R an equivalence relation on A.
  • The quotient of A w.r.t R, denoted A/R, is the
    set of all R-equivalence classes of A. (i.e., A/R
    xR x Î A ).
  • Ex The set Z/º4 04,14,24,34.
  • in general we have Z/ºn 0n,1n,...,n-1n.
  • Ex A n R an equivalence relation on A s.t.
    aR k for all
  • a Î A. gt A/R ___

61
Equivalence classes and partitions
  • Lemma 1 R an equivalence relation on A.
  • Then for any x,y Î A,
  • 1. x R y iff xR yR iff xRÇ
    yR ¹ Æ.
  • 2. not x R y iff xR ¹ yR iff xRÇ
    yR Æ.
  • 3. Every class x is not empty and every x
    belongs to exactly one class.
  • Pf 2,3 direct from 1.

not x1Ry1
xRy
Key proprty xRy and xRz ? yRz (order xRy or yRx
dont care !!)
A
62
Proof (decompose Prop 1 of Lemma 1 into 1.1 and
1.2)
  • 1.1 xRy iff xR yR 1.2 xRy
    iff xRÇ yR ¹ Æ.
  • pf 1.1
  • only-if part(gt) Suppose xRy.
  • gt ?b, b ? x iff xRb iff yRb iff b ?
    y
  • gt x y.
  • if-part(lt) Suppose x y gt y ? y
    x gt xRy.
  • 1.2
  • only-if part Since xRy gt y ? x.
  • But we also have yRy hence y ? y.
  • As a result y ? xR Ç yR ¹ Æ.
  • if-part Let b be any element in xR Ç yR ¹
    Æ.
  • gt xRb and yRb gt xRy.

63
Partitions
  • A a set. P? 2A a set of subsets of A.
  • P is said to be a partition of A iff
  • 1. Every Ai in P is not empty.
  • 2. For all Ai, Aj Î P, if Ai ¹ Aj then Ai Ç Aj
    .
  • 3. UAi Ai Î P (i.e., the union of all
    members of P) A.
  • Note Every member of P is called a block.
  • Ex Let A N be the set of all nonnegative
    integers.
  • PODD, EVEN, where ODD 1,3,5,7,9,..
  • EVEN 0,2,4,6,...
  • gt P is a partition of A.
  • Note Every element of A
  • belongs to exactly one member
  • Aj in P.


64
Equivalence of partition and quotient set
  • A a set.
  • For each partition P of A, define a relation ÂP
    as follows
  • ? x, y Î A,
  • x ÂP y iff x and y belong to the same block
    of P
  • (i.e., Aj Î P s.t. x,y Í Aj).
  • Lem 1 Âp is an equivalence relation. ÂP is
    called the equivalence relation induced by P.
  • pf left to you.
  • Theorem R an equivalence relation on A P a
    partition on A. Then
  • 1. A/R is a partition of A Ex P
    ODD,EVEN
  • 2. ÂA/R R. gt ÂP
    (x,y) x º y mod 2
  • 3. A/ Âp P. gt N /Âp
    1,3,5,..., 0,2,4,... P

65
Proof
  • 1. A/R is a partition Note A/R x x Î
    A.
  • 1. A/R contains no empty set since every x
    is not empty.
  • 2. for any Aix, Ajy Î A/R, if Ai ¹ Aj gt
    x ¹ y
  • gt xÇ y .
  • 3. U B B in A/R U x x Î A A.
  • 2. xRy ltgt x y
  • ltgt x and y are in the same block
    class of A/R
  • ltgt x ÂA/R y.
  • 3. By definition
  • x Âp y iff x and y are in the same block of P.
    Hence,
  • for all x Î A ( U P) ,
  • xÂp y x Âp y is the block
    containing x and Î P.
  • and A/ÂP xÂp x Î A is the set of all
    blocks of P.

66
6.6 partial orderings (??)
  • Def 1 a binary relation on a set S.
  • 1. is a partial ordering (or called partial
    order, (po)) iff it is
  • reflexive (x x ),
  • antisymmetric (x y and y x ? x y ) and
  • transitive ( x y and y z ? x z ).
  • 2. If R is a po on S gt (S,R) is called a
    partial ordered set (poset).
  • Ex1 (Z, ³) is a poset.
  • Ex2 (Z, ) is a poset.
  • pf ref x x trans x y /\ y z gt x
    z
  • antisym x y /\ y x gt x y.
  • Ex3 (2S, Í ) is a poset.
  • pf ref A Í A trans AÍ B and BÍC gt
    A ÍC
  • antisym AÍ B and BÍ A gt A B. QED

67
Strict partial ordering
  • Def R a binary relation on A. If R is
  • irreflexive( not x R x ) and
  • transitive,
  • then we say R is a strict partial ordering
    (spo) on A.
  • Note We usually use lt to represent a spo.
  • EX The less than relation lt on Z is a spo.
  • pf irref for all x, not x lt x.
  • trans x lt y and y lt z gt x lt z.
  • Lemma Every strict partial ordering is
    asymmetric.
  • pf Let x,y be any pair of elements with xRy.
  • Assume also that y R x. Then
  • By transitivity of R, we have xRx, which is a
    contradiction.
  • Hence it must be the case that not (yRx).

68
Relationship between po and spo
  • S a set R a relation on S Is Identity
    Relation of S.
  • 1. Define a new relation ltR R IS as
    follows
  • ? x, y ?S, x ltR y iff
    xRy ? x ¹ y.
  • 2. Define a new relation R R U IS as
    follows
  • ? x,y ? S, x R y iff
    xRy ? x y.
  • Ex
  • In Z, if R is , then ltR is lt (less than).
  • In Z, if R is gt, then R is .

69
Lemma
  • 1. If R is a po( ) gt ltR R IS is a
    spo.
  • 2. If R is a spo(lt) gt R R U IS is a po.
  • 3. If R is a po () gt (ltR) R
  • 4. If R is a spo(lt) gt lt(R) R.
  • pf 1. irref (ok) since no (x,x) ? R Is.
  • transitive If both (x,y) and (y,z) ? R
    Is, then
  • () (x,z) ? R and
  • () (x,z) ? Is (otherwise x z gt x
    y z gt (x,y) ? R Is).
  • Hence (x,z) ? R Is.
  • 2. left as an exercise.
  • 3. R is po gt R is reflexive gt Is ? R gt (R
    Is) U Is R.
  • 4. R is spo gt R is irreflexive gt Is Ç R
  • gt (R U Is) - Is R.

70
Notations
  • (S,R) is a poset
  • gt use the symbol (less than or equal to)
    to stand for the relation R,
  • and its "less than" version ltR are written
    by lt.
  • If (S,R) is a sposet,
  • gt use the symbol lt (less than) to stand
    for the relation R and use to stand for its
    "less than or equal to" version R.
  • Note
  • By previous theorem, lt and can be uniquely
    defined by each other. Hence we can choose either
    one as the basic definition once one ordering is
    defined, the other can be obtained too.
  • Mathematicians usually use as official
    definition of ordering while some computer
    scientists may choose lt as official definition.

71
Total ordering
  • Def 2 (S, ) a poset x, y in S
  • x and y are said to be comparable iff either x
    y or y x.
  • If x and y are not comparable, they are said to
    be incomparable.
  • Def 3 (S, ) a poset
  • If every two elements of S are comparable, S is
    said to be a totally ordered set or linearly
    ordered set and is called a total order or
    linear order.
  • A totally ordered set is also called a chain.
  • Def 4 (S, ) a totally ordered set
  • (S,) is called a well-ordered set if every
    nonempty subset of S has a least element.
  • Ex5 (Z, ) is a totally ordered set but not
    well-ordered.
  • Ex6 (N, ) is not a chain, Since neither 5 7
    nor 7 5.
  • Ex7 (N, ) is well-ordered (N, ³) is totally
    ordered but not well-ordered.

72
Lexicographical ordering
  • Let (S , )be a poset and S be the set of all
    finite strings over S. Define a relation S on
    S as follows
  • For any string x, y
  • x S y iff
  • 1. x is a prefix of y or
  • 2. there are strings z, u, v and symbol a, b in S
    s.t.
  • x z a u and y z b v with a lt b.
  • Some facts
  • 1. If (S,) is a poset then so is (S,S).
  • 2. If (S,) is a total ordered set, then so is
    (S,S).

73
Maximal and minimal elements
  • (S, ) a poset X a subset of S
  • 1. x ? X is the least (smallest, minimum) element
    of X iff
  • ? y ? X x y.
  • 2. x ? X is the greatest (largest, maximum)
    element of X iff ?
  • ? y ? X y x.
  • 3. x ? X is a minimal element of X iff
  • ? y? X ylt x (or equivalently, ? y ? X y
    xgtyx)
  • 4. x ? X is a maximal element of X iff ?
  • ? y? X x lt y (or equivalently, ? y ? X x
    ygtyx)
  • Notes
  • 1. Least and greatest element, if existing, must
    be unique,
  • 2. There may exist multiple minimal (and
    maximal) elements for some subset of a poset.

74
glb and lub
  • 5. x ? S is a lower bound of X iff ? y ? X
    x y.
  • 6. x ? S is an upper bound of X iff ? y ? X
    y x.
  • definition
  • lb(X) x ? S x is an lower bound of X
  • ub(X) x ? S x is an upper bound of X
  • 7. x ? S is the greatest lower bound of X (glb(X)
    ) iff
  • x is the greatest element of
    lb(X).
  • 8. x is the least upper bound of X ( lub(X) ) iff
  • x is an upper bound of X and x y for all
    upper bounds y of X.
  • Lemma glb and lub, if existing, must be unique.
  • pf if both x and y are lub of X, then both are
    least elements of ub(X), then x y and y x and
    by antisymetric property of poset, x y.

75
Lattices
  • Def (S,) is a lattice if
  • 1. (S,) is a poset
  • 2. Every pair (x,y) of elements of S has both
    lub and glb.
  • where lub(x,y) (written x \/ y ) the join
    of x and y
  • glb(x,y) (wriiten x /\ y ) the
    meet of x and y.
  • Ex 1. (N, ) is a lattice
  • with x\/y lcm(x,y) and x /\ y
    gcd(x,y).
  • 2. (2S,Í ) is a lattice
  • with x \/ y x U y and x /\ y xÇ y.
  • Def (S,) a lattice
  • gt (S, ) is bounded iff it has a least and a
    greatest element.
  • (S,) is a complete lattice iff every
    subset(including empty set) of S has both lub
    and glb.

76
Topological sorting
  • (S,R) a poset.
  • a total ordering on S is said to be compatible
    with R if a b implies aRb (i.e., is an
    extension of R R Í ).
  • Topological sorting
  • Given a finite poset, construct a total
    ordering compatible with it.
  • Lem 1 Every finite nonempty poset has a minimal
    element.
  • pf choose a0 from S, if a0 is not minimal
  • gt a1 lt a0, if a1 is not minimal
  • gt a2 lt a1,...
  • ...
  • gt The process must end with a minimal element
    an (since S is finite). QED

77
Topological sorting (cont'd)
  • The algorithm
  • procedure topological-sort(S a poset)
  • 1. k 1
  • 2. while( S ¹ )
  • 3. ak any minimal element of S
  • 4. S S - ak
  • 5. k k 1
  • 6. / a1,a2,...an is a compatible
    total ordering. /
  • Ex The poset is (1,2,4,5,12,20, )
  • gt 1. Draw a graphical representation for (S,
    ).
  • gt 2. find a total ordering compatible with .
Write a Comment
User Comments (0)
About PowerShow.com