lect_30

Upload: npnbkck

Post on 06-Jan-2016

2 views

Category:

Documents


0 download

DESCRIPTION

database

TRANSCRIPT

  • Relations I

    Margaret M. Fleck

    7 November 2008

    This lecture covers relations and basic properties of relations, which ismost of section 8.1 of Rosen.

    1 Announcements

    Model solutions for the second midterm will be posted as soon as we finishdoing makeup exams, which will probably be mid next week.

    2 Relations

    Suppose that A and B are sets. A relation from A to B is a subset of AB.That is, its a set of ordered pairs (a, b) where a A and b B.

    For example, suppose that A is a set of US states and B is the set of USpolitical parties. We could create a relation S that associates each state withthe parties of its senators. So S would contain the pair (Illinois, Democrat)since both of our senators are democrats. And it contains (Utah, Republican)because they have two Republican senators.

    Notice that S isnt a function. Iowa has one senator of each party. SoS must contain both the pairs (Iowa, Democrat) and (Iowa, Republican). Arelation from A to B can associate an element of A with multiple elements ofB, or no elements of B. For example, we could extend our set A to includethe District of Columbia, even though it doesnt have a senator.

    1

  • Relations are a very general construct, which unifies a wide range ofexamples that probably dont look very similar to you. In fact, much of thetime, we will be looking at one or another special type of relation.

    3 Functions and relations

    Functions are one special case of relations. For example, suppose we want toassociate each state with its capital. We can describe this as a relation F fromthe set of US states (A) to the set of US cities (C) So F is a set containingpairs like (Massachusetts, Boston) and (Iowa, Des Moines). In this case, therelation happens to associate each state with exactly one city. So we can alsodescribe C as a function from A to C.

    You can formally define a function from A to B as a relation from A toB such that each element of A is associated with exactly one element of B.

    Or you can think of a relation as a generalized type of function, whichallows missing or multiple outputs for certain inputs. Many mathematicalapplications use partial functions, which are functions that might not re-turn a value for every input. Similarly, many programming languages let youdefine procedures that do not return a value. Some programming languageseven let you define procedures that return multiple values.

    If a relation is a function, its usually more convenient to define it as afunction and use function notation. So, if we observe them in the wild, math-ematicians normally call something a relation only when it isnt a function,or they are worried that it might not turn out to be a function.

    4 Relations and databases

    The facts stored in a computer database are also relations. For this applica-tion, we need to generalize our notion of relation to more than two sets. Forexample, a simple registration database might contain 4-tuples like

    (Jean Luc Picard, Math CS 173, Fall 2325, A-)

    2

  • (James T. Kirk, CS 173, Spring 2248, B)

    (Kathryn Janeway, Math 347, Fall 2345, A+)

    So this database is a subset of A B C D, where A is the set ofstudents, B is the set of courses, C is the set of terms, and D is the set ofletter grades.

    Database records dont look at all like functions, because theres oftenno obvious way to identify one field as the input. The whole point of adatabase is to let us easily switch perspective and organize the data accordingto a different field, e.g. sort students by name or by grade.

    We wont make much use of n-ary relations in this class. If you are curiousabout database applications, look in section 8.2 of Rosen.

    5 Relations on a single set

    The final big group of relations are relations that associate two elementsof the same set. A relation R on a set A is a relation from A to A, orequivalently, a subset of AA. That is, R is a set of pairs of elements fromA.

    For example, suppose we let A = {2, 3, 4, 5, 6, 7} We can define a relationR on A by (x, y) R if and only if |x y| 2. This relation contains thepairs (3, 4) and (4, 6) and (6, 4), but not the pair (3, 6).

    Another relation on A is the familiar = relation. It contains only pairswhose two elements are identical, such as (5, 5) and (3, 3). Another similarrelation is congruence mod 3 (3). The 3 relation contains a wider rangeof pairs, e.g. (4, 7) and (6, 3). Relations like these, which resemble equality,are known as equivalence relations. Well get back to defining them preciselyin a couple lectures.

    Order relations such as < and are also relations on the set A. Forexample, the relation contains pairs like (5, 4) and (7, 2) and (5, 5).

    No one said that a relation needs to involve finite sets or even one-dimensional sets. We can define similar relations on infinite sets. For ex-ample, normal numerical is a relation on the set of integers or the set of

    3

  • real numbers. Another example: we can define a relation T on the real planeR

    2 in which (x, y) is related to (p, q) if and only if x2 + y2 = p2 + q2. In otherwords, two points are related if they are the same distance from the origin.

    6 Random relations

    No one said that a relation had to make sense or have any practical use.For example, lets let A = {a, b, c, d}. We can select any random subset ofA A to be a relation. For example, R = {(a, a), (b, a), (c, d), (d, d)} is aperfectly good relation on A. Wierdo relations like this are often easiest torepresent by making a table showing which pairs are in the set, or by drawinga dot-and-arrow diagram of the relation. [See pictures pp. 520521 of Rosen.Notice that we can do the dot-and-arrow diagram with either one copy ofthe points or two copies.]

    If a set A has n elements, how many possible relations are there on A?AA contains n2 elements. A relation is just a subset of AA, and so thereare 2n

    2

    relations on A. So a 3-element set has 29 = 512 possible relations.

    Yup: most of these relations are of no practical use whatsoever. Most ofthe time, you will be manipulating relations generated by some underlyingpattern and, thus, make more sense. Just be aware that these random guysare also legitimate relations.

    7 Properties of relations: reflexive

    Familiar relations such as = and < have certain special properties whichmake them especially useful, both in proofs and in practical applications.Moreover, some relations seem to be structurally similar: similar propertiesfor apparently similar reasons. For example, and and divides. We canmake these intuitions concrete by classifying relations according to certainkey properties.

    The commonly-used properties are: reflexive, irreflexive, symmetric, an-tisymmetric, and transitive. Relations that act like equality are all reflexive,symmetric, and transitive. Relations that act like < are all irreflexive, anti-

    4

  • symmetric, and transitive. And so forth.

    A relation R on a set A is reflexive if every element is related to itself.That is, for every x A, (x, x) R.

    For example, normal equality (on the integers, or on the reals, or on setsof integers, or whatever) is reflexive.

    Another example: let B contain sets of UIUC students. So B might con-tain {Hanna, Melissa} and {John, Marco, Oscar} and so forth. Lets define arelation R by saying that x and y are in R iff |x| = |y| (i.e x and y contain thesame number of people. R isnt equality, because two non-equal elements canbe related. For example, R contains ({Hanna, Melissa}, {Yunsook, Jaebum}).But R is reflexive, because every set of students is related to itself.

    Yet another example: the relation on B is reflexive, because every setis a subset of itself.

    8 Irreflexive

    A relation R on a set A is irreflexive if every element is not related to itself.That is, for every x A, (x, x) 6 R.

    For example, the < relation on the integers is irreflexive, because nointeger is less than itself.

    Notice that irreflexive is not the negation of reflexive. Lets look at thedefinitions more closely:

    reflexive: x A, (x, y) R

    not reflexive: x A, (x, y) 6 R.

    irreflexive: x A, (x, y) 6 R

    So we can have a relation which is neither reflexive nor irreflexive, if someelements are related to themselves and some arent. For example, lets definea relation S on the integers by (x, y) S if and only if x + y = 0. (2, 2) isntin S, but (0, 0) is.

    5

  • 9 Symmetric and antisymmetric

    Another important property of a relation is whether the order matters withineach pair. That is, if (x, y) is in R, is (y, x) always in R? A relation satisfyingthis property is called symmetric.

    Generalizations of equality are normally symmetric. For example, therelation R on the integers defined by (x, y) R iff |x| = |y|. Or the relationS on the real plane defined by ((x, y), (p, q)) S iff (x p)2 + (y q)2 25(i.e. the two points are no more than 5 units apart).

    A relation R is antisymmetric if (y, x) and (x, y) are never both in R,except when x = y. This is typically true of order relations such as < and ,or relations based on them. For example, the divides relation on the integersis antisymmetric because n|m and m|n are only both true when m = n.

    Formally, we can define them as:

    symmetric: x, y A, (x, y) R (y, x) R

    antisymmetric: x, y A with x 6= y, (x, y) R (y, x) 6 R

    Even if we restrict our attention to pairs whose two coordinates arentthe same, symmetric and antisymmetric still arent opposites. Rather, theyrepresent two pure patterns of what might happen with the reversed pairs.Most random relations and some real practical relations are mixed cases.

    Specifically a relation is neither symmetric nor antisymmetric if (x, y)and (y, x) are both in the relation for some values of x and y, but only oneis in the set for other values. For example, the loves relation is neithersymmetric nor antisymmetric for most large sets of people. There are somepairs of people who both love each other. But there are also some peoplewho love another person, but their love isnt reciprocated.

    6