Presentation is loading. Please wait.

Presentation is loading. Please wait.

Discrete Structures for Computer Science

Similar presentations


Presentation on theme: "Discrete Structures for Computer Science"โ€” Presentation transcript:

1 Discrete Structures for Computer Science
Presented By:Andrew F. Conn Lecture #23: N-ary relations and Representations November 30th, 2016

2 Announcements This is the end of new material!!! Thank you for sticking it out with me. Please take your OMET evaluations. Also, a huge thanks to Dr. Lee whose material was the basis for this class.

3 Today n-ary relations Relation Representations Definitions
CS application: Relational DBMS Relation Representations Matrix Representation Directed Graphs

4 We can also โ€œrelateโ€ elements of more than two sets
Definition: Let ๐ด 1 , ๐ด 2 ,โ€ฆ, ๐ด ๐‘› be sets. An n-ary relation on these sets is a subset of ๐ด 1 ร— ๐ด 2 ร—โ€ฆร—๐ด. The sets ๐ด 1 , ๐ด 2 ,โ€ฆ, ๐ด ๐‘› are called the domains of the relation, and ๐‘› is its degree. Example: Let ๐‘… be the relation on โ„คร—โ„คร— โ„ค + consisting of triples ๐‘Ž, ๐‘, ๐‘š where ๐‘Žโ‰ก๐‘ (๐ฆ๐จ๐ ๐‘š). What is the degree of this relation? What are the domains of this relation? Are the following tuples in this relation? (8,2,3) (-1,9,5) (11,0,6)

5 N-ary relations are the basis of relational database management systems
Data is stored in relations (a.k.a., tables) Columns of a table represent the attributes of a relation Rows, or records, contain the actual data defining the relation Students Enrollment Name ID Major GPA Stud_ID Course Alice 334322 CS 3.45 Bob 546346 Math 3.23 Charlie 045628 2.75 Denise 964389 Art 4.0 334322 CS 441 Math 336 546346 Math 422 964389 Art 707 Ask about degree, domains

6 Operations on an RDBMS are formally defined in terms of a relational algebra
Relational algebra gives a formal semantics to the operations performed on a database by rigorously defining these operations in terms of manipulations on sets of tuples (i.e., records) Operators in relational algebra include: Selection Projection Rename Join Inner Join Outer join โ€ฆ Aggregation

7 The selection operator allows us to filter the rows in a table
Definition: Let ๐‘… be an n-ary relation and let ๐ถ be a condition that elements in ๐‘… must satisfy. The selection ๐‘  ๐‘ maps the n-ary relation ๐‘… to the n-ary relation of all n-tuples from ๐‘… that satisfy the condition ๐ถ. Example: Consider the Students relation from earlier in lecture. Let the condition ๐ถ1 be Major=โ€œCSโ€ and let ๐ถ2 be GPA > What is the result of ๐‘  ๐ถ1 โˆง ๐ถ2 (Students)? Answer: (Alice, , CS, 3.45) (Charlie, , CS, 2.75) Students Name ID Major GPA Alice 334322 CS 3.45 Bob 546346 Math 3.23 Charlie 045628 2.75 Denise 964389 Art 4.0

8 The projection operator allows us to consider only a subset of the columns of a table
Definition: The projection ๐‘ƒ ๐‘– 1 , ๐‘– 2 ,โ€ฆ, ๐‘– ๐‘š maps the n-tuple ( ๐‘Ž 1 , ๐‘Ž 2 , โ€ฆ, ๐‘Ž ๐‘› ) to the m-tuple ( ๐‘Ž ๐‘– 1 , ๐‘Ž ๐‘– 2 ,โ€ฆ, ๐‘Ž ๐‘– ๐‘š ) where ๐‘šโ‰ค๐‘› Example: What is the result of applying the projection ๐‘ƒ 1,3 to the Students table? Students Name ID Major GPA Alice 334322 CS 3.45 Bob 546346 Math 3.23 Charlie 045628 2.75 Denise 964389 Art 4.0 Name Major Alice CS Bob Math Charlie Denise Art

9 The join operator allows us to create a new table based on data from two or more related tables
Definition: Let ๐‘… be a relation of degree ๐‘š and ๐‘† be a relation of degree ๐‘›. The Join ๐ฝ ๐‘ ๐‘…,๐‘† , where ๐‘โ‰ค๐‘š and ๐‘โ‰ค๐‘›, creates a new relation of degree ๐‘š+๐‘›โˆ’๐‘ containing the subset of ๐‘†ร—๐‘… in which elements from column ๐‘ match. Example: What is the result of the join ๐ฝ 2 (Students,Enrollments)? Students Name ID Major GPA Alice 334322 CS 3.45 Bob 546346 Math 3.23 Charlie 045628 2.75 Denise 964389 Art 4.0 Enrollment Stud_ID Course 334322 CS 441 Math 336 546346 Math 422 964389 Art 707 NOTE: This syntax different than in book

10 What is the result of the join ๐ฝ 2 Students,Enrollments ?
Name ID Major GPA Alice 334322 CS 3.45 Bob 546346 Math 3.23 Charlie 045628 2.75 Denise 964389 Art 4.0 Enrollment Stud_ID Course 334322 CS 441 Math 336 546346 Math 422 964389 Art 707 ๐‘=2 Corresponding Column in ๐‘† Name ID Major GPA Course Alice 334322 CS 3.45 CS 441 Math 336 Bob 546346 Math 3.23 Math 422 Denise 964389 Art 4.0 Art 707

11 SQL queries correspond to statements in relational algebra
Students Name ID Major GPA Alice 334322 CS 3.45 Bob 546346 Math 3.23 Charlie 045628 2.75 Denise 964389 Art 4.0 Name ID Alice 334322 Charlie 045628 SELECT is actually a projection (in this case, ๐‘ท ๐Ÿ,๐Ÿ ) SELECT Name, ID FROM Students WHERE Major = โ€œCSโ€ AND GPA > 2.5 The WHERE clause lets us filter (in this case, ๐‘บ ๐Œ๐š๐ฃ๐จ๐ซ="๐‘ช๐‘บ" โˆง ๐‘ฎ๐‘ท๐‘จ>๐Ÿ.๐Ÿ“ )

12 SQL: An Join Example Students Name ID Major GPA Alice 334322 CS 3.45 Bob 546346 Math 3.23 Charlie 045628 2.75 Denise 964389 Art 4.0 Enrollment Stud_ID Course 334322 CS 441 Math 336 546346 Math 422 964389 Art 707 SELECT Name, ID, Major, GPA, Course FROM Students JOIN Enrollment ON ID = Stud_ID Name ID Major GPA Course Alice 334322 CS 3.45 CS 441 Math 336 Bob 546346 Math 3.23 Math 422 Denise 964389 Art 4.0 Art 707

13 Group Work! Students Name ID Major GPA Alice 334322 CS 3.45 Bob 546346 Math 3.23 Charlie 045628 2.75 Denise 964389 Art 4.0 Enrollment Stud_ID Course 334322 CS 441 Math 336 546346 Math 422 964389 Art 707 Problem 1: What is ๐‘ƒ 1,4 Students ? Problem 2: What relational operators would you use to generate a table containing only the names of Math and CS majors with a GPA > 3.0? Problem 3: Write an SQL statement corresponding to the solution to problem 2. Student/GPA S_{(Major = CS OR Major = Math) AND GPA > 3.0} P_1 Students SELECT Name FROM Students WHERE (Major = CS OR Major = Math) AND GPA > 3.0

14 Representations of Binary Relations
Weโ€™ve already learned that Binary Relations can be represented with tables, but letโ€™s formalize that. A Relation between finite sets can be represented as a zero-one matrix. Let ๐‘… be a relation from ๐ด to ๐ต. Then the matrix that represents ๐‘… is defined by: ๐‘š ๐‘–,๐‘— = 1, ๐‘–๐‘“ ๐‘Ž ๐‘– , ๐‘ ๐‘– โˆˆ๐‘… 0, ๐‘–๐‘“ ๐‘Ž ๐‘– , ๐‘ ๐‘– โˆ‰๐‘… We label the resulting matrix ๐‘€ ๐‘… .

15 Example Relations and Matrices
๐‘… 1 = 2,1 , 3,1 , 3,2 ๐‘€ ๐‘… 1 = ๐‘€ ๐‘… 2 = ๐‘… 2 = ๐‘Ž 1 , ๐‘ 2 , ๐‘Ž 2 , ๐‘ 1 , ๐‘Ž 2 , ๐‘ 3 , ๐‘Ž 2 , ๐‘ 4 , ๐‘Ž 3 , ๐‘ 1 , ๐‘Ž 3 , ๐‘ 3 , ๐‘Ž 3 , ๐‘ 5

16 Final Thoughts Next time: Wrap up and Exam Review!
Relations allow us to represent and reason about the relationships between sets in a more general way than functions did Without n-ary relations, DBMS systems would not exist! Do you find this stuff interesting? CS 1555: Introduction to Database Systems CS 1571: Introduction to Artificial Intelligence Next time: Wrap up and Exam Review!


Download ppt "Discrete Structures for Computer Science"

Similar presentations


Ads by Google