270 likes | 398 Views
CS1022 Computer Programming & Principles. Lecture 1 Relations. Plan of lecture. Motivation Binary relations Graphs to represent relations Arrays to represent relations Properties of relations Closure of relations. Why relations?. Pieces of information are inherently related
E N D
CS1022Computer Programming & Principles Lecture 1 Relations
Plan of lecture • Motivation • Binary relations • Graphs to represent relations • Arrays to represent relations • Properties of relations • Closure of relations CS1022
Why relations? • Pieces of information are inherently related • People, addresses, age, what they have bought, etc. • When modelling information, we need to capture such relations: • Bob is the husband of Jane • Relation captured as an ordered pair (husband, wife), • An element of the Cartesian product of Male Female sets • Another scenario: students registered for courses • Set S = {Bob, Tony, ...} of students • Set C = {CS1022, CS1015, ...} of courses • Cartesian product S C = {(s, c) : s S, c C} • {(Bob,CS1022),(Bob,CS1015),(Bob, ...),(Tony,CS1022),(Tony,...)} • Very large set with all possible ways students can be registered CS1022
What’s important in relations? • Binary relations model any relationship between elements of two sets • Important: • To know how to represent relations • Properties we want to study in some relations • Equivalence and partial orders • Databases borrow extensively from sets & relations • Whether you will work in IT or not, you will encounter information and how it is gathered, stored and managed • You might have to help choosing information to gather CS1022
Binary relations • A binary relation between sets A and B is a subset R of the Cartesian product A B, R A B • When A B, we refer to R as a relation onA • Example: family tree • R = {(x, y) : x is a grandfather of y} • S = {(x, y) : x is a sister of y} Fred & Mavis John & Mary Alice Ken & Sue Mike Penny Jane Fiona Alan CS1022
Binary relations • A binary relation between sets A and B is a subset R of the Cartesian product A B, R A B • When A B, we refer to R as a relation onA • Example: family tree • R = {(x, y) : x is a grandfather of y} R = {(Fred, Jane), (Fred, Fiona), (Fred, Alan), (John, Jane), (John, Fiona), (John, Alan)} • S = {(x, y) : x is a sister of y} Fred & Mavis John & Mary Alice Ken & Sue Mike Penny Jane Fiona Alan CS1022
Binary relations • A binary relation between sets A and B is a subset R of the Cartesian product A B, R A B • When A B, we refer to R as a relation onA • Example: family tree • R = {(x, y) : x is a grandfather of y} R = {(Fred, Jane), (Fred, Fiona), (Fred, Alan), (John, Jane), (John, Fiona), (John, Alan)} • S = {(x, y) : x is a sister of y} S = {(Alice, Ken), (Sue, Mike), (Sue, Penny), (Penny, Sue), (Penny, Mike), (Jane, Fiona), (Jane, Alan), (Fiona, Jane), (Fiona, Alan)} Fred & Mavis John & Mary Alice Ken & Sue Mike Penny Jane Fiona Alan CS1022
Graphs to represent relations (1) • Let A and B be two finite sets • Let R A B be a relation between Aand B • We build a directed graph (or digraph): • For each (x, y) R, draw an arrow (or arc) from x to y • Notice that arrow has a direction: fromxto y • Example: Let A = {1, 3, 5, 7}, B = {2, 4, 6} • Relation V A B, V = {(x, y) : x y} • Has the following graph 1 7 3 5 2 4 6 CS1022
Graphs to represent relations (2) • Example: Let A = {1, 2, 3, 4, 5, 6} • Relation R A A, R = {(x, y) : x is a divisor of y} • R = {(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (2, 2), (2, 4), (2, 6), (3, 3), (3, 6), (4, 4), (5, 5), (6, 6)} • Graph: divisor m of an integer n is an integer m that can be multiplied by some other integer o to produce n – 3 is a divisor of 6 since 3*2 = 6. 1 5 6 2 3 4 CS1022
Graphs to represent relations (3) • Graphs: very popular way to model information • Examples: • Cities and roads connecting them (some one-way) • “Friends” in social networks • Computing networks in general • N.B.: many algorithms (with guarantees) for graphs • Find out if two nodes are (not) connected • Find out if there are “cliques” (isolated groups) • Find out “source” (a node from which all arrows leave) and “sinks” (nodes from which no arrows leave) CS1022
Arrays to represent relations (1) • Arrays (a kind of table) to represent graphs • First, we • Label elements of sets A and B • List their elements in a particular order: A = {a1i, a2j, , aqn} B = {b1k,b2l, , brm} • Notice: • We are not ordering sets – these are element positions • Whatever order, there will be an element 1, 2, etc. Recall the discussion about subscripts to distinguish elements in a set and superscripts to order those elements. CS1022
Arrays to represent relations (2) • Let A = {a1i, a2j, , aqn} B = {b1k,b2l, , brm} • Relation R A B: arrayM with q rows, r columns • Array M is called an q by r matrix • Rows labelled by elements of A (in chosen order) • Columns labelled by elements of B (in chosen order) • Entry in row i and column j is M(i, j) where: • M(i, j) = T (true) if, and only if, (ai, bj) R • M(i, j) = F (false) if, and only if, (ai, bj) R CS1022
Arrays to represent relations (3) • Example: A = {a1, e3, c5, g7}, B = {b2, f4, d6} • Relation V A B, V = {(x, y) : x y in the alphabet} 1 7 3 5 2 4 6 CS1022
Arrays to represent relations (4) • We could swap rows and columns around • We could use 0 and 1 instead of T and F • Important: • In some languages (e.g., Java, C, C++, C#) matrices are a standard way to manage collections • If you ever need to represent graphs in such languages, then use matrices/arrays CS1022
Infix notation for relations • If Ris a relation between two sets, we also write xRy whenever (x, y) R • xRy reads “x is R-related to y” • This is the “infix” notation to improve reading: xis_a_sister_ofy xis_a_divisor_ofy CS1022
Notation for relations: summary Relation Rbetween two finite sets can be described: • In words (using a suitable predicate) • As a set of ordered pairs • As a digraph (directed graph) • As a matrix Important: you are expected to • Describe relations in any representation • “Translate” between any two representations Question: how would you represent relations using your “pet” programming language? CS1022
Properties of relations (1) • We focus on relations on a single set A A relation R on a set A (using infix notation) is: • Reflexive when x ((x A) (x R x)) • Symmetric when xy ((x R y) (y R x)) • Antisymmetric when xy (((x, y A) and (x R y) and (y R x)) (x y)) • Transitive when xyz((x, y, z A) and ((x R y) and (y R z))) (x R z)) CS1022
Properties of relations (2) A relation R on a set A is (using pairs) is • Reflexive when x ((x A) ((x, x) R)) • Symmetric when xy (((x, y) R) ((y, x) R)) • Antisymmetric when xy (((x, y A) and ((x, y) R) and (x y)) ((y, x) R)) • Transitive when xyz(((x, y, z A) and ((x, y) R) and ((y, z) R)) ((x, z) R)) CS1022
Properties of relations (3) A relation R on a set A is (now using a graph) is • Reflexive if there is an arc from each vertex to itself • Symmetric if whenever there is an arc from x to y there is also an arc from y to x • Antisymmetric if whenever there is an arc from x to y and x y, there is no arc from y to x • Transitive if whenever there is an arc from x to y and an arc from y to z, then there is also an arc from x to z CS1022
Properties of relations (4) Suppose relation xhas the same age as y over People • It is reflexive as any x has the same age as itself • It is symmetric since xhas the same age asy means the same as yhas the same age asx • It is notantisymmetric because we can have many different people with the same age • It is transitive since if xhas the same age asy and yhas the same age asz, then xhas the same age asz "is taller than" has what properties? CS1022
Properties of relations (5) Antisymmetric? • has the same finger print as • has the same DNA as • has the same ID as CS1022
Properties of relations (6) You should be able to • Check if a relation has any of the properties • Proof: • By a counter-example showing property does not hold • Assume premises (general) and reach conclusion, to show the property does hold • We might need to assume properties hold: • E.g., algorithm only works if relation is transitive • Algorithm needs pre-processing to check this is the case CS1022
Closure of relations (1) • We can extend a relation R with more pairs with respect to a property or relations P • Example: what is the closure of the following ancestor with respect to transitivity? xyz(((x, y, z A) and ((x, y) R) and ((y, z) R)) ((x, z) R)) • Ancestor = {(bill, jill), (jill, kim), (phil,mary), (kim,phil), (mary,phil)} • Ancestor* = {(bill, jill), (jill, kim), (bill,kim), (phil,mary), (kim,phil), (jill,phil), (kim,mary), (bill, phil), (bill,mary), (mary,phil), (phil,jane), (bill,jane)} • Do we have all of them?? Do we have anything extra?? CS1022
Closure of relations (1) • We can extend a relation R with more pairs until a certain property P holds • If extended relation is smallest possible one, then • It is the closure of R with respect to property P • It is represented as R* • “Smallest possible”: only essential new pairs added • Formally, R* is the closure of Rw.r.t. property P if: • R* has property P • R R* • R* is a subset of any other relation that includes R and has property P CS1022
Computing closure of relations (1) • Let there be a relation R over A = {a1,a2, , an} and a property P CS1022
Summary You should now know: • Why we need relations • How to represent relations • Properties of relations • Closure of relations CS1022
Further reading • R. Haggarty. “Discrete Mathematics for Computing”. Pearson Education Ltd. 2002. (Chapter 3) • Wikipedia’s entry • Wikibooks entry CS1022