Download presentation
Presentation is loading. Please wait.
Published byAnnabella Greene Modified over 9 years ago
1
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Relations
2
Definitions & Notation –A binary relation from A to B is a subset of A x B –A binary relation on A is a subset of A x A –A binary relation is defined by Enumerating elements Relations definition: x r y x + y is odd –Binary relations can be one-to-one one-to-many many-to-one many-to-many –An n-ary relation on S 1,S 2,…,S n is a subset of S 1 x S 2 x S 3 x … x S n S i are called the domains n is called the degree
3
Properties of Relations –Let r be a binary relation on set S PropertyDefinition reflexive x(x S (x,x) r) symmetric x y(x S y S (x,y) r (y,x) r ) transitive x y z(x S y S z S (x,y) r (y,z) r (x,z) r) antisymmetric x y(x S y S (x,y) r (y,x) r x = y) Don’t confuse antisymmetric with “not symmetric”! Likewise irreflexive and “not reflexive”
4
Relations built from Relations –Closure Definition: A binary relation r* on a set S is the closure of a relation r on S with respect to property p if –r* has property p –r r* –r* is a subset of every other relation on S that includes r and has property p. –Composite Definition: Let r be a relation from A to B and s be a relation from B to C. The composite (r s) is the relation consisting of ordered pairs (a,c) where a A, c C, and for which there exists an element b B such that (a,b) r and (b,c) s. Definition: Let r be a relation on set A. The powers r n, n = 1,2, … are define recursively by –r 1 = r –r i+1 = r i r
5
Types of Relations –Partial Ordering Definition: A relation r on a set S is a partial ordering if it is reflexive, antisymmetric, and transitive. –(S, r) is called a partially ordered set or poset –The elements a and b of a poset (S, r) are called comparable if either (a,b) r or (b,a) r –Strict Partial Ordering Definition: A relation r on a set S is a strict partial ordering if it is irreflexive, antisymmetric, and transitive. –Total Ordering Definition: A relation r on a set S is a total ordering if it is (S,r) is a poset and every two elements of S are comparable. –Strict Total Ordering Definition: A relation r on a set S is a strict total ordering if it is (S,r) is a strict poset and every two elements of S are comparable.
6
Types of Relations –Equivalence Relations Definition: A relation r on a set S is an equivalence relation if it is reflexive, symmetric, and transitive. –Two elements related by an equivalence relation are said to be equivalent –The set of all elements that are related to an element a of S is called the equivalence class of a. –A partition of a set is a collection of disjoint nonempty subsets of S such that they have S as their union. The equivalence classes of r form a partition of S.
7
Practice Problems Mathematical Structures for Computer Science Pg. 301 # 6, 9, 12, 13, 15, 17, 19, 30, 42
8
Application: Relation Representation –Enumeration list the ordered pairs –Zero-One Matrix Suppose r is a relation from A {a 1,a 2,…,a m } to B {b 1,b 2,…,b n } r can be represented by matrix M r = [m ij ] where –Digraph A relation r on a set S is represented by a directed graph (digraph) that has the elements of S as it vertices and the ordered pairs (a,b) where (a,b) r, as edges. –So how do we represent digraphs in a computer? Later…
9
Application: Transitive Closure –The transitive closure of a binary relation r on the set S is the smallest transitive relation r* on S that contains r. –r* is transitive –r r* –r* is a subset of any other relation S that includes r and is transitive –Transitive closures are particularly interesting in that they provide “connection” information –Is it possible to travel from city X to city Y? –Algorithm to find transitive closure? –Example: S = {1, 2, 3, 4} r = {(1,4), (2,1), (2,3), (3,1), (3,4), (4,3)} 1 3 24
10
Application: Transitive Closure –Transitive closure finding a path in the relation graph between two points –Use an algorithm we already have to solve: –Shortest Path – Dynamic Programming –Stephen Warshall circa 1960 –Conversion: Given a relation, create a graph, G = (V,E) Given shortest paths, add ordered pairs to relation
11
Conversion Example –Set S = {1, 2, 3, 4} –Relation r = {(1,4), (2,1),(2,3),(3,1),(3,4),(4,1)} –Graph: –Transitive Closure: {(1,1), (1,2), (1,3), (2,1), (2,3), (2,4), (3,1), (3,3), (3,4), (4,1), (4,3), (4,4)} v1v1 v4v4 v2v2 v3v3 1 1 11 1 1
12
Application: Relational Databases –Entity-Relationship Model Relational Model Both “Entity Sets” and “Relations” are relations in the mathematical sense Relations described using tables –No duplicates and No order –Table values: a subset of D 1 D 2 … D n where D i is the domain from which attribute A i takes its value –A table is an n-ary relation on D i s –Relationship types One-to-one One-to-many Many-to-one Many-to-many
13
Application: Relational Databases –Operations on Relations restrict –Let r be an n-ary relation and c a condition that elements of r must satisfy. Then the restrict operator r c maps the n-ary relation r to the n-ary relation of all n-tuples from r that satisfy the condition c. –leads to the SQL “where” clause project –The projection P i1,i2,…,im maps the n-tuple (a 1,a 2, …,a n ) to the m-tuple (a i1,a i2,…,a im ) where m n. –leads to the SQL “select” clause join –Let r be a relation of degree m and s a relation of degree n. The join j p (r,s), where p m and p n, is a relation of degree m + n – p that consists of all (m + n – p)-tuples (a 1,a 2,…,a m- p,c 1,c 2,…,c p,b 1,b 2,…,b n-p ) where the m-tuple (a 1,a 2,..,a m- p,c 1,c 2,…,c p ) r and the n-tuple (c 1,c 2,…,c p,b 1,b 2,…,b n-p ) s. –leads to the SQL “from a,b,…,c” clause
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.