Relations Relations As Sets Computing is the science

  • Slides: 17
Download presentation
Relations

Relations

Relations As Sets • Computing is the science (and art) of processing information. –

Relations As Sets • Computing is the science (and art) of processing information. – A major concern is the relationship between different pieces of information. à Databases. • Examples of relations : – John has savings account number 333. – The File menu contains the exit option. – The root directory is the parent of the directory tree. • A binary relation can be represented as a set of pairs. – N-ary relations are simply an extension of this. 5. 2

Relations in Computing • Something like 80% of the software systems in existence involve

Relations in Computing • Something like 80% of the software systems in existence involve databases. The vast majority of the databases in existence are based on relations : Relational Databases. There is a whole programming paradigm based on relations : the Relational programming languages. (PROLOG) Relational database query languages (SQL etc. ) are Relational languages. 5. 3

Source And Target • Assume we have a set : Transport = { Car,

Source And Target • Assume we have a set : Transport = { Car, Bike, Horse, Plane } • The owns relation is a subset of People x Transport. owns = { (Dick, Bike), (Joe, Car), (Mary, Car) } • In this case People is the source set of the relation owns and Transport is the target set of the relation owns. • Note that not all the elements of either source or target sets needs to occur in the actual relation. – The relation may be partial. ( x Transport (Stan, x) owns) x Transport (Stan, x) owns 5. 4

Domain And Range • The set of elements which occur as the first of

Domain And Range • The set of elements which occur as the first of the pairs in a relation is called the domain (dom) of the relation. • The set of elements which occur as the second of the pairs in a relation is called the range (ran) of the relation. dom(owns) = { Dick, Joe, Mary } ran(owns) = { Bike, Car } • As a pretty picture : Mary Bike Dick Car Stan Joe Plane 5. 5

A relation is a set of ordered pairs. The domain is the set of

A relation is a set of ordered pairs. The domain is the set of all x values in the relation domain = {-1, 0, 2, 4, 9} These are the x values written in a set from smallest to largest {(2, 3), (-1, 5), (4, -2), (9, 9), (0, -6)} These are the y values written in a set from smallest to largest range = {-6, -2, 3, 5, 9} The range is the set of all y values in the relation Relations are theoretical basis for Relational programming languages such as Prolog and SQL. This is a relation

A relation assigns the x’s with y’s 1 2 2 4 3 6 4

A relation assigns the x’s with y’s 1 2 2 4 3 6 4 8 5 10 Domain (set of all x’s) Range (set of all y’s) This relation can be written {(1, 6), (2, 2), (3, 4), (4, 8), (5, 10)}

Relational Inverse • The inverse of a relation is obtained by swapping round the

Relational Inverse • The inverse of a relation is obtained by swapping round the components of each pair. R~ = { (1, 0), (2, 1), (5, 3) } S~ = { (2, 1), (5, 3) } • Mathematically, we can write S~ { (x, y) S (y, x) } 5. 8

Relational Composition • The composition of R with S is equal to the set

Relational Composition • The composition of R with S is equal to the set of all those ordered pairs (x, z) such that we can find at least one value, say y, such that x. Ry and y. Sz. • That is R ; S { (x, y) R, (y, z) S (x, z) } • As usual, an alternative notation is sometimes used. Instead of R ; S we write S 1 thing. 0 1 3 • So R to mean the same 2 3 5 1 5 ; S = { (0, 1), (1, 1) }. 5. 9

Relational Override • This is essentially an update operation. • The override of R

Relational Override • This is essentially an update operation. • The override of R by S is denoted R S by adding to S all those ordered pairs from R whose first component is not in the domain of S. R S { (x, y) R x dom S } • This sounds a lot more complex than it is. Lets have an example. R = { (1, 2), (3, 4), (2, 3) } S = { (1, 6), (4, 5), (2, 7) } R S = { (1, 6), (4, 5), (2, 7), (3, 4) } • A good way to think of this is that R is the master database and S is a set of new records to be added, some of which update the data in already existing records. 5. 10

Diagrammatic Representations • Relations are often best understood when represented by a pretty picture.

Diagrammatic Representations • Relations are often best understood when represented by a pretty picture. – A directed graph or digraph. R (2, 5), = { (1, 2), (2, 1), (2, 2), (2, 4), (3, 1), (3, 5), (4, 5), (5, 5) } • The following diagraph represents R : 4 5 2 1 3 5. 11

Diagrammatic Representations II • Given R = { (1, 2), (4, 3), (1, 3),

Diagrammatic Representations II • Given R = { (1, 2), (4, 3), (1, 3), (4, 1), (3, 2), (3, 3), (4, 5) } • The following diagraph represents R : 1 2 3 4 5. 12

Types Of Relations • An homogeneous relation is a binary relation in which the

Types Of Relations • An homogeneous relation is a binary relation in which the source and target sets are identical. R Names • Useful for things like – A family tree (parent, ancestor, married to etc. ) – Traveling problems (bus, rail and road connections etc. ) • An identity relation is a binary relation in which every element of a set is related to itself and only to itself. Ix = {x X (x, x) } • For example if X = { 0, 1, 2 } then Ix = { (0, 0), (1, 1), (2, 2) } 5. 13

Types Of Homogenous Relations • Can R = R ~? – Obviously, only for

Types Of Homogenous Relations • Can R = R ~? – Obviously, only for homogeneous relations. – Such a relation is called symmetric. – Given a relation R on the set X, for a symmetric relation : x, y X (x, y) R (y, x) R • Reflexive relations : every element of the domain is related to itself. Ix R – Not quite the same as a identity relation. – There may be other pairs as well. For example : R = { (1, 1), (2, 2), (1, 5), (2, 3) } 5. 14

Types Of Homogenous Relations II • If whenever a is related to b and

Types Of Homogenous Relations II • If whenever a is related to b and b is related to c then a is related to c then the relation is transitive. R ; R R • If a relation is reflexive, symmetric and transitive then it is called an equivalence relation. • Given the relation R on the set { 1, 2, 3 } R = { (1, 1), (1, 2), (2, 1), (2, 2), (3, 3) } • Is this relation : – – Reflexive? Symmetric? Transitive? An equivalence relation? 5. 15

5. 16

5. 16

5. 17

5. 17