8.3 Representing Relations • Directed Graphs – Vertex – Arc (directed edge) – Initial vertex – Terminal vertex Example • Draw the “divides” relation on the set {2,3,4,5,6,7,8,9} as a directed graph The zero-one Matrix Representation MR • MR is just a zero-one version of the “chart” representation of R. 2 3 4 5 6 7 8 9 2 1 0 0 0 0 0 0 0 3 0 1 0 0 0 0 0 0 4 1 0 1 0 0 0 0 0 5 0 0 0 1 0 0 0 0 6 1 1 0 0 1 0 0 0 7 0 0 0 0 0 1 0 0 8 1 0 1 0 0 0 1 0 9 0 1 0 0 0 0 0 1 Reflexivity Directed graph picture Zero-one matrix picture Symmetry Directed graph picture Zero-one matrix picture Antisymmetry Directed graph picture Zero-one matrix picture Transitivity Directed graph picture Zero-one matrix picture Theorem For relations R1 and R2 on set A, M R1 R2 M R1 M R2 , M R1 R2 M R1 M R2 , and M R1 R2 M R2 M R1 Example: Let 𝑅1 = { 1,2 , 2,1 , 2,2 , 3,3 } and 𝑅2 = { 1,1 , 1,2 , 1,3 , 3,2 } be binary relations. Find 𝑀𝑅1 and 𝑀𝑅2 . Use them to find 𝑀𝑅1∘𝑅2 . Verify by calculating 𝑅1 ∘ 𝑅2 without matrices. Corollary For a relation R on set A, M R n M R [n ] for any positive integer n. 1 0 1 1 1 0 Example: Let 𝑀𝑅 = 0 0 1 and 𝑀𝑆 = 0 0 0 . Calculate 𝑀𝑅𝑛 and 1 1 0 1 1 1 𝑀𝑆 𝑛 to determine if the relations 𝑅 and 𝑆 are transitive. 8.4 Closures of Relations • Reflexive closure • Symmetric closure Example: Let R be the relation on the set {1,2,3,4} containing the pairs { 1,1 , 1,2 , 2,4 , 3,1 , 4,2 } What is the reflexive closure of R? What is the symmetric closure of R? Paths in Directed Graphs • A path in a directed graph is a sequence of vertices for which any two consecutive vertices ai and ai+1 in the sequence are joined by an arc from ai to ai+1. • Theorem: Let R be a relation on set A, and n a positive integer. Then there is a path of length n from a to b in R if and only if (a,b) is in Rn. Example: The “Connectivity Relation” R* • Let R be a relation on set A. We define R R * i 1 i Example: Let 𝑅 be the relation on the set of all people in the world that contains (𝑎, 𝑏) if 𝑎 has met 𝑏. What is 𝑅𝑛 , where 𝑛 is a positive integer greater than one? What is 𝑅∗ ? The Transitive Closure • For a relation R on a set A, we define the transitive closure of R to be the smallest transitive relation containing R. • Theorem: Finding transitive closure the “hard” way: 𝑅 = { 1,1 , 1,2 , 2,4 , 3,1 , 4,2 } Computing R* • If A is a set with n elements, and R is a relation on A, then any time there is a path of length one or more from a to b in R then there is a path of length n or less. • So actually n R R * i i 1 and M R* M n i 1 [i ] R • Interestingly, this is not the best way of computing R* 𝑅 = { 1,1 , 1,2 , 2,4 , 3,1 , 4,2 } Computing transitive closure the better way: 1 𝑀𝑅 = 0 1 0 ⋁ 1 0 0 1 0 0 0 0 𝑀𝑅 [3] 0 1 0 0 ⋁ 1 = 0 1 0 1 0 1 1 𝑀𝑅 0 0 0 0 [2] 1 = 0 1 0 1 1 1 0 1 = 𝑀𝑅 ∗ = 0 1 0 1 1 1 0 0 0 0 0 ⋁ 1 1 1 1 1 0 0 1 𝑀𝑅 0 0 0 0 1 1 1 1 [4] 1 = 0 1 0 1 1 1 0 0 0 0 0 1 0 1 1 Warshall’s Algorithm procedure Warshall(MR: n by n zero-one matrix) W := MR for k:=1 to n for i:=1 to n for j:=1 to n wij := wij (wik wkj) { W now contains MR* } Illustration of Warshall’s Algorithm 1 𝑀𝑅 = 0 1 0 1 0 0 1 0 0 0 0 0 1 0 0 1 0 1 0 1 0 0 1 0 0 0 0 1 0 1 0 0 1 0 1 0 1 0 1 0 1 0 0 0 0 1 1 0 0 1 0 1 0 1 1 0 1 1 0 0 0 0 1 1 0 0 1 0 1 0 1 1 1 1 0 0 0 0 1 1 0 1 0 1 0 1 0 1 1 1 1 0 0 0 0 1 1 1 0 1 8.5 Equivalence Relations • Definition: A relation R on set A is an equivalence relation if … Examples • aRb if and only if a and b have the same first name (on the set of students in this class) • aRb if and only if a ≡ b (mod 5) (on the set of integers) Equivalence Classes • If R is a relation on set A, and a is an element of A, then… Examples (continued) • [Michael] • [4]5 Theorem For an equivalence relation R on set A and elements a and b of A, the following are all logically equivalent: a) a R b b) [a]R = [b]R c) [a]R [b]R Partitions • For a set S, a partition of S is a collection = {A1, A2, …, Am} of nonempty subsets of S which satisfies the following properties: – Every element of A is in one of the sets Ai. – For all i, j {1, 2, …, m}, if i j then Ai Aj = • Terminology: We say that the collection partitions S. Theorem • Let R be an equivalence relation on set S. Then the equivalence classes of R partition S. Conversely, for any partition of S there is an equivalence relation R whose equivalence classes are the sets in . Visual …,-10, …, ,-9, , , ,-7, ,-8, , , ,-6, , , ,-5, ,-4, ,-3, , , ,-2, ,-1, , ,0, , , , ,1, ,2, ,3, , , , ,4, ,5, ,6, , , , ,7, ,8, ,9, , , , … ,10, [2]={… ,-10, -7, -4, -1, 2, 5, 8, …} [0]={… ,-9, -6, -3, 0, 3, 6, 9, …} [1]={… ,-8, -5, -2, 1, 4, 7, 10, …} Visual [2]={… ,-10, -7, -4, -1, 2, 5, 8, …} [0]={… ,-9, -6, -3, 0, 3, 6, 9, …} [1]={… ,-8, -5, -2, 1, 4, 7, 10, …} …,-10, , …, ,-9, …, , ,-7, , ,-8, , ,-6, , ,-4, , ,-5, , ,-3, , ,-1, , ,2, , ,5, , ,8, , , … , , … ,-2, ,0, , ,3, , ,6, , ,9, , ,1, , ,4, , ,7, , ,10, … Example: Turning a partition into an equivalence relation 𝐴1 = 1,5 , 𝐴2 = 2,3,6 , 𝐴3 = 4 𝑜𝑛 𝑡ℎ𝑒 𝑠𝑒𝑡 𝑆 = {1,2,3,4,5,6} Zero-One Matrix Representation of an Equivalence Relation Examples Digraph Representation of an Equivalence Relation Examples Equivalence as “sameness” • Almost every equivalence relation definition comes down to identifying some notion of “sameness” – Same remainder when divided by n – Same name – Same set of a partition Number of Partitions of a Set with n Elements • n=1 • n=2 • n=3 • n=4 Recurrence Relation for the Number of Partitions of a Set with n Elements 8.6 Partial Orderings Let A be a set, and R a relation on A. We say that R is a partial ordering if and only if… In this case we say that the pair (A, R) is a partially ordered set (poset). Examples: 1. The real numbers R under the relation 2. The real numbers R under the relation 3. The positive integers under the “divides” relation 4. Any set of sets under the (subset) relation 5. The cartesian product ZZ under the “(,)” relation R. (i.e. (x,y) R (z,w) if and only if x z and y w.) Convention • The symbol ≼ is the default symbol used to represent a partial ordering. • Example: “Let A be a set, and let ≼ be a partial ordering on A.” Comparable and Incomparable Elements • Two elements a and b of a partially ordered set are said to be incomparable if and only if the statements a ≼ b and b ≽ a are both false. Otherwise the elements are comparable. • Examples: – Subsets – Cartesian products – Divides relation Examples: Total Orderings • Let A be a set, and let ≼ be a partial ordering on A. We say that ≼ is a total ordering provided… In this case we say that the pair (A, ≼) is a totally ordered set. (linearly ordered set, chain) • Examples: 1) Real numbers under 2) Any set of strings under the “dictionary”, or lexicographic, ordering A partial order on 𝐴1 × 𝐴2 “induced” by partial orders ≼1 on 𝐴1 and ≼2 on 𝐴2 • Lexicographic ordering ≼ • Example: Well-Ordered Sets • A set S is well-ordered by the partial ordering ≼ if and only if every nonempty subset of S has a least element (minimum element). • Examples: ℕ, ℤ+ • Non-Examples: ℝ+ , ℚ+ , ℤ Hasse Diagrams • Begin with the digraph representation of the partial ordering • Omit the reflexive loops • Omit all edges which would be implied by transitivity • Orient all vertices and arcs so that the direction of each arc is up. • Remove the direction arrow from each arc Hasse Diagram Example I • Pairs in {1,2,3}{1,2,3} under lexicographic order Hasse Diagram Example I • Pairs in {1,2,3}{1,2,3} under lexicographic order Hasse Diagram Example I • Pairs in {1,2,3}{1,2,3} under lexicographic order Hasse Diagram Example I • Pairs in {1,2,3}{1,2,3} under lexicographic order Hasse Diagram Example I • Pairs in {1,2,3}{1,2,3} under lexicographic order Hasse Diagram Example II • Integers 1-12 under “divides” Hasse Diagram Example III • Integers 1-12 under “divides” Maximal and Minimal Elements Given a poset (𝑆, ≼) and an element 𝑎 ∈ 𝑆, we say • 𝑎 is maximal in the poset if • 𝑎 is minimal in the poset if • 𝑎 is the greatest element of the poset if • 𝑎 is the least element of the poset if Theorems • Every finite poset has a minimal element • Every finite poset has a maximal element Example: Maximal Elements? Minimal Elements? Greatest Element? Least Element? Upper Bounds and Lower Bounds Given a subset 𝐴 of a poset (𝑆, ≼) we say • 𝑢 is an upper bound of 𝐴 if • 𝑙 is a lower bound of 𝐴 if • 𝑥 is the least upper bound of 𝐴 if • 𝑦 is the greatest lower bound of 𝐴 if Example: Find the upper and lower bounds of the subsets {c,f}, {h,i}, {c,d,e}. Find the greatest lower bound and the least upper bound of {b,d} and {a,b,g}. Topological Sorts • Let (A,≼) be a partial ordering, and let ≤ be a total ordering on the same set A. We say that ≤ is a compatible total ordering for ≼ provided that for all a, b in A, whenever a ≼ b then also a ≤ b. • A topological sort is an algorithm which, given a partially ordered set, generates a compatible total ordering. In other words, it generates the elements of A one by one in a linear order compatible with the partial ordering. (PERT charts) Topological Sort Algorithm Procedure TopologicalSort(S: finite poset) Let q be a queue of elements of S, initially empty While S is not empty, do begin Choose and enqueue a minimal element s of S onto the queue q. Remove s from S End { The queue q now contains all the elements of S, arranged in a compatible total ordering of S. } Topological Sort Trace