- 149 Views
- Uploaded on
- Presentation posted in: General

Chapter 7

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Chapter 7

Relations

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

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 > 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}.

==> (New York, USA) Î R and Tokyo R Japan, ...

Ex3: A = {1,2,3,4}; B={a,b}

==> {(0,a),(0,b),(1,a),(2,b)} is a relation from A to B.

==> Graphical representation = ?

==> table (or matrix) representation = ?

Def. 2: A relation R on (or over) a set A is a subset of AxA (= A2).

Ex4: A={1,2,3,4};

Let R = {(a,b) Î A2 | a divides b}

==> 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 > b };

R3 = {(a,b) | a = b or a = -b}; R4={(a,b) | a = b};

R5={(a,b) | a = b+1};R6={(A,B) | A + B < 4}

==> Which relations contain the pairs:

(1,1), (1,2),(2,1),(1,-1),(2,2)?

Ex 6: |A| = n; |B| = m

==> 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},

==> |rel(A,B)| = ? )

Sol: 1. |AxB| = ____.

2. Since Rel(A,B) = 2AxB. Hence |rel(A,B)| = ___

Ex6': |Rel(A,A)| = #relations on A = ____

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 => (b,a) ÎR.
- R is asymmetric if for every a,b Î A, (a,b) Î R => (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.

- 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. ?

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 ?

- |A| = n.
1. # relations on A = ? (2^(n^2))

2. # reflexive relations on A = ? (2 ^ (n^2 - 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(n+1)/2))

6. #asymmetric relations on A = ? (3^(n(n-1)/2))

7. #antisymmetric relations on A = ? (3^(n(n-1)/2) x 2^n)

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!)

- Relations are sets:
- ==> All set operations can be applied to relations
- Ex: R1; R2: relations on A;
- ==> R1 U R2; R1 ⋂ R2; R1 \ R2 and ~R are defined as usual.

- [Binary ] relation compositions:
R Í AxB ; S Í BxC;

==> R·S (the composition of R and S) Í AXC is the set:

{(x,z) | $y Î B s.t. xRy ∧ ySz}

Note: The textbook use S·R (less popular) instead of R·S to denote the composition.

1

0

1

2

2

1

3

2

3

4

R

S

C

A

B

Ex19: A={1,2,3}; B={1,2,3,4}; C={0,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)} => R·S= ?

Fact: Relation composition is associative (i.e., R·(S·T) = (R·S)·T )

Hence we can write RST w/t parentheses.

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. Rk+1 = RRk = {(x,z) | $y Î R s.t. (x,y) ÎR /\ (y,z) ÎRk }.

Ex: show that If a, b 0 then RaRb = Ra+b.

Pf : by induction on a.

- basis: a = 0: => RaRb = IARb =
- {(x,z) | $y (x,y) Î IA /\ (y,z) Î Rb } =
- {(x, z) | (x,x) Î IA /\ (x,z) Î Rb} = {(x,z) | (x,z) Î Rb } = Rb.
- Ind. step : assume RaRb= Ra+b =>
- Ra+1Rb = (RRa)Rb = R(RaRb) = RRa+b = Ra+1+b.
Corollary: RRk = Rk R.

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 = Rn+1 then Rk = Rk-1 for all k > n.

Ex: If R5 = R6 => R10 = R6R4 = R5R4 = R9

Pf: left as an exercise.

Corollary: If Rn = Rn+1 ==> Rk = Rn for all k > n.

Pf: Rk = Rk-1 = R(k-1)-1 = Rk-2 = ...Rn+1 = Rn.

Theorem 1: Relation R on a set A is transitive iff RnÍ R for all n > 0.

Pf: (=>) : trivial (by induction on n).

Basis: n = 1 (ok). Ind. step: for any (x,z) Î Rn+1 => $z s.t. (x,y) Î Rn /\ (y,z) Î R. By ind. hyp. both (x,y) and (y,z) Î R. By tran. of R, (x,z) Î R.

(<=) : for any x,y,z, (x,y) Î R /\ (y,z) Î R => (x,z) Î R2 => (x,y) Î R. QED

Corollary: R is transitive iff R2 Í R. (why ?)

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 < y < z, x,y,z ∈Z}.

Then (1,2,3) Î R but (2,4,3) Ï R.

- 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 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.

- A database DB (at a time instant, say, time t) consists of a pair <Dom,Rel> 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 <= s < = 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.

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]).

=> DB = <{A,B,C,D}, {S}> 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

...

- 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,...)

- Selection (or restriction) --- find subset of R with a certain

- Restriction (selection):
P(x1,...,xn): a statement (or property) on n-tuples

(x1,...,xn).

R: a relation on A1xA2...,xAn.

==> R|P ( 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 < x2 < x3 or x3 = 2 " and

R = { (1,2,3),

(1,3,2),

(2,1,3), ==> R|p = {(1,2,3), (1,3,2), (3,1,2) }.

(2,3,1),

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

R : a relation on A1,A2,...,An

w= j1j2...jm is a sequence with 0< js< n+1 for all s = 1,..,m.

==> 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)}

=> 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)}

- R : A1xA2x...xAm-pxAm-p+1x...xAm;
S : B1 xB2x..Bpx Bp+1x ..xBm;

p ≤ min(m,n).

==> Jp(R,S) = {(a1,...,am-p, c1,c2,...,cp, bp+1,..., bn) |

$ (a1,...,am-p, c1,c2,...,cp ) Î R /\

(c1,c2,...,cp, bp+1,..., 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)}

==> J2(R,S) = ?

J1(R,S) = ?

J3(S,R) = ?

A

B

A->B

1

1

a

a

2

2

b

b

3

3

- Three possible ways to represent a finite relation
- a list (or array) of ordered pairs.
- (0-1) matrix
- digraph (directed graph)
Ex: A={1,2,3}, B={a,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

- A={a1,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: A={a1,a2,a3}; B = {b1,b2,...,b5}

If R1={(a1,b1),(a1,b2),(a3,b2),(a3,b5)}

==> MR1 = ?

01000

If MR2= 10110 ==> R2 = ?

10101

Note: 1. If R ∈ AxA with |A| = n, ==> MR is a nxn 0-1 matrix.

2. R is ref. (resp. irref. ) iff mii = 1 (0) for every 1i n.

3. R is symmetric (asym) iff for all i, j mij = mji (mij /\ mji ≠ 1)

4. R is tansitive iff for all i,j,k if mij=mjk=1 => mik = 1.

- Boolean operation on {0,1}
- x ∨ y = max(x,y)
- x ∧ y = min(x,y)
- ~x = 1-x

- Boolean operation 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 = \/k=1,n M1ik /\ M2kj. --- product
- M0 = I; Mn+1 = Mn x M. --- power of 0-1 matrix.

- R1, R2: two relations from A to B.
1. MR1 UR2 = MR1 ∨ MR2.

2. MR1ÇR2 = MR1 ∧ MR2

3. M~R1 = ~MR1.

- R: AXB; S: BxC with
A = {a1,...,am}, B = {b1,...,bn} and C= {c1,..,cp}.

==> 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

\/j=1,n (MR)ij /\ (MS)jk = 1

- Hence MRS = MR x MS. ==> MRk = (MR)k for any k 0.

Ex3: MR = [110; 111; 011]

==> Is R ref, symmetric or antisymmetric ?

Ex4: R1,R2: two relation on A.

MR1= [101;100;010]; MR2 = [101;011;100].

==> MR1UR2 = ? MR1Ç R2 = ? M~R1 = ?

Ex5: MR = [101;110;000]; MS = [010; 001;101]

=> MRS = ?

Ex6: MR = [010;011;100]

==> MR2 = (MR)2 = ?

b

a

d

c

- 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)

- A = {a1,...,am}, B = {b1,...,bn}
R: a binary relation from A to B

==> 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 > y} ⊆ AXB.

==> R = ?

The digraph GR for R = ({1,2,3,4}, R).

==> What is its graphical representation ?

- 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 -> 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 includes R and satisfies the property P. (i.e., CLp(R) is the relation on A s.t.

- 1. R Í CLP(R) --- including R
- 2. P(CLP(R)) is true --- satisfying P
- 3. For any relation S on A, if it satisfies (1) and (2),
- then CLP(R) Í S. --- smallest

- CLp(R) is called the P closure of R.

Ex: A = {1,2,3}; R={(1,1),(1,2),(2,1),(3,2)}

Let P(S) means " S is reflexive"

==> 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.

==> TT = ?

2. Which is the smallest in TT ? (= R U IA !)

- Theorem: CLP(R), if existing, is unique.
pf: Let S and T be any two P-closures of R.

==> By (3) S ÍT and T Í S, hence S = T.

3. Problem: Does CLP(R) always exist ? (no!)

Does the irreflexive closure of R exist in the example ?

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.

- Ex: property increasing closed under intersection
- ref. yes yes
- symmetric yesyes
- transitivity yesyes
- 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.

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. }

==> 1. (1) RUIAÎ Tref (2) S Î T ref ==> R U IAÍ S.

Hence R U IA = CLref(R).

2. Similar to 1.

6

2

1

3

5

4

- R: a binary relation on A
==> 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)}

==> GR = Fig 1.

==> MR = ?

==> GS = ?

==> MS = ?

==> S = ?

- The reflexive and transitive closure of R is the smallest reflexive and transitive relation on A including R.

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, xi+1) Î R for all i = 0,..,n-1.
2. If x0 = xn and n > 0 ==> the path is called a cycle ( or circuit).

Ex: If R = {(1,2),(2,1),(2,3)(3,4)(4,2)(4,5)}

==> "1,2,3,4,5" is a path of length ? from ? to ?.

Theorem 1: |A|= m; R is binary relation on A;

(Hence G=(A,R) is a digraph)

==> 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

x=y iff (x,y) Î IA.

Ind. case: n = k+1.

$ a path of length n from x to y <=> (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 <=>

(by ind. hyp.) $ z Î A s.t. (x,z) Î Rk /\ (z,y) Î R <=> (by def)

(x,y) Rn. QED

Def. 2: R: a binary relation on A (hence (A,R) is a digraph).

R+ =def {(x,y) $ a path of length > 0 from x to y in (A,R)}

(i.e., R+ = Uk=1,¥ Rk)

R* =def {(x,y) | there is a path of length ³ 0 from x to y}

=Uk=0,¥ 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".

pf: (1) left to the students.

(2). 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* => $m and n ³ 0 s.t. (x,y) Î Rm and (y,z) Î Rn => (x,z)Î Rm+n => (x,z) Î R*.

3. Let T be any ref. and tran. relation including R.

we show by induction on n that Rn ÍT for all n 0.

Hence R* = R0 U R UR2U... Í 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 = k+1 > 1: by ind.hyp., RJ Í T for all J < n.

==> if (x,y) Î Rn => $z s.t. (x,z) Î Rk and (z,y) Î R

=> (by ind. hyp.) (x,z)Î T and (z,y)Î T => (by tran. of T) (x,y)Î T.

Lem 1: |A|= n > 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 < 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 n from a to b.

pf: 1. Let a=x0, x1,x2,..., xm-1,xm=b be the path of length m from a to b. Since m > n, by pigeonhole principle, there is

1 i<j m s.t. xi = xj.

==> The path x0,x1,..,xi, xj+1,...,xm is a path from a to b with length m + (j-i) < m.

2. Let a0 be any path from a to b. if | a0 | ≤ n, we are done.

O/w, by 1. we can construct a path a1 with length | a1 | <| a0 |.

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 | > | a1 | > ...

But since | a0 | is finite, we will eventually find a path ak with length n . QED

lemma: If a ¹ b and there is a path of length m n, then there is a path of length 0 < p < n from a to b.

pf: similar to previous Lemma with the note that during Lem 1.1, m n, x0 xm, and by pigeonhole principle,

there is 0 i<j m s.t. xi = xj and (i,j) (0,m).

Hence 0< (j-i) < m and the resulting path has size

0< m –(j-i) < m

Corollary: |A| = n.

1. R+ = Uk= 1, n Rk. 2. R* = Uk=0,n Rk.

Pf: (1.) Since ∀m > n, RmÍ RUR2U...URn.

(x,y) Î Rm <=> $ a path from x to y of length m > n

=> $ a path from x to y of length 0 < p n

<=> (x,y) Î Rp <=> (x,y) Î RUR2U...URn. (2.) Similar to 1.

Corollary: R* = Uk=0,n-1 Rk.

Pf: Left as an exercise. Hint: RnÍ IA U Uk=1,n-1 Rk.

(x,y) ∈ Rn ==> (x=y /\ (x,y) Î IA) or (xy /\ (x,y) Î Uk=1,n-1 Rk).

Theorem:

1. MR+ = MR \/ MR2 \/...\/MRn.

2. MR* = MR0 \/ MR \/ MR2 \/...\/MRn-1.

- 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 = Uk=1,i MRk.
- 4. return(B) // B = MR+.

- Exercise: Give an alg. for finding MR*.
- Complexity:
- each execution of step 3.1 takes time O(n3)
- Total execution time = O(n4).
- ==> more efficient algorithm ( O(n3) ) is possible!!

- 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 >0 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}.
Facts:

1. W0 = MR. [ since (vi,vj) Î R <=> $ a path of length 1 from vi to vj

(w/o interior vertex) <=> (i,j) Î W0. ]

2. Wn = MR+. [ Since (vi,vj) Î R+ <=> $ a path of length > 0 from vi

to vj <=> $ a path of length > 0 from vi to vj with interior vertices among the set {v1,v2,...,vn} <=> (i,j)Î Wn. ]

Fafct 3: Wk+1 can be defined in terms of Wk.

Lem 2: (Wk+1)ij = (Wk)ij \/ ((Wk)i(k+1) /\ (Wk)(k+1)j )

pf: (Wk+1)ij = 1 <=> (by def.)

$ a path a=vi,u1,...,us, vj from vi to vj

with {u1,...,us} Í{v1,...,vk+1}.

<=> two possible cases:

case 1: no ui (i= 1,..,s) is vk+1 (i.e., vk+1Ï {u1,...,us}).

==> a is also a path from vi to vj with interior vertices from {v1, ...,vk} ==> (Wk)ij = 1.

case 2:$ a path a = vi,u1,...,us, vj from vi to vj

with some ui = vk+1. (i.e., vk+1 ∈ {u1,...,us}).

Now let IDX = {i | ui = vk+1} 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,uq+1,...,us,vj

is a path from vi to vjcontaining only one vk+1 = up.

==> v1,u1,..,up ia a path from v1 to vk+1 and

up,u q+1,..,vj is a path from vk+1 to vj

==> (Wk)i(k+1) = (Wk)(k+1)j = 1.

<=> (Wk)ij \/ ( (Wk)i(k+1) /\ (Wk)(k+1)j )= 1. QED

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. Wk[i,j] := Wk-1[i,j] \/ (Wk-1[i,k]/\Wk-1[k,j] );

6. Return(Wn) // Wn = MR+.

analysis: 1. The algorithm takes time O(n3).

2. The algorithm require memory space O(n3).

==> 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?)

Note that Wk[i,j] := Wk-1[i,j] \/ (Wk-1[i,k]/\Wk-1[k,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 Wk[i,k] or Wk[k,j], we have

Wk[i,k] = Wk-1[i,k] \/ (Wk-1[i,k]/\Wk-1[k,k] )

= Wk-1[i,k] , and

Wk[k,j] =

Wk-1[k,j] \/ (Wk-1[k,k]/\Wk-1[k,j] )

= Wk[k,j] .

i.e, The cells [i,k] and [k,j]

do not change value during

the computation of Wk.

Hence Wk[i,j] can be correctly

computed no matter the

content of [i,k] and [k,j]

come from Wk or Wk-1.

j

k

(k,j)

k

(i,k)

(i,j)

i

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|.

==> 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 x=y or x = -y.

==> R is an equivalence relation ?

Ex3: R: a relation on real numbers s.t. xRy iff x-y is an integer.

==> R is an equivalence relation ?

Ex5: [congruence modulo m](共餘)

m: a positive integer > 1.

ºm: a binary relation on Z s.t. x ºm y iff m divides (x-y).

==> ºm is an equivalence relation on Z.

pf:

Equivalence classes:

Def 2: R: an equivalence relation on A;

a : any element of A.

==> the equivalence class of a w.r.t. R, denoted

[a]R, is the set of all elements of A equivalent to a, i.e.,

[a]R = { b | aRb ∧ b Î A}.

- If b [a]R, then say b is a representative of the class [a]R.

Ex 5: R: a binary relation on Z s.t. xRy iff x = y or x = -y.

=> 1. R is an equivalence relation.

2. For each x in Z, [x]R = {x, -x}.

3. e.g., [3]R = { y Î Z | 3 = y or 3 = -y} = {3, -3}.

Ex6: R Z2 s.t. xRy iff x º y (mod 4).

=> [0]R = {y ÎZ: 4 | (0-y) } = {..., -8, -4, 0, 4, 8 ,...}

[1]R = {y ÎZ: 4 | (1-y) } = {..., -7, -3, 1, 5, 9 ,...},

[2]R = {y ÎZ: 4 | (2-y) } = {..., -6, -2, 2, 6, 10 ,...},

[3]R ={y ÎZ: 4 | (3-y) } = {..., -5, -1, 3, 7,11,...}

[4]R = {y ÎZ: 4 | (0-4) } = {..., -8, -4, 0, 4, 8 ,...}= [0]R.

[5]R = [1]R, [6]R = [2]R,...

=> 1. [i]R = [i+4k]R, where i and k are any integers.

2. There are only 4 equivalence classes w.r.t. R. i.e.,

{[a]R | a Î Z} = {[0]R, [1]R, [2]R, [3]R}=

{ {...,-4,0,4,..}, {...,-3,1,5,..}, {...,-2,2,6,...}, {...,-1,3,7,...}}.

3. the class [a]R is called a congruence class (modulo m).

4. For any integer x, xÎ [y]R 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 = {[x]R | x Î A} ).

Ex: The set Z/º4 = {[0]4,[1]4,[2]4,[3]4}.

in general we have Z/ºn = {[0]n,[1]n,...,[n-1]n}.

Ex: |A| = n; R: an equivalence relation on A s.t. |[a]R| = k for all

a Î A. ==> | A/R | = ___

x

x1

y

y1

Lemma 1: R: an equivalence relation on A.

Then for any x,y Î A,

1. x R y iff [x]R = [y]R iff [x]RÇ [y]R ¹Æ.

2. not x R y iff [x]R ¹ [y]R iff [x]RÇ [y]R = Æ.

3. Every class [x] is not empty and every x belongs to exactly one class.

pf: 1. (=>:) x R y =>

[x] = {b | xRb} = {b | yRb} = [y]

=> since [x]=[y] and x Î [x],

[x]RÇ [y]R ¹Æ .

1. (<=:) let z in [x]RÇ [y]R ¹Æ.

=> [x] = {b | xRb} = {b | zRb}

= {b |yRb} = [y].

=> y Î [x] => xRy.

2,3: direct from 1.

not x1Ry1

xRy

A

A2

A1

A3

A5

A4

A6

- 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. U{Ai | 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.

P={ODD, EVEN}, where ODD = {1,3,5,7,9,..}

EVEN = {0,2,4,6,...}

==> P is a partition of A.

Note: Every element of A

belongs to exactly one member

Aj in P.

- A: a set.
For each partition P of A, define a relation ÂP as follows:

for all 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. ==> ÂP = {(x,y) | x º y mod 2}

3. A/ Âp = P. ==> N /Âp = { {1,3,5,...}, {0,2,4,...}} = P

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 Ai=[x], Aj=[y] Î A/R, if Ai ¹ Aj => [x] ¹ [y]

=> [x]Ç [y] = {}.

3. U{ B | B in A/R} = U {[x] | x Î A} = A.

2. xRy <=> [x] = [y]

<=> x and y are in the same block [class] of A/R

<=> 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.

Def 1: R: a binary relation on a set S.

1. R is a partial ordering (or called partial order, (po)) iff it is reflexive, antisymmetric and transitive.

2. If R is a po on S ==> (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 => x | z;

antisym: x | y /\ y | x ==> x = y.

Ex3: (2S, Í ) is a poset.

pf: ref: A Í A ; trans: AÍ B and BÍC ==> A ÍC;

antisym: AÍ B and BÍ A => A = B. QED

Def: R: a binary relation on A.

If R is irreflexive and transitive, then we say R is a strict partial ordering (spo) on A.

EX: The less than relation < on Z is a spo.

pf: irref: for all x, not x < x.

trans: x < y and y < z ==> x < z.

Lemma: Every strict partial ordering is asymmetric.

pf: Proof by contradiction. Assume there are x, y s.t.

xRy and yRx. Then by transitivity of R, we must have xRx, a

contradiction!. QED

- S: a set; R: a relation on S
1. Define a new relation <·R as follows:

∀ x, y ∈S, x <·R y iff xRy ∧ x ¹ y.

2. Define a new relation £·R as follows:

∀ x,y ∈ S, x £·R y iff xRy ∨ x = y.

Theorem:

1. If R is a po => <· R is a spo.

2. If R is a spo => £·R is a po.

3. If R is a po => £·(<·R) = R.

4. If R is a spo ==> <·(£·R)= R.

pf: 1. irref: x<·R x => xRx and x ¹ x => contrdiction!

2. trans: 2.1: x <·R y /\ y <·R z => x R z.

2.2: assume x = z => xRy /\ yRz => xRy /\ yRx

=> x=y, a contradiction with x <·R y !

==> (xRz and x ¹ z ) and hence x <·R z.

- (S,R) is a poset
==> use the symbol £ (less than or equal to) to stand for the relation R,

[and its "less than" version <·R are written by <. ]

- If (S,R) is a sposet,
==> use the symbol < (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, < 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 < as official definition.

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.

- Let (S ,£ )be a poset and S* be the set of all finite strings over S. Define a relation £Son 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 < 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).

- (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 y< x (or equivalently, y X y£ x=>y=x)

4. x X is a maximal element of X iff ?

~ y X x < y (or equivalently, y X x£ y=>y=x)

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.

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.

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

==> (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.

- (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

=> $ a1 < a0, if a1 is not minimal

=> $ a2 < a1,...

...

=> The process must end with a minimal element an (since S is finite). QED

- The algorithm:
procedure topological-sort(S: a poset)

1. { k = 1;

2. while( S ¹ {} ){

3. a[k] = any minimal element of S;

4. S = S - {a[k]};

5. k = k + 1}

6. } /* a[1],a[2],...a[n] is a compatible total ordering. */

Ex: The poset is ({1,2,4,5,12,20}, |)

==> 1. Draw a graphical representation for (S, |).

==> 2. find a total ordering compatible with |.