Relations: Operations & Properties. Power Sets Set of all subsets of a set A. –A = {1,2} –P(A) =...

Post on 26-Dec-2015

213 views 0 download

Transcript of Relations: Operations & Properties. Power Sets Set of all subsets of a set A. –A = {1,2} –P(A) =...

Relations: Operations & Properties

Power Sets• Set of all subsets of a set A.

– A = {1,2}– P(A) = 2A = { {}, {1}, {2}, {1,2} }

• We note that each element of A is either present (1) or not present (0). If we treat the elements of A as a sequence, we get a bit-string that represents the set.– If A = {a,b,c,d}, we can order the elements in a sequence <a,b,c,d>.– Then, we can let the bit-string say which elements are present: e.g.

0110 means {b,c}.– We can represent all the subsets of A, from = 0000 to U = 1111.

• This bit-string notation also helps us know the number of subsets in the powerset (just count in binary)– 2#A, 2|A| = 2 · 2 · … · 2 (|A| times)– This motivates the notation 2A for the power set.

Bit-String Operations on Power Sets• With bit string representations

– Set intersection: = pairwise – Set union: = pairwise – Set complement: ~ = bit complement– Set minus: – = mask out using 1’s = complement 2nd

operand and do pairwise • E.g. using {a,b,c,d}

– 1011 1101 = 1001 i.e. {a,c,d} {a,b,d} = {a,d}– 1011 1101 = 1111 i.e. {a,c,d} {a,b,d} = {a,b,c,d}– ~1011 = 0100 i.e. ~{a,c,d} = {b}– 1010 – 1100 = 0010 i.e. {a,c} – {a,b} = {c}

Binary Relations

• Sets of ordered 2-tuples (pairs) with values selected from domains (sets)

• Formally, a relation R from set A to set B is a set of pairs (x,y) such that x A and y B. We may write R:A B to express this.– R AB– If (x,y) R, we say that x is R-related to y; we may also write xRy.– Example:

• <: NNNN (where NN is the set of natural numbers)• (2,3) < or 2<3

– Predicates also define relations<:{0,1,2}{1,0,1,2} = {(x,y) | x {0,1,2} y {1,0,1,2} x < y}

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

Representations for Binary Relations

Tables< -1 0 1 2

0 0 0 1 1

1 0 0 0 1

2 0 0 0 0

2

1

0

2

1

0

-1

Matrices

Graphs- directed graph or digraph

- directed arcs

-1 0 1 2

0 0 0 1 1

1 0 0 0 1

2 0 0 0 0

Domains and Ranges for Binary RelationsLet R = {(0,1), (0,2), (1,2)}, then

• Domain of R = dom R = dom(R) = {x | y((x,y) R)}– i.e. {0,1}

– like XR

• Range of R = ran R = ran(R) = {y | x((x,y) R)}– i.e. {1,2}

– like YR

2

1

0

2

1

0

-1 Range Space

RangeDomain Space

Domain

Inverse

• If R: AB, then the inverse of R is R~: BA.

• R-1 is also a common notation

• R~ is defined by {(y,x) | (x,y) R}.– If R = {(a,b), (a,c)}, then R~ = {(b,a), (c,a)}– > is the inverse of < on the reals

• Note that R~ ~R– The complement of < is – But the inverse of < is >

Set Operations

• Since relations are sets, set operations apply (just like relational algebra).

• The arity must be the same indeed, the sets for the domain space and range space must be the same (just like in relational algebra).

Restriction & Extension• Restriction decreases the domain space or range space.

Example: Let < be the relation {(1,2), (1,3), (2,3)}. The restriction of the domain space to {1} restricts < to {(1,2), (1,3)}.

• Extensions increase the domain space or range space.Example: Let < be the relation {(1,2), (1,3), (2,3)}. The extension of

both the domain space and range space to {1,2,3,4} extends < to {(1,2), (1,3), (1,4), (2,3), (2,4), (3,4)}

• Extensions and restrictions need not change the relation.– Suppose {M1, M2, M3} are three males and {F1, F2, F3} are three

females and married_to is {(M1,F3), (M2,F2)}.

– Removing the unmarried male M3 or the unmarried female F1 does nothing to the relation, and adding the unmarried male M4 or the unmarried female F4 does nothing to the relation.

12345

1234

1234

Composition• Let R:AB and S:BC be two relations. The composition of R and S,

denoted by RS, contains the pairs (x,z) if and only if there is an intermediate object y such that (x,y) is in R and (y,z) is in S.

• Given R:AB and S:BC, with A = {1,2,3,4}, B = {1,2,3,4,5}, C = {1,2,3,4}, and R = {(1,3), (4,2), (1,1)} and S = {(3,4), (2,1), (4,2)}, we have:

RS = {(1,4), (4,1)}SR = {(3,2), (2,3), (2,1)} (i.e. not commutative)R(SR) = {(1,2), (4,3), (4,1)}(RS)R = {(1,2), (4,3), (4,1)} (i.e. is associative)

R SGraphically:

Composition & Matrix Multiplication

• Relation matrices only contain 0’s and 1’s.• For relational matrix multiplication, use

for *, for +, with 1=T and 0=F.

0001

0000

0000

1000

000104

000003

000002

001011

54321R

00005

00104

10003

00012

00001

4321S

RS(1,1) = (10)(01)(10)(00)(00) = 0

4

3

2

1

4321RS

=

RS = {(1,4), (4,1)}

Combined Operations• All operations we have discussed can be combined

so long as the compatibility requirements are met.• Example:

R = {(1,1), (3,1), (1,2)}S = {(1,1), (2,3)}(R S)~ R

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

R: A B S: B A

A = {1,2,3}

B = {1,2,3}

Properties of Binary Relations• Properties of binary relations on a set R: AA help

us with lots of things groupings, orderings, …• Because there is only one set A:

– matrices are square |A| |A|– graphs can be drawn with only one set of nodes

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

00104

11003

00002

01001

43211

3

2

4

Reflexivity• Reflexive: x(xRx)

=, , “is in same major as”, etc.

Reflexive

Irreflexive Neither

13

12

11

321

03

02

01

321

13

12

01

321

Note:

e.g., “loves”e.g., <

Symmetry• Symmetric: xy(xRy yRx)• Antisymmetric: xy(xRy yRx x = y)

“sibling” is symmetric are antisymmetric (if ab and ba, then a=b)= is symmetric and antisymmetric“loves” is neither symmetric nor antisymmetric

(always both ways)

(but to self is ok)

(never both ways)

113

102

101

321

013

12

01

321

Note: antisymmetry is

NOT asymmetry

Transitivity

• Transitive: xyz(xRy yRz xRz)“taller” is transitive< is transitive x<y<z“ancestor” is transitive“brother-in-law” is not transitive

• Transitive: “If I can get there in two, then I can get there in one.”

(for every x, y, z)if and

then

x y z

Transitivity (cont’d)• Note: xRy yRz corresponds to xRRz, which is

equivalent to xR×Rz = xR2z; thus we can define transitivity as:

xyz(xR2z xRz)“If I can get there in two, then I can get there in one.”

• For transitivity:– if reachable through an intermediate, then reachable

directly is required.– if (x,z)R2, then (x,z)R

(x,z)R2 (x,z)R R2 R (recall our def. of subset: xA xB, then AB)

Transitivity (cont’d)

1003

1002

1101

321

1003

1002

1101

321

1003

1002

1001

321

0003

1002

0101

321

0003

1002

0101

321

0003

0002

1001

321

=

=

R R R2 R

R R R2 R

1

2 3Not Transitive

Transitive

1

2 3

Closures & Equivalence Relations

Closure

Closure means adding something until done.– Normally adding as little as possible until some

condition is satisfied– Least fixed point similarities

Reflexive Closure

Reflexive closure of a relation: R(r) – smallest reflexive relation that contains R (i.e.

fewest pairs added)– R(r) = R IA (R is a relation on a set A, and IA

is the identity relation 1’s on the diagonal and 0’s elsewhere.)

1113

1002

0101

321

1113

1102

0111

321

R = R(r) =

x (xRx)

Symmetric Closure

Symmetric closure of a relation: R(s) – smallest symmetric relation that contains R (i.e.

fewest pairs added)– R(s) = R R~ (R~ is R inverse)

1113

1002

0101

321

1103

1012

1001

321

R = R~ =

1113

1012

1101

321

R R~ =

xy(xRy yRx)

Transitive Closure

Transitive closure of a relation: R(t) = R+

– smallest transitive relation that contains R (i.e. fewest pairs added)

– for each path of length i, there must be a direct path. (This follows from xy, yz xz; since, if we also have vx, we must have vz, a path of length 3.)

– R(t) = R R2 R3 … R|A|. (No path can be longer than |A|, the number of elements in A.)

RR2R3 =

R×R2 = R3 =

Transitive Closure – Example 1

1113

1002

0101

321

1113

1112

1001

321

R =

R2 =

1113

1112

1111

321

1

2 3

1

2 3

1

2 3

All paths of length 3

All paths of length 2

All paths of length 1

1113

1112

1111

321

Transitive Closure – Example 2

0103

1002

0001

321

1003

0102

0001

321

R =

R2 =

0103

1002

0001

321

R×R2 = R3 =

1

2 3

1

2 3

1

2 3

1103

1102

0001

321

RR2R3 =1

2 3

All paths of length 3

All paths of length 2

Paths of any length

All paths of length 1

Reflexive Transitive Closure• Reflexive transitive closure of a relation: R*

– smallest reflexive and transitive relation that contains R– R* = IA R+ = R0 R+ = R0 R1 R2 …R|A|

• Example:

1003

0102

0011

321

IA = R0 =

1103

1102

0011

321

1

2 3

R1 R2 R3 =

1103

1102

0001

321

R0 R1 R2 R3 =

Equivalence Relations A relation R on a set A is an equivalence relation if R

is reflexive, symmetric, and transitive.– Equivalence relations are about “equivalence”– Examples:

• = for integers x = x reflexivex = y y = x symmetricx=y y=z x=z transitive

• = for sets A = A reflexiveA = B B = A symmetricA=B B=C A=C transitive

• Let R be “has same major as” for college studentsxRx reflexive: same major as selfxRy yRx symmetric: same major as each otherxRy yRz xRz transitive: same as, same as

same as

Partitions• A partition of a set S is

– a set of subsets Si=1,2,…n of S– such that n

i=1 Si = S, Sj Sk = for j k.• Each Si is called a block (also called an equivalence class)• Example:

– Suppose we form teams (e.g. for a doubles tennis tournament) from the set:

{Abe, Kay, Jim, Nan, Pat, Zed}then teams could be:

{ {Abe, Nan}, {Kay, Jim}, {Pat, Zed} }• Note: “on same team as” is reflexive, symmetric,

transitive an equivalence relation. Equivalence relations and partitions are the same thing (two sides of the same coin).

Partitions (cont’d)• Since individual elements can only appear in one

block (Sj Sk = for j k), blocks can be represented by any element within the block.e.g. Nan’s Team

Jimmer’s 2011 sweet-16 team

• Formally, [x] = set of all elements related to x and y [x] iff xRy

e.g. [Nan] represents {Abe, Nan}, Nan’s team

[Abe] represents {Abe, Nan}, Abe’s team

[Jimmer] represents the set of players who played on BYU’s 2011 sweet-16 team

Partitions & Equivalence Relations

• The mod function partitions the natural numbers into equivalence classes.– 0 mod 3 = 0 so 0 forms a class [0]– 1 mod 3 = 1 so 1 forms new class [1]– 2 mod 3 = 2 so 2 forms new class [2]– 3 mod 3 = 0 so 3 belongs to [0] – 4 mod 3 = 1 so 4 belongs to [1]– 5 mod 3 = 2 so 5 belongs to [2]– 6 mod 3 = 0 so 6 belongs to [0] – …

• Thus, the mod function partitions the natural numbers into equivalence classes.– [0] = {0, 3, 6, …}– [1] = {1, 4, 7,…}– [2] = {2, 5, 8, …}

Example:

Partitions Equivalence Relations

Theorem: If {S1, …, Sn} is a partition of S, then R:SS is an equivalence relation, where R is “in same block as.”

Note: to prove that R is an equivalence relation, we must prove that R is reflexive, symmetric, and transitive.

Proof: Reflexive: since every element is in the same block as itself. Symmetric: since if x is in the same block as y, y is in the same block as x. Transitive: since if x and y are in the same block and y and z are in the same block, x and z are in the same block.

Equivalence Relations PartitionsTheorem: If R:SS is an equivalence relation and [x] = { y | xRy }, then { [x] | x S } is a partition P of S.

Note: to prove that we have a partition, we must prove (1) that every element of S is in a block of P, and (2) that for every pair of distinct blocks Sj and Sk (jk) of P, Sj Sk = .

Proof: (1) Since R is reflexive, xRx, every element of S is at least in its own block and thus in some block of P. (2) Suppose Sj Sk for distinct blocks Sj and Sk of P. Then, at least one element y is in both Sj and Sk. Let Sj = {y, x1, … xn} and Sk = {y, z1, … zm}, then yRxi, i=1, 2, …, n, and yRzp, p=1, 2, …, m. Since R is symmetric, xiRy, and since R is transitive xiRzp. But now, since the elements of Sj are R-related to the elements of Sk, x1, …, xn, y, z1, …, zm are together in a block of P and thus Si and Sk are not distinct blocks of P.

Partial Orders

Partial Orders• Total orderings: single sequence of elements• Partial orderings: some elements may come before/after others, but some need not be ordered• Examples of partial orderings:

foundation

framing

plumbing wiring

finishing

{a, b, c}

{a, b} {a, c} {b, c}

{a} {b} {c}

“must be completed before” “set inclusion, ”

Partial Order Definitions(Poset Definitions)

• A relation R: SS is called a (weak) partial order if it is reflexive, antisymmetric, and transitive.

1

2 3

• A relation R: SS is called a strict partial order if it is irreflexive, antisymmetric, and transitive.

1

2 3

e.g. on the integers

e.g. < on the integers

Total Order• A total ordering is a partial ordering in which every

element is related to every other element. (This forces a linear order or chain.)

• Examples:

R: on {1, 2, 3, 4, 5} is total.

Pick any two; they’re related one way or the other with respect to .

{a,b}

{a} {b}

R: on {{a, b}, {a}, {b}, } is not total.

We can find a pair not related one way or the other with respect to .

{a} & {b}: neither {a} {b} nor {b} {a}

1

2

3

4

5

Hasse Diagrams We produce Hasse Diagrams from directed graphs of

relations by doing a transitive reduction plus a reflexive reduction (if weak) and (usually) dropping arrowheads (using, instead, “above” to give direction)1) Transitive reduction discard all arcs except those that

“directly cover” an element.2) Reflexive reduction discard all self loops.

{b}{a}

{a, b}

{b}{a}

{a, b}

For we write:

Upper and Lower Bounds

• If a poset is built from relation R on set A, then any x A satisfying xRy is an upper bound of y, and any x A satisfying yRx is a lower bound of y.

• Examples: If A = {a, b, c} and R is , then {a, c}- is an upper bound of {a}, {c}, and . - is also an upper bound of {a, c} (weak poset).- is a lower bound of {a, b, c}.- is also a lower bound of {a, c} (weak poset).

Maximal and Minimal Elements• If a poset is built from relation R on set A, then y A is a

maximal element if there is no x such that xRy, and x A is a minimal element if there is no y such that xRy. (Note: We either need the poset to be strict or x y.)

• In a Hasse diagram, every element with no element “above” it is a maximal element, whereas every element with no element “below” it is a minimal element.

Maximal elements

Minimal elements

Least Upper and Greatest Lower Bounds• A least upper bound of two elements x and y is a

minimal element in the intersection of the upper bounds of x and y.

• A greatest lower bound is a maximal element in the intersection of the lower bounds of x and y.

• Examples:– For , {a, c} is a least upper bound of {a} and {c},

is a greatest lower bound of {a} and {b, c}, and{a} is a least upper bound of {a} and .

– For the following strict poset, lub(x,y) = {a,b}, lub(y,y) = {a,b,c}, lub(a,y) = , glb(a,b) = {x,y}, glb(a,c) = {y}

a b

x y

c

Functions

• A function is a special kind of binary relation.

• A binary relation f A B is a function if for each a A there is a unique b B

Function Definition

1

2

3

α

β

γ

x

y

NOT Functions

1

2

3

α

β

γ

f = {(1, α), (2, β)} “For each” violated

Some x’s do not have corresponding y’s

x

y

NOT Functions

Uniqueness violated for some x’s

x

y

1

2

3

α

β

γ

f = {(1, α), (2, β), (3, β), (3, γ)} uniqueness violated for 3 appears twice

An (n+1)-ary relation f A1 A2 … An B is a function if for each < a1, a2, …, an> A1 A2 … An there is a unique b B.

Functionswith N-Dimensional Domains

α

β

γ

<1,1>

<1,2>

<1,3>

• We can use various notation for functions: for f = {(1, α),(2, β),(3, β)}

Notation for Functions

Notation (x, y) f f : x→y y = f(x)

Example (2, β) f f : 2→β β = f(2)

• In the notation, x is the argument or preimage and y is the image. We can also have the image of a set of arguments.

• For functions with n-ary domains, use <x0, x1, …, xn> in place of x.

Function Domain and Range

• f : A → B– A is the domain space

• same as the domain (since all elements participate)• dom f, dom(f), or domain(f)

– B is the range space• may or may not be the same as the range, which is:

– {y | x(y=f(x))}– All rhs values in pairs (all that get “hit”) Bf

• ran f, ran(f), range(f)

• f : D1 D2 … Dn → Z• f : Dn → Z (when all domains are the same)

Remove the requirement that each a A must participate. Retain the uniqueness requirement.

Partial Functions

Partial Function:

α

β

γ

f = {(<1,2>, β),(<1,3>, β),(<1,3>, γ)} <1,3> not unique

<1,1>

<1,2>

<1,3>

α

β

γ

<1,1>

<1,2>

<1,3>

NOT a Partial Function:

α

β

γ

<1,1>

<1,2>

<1,3>

Partial Function: (A Total Function is also a Partial Function.)

• Identity Function– IA : A → A

– IA = {(x, x) | x A}

• Constant Function– C : A → B– C = {(x, c) | x A c B }– Often A and B are the same

• C : A → A• C= {(x, c) | x A c A}

Special Functions

Overloading• The same name can be used for different

functions depending on the domain.• Examples

a – b• Means number subtraction if a and b are numbers• Means set subtraction if a and b are sets

a + b• Implemented differently for integers and reals• Could potentially be overridden and implemented

by a programmer for very long integers

Composition of Functions

• Composition is written “°”

• Range space of f = domain space of g

a

c

1

2

4

fg

b

α

β3

f(a) = 2 g(2) = α g(f(a)) = α

g°f(a) = α

f(b) = 2 g(2) = α g(f(b)) = α

g°f(b) = α

f(c) = 4 g(4) = β g(f(c)) = β

g°f(c) = β

Injection: “one-to-one” or “1-1” xy(f(x) = f(y) x = y)– For f : A → B, the elements in B are “hit” at most once

Injection

a

b

d

1

2

3

c

Injective

a

b

d

1

2

3

c

NOT Injective

x

y

x

y

Surjection: “onto” yx(y = f(x))– For f : A → B, the elements in B are all “hit” at least once

Surjection

1

2

4

a

b

c3

Surjective NOT Surjective

x

y

x

y

1

2

4

a

b

c3

{ not “hit”

Bijection: “one-to-one and onto” or “1-1 correspondence” xy(f(x) = f(y) x = y) yx(y = f(x))– For f : A → B, every B element is “hit” once and only once

Bijection

1

2

a

b

c3

Bijective NOT Bijective

x

y

x

y

1

2

4

a

b

c3

NOT Surjective

NOT injective

Notes on Bijection1. |A| = |B|

– An “extra” B cannot be “hit” (not a surjection)

– An “extra” A requires that at least one B must be “hit” twice (not an injection)

2. If f is a bijection, swapping the elements of the ordered pairs is a function

– Called the inverse

– Denoted f-1

– Is also a bijection

– f-1(f(x)) is the identity function, i.e. f-1(f(x)) = x.

Notes on Bijection (cont’d)3. The inverse of an injection is a partial function.

If f : A →B is an injection, then f-1 is a partial function

4. Restricting the range space of an injective function to the range yields a bijection

Remove b

a

b

d

1

2

3

c

f

a

b

d

1

2

3c

f-1

Which is bigger? NN or R[0..1]R[0..1]?

Assume |NN| = |R[0..1]R[0..1]|, then there exists a bijection:

1 0.34234…2 0.34987… diagonalization3 0.00040…

But now there exists a number in R[0..1] R[0..1] such that d1 = not 3, d2 = not 4, d3 = not 0, … . Hence, not surjective and thus not bijective.

Which is bigger? NN or ZZ?

f(x) =x odd: (x+1)/−2

x even: x/2

y negative: −2x−1

y positive: 2xg(y) =

{{

x y 0 0 1 −1 2 1 3 −2 4 2

Since g = f−1, there is a bijection from NN to ZZ and thus |NN| = |ZZ|.

Countable if same cardinality as some subset of NN.

Alternatively, a set S is countable if there exists an injective function from S to NN.