03 Cis1103 Set Theory P3

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 35

CIS 1103

DISCRETE STRUCTURES 1

Set Theory:
 Computer Representation of Sets
 Cartesian Product
Set Theory:

Computer Representation of Sets


COMPUTER REPRESENTATION OF SETS: METHOD 1

 Store the elements of the set in an unordered fashion.

 Note:
o However, if this is done, the operations of
computing the union, intersection, or difference of
two sets would be time-consuming, because each of
these operations would require a large amount of
searching elements.
COMPUTER REPRESENTATION OF SETS: METHOD 2

 Store the elements of the set using an arbitrary


ordering of the elements of the universal set.

 This method of representing sets makes computing


combinations of sets easy.
COMPUTER REPRESENTATION OF SETS: RULES

 Assume that the universal set U is finite (and of reasonable size


so that the number of elements of U is not larger than the
memory size of the computer being used).

 First, specify an arbitrary ordering of the elements of U, for


instance, a1, a2, … , an.

 Represent a subset A of U with the bit strength of length n, where


the ith bit in this string is 1 if ai belongs to A and is 0 if ai does
not belong to A.
COMPUTER REPRESENTATION OF SETS: EXAMPLE
Given:
Let U = {1,2,3,4,5,6,7,8,9,10} and the ordering of elements
of U has the elements in increasing order; that is, ai = i.

Tasks:
Determine the bit strings that represent the following:
a) the subset (V) of all odd integers in U,
b) the subset (W) of all even integers in U, and
c) the subset (X) of integers not exceeding 5 in U.
COMPUTER REPRESENTATION OF SETS: EXAMPLE
 UNION & INTERSECTION:

Given:
Let U = {1,2,3,4,5,6,7,8,9,10}
Bit string for the set A = {1,2,3,4,5} = 1111100000
Bit string for the set B = {1,3,5,7,9} = 1010101010

Tasks:
Using the above bit strings,
a) find the bit string for the union (set C) of the given subsets;
b) find the bit string for the intersection (set D) of the given subsets.
COMPUTER REPRESENTATION OF SETS: EXAMPLE

 COMPLEMENT:

Given:
Let U = {1,2,3,4,5,6,7,8,9,10}
Bit string for the set E = {1,3,5,7,9} = 1010101010

Task:
Determine the bit string for the complement of
the given subset E.
COMPUTER REPRESENTATION OF SETS: IMPORTANT NOTE

 A better solution to computer representation of sets is:

o assigning a bit in a bit string to each element in the universal set

o and set the bit to 1 if the element is present

o otherwise use 0.
Set Theory:

Cartesian Product
CARTESIAN PRODUCT: INTRODUCTION

 The order of elements in a collection is often important.

 A different structure is needed to represent ordered


collections because sets are unordered.

 This is provided by ordered n-tuples.

 The ordered n-tuple (a1, a2, …, an) is the ordered


collection that has a1 as its first element, a2 as its second
element, …, and an as its nth element.
CARTESIAN PRODUCT: INTRODUCTION
 Cartesian Product is a mathematical operation that returns a
set (or product set or simply product) from multiple sets.

 It is the set of all ordered pairs (a, b), where a is an element


of A and b is an element of B that is called the Cartesian
product of A and B, and is denoted by A x B.

 Cartesian Products can be specified using set-builder notation


such as:

A x B = {(a, b) | a ∈ A and b ∈ B}
CARTESIAN PRODUCT: INTRODUCTION

 A table can be created by taking the Cartesian product of a set of


rows and a set of columns.

 If the Cartesian product rows x columns is taken, the cells of the


table contain ordered pairs of the form (row value, column value).
CARTESIAN PRODUCT: EXAMPLE 1

Given:
A = {x, y, z}
B = {1, 2, 3}

Task:
Create a table showing the Cartesian Product
of A and B as a set of rows and a set of columns
respectively.
CARTESIAN PRODUCT: TUPLE & n-TUPLE

 Tuple:
 It is a finite ordered list (sequence) of elements.

 n-Tuple:
 It is a sequence (or ordered list) of n elements, where n is
a non-negative integer.

 Note:
 There is only one 0-tuple, an empty sequence, or empty
tuple, as it is referred to.
CARTESIAN PRODUCT: ORDERED PAIR
 It is a pair of objects with an order associated with them.
 If objects are represented by a and b, then we write the ordered pair
as (a, b).
 Note (Equality of n-tuples):
 Two ordered n-tuples are equal if and only if each
corresponding pair of their elements is equal.
 In other words,
(a1, a2, . . . , an) = (b1, b2, . . . , bn)
if and only if ai = bi , for i = 1, 2, . . . , n.
CARTESIAN PRODUCT: ORDERED PAIR
 In particular, 2-tuples are called ordered pairs.

 The ordered pairs (a, b) and (c, d) are equal if and only if
a = c and b = d.

Example:
The ordered pair (1, 2) is not equal to the ordered pair (2, 1).

 Note:
 The ordered pairs (a, b) and (b, a) are not equal unless a = b.
 A x B ≠ B x A unless A = B
CARTESIAN PRODUCT: EXAMPLE 2

Given:

A = {1, 2}
B = {a, b, c}

Task:
Find the Cartesian product A x B.
CARTESIAN PRODUCT: EXAMPLE 3

Given: o Note:
A = {1, 2}
B = {a, b, c} Therefore:

Task: AxB≠BxA

Find the Cartesian product B x A.


CARTESIAN PRODUCT: EXAMPLE 4

Given:
A = {0,1}
B = {1,2}
C = {0,1,2}

Task:
Find the Cartesian product A x B x C.
CARTESIAN PRODUCT: RENÉ DESCARTES

 He was a French philosopher, mathematician,


and scientist.

 A native of the Kingdom of France, he spent


about 20 years of his life in the Dutch
Republic after serving for a while in the
Dutch States Army of Maurice of Nassau,
Prince of Orange and the Stadtholder of the
United Provinces.
SQL: INTRODUCTION

 Stands for Structured Query Language.

 A standard language for accessing and manipulating databases.

 Became a standard of the:


o American National Standards Institute (ANSI) in 1986
o International Organization for Standardization (ISO) in 1987
WHAT CAN SQL DO?
 SQL can execute queries against a database
 SQL can retrieve data from a database
 SQL can insert records in a database
 SQL can update records in a database
 SQL can delete records from a database
 SQL can create new databases
 SQL can create new tables in a database
 SQL can create stored procedures in a database
 SQL can create views in a database
 SQL can set permissions on tables, procedures, and views
SQL CROSS JOIN

 The SQL CROSS JOIN:


 produces a result set which is
• the number of rows in the first table
• multiplied by
• the number of rows in the second table
• if no WHERE clause is used along with CROSS JOIN.
SQL CROSS JOIN

 If WHERE clause is used with CROSS JOIN, it


functions like an INNER JOIN.

 An alternative way of achieving the same result is to


 use column names separated by commas after
SELECT and
 mentioning the table names involved, after a
FROM clause.
SQL CROSS JOIN: SYNTAX

SELECT [select_list]
FROM [table1]
CROSS JOIN [table2]

 Note:
o Some sources use semicolon after the SQL code
o Normally, semicolon is not used after the SQL code
PICTORIAL PRESENTATION OF CROSS JOIN
SQL CROSS JOIN

 In CROSS JOIN,
o each row from the 1st table
o joins with all the rows
o of another table.

 If:
o the 1st table contains x rows and
o the 2nd table contains y rows,
o the result set will x * y rows.
SQL CROSS JOIN: IMPORTANT NOTES

 Joined every row from the first table (T1) with every
row from the second table (T2)

 Returns a Cartesian product of rows from both tables


SQL CROSS JOIN : EXAMPLE

CROSS JOIN in SQL between TWO TABLES

Table1: foods Table2: company


SQL CROSS JOIN : EXAMPLE

 To get item name and item unit columns from foods table; company
name, company city columns from company table; and after a
CROSS JOINING with these mentioned tables, the following SQL
statement can be used:

SELECT foods.item_name, foods.item_unit,


company.company_name, company.company_city
FROM foods
CROSS JOIN company;
SQL CROSS JOIN : EXAMPLE

 To get item name and item unit columns from foods table; company
name, company city columns from company table; and after a
CROSS JOINING with these mentioned tables, the following SQL
statement can be used:

SELECT foods.item_name, foods.item_unit,


company.company_name, company.company_city
FROM foods, company;
SQL CROSS JOIN: EXAMPLE
SQL CROSS JOIN : OUTPUT PRESENTATION 1
SQL CROSS JOIN : OUTPUT PRESENTATION 2

You might also like