14/09/2015
Reminder About Basic Properties of Relations
Definitions:
VCU, Department of Computer Science A relation R on a set A is called reflexive if (a, a)R for every element aA. CMSC 302 Relations 2 A relation R on a set A is called symmetric if (b, a)R Vojislav Kecman whenever (a, b)R for all a, bA.
A relation R on a set A is called transitive if whenever (a, b)R and (b, c)R, then (a, c)R for a, b, c A.
14/09/2015 1/57 14/09/2015 2/57
Representing Relations Representing Relations
•Example: How can we represent the relation R defined •We already know different ways of representing relations. We will between the set A{1, 2, 3} and set B{1, 2} where now take a closer look at two ways of representation: Zero-one R = {(2, 1), (3, 1), (3, 2)} as a zero-one matrix? matrices and directed graphs (digraphs).
•If R is a relation from A = {a1, a2, …, am} to B = •Solution: The matrix MR is given by {b1, b2, …, bn}, then R can be represented by the zero-one matrix MR = [mij] with
•mij = 1, if (ai, bj)R, and 00 •mij = 0, if (ai, bj)R. M 10 R •Note that for creating this matrix we first need to list the elements 11 in A and B in a particular, but arbitrary order.
14/09/2015 3/57 14/09/2015 4/57
1 14/09/2015
Representing Relations Representing Relations
•What do we know about the matrices representing a relation •What do we know about the matrices representing symmetric on a set (a relation from A to A) ? relations? t •They are square matrices. •These matrices are symmetric, that is, MR = (MR) . •What do we know about matrices representing reflexive relations? 1011 110 0 •All the elements on the diagonal of such matrices Mref must be 1s. 0100 110 0 M R M R 1 1001 110 0 1 1011 110 0 . M ref . •symmetric matrix, •non-symmetric matrix, . symmetric relation. non-symmetric relation. 1 14/09/2015 5/57 14/09/2015 6/57
Representing Relations Zero-One Reflexive, Symmetric •Terms:Reflexive, non-reflexive, irreflexive, symmetric, asymmetric, and antisymmetric. •The Boolean operations join and meet can be used to – These relation characteristics are very easy to determine the matrices representing the union and the recognize by inspection of the zero-one matrix. intersection of two relations, respectively.
1 0 1 0 •To obtain the join of two zero-one matrices, we apply the any- any- Boolean “or” function to all corresponding elements in the a thing thing a n n yt 1 0 yt 0 h matrices. 1 h in 1 in 1 g g 1 0 any- any- •To obtain the meet of two zero-one matrices, we apply the thing 1 thing 0 0 0 Boolean “and” function to all corresponding elements in the matrices. Symmetric: Antisymmetric: Reflexive: Irreflexive: all identical all 1’s are across all 1’s on diagonal all 0’s on diagonal across diagonal from 0’s 14/09/2015 7/57 14/09/2015 8/57
2 14/09/2015
Representing Relations Using Matrices Representing Relations Using Matrices
•Example: Let the relations R and S be represented by the matrices -Do you remember the Boolean product of two zero-one 101 101 matrices? M 100 M 011 R S -Let A = [aij] be an mk zero-one matrix and 010 100 B = [bij] be a kn zero-one matrix. •What are the matrices representing RS and RS? -Then the Boolean product of A and B, denoted by AB, is •Solution: These matrices are given by the mn matrix with (i, j)th entry [cij], where
-cij = (ai1 b1j) (ai2 b2i) … (aik bkj). 101 101 -cij = 1 if and only if at least one of the terms MMM111 MMMRS R S 000 RS R S (a b ) = 1 for some n; otherwise c = 0. 000 in nj ij 110
14/09/2015 9/57 14/09/2015 10/57
Representing Relations Using Matrices Representing Relations Using Matrices
-Let us now assume that the zero-one matrices -This gives us the following rule: MA = [aij], MB = [bij] and MC = [cij] represent relations A, B, and C, respectively. -MBA = MAMB
-Remember: For MC = MAMB we have: -In other words, the matrix representing the composite of relations A and B is the Boolean product of the matrices -cij = 1 if and only if at least one of the terms representing A and B. (ain bnj) = 1 for some n; otherwise cij = 0. -In terms of the relations, this means that C contains a pair (x , i -Analogously, we can find matrices representing the powers z ) if and only if there is an element y such that (x , y ) is in j n i n of relations: relation A and [n] (yn, zj) is in relation B. -MRn = MR (n-th Boolean power). -Therefore, C = BA (composite of A and B).
14/09/2015 11/57 14/09/2015 12/57
3 14/09/2015
Representing Relations Using Matrices Representing Relations Using Digraphs
•Example: Find the matrix representing R2, where the matrix representing R is given by •Solution: The matrix for R2 is given by 010 •Definition: A directed graph, or digraph, consists of a set V M 011 of vertices (or nodes) together with a set E of ordered pairs of R 011 elements of V called edges (or arcs). 100 [2] MM2 111 R R •The vertex a is called the initial vertex of the edge (a, b), and 010 the vertex b is called the terminal vertex of this edge. How did we get this one? Well, … •We can use arrows to display graphs. 010 010 (00)(10)(01) 1 1 011 011 1 (01)(11)(10) 1 100 100 0 1 (10)(01)(00)
14/09/2015 13/57 14/09/2015 14/57
Representing Relations Using Digraphs Using Directed Graphs •Example: Display the digraph with V = {a, b, c, d}, E = {(a, b), (a, d), (b, b), (b, d), (c, a), (c, b), (d, b)}. •Def.A directed graph or digraph G=(VG,EG) is a set VG of vertices (nodes) with a set EGVG×VG of edges a (arcs,links). Visually represented using dots for nodes, b and arrows for edges. Notice that a relation R:A×B can
be represented as a graph GR=(VG=AB, EG=R).
Edge set EG Matrix representation MR: Graph representation GR: (blue arrows) Susan Mary Sally Joe Susan d c Joe 1 1 0 Fred Mary Fred 0 1 0 Mark 0 0 1 Mark Sally •An edge of the form (b, b) is called a loop.
Node set VG (black dots) 14/09/2015 15/57 14/09/2015 16/57
4 14/09/2015
Digraph Reflexive, Symmetric Representing Relations Using Digraphs
• It is extremely easy to recognize the reflexive/irreflexive/ symmetric/antisymmetric •Obviously, we can represent any relation R on a set A by the properties by graph inspection. digraph with A as its vertices and all pairs (a, b)R as its edges. •Vice versa, any digraph with vertices V and edges E can be represented by a relation on V containing all the pairs in E. •This one-to-one correspondence between relations and digraphs means that any statement about relations also applies Reflexive: Irreflexive: Symmetric: Antisymmetric: to digraphs, and vice versa. Every node No node Every link is No link is has a self-loop links to itself bidirectional bidirectional This then means that digraphs are sets, and that all the set These are asymmetric & non-antisymmetric These are non-reflexive & non-irreflexive operations apply. We’ll use it in closures which come next! 14/09/2015 17/57 14/09/2015 18/57
Closures of Relations, or Closures of Relations Relational Closures •Def.For any property X, the “X closure” of a set A is defined as the “smallest” superset of A that has the given property.
• Three types we will study The reflexive closure of a relation R on A is obtained by
adding (a, a) to R for each aA. i.e.,it is R IA – Reflexive The symmetric closure of R is obtained by adding (b,a) to R •Easy for each (a, b) in R. i.e., it is R RT(note in book is R-1 used) –Symmetric • The transitive closure or connectivity relation of R is obtained by repeatedly adding (a,c) to R for each (a,b),(b,c) in •Easy R., i.e., it is
* n 23nn 1 –Transitive R R R RR R R •Hard nZ 14/09/2015 19/57 14/09/2015 20/57
5 14/09/2015
Reflexive closure Reflexive closure
• Consider a relation R: • In order to find the reflexive closure of a – Note that it is not reflexive g relation R, we add a loop at each node • We want to add edges to that does not have one make the relation reflexive f c d • By adding those edges, e • The reflexive closure of R is R U we have made a non- b reflexive relation R into –Where = { (a, a) | a R } a reflexive relation • Called the “diagonal relation” a – With matrices, we set the diagonal to all 1’s, • This new relation is called the reflexive closure of R meaning = diag([1 1 1 … 1])
14/09/2015 21/57 14/09/2015 22/57
Reflexive closure example Reflexive closure example with matrices
• Let R be a relation on the set { 0, 1, 2, 3 } containing the • Let R be a relation on the set { 0, 1, 2, 3 } containing the ordered pairs ordered pairs (0,1), (1,1), (1,2), (2,0), (2,2), and (3,0) (0,1), (1,1), (1,2), (2,0), (2,2), and (3,0) • What is the reflexive closure of R? • What is the reflexive closure of R? • We add all pairs of edges (a,a) that do not already exist • We ‘add’ a diagonal matrix with ones, called also identity matrix 0100 10001 100 01 0110 0100 0110 We add edges: R (0,0), (3,3) 1010 0010 1010 1000 0001 1001
32 We add entries: (0,0), (3,3)
14/09/2015 23/57 14/09/2015 24/57
6 14/09/2015
Symmetric closure Symmetric closure
• Consider a relation R: • In order to find the symmetric closure of a – Note that it is not symmetric g relation R, we add an edge from a to b, • We want to add edges to where there is already an edge from b to a make the relation symmetric f c d T • By adding those edges, e • The symmetric closure of R is R U R we have made a non- b symmetric relation R into –If R = { (a,b) | … } a symmetric relation – Then RT = { (b,a) | … } a • This new relation is called the symmetric closure of R
14/09/2015 25/57 14/09/2015 26/57
Symmetric closure example Symmetric closure example with matrices • Let R be a relation on the set { 0, 1, 2, 3 } containing the • Let R be a relation on the set { 0, 1, 2, 3 } containing the ordered pairs (0,1), (1,1), (1,2), (2,0), (2,2), and (3,0) ordered pairs (0,1), (1,1), (1,2), (2,0), (2,2), and (3,0) • What is the symmetric closure of R? • What is the symmetric closure of R? • We add all pairs of edges (a,b) where (b,a) exists • We add all pairs of edges (a,b) where (b,a) exists – We make all “single” edges into anti-parallel pairs – We make all “single” edges into anti-parallel pairs 0100 0011 0111 01 0110 11001 110 We add edges: R 1010 (0,2), (0,3) 0110 11 10 (1,0), (2,1) 1000 0000 1000
32 We add edges: (0,2), (0,3), (1,0), (2,1) 14/09/2015 27/57 14/09/2015 28/57
7 14/09/2015
Transitive closure Transitive closure
• Informal definition: If there is a path from a to b, then • Informal definition: If there is a path from a to b, then there should be an edge from a to b in the transitive there should be an edge from a to b in the transitive closure closure • First take of a definition: • Second take of a definition: – In order to find the transitive closure of a relation R, we add an – In order to find the transitive closure of a relation R, we add an edge from a to c, when there are edges from a to b and b to c edge from a to c, when there are edges from a to b and b to c • But there is a path from 1 to 4 with no edge! – Repeat this step until no new edges are added to the relation • We will study matrix algorithm (out of few different (1,2) & (2,3) (1,3) ways) for determining the transitive closure 4 R = { (1,2), (2,3), (3,4) } (2,3) & (3,4) (2,4) 4 4 •redmeans added on the first repeat (that will be in R2) 1 1 1 • teal means added on 23 23 23 the second repeat (that will be in R3)
14/09/2015 29/57 14/09/2015 30/57
Transitive closure – Matrix Algorithm Connectivity relation 4 R = { (1,2), (2,3), (3,4) } • R contains edges between all the nodes reachable via 1 edge 1 MR • R◦R = R2 contains edges between nodes that are reachable via 2 23 edges in R (first repeat) 0100 0100 001 0 • R2◦R = R3 contains edges between nodes that are reachable via 3 edges in R (second repeat) 0010 0010 0001 • Rn = contains edges between nodes that are reachable via n edges M 2 in R R 0001 0001 0000 • R* contains edges between nodes that are reachable via any 0000 0000 0000 number of edges (i.e. via any path) in R – Rephrased: R* contains all the edges between nodes a and b when is a path of length at least 1 between a and b in R 001 0 0100 0001 • R* is the transitive closure of R 0001 0010 0000 – The definition of a transitive closure is that there are edges between any M 3 R nodes (a,b) that contain a path between them 0000 0001 0000… it continues on slide 35 0000 0000 0000 14/09/2015 31/57 14/09/2015 32/57
8 14/09/2015
Finding the transitive closure Continuation of slide 33 with matrices •Let M be the zero-one matrix of the relation R R 011 1 on a set with n elements. Then the zero-one matrix of the transitive closure R* is: 0011 • Now you prove that M R* 0001 M M M[2] M[3] M[n] R* R R R R 0000
Nodes reachable Nodes reachable Nodes reachable with one application with two applications with n applications 4 of the relation of the relation of the relation 1 23
14/09/2015 33/57 14/09/2015 34/57
Sample questions Sample questions
• Find the zero-one matrix of the transitive 1 1 1 1 0 1 1 1 1 M[3] M[2] M 0 1 0 0 1 0 0 1 0 closure of the relation R given by: R R R 1 1 1 1 1 0 1 1 1 1 0 1 1212 M 0 1 0 12 R M[2] 1 1 0 R
[2] [3] 3 3 M R* M R M R M R 3 1 0 1 1 0 1 1 1 1 M[2] M M 0 1 0 0 1 0 0 1 0 1 0 1 1 1 1 1 1 1 1 1 1 R R R M 0 1 0 0 1 0 0 1 0 0 1 0 1 1 0 1 1 0 1 1 1 R* 1 1 0 1 1 1 1 1 1 1 1 1 14/09/2015 35/57 14/09/2015 36/57
9 14/09/2015
Transitive closure algorithm Transitive closure algorithm
• What we did (or rather, could have done): procedure transitive_closure (MR: zero-one nn matrix) [i] A := MR – Compute the next matrix M R , where 1 ≤ i ≤ n B := A – Do a Boolean join with the previously for i := 2 to n computed matrix begin • For our example: A := A MR [2] –Compute M R M R M R B := B A [2] end { B is the zero-one matrix for R* } – Join that with M R to yield M R M R [3] [2] –Compute M R M R M R [2] – Join that with M R M R from above
14/09/2015 37/57 14/09/2015 38/57
Equivalence Relations More transitive closure algorithms
• More efficient algorithms exist, such as •Equivalence relations are used to relate Warshall’s algorithm objects that are similar in some way. – We won’t be studying it in this class •Definition: A relation on a set A is called an equivalence relation if it is reflexive, symmetric, and transitive.
•Two elements that are related by an equivalence relation R are called equivalent.
14/09/2015 39/57 14/09/2015 40/57
10 14/09/2015
Equivalence Relations Equivalence Relations
•Since R is reflexive, every element is equivalent to itself. (For every aS, aRa).
•Since R is symmetric, a is equivalent to b whenever More equivalency: b is equivalent to a. (If aRb then bRa) •Since R is transitive, if a and b are equivalent and b and c are equivalent, then a and c are equivalent. (If aRb and bRc then aRc).
•Obviously, these three properties are necessary for a reasonable definition of equivalence.
14/09/2015 41/57 14/09/2015 42/57
Proof that ‘congruence modulo Equivalence Relation m’ is an equivalence relation More Examples 11 3 (mod 4), • “Strings a and b are the same length.”(see because it's reflexive 11 11 (mod 4), next slide) it's symmetric 3 11 (mod 4), • “Integers a and b have the same absolute and it is transitive value.” • “Integers a and b have the same residue 11 3 (mod 4) and 3 1 (mod 4), modulo m.” (for a given m>1, see results into 11 1 (mod 4). previous slide)
14/09/2015 43/57 14/09/2015 44/57
11 14/09/2015
Equivalence Relations Equivalence Classes
•Example: Suppose that R is the relation on the set of strings that consist of English letters such that aRb if •Definition: Let R be an equivalence relation on a set A. The and only if L(a) = L(b), where L (x) is the length of the set of all elements that are related to an element a of A is string x. Is R an equivalence relation? called the equivalence class of a. •Solution: •The equivalence class of a with respect to R is denoted by • R is reflexive, because L(a) = L(a) and therefore [a]R. aRa for any string a. •When only one relation is under consideration, we will delete • R is symmetric, because if L(a) = L(b) then L(b) = the subscript R and write [a] for this equivalence class. L(a), so if aRb then bRa. •If b[a]R, b is called a representative of this equivalence • R is transitive, because if L(a) = L(b) and L(b) = L(c), class. then L(a) = L(c), so aRb and bRc implies aRc. •R is an equivalence relation.
14/09/2015 45/57 14/09/2015 46/57
Equivalence Classes Equivalence Classes •Theorem: Let R be an equivalence relation on a set A. The following statements are equivalent: •Example: In the previous example (strings of identical •aRb length), what is the equivalence class of the word mouse, • [a] = [b] denoted by [mouse] ? •[a] [b]
•Solution: [mouse] is the set of all English words containing five letters. •Definition: A partition of a set S is a collection of disjoint nonempty subsets of S that have S as their union. In other •For example, ‘horse’ would be a representative of this words, the collection of subsets Ai, equivalence class. iI, forms a partition of S if and only if
( i ) Ai for iI
( ii ) Ai Aj = , if i j
( iii ) iI Ai = S 14/09/2015 47/57 14/09/2015 48/57
12 14/09/2015
Examples of s Equivalence Classes partitions a •Examples: Let S be the set {u, m, b, r, o, c, k, s}. 1 S={a, b, M, p, 1, &} Do the following collections of sets partition S ? M &
p •{{m, o, c, k}, {r, u, b, s}} •yes. b a NO •{{c, o, m, b}, {u, s}, {r}} •no (k is missing). 1 M & •{{b, r, o, c, k}, {m, u, s, t}} •no (t is not in S). NO p a •{{u, m, b, r, o, c, k, s}} •yes. 1 M & •{{b, o, o, k}, {r, u, m}, {c, s}} •yes ({b,o,o,k} = {b,o,k}). YES p •{{u, m, b}, {r, o, c, k, s}, } •no ( not allowed).
14/09/2015 49/57 14/09/2015 50/57
Equivalence Classes Equivalence Classes •Example: Let us assume that Frank, Suzanne and George live in Boston, Stephanie and Max live in Lübeck, and Sava lives in Belgrade. •Theorem: Let R be an equivalence relation on a •Let R be the equivalence relation {(a, b) | a and b set S. live in the same city} on the set P = {Frank, Suzanne, George, Stephanie, Max, Sava}. •Then the equivalence classes of R form a partition of S. Conversely, given a partition •Then R = {(Frank, Frank), (Frank, Suzanne), (Frank, George), (Suzanne, Frank), (Suzanne, {Ai | iI} of the set S, there is an equivalence Suzanne), (Suzanne, George), (George, Frank), relation R that has the sets Ai, iI, as its (George, Suzanne), (George, George), (Stephanie, equivalence classes. Stephanie), (Stephanie, Max), (Max, Stephanie), (Max, Max), (Sava, Sava)}. … it continues
14/09/2015 51/57 14/09/2015 52/57
13 14/09/2015
Equivalence Classes Equivalence Classes •Another example: Let R be the relation •Then the equivalence classes of R are: {(a, b) | a b (mod 3)} on the set of integers. •{{Frank, Suzanne, George}, {Stephanie, Max}, {Sava}}. •Is R an equivalence relation? •This is a partition of P. •Yes, R is reflexive, symmetric, and transitive.
•The equivalence classes of any equivalence relation R defined on a set S constitute a partition of S, •What are the equivalence classes of R ? because every element in S is assigned to exactly •{{…, -6, -3, 0, 3, 6, …}, one of the equivalence classes. {…, -5, -2, 1, 4, 7, …}, {…, -4, -1, 2, 5, 8, …}} 14/09/2015 53/57 14/09/2015 54/57
Quick survey Quick survey
• I understood the material in this slide set… The pace of the lecture for this slide set was… a) Very well, or close b) With some review, I’ll be good a) Fast c) Not really b) About right d) Not at all c) A little slow d) Too slow
14/09/2015 55/57 14/09/2015 56/57
14 14/09/2015
Quick survey • How interesting was the material in this slide set? Be honest!
a) Wow! That was cooooooooool! b) Somewhat interesting c) Rather boring
d) zzzzzzzzzzzzzzZZZZZZZZZZZ
14/09/2015 57/57
15