cs1022 computer programming & principles
DESCRIPTION
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 - PowerPoint PPT PresentationTRANSCRIPT
CS1022 Computer Programming &
Principles
Lecture 1Relations
Plan of lecture• Motivation• Binary relations• Graphs to represent relations• Arrays to represent relations• Properties of relations• Closure of relations
2CS1022
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
3CS1022
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
4CS1022
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 on A
• Example: family tree
– R = {(x, y) : x is a grandfather of y}
– S = {(x, y) : x is a sister of y}
5CS1022
Fred & Mavis
Alice Ken & Sue
John & Mary
Mike Penny
Jane Fiona Alan
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 on A
• 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}
6CS1022
Fred & Mavis
Alice Ken & Sue
John & Mary
Mike Penny
Jane Fiona Alan
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 on A
• 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)}
7CS1022
Fred & Mavis
Alice Ken & Sue
John & Mary
Mike Penny
Jane Fiona Alan
Graphs to represent relations (1)• Let A and B be two finite sets• Let R A B be a relation between A and 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: from x to 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
8CS1022
1
7
3
5
2
4
6
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:
9CS1022
1
5
6
2
3
4
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.
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)
10CS1022
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, a2
j, , aqn} B = {b1
k, b2l, , br
m}
• Notice:– We are not ordering sets – these are element positions– Whatever order, there will be an element 1, 2, etc.
11CS1022
Recall the discussion about subscripts to distinguish elements in a set and superscripts to order those elements.
Arrays to represent relations (2)• Let A = {a1
i, a2j, , aq
n} B = {b1k, b2
l, , brm}
• Relation R A B: array M with q rows, r columns– Array M is called a 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
12CS1022
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}
13CS1022
1
7
3
5
2
4
6
2 4 61 T T T3 F T F5 F T T7 F F F
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
14CS1022
Infix notation for relations• If R is a relation between two sets, we also write
x R y whenever (x, y) R• x R y reads “x is R-related to y”• This is the “infix” notation to improve reading:
x is_a_sister_of yx is_a_divisor_of y
15CS1022
Notation for relations: summaryRelation R between 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 matrixImportant: you are expected to• Describe relations in any representation• “Translate” between any two representationsQuestion: how would you represent relations using
your “pet” programming language?16CS1022
Properties of relations (1)• We focus on relations on a single set AA 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))
17CS1022
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 whenxyz (((x, y, z A) and ((x, y) R) and ((y, z) R))
((x, z) R))
18CS1022
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
19CS1022
Properties of relations (4)Suppose relation x has the same age as y over People• It is reflexive as any x has the same age as itself• It is symmetric since x has the same age as y means
the same as y has the same age as x• It is not antisymmetric because we can have many
different people with the same age• It is transitive since if x has the same age as y and y
has the same age as z, then x has the same age as z
20CS1022
"is taller than" has what properties?
Properties of relations (5)Antisymmetric?• has the same finger print as• has the same DNA as• has the same ID as• is parent of• is less than
21CS1022
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
22CS1022
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??
23CS1022
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 R w.r.t. property P if:
1. R* has property P2. R R* 3. R* is a subset of any other relation that includes R and
has property P
24CS1022
Computing closure of relations (1)• Let there be a relation R over A = {a1,a2, , an} and a
property P
25CS1022
{algorithm to compute closure of R A2 w.r.t. property P}begin
input R, A, P;while not (P(R) = true) do
select (x, y) A2, (x, y) R;R := R {(x, y)};
output Rend
• No guarantee that minimal R computed• Algorithm keeps adding pairs according to property
You should now know:• Why we need relations• How to represent relations• Properties of relations• Closure of relations
Summary
26CS1022
Further reading• R. Haggarty. “Discrete Mathematics for
Computing”. Pearson Education Ltd. 2002. (Chapter 3)
• Wikipedia’s entry• Wikibooks entry
27CS1022